1# 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
12## Check that program headers are not written, unless we explicitly tell
13## 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
23## 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
41
42## Check that error is reported when trying to use phdr which is not listed
43## inside PHDRS {} block
44## TODO: If script doesn't contain PHDRS {} block then default phdr is always
45## 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:      ]
93
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
140
141.global _start
142_start:
143 nop
144
145.section .foo.1,"a"
146foo1:
147 .long 0
148
149.section .foo.2,"aw"
150foo2:
151 .long 0
152