xref: /llvm-project/lld/test/MachO/lto-common-symbol-resolution.ll (revision f01fdd17b4d202727023da23bdd120eb0bd94f9f)
1; REQUIRES: x86
2
3;; NOTE: We deviate significantly from ld64's behavior here. We treat common
4;; bitcode symbols like regular common symbols, but ld64 gives them different
5;; (and IMO very strange) precedence. This test documents the differences.
6
7; RUN: rm -rf %t; split-file %s %t
8; RUN: opt -module-summary %t/common.ll -o %t/common.o
9; RUN: opt -module-summary %t/defined.ll -o %t/defined.o
10; RUN: opt -module-summary %t/weak-defined.ll -o %t/weak-defined.o
11; RUN: opt -module-summary %t/libfoo.ll -o %t/libfoo.o
12; RUN: opt -module-summary %t/refs-foo.ll -o %t/refs-foo.o
13; RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/weak-defined.s -o %t/weak-defined-asm.o
14
15; RUN: %lld -dylib -dylib %t/libfoo.o -o %t/libfoo.dylib
16
17; RUN: llvm-ar rcs %t/defined.a %t/defined.o
18; RUN: llvm-ar rcs %t/defined-and-common.a %t/defined.o %t/common.o
19; RUN: llvm-ar rcs %t/common-and-defined.a %t/common.o %t/defined.o
20; RUN: llvm-ar rcs %t/weak-defined-and-common.a %t/weak-defined.o %t/common.o
21; RUN: llvm-ar rcs %t/common-and-weak-defined.a %t/common.o %t/weak-defined.o
22
23;; Defined symbols take precedence over common bitcode symbols.
24; RUN: %lld -dylib %t/defined.o %t/common.o -o %t/test
25; RUN: llvm-objdump --syms %t/test | FileCheck %s --check-prefix=DEFINED
26; RUN: %lld -dylib %t/common.o %t/defined.o -o %t/test
27; RUN: llvm-objdump --syms %t/test | FileCheck %s --check-prefix=DEFINED
28
29;; Defined symbols have the same precedence as common bitcode symbols within
30;; an archive.
31; RUN: %lld -dylib %t/defined-and-common.a %t/refs-foo.o -o %t/refs-foo
32; RUN: llvm-objdump --syms %t/refs-foo | FileCheck %s --check-prefix=DEFINED
33; RUN: %lld -dylib %t/common-and-defined.a %t/refs-foo.o -o %t/refs-foo
34; RUN: llvm-objdump --syms %t/refs-foo | FileCheck %s --check-prefix=COMMON
35
36;; ld64: Weak bitcode symbols have the same precedence as common bitcode symbols.
37;; lld: Weak bitcode symbols take precedence over common bitcode symbols.
38; RUN: %lld -dylib %t/weak-defined.o %t/common.o -o %t/test
39; RUN: llvm-objdump --syms %t/test | FileCheck %s --check-prefix=WEAK-DEFINED
40; RUN: %lld -dylib %t/common.o %t/weak-defined.o -o %t/test
41; RUN: llvm-objdump --syms %t/test | FileCheck %s --check-prefix=WEAK-DEFINED
42
43;; Weak non-bitcode symbols take precedence over common bitcode symbols.
44; RUN: %lld -dylib %t/weak-defined-asm.o %t/common.o -o %t/test
45; RUN: llvm-objdump --syms %t/test | FileCheck %s --check-prefix=WEAK-DEFINED
46; RUN: %lld -dylib %t/common.o %t/weak-defined-asm.o -o %t/test
47; RUN: llvm-objdump --syms %t/test | FileCheck %s --check-prefix=WEAK-DEFINED
48
49;; ld64: Archive symbols take precedence over common bitcode symbols.
50;; lld: Common bitcode symbols take precedence over archive symbols.
51; RUN: %lld -dylib %t/defined.a %t/common.o -o %t/test
52; RUN: llvm-objdump --syms %t/test | FileCheck %s --check-prefix=COMMON
53; COM (ld64): llvm-objdump --syms %t/test | FileCheck %s --check-prefix=DEFINED
54; RUN: %lld -dylib %t/common.o %t/defined.a -o %t/test
55; RUN: llvm-objdump --syms %t/test | FileCheck %s --check-prefix=COMMON
56; COM (ld64): llvm-objdump --syms %t/test | FileCheck %s --check-prefix=DEFINED
57
58;; ld64: Dylib symbols take precedence over common bitcode symbols.
59;; lld: Common bitcode symbols take precedence over dylib symbols.
60; RUN: %lld -dylib %t/libfoo.dylib %t/common.o %t/refs-foo.o -o %t/test
61; RUN: llvm-objdump --syms %t/test | FileCheck %s --check-prefix=COMMON
62; COM (ld64): llvm-objdump --syms %t/test | FileCheck %s --check-prefix=DYLIB
63; RUN: %lld -dylib %t/common.o %t/libfoo.dylib %t/refs-foo.o -o %t/test
64; RUN: llvm-objdump --syms %t/test | FileCheck %s --check-prefix=COMMON
65; COM (ld64): llvm-objdump --syms %t/test | FileCheck %s --check-prefix=DYLIB
66
67; COMMON:       g     O __DATA,__common _foo
68; DEFINED:      g     O __DATA,__data _foo
69; WEAK-DEFINED: w     O __DATA,__data _foo
70; DYLIB:        *UND* _foo
71
72;--- common.ll
73target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
74target triple = "x86_64-apple-macosx10.15.0"
75
76@foo = common global i8 0
77
78;--- defined.ll
79target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
80target triple = "x86_64-apple-macosx10.15.0"
81
82@foo = global i8 12
83
84;--- weak-defined.ll
85target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
86target triple = "x86_64-apple-macosx10.15.0"
87
88@foo = weak global i8 12
89
90;--- weak-defined.s
91.globl _foo
92.weak_definition _foo
93.data
94_foo:
95
96;--- libfoo.ll
97target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
98target triple = "x86_64-apple-macosx10.15.0"
99
100@foo = common global i8 0
101
102;--- refs-foo.ll
103target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
104target triple = "x86_64-apple-macosx10.15.0"
105
106@foo = external global i8
107
108define void @f() {
109  %1 = load i8, ptr @foo
110  ret void
111}
112