[lld] 1b67ce7 - [lld-macho] Honor weak and thread-local flags for TAPI symbols

Daniel Bertalan via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 16 23:00:23 PDT 2022


Author: Daniel Bertalan
Date: 2022-08-17T07:03:24+02:00
New Revision: 1b67ce79e3c8c8aa4f26973d58914c4b4fdff000

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

LOG: [lld-macho] Honor weak and thread-local flags for TAPI symbols

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

Added: 
    

Modified: 
    lld/MachO/InputFiles.cpp
    lld/test/MachO/tapi-link.s

Removed: 
    


################################################################################
diff  --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index 62f27c7262a8e..55d4888f60c2d 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -1945,14 +1945,15 @@ DylibFile::DylibFile(const InterfaceFile &interface, DylibFile *umbrella,
   checkAppExtensionSafety(interface.isApplicationExtensionSafe());
 
   exportingFile = isImplicitlyLinked(installName) ? this : umbrella;
-  auto addSymbol = [&](const Twine &name) -> void {
+  auto addSymbol = [&](const llvm::MachO::Symbol &symbol,
+                       const Twine &name) -> void {
     StringRef savedName = saver().save(name);
     if (exportingFile->hiddenSymbols.contains(CachedHashStringRef(savedName)))
       return;
 
     symbols.push_back(symtab->addDylib(savedName, exportingFile,
-                                       /*isWeakDef=*/false,
-                                       /*isTlv=*/false));
+                                       symbol.isWeakDefined(),
+                                       symbol.isThreadLocalValue()));
   };
 
   std::vector<const llvm::MachO::Symbol *> normalSymbols;
@@ -1973,23 +1974,22 @@ DylibFile::DylibFile(const InterfaceFile &interface, DylibFile *umbrella,
   }
 
   // TODO(compnerd) filter out symbols based on the target platform
-  // TODO: handle weak defs, thread locals
   for (const auto *symbol : normalSymbols) {
     switch (symbol->getKind()) {
     case SymbolKind::GlobalSymbol:
-      addSymbol(symbol->getName());
+      addSymbol(*symbol, symbol->getName());
       break;
     case SymbolKind::ObjectiveCClass:
       // XXX ld64 only creates these symbols when -ObjC is passed in. We may
       // want to emulate that.
-      addSymbol(objc::klass + symbol->getName());
-      addSymbol(objc::metaclass + symbol->getName());
+      addSymbol(*symbol, objc::klass + symbol->getName());
+      addSymbol(*symbol, objc::metaclass + symbol->getName());
       break;
     case SymbolKind::ObjectiveCClassEHType:
-      addSymbol(objc::ehtype + symbol->getName());
+      addSymbol(*symbol, objc::ehtype + symbol->getName());
       break;
     case SymbolKind::ObjectiveCInstanceVariable:
-      addSymbol(objc::ivar + symbol->getName());
+      addSymbol(*symbol, objc::ivar + symbol->getName());
       break;
     }
   }

diff  --git a/lld/test/MachO/tapi-link.s b/lld/test/MachO/tapi-link.s
index a47be95f91077..b2aa2f208281e 100644
--- a/lld/test/MachO/tapi-link.s
+++ b/lld/test/MachO/tapi-link.s
@@ -4,16 +4,16 @@
 
 # RUN: llvm-mc -filetype obj -triple x86_64-apple-darwin %t/test.s -o %t/test.o
 
-# RUN: %lld -o %t/test -lSystem -lc++ -framework CoreFoundation %t/libNested.tbd %t/test.o
-# RUN: llvm-objdump --bind --no-show-raw-insn -d -r %t/test | FileCheck %s
+# RUN: %lld -o %t/test -lSystem -lc++ -framework CoreFoundation %t/libNested.tbd %t/libTlvWeak.tbd %t/test.o
+# RUN: llvm-objdump --bind --weak-bind --no-show-raw-insn -d -r %t/test | FileCheck %s
 
 ## Targeting an arch not listed in the tbd should fallback to an ABI compatible arch
-# RUN: %lld -arch x86_64h -o %t/test-compat -lSystem -lc++ -framework CoreFoundation %t/libNested.tbd %t/test.o
-# RUN: llvm-objdump --bind --no-show-raw-insn -d -r %t/test-compat | FileCheck %s
+# RUN: %lld -arch x86_64h -o %t/test-compat -lSystem -lc++ -framework CoreFoundation %t/libNested.tbd %t/libTlvWeak.tbd %t/test.o
+# RUN: llvm-objdump --bind --weak-bind --no-show-raw-insn -d -r %t/test-compat | FileCheck %s
 
 ## Setting LD_DYLIB_CPU_SUBTYPES_MUST_MATCH forces exact target arch match.
 # RUN: env LD_DYLIB_CPU_SUBTYPES_MUST_MATCH=1 not %lld -arch x86_64h -o /dev/null -lSystem -lc++ -framework \
-# RUN:   CoreFoundation %t/libNested.tbd %t/test.o 2>&1 | FileCheck %s -check-prefix=INCOMPATIBLE
+# RUN:   CoreFoundation %t/libNested.tbd %t/libTlvWeak.tbd %t/test.o 2>&1 | FileCheck %s -check-prefix=INCOMPATIBLE
 
 # INCOMPATIBLE:      error: {{.*}}libSystem.tbd(/usr/lib/libSystem.dylib) is incompatible with x86_64h (macOS)
 # INCOMPATIBLE-NEXT: error: {{.*}}libc++.tbd(/usr/lib/libc++.dylib) is incompatible with x86_64h (macOS)
@@ -22,8 +22,8 @@
 ## libReexportSystem.tbd tests that we can reference symbols from a 2nd-level
 ## tapi document, re-exported by a top-level tapi document, which itself is
 ## re-exported by another top-level tapi document.
-# RUN: %lld -o %t/with-reexport -lSystem -L%t %t/libReexportNested.tbd -lc++ -framework CoreFoundation %t/test.o
-# RUN: llvm-objdump --bind --no-show-raw-insn -d -r %t/with-reexport | FileCheck %s
+# RUN: %lld -o %t/with-reexport -lSystem -L%t %t/libReexportNested.tbd %t/libTlvWeak.tbd -lc++ -framework CoreFoundation %t/test.o
+# RUN: llvm-objdump --bind --weak-bind --no-show-raw-insn -d -r %t/with-reexport | FileCheck %s
 
 # CHECK: Bind table:
 # CHECK-DAG: __DATA __data {{.*}} pointer 0 CoreFoundation _OBJC_CLASS_$_NSObject
@@ -32,6 +32,11 @@
 # CHECK-DAG: __DATA __data {{.*}} pointer 0 CoreFoundation _OBJC_EHTYPE_$_NSException
 # CHECK-DAG: __DATA __data {{.*}} pointer 0 libc++abi      ___gxx_personality_v0
 # CHECK-DAG: __DATA __data {{.*}} pointer 0 libNested3     _deeply_nested
+# CHECK-DAG: __DATA __data {{.*}} pointer 0 libTlvWeak     _weak
+# CHECK-DAG: __DATA __thread_ptrs {{.*}} pointer 0 libTlvWeak _tlv
+
+# CHECK: Weak bind table:
+# CHECK-DAG: __DATA __data {{.*}} pointer 0 _weak
 
 # RUN: llvm-otool -l %t/test | FileCheck --check-prefix=LOAD %s
 
@@ -57,6 +62,7 @@
 .global _main
 
 _main:
+  mov _tlv at TLVP(%rip), %rax
   ret
 
 .data
@@ -73,6 +79,8 @@ _main:
 ## same .tbd file, but to other on-disk files as well.
   .quad ___gxx_personality_v0
 
+  .quad _weak
+
 ## This tests that we can locate a symbol re-exported by a child of a TAPI
 ## document.
 #--- libNested.tbd
@@ -112,3 +120,19 @@ exports:
   - archs:      [ i386, x86_64 ]
     re-exports: [ 'libNested.dylib' ]
 ...
+
+## This tests that weak and thread-local symbols are imported as such.
+#--- libTlvWeak.tbd
+--- !tapi-tbd
+tbd-version:      4
+targets:          [ x86_64-macos ]
+uuids:
+  - target:       x86_64-macos
+    value:        00000000-0000-0000-0000-000000000000
+install-name:     '/usr/lib/libTlvWeak.dylib'
+current-version:  0001.001.1
+exports:
+  - targets:      [ x86_64-macos ]
+    weak-symbols: [ _weak ]
+    thread-local-symbols: [ _tlv ]
+---


        


More information about the llvm-commits mailing list