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,16 @@ } uint64_t getVA() const override; + bool isWeakDef() const override { return weakDef; } - bool isWeakRef() const override { return refState == RefState::Weak; } + + // Libraries and frameworks can be loaded multiple times with different + // forceWeakImport (eg., first loaded as strong then as weak) + // When that happens we must ensure the weak-property sticks. + bool isWeakRef() const override { + return refState == RefState::Weak || + (file && getFile()->umbrella->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/Inputs/MacOSX.sdk/System/Library/Frameworks/NewCoreFoundation.framework/NewCoreFoundation.tbd b/lld/test/MachO/Inputs/MacOSX.sdk/System/Library/Frameworks/NewCoreFoundation.framework/NewCoreFoundation.tbd new file mode 100644 --- /dev/null +++ b/lld/test/MachO/Inputs/MacOSX.sdk/System/Library/Frameworks/NewCoreFoundation.framework/NewCoreFoundation.tbd @@ -0,0 +1,14 @@ +--- !tapi-tbd-v3 +archs: [ x86_64 ] +uuids: [ 'x86_64: 00000000-0000-0000-0000-000000000000' ] +platform: macosx +install-name: '/System/Library/Frameworks/CoreFoundation.framework/NewCoreFoundation' +current-version: 0001.001.1 +compatibility-version: 150 +exports: + - archs: [ 'x86_64' ] + symbols: [ __CFBigNumGetInt128, ___CFConstantStringClassReference ] + objc-classes: [ NSObject ] + objc-ivars: [ NSConstantArray._count ] + objc-eh-types: [ NSException ] +... 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 @@ -6,15 +6,18 @@ # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/mixed-ref.s -o %t/mixed-ref.o # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/foo.s -o %t/foo.o # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/bar.s -o %t/bar.o +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/weak-reexport.s -o %t/weak-reexport.o + + # 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/basic +# RUN: llvm-objdump --macho --all-headers %t/basic | 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/basic-weak-strong +# RUN: llvm-objdump --macho --all-headers %t/basic-weak-strong | 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 +25,16 @@ # 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: llvm-objdump --macho --bind %t/strong-weak-import.out | FileCheck %s --check-prefix=WEAK-IMP +# RUN: llvm-objdump --macho --bind %t/basic-weak-strong | FileCheck %s --check-prefix=WEAK-IMP + +## This refernces the gxx_personality_v0 symbol which is defined in libc++abi, which is then +## reexported by libc++. +## Checking that the weak-ref is preserved. +# RUN: %lld -weak-lc++ %t/weak-reexport.o -o %t/weak-reexport.out +# RUN: llvm-objdump --macho --bind %t/weak-reexport.out | FileCheck %s --check-prefix=WEAK-REEXPORT + # WEAK-SYS: cmd LC_LOAD_WEAK_DYLIB # WEAK-SYS-NEXT: cmdsize # WEAK-SYS-NEXT: name /usr/lib/libSystem.dylib @@ -42,6 +55,8 @@ # FOO-NEXT: cmdsize # FOO-NEXT: name [[DIR]]/libfoo.dylib +# WEAK-IMP: {{.+}} pointer 0 CoreFoundation __CFBigNumGetInt128 (weak_import) +# WEAK-REEXPORT: {{.+}} pointer 0 libc++abi ___gxx_personality_v0 (weak_import) #--- foo.s .globl _foo _foo: @@ -69,4 +84,12 @@ #--- test.s .globl _main _main: + movq __CFBigNumGetInt128@GOTPCREL(%rip), %rax ret + +#--- weak-reexport.s +.globl _main +_main: + +.data +.quad ___gxx_personality_v0