[lld] r244636 - Add support for weak symbols.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 11 10:33:03 PDT 2015


Author: rafael
Date: Tue Aug 11 12:33:02 2015
New Revision: 244636

URL: http://llvm.org/viewvc/llvm-project?rev=244636&view=rev
Log:
Add support for weak symbols.

Added:
    lld/trunk/test/elf2/Inputs/invalid-binding.elf
    lld/trunk/test/elf2/Inputs/resolution.s
    lld/trunk/test/elf2/resolution.s
      - copied, changed from r244632, lld/trunk/test/elf2/local.s
Removed:
    lld/trunk/test/elf2/Inputs/local.s
    lld/trunk/test/elf2/local.s
Modified:
    lld/trunk/ELF/InputFiles.cpp
    lld/trunk/ELF/Symbols.cpp
    lld/trunk/ELF/Symbols.h
    lld/trunk/test/elf2/invalid-elf.test

Modified: lld/trunk/ELF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.cpp?rev=244636&r1=244635&r2=244636&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.cpp (original)
+++ lld/trunk/ELF/InputFiles.cpp Tue Aug 11 12:33:02 2015
@@ -75,9 +75,17 @@ SymbolBody *elf2::ObjectFile<ELFT>::crea
   ErrorOr<StringRef> NameOrErr = Sym->getName(StringTable);
   error(NameOrErr.getError());
   StringRef Name = *NameOrErr;
-  if (Sym->isUndefined())
-    return new (Alloc) Undefined(Name);
-  return new (Alloc) DefinedRegular<ELFT>(Name);
+  switch (Sym->getBinding()) {
+  default:
+    error("unexpected binding");
+  case STB_GLOBAL:
+    if (Sym->isUndefined())
+      return new (Alloc) Undefined(Name);
+    return new (Alloc) DefinedRegular<ELFT>(Name);
+  case STB_WEAK:
+    // FIXME: add support for weak undefined
+    return new (Alloc) DefinedWeak<ELFT>(Name);
+  }
 }
 
 namespace lld {

Modified: lld/trunk/ELF/Symbols.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Symbols.cpp?rev=244636&r1=244635&r2=244636&view=diff
==============================================================================
--- lld/trunk/ELF/Symbols.cpp (original)
+++ lld/trunk/ELF/Symbols.cpp Tue Aug 11 12:33:02 2015
@@ -21,6 +21,10 @@ template <class ELFT>
 DefinedRegular<ELFT>::DefinedRegular(StringRef Name)
     : Defined(DefinedRegularKind, Name) {}
 
+template <class ELFT>
+DefinedWeak<ELFT>::DefinedWeak(StringRef Name)
+    : Defined(DefinedWeakKind, Name) {}
+
 // Returns 1, 0 or -1 if this symbol should take precedence
 // over the Other, tie or lose, respectively.
 int SymbolBody::compare(SymbolBody *Other) {
@@ -32,16 +36,14 @@ int SymbolBody::compare(SymbolBody *Othe
     return -Other->compare(this);
 
   // First handle comparisons between two different kinds.
-  if (LK != RK) {
-    assert(LK == DefinedRegularKind);
-    assert(RK == UndefinedKind);
+  if (LK != RK)
     return 1;
-  }
 
   // Now handle the case where the kinds are the same.
   switch (LK) {
   case DefinedRegularKind:
     return 0;
+  case DefinedWeakKind:
   case UndefinedKind:
     return 1;
   }
@@ -54,5 +56,10 @@ template class DefinedRegular<llvm::obje
 template class DefinedRegular<llvm::object::ELF32BE>;
 template class DefinedRegular<llvm::object::ELF64LE>;
 template class DefinedRegular<llvm::object::ELF64BE>;
+
+template class DefinedWeak<llvm::object::ELF32LE>;
+template class DefinedWeak<llvm::object::ELF32BE>;
+template class DefinedWeak<llvm::object::ELF64LE>;
+template class DefinedWeak<llvm::object::ELF64BE>;
 }
 }

Modified: lld/trunk/ELF/Symbols.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Symbols.h?rev=244636&r1=244635&r2=244636&view=diff
==============================================================================
--- lld/trunk/ELF/Symbols.h (original)
+++ lld/trunk/ELF/Symbols.h Tue Aug 11 12:33:02 2015
@@ -37,8 +37,9 @@ public:
   enum Kind {
     DefinedFirst = 0,
     DefinedRegularKind = 0,
-    DefinedLast = 0,
-    UndefinedKind = 1,
+    DefinedWeakKind = 1,
+    DefinedLast = 1,
+    UndefinedKind = 2
   };
 
   Kind kind() const { return static_cast<Kind>(SymbolKind); }
@@ -92,6 +93,15 @@ public:
   }
 };
 
+template <class ELFT> class DefinedWeak : public Defined {
+public:
+  DefinedWeak(StringRef Name);
+
+  static bool classof(const SymbolBody *S) {
+    return S->kind() == DefinedWeakKind;
+  }
+};
+
 // Undefined symbols.
 class Undefined : public SymbolBody {
 public:

Added: lld/trunk/test/elf2/Inputs/invalid-binding.elf
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/Inputs/invalid-binding.elf?rev=244636&view=auto
==============================================================================
Binary files lld/trunk/test/elf2/Inputs/invalid-binding.elf (added) and lld/trunk/test/elf2/Inputs/invalid-binding.elf Tue Aug 11 12:33:02 2015 differ

Removed: lld/trunk/test/elf2/Inputs/local.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/Inputs/local.s?rev=244635&view=auto
==============================================================================
--- lld/trunk/test/elf2/Inputs/local.s (original)
+++ lld/trunk/test/elf2/Inputs/local.s (removed)
@@ -1 +0,0 @@
-local:

Added: lld/trunk/test/elf2/Inputs/resolution.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/Inputs/resolution.s?rev=244636&view=auto
==============================================================================
--- lld/trunk/test/elf2/Inputs/resolution.s (added)
+++ lld/trunk/test/elf2/Inputs/resolution.s Tue Aug 11 12:33:02 2015
@@ -0,0 +1,10 @@
+local:
+
+.weak foo
+foo:
+
+.weak _start
+_start:
+
+.weak bar
+bar:

Modified: lld/trunk/test/elf2/invalid-elf.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/invalid-elf.test?rev=244636&r1=244635&r2=244636&view=diff
==============================================================================
--- lld/trunk/test/elf2/invalid-elf.test (original)
+++ lld/trunk/test/elf2/invalid-elf.test Tue Aug 11 12:33:02 2015
@@ -9,3 +9,7 @@ INVALID-FILE-CLASS: Invalid file class
 RUN: not lld -flavor gnu2 %p/Inputs/invalid-symtab-sh_info.elf -o %t2 2>&1 | \
 RUN:   FileCheck --check-prefix=INVALID-SYMTAB-SHINFO %s
 INVALID-SYMTAB-SHINFO: Invalid sh_info in symbol table
+
+RUN: not lld -flavor gnu2 %p/Inputs/invalid-binding.elf -o %t2 2>&1 | \
+RUN:   FileCheck --check-prefix=INVALID-BINDING %s
+INVALID-BINDING: unexpected binding

Removed: lld/trunk/test/elf2/local.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/local.s?rev=244635&view=auto
==============================================================================
--- lld/trunk/test/elf2/local.s (original)
+++ lld/trunk/test/elf2/local.s (removed)
@@ -1,10 +0,0 @@
-// RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux %s -o %t
-// RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux %p/Inputs/local.s -o %t2
-// RUN: lld -flavor gnu2 %t %t2 -o %t3
-// REQUIRES: x86
-
-.globl _start
-_start:
-        nop
-
-local:

Copied: lld/trunk/test/elf2/resolution.s (from r244632, lld/trunk/test/elf2/local.s)
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/resolution.s?p2=lld/trunk/test/elf2/resolution.s&p1=lld/trunk/test/elf2/local.s&r1=244632&r2=244636&rev=244636&view=diff
==============================================================================
--- lld/trunk/test/elf2/local.s (original)
+++ lld/trunk/test/elf2/resolution.s Tue Aug 11 12:33:02 2015
@@ -1,5 +1,5 @@
 // RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux %s -o %t
-// RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux %p/Inputs/local.s -o %t2
+// RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux %p/Inputs/resolution.s -o %t2
 // RUN: lld -flavor gnu2 %t %t2 -o %t3
 // REQUIRES: x86
 
@@ -8,3 +8,8 @@ _start:
         nop
 
 local:
+
+.weak foo
+foo:
+
+.long bar




More information about the llvm-commits mailing list