This is an archive of the discontinued LLVM Phabricator instance.

[ELF] Avoid adding an orphan section to a less suitable segment
ClosedPublic

Authored by ikudrin on Oct 13 2021, 8:00 AM.

Details

Summary

This is an alternative for D111137.

If segments are defined in a linker script, placing an orphan section before the found closest-rank section can result in adding it in a previous segment and changing flags of that segment. This happens if the orphan section has a lower sort rank than the found section. To avoid that, the patch forces orphan sections to be moved after the found sections if segments are explicitly defined.

Diff Detail

Event Timeline

ikudrin created this revision.Oct 13 2021, 8:00 AM
ikudrin requested review of this revision.Oct 13 2021, 8:00 AM

Adding an orphan before the (previously) first section of a segment can create unintended effects (say the user specifies output section address for the first section).
We have quite refined RankFlags. If the needle section sec mostly matches the first section but has a refined rank, it makes sense to ignore the additional bits in this case.

It may be useful to strengthen the test by leveraging non-PROGBITS and PROGBITS read-only sections (RF_RODATA).

Am I right that this makes our behavior closer to ld.bfd? If yes, this looks quite nice.


As of PT_GNU_RELRO, this may be another fallout of our default -z relro. We probably shouldn't change it now.
If a generic improvement like this patch makes the relro issue moot, that is certainly great.

lld/test/ELF/linkerscript/orphan-phdrs2.test
13

CHECK-NEXT:

16

NOT is not needed. A positive CHECK exists.

ikudrin updated this revision to Diff 380358.Oct 18 2021, 6:24 AM
  • Added CHECK-NEXT checks
  • Added checks for read-only orphan sections

Am I right that this makes our behavior closer to ld.bfd? If yes, this looks quite nice.

I hope so.

lld/test/ELF/linkerscript/orphan-phdrs2.test
16

As a section can be put in several segments, I initially added this CHECK-NOT to ensure/demonstrate that .dynamic is not in the first PT_LOAD segment. A positive CHECK can be used, but with a terminating {{$}}.

ikudrin marked 2 inline comments as done.Oct 18 2021, 6:25 AM
MaskRay accepted this revision.Oct 19 2021, 11:19 AM

LGTM. Worth checking with @peter.smith as well.

This revision is now accepted and ready to land.Oct 19 2021, 11:19 AM
MaskRay added inline comments.Oct 19 2021, 11:21 AM
lld/ELF/Writer.cpp
1261

the blank line may be deleted

1264
MaskRay added a comment.EditedOct 19 2021, 11:23 AM

Avoid adding orphan sections to a less fit segment

I may use something like: "Make orphan section at the boundary of two sort ranks belong to the latter one"

ikudrin updated this revision to Diff 380884.Oct 20 2021, 3:00 AM
ikudrin marked 2 inline comments as done.
ikudrin retitled this revision from [ELF] Avoid adding orphan sections to a less fit segment to [ELF] Avoid adding an orphan section to a less suitable segment.

Avoid adding orphan sections to a less fit segment

I may use something like: "Make orphan section at the boundary of two sort ranks belong to the latter one"

That does not seem to be accurate enough because the sort rank is adjusted only in presence of PHDRS.

I don't have any fundamental objections. I've left my comments on D111137 about disabling RELRO for PHDRS like ld.bfd does and put more burden on the user to give a more complete linker script. However this won't get in the way of a more complete linker script and I prefer this to D111137 so I'm happy to go with the consensus.