Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -852,18 +852,22 @@ // The linker is expected to define SECNAME_start and SECNAME_end // symbols for a few sections. This function defines them. template void Writer::addStartEndSymbols() { - auto Define = [&](StringRef Start, StringRef End, - OutputSectionBase *OS) { + auto Add = [](StringRef Name, OutputSectionBase *OS, uintX_t Value) { if (OS) { - Symtab::X->addSynthetic(Start, OS, 0); - Symtab::X->addSynthetic(End, OS, - DefinedSynthetic::SectionEnd); + SymbolBody *B = Symtab::X->find(Name); + if (!B || B->isUndefined() || B->isShared()) + Symtab::X->addSynthetic(Name, OS, Value); } else { - addOptionalSynthetic(Start, (OutputSectionBase *)nullptr, 0); - addOptionalSynthetic(End, (OutputSectionBase *)nullptr, 0); + addOptionalSynthetic(Name, OS, Value); } }; + auto Define = [&](StringRef Start, StringRef End, + OutputSectionBase *OS) { + Add(Start, OS, 0); + Add(End, OS, OS ? DefinedSynthetic::SectionEnd : 0); + }; + Define("__preinit_array_start", "__preinit_array_end", Out::PreinitArray); Define("__init_array_start", "__init_array_end", Out::InitArray); Index: test/ELF/linkerscript/linkerscript-start-end.s =================================================================== --- test/ELF/linkerscript/linkerscript-start-end.s +++ test/ELF/linkerscript/linkerscript-start-end.s @@ -0,0 +1,16 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o +# RUN: echo "SECTIONS { \ +# RUN: .init_array : { \ +# RUN: __init_array_start = .; \ +# RUN: *(.init_array) \ +# RUN: __init_array_end = .; } }" > %t.script +# RUN: ld.lld %t.o -script %t.script -o %t 2>&1 + +.globl _start +.text +_start: + nop + +.section .init_array, "aw" + .quad 0