[PATCH] D59549: [ELF] Don't emit weak undefined symbols in static executables

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 19 08:42:57 PDT 2019


MaskRay created this revision.
MaskRay added reviewers: ruiu, pcc, sivachandra.
Herald added subscribers: llvm-commits, jdoerfert, arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.

D59275 <https://reviews.llvm.org/D59275> added the following clause to Symbol::includeInDynsym()

  if (isUndefWeak() && Config->Pie && SharedFiles.empty())
    return false;

It seems we don't have to differentiate non-PIE from PIE, thus we can
generalize it to weak undefined symbols in any static executable:

  if (!Config->Shared && isUndefWeak() && SharedFiles.empty())
    return false;

Note `!Config->Shared && isUndefWeak()` is used to decide whether a
relocation which is not a constant at link time should resolve to 0.
This patch adjusts the order of clauses to make the code simpler.

ld.bfd has complex and architecture-varying rules. We try to provide
reasonable semantics to exclude some weak undefined symbols but not be
too smart. Future improvement may include the refinement of
ExportDynamic assignment.

In the updated tests, weak-undef-rw.s is folded into
weak-undef-exported.s as they are checking the same thing except that
the former checks a writable section.


Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D59549

Files:
  ELF/Symbols.cpp
  test/ELF/weak-undef-export.s
  test/ELF/weak-undef-rw.s


Index: test/ELF/weak-undef-rw.s
===================================================================
--- test/ELF/weak-undef-rw.s
+++ /dev/null
@@ -1,12 +0,0 @@
-# REQUIRES: x86
-# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-# RUN: ld.lld %t.o -o %t --export-dynamic
-# RUN: llvm-readobj -r %t | FileCheck %s
-
-# CHECK: R_X86_64_64 foobar 0x0
-
-        .global _start
-_start:
-        .data
-        .weak foobar
-        .quad foobar
Index: test/ELF/weak-undef-export.s
===================================================================
--- test/ELF/weak-undef-export.s
+++ test/ELF/weak-undef-export.s
@@ -16,16 +16,12 @@
 # CHECK-NEXT:     Other: 0
 # CHECK-NEXT:     Section: Undefined (0x0)
 # CHECK-NEXT:   }
-# CHECK-NEXT:   Symbol {
-# CHECK-NEXT:     Name: foo
-# CHECK-NEXT:     Value: 0x0
-# CHECK-NEXT:     Size: 0
-# CHECK-NEXT:     Binding: Weak (0x2)
-# CHECK-NEXT:     Type: None (0x0)
-# CHECK-NEXT:     Other: 0
-# CHECK-NEXT:     Section: Undefined (0x0)
-# CHECK-NEXT:  }
 # CHECK-NEXT: ]
 
+        .text
+        .weak foo
+        .quad foo
+
+        .data
         .weak foo
         .quad foo
Index: ELF/Symbols.cpp
===================================================================
--- ELF/Symbols.cpp
+++ ELF/Symbols.cpp
@@ -267,13 +267,11 @@
     return false;
   if (computeBinding() == STB_LOCAL)
     return false;
-  // If a PIE binary was not linked against any shared libraries, then we can
-  // safely drop weak undef symbols from .dynsym.
-  if (isUndefWeak() && Config->Pie && SharedFiles.empty())
-    return false;
-  if (!isDefined())
-    return true;
-  return ExportDynamic;
+  if (isDefined())
+    return ExportDynamic;
+  // When linking a static executable (emulated by SharedFiles.empty()), weak
+  // undef symbols can be safely dropped from .dynsym.
+  return Config->Shared || !isUndefWeak() || !SharedFiles.empty();
 }
 
 // Print out a log message for --trace-symbol.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59549.191314.patch
Type: text/x-patch
Size: 1946 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190319/2043b0ed/attachment.bin>


More information about the llvm-commits mailing list