xref: /llvm-project/lld/test/ELF/linkerscript/overwrite-sections.test (revision 54e76cb17abdfef1b3549fff00f51e72ae966b4c)
1# REQUIRES: x86
2# RUN: rm -rf %t && split-file --leading-lines %s %t
3# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/a.s -o %t/a.o
4
5## There is no main linker script. OVERWRITE_SECTIONS defines output section
6## descriptions and follows the usual orphan placement rules.
7
8# RUN: ld.lld %t/overwrite1.lds %t/a.o -o %t1
9# RUN: llvm-readelf -S %t1 | FileCheck %s --check-prefix=CHECK1
10
11# CHECK1:      Name      Type     Address  Off      Size   ES Flg Lk Inf Al
12# CHECK1-NEXT:           NULL     [[#%x,]] [[#%x,]] 000000
13# CHECK1-NEXT: .text     PROGBITS [[#%x,]] [[#%x,]] 000001 00  AX  0   0  4
14# CHECK1-NEXT: .foo.text PROGBITS [[#%x,]] [[#%x,]] 000002 00 WAX  0   0  8
15# CHECK1-NEXT: .data     PROGBITS [[#%x,]] [[#%x,]] 000001 00  WA  0   0  1
16# CHECK1-NEXT: .comment  PROGBITS {{.*}}
17
18# RUN: ld.lld %t/overwrite1.lds %t/overwrite1.lds %t/a.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=WARN1
19
20# WARN1: warning: OVERWRITE_SECTIONS specifies duplicate .foo.text
21
22## The output section description order (.foo.data .foo.text) does not affect
23## the output order.
24
25# RUN: ld.lld %t/overwrite2.lds %t/a.o -o %t2
26# RUN: llvm-readelf -S -s %t2 | FileCheck %s --check-prefix=CHECK2
27
28# CHECK2:      [Nr] Name      Type     Address  Off      Size   ES Flg Lk Inf Al
29# CHECK2-NEXT: [ 0]           NULL     [[#%x,]] [[#%x,]] 000000
30# CHECK2-NEXT: [ 1] .text     PROGBITS [[#%x,]] [[#%x,]] 000001 00  AX  0   0  4
31# CHECK2-NEXT: [ 2] .foo.text PROGBITS [[#%x,]] [[#%x,]] 000001 00  AX  0   0  1
32# CHECK2-NEXT: [ 3] .data     PROGBITS [[#%x,]] [[#%x,]] 000001 00  WA  0   0  1
33# CHECK2-NEXT: [ 4] .foo.data PROGBITS [[#%x,]] [[#%x,]] 000001 00  WA  0   0  1
34# CHECK2-NEXT: [ 5] .comment  PROGBITS {{.*}}
35
36# CHECK2:        Num: Value            Size Type   Bind   Vis     Ndx Name
37# CHECK2:      [[#]]: [[#%x,ADDR:]]       0  NOTYPE GLOBAL DEFAULT   4 FOO_DATA_START
38# CHECK2-NEXT: [[#]]: {{0*}}[[#ADDR+1]]   0  NOTYPE GLOBAL DEFAULT   4 FOO_DATA_END
39# CHECK2-NEXT: [[#]]: [[#%x,ADDR:]]       0  NOTYPE GLOBAL DEFAULT   2 FOO_TEXT_START
40# CHECK2-NEXT: [[#]]: {{0*}}[[#ADDR+1]]   0  NOTYPE GLOBAL DEFAULT   2 FOO_TEXT_END
41
42## OVERWRITE_SECTIONS is processed before the main script. The main script
43## specifies the output order. The alignment of .foo.text is specified by
44## OVERWRITE_SECTIONS insteaad of the main script.
45
46# RUN: ld.lld -T %t/main.lds %t/overwrite1.lds %t/a.o -o %t3 2>&1 | count 0
47# RUN: llvm-readelf -S %t3 | FileCheck %s --check-prefix=CHECK3
48# RUN: ld.lld %t/overwrite1.lds -T %t/main.lds %t/a.o -o %t3 2>&1 | count 0
49# RUN: llvm-readelf -S %t3 | FileCheck %s --check-prefix=CHECK3
50
51# CHECK3:      Name      Type     Address  Off      Size   ES Flg Lk Inf Al
52# CHECK3-NEXT:           NULL     [[#%x,]] [[#%x,]] 000000
53# CHECK3-NEXT: .data     PROGBITS [[#%x,]] [[#%x,]] 000001 00  WA  0   0  1
54# CHECK3-NEXT: .text     PROGBITS [[#%x,]] [[#%x,]] 000001 00  AX  0   0  4
55# CHECK3-NEXT: .foo.text PROGBITS [[#%x,]] [[#%x,]] 000002 00 WAX  0   0  8
56# CHECK3-NEXT: .comment  PROGBITS {{.*}}
57
58# RUN: ld.lld -T %t/main.lds %t/overwrite1.lds %t/a.o -o %t3 --verbose 2>&1 | FileCheck %s --check-prefix=VERBOSE3
59
60# VERBOSE3: {{.*}}overwrite1.lds:[[#]] overwrites .foo.text
61
62## If INSERT commands are specified, INSERT commands are processed after overwrite sections.
63# RUN: ld.lld %t/insert.lds %t/overwrite2.lds %t/a.o -o %t4
64# RUN: llvm-readelf -S -s %t4 | FileCheck %s --check-prefix=CHECK4
65
66# CHECK4:      [Nr] Name      Type     Address  Off      Size   ES Flg Lk Inf Al
67# CHECK4-NEXT: [ 0]           NULL     [[#%x,]] [[#%x,]] 000000
68# CHECK4-NEXT: [ 1] .text     PROGBITS [[#%x,]] [[#%x,]] 000001 00  AX  0   0  4
69# CHECK4-NEXT: [ 2] .data     PROGBITS [[#%x,]] [[#%x,]] 000001 00  WA  0   0  1
70# CHECK4-NEXT: [ 3] .foo.data PROGBITS [[#%x,]] [[#%x,]] 000001 00  WA  0   0  1
71# CHECK4-NEXT: [ 4] .foo.text PROGBITS [[#%x,]] [[#%x,]] 000001 00  AX  0   0  1
72# CHECK4-NEXT: [ 5] .comment  PROGBITS {{.*}}
73
74# CHECK4:        Num: Value            Size Type   Bind   Vis     Ndx Name
75# CHECK4:      [[#]]: [[#%x,ADDR:]]       0  NOTYPE GLOBAL DEFAULT   4 FOO_TEXT_START
76# CHECK4-NEXT: [[#]]: {{0*}}[[#ADDR+1]]   0  NOTYPE GLOBAL DEFAULT   4 FOO_TEXT_END
77# CHECK4-NEXT: [[#]]: [[#%x,ADDR:]]       0  NOTYPE GLOBAL DEFAULT   3 FOO_DATA_START
78# CHECK4-NEXT: [[#]]: {{0*}}[[#ADDR+1]]   0  NOTYPE GLOBAL DEFAULT   3 FOO_DATA_END
79
80## OVERWRITE_SECTIONS describes a section used as an anchor of an insert command.
81## Place .foo.data as an orphan, then insert .foo.text after .foo.data.
82# RUN: ld.lld %t/insert.lds %t/overwrite3.lds %t/a.o -o %t5
83# RUN: llvm-readelf -S -s %t4 | FileCheck %s --check-prefix=CHECK5
84
85# CHECK5:      [Nr] Name      Type     Address  Off      Size   ES Flg Lk Inf Al
86# CHECK5-NEXT: [ 0]           NULL     [[#%x,]] [[#%x,]] 000000
87# CHECK5-NEXT: [ 1] .text     PROGBITS [[#%x,]] [[#%x,]] 000001 00  AX  0   0  4
88# CHECK5-NEXT: [ 2] .data     PROGBITS [[#%x,]] [[#%x,]] 000001 00  WA  0   0  1
89# CHECK5-NEXT: [ 3] .foo.data PROGBITS [[#%x,]] [[#%x,]] 000001 00  WA  0   0  1
90# CHECK5-NEXT: [ 4] .foo.text PROGBITS [[#%x,]] [[#%x,]] 000001 00  AX  0   0  1
91# CHECK5-NEXT: [ 5] .comment  PROGBITS {{.*}}
92
93
94#--- a.s
95.globl _start
96_start:
97
98.section .foo.text,"ax"; .byte 1
99.section .foo.data,"aw"; .byte 2
100.section .text.1,"ax"; .byte 3
101.section .data.1,"aw"; .byte 4
102
103#--- main.lds
104SECTIONS {
105  .data : { *(.data*) }
106  .foo.data : { *(.foo.data) }
107  .text : { *(.text*) }
108  .foo.text : ALIGN(16) { *(.foo.text) }
109}
110
111#--- overwrite1.lds
112OVERWRITE_SECTIONS {
113  .foo.text : ALIGN(8) { *(.foo.data .foo.text) }
114}
115
116#--- overwrite2.lds
117OVERWRITE_SECTIONS {
118  .foo.data : { FOO_DATA_START = .; *(.foo.data) FOO_DATA_END = .;  }
119  .foo.text : { FOO_TEXT_START = .; *(.foo.text) FOO_TEXT_END = .;  }
120}
121
122#--- overwrite3.lds
123OVERWRITE_SECTIONS {
124  .foo.data : {}
125}
126
127#--- insert.lds
128SECTIONS { .foo.text : {} } INSERT AFTER .foo.data;
129
130#--- err1.lds
131## TODO Fix the diagnostic 'malformed number'
132# RUN: not ld.lld %t/err1.lds %t/a.o -o /dev/null 2>&1 | FileCheck %s -DFILE=%t/err1.lds --check-prefix=ERR1
133# ERR1: error: [[FILE]]:[[#@LINE+2]]: malformed number: =
134OVERWRITE_SECTIONS {
135  A = 1;
136}
137