xref: /llvm-project/lld/test/MachO/U-dynamic-lookup.s (revision 3d5e5066f1af50ea622d136e9543aedae178c8e5)
1# REQUIRES: x86
2# RUN: rm -rf %t; split-file %s %t
3
4# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos -o %t/foo.o %t/foo.s
5# RUN: %lld -dylib -o %t/foo.dylib %t/foo.o
6
7# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos -o %t/main.o %t/main.s
8
9# _foo starts out as a (non-weak) dynamically looked up symbol and is merged
10# against the Undefined from foo.o. _bar isn't referenced in any object file,
11# but starts out as Undefined because of the -u flag. _baz isn't referenced
12# at all.
13# RUN: %lld -lSystem %t/main.o -U _foo -U _bar -u _bar -U _baz -o %t/out
14# RUN: llvm-objdump --macho --lazy-bind %t/out | FileCheck --check-prefix=DYNAMIC %s
15# RUN: llvm-nm -m %t/out | FileCheck --check-prefix=DYNAMICSYM %s
16
17# Same thing should happen if _foo starts out as an Undefined.
18# `-U _foo` being passed twice shouldn't have an effect either.
19# RUN: %lld -lSystem %t/main.o -u _foo -U _foo -U _foo -u _bar -U _bar -U _baz -o %t/out
20# RUN: llvm-objdump --macho --lazy-bind %t/out | FileCheck --check-prefix=DYNAMIC %s
21# RUN: llvm-nm -m %t/out | FileCheck --check-prefix=DYNAMICSYM %s
22
23# Unreferenced dynamic lookup symbols don't make it into the bind tables, but
24# they do make it into the symbol table in ld64 if they're an undefined from -u
25# for some reason. lld happens to have the same behavior when no explicit code
26# handles this case, so match ld64's behavior.
27
28# DYNAMIC-NOT: _bar
29# DYNAMIC-NOT: _baz
30# DYNAMIC: flat-namespace   _foo
31
32# DYNAMICSYM:      (undefined) external _bar (dynamically looked up)
33# DYNAMICSYM-NOT:      (undefined) external _bar (dynamically looked up)
34# DYNAMICSYM-NEXT: (undefined) external _foo (dynamically looked up)
35
36# Test with a Defined. Here, foo.o provides _foo and the symbol doesn't need
37# to be imported.
38# RUN: %lld -lSystem %t/main.o %t/foo.o -U _foo -o %t/out
39# RUN: llvm-objdump --macho --lazy-bind %t/out | FileCheck --check-prefix=NOTDYNAMIC %s
40
41# NOTDYNAMIC-NOT: _foo
42
43# Here, foo.dylib provides _foo and the symbol doesn't need to be imported
44# dynamically.
45# RUN: %lld -lSystem %t/main.o %t/foo.dylib -U _foo -o %t/out
46# RUN: llvm-objdump --macho --lazy-bind %t/out | FileCheck --check-prefix=TWOLEVEL %s
47# RUN: llvm-nm -m %t/out | FileCheck --check-prefix=TWOLEVELSYM %s
48
49# TWOLEVEL: foo              _foo
50# TWOLEVELSYM: (undefined) external _foo (from foo)
51
52# Test resolving dynamic lookup symbol with weak defined.
53# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos -o %t/weak-foo.o %t/weak-foo.s
54# RUN: %lld -dylib -o %t/weak-foo.dylib %t/weak-foo.o -U _foo
55# RUN: llvm-objdump --macho --bind --lazy-bind --weak-bind %t/weak-foo.dylib | FileCheck --check-prefix=WEAKDEF %s
56# RUN: llvm-nm -m %t/weak-foo.dylib | FileCheck --check-prefix=WEAKDEFSYM %s
57# WEAKDEF-NOT: _foo
58# WEAKDEFSYM: weak external _foo
59
60# Same if foo.dylib provides _foo weakly, except that the symbol is weak then.
61# RUN: %lld -lSystem %t/main.o %t/weak-foo.dylib -U _foo -o %t/out
62# RUN: llvm-objdump --macho --bind --lazy-bind --weak-bind %t/out | FileCheck --check-prefix=TWOLEVELWEAK %s
63# RUN: llvm-nm -m %t/out | FileCheck --check-prefix=TWOLEVELWEAKSYM %s
64
65# TWOLEVELWEAK-LABEL: Bind table:
66# TWOLEVELWEAK:       __DATA        __la_symbol_ptr 0x[[#%x,ADDR:]]   pointer 0 weak-foo    _foo
67# TWOLEVELWEAK-LABEL: Lazy bind table:
68# TWOLEVELWEAK-NOT:   weak-foo         _foo
69# TWOLEVELWEAK-LABEL: Weak bind table:
70# TWOLEVELWEAK:       __DATA   __la_symbol_ptr      0x[[#ADDR]]       pointer 0 _foo
71
72# TWOLEVELWEAKSYM: (undefined) weak external _foo (from weak-foo)
73
74#--- foo.s
75.globl _foo
76_foo:
77  ret
78
79#--- weak-foo.s
80.globl _foo
81.weak_definition _foo
82_foo:
83  ret
84
85#--- main.s
86.globl _main
87_main:
88  callq _foo
89  ret
90