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

Differential Revision: https://reviews.llvm.org/D131995
This commit is contained in:
Daniel Bertalan 2022-08-16 23:09:46 +02:00
parent d27c147aaa
commit 1b67ce79e3
2 changed files with 40 additions and 16 deletions

View File

@ -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;
}
}

View File

@ -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@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 ]
---