This is an archive of the discontinued LLVM Phabricator instance.

[ELF] - Do not merge sections from SHT_GROUP when -relocatable
ClosedPublic

Authored by grimar on Sep 7 2017, 8:59 AM.

Details

Summary

This is PR34506.

Imagine we have 2 sections the same name but different COMDAT groups:

.section        .foo,"axG",@progbits,bar,comdat
.section        .foo,"axG",@progbits,zed,comdat

When linking relocatable we do not merge SHT_GROUP sections. But still would merge
both input sections .foo into single output section .foo.
As a result we will have 2 different SHT_GROUPs containing the same section, what
is wrong.

Patch fixes the issue, preventing merging SHF_GROUP sections with any others.

Diff Detail

Repository
rL LLVM

Event Timeline

grimar created this revision.Sep 7 2017, 8:59 AM
ruiu edited edge metadata.Sep 7 2017, 12:23 PM

This does not seems like a correct fix. Why do we merge sections in the first place? If we don't do that for -r, it shouldn't have been the issue, no?

grimar added a comment.Sep 8 2017, 3:53 AM
In D37574#863756, @ruiu wrote:

This does not seems like a correct fix. Why do we merge sections in the first place? If we don't do that for -r, it shouldn't have been the issue, no?

What is intention of relocatable link and why it is called "partial linking" ? As far I understand intention is to speedup final link.
When we have multiple .text sections for example, then relocatable link merge them by name to output .text. Merging of sections helps to reduce amount of
input sections for latter final link. We also merge SHT_REL[A] sections when doing that, it should probably help to speedup futher relocation processing as well.
As a last resort is also consistent with GNU linkers, it may be dangerous to deviate without solid reason as all kind of tools may rely on this behavior.

ruiu added a comment.Sep 8 2017, 3:52 PM

What is intention of relocatable link and why it is called "partial linking" ? As far I understand intention is to speedup final link.
When we have multiple .text sections for example, then relocatable link merge them by name to output .text. Merging of sections helps to reduce amount of
input sections for latter final link. We also merge SHT_REL[A] sections when doing that, it should probably help to speedup futher relocation processing as well.
As a last resort is also consistent with GNU linkers, it may be dangerous to deviate without solid reason as all kind of tools may rely on this behavior.

I don't agree. As we have found when you tried to implement the -r option, a lot of information that can be merged are passed through instead of being merged for a relocatable link. If sections have to be merged for some reason, that is fine, but both "-r is for speeding up the linking" and "doing something different than GNU linkers is dangerous" are not convincing arguments.

grimar updated this revision to Diff 115390.Sep 15 2017, 3:52 AM
  • Original version of patch was silly. Rui, will you be ok with this version ?
ruiu added inline comments.Sep 15 2017, 11:04 AM
ELF/OutputSections.cpp
210 ↗(On Diff #115390)

s/- r/-r/

215 ↗(On Diff #115390)

This comment is not correct. Assume that there's only one section group containing a section .foo, and there's other object file having a section .foo which is not a member of any section group. It'll end up with only one section group, but we still shouldn't merge the member .foo with non-member .foo, because it is semantically incorrect.

grimar updated this revision to Diff 115616.Sep 18 2017, 3:18 AM
  • Addressed review comments.
ELF/OutputSections.cpp
215 ↗(On Diff #115390)

Right. Updated comment and added regular (non-group) section to testcase.

grimar edited the summary of this revision. (Show Details)Sep 18 2017, 3:19 AM
ruiu accepted this revision.Sep 18 2017, 6:26 PM

LGTM with this change.

ELF/OutputSections.cpp
254–262 ↗(On Diff #115616)

This comment becomes a bit too lengthy. Instead of appending more and more comments, it is sometimes better to rewrite. I'd rewrite this:

Sections with SHT_GROUP or SHF_GROUP attributes reach here only when the -r option is given. A section with SHT_GROUP defines a "section group", and its members have SHF_GROUP attribute. Usually these flags have already been stripped by InputFiles.cpp as section groups are processed and uniquified. However, for the -r option, we want to pass through all section groups as-is because adding/removing members or merging them with other groups change their semantics.

This revision is now accepted and ready to land.Sep 18 2017, 6:26 PM
grimar added inline comments.Sep 19 2017, 2:41 AM
ELF/OutputSections.cpp
254–262 ↗(On Diff #115616)

Thanks !

This revision was automatically updated to reflect the committed changes.