Index: ELF/LinkerScript.h =================================================================== --- ELF/LinkerScript.h +++ ELF/LinkerScript.h @@ -184,6 +184,7 @@ ~LinkerScript(); void processCommands(OutputSectionFactory &Factory); void createSections(OutputSectionFactory &Factory); + void adjustSectionsBeforeSorting(); std::vector> createPhdrs(); bool ignoreInterpSection(); Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -445,7 +445,7 @@ [this](std::unique_ptr &B) { process(*B.get()); }); } -template void LinkerScript::assignAddresses() { +template void LinkerScript::adjustSectionsBeforeSorting() { // It is common practice to use very generic linker scripts. So for any // given run some of the output sections in the script will be empty. // We could create corresponding empty output sections, but that would @@ -469,6 +469,29 @@ }); Opt.Commands.erase(Pos, Opt.Commands.end()); + // If the output section contains only symbol assignments, create a + // corresponding output section. The bfd linker seems to only create them if + // '.' is assigned to, but creating these section should not have any bad + // consequeces and gives us a section to put the symbol in. + uintX_t Flags = SHF_ALLOC; + for (const std::unique_ptr &Base : Opt.Commands) { + auto *Cmd = dyn_cast(Base.get()); + if (!Cmd) + continue; + std::vector *> Secs = + findSections(*Cmd, *OutputSections); + if (!Secs.empty()) { + Flags = Secs[0]->getFlags(); + continue; + } + + auto *OutSec = new OutputSection(Cmd->Name, /*Type*/ 0, Flags); + Out::Pool.emplace_back(OutSec); + OutputSections->push_back(OutSec); + } +} + +template void LinkerScript::assignAddresses() { // Orphan sections are sections present in the input files which // are not explicitly placed into the output file by the linker script. // We place orphan sections at end of file. Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -777,6 +777,9 @@ // This function adds linker-created Out::* sections. addPredefinedSections(); + if (ScriptConfig->HasSections) + Script::X->adjustSectionsBeforeSorting(); + std::stable_sort(OutputSections.begin(), OutputSections.end(), compareSections); Index: test/ELF/linkerscript/symbol-only.s =================================================================== --- /dev/null +++ test/ELF/linkerscript/symbol-only.s @@ -0,0 +1,18 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t + +# RUN: echo "SECTIONS { \ +# RUN: abc : { foo = .; } \ +# RUN: . = ALIGN(0x1000); \ +# RUN: .text : { *(.text) } \ +# RUN: }" > %t.script +# RUN: ld.lld -o %t1 --script %t.script %t -shared +# RUN: llvm-objdump -section-headers -t %t1 | FileCheck %s +# CHECK: Sections: +# CHECK-NEXT: Idx Name Size Address +# CHECK-NEXT: 0 00000000 0000000000000000 +# CHECK-NEXT: 1 abc 00000000 0000000000000200 +# CHECK-NEXT: 2 .text 00000000 0000000000001000 TEXT DATA + +# CHECK: SYMBOL TABLE: +# CHECK: 0000000000000200 abc 00000000 foo