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 %t2.a '-(' %t1.o '-)' -o /dev/null 2>&1 | FileCheck %s
22# RUN: ld.lld --warn-backrefs --warn-backrefs-exclude='*3.a' %t2.a %t1.o -o /dev/null 2>&1 | FileCheck %s
23# RUN: ld.lld --fatal-warnings --warn-backrefs --warn-backrefs-exclude='*2.a(*2.o)' %t2.a %t1.o -o /dev/null
24# RUN: ld.lld --fatal-warnings --warn-backrefs --warn-backrefs-exclude '*2.a(*2.o)' \
25# RUN:   --warn-backrefs-exclude not_exist %t2.a %t1.o -o /dev/null
26## Without --warn-backrefs, --warn-backrefs-exclude is ignored.
27# RUN: ld.lld --fatal-warnings --warn-backrefs-exclude=not_exist %t2.a %t1.o -o /dev/null
28
29## Placing the definition and the backward reference in a group can suppress the warning.
30# RUN: echo 'GROUP("%t2.a" "%t1.o")' > %t2.lds
31# RUN: ld.lld --fatal-warnings --warn-backrefs %t2.lds -o /dev/null
32# RUN: ld.lld --fatal-warnings --warn-backrefs '-(' %t2.a %t1.o '-)' -o /dev/null
33
34## A backward reference from %t1.o to %t2.a (added by %t3.lds).
35# RUN: echo 'GROUP("%t2.a")' > %t3.lds
36# RUN: ld.lld --warn-backrefs %t3.lds %t1.o -o /dev/null 2>&1 | FileCheck %s
37# RUN: ld.lld --fatal-warnings --warn-backrefs '-(' %t3.lds %t1.o '-)' -o /dev/null
38# RUN: ld.lld --fatal-warnings --warn-backrefs --warn-backrefs-exclude='*2.a(*2.o)' -o /dev/null %t3.lds %t1.o
39## If a lazy definition appears after the backward reference, don't warn.
40# RUN: ld.lld --fatal-warnings --warn-backrefs %t3.lds %t1.o %t3.lds -o /dev/null
41
42# CHECK: warning: backward reference detected: foo in {{.*}}1.o refers to {{.*}}2.a
43
44## A backward reference from %t1.o to %t2.o
45## --warn-backrefs-exclude= applies to --start-lib covered object files.
46# RUN: ld.lld --warn-backrefs --start-lib %t2.o --end-lib %t1.o -o /dev/null 2>&1 | \
47# RUN:   FileCheck --check-prefix=OBJECT %s
48# RUN: ld.lld --fatal-warnings --warn-backrefs --warn-backrefs-exclude=%/t2.o --start-lib %/t2.o --end-lib %t1.o -o /dev/null
49## If a lazy definition appears after the backward reference, don't warn.
50# RUN: ld.lld --fatal-warnings --warn-backrefs --start-lib %t2.o --end-lib %t1.o --start-lib %t2.o --end-lib -o /dev/null
51
52# OBJECT: warning: backward reference detected: foo in {{.*}}1.o refers to {{.*}}2.o
53
54## Back reference from an fetched --start-lib to a previous --start-lib.
55# RUN: ld.lld -m elf_x86_64 -u _start --warn-backrefs --start-lib %/t2.o --end-lib \
56# RUN:   --start-lib %t1.o --end-lib -o /dev/null 2>&1 | FileCheck --check-prefix=OBJECT %s
57## --warn-backrefs-exclude=%/t2.o can be used for a fetched --start-lib.
58# 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
59
60## Don't warn if the definition and the backward reference are in a group.
61# RUN: echo '.globl bar; bar:' | llvm-mc -filetype=obj -triple=x86_64 - -o %t3.o
62# RUN: echo '.globl foo; foo: call bar' | llvm-mc -filetype=obj -triple=x86_64 - -o %t4.o
63# RUN: ld.lld --fatal-warnings --warn-backrefs %t1.o --start-lib %t3.o %t4.o --end-lib -o /dev/null
64
65## We don't report backward references to weak symbols as they can be overridden later.
66# RUN: echo '.weak foo; foo:' | llvm-mc -filetype=obj -triple=x86_64 - -o %tweak.o
67# RUN: ld.lld --fatal-warnings --warn-backrefs --start-lib %tweak.o --end-lib %t1.o %t2.o -o /dev/null
68
69## If a lazy definition appears after the backward reference, don't warn.
70## A traditional Unix linker will resolve the reference to the later definition.
71# RUN: ld.lld --fatal-warnings --warn-backrefs %t2.a %t1.o %t2.a -o /dev/null
72
73## lld fetches the archive while GNU ld resolves the reference to the shared definition.
74## Warn because the resolution rules are different.
75# RUN: ld.lld --warn-backrefs %t2.a %t1.o %t2.so -o /dev/null 2>&1 | FileCheck %s
76
77## This is a limitation. The resolution rules are different but
78## --warn-backrefs does not warn.
79# RUN: ld.lld --fatal-warnings --warn-backrefs %t2.a %t1.o %t2.so %t2.a -o /dev/null
80
81## In GNU linkers, -u does not make a backward reference.
82# RUN: ld.lld --fatal-warnings --warn-backrefs -u foo %t2.a %t1.o -o /dev/null
83
84## -u does not make a backward reference.
85# RUN: ld.lld --fatal-warnings --warn-backrefs -u foo %t2.a %t1.o -o /dev/null
86
87# RUN: not ld.lld --warn-backrefs-exclude='[' 2>&1 | FileCheck --check-prefix=INVALID %s
88# INVALID: error: --warn-backrefs-exclude: invalid glob pattern: [
89
90.globl _start, foo
91_start:
92  call foo
93