This is an archive of the discontinued LLVM Phabricator instance.

[ELF] - Stop naming relocation sections with first input section name.
ClosedPublic

Authored by grimar on Oct 24 2017, 6:58 AM.

Details

Summary

It was reported (https://reviews.llvm.org/D38724#902841) that when we use
-ffunction-sections --emit-relocs build, REL[A] output section receives the name of first
input section, like .rela.text.first_function_in_text rather than .rela.text.

It is probably not really an issue as sh_info still points to correct target section, but
it does not look clean in output and allows internal section name to leak there,
what at least looks confusing and is not consistent with ld.bfd.

Patch changes this behavior so that target output section name is used as a base.

Diff Detail

Repository
rL LLVM

Event Timeline

grimar created this revision.Oct 24 2017, 6:58 AM
grimar planned changes to this revision.Oct 24 2017, 7:09 AM
grimar updated this revision to Diff 120072.Oct 24 2017, 7:11 AM
  • Better version.
ruiu added inline comments.Oct 24 2017, 11:26 AM
ELF/Writer.cpp
101 ↗(On Diff #120072)

Is there an "abnormal" situation in which this code is used even if --emit-relocs is not given?

104–110 ↗(On Diff #120072)

Is this the same as this?

if (Name.startswith(".rel."))
  return Saver.save(".rel." + getOutputSectionName(Name.substr(4)));
if (Name.startswith(".rela."))
  return Saver.save(".rela." + getOutputSectionName(Name.substr(5)));
grimar added inline comments.Oct 25 2017, 1:44 AM
ELF/Writer.cpp
101 ↗(On Diff #120072)

Yes. Since this code called for synthetic sections, it executes for ".rela.dyn" for example.
It looks to be safe, as it returns the same result as original code for that case, so does
not seem we need additional checks here.

104–110 ↗(On Diff #120072)

That was something close to what I did in the first diff. I do not think we want to do that.
getOutputSectionName sometimes returns value from string saver, or for "COMMON" case it returns
".bss". So it is not consistent and not always returns the part of the same string.

I am not sure how much is possible to create .rela.COMMON or .rela.zdebug_ section, but I think we should not
write code that assumes it is impossible, as it is too complicated assumption which needs additional testing probably.
I believe my version is much more straightforward and the fact it rarely allocates the string is not a great issue.

ruiu added inline comments.Oct 25 2017, 8:36 AM
ELF/Writer.cpp
104–110 ↗(On Diff #120072)

I just tried to unroll your loop, so there shouldn't be any semantic differences. What am I missing?

grimar added inline comments.Oct 25 2017, 8:40 AM
ELF/Writer.cpp
104–110 ↗(On Diff #120072)

I am sorry, I think I read your code wrong at first. Yes, that should be equal and your way looks better for me.

grimar updated this revision to Diff 120389.Oct 26 2017, 3:49 AM
  • Used code suggested.
ruiu accepted this revision.Oct 26 2017, 2:15 PM

LGTM

ELF/Writer.cpp
101–103 ↗(On Diff #120389)

This is for --emit-relocs. If .text.foo is emitted as .text, we want to emit .rela.text.foo as .rel.text for consistency (this is not technically required, but not doing it is odd). This code guarantees that.

This revision is now accepted and ready to land.Oct 26 2017, 2:15 PM
This revision was automatically updated to reflect the committed changes.