This is an archive of the discontinued LLVM Phabricator instance.

[llvm-objcopy] Rename relocation sections together with their targets.
ClosedPublic

Authored by ikudrin on Sep 23 2021, 10:48 AM.

Details

Summary

As for now, llvm-objcopy renames only sections that are specified explicitly in --rename-section, while GNU objcopy keeps names of relocation sections in sync with their targets. For example:

> readelf -S test.o
...
  [ 1] .foo      PROGBITS
  [ 2] .rela.foo RELA

> objcopy --rename-section .foo=.bar test.o gnu.o
> readelf -S gnu.o
...
  [ 1] .bar      PROGBITS
  [ 2] .rela.bar RELA

> llvm-objcopy --rename-section .foo=.bar test.o llvm.o
> readelf -S llvm.o
...
  [ 1] .bar      PROGBITS
  [ 2] .rela.foo RELA

This patch makes llvm-objcopy to match the behavior of GNU objcopy better.

Diff Detail

Event Timeline

ikudrin created this revision.Sep 23 2021, 10:48 AM
MaskRay added inline comments.Sep 23 2021, 1:06 PM
llvm/test/tools/llvm-objcopy/ELF/rename-section-relocsec.test
2

The two tests can be combined.

One file with different cases is sometimes easier to comprehend than having multiple files.

Is there a test renaming non-SHF_ALLOC relocation section?

llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp
596

You can omit SHT_REL[A] since the list will be incomplete. We may have [[ https://sourceware.org/bugzilla/show_bug.cgi?id=27924 | SHT_RELR ]] in the future :)

617–618

according to?

ikudrin updated this revision to Diff 374725.Sep 23 2021, 9:37 PM
ikudrin marked 2 inline comments as done.

Thanks for the review!

  • Combined tests
  • Updated comments
llvm/test/tools/llvm-objcopy/ELF/rename-section-relocsec.test
2

Is there a test renaming non-SHF_ALLOC relocation section?

There are no tests for these cases because I didn't have the intention to fasten the behavior. Previously, llvm-objcopy allowed such sections to be renamed with an explicit --rename-section. GNU objcopy and the patched llvm-objcopy ignore the setting. If someone ever finds that important, they should be free to improve the implementation in whatever way.

The llvm-objcopy design goal was to be behave like GNU objcopy, as far as GNU objcopy's behaviour made sense. I think renaming relocation sections like this implicitly does indeed make sense. However, I don't think think the dynamic/non-dynamic distinction makes much sense to me. I personally think the behaviour should be:

  1. If a section is renamed, a relocation section targeting it will be renamed, if not explicitly named in another --rename-section command.
  2. If a relocation section is specified by --rename-section, it will be renamed accordingly.
  3. Relocation sections whose targets are not renamed are not renamed themselves, even if the names are already different (this ensures changes via point 2 above aren't lost).
llvm/test/tools/llvm-objcopy/ELF/rename-section-relocsec.test
2

I have some disagreements about the behaviour in this patch (see out-of-line comment), but if we do stick with the behaviour you've proposed, I think we should test some additional cases:

  1. Dynamic relocations are not implicitly renamed when their target sections are.
  2. Non-dynamic relocation sections which target a section with an unrelated name, are renamed if their target section is renamed (i.e. show the behaviour is driven by the target section link, not some link based on name).
  3. Non-dynamic relocaction sections which target a section with an unrelated name, are not renamed if the target section is not renamed.
  4. Non-dynamic relocation sections which target a section with a different name, but which have a common name with an unrelated section, are not renamed if the unrelated section is renamed (i.e. again show that the sh_info value is what's important, not the name).

Some of these points are applicable even if we adopt the behaviour I'm suggesting in my out-of-line comment.

7–10

This wording sounds a bit more natural to me.

llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp
618

I think this could be a little more explicit, and avoid ambiguity about "targets"

ikudrin updated this revision to Diff 374886.Sep 24 2021, 10:04 AM
ikudrin marked 3 inline comments as done.

Thanks for the suggestions!

I've tried to integrate all of them, except for ignoring SHF_ALLOC. We should not automatically rename dynamic relocation sections because their names may not follow the typical pattern, e.g. .rela.plt targets .got.plt, and renaming them together seems wrong.

jhenderson added inline comments.Sep 27 2021, 12:29 AM
llvm/test/tools/llvm-objcopy/ELF/rename-section-relocsec.test
2

One more test case, I think we need is - a relocation section whose prefix doesn't match its type (or indeed which doesn't have a prefix), so e.g. ".rela.foo" is a SHT_REL section, and ".baz" is a SHT_RELA section etc.

5–6

I'm not going to insist on this, but I have a strong preference for writing the output file to disk, and then feeding that file to llvm-readobj. The reason is because it makes test debugging significantly easier (if something fails, you can just go and inspect the file on disk, without having to modify the test to create the object).

13–14

No strong opinion on this, so it's fine if you prefer it this way, but did you consider having just one llvm-objcopy execution that tests all the test cases? If you did that, I'd name the sections so that they describe the case they correspond to.

23–24

I think the final "related" should be "renamed"?

34

Maybe rename one of these to something that doesn't have ".rel" or ".rela" as a prefix?

43–44

This might just be because of my fairly small laptop screen, but it might be better if this was split over a third line, to reduce the width.

55–56

By extending one of the other checks, you can avoid adding another llvm-objcopy/llvm-readobj execution, since this case is also exercised by the invocation for e.g. TEST1.

64
80

There's quite a big gap between the key and value, without it adding anything. I think here and below you should remove the excessive spacing - it makes things a little easier for me to read, I find. The approach I follow, is to keep things vertically lined up within a block, such that there is one space between the longest key and its value.

87

Here and below, with the execption of the .rela.plt section, I don't think the flags are relevant, so I'd remove them to remove noise from the patch.

llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp
604–608
611

Nit: I'd be tempted to put a blank line before this comment.

613

What happens if the sh_link field of the relocation section is 0/invalid? Will this blow up?

ikudrin updated this revision to Diff 375257.Sep 27 2021, 7:55 AM
ikudrin marked 12 inline comments as done.

Thank you for the review, @jhenderson!

  • The test is rewritten so that llvm-objcopy runs only once.
  • An intermediate file is kept.
llvm/test/tools/llvm-objcopy/ELF/rename-section-relocsec.test
2

Please, take a look at Test 2 in the updated test file, where the relocation section is called .tst2.foo.rel. Does that match your suggestion?

34

Done in the Test 4 in the update test file. .rel.tst4.foo is renamed to .tst4.ren.foo.rel.

55–56

In the updated test file, there are no --rename-section commands for .tst5 sections.

llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp
604–608

Thanks! I have to be more attentive...

613

You probably meant sh_info, not sh_link? In that case, getSection() will just return nullptr, which is not in the set, so find() will return RenamedSections.end().

ikudrin updated this revision to Diff 375541.Sep 28 2021, 5:41 AM
  • Fix a formatting issue
jhenderson added inline comments.Sep 29 2021, 12:29 AM
llvm/test/tools/llvm-objcopy/ELF/rename-section-relocsec.test
2

Thanks, yes that works for me.

12

Add something to prevent prefix-only matches with FileCheck. At the moment, a name ".rel.tst1.ren.foo.bar" would match the second CHECK line in the first test case, which isn't the intent. I'd suggest either adding --match-full-lines to the FileCheck run, or adding {{ }} (or {{$}} if it's the line end - can't remember if there's anything after the name) to the end of each name.

llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp
613

Yeah, I meant sh_info, thanks.

ikudrin updated this revision to Diff 375801.Sep 29 2021, 1:00 AM
ikudrin marked an inline comment as done.
  • Add {{ }} in checks
This revision is now accepted and ready to land.Sep 29 2021, 1:11 AM
This revision was landed with ongoing or failed builds.Sep 29 2021, 2:36 AM
This revision was automatically updated to reflect the committed changes.