[lld] ccbacdd - [lld-macho] Weak locals should be relaxed too

Jez Ng via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 27 17:44:49 PDT 2020


Author: Jez Ng
Date: 2020-08-27T17:44:17-07:00
New Revision: ccbacdded4563baa5f016b1d084cde6c5876bf3e

URL: https://github.com/llvm/llvm-project/commit/ccbacdded4563baa5f016b1d084cde6c5876bf3e
DIFF: https://github.com/llvm/llvm-project/commit/ccbacdded4563baa5f016b1d084cde6c5876bf3e.diff

LOG: [lld-macho] Weak locals should be relaxed too

Reviewed By: #lld-macho, smeenai

Differential Revision: https://reviews.llvm.org/D86746

Added: 
    

Modified: 
    lld/MachO/Arch/X86_64.cpp
    lld/MachO/SyntheticSections.cpp
    lld/MachO/SyntheticSections.h
    lld/test/MachO/weak-binding.s

Removed: 
    


################################################################################
diff  --git a/lld/MachO/Arch/X86_64.cpp b/lld/MachO/Arch/X86_64.cpp
index 4fc20e377f1f..5d61b5b5cd2a 100644
--- a/lld/MachO/Arch/X86_64.cpp
+++ b/lld/MachO/Arch/X86_64.cpp
@@ -221,7 +221,7 @@ void X86_64::prepareSymbolRelocation(lld::macho::Symbol *sym,
                                      const InputSection *isec, const Reloc &r) {
   switch (r.type) {
   case X86_64_RELOC_GOT_LOAD: {
-    if (sym->isWeakDef() || isa<DylibSymbol>(sym))
+    if (needsBinding(sym))
       in.got->addEntry(sym);
 
     if (sym->isTlv())
@@ -275,7 +275,7 @@ void X86_64::prepareSymbolRelocation(lld::macho::Symbol *sym,
     // TODO: warn if they refer to a weak global
     break;
   case X86_64_RELOC_TLV: {
-    if (sym->isWeakDef() || isa<DylibSymbol>(sym))
+    if (needsBinding(sym))
       in.tlvPointers->addEntry(sym);
 
     if (!sym->isTlv())

diff  --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
index 679a1c9bd6a3..0de7b3c03504 100644
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -266,6 +266,16 @@ void WeakBindingSection::writeTo(uint8_t *buf) const {
   memcpy(buf, contents.data(), contents.size());
 }
 
+bool macho::needsBinding(const Symbol *sym) {
+  if (isa<DylibSymbol>(sym)) {
+    return true;
+  } else if (const auto *defined = dyn_cast<Defined>(sym)) {
+    if (defined->isWeakDef() && defined->isExternal())
+      return true;
+  }
+  return false;
+}
+
 void macho::addNonLazyBindingEntries(const Symbol *sym,
                                      SectionPointerUnion section,
                                      uint64_t offset, int64_t addend) {

diff  --git a/lld/MachO/SyntheticSections.h b/lld/MachO/SyntheticSections.h
index 1000dd13fc54..b75fd546a241 100644
--- a/lld/MachO/SyntheticSections.h
+++ b/lld/MachO/SyntheticSections.h
@@ -234,6 +234,9 @@ class WeakBindingSection : public LinkEditSection {
   SmallVector<char, 128> contents;
 };
 
+// Whether a given symbol's address can only be resolved at runtime.
+bool needsBinding(const Symbol *);
+
 // Add bindings for symbols that need weak or non-lazy bindings.
 void addNonLazyBindingEntries(const Symbol *, SectionPointerUnion,
                               uint64_t offset, int64_t addend = 0);

diff  --git a/lld/test/MachO/weak-binding.s b/lld/test/MachO/weak-binding.s
index 6f5c559994e8..3474d35ce921 100644
--- a/lld/test/MachO/weak-binding.s
+++ b/lld/test/MachO/weak-binding.s
@@ -21,9 +21,10 @@
 # CHECK:      <_main>:
 # CHECK-NEXT: movq	[[#]](%rip), %rax  # [[#%X,WEAK_DY_GOT_ADDR:]]
 # CHECK-NEXT: movq	[[#]](%rip), %rax  # [[#%X,WEAK_EXT_GOT_ADDR:]]
+# CHECK-NEXT: leaq	[[#]](%rip), %rax  # [[#%X,WEAK_INT_GOT_ADDR:]]
 # CHECK-NEXT: movq	[[#]](%rip), %rax  # [[#%X,WEAK_TLV_ADDR:]]
 # CHECK-NEXT: movq	[[#]](%rip), %rax  # [[#%X,WEAK_DY_TLV_ADDR:]]
-# CHECK-NEXT: movq	[[#]](%rip), %rax  # [[#%X,WEAK_INT_TLV_ADDR:]]
+# CHECK-NEXT: leaq	[[#]](%rip), %rax  # [[#%X,WEAK_INT_TLV_ADDR:]]
 # CHECK-NEXT: callq 0x{{[0-9a-f]*}}
 # CHECK-NEXT: callq 0x{{[0-9a-f]*}}
 # CHECK-NEXT: callq 0x{{[0-9a-f]*}}
@@ -86,11 +87,12 @@ _weak_dysym_tlv:
 #--- test.s
 
 .globl _main, _weak_external, _weak_external_for_gotpcrel, _weak_external_fn
-.weak_definition _weak_external, _weak_external_for_gotpcrel, _weak_external_fn, _weak_internal, _weak_internal_fn
+.weak_definition _weak_external, _weak_external_for_gotpcrel, _weak_external_fn, _weak_internal, _weak_internal_for_gotpcrel, _weak_internal_fn
 
 _main:
   mov _weak_dysym_for_gotpcrel at GOTPCREL(%rip), %rax
   mov _weak_external_for_gotpcrel at GOTPCREL(%rip), %rax
+  mov _weak_internal_for_gotpcrel at GOTPCREL(%rip), %rax
   mov _weak_tlv at TLVP(%rip), %rax
   mov _weak_dysym_tlv at TLVP(%rip), %rax
   mov _weak_internal_tlv at TLVP(%rip), %rax
@@ -112,6 +114,9 @@ _weak_external_fn:
 _weak_internal:
   .quad 0x1234
 
+_weak_internal_for_gotpcrel:
+  .quad 0x1234
+
 _weak_internal_fn:
   ret
 


        


More information about the llvm-commits mailing list