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

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).

Reviewed By: peter.smith, bluca

Differential Revision: https://reviews.llvm.org/D118529
This commit is contained in:
Fangrui Song 2022-02-01 10:16:12 -08:00
parent c40744d4d6
commit a0318711c8
5 changed files with 18 additions and 20 deletions

View File

@ -310,7 +310,7 @@ void LinkerScript::processInsertCommands() {
for (const InsertCommand &cmd : insertCommands) {
for (StringRef name : cmd.names) {
// If base is empty, it may have been discarded by
// adjustSectionsBeforeSorting(). We do not handle such output sections.
// adjustOutputSections(). We do not handle such output sections.
auto from = llvm::find_if(sectionCommands, [&](SectionCommand *subCmd) {
return isa<OutputSection>(subCmd) &&
cast<OutputSection>(subCmd)->name == name;
@ -1114,7 +1114,7 @@ static void maybePropagatePhdrs(OutputSection &sec,
}
}
void LinkerScript::adjustSectionsBeforeSorting() {
void LinkerScript::adjustOutputSections() {
// If the output section contains only symbol assignments, create a
// corresponding output section. The issue is what to do with linker script
// like ".foo : { symbol = 42; }". One option would be to convert it to

View File

@ -319,7 +319,7 @@ public:
void addOrphanSections();
void diagnoseOrphanHandling() const;
void adjustSectionsBeforeSorting();
void adjustOutputSections();
void adjustSectionsAfterSorting();
SmallVector<PhdrEntry *, 0> createPhdrs();

View File

@ -1429,22 +1429,19 @@ template <class ELFT> void Writer<ELFT>::sortInputSections() {
template <class ELFT> void Writer<ELFT>::sortSections() {
llvm::TimeTraceScope timeScope("Sort sections");
script->adjustSectionsBeforeSorting();
// Don't sort if using -r. It is not necessary and we want to preserve the
// relative order for SHF_LINK_ORDER sections.
if (config->relocatable)
if (config->relocatable) {
script->adjustOutputSections();
return;
}
sortInputSections();
for (SectionCommand *cmd : script->sectionCommands) {
auto *os = dyn_cast<OutputSection>(cmd);
if (!os)
continue;
os->sortRank = getSectionRank(os);
}
for (SectionCommand *cmd : script->sectionCommands)
if (auto *osec = dyn_cast_or_null<OutputSection>(cmd))
osec->sortRank = getSectionRank(osec);
if (!script->hasSectionsCommand) {
// We know that all the OutputSections are contiguous in this case.
auto isSection = [](SectionCommand *cmd) {
@ -1454,14 +1451,15 @@ template <class ELFT> void Writer<ELFT>::sortSections() {
llvm::find_if(script->sectionCommands, isSection),
llvm::find_if(llvm::reverse(script->sectionCommands), isSection).base(),
compareSections);
// Process INSERT commands. From this point onwards the order of
// script->sectionCommands is fixed.
script->processInsertCommands();
return;
}
// Process INSERT commands and update output section attributes. From this
// point onwards the order of script->sectionCommands is fixed.
script->processInsertCommands();
script->adjustOutputSections();
if (!script->hasSectionsCommand)
return;
// Orphan sections are sections present in the input files which are
// not explicitly placed into the output file by the linker script.

View File

@ -31,7 +31,7 @@
# CHECK2-NEXT: .foo.text PROGBITS [[#%x,]] [[#%x,]] 000008 00 AX
# CHECK2-NEXT: .data PROGBITS [[#%x,]] [[#%x,]] 000008 00 WA
# CHECK2-NEXT: .foo.data PROGBITS [[#%x,]] [[#%x,]] 000008 00 WA
# CHECK2-NEXT: .byte PROGBITS [[#%x,]] [[#%x,]] 000001 00 0
# CHECK2-NEXT: .byte PROGBITS [[#%x,]] [[#%x,]] 000001 00 WA
# CHECK2: Type {{.*}} Flg Align
# CHECK2-NEXT: PHDR {{.*}} R 0x8
# CHECK2-NEXT: LOAD {{.*}} R 0x1000

View File

@ -11,7 +11,7 @@
# CHECK-NEXT: NULL
# CHECK-NEXT: .foo.text PROGBITS 0000000000000000 001000 000008 00 AX
# CHECK-NEXT: .text PROGBITS 0000000000000008 001008 000008 00 AX
# CHECK-NEXT: .byte PROGBITS 0000000000000010 001010 000001 00 WA
# CHECK-NEXT: .byte PROGBITS 0000000000000010 001010 000001 00 AX
# CHECK-NEXT: .foo.data PROGBITS 0000000000000011 001011 000008 00 WA
# CHECK-NEXT: .data PROGBITS 0000000000000019 001019 000008 00 WA
# CHECK: Type
@ -29,7 +29,7 @@
# CHECK2-NEXT: NULL
# CHECK2-NEXT: .foo.text PROGBITS 000000000020{{.*}} [[#%x,]] 000008 00 AX
# CHECK2-NEXT: .text PROGBITS [[#%x,]] [[#%x,]] 000008 00 AX
# CHECK2-NEXT: .byte PROGBITS [[#%x,]] [[#%x,]] 000001 00 0
# CHECK2-NEXT: .byte PROGBITS [[#%x,]] [[#%x,]] 000001 00 WA
# CHECK2-NEXT: .foo.data PROGBITS [[#%x,]] [[#%x,]] 000008 00 WA
# CHECK2-NEXT: .data PROGBITS [[#%x,]] [[#%x,]] 000008 00 WA
# CHECK2: Type {{.*}} Flg Align