xref: /llvm-project/lld/test/ELF/linkerscript/discard-section.s (revision 8fe3e70e810b409dce36f6d415e86f0f9b1cf22d)
1# REQUIRES: x86
2## Test relocations referencing symbols defined relative to sections discarded by /DISCARD/.
3
4# RUN: rm -rf %t && split-file %s %t && cd %t
5# RUN: llvm-mc -filetype=obj -triple=x86_64 a.s -o a.o
6# RUN: llvm-mc -filetype=obj -triple=x86_64 b.s -o b.o
7# RUN: not ld.lld --threads=1 -T a.lds a.o b.o -z undefs -o /dev/null 2>&1 | FileCheck %s --check-prefix=LOCAL --implicit-check-not=error:
8# RUN: not ld.lld --threads=1 -T a.lds a.o b.o -o /dev/null 2>&1 | FileCheck %s --check-prefixes=LOCAL,NONLOCAL --implicit-check-not=error:
9# RUN: ld.lld -r -T a.lds a.o b.o -o a.ro 2>&1 | FileCheck %s --check-prefix=WARNING --implicit-check-not=warning:
10# RUN: llvm-readelf -r -s a.ro | FileCheck %s --check-prefix=RELOC
11
12# RUN: ld.lld -r --gc-sections -T a.lds a.o b.o -o a.gc.ro --no-fatal-warnings
13# RUN: llvm-readelf -r -s a.gc.ro | FileCheck %s --check-prefix=RELOC-GC
14
15# LOCAL:      error: relocation refers to a discarded section: .aaa
16# LOCAL-NEXT: >>> defined in a.o
17# LOCAL-NEXT: >>> referenced by a.o:(.bbb+0x0)
18
19# NONLOCAL:      error: relocation refers to a symbol in a discarded section: global
20# NONLOCAL-NEXT: >>> defined in a.o
21# NONLOCAL-NEXT: >>> referenced by b.o:(.data+0x0)
22
23# NONLOCAL:      error: relocation refers to a symbol in a discarded section: weak
24# NONLOCAL-NEXT: >>> defined in a.o
25# NONLOCAL-NEXT: >>> referenced by b.o:(.data+0x8)
26
27# NONLOCAL:      error: relocation refers to a symbol in a discarded section: weakref1
28# NONLOCAL-NEXT: >>> defined in a.o
29# NONLOCAL-NEXT: >>> referenced by b.o:(.data+0x10)
30
31# NONLOCAL:      error: relocation refers to a symbol in a discarded section: weakref2
32# NONLOCAL-NEXT: >>> defined in a.o
33# NONLOCAL-NEXT: >>> referenced by b.o:(.data+0x18)
34
35# WARNING:      warning: relocation refers to a discarded section: .aaa
36# WARNING-NEXT: >>> referenced by a.o:(.rela.bbb+0x0)
37
38## GNU ld reports "defined in discarded secion" errors even in -r mode.
39## We set the symbol index to 0.
40# RELOC:      Relocation section '.rela.bbb' at offset {{.*}} contains 1 entries:
41# RELOC-NEXT:     Offset             Info             Type               Symbol's Value  Symbol's Name + Addend
42# RELOC-NEXT: 0000000000000000  0000000000000000 R_X86_64_NONE                             0
43# RELOC-EMPTY:
44# RELOC-NEXT: Relocation section '.rela.data' at offset {{.*}} contains 4 entries:
45# RELOC-NEXT:     Offset             Info             Type               Symbol's Value  Symbol's Name + Addend
46# RELOC-NEXT: 0000000000000000  0000000000000001 R_X86_64_64                             0
47# RELOC-NEXT: 0000000000000008  0000000000000001 R_X86_64_64                             0
48# RELOC-NEXT: 0000000000000010  0000000000000001 R_X86_64_64                             0
49# RELOC-NEXT: 0000000000000018  0000000000000001 R_X86_64_64                             0
50
51# RELOC:      Num:    Value          Size Type    Bind   Vis      Ndx Name
52# RELOC-NEXT:   0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
53# RELOC-NEXT:   1: 0000000000000000     0 SECTION LOCAL  DEFAULT    1 .text
54# RELOC-NEXT:   2: 0000000000000000     0 SECTION LOCAL  DEFAULT    2 .bbb
55# RELOC-NEXT:   3: 0000000000000000     0 SECTION LOCAL  DEFAULT    4 .data
56# RELOC-NEXT:   4: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT    1 _start
57# RELOC-EMPTY:
58
59# RELOC-GC:   There are no relocations in this file.
60
61#--- a.s
62.globl _start
63_start:
64
65.section .aaa,"a"
66.globl global, weakref1, unused
67.weak weak, weakref2
68global:
69weak:
70weakref1:
71weakref2:
72## Eliminate `unused` just like GC discarded definitions.
73## Linux kernel's CONFIG_DEBUG_FORCE_WEAK_PER_CPU=y configuration expects
74## that the unreferenced `unused` is not emitted to .symtab.
75unused:
76  .quad 0
77
78.section .bbb,"aw"
79  .quad .aaa
80
81#--- b.s
82.weak weakref1, weakref2
83.section .data,"aw"
84  .quad global
85  .quad weak
86  .quad weakref1
87  .quad weakref2
88
89#--- a.lds
90SECTIONS { /DISCARD/ : { *(.aaa) } }
91