Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -294,18 +294,20 @@ } } - // Add orphan sections. - for (const std::unique_ptr> &F : - Symtab::X->getObjectFiles()) { - for (InputSectionBase *S : F->getSections()) { - if (isDiscarded(S) || S->OutSec) - continue; - OutputSectionBase *OutSec; - bool IsNew; - std::tie(OutSec, IsNew) = Factory.create(S, getOutputSectionName(S)); - if (IsNew) - OutputSections->push_back(OutSec); - OutSec->addSection(S); + if (Opt.HasContents) { + // Add orphan sections. + for (const std::unique_ptr> &F : + Symtab::X->getObjectFiles()) { + for (InputSectionBase *S : F->getSections()) { + if (isDiscarded(S) || S->OutSec) + continue; + OutputSectionBase *OutSec; + bool IsNew; + std::tie(OutSec, IsNew) = Factory.create(S, getOutputSectionName(S)); + if (IsNew) + OutputSections->push_back(OutSec); + OutSec->addSection(S); + } } } } @@ -363,10 +365,12 @@ // We place orphan sections at end of file. // Other linkers places them using some heuristics as described in // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections. - for (OutputSectionBase *Sec : *OutputSections) { - StringRef Name = Sec->getName(); - if (getSectionIndex(Name) == INT_MAX) - Opt.Commands.push_back(llvm::make_unique(Name)); + if (Opt.HasContents) { + for (OutputSectionBase *Sec : *OutputSections) { + StringRef Name = Sec->getName(); + if (getSectionIndex(Name) == INT_MAX) + Opt.Commands.push_back(llvm::make_unique(Name)); + } } // Assign addresses as instructed by linker script SECTIONS sub-commands. @@ -421,13 +425,15 @@ Dot += Sec->getSize(); } - // ELF and Program headers need to be right before the first section in - // memory. Set their addresses accordingly. - MinVA = alignDown(MinVA - Out::ElfHeader->getSize() - - Out::ProgramHeaders->getSize(), - Target->PageSize); - Out::ElfHeader->setVA(MinVA); - Out::ProgramHeaders->setVA(Out::ElfHeader->getSize() + MinVA); + if (Opt.HasContents) { + // ELF and Program headers need to be right before the first section in + // memory. Set their addresses accordingly. + MinVA = alignDown(MinVA - Out::ElfHeader->getSize() - + Out::ProgramHeaders->getSize(), + Target->PageSize); + Out::ElfHeader->setVA(MinVA); + Out::ProgramHeaders->setVA(Out::ElfHeader->getSize() + MinVA); + } } // Creates program headers as instructed by PHDRS linker script command. Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -247,9 +247,8 @@ CommonInputSection::X = &Common; Script::X->OutputSections = &OutputSections; - if (ScriptConfig->HasContents) - Script::X->createSections(Factory); - else + Script::X->createSections(Factory); + if (!ScriptConfig->HasContents) createSections(); finalizeSections(); @@ -262,9 +261,8 @@ Phdrs = Script::X->hasPhdrsCommands() ? Script::X->createPhdrs() : createPhdrs(); fixHeaders(); - if (ScriptConfig->HasContents) { - Script::X->assignAddresses(); - } else { + Script::X->assignAddresses(); + if (!ScriptConfig->HasContents) { fixSectionAlignments(); assignAddresses(); } Index: test/ELF/linkerscript/linkerscript-symbols.s =================================================================== --- test/ELF/linkerscript/linkerscript-symbols.s +++ test/ELF/linkerscript/linkerscript-symbols.s @@ -67,6 +67,15 @@ # RUN: llvm-objdump -t %t1 | FileCheck --check-prefix=HIDDEN5 %s # HIDDEN5: 0000000000000000 *ABS* 00000000 somesym +# Simple symbol assignment. All three symbols should have the +# same value. +# RUN: echo "foo = 0x100; SECTIONS { bar = foo; } baz = bar;" > %t.script +# RUN: ld.lld -o %t1 --script %t.script %t +# RUN: llvm-objdump -t %t1 | FileCheck --check-prefix=SIMPLE2 %s +# SIMPLE2: 0000000000000100 *ABS* 00000000 foo +# SIMPLE2: 0000000000000100 *ABS* 00000000 bar +# SIMPLE2: 0000000000000100 *ABS* 00000000 baz + .global _start _start: nop