This is an archive of the discontinued LLVM Phabricator instance.

[ELF] Preserve section order within an INSERT AFTER command
ClosedPublic

Authored by MaskRay on Jun 29 2021, 4:14 PM.

Details

Summary

For

SECTIONS {
  text.0 : {}
  text.1 : {}
  text.2 : {}
} INSERT AFTER .data;

the current order is .data text.2 text.1 text.0. It makes more sense to
preserve the specified order and thus improve compatibility with GNU ld.

For

SECTIONS { text.0 : {} } INSERT AFTER .data;
SECTIONS { text.3 : {} } INSERT AFTER .data;

GNU ld somehow collects sections with INSERT AFTER .data together (IMO
inconsistent) but I think it makes more sense to execute the commands in order
and get .data text.3 text.0 instead.

Diff Detail

Event Timeline

MaskRay created this revision.Jun 29 2021, 4:14 PM
MaskRay requested review of this revision.Jun 29 2021, 4:14 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 29 2021, 4:14 PM

Change looks sensible to me. One suggestion that might work out better is to use something like:

struct InsertCommand {
  std::vector<StringRef> names;
  bool isAfter;
  StringRef where;
};

Then all OutputSections in a SECTIONS can be added in order to names. This might make it easier to understand the nested loops in processInsertCommands. This is only a weak suggestion though, it may make things worse.

lld/ELF/LinkerScript.h
230

Suggest comment, something like:
// All OutputSections in a SECTIONS get the same commandId;

MaskRay updated this revision to Diff 355608.Jun 30 2021, 9:57 AM

Thanks for the suggestion. Switched to std::vector<StringRef>

If we ever care about insert order of SymbolAssignment's, we can think how to address that in the future.

peter.smith accepted this revision.Jun 30 2021, 10:59 AM

LGTM, thanks for the update.

This revision is now accepted and ready to land.Jun 30 2021, 10:59 AM
This revision was landed with ongoing or failed builds.Jun 30 2021, 11:35 AM
This revision was automatically updated to reflect the committed changes.