This is an archive of the discontinued LLVM Phabricator instance.

[llvm-objcopy][ELF] Avoid reordering section headers
ClosedPublic

Authored by ikudrin on Aug 6 2021, 8:28 AM.

Details

Summary

As for now, llvm-objcopy sorts section headers according to the offsets of the sections in the input file. That can corrupt section references in the dynamic symbol table because it is a loadable section and as such is not updated by the tool. Even though the section references are not required for loading the binary correctly, they are still handy for a user who analyzes the file.

While the patch removes global reordering of section headers, it layouts the sections in the same way as before, i.e. according to their original offsets. All that helps the output file to resemble the input better.

Note that the patch removes sorting SHT_GROUP sections to the start of the list, which was introduced in D62620 in order to ensure that they come before the group members, along with the corresponding test. The original issue was caused by the sorting of section headers, so dropping the sorting also resolves the issue.

Diff Detail

Event Timeline

ikudrin created this revision.Aug 6 2021, 8:28 AM
ikudrin requested review of this revision.Aug 6 2021, 8:28 AM
ikudrin updated this revision to Diff 365165.Aug 9 2021, 5:24 AM
ikudrin edited the summary of this revision. (Show Details)
  • When debug sections are compressed or decompressed, place the new sections at the positions of removed ones. That fixes failures in compress-debug-sections-zlib.test and compress-debug-sections-zlib-gnu.test.
ikudrin updated this revision to Diff 365353.Aug 9 2021, 9:40 PM
  • Fixed a code formatting issue

D62620 was related to section groups: "The section header table entry for a group section must appear in the section header table before the entries for any of the sections that are members of the group."
(ld.lld has dropped the limitation)

That can corrupt section references in the dynamic symbol table because it is a loadable section and as such is not updated by the tool. Even though the section references are not required for loading the binary correctly, they are still handy for a user who analyzes the file.

All dynamic loaders I know only use 3 categories:

  • SHN_UNDEF
  • SHN_ABS
  • other values

There are no behavior differences if an index of 1 is replaced with 2, but I agree that preserving the section index and making the output as close to the input as possible will be nice.

The approach looks good at a glance, but I'll need to look more closely tomorrow.

llvm/test/tools/llvm-objcopy/ELF/dynsym-valid-refs.test
56

Add a comment that dummy is before .text by intention even if its offset is larger.

ikudrin updated this revision to Diff 365363.Aug 10 2021, 2:19 AM
ikudrin marked an inline comment as done.
ikudrin edited the summary of this revision. (Show Details)
  • Added a comment that 'dummy' comes before '.text' by intention.

D62620 was related to section groups: "The section header table entry for a group section must appear in the section header table before the entries for any of the sections that are members of the group."
(ld.lld has dropped the limitation)

Right, D62620 enforces SHT_GROUP sections to be moved at the start of the table. That was required because the tool sorted section headers and, without the fix, could place such sections in the table after members of their groups. This patch drops sorting, so there is no need to do anything special for group sections.

That can corrupt section references in the dynamic symbol table because it is a loadable section and as such is not updated by the tool. Even though the section references are not required for loading the binary correctly, they are still handy for a user who analyzes the file.

All dynamic loaders I know only use 3 categories:

  • SHN_UNDEF
  • SHN_ABS
  • other values

There are no behavior differences if an index of 1 is replaced with 2, but I agree that preserving the section index and making the output as close to the input as possible will be nice.

My point exactly. Skewed section references can bewilder a person, but not a dynamic loader.

The approach looks good at a glance, but I'll need to look more closely tomorrow.

Thanks!

Looks good.

llvm/tools/llvm-objcopy/ELF/Object.cpp
2317

No test fails if I comment out this stable_sort .

You may need a --only-keep-debug test.

ikudrin updated this revision to Diff 365717.Aug 11 2021, 4:54 AM
ikudrin marked an inline comment as done.
ikudrin edited the summary of this revision. (Show Details)
  • Added a test for --only-keep-debug
  • Moved updating references for replaced sections from replaceDebugSections() to Object::replaceSections()
MaskRay accepted this revision.Aug 11 2021, 8:36 AM

LGTM.

This revision is now accepted and ready to land.Aug 11 2021, 8:36 AM
This revision was automatically updated to reflect the committed changes.
llvm/test/tools/llvm-objcopy/ELF/dynsym-valid-refs.test