[lld] r295780 - [ELF] Insert linkerscript symbols directly into symbol table

Petr Hosek via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 21 14:32:51 PST 2017


Author: phosek
Date: Tue Feb 21 16:32:51 2017
New Revision: 295780

URL: http://llvm.org/viewvc/llvm-project?rev=295780&view=rev
Log:
[ELF] Insert linkerscript symbols directly into symbol table

This change exposes the symbol table insert method and uses it to
insert the linkerscript defined symbols directly into the symbol
table to avoid unnecessarily pulling the object out of an archive.

Differential Revision: https://reviews.llvm.org/D30224

Added:
    lld/trunk/test/ELF/linkerscript/Inputs/lazy-symbols.s
    lld/trunk/test/ELF/linkerscript/lazy-symbols.s
Modified:
    lld/trunk/ELF/LinkerScript.cpp
    lld/trunk/ELF/SymbolTable.h

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=295780&r1=295779&r2=295780&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Tue Feb 21 16:32:51 2017
@@ -57,12 +57,12 @@ LinkerScriptBase *elf::ScriptBase;
 ScriptConfiguration *elf::ScriptConfig;
 
 template <class ELFT> static SymbolBody *addRegular(SymbolAssignment *Cmd) {
+  Symbol *Sym;
   uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
-  Symbol *Sym = Symtab<ELFT>::X->addUndefined(
-      Cmd->Name, /*IsLocal=*/false, STB_GLOBAL, Visibility,
-      /*Type*/ 0,
-      /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
-
+  std::tie(Sym, std::ignore) = Symtab<ELFT>::X->insert(
+      Cmd->Name, /*Type*/ 0, Visibility, /*CanOmitFromDynSym*/ false,
+      /*File*/ nullptr);
+  Sym->Binding = STB_GLOBAL;
   replaceBody<DefinedRegular<ELFT>>(Sym, Cmd->Name, /*IsLocal=*/false,
                                     Visibility, STT_NOTYPE, 0, 0, nullptr,
                                     nullptr);
@@ -70,14 +70,14 @@ template <class ELFT> static SymbolBody
 }
 
 template <class ELFT> static SymbolBody *addSynthetic(SymbolAssignment *Cmd) {
+  Symbol *Sym;
   uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
   const OutputSectionBase *Sec =
       ScriptConfig->HasSections ? nullptr : Cmd->Expression.Section();
-  Symbol *Sym = Symtab<ELFT>::X->addUndefined(
-      Cmd->Name, /*IsLocal=*/false, STB_GLOBAL, Visibility,
-      /*Type*/ 0,
-      /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
-
+  std::tie(Sym, std::ignore) = Symtab<ELFT>::X->insert(
+      Cmd->Name, /*Type*/ 0, Visibility, /*CanOmitFromDynSym*/ false,
+      /*File*/ nullptr);
+  Sym->Binding = STB_GLOBAL;
   replaceBody<DefinedSynthetic>(Sym, Cmd->Name, 0, Sec);
   return Sym->body();
 }

Modified: lld/trunk/ELF/SymbolTable.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SymbolTable.h?rev=295780&r1=295779&r2=295780&view=diff
==============================================================================
--- lld/trunk/ELF/SymbolTable.h (original)
+++ lld/trunk/ELF/SymbolTable.h Tue Feb 21 16:32:51 2017
@@ -77,6 +77,11 @@ public:
                     uint8_t Binding, uint8_t StOther, uint8_t Type,
                     InputFile *File);
 
+  std::pair<Symbol *, bool> insert(StringRef Name);
+  std::pair<Symbol *, bool> insert(StringRef Name, uint8_t Type,
+                                   uint8_t Visibility, bool CanOmitFromDynSym,
+                                   InputFile *File);
+
   void scanUndefinedFlags();
   void scanShlibUndefined();
   void scanVersionScript();
@@ -90,11 +95,6 @@ public:
   std::vector<InputSectionBase<ELFT> *> Sections;
 
 private:
-  std::pair<Symbol *, bool> insert(StringRef Name);
-  std::pair<Symbol *, bool> insert(StringRef Name, uint8_t Type,
-                                   uint8_t Visibility, bool CanOmitFromDynSym,
-                                   InputFile *File);
-
   std::vector<SymbolBody *> findByVersion(SymbolVersion Ver);
   std::vector<SymbolBody *> findAllByVersion(SymbolVersion Ver);
 

Added: lld/trunk/test/ELF/linkerscript/Inputs/lazy-symbols.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/Inputs/lazy-symbols.s?rev=295780&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript/Inputs/lazy-symbols.s (added)
+++ lld/trunk/test/ELF/linkerscript/Inputs/lazy-symbols.s Tue Feb 21 16:32:51 2017
@@ -0,0 +1,2 @@
+.globl foo
+foo:

Added: lld/trunk/test/ELF/linkerscript/lazy-symbols.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/lazy-symbols.s?rev=295780&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript/lazy-symbols.s (added)
+++ lld/trunk/test/ELF/linkerscript/lazy-symbols.s Tue Feb 21 16:32:51 2017
@@ -0,0 +1,13 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %S/Inputs/lazy-symbols.s -o %t1
+# RUN: llvm-ar rcs %tar %t1
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t2
+# RUN: echo "foo = 1;" > %t.script
+# RUN: ld.lld %t2 %tar --script %t.script -o %tout
+# RUN: llvm-readobj -symbols %tout | FileCheck %s
+
+# This test is to ensure a linker script can define a symbol which have the same
+# name as a lazy symbol.
+
+# CHECK: Name: foo
+# CHECK-NEXT: Value: 0x1




More information about the llvm-commits mailing list