diff --git a/lld/ELF/LinkerScript.h b/lld/ELF/LinkerScript.h --- a/lld/ELF/LinkerScript.h +++ b/lld/ELF/LinkerScript.h @@ -319,7 +319,7 @@ void addOrphanSections(); void diagnoseOrphanHandling() const; - void adjustSectionsBeforeSorting(); + void adjustOutputSections(); void adjustSectionsAfterSorting(); SmallVector createPhdrs(); diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -310,7 +310,7 @@ 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 @@ } } -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/Writer.cpp b/lld/ELF/Writer.cpp --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -1429,22 +1429,19 @@ 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 @@ 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 --- 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 --- 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