diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -60,6 +60,7 @@ void finalizeAddressDependentContent(); void optimizeBasicBlockJumps(); void sortInputSections(); + void sortOrphanSections(); void finalizeSections(); void checkExecuteOnly(); void setReservedSymbolSections(); @@ -1456,9 +1457,13 @@ script->processInsertCommands(); script->adjustOutputSections(); - if (!script->hasSectionsCommand) - return; + if (script->hasSectionsCommand) + sortOrphanSections(); + script->adjustSectionsAfterSorting(); +} + +template void Writer::sortOrphanSections() { // Orphan sections are sections present in the input files which are // not explicitly placed into the output file by the linker script. // @@ -1533,8 +1538,6 @@ std::rotate(pos, nonScriptI, end); nonScriptI = end; } - - script->adjustSectionsAfterSorting(); } static bool compareByFilePosition(InputSection *a, InputSection *b) { diff --git a/lld/test/ELF/linkerscript/memory-no-sections.test b/lld/test/ELF/linkerscript/memory-no-sections.test new file mode 100644 --- /dev/null +++ b/lld/test/ELF/linkerscript/memory-no-sections.test @@ -0,0 +1,26 @@ +REQUIRES: x86 + +## Check that we respect MEMORY commands in linker scripts without +## SECTIONS commands. + +RUN: split-file %s %ts +RUN: llvm-mc -filetype=obj -triple=x86_64 %ts/asm.s -o %t.o +RUN: ld.lld -o %t -T %ts/script %t.o +RUN: llvm-readelf -S %t | FileCheck %s + +CHECK: .text PROGBITS 0000000000001000 +CHECK: .data PROGBITS 0000000010000000 + +#--- script + +MEMORY { + flash (rx!w): org = 0x1000, len = 0x3000 + ram (rwx): org = 0x10000000, len = 0x1000 +} + +#--- asm.s + +.text +.byte 1 +.data +.byte 2