[PATCH] D120638: [llvm-objcopy] --weaken-symbol/--weaken: weaken STB_GNU_UNIQUE symbols

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Feb 27 17:20:53 PST 2022


MaskRay created this revision.
MaskRay added reviewers: jhenderson, ikudrin.
Herald added subscribers: abrachet, hiraditya, emaste.
Herald added a reviewer: alexander-shaposhnikov.
Herald added a reviewer: rupprecht.
MaskRay requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

STB_GNU_UNIQUE is like STB_GLOBAL with extra semantics:

- gold and ld.lld: changed to STB_GLOBAL if --no-gnu-unique is specified
- glibc: unique even with dlopen `RTLD_LOCAL`, implies DF_1_NODELETE

Therefore, I think it makes sense for --weaken-symbol/--weaken-symbols/--weaken
to change STB_GNU_UNIQUE symbols.

objcopy PR: https://sourceware.org/bugzilla/show_bug.cgi?id=28926


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120638

Files:
  llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
  llvm/test/tools/llvm-objcopy/ELF/weaken-all.test
  llvm/test/tools/llvm-objcopy/ELF/weaken.test


Index: llvm/test/tools/llvm-objcopy/ELF/weaken.test
===================================================================
--- llvm/test/tools/llvm-objcopy/ELF/weaken.test
+++ llvm/test/tools/llvm-objcopy/ELF/weaken.test
@@ -1,17 +1,19 @@
 # RUN: yaml2obj %s -o %t
-# RUN: llvm-objcopy --weaken-symbol Global -W Local -W Weak -W Missing %t %t2
+# RUN: llvm-objcopy --weaken-symbol Global -W Local -W Weak -W GnuUnique -W Missing %t %t2
 # RUN: llvm-readobj --symbols %t2 | FileCheck %s
 # RUN: llvm-objcopy --regex --weaken-symbol='[LGW].*' %t %t3
 # RUN: cmp %t2 %t3
 # RUN: echo "  Global  # comment" > %t-list.txt
 # RUN: echo "Local" >> %t-list.txt
 # RUN: echo "Weak" >> %t-list.txt
+# RUN: echo "GnuUnique" >> %t-list.txt
 # RUN: echo "Missing" >> %t-list.txt
 # RUN: echo " # comment" >> %t-list.txt
 # RUN: llvm-objcopy --weaken-symbols %t-list.txt %t %t4
 # RUN: cmp %t2 %t4
 # RUN: echo ".*al # Global + Local == .*al" > %t-list2.txt
 # RUN: echo "Weak # weak" >> %t-list2.txt
+# RUN: echo " Gnu.*" >> %t-list2.txt
 # RUN: llvm-objcopy --regex --weaken-symbols %t-list2.txt %t %t5
 # RUN: cmp %t2 %t5
 
@@ -46,6 +48,9 @@
     Section:  .text
     Value:    0x1008
     Binding:  STB_WEAK
+  - Name:     GnuUnique
+    Section:  .text
+    Binding:  STB_GNU_UNIQUE
   - Name:     Unspecified
     Section:  .text
     Binding:  STB_GLOBAL
@@ -88,6 +93,15 @@
 #CHECK-NEXT:    Section: .text
 #CHECK-NEXT:  }
 #CHECK-NEXT:  Symbol {
+#CHECK-NEXT:    Name: GnuUnique
+#CHECK-NEXT:    Value: 0x0
+#CHECK-NEXT:    Size: 0
+#CHECK-NEXT:    Binding: Weak
+#CHECK-NEXT:    Type: None
+#CHECK-NEXT:    Other: 0
+#CHECK-NEXT:    Section: .text
+#CHECK-NEXT:  }
+#CHECK-NEXT:  Symbol {
 #CHECK-NEXT:    Name: Unspecified
 #CHECK-NEXT:    Value: 0x0
 #CHECK-NEXT:    Size: 0
Index: llvm/test/tools/llvm-objcopy/ELF/weaken-all.test
===================================================================
--- llvm/test/tools/llvm-objcopy/ELF/weaken-all.test
+++ llvm/test/tools/llvm-objcopy/ELF/weaken-all.test
@@ -44,6 +44,10 @@
     Section:  .text
     Value:    0x1010
     Binding:  STB_WEAK
+  - Name:     GnuUnique
+    Section:  .text
+    Value:    0x1010
+    Binding:  STB_GNU_UNIQUE
 
 #CHECK: Symbols [
 #CHECK-NEXT:  Symbol {
@@ -91,4 +95,13 @@
 #CHECK-NEXT:    Other: 0
 #CHECK-NEXT:    Section: .text
 #CHECK-NEXT:  }
+#CHECK-NEXT:  Symbol {
+#CHECK-NEXT:    Name: GnuUnique
+#CHECK-NEXT:    Value: 0x1010
+#CHECK-NEXT:    Size: 0
+#CHECK-NEXT:    Binding: Weak
+#CHECK-NEXT:    Type: None
+#CHECK-NEXT:    Other: 0
+#CHECK-NEXT:    Section: .text
+#CHECK-NEXT:  }
 #CHECK-NEXT:]
Index: llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
===================================================================
--- llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
+++ llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
@@ -302,10 +302,11 @@
         Sym.getShndx() != SHN_UNDEF)
       Sym.Binding = STB_GLOBAL;
 
-    if (Config.SymbolsToWeaken.matches(Sym.Name) && Sym.Binding == STB_GLOBAL)
+    // SymbolsToWeaken applies to both STB_GLOBAL and STB_GNU_UNIQUE.
+    if (Config.SymbolsToWeaken.matches(Sym.Name) && Sym.Binding != STB_LOCAL)
       Sym.Binding = STB_WEAK;
 
-    if (Config.Weaken && Sym.Binding == STB_GLOBAL &&
+    if (Config.Weaken && Sym.Binding != STB_LOCAL &&
         Sym.getShndx() != SHN_UNDEF)
       Sym.Binding = STB_WEAK;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D120638.411713.patch
Type: text/x-patch
Size: 3313 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220228/8496bc52/attachment-0001.bin>


More information about the llvm-commits mailing list