Skip to content

Commit 4f5ad22

Browse files
committed
[ELF,test] Improve PHDRS tests
1 parent 06429a4 commit 4f5ad22

File tree

3 files changed

+89
-159
lines changed

3 files changed

+89
-159
lines changed

lld/test/ELF/linkerscript/header-phdr.test

Lines changed: 0 additions & 15 deletions
This file was deleted.

lld/test/ELF/linkerscript/phdr-check.s

Lines changed: 0 additions & 15 deletions
This file was deleted.

lld/test/ELF/linkerscript/phdrs.s

Lines changed: 89 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,103 @@
11
# REQUIRES: x86
2-
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
3-
# RUN: echo "PHDRS {all PT_LOAD FILEHDR PHDRS ;} \
4-
# RUN: SECTIONS { \
5-
# RUN: . = 0x10000200; \
6-
# RUN: .text : {*(.text*)} :all \
7-
# RUN: .foo : {*(.foo.*)} :all \
8-
# RUN: .data : {*(.data.*)} :all}" > %t.script
9-
# RUN: ld.lld -o %t1 --script %t.script %t
10-
# RUN: llvm-readobj -l %t1 | FileCheck %s
11-
2+
# RUN: rm -rf %t && split-file %s %t && cd %t
3+
# RUN: llvm-mc -filetype=obj -triple=x86_64 a.s -o a.o
4+
5+
#--- 1.lds
6+
PHDRS {all PT_LOAD FILEHDR PHDRS ;}
7+
SECTIONS {
8+
. = 0x10000200;
9+
.text : {*(.text*)} :all
10+
.foo : {*(.foo.*)} :all
11+
.data : {*(.data.*)} :all}
12+
13+
# RUN: ld.lld -o 1 -T 1.lds a.o
14+
# RUN: llvm-readelf -Sl 1 | FileCheck %s
15+
# CHECK: [Nr] Name Type Address Off Size ES Flg Lk Inf Al
16+
# CHECK: [ 1] .text PROGBITS 0000000010000200 000200 000001 00 AX 0 0 4
17+
# CHECK-NEXT: [ 2] .foo PROGBITS 0000000010000201 000201 000008 00 WA 0 0 1
18+
19+
# CHECK: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
20+
# CHECK-NEXT: LOAD 0x000000 0x0000000010000000 0x0000000010000000 0x000209 0x000209 RWE 0x1000
21+
22+
#--- 2.lds
1223
## Check that program headers are not written, unless we explicitly tell
1324
## lld to do this.
14-
# RUN: echo "PHDRS {all PT_LOAD;} \
15-
# RUN: SECTIONS { \
16-
# RUN: . = 0x10000200; \
17-
# RUN: /DISCARD/ : {*(.text*)} \
18-
# RUN: .foo : {*(.foo.*)} :all \
19-
# RUN: }" > %t.script
20-
# RUN: ld.lld -o %t1 --script %t.script %t
21-
# RUN: llvm-readobj -l %t1 | FileCheck --check-prefix=NOPHDR %s
22-
25+
PHDRS {all PT_LOAD;}
26+
SECTIONS {
27+
. = 0x10000200;
28+
/DISCARD/ : {*(.text*)}
29+
.foo : {*(.foo.*)} :all
30+
}
31+
32+
# RUN: ld.lld -o 2 -T 2.lds a.o
33+
# RUN: llvm-readelf -l 2 | FileCheck --check-prefix=NOPHDR %s
34+
# NOPHDR: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
35+
# NOPHDR-NEXT: LOAD 0x000200 0x0000000010000200 0x0000000010000200 0x000008 0x000008 RW 0x1000
36+
37+
#--- 3.lds
38+
PHDRS {all PT_LOAD FILEHDR PHDRS ;}
39+
SECTIONS {
40+
. = 0x10000200;
41+
.text : {*(.text*)} :all
42+
.foo : {*(.foo.*)}
43+
.data : {*(.data.*)} }
44+
45+
# RUN: ld.lld -o 3 -T 3.lds a.o
46+
# RUN: llvm-readelf -l 3 | FileCheck --check-prefix=DEFHDR %s
47+
# DEFHDR: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
48+
# DEFHDR-NEXT: LOAD 0x000000 0x0000000010000000 0x0000000010000000 0x000209 0x000209 RWE 0x1000
49+
50+
#--- at.lds
2351
## Check the AT(expr)
24-
# RUN: echo "PHDRS {all PT_LOAD FILEHDR PHDRS AT(0x500 + 0x500) ;} \
25-
# RUN: SECTIONS { \
26-
# RUN: . = 0x10000200; \
27-
# RUN: .text : {*(.text*)} :all \
28-
# RUN: .foo : {*(.foo.*)} :all \
29-
# RUN: .data : {*(.data.*)} :all}" > %t.script
30-
# RUN: ld.lld -o %t1 --script %t.script %t
31-
# RUN: llvm-readobj -l %t1 | FileCheck --check-prefix=AT %s
32-
33-
# RUN: echo "PHDRS {all PT_LOAD FILEHDR PHDRS ;} \
34-
# RUN: SECTIONS { \
35-
# RUN: . = 0x10000200; \
36-
# RUN: .text : {*(.text*)} :all \
37-
# RUN: .foo : {*(.foo.*)} \
38-
# RUN: .data : {*(.data.*)} }" > %t.script
39-
# RUN: ld.lld -o %t1 --script %t.script %t
40-
# RUN: llvm-readobj -l %t1 | FileCheck --check-prefix=DEFHDR %s
52+
PHDRS {all PT_LOAD FILEHDR PHDRS AT(0x500 + 0x500) ;}
53+
SECTIONS {
54+
. = 0x10000200;
55+
.text : {*(.text*)} :all
56+
.foo : {*(.foo.*)} :all
57+
.data : {*(.data.*)} :all}
58+
59+
# RUN: ld.lld -o at -T at.lds a.o
60+
# RUN: llvm-readelf -l at | FileCheck --check-prefix=AT %s
61+
# AT: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
62+
# AT-NEXT: LOAD 0x000000 0x0000000010000000 0x0000000000000a00 0x000209 0x000209 RWE 0x1000
63+
64+
#--- int.lds
65+
## Check the numetic values for PHDRS.
66+
PHDRS {text PT_LOAD FILEHDR PHDRS; foo 0x11223344; }
67+
SECTIONS { . = SIZEOF_HEADERS; .foo : { *(.foo* .text*) } : text : foo}
4168

69+
# RUN: ld.lld -o int -T int.lds a.o
70+
# RUN: llvm-readelf -l int | FileCheck --check-prefix=INT-PHDRS %s
71+
# INT-PHDRS: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
72+
# INT-PHDRS-NEXT: LOAD 0x000000 0x0000000000000000 0x0000000000000000 0x0000b9 0x0000b9 RWE 0x1000
73+
# INT-PHDRS-NEXT: <unknown>: 0x11223344 0x0000b0 0x00000000000000b0 0x00000000000000b0 0x000009 0x000009 RWE 0x4
74+
75+
#--- unspecified.lds
4276
## Check that error is reported when trying to use phdr which is not listed
4377
## inside PHDRS {} block
4478
## TODO: If script doesn't contain PHDRS {} block then default phdr is always
4579
## created and error is not reported.
46-
# RUN: echo "PHDRS { all PT_LOAD; } \
47-
# RUN: SECTIONS { .baz : {*(.foo.*)} :bar }" > %t.script
48-
# RUN: not ld.lld -o /dev/null --script %t.script %t 2>&1 | FileCheck --check-prefix=BADHDR %s
49-
50-
# CHECK: ProgramHeaders [
51-
# CHECK-NEXT: ProgramHeader {
52-
# CHECK-NEXT: Type: PT_LOAD (0x1)
53-
# CHECK-NEXT: Offset: 0x0
54-
# CHECK-NEXT: VirtualAddress: 0x10000000
55-
# CHECK-NEXT: PhysicalAddress: 0x10000000
56-
# CHECK-NEXT: FileSize: 521
57-
# CHECK-NEXT: MemSize: 521
58-
# CHECK-NEXT: Flags [ (0x7)
59-
# CHECK-NEXT: PF_R (0x4)
60-
# CHECK-NEXT: PF_W (0x2)
61-
# CHECK-NEXT: PF_X (0x1)
62-
# CHECK-NEXT: ]
63-
64-
# NOPHDR: ProgramHeaders [
65-
# NOPHDR-NEXT: ProgramHeader {
66-
# NOPHDR-NEXT: Type: PT_LOAD (0x1)
67-
# NOPHDR-NEXT: Offset: 0x200
68-
# NOPHDR-NEXT: VirtualAddress: 0x10000200
69-
# NOPHDR-NEXT: PhysicalAddress: 0x10000200
70-
# NOPHDR-NEXT: FileSize: 8
71-
# NOPHDR-NEXT: MemSize: 8
72-
# NOPHDR-NEXT: Flags [ (0x6)
73-
# NOPHDR-NEXT: PF_R (0x4)
74-
# NOPHDR-NEXT: PF_W (0x2)
75-
# NOPHDR-NEXT: ]
76-
# NOPHDR-NEXT: Alignment: 4096
77-
# NOPHDR-NEXT: }
78-
# NOPHDR-NEXT: ]
79-
80-
# AT: ProgramHeaders [
81-
# AT-NEXT: ProgramHeader {
82-
# AT-NEXT: Type: PT_LOAD (0x1)
83-
# AT-NEXT: Offset: 0x0
84-
# AT-NEXT: VirtualAddress: 0x10000000
85-
# AT-NEXT: PhysicalAddress: 0xA00
86-
# AT-NEXT: FileSize: 521
87-
# AT-NEXT: MemSize: 521
88-
# AT-NEXT: Flags [ (0x7)
89-
# AT-NEXT: PF_R (0x4)
90-
# AT-NEXT: PF_W (0x2)
91-
# AT-NEXT: PF_X (0x1)
92-
# AT-NEXT: ]
80+
PHDRS { all PT_LOAD; }
81+
SECTIONS { .baz : {*(.foo.*)} :bar }
9382

94-
## Check the numetic values for PHDRS.
95-
# RUN: echo "PHDRS {text PT_LOAD FILEHDR PHDRS; foo 0x11223344; } \
96-
# RUN: SECTIONS { . = SIZEOF_HEADERS; .foo : { *(.foo* .text*) } : text : foo}" > %t1.script
97-
# RUN: ld.lld -o %t2 --script %t1.script %t
98-
# RUN: llvm-readobj -l %t2 | FileCheck --check-prefix=INT-PHDRS %s
99-
100-
# INT-PHDRS: ProgramHeaders [
101-
# INT-PHDRS: ProgramHeader {
102-
# INT-PHDRS: Type: Unknown (0x11223344)
103-
# INT-PHDRS-NEXT: Offset: 0xB0
104-
# INT-PHDRS-NEXT: VirtualAddress: 0xB0
105-
# INT-PHDRS-NEXT: PhysicalAddress: 0xB0
106-
# INT-PHDRS-NEXT: FileSize:
107-
# INT-PHDRS-NEXT: MemSize:
108-
# INT-PHDRS-NEXT: Flags [
109-
# INT-PHDRS-NEXT: PF_R
110-
# INT-PHDRS-NEXT: PF_W
111-
# INT-PHDRS-NEXT: PF_X
112-
# INT-PHDRS-NEXT: ]
113-
# INT-PHDRS-NEXT: Alignment:
114-
# INT-PHDRS-NEXT: }
115-
# INT-PHDRS-NEXT: ]
116-
117-
# DEFHDR: ProgramHeaders [
118-
# DEFHDR-NEXT: ProgramHeader {
119-
# DEFHDR-NEXT: Type: PT_LOAD (0x1)
120-
# DEFHDR-NEXT: Offset: 0x0
121-
# DEFHDR-NEXT: VirtualAddress: 0x10000000
122-
# DEFHDR-NEXT: PhysicalAddress: 0x10000000
123-
# DEFHDR-NEXT: FileSize: 521
124-
# DEFHDR-NEXT: MemSize: 521
125-
# DEFHDR-NEXT: Flags [ (0x7)
126-
# DEFHDR-NEXT: PF_R (0x4)
127-
# DEFHDR-NEXT: PF_W (0x2)
128-
# DEFHDR-NEXT: PF_X (0x1)
129-
# DEFHDR-NEXT: ]
130-
131-
# BADHDR: {{.*}}.script:1: program header 'bar' is not listed in PHDRS
132-
133-
# RUN: echo "PHDRS { text PT_LOAD FOOHDR; }" > %t1.script
134-
# RUN: not ld.lld -o /dev/null --script %t1.script %t 2>&1 | FileCheck --check-prefix=FOOHDR %s
135-
# FOOHDR: error: {{.*}}.script:1: unexpected header attribute: FOOHDR
136-
137-
# RUN: echo "PHDRS { text PT_FOO FOOHDR; }" > %t1.script
138-
# RUN: not ld.lld -o /dev/null --script %t1.script %t 2>&1 | FileCheck --check-prefix=PTFOO %s
139-
# PTFOO: invalid program header type: PT_FOO
83+
# RUN: not ld.lld -T unspecified.lds a.o 2>&1 | FileCheck --check-prefix=UNSPECIFIED %s
84+
# UNSPECIFIED: unspecified.lds:6: program header 'bar' is not listed in PHDRS
85+
86+
#--- foohdr.lds
87+
PHDRS { text PT_LOAD FOOHDR; }
88+
89+
# RUN: not ld.lld -T foohdr.lds a.o 2>&1 | FileCheck --check-prefix=FOOHDR %s
90+
# FOOHDR: error: foohdr.lds:1: unexpected header attribute: FOOHDR
91+
92+
#--- pt_foo.lds
93+
PHDRS { text PT_FOO FOOHDR; }
94+
95+
# RUN: not ld.lld -T pt_foo.lds a.o 2>&1 | FileCheck --check-prefix=PTFOO %s --strict-whitespace
96+
# PTFOO:{{.*}}error: pt_foo.lds:1: invalid program header type: PT_FOO
97+
# PTFOO-NEXT:>>> PHDRS { text PT_FOO FOOHDR; }
98+
# PTFOO-NEXT:>>> ^
14099

100+
#--- a.s
141101
.global _start
142102
_start:
143103
nop

0 commit comments

Comments
 (0)