This is an archive of the discontinued LLVM Phabricator instance.

[ELF] Rename adjustSectionsBeforeSorting to adjustOutputSections and make it affect INSERT commands
ClosedPublic

Authored by MaskRay on Jan 28 2022, 10:56 PM.

Details

Summary

adjustSectionsBeforeSorting updates some output section attributes
(alignment/flags) and removes discardable empty sections. When it is called,
INSERT commands have not been processed. Therefore the flags propagation rule
may not affect output sections defined in an INSERT command properly.

Fix this by moving processInsertCommands before adjustSectionsBeforeSorting.

adjustSectionsBeforeSorting is somewhat misnamed. The order between it and
sortInputSections does not matter. With the pass shuffle, the name of
adjustSectionsBeforeSorting becomes wrong. Therefore rename it. The new
name is not set into stone. The function mixes several tasks and the
code may be refactored in a way that we may give them more meaningful
names.

With this patch, I think the behavior of attribute propagation becomes more
reasonable. In particular, in the absence of non-INSERT SECTIONS,
inserting a section after a SHF_ALLOC one will give us a SHF_ALLOC section,
not a non-SHF_ALLOC one (see linkerscript/insert-after.test).

Diff Detail

Event Timeline

MaskRay created this revision.Jan 28 2022, 10:56 PM
MaskRay requested review of this revision.Jan 28 2022, 10:56 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 28 2022, 10:56 PM
bluca accepted this revision.Jan 29 2022, 10:04 AM

Tested, this solves the problem with SHF_ALLOC in a SHT_NOTE section.

This revision is now accepted and ready to land.Jan 29 2022, 10:04 AM

Would it be possible to go a bit further and move all the functionality from adjustSections that isn't important for placing Orphan Sections to adjustSectionsAfterSorting. We'd then be able to give adjustSections a more specific name. For example adjustOutputSectionFlags. As it stands adjustSections could mean almost anything.

No objections to the work done so far though.

lld/ELF/LinkerScript.cpp
1169

I'm not sure I understand the edit here. The function we are in is adjustOutputSections() and it is not called again. There is an adjustSectionsAfterSorting that is called after Orphans are placed.

MaskRay updated this revision to Diff 404599.Jan 31 2022, 10:24 AM
MaskRay marked an inline comment as done.

drop an incorrect comment update

MaskRay accepted this revision.Jan 31 2022, 10:24 AM

Would it be possible to go a bit further and move all the functionality from adjustSections that isn't important for placing Orphan Sections to adjustSectionsAfterSorting. We'd then be able to give adjustSections a more specific name. For example adjustOutputSectionFlags. As it stands adjustSections could mean almost anything.

No objections to the work done so far though.

It's difficult to split adjustSectionsBeforeSorting. The isDiscardable functionality is coupled with flag propagation.
So I'd like to the keep the core logic as is.

lld/ELF/LinkerScript.cpp
1169

It's my mistake. It's "*After*", not "*Before*". I'll drop this change.

peter.smith accepted this revision.Feb 1 2022, 1:58 AM

Thanks for the update. The adjustOutputSections still makes me a bit nervous as the name implies "do stuff" without any real idea of what just from the name. One possibility would be to split it into smaller functions, for example: setAlignment, removeFlagsFromEmptySection removeDiscardedOutputSections. Anyhow I don't want to let that get in the way of the actual fix.

MaskRay edited the summary of this revision. (Show Details)Feb 1 2022, 10:14 AM

Thanks for the update. The adjustOutputSections still makes me a bit nervous as the name implies "do stuff" without any real idea of what just from the name. One possibility would be to split it into smaller functions, for example: setAlignment, removeFlagsFromEmptySection removeDiscardedOutputSections. Anyhow I don't want to let that get in the way of the actual fix.

Thanks for the review. I acknowledge this. Added the following to the description:

The new name is not set into stone. The function mixes several tasks and the code may be refactored in a way that we may give them more meaningful names.