This is an archive of the discontinued LLVM Phabricator instance.

[ELF] - Fixed linkage error when using -g --gc-sections together.
ClosedPublic

Authored by grimar on Sep 27 2016, 8:26 AM.

Details

Summary

r282444 introduced new issue, sample program below
fails to link on

assert(Piece.Live);
int main() { return 0; }

clang test.cpp -c -o out.o -g
ld.lld -flavor gnu --gc-sections out.o -o out

Problem is that .debug_info contains relocations to .debug_str:
Section (7) .rela.debug_info {
..

0xC R_X86_64_32 .debug_str 0x0
0x12 R_X86_64_32 .debug_str 0x37

..
But we do not preserve .debug_str in a right way now.

To fix this we should ignore relocations from non-allocatable sections to allocatable
to allow GC work at full power, but still should proccess relocations from non-allocatable to non-allocatable sections
as usual to mark some parts of debug sections alive to keep them so we do not end
up with such assert when trying to access dead pieces. That looks like what gold/ld do, they do
not strip .debug_str section from what I saw using sample provided.

Thanks to Evgeny Leviant for suggestions about how to fix this.

Diff Detail

Repository
rL LLVM

Event Timeline

grimar updated this revision to Diff 72656.Sep 27 2016, 8:26 AM
grimar retitled this revision from to [ELF] - Fixed linkage error when using -g --gc-sections together..
grimar updated this object.
grimar added reviewers: ruiu, rafael.
grimar added subscribers: llvm-commits, grimar, evgeny777.
rafael accepted this revision.Sep 27 2016, 8:46 AM
rafael edited edge metadata.

LGTM with a test nit.

test/ELF/debug-gc.s
18 ↗(On Diff #72656)

Add a third, unused string and check if it is kept or not.

This revision is now accepted and ready to land.Sep 27 2016, 8:46 AM
This revision was automatically updated to reflect the committed changes.
grimar added inline comments.Sep 27 2016, 9:05 AM
test/ELF/debug-gc.s
18 ↗(On Diff #72656)

Hmm, interesting that we GC it (that I think is what we want), but ld/gold do not..