Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -521,6 +521,15 @@ return Symtab::X->addSynthetic(Name, Sec, Val); } +template +static Symbol *addRequiredSynthetic(StringRef Name, + OutputSectionBase *Sec, + typename ELFT::uint Val) { + SymbolBody *S = Symtab::X->find(Name); + return (!S || S->isUndefined() || S->isShared()) + ? Symtab::X->addSynthetic(Name, Sec, Val) + : S->symbol(); +} // The beginning and the ending of .rel[a].plt section are marked // with __rel[a]_iplt_{start,end} symbols if it is a statically linked // executable. The runtime needs these symbols in order to resolve @@ -856,9 +865,8 @@ auto Define = [&](StringRef Start, StringRef End, OutputSectionBase *OS) { if (OS) { - Symtab::X->addSynthetic(Start, OS, 0); - Symtab::X->addSynthetic(End, OS, - DefinedSynthetic::SectionEnd); + addRequiredSynthetic(Start, OS, 0); + addRequiredSynthetic(End, OS, DefinedSynthetic::SectionEnd); } else { addOptionalSynthetic(Start, (OutputSectionBase *)nullptr, 0); addOptionalSynthetic(End, (OutputSectionBase *)nullptr, 0); 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