diff --git a/lld/MachO/SymbolTable.cpp b/lld/MachO/SymbolTable.cpp --- a/lld/MachO/SymbolTable.cpp +++ b/lld/MachO/SymbolTable.cpp @@ -150,6 +150,7 @@ std::tie(s, wasInserted) = insert(name, file); RefState refState = RefState::Unreferenced; + bool wasForceWeak = false; if (!wasInserted) { if (auto *defined = dyn_cast(s)) { if (isWeakDef && !defined->isWeakDef()) @@ -158,14 +159,20 @@ refState = undefined->refState; } else if (auto *dysym = dyn_cast(s)) { refState = dysym->getRefState(); + if (DylibFile *prevFile = dysym->getFile()) + wasForceWeak = prevFile->forceWeakImport; } } bool isDynamicLookup = file == nullptr; - if (wasInserted || isa(s) || - (isa(s) && - ((!isWeakDef && s->isWeakDef()) || - (!isDynamicLookup && cast(s)->isDynamicLookup())))) { + if (!isDynamicLookup && !wasInserted && file->forceWeakImport && + !wasForceWeak) { + replaceSymbol(s, file, name, isWeakDef, refState, isTlv); + } else if (wasInserted || isa(s) || + (isa(s) && + ((!isWeakDef && s->isWeakDef()) || + (!isDynamicLookup && + cast(s)->isDynamicLookup())))) { if (auto *dynsym = dyn_cast(s)) dynsym->unreference(); replaceSymbol(s, file, name, isWeakDef, refState, isTlv); diff --git a/lld/MachO/Symbols.h b/lld/MachO/Symbols.h --- a/lld/MachO/Symbols.h +++ b/lld/MachO/Symbols.h @@ -235,8 +235,13 @@ } uint64_t getVA() const override; - bool isWeakDef() const override { return weakDef; } - bool isWeakRef() const override { return refState == RefState::Weak; } + bool isWeakDef() const override { + return weakDef || (file && dyn_cast(file)->forceWeakImport); + } + bool isWeakRef() const override { + return refState == RefState::Weak || + (file && dyn_cast(file)->forceWeakImport); + } bool isReferenced() const { return refState != RefState::Unreferenced; } bool isTlv() const override { return tlv; } bool isDynamicLookup() const { return file == nullptr; } diff --git a/lld/test/MachO/weak-import.s b/lld/test/MachO/weak-import.s --- a/lld/test/MachO/weak-import.s +++ b/lld/test/MachO/weak-import.s @@ -9,12 +9,12 @@ # RUN: %lld -lSystem -dylib %t/bar.o -o %t/libbar.dylib # RUN: %lld -lSystem -dylib %t/foo.o %t/libbar.dylib -sub_library libbar -o %t/libfoo.dylib -# RUN: %lld -weak-lSystem %t/test.o -weak_framework CoreFoundation -weak_library %t/libfoo.dylib -o %t/test -# RUN: llvm-objdump --macho --all-headers %t/test | FileCheck %s -DDIR=%t --check-prefixes=WEAK-SYS,WEAK-FOO +# RUN: %lld -weak-lSystem %t/test.o -weak_framework CoreFoundation -weak_library %t/libfoo.dylib -o %t/test_weak_core +# RUN: llvm-objdump --macho --all-headers %t/test_weak_core | FileCheck %s -DDIR=%t --check-prefixes=WEAK-SYS,WEAK-FOO # RUN: %lld -weak-lSystem %t/test.o \ # RUN: -framework CoreFoundation -weak_framework CoreFoundation -framework CoreFoundation \ -# RUN: %t/libfoo.dylib -weak_library %t/libfoo.dylib %t/libfoo.dylib -o %t/test -# RUN: llvm-objdump --macho --all-headers %t/test | FileCheck %s -DDIR=%t --check-prefixes=WEAK-SYS,WEAK-FOO +# RUN: %t/libfoo.dylib -weak_library %t/libfoo.dylib %t/libfoo.dylib -o %t/test_strong_weak_core +# RUN: llvm-objdump --macho --all-headers %t/test_strong_weak_core | FileCheck %s -DDIR=%t --check-prefixes=WEAK-SYS,WEAK-FOO # RUN: %lld -lSystem -dylib %t/libfoo.dylib %t/weak-ref-only.o -o %t/weak-ref-only # RUN: llvm-objdump --macho --all-headers %t/weak-ref-only | FileCheck %s -DDIR=%t --check-prefixes=SYS,WEAK-FOO # RUN: %lld -lSystem -dylib %t/libfoo.dylib %t/weak-ref-sub-library.o -o %t/weak-ref-sub-library @@ -22,6 +22,11 @@ # RUN: %lld -lSystem -dylib %t/libfoo.dylib %t/mixed-ref.o -o %t/mixed-ref # RUN: llvm-objdump --macho --all-headers %t/mixed-ref | FileCheck %s -DDIR=%t --check-prefixes=SYS,FOO +# RUN: %lld -framework CoreFoundation %t/test.o -weak_framework CoreFoundation -o %t/strong_weak_import.out +# RUN: %lld -weak_framework CoreFoundation %t/test.o -framework CoreFoundation -o %t/weak_strong_import.out +# RUN: llvm-objdump --macho --bind %t/strong_weak_import.out | FileCheck %s --check-prefix=WEAK-IMP +# RUN: llvm-objdump --macho --bind %t/weak_strong_import.out | FileCheck %s --check-prefix=WEAK-IMP + # WEAK-SYS: cmd LC_LOAD_WEAK_DYLIB # WEAK-SYS-NEXT: cmdsize # WEAK-SYS-NEXT: name /usr/lib/libSystem.dylib @@ -42,6 +47,7 @@ # FOO-NEXT: cmdsize # FOO-NEXT: name [[DIR]]/libfoo.dylib +# WEAK-IMP: {{.+}} pointer 0 CoreFoundation __CFBigNumGetInt128 (weak_import) #--- foo.s .globl _foo _foo: @@ -69,4 +75,5 @@ #--- test.s .globl _main _main: + movq __CFBigNumGetInt128@GOTPCREL(%rip), %rax ret