Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -431,10 +431,10 @@ // The Sections with -T
are sorted in order of ascending address // we must use this if it is lower than StartAddr as calls to setDot() must // be monotonically increasing - if (!Config->SectionStartMap.empty()) { - uint64_t LowestSecStart = Config->SectionStartMap.begin()->second; - StartAddr = std::min(StartAddr, LowestSecStart); - } + if (!Config->SectionStartMap.empty()) + for (auto& KV : Config->SectionStartMap) + StartAddr = std::min(StartAddr, KV.second); + Commands.push_back( make(".", [=] { return StartAddr; }, "")); @@ -444,17 +444,19 @@ if (!(Sec->Flags & SHF_ALLOC)) continue; + auto *OSCmd = make(Sec->Name); + OSCmd->Sec = Sec; + + // Prefer user supplied address over additional alignment constraint auto I = Config->SectionStartMap.find(Sec->Name); if (I != Config->SectionStartMap.end()) Commands.push_back( make(".", [=] { return I->second; }, "")); - - auto *OSCmd = make(Sec->Name); - OSCmd->Sec = Sec; - if (Sec->PageAlign) + else if (Sec->PageAlign) OSCmd->AddrExpr = [=] { return alignTo(Script->getDot(), Config->MaxPageSize); }; + Commands.push_back(OSCmd); if (Sec->Sections.size()) { auto *ISD = make(""); Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -252,7 +252,7 @@ } else { if (!Script->Opt.HasSections) { fixSectionAlignments(); - Script->fabricateDefaultCommands(Config->MaxPageSize); + Script->fabricateDefaultCommands(AllocateHeader); } Script->synchronize(); Script->assignAddresses(Phdrs); Index: test/ELF/sectionstart-noallochdr.s =================================================================== --- /dev/null +++ test/ELF/sectionstart-noallochdr.s @@ -0,0 +1,23 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o +# RUN: ld.lld %t.o --section-start .data=0x20 \ +# RUN: --section-start .bss=0x30 --section-start .text=0x10 -o %t1 +# RUN: llvm-objdump -section-headers %t1 | FileCheck %s + +# CHECK: Sections: +# CHECK-NEXT: Idx Name Size Address Type +# CHECK-NEXT: 0 00000000 0000000000000000 +# CHECK-NEXT: 1 .text 00000001 0000000000000010 TEXT DATA +# CHECK-NEXT: 2 .data 00000004 0000000000000020 DATA +# CHECK-NEXT: 3 .bss 00000004 0000000000000030 BSS + +.text +.globl _start +_start: + nop + +.data +.long 0 + +.bss +.zero 4