Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -666,19 +666,27 @@ // doesn't break the intended meaning of the begin/end symbols. // We don't want to go over sections since Writer::sortSections is the // one in charge of deciding the order of the sections. -// We don't want to go over alignments, since doing so in +// We don't want to go over alignments for SHF_ALLOC sections, since doing so in // rx_sec : { *(rx_sec) } // . = ALIGN(0x1000); // /* The RW PT_LOAD starts here*/ // rw_sec : { *(rw_sec) } // would mean that the RW PT_LOAD would become unaligned. -static bool shouldSkip(const BaseCommand &Cmd) { +// We allow going over alignments for non-allocatable sections, that allows +// next script to assign _end an expected value. Non-allocatable sections placed +// last, and can't be a reason of unaligned PT_LOAD. +// SECTIONS { +// .foo : { *(.foo*) } +// . = ALIGN(16); +// _end = .; +// /* All non-allocatable orphans placed here */ +static bool shouldSkip(const BaseCommand &Cmd, bool IsAlloc) { if (isa(Cmd)) return false; const auto *Assign = dyn_cast(&Cmd); if (!Assign) return true; - return Assign->Name != "."; + return !IsAlloc || Assign->Name != "."; } // Orphan sections are sections present in the input files which are @@ -736,7 +744,7 @@ // correct result. auto CmdIter = Opt.Commands.begin() + CmdIndex; auto E = Opt.Commands.end(); - while (CmdIter != E && shouldSkip(**CmdIter)) { + while (CmdIter != E && shouldSkip(**CmdIter, Sec->Flags & SHF_ALLOC)) { ++CmdIter; ++CmdIndex; } Index: test/ELF/linkerscript/symbols-and-orphans.s =================================================================== --- test/ELF/linkerscript/symbols-and-orphans.s +++ test/ELF/linkerscript/symbols-and-orphans.s @@ -0,0 +1,17 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t + +# RUN: echo "SECTIONS { . = SIZEOF_HEADERS; \ +# RUN: .text : { *(.text) } \ +# RUN: . = ALIGN(8); \ +# RUN: Sym = .; }" > %t.script +# RUN: ld.lld -o %t2 --script %t.script %t +# RUN: llvm-objdump -section-headers -t %t2 | FileCheck %s + +# CHECK: Sections: +# CHECK: 1 .text 00000000 00000000000000e8 TEXT DATA +# CHECK: SYMBOL TABLE: +# CHECK: 00000000000000e8 .text 00000000 Sym + +.section .orphan,"" + .quad 0