[ELF] - Do not change binding of symbols when creating relocatable output.
authorGeorge Rimar <grimar@accesssoftek.com>
Fri, 19 Aug 2016 08:31:02 +0000 (08:31 +0000)
committerGeorge Rimar <grimar@accesssoftek.com>
Fri, 19 Aug 2016 08:31:02 +0000 (08:31 +0000)
Spec says "A hidden symbol contained in a relocatable object must be either
removed or converted to STB_LOCAL binding by the link-editor when the
relocatable object is included in an executable file or shared object".
But we previously converted symbols to STB_LOCAL even when -r was specified.

Broken binary was produced, this is PR28967, patch fixes the issue.

Differential revision: https://reviews.llvm.org/D23514

llvm-svn: 279220

lld/ELF/OutputSections.cpp
lld/test/ELF/relocatable-visibility.s [new file with mode: 0644]

index 139e675..972457a 100644 (file)
@@ -1316,6 +1316,8 @@ static bool sortMipsSymbols(const std::pair<SymbolBody *, unsigned> &L,
 
 static uint8_t getSymbolBinding(SymbolBody *Body) {
   Symbol *S = Body->symbol();
+  if (Config->Relocatable)
+    return S->Binding;
   uint8_t Visibility = S->Visibility;
   if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
     return STB_LOCAL;
diff --git a/lld/test/ELF/relocatable-visibility.s b/lld/test/ELF/relocatable-visibility.s
new file mode 100644 (file)
index 0000000..7cc7da3
--- /dev/null
@@ -0,0 +1,19 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+# RUN: ld.lld -r %t.o -o %t1
+# RUN: llvm-readobj -t %t1 | FileCheck --check-prefix=RELOCATABLE %s
+
+# RELOCATABLE:      Name: foo
+# RELOCATABLE-NEXT: Value: 0x0
+# RELOCATABLE-NEXT: Size: 0
+# RELOCATABLE-NEXT: Binding: Global
+# RELOCATABLE-NEXT: Type: None
+# RELOCATABLE-NEXT: Other [
+# RELOCATABLE-NEXT:   STV_HIDDEN
+# RELOCATABLE-NEXT: ]
+# RELOCATABLE-NEXT: Section: Undefined
+
+.global _start
+_start:
+ callq foo
+ .hidden foo