From a0318711c8cfcb0ecdd956eb400916c5c2b08d3c Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Tue, 1 Feb 2022 10:16:12 -0800 Subject: [PATCH] [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 --- lld/ELF/LinkerScript.cpp | 4 +-- lld/ELF/LinkerScript.h | 2 +- lld/ELF/Writer.cpp | 26 +++++++++----------- lld/test/ELF/linkerscript/insert-after.test | 2 +- lld/test/ELF/linkerscript/insert-before.test | 4 +-- 5 files changed, 18 insertions(+), 20 deletions(-) diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index bfb583453735..7188ce58376d 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -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(subCmd) && cast(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 diff --git a/lld/ELF/LinkerScript.h b/lld/ELF/LinkerScript.h index d2a6f5e9acb1..24c2c632f93b 100644 --- a/lld/ELF/LinkerScript.h +++ b/lld/ELF/LinkerScript.h @@ -319,7 +319,7 @@ public: void addOrphanSections(); void diagnoseOrphanHandling() const; - void adjustSectionsBeforeSorting(); + void adjustOutputSections(); void adjustSectionsAfterSorting(); SmallVector createPhdrs(); diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index a3140c98dbf7..9383ac46c8e7 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -1429,22 +1429,19 @@ template void Writer::sortInputSections() { template void Writer::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(cmd); - if (!os) - continue; - os->sortRank = getSectionRank(os); - } - + for (SectionCommand *cmd : script->sectionCommands) + if (auto *osec = dyn_cast_or_null(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 void Writer::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. diff --git a/lld/test/ELF/linkerscript/insert-after.test b/lld/test/ELF/linkerscript/insert-after.test index b438389c795f..4b25ff36806c 100644 --- a/lld/test/ELF/linkerscript/insert-after.test +++ b/lld/test/ELF/linkerscript/insert-after.test @@ -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 diff --git a/lld/test/ELF/linkerscript/insert-before.test b/lld/test/ELF/linkerscript/insert-before.test index ff407464e3f5..f9611538c013 100644 --- a/lld/test/ELF/linkerscript/insert-before.test +++ b/lld/test/ELF/linkerscript/insert-before.test @@ -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