xref: /llvm-project/lld/test/ELF/warn-backrefs.s (revision 9a1ca245c8bc60b1ca12cd906fb31130801d977e)
1# REQUIRES: x86
2
3# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t1.o
4# RUN: echo '.globl foo; foo:' | llvm-mc -filetype=obj -triple=x86_64 - -o %t2.o
5# RUN: rm -f %t2.a
6# RUN: llvm-ar rcs %t2.a %t2.o
7# RUN: ld.lld -shared %t2.o -o %t2.so
8
9## A forward reference is accepted by a traditional Unix linker.
10# RUN: ld.lld --fatal-warnings %t1.o %t2.a -o /dev/null
11# RUN: ld.lld --fatal-warnings --warn-backrefs %t1.o %t2.a -o /dev/null
12# RUN: ld.lld --fatal-warnings --warn-backrefs %t1.o --start-lib %t2.o --end-lib -o /dev/null
13
14# RUN: echo 'INPUT("%t1.o" "%t2.a")' > %t1.lds
15# RUN: ld.lld --fatal-warnings --warn-backrefs %t1.lds -o /dev/null
16
17## A backward reference from %t1.o to %t2.a
18## Warn unless the archive is excluded by --warn-backrefs-exclude
19# RUN: ld.lld --fatal-warnings %t2.a %t1.o -o /dev/null
20# RUN: ld.lld --warn-backrefs %t2.a %t1.o -o /dev/null 2>&1 | FileCheck %s
21# RUN: ld.lld --warn-backrefs --no-warn-backrefs %t2.a %t1.o -o /dev/null 2>&1 | count 0
22# RUN: ld.lld --warn-backrefs %t2.a '-(' %t1.o '-)' -o /dev/null 2>&1 | FileCheck %s
23# RUN: ld.lld --warn-backrefs --warn-backrefs-exclude='*3.a' %t2.a %t1.o -o /dev/null 2>&1 | FileCheck %s
24# RUN: ld.lld --fatal-warnings --warn-backrefs --warn-backrefs-exclude='*2.a(*2.o)' %t2.a %t1.o -o /dev/null
25# RUN: ld.lld --fatal-warnings --warn-backrefs --warn-backrefs-exclude '*2.a(*2.o)' \
26# RUN:   --warn-backrefs-exclude not_exist %t2.a %t1.o -o /dev/null
27## Without --warn-backrefs, --warn-backrefs-exclude is ignored.
28# RUN: ld.lld --fatal-warnings --warn-backrefs-exclude=not_exist %t2.a %t1.o -o /dev/null
29
30## Placing the definition and the backward reference in a group can suppress the warning.
31# RUN: echo 'GROUP("%t2.a" "%t1.o")' > %t2.lds
32# RUN: ld.lld --fatal-warnings --warn-backrefs %t2.lds -o /dev/null
33# RUN: ld.lld --fatal-warnings --warn-backrefs '-(' %t2.a %t1.o '-)' -o /dev/null
34# RUN: ld.lld --fatal-warnings --warn-backrefs --start-group %t2.a %t1.o --end-group -o /dev/null
35
36## A backward reference from %t1.o to %t2.a (added by %t3.lds).
37# RUN: echo 'GROUP("%t2.a")' > %t3.lds
38# RUN: ld.lld --warn-backrefs %t3.lds %t1.o -o /dev/null 2>&1 | FileCheck %s
39# RUN: ld.lld --fatal-warnings --warn-backrefs '-(' %t3.lds %t1.o '-)' -o /dev/null
40# RUN: ld.lld --fatal-warnings --warn-backrefs --warn-backrefs-exclude='*2.a(*2.o)' -o /dev/null %t3.lds %t1.o
41## If a lazy definition appears after the backward reference, don't warn.
42# RUN: ld.lld --fatal-warnings --warn-backrefs %t3.lds %t1.o %t3.lds -o /dev/null
43
44# CHECK: warning: backward reference detected: foo in {{.*}}1.o refers to {{.*}}2.a
45
46## A backward reference from %t1.o to %t2.o
47## --warn-backrefs-exclude= applies to --start-lib covered object files.
48# RUN: ld.lld --warn-backrefs --start-lib %t2.o --end-lib %t1.o -o /dev/null 2>&1 | \
49# RUN:   FileCheck --check-prefix=OBJECT %s
50# RUN: ld.lld --fatal-warnings --warn-backrefs --warn-backrefs-exclude=%/t2.o --start-lib %/t2.o --end-lib %t1.o -o /dev/null
51## If a lazy definition appears after the backward reference, don't warn.
52# RUN: ld.lld --fatal-warnings --warn-backrefs --start-lib %t2.o --end-lib %t1.o --start-lib %t2.o --end-lib -o /dev/null
53
54# OBJECT: warning: backward reference detected: foo in {{.*}}1.o refers to {{.*}}2.o
55
56## Back reference from an fetched --start-lib to a previous --start-lib.
57# RUN: ld.lld -m elf_x86_64 -u _start --warn-backrefs --start-lib %/t2.o --end-lib \
58# RUN:   --start-lib %t1.o --end-lib -o /dev/null 2>&1 | FileCheck --check-prefix=OBJECT %s
59## --warn-backrefs-exclude=%/t2.o can be used for a fetched --start-lib.
60# RUN: ld.lld --fatal-warnings -m elf_x86_64 -u _start --warn-backrefs --warn-backrefs-exclude=%/t2.o --start-lib %/t2.o --end-lib --start-lib %t1.o --end-lib -o /dev/null
61
62## Don't warn if the definition and the backward reference are in a group.
63# RUN: echo '.globl bar; bar:' | llvm-mc -filetype=obj -triple=x86_64 - -o %t3.o
64# RUN: echo '.globl foo; foo: call bar' | llvm-mc -filetype=obj -triple=x86_64 - -o %t4.o
65# RUN: ld.lld --fatal-warnings --warn-backrefs %t1.o --start-lib %t3.o %t4.o --end-lib -o /dev/null
66# RUN: rm -f %t34.a && llvm-ar rcS %t34.a %t3.o %t4.o
67# RUN: ld.lld --fatal-warnings --warn-backrefs %t1.o %t34.a -o /dev/null
68
69## We don't report backward references to weak symbols as they can be overridden later.
70# RUN: echo '.weak foo; foo:' | llvm-mc -filetype=obj -triple=x86_64 - -o %tweak.o
71# RUN: ld.lld --fatal-warnings --warn-backrefs --start-lib %tweak.o --end-lib %t1.o %t2.o -o /dev/null
72
73## Check common symbols. A common sym might later be replaced by a non-common definition.
74# RUN: echo '.comm obj, 4' | llvm-mc -filetype=obj -triple=x86_64 -o %tcomm.o
75# RUN: echo '.type obj,@object; .data; .globl obj; .p2align 2; obj: .long 55; .size obj, 4' | llvm-mc -filetype=obj -triple=x86_64 -o %tstrong.o
76# RUN: echo '.globl foo; foo: movl obj(%rip), %eax' | llvm-mc -triple=x86_64 -filetype=obj -o %t5.o
77# RUN: llvm-ar rcs %tcomm.a %tcomm.o
78# RUN: llvm-ar rcs %tstrong.a %tstrong.o
79# RUN: ld.lld --warn-backrefs %tcomm.a %t1.o %t5.o 2>&1 -o /dev/null | FileCheck --check-prefix=COMM %s
80# RUN: ld.lld --fatal-warnings --fortran-common --warn-backrefs %tcomm.a %t1.o %t5.o %tstrong.a 2>&1 -o /dev/null
81# RUN: ld.lld --warn-backrefs --no-fortran-common %tcomm.a %t1.o %t5.o %tstrong.a 2>&1 -o /dev/null | FileCheck --check-prefix=COMM %s
82
83# COMM: ld.lld: warning: backward reference detected: obj in {{.*}}5.o refers to {{.*}}comm.a
84
85## If a lazy definition appears after the backward reference, don't warn.
86## A traditional Unix linker will resolve the reference to the later definition.
87# RUN: ld.lld --fatal-warnings --warn-backrefs %t2.a %t1.o %t2.a -o /dev/null
88
89## lld fetches the archive while GNU ld resolves the reference to the shared definition.
90## Warn because the resolution rules are different.
91# RUN: ld.lld --warn-backrefs %t2.a %t1.o %t2.so -o /dev/null 2>&1 | FileCheck %s
92
93## This is a limitation. The resolution rules are different but
94## --warn-backrefs does not warn.
95# RUN: ld.lld --fatal-warnings --warn-backrefs %t2.a %t1.o %t2.so %t2.a -o /dev/null
96
97## In GNU linkers, -u does not make a backward reference.
98# RUN: ld.lld --fatal-warnings --warn-backrefs -u foo %t2.a %t1.o -o /dev/null
99
100## -u does not make a backward reference.
101# RUN: ld.lld --fatal-warnings --warn-backrefs -u foo %t2.a %t1.o -o /dev/null
102
103## --defsym does not make a backward reference, but it does not suppress the warning due to another file.
104# RUN: ld.lld --fatal-warnings --warn-backrefs --defsym=x=foo -e 0 %t2.a -o /dev/null
105# RUN: ld.lld --warn-backrefs --defsym=x=foo %t2.a %t1.o -o /dev/null 2>&1 | FileCheck %s
106
107# RUN: not ld.lld --warn-backrefs-exclude='[' 2>&1 | FileCheck --check-prefix=INVALID %s
108# INVALID: error: --warn-backrefs-exclude: invalid glob pattern, unmatched '[': [
109
110.globl _start, foo
111_start:
112  call foo
113