[PATCH] D40503: Store the real binding of shared symbols

Rafael Ávila de Espíndola via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 27 09:27:02 PST 2017


rafael created this revision.
Herald added subscribers: arichardson, emaste.

Currently we mark every shared symbol as STB_WEAK.

That is a hack to make it easy to decide when a .so is needed or not because of a reference to a given symbol.

That hack leaks when we create copy relocations as shown by the update to relocation-copy-alias.s.

This patch stores the original binding when we first read a shared symbol. We still have to update the binding to weak if we see a weak undef, but I find the logic easier to read where it is now.


https://reviews.llvm.org/D40503

Files:
  ELF/SymbolTable.cpp
  ELF/Symbols.h
  test/ELF/relocation-copy-alias.s


Index: test/ELF/relocation-copy-alias.s
===================================================================
--- test/ELF/relocation-copy-alias.s
+++ test/ELF/relocation-copy-alias.s
@@ -61,7 +61,7 @@
 // CHECK:      Name: b3
 // CHECK-NEXT: Value: [[B]]
 // CHECK-NEXT: Size: 1
-// CHECK-NEXT: Binding: Weak
+// CHECK-NEXT: Binding: Global
 // CHECK-NEXT: Type: Object (0x1)
 // CHECK-NEXT: Other: 0
 // CHECK-NEXT: Section: .bss
Index: ELF/Symbols.h
===================================================================
--- ELF/Symbols.h
+++ ELF/Symbols.h
@@ -209,10 +209,11 @@
 public:
   static bool classof(const Symbol *S) { return S->kind() == SharedKind; }
 
-  SharedSymbol(StringRef Name, uint8_t StOther, uint8_t Type, uint64_t Value,
-               uint64_t Size, uint32_t Alignment, const void *Verdef)
-      : Symbol(SharedKind, Name, llvm::ELF::STB_WEAK, StOther, Type),
-        Verdef(Verdef), Value(Value), Size(Size), Alignment(Alignment) {
+  SharedSymbol(StringRef Name, uint8_t Binding, uint8_t StOther, uint8_t Type,
+               uint64_t Value, uint64_t Size, uint32_t Alignment,
+               const void *Verdef)
+      : Symbol(SharedKind, Name, Binding, StOther, Type), Verdef(Verdef),
+        Value(Value), Size(Size), Alignment(Alignment) {
     // GNU ifunc is a mechanism to allow user-supplied functions to
     // resolve PLT slot values at load-time. This is contrary to the
     // regualr symbol resolution scheme in which symbols are resolved just
Index: ELF/SymbolTable.cpp
===================================================================
--- ELF/SymbolTable.cpp
+++ ELF/SymbolTable.cpp
@@ -300,22 +300,20 @@
     replaceSymbol<Undefined>(S, File, Name, Binding, StOther, Type);
     return S;
   }
+  if (S->isShared() || S->isLazy() || (S->isUndefined() && Binding != STB_WEAK))
+    S->Binding = Binding;
   if (Binding != STB_WEAK) {
-    if (!S->isDefined())
-      S->Binding = Binding;
     if (auto *SS = dyn_cast<SharedSymbol>(S))
       if (!Config->GcSections)
         SS->getFile<ELFT>()->IsNeeded = true;
   }
   if (auto *L = dyn_cast<Lazy>(S)) {
     // An undefined weak will not fetch archive members. See comment on Lazy in
     // Symbols.h for the details.
-    if (Binding == STB_WEAK) {
+    if (Binding == STB_WEAK)
       L->Type = Type;
-      L->Binding = STB_WEAK;
-    } else if (InputFile *F = L->fetch()) {
+    else if (InputFile *F = L->fetch())
       addFile<ELFT>(F);
-    }
   }
   return S;
 }
@@ -497,8 +495,9 @@
   if (WasInserted || ((S->isUndefined() || S->isLazy()) &&
                       S->getVisibility() == STV_DEFAULT)) {
     uint8_t Binding = S->Binding;
-    replaceSymbol<SharedSymbol>(S, File, Name, Sym.st_other, Sym.getType(),
-                                Sym.st_value, Sym.st_size, Alignment, Verdef);
+    replaceSymbol<SharedSymbol>(S, File, Name, Sym.getBinding(), Sym.st_other,
+                                Sym.getType(), Sym.st_value, Sym.st_size,
+                                Alignment, Verdef);
     if (!WasInserted) {
       S->Binding = Binding;
       if (!S->isWeak() && !Config->GcSections)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D40503.124403.patch
Type: text/x-patch
Size: 3121 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171127/680fe2c3/attachment.bin>


More information about the llvm-commits mailing list