[PATCH] D36579: [ELF] - Do not fail when set versions for linkerscript's symbol aliases

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 6 04:13:59 PDT 2017


grimar updated this revision to Diff 113984.
grimar added a comment.

- Addressed comments.


https://reviews.llvm.org/D36579

Files:
  ELF/Driver.cpp
  test/ELF/linkerscript/symbols-synthetic.s
  test/ELF/linkerscript/version-script.s


Index: test/ELF/linkerscript/version-script.s
===================================================================
--- test/ELF/linkerscript/version-script.s
+++ test/ELF/linkerscript/version-script.s
@@ -0,0 +1,37 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+
+# RUN: echo "bar = foo; VERSION { V { global: foo; bar; local: *; }; }" > %t.script
+# RUN: ld.lld -T %t.script -shared --no-undefined-version %t.o -o %t.so
+# RUN: llvm-readobj -V %t.so | FileCheck %s
+
+# CHECK:      Symbols [
+# CHECK-NEXT:   Symbol {
+# CHECK-NEXT:     Version: 0
+# CHECK-NEXT:     Name: @
+# CHECK-NEXT:   }
+# CHECK-NEXT:   Symbol {
+# CHECK-NEXT:     Version: 2
+# CHECK-NEXT:     Name: foo@@V
+# CHECK-NEXT:   }
+# CHECK-NEXT:   Symbol {
+# CHECK-NEXT:     Version: 0
+# CHECK-NEXT:     Name: und@
+# CHECK-NEXT:   }
+# CHECK-NEXT:   Symbol {
+# CHECK-NEXT:     Version: 2
+# CHECK-NEXT:     Name: bar@@V
+# CHECK-NEXT:   }
+# CHECK-NEXT: ]
+
+# RUN: echo "bar = und; VERSION { V { global: foo; bar; local: *; }; }" > %t.script
+# RUN: not ld.lld -T %t.script -shared --no-undefined-version %t.o -o %t.so \
+# RUN:   2>&1 | FileCheck --check-prefix=ERR %s
+# ERR: symbol not found: und
+
+.global und
+
+.text
+.globl foo
+.type foo, at function
+foo:
Index: test/ELF/linkerscript/symbols-synthetic.s
===================================================================
--- test/ELF/linkerscript/symbols-synthetic.s
+++ test/ELF/linkerscript/symbols-synthetic.s
@@ -61,14 +61,14 @@
 # SIMPLE-NEXT: 0000000000000120         .foo    00000000 _begin_sec
 # SIMPLE-NEXT: 0000000000000128         *ABS*   00000000 _end_sec_abs
 # SIMPLE-NEXT: 0000000000001048         .text   00000000 _start
+# SIMPLE-NEXT: 0000000000000ee4         *ABS*   00000000 size_foo_3
 # SIMPLE-NEXT: 0000000000000120         .foo    00000000 begin_foo
 # SIMPLE-NEXT: 0000000000000128         .foo    00000000 end_foo
 # SIMPLE-NEXT: 0000000000000008         *ABS*   00000000 size_foo_1
 # SIMPLE-NEXT: 0000000000000008         *ABS*   00000000 size_foo_1_abs
 # SIMPLE-NEXT: 0000000000001000         .foo    00000000 begin_bar
 # SIMPLE-NEXT: 0000000000001004         .foo    00000000 end_bar
 # SIMPLE-NEXT: 0000000000000ee4         *ABS*   00000000 size_foo_2
-# SIMPLE-NEXT: 0000000000000ee4         *ABS*   00000000 size_foo_3
 # SIMPLE-NEXT: 0000000000001004         .eh_frame_hdr     00000000 __eh_frame_hdr_start
 # SIMPLE-NEXT: 0000000000001010         *ABS*             00000000 __eh_frame_hdr_start2
 # SIMPLE-NEXT: 0000000000001018         .eh_frame_hdr     00000000 __eh_frame_hdr_end
Index: ELF/Driver.cpp
===================================================================
--- ELF/Driver.cpp
+++ ELF/Driver.cpp
@@ -974,6 +974,20 @@
           Sym->symbol()->VersionId = VER_NDX_LOCAL;
 }
 
+template <class ELFT>
+static void addScriptSymbols() {
+  // Some symbols (such as __ehdr_start) are defined lazily only when there
+  // are undefined symbols for them, so we add these to trigger that logic.
+  for (StringRef Sym : Script->Opt.ReferencedSymbols)
+    Symtab->addUndefined<ELFT>(Sym);
+
+  // Add symbols defined in linkerscript.
+  for (BaseCommand *Base : Script->Opt.Commands)
+    if (SymbolAssignment *Cmd = dyn_cast<SymbolAssignment>(Base))
+      if (Cmd->Name != "." && !Symtab->find(Cmd->Name) && !Cmd->Provide)
+        Symtab->addAbsolute<ELFT>(Cmd->Name, STV_DEFAULT, STB_WEAK);
+}
+
 // Do actual linking. Note that when this function is called,
 // all linker scripts have already been parsed.
 template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
@@ -1013,10 +1027,7 @@
   for (InputFile *F : Files)
     Symtab->addFile<ELFT>(F);
 
-  // Some symbols (such as __ehdr_start) are defined lazily only when there
-  // are undefined symbols for them, so we add these to trigger that logic.
-  for (StringRef Sym : Script->Opt.ReferencedSymbols)
-    Symtab->addUndefined<ELFT>(Sym);
+  addScriptSymbols<ELFT>();
 
   // If an entry symbol is in a static archive, pull out that file now
   // to complete the symbol table. After this, no new names except a


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36579.113984.patch
Type: text/x-patch
Size: 4113 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170906/2544c19c/attachment.bin>


More information about the llvm-commits mailing list