This is an archive of the discontinued LLVM Phabricator instance.

[LLD] [COFF] Order .debug_* sections at the end, to avoid leaving gaps if stripped
ClosedPublic

Authored by mstorsjo on Mar 2 2022, 2:50 AM.

Details

Summary

So far, we sort all discardable sections at the end, with only some
extra logic to make sure that the .reloc section is at the start
of that group of sections. But if there are other discardable
sections, other than .reloc, they must also be ordered before
.debug_* sections, to avoid leaving gaps if the executable is
stripped.

(Stripping executables doesn't remove all discardable sections,
only the ones named .debug_*).

Rust binaries seem to include a .rmeta section, which is marked
discardable. This fixes stripping such binaries if built with
dwarf debug info included.

This fixes issues observed in MSYS2 in
https://github.com/msys2/MINGW-packages/pull/10555.

Diff Detail

Event Timeline

mstorsjo created this revision.Mar 2 2022, 2:50 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 2 2022, 2:50 AM
mstorsjo requested review of this revision.Mar 2 2022, 2:50 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 2 2022, 2:50 AM

Other earlier changes in the same area can be found here:

https://github.com/llvm/llvm-project/commit/98ff9f845ded3df23b5c0a6386e3bba96d9bb557 / D49351 - "Sort .reloc before all other discardable sections"
https://github.com/llvm/llvm-project/commit/13ac40ea6ef9621838068a70cbef86d544ee9700 / D11655 - "Sort output sections which start with .debug to the end of the file"

(Despite the commit subject in the latter of those, it only looked at the IMAGE_SCN_MEM_DISCARDABLE flag.)

Also, see https://github.com/llvm/llvm-project/blob/main/llvm/lib/ObjCopy/COFF/COFFObjcopy.cpp#L142-L147 (plus line 31) as reference to what stripping does here - llvm-objcopy matches GNU objcopy in this aspect.

rnk accepted this revision.Mar 2 2022, 10:27 AM

lgtm

This revision is now accepted and ready to land.Mar 2 2022, 10:27 AM
mati865 accepted this revision.Mar 2 2022, 4:11 PM

LGTM

Rebuilt Rust with this patch applied to host LLVM and it still works after stripping binaries and libraries.

LGTM

Rebuilt Rust with this patch applied to host LLVM and it still works after stripping binaries and libraries.

Thanks for testing!