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

Jez Ng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 27 17:45:15 PDT 2020


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGccbacdded456: [lld-macho] Weak locals should be relaxed too (authored by int3).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D86746/new/

https://reviews.llvm.org/D86746

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


Index: lld/test/MachO/weak-binding.s
===================================================================
--- lld/test/MachO/weak-binding.s
+++ 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 @@
 #--- 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_internal:
   .quad 0x1234
 
+_weak_internal_for_gotpcrel:
+  .quad 0x1234
+
 _weak_internal_fn:
   ret
 
Index: lld/MachO/SyntheticSections.h
===================================================================
--- lld/MachO/SyntheticSections.h
+++ lld/MachO/SyntheticSections.h
@@ -234,6 +234,9 @@
   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);
Index: lld/MachO/SyntheticSections.cpp
===================================================================
--- lld/MachO/SyntheticSections.cpp
+++ lld/MachO/SyntheticSections.cpp
@@ -266,6 +266,16 @@
   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) {
Index: lld/MachO/Arch/X86_64.cpp
===================================================================
--- lld/MachO/Arch/X86_64.cpp
+++ lld/MachO/Arch/X86_64.cpp
@@ -221,7 +221,7 @@
                                      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 @@
     // 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())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D86746.288493.patch
Type: text/x-patch
Size: 3561 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200828/bf4112f3/attachment.bin>


More information about the llvm-commits mailing list