[PATCH] D23405: [ELF] Do not add start and end symbols in case they are defined in linker script

Eugene Leviant via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 11 06:01:12 PDT 2016


evgeny777 created this revision.
evgeny777 added a reviewer: ruiu.
evgeny777 added subscribers: grimar, ikudrin, emaste, davide, llvm-commits.
evgeny777 set the repository for this revision to rL LLVM.
evgeny777 added a project: lld.

Without this patch, the following script triggers link error

```
SECTIONS {
   .init_array : { __init_array_start = .; *(.init_array) __init_array_end = .; }
}
```

Repository:
  rL LLVM

https://reviews.llvm.org/D23405

Files:
  ELF/Writer.cpp
  test/ELF/linkerscript/linkerscript-start-end.s

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
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 <class ELFT> void Writer<ELFT>::addStartEndSymbols() {
-  auto Define = [&](StringRef Start, StringRef End,
-                    OutputSectionBase<ELFT> *OS) {
+  auto Add = [](StringRef Name, OutputSectionBase<ELFT> *OS, uintX_t Value) {
     if (OS) {
-      Symtab<ELFT>::X->addSynthetic(Start, OS, 0);
-      Symtab<ELFT>::X->addSynthetic(End, OS,
-                                    DefinedSynthetic<ELFT>::SectionEnd);
+      SymbolBody *B = Symtab<ELFT>::X->find(Name);
+      if (!B || B->isUndefined() || B->isShared())
+        Symtab<ELFT>::X->addSynthetic(Name, OS, Value);
     } else {
-      addOptionalSynthetic(Start, (OutputSectionBase<ELFT> *)nullptr, 0);
-      addOptionalSynthetic(End, (OutputSectionBase<ELFT> *)nullptr, 0);
+      addOptionalSynthetic(Name, OS, Value);
     }
   };
 
+  auto Define = [&](StringRef Start, StringRef End,
+                    OutputSectionBase<ELFT> *OS) {
+    Add(Start, OS, 0);
+    Add(End, OS, OS ? DefinedSynthetic<ELFT>::SectionEnd : 0);
+  };
+
   Define("__preinit_array_start", "__preinit_array_end",
          Out<ELFT>::PreinitArray);
   Define("__init_array_start", "__init_array_end", Out<ELFT>::InitArray);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D23405.67680.patch
Type: text/x-patch
Size: 2105 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160811/efeae1be/attachment.bin>


More information about the llvm-commits mailing list