From a5c8a685356a5c5e956723429f7c2c39f8fbcd37 Mon Sep 17 00:00:00 2001 From: Eugene Leviant Date: Mon, 15 Aug 2016 07:24:20 +0000 Subject: [PATCH] [ELF] Do not add start and end symbols in case they are already defined llvm-svn: 278657 --- lld/ELF/Writer.cpp | 12 +++++++++--- lld/test/ELF/linkerscript/linkerscript-start-end.s | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 lld/test/ELF/linkerscript/linkerscript-start-end.s diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 416ef76..108ca54 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -521,6 +521,13 @@ static Symbol *addOptionalSynthetic(StringRef Name, return Symtab::X->addSynthetic(Name, Sec, Val); } +template +static void addSynthetic(StringRef Name, OutputSectionBase *Sec, + typename ELFT::uint Val) { + SymbolBody *S = Symtab::X->find(Name); + if (!S || S->isUndefined() || S->isShared()) + Symtab::X->addSynthetic(Name, Sec, Val); +} // 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 +863,8 @@ template void Writer::addStartEndSymbols() { auto Define = [&](StringRef Start, StringRef End, OutputSectionBase *OS) { if (OS) { - Symtab::X->addSynthetic(Start, OS, 0); - Symtab::X->addSynthetic(End, OS, - DefinedSynthetic::SectionEnd); + addSynthetic(Start, OS, 0); + addSynthetic(End, OS, DefinedSynthetic::SectionEnd); } else { addOptionalSynthetic(Start, (OutputSectionBase *)nullptr, 0); addOptionalSynthetic(End, (OutputSectionBase *)nullptr, 0); diff --git a/lld/test/ELF/linkerscript/linkerscript-start-end.s b/lld/test/ELF/linkerscript/linkerscript-start-end.s new file mode 100644 index 0000000..b68606a --- /dev/null +++ b/lld/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 -- 2.7.4