Clang Project

clang_source_code/test/Driver/pic.c
1// Test the driver's control over the PIC behavior. These consist of tests of
2// the relocation model flags and the pic level flags passed to CC1.
3//
4// CHECK-NO-PIC: "-mrelocation-model" "static"
5// CHECK-NO-PIC-NOT: "-pic-level"
6// CHECK-NO-PIC-NOT: "-pic-is-pie"
7//
8// CHECK-PIC1: "-mrelocation-model" "pic"
9// CHECK-PIC1: "-pic-level" "1"
10// CHECK-PIC1-NOT: "-pic-is-pie"
11//
12// CHECK-PIC2: "-mrelocation-model" "pic"
13// CHECK-PIC2: "-pic-level" "2"
14// CHECK-PIC2-NOT: "-pic-is-pie"
15//
16// CHECK-STATIC: "-static"
17// CHECK-NO-STATIC-NOT: "-static"
18//
19// CHECK-PIE1: "-mrelocation-model" "pic"
20// CHECK-PIE1: "-pic-level" "1"
21// CHECK-PIE1: "-pic-is-pie"
22//
23// CHECK-PIE2: "-mrelocation-model" "pic"
24// CHECK-PIE2: "-pic-level" "2"
25// CHECK-PIE2: "-pic-is-pie"
26//
27// CHECK-PIE-LD: "{{.*}}ld{{(.exe)?}}"
28// CHECK-PIE-LD: "-pie"
29// CHECK-PIE-LD: "Scrt1.o" "crti.o" "crtbeginS.o"
30// CHECK-PIE-LD: "crtendS.o" "crtn.o"
31//
32// CHECK-NOPIE-LD: "-nopie"
33//
34// CHECK-DYNAMIC-NO-PIC-32: "-mrelocation-model" "dynamic-no-pic"
35// CHECK-DYNAMIC-NO-PIC-32-NOT: "-pic-level"
36// CHECK-DYNAMIC-NO-PIC-32-NOT: "-pic-is-pie"
37//
38// CHECK-DYNAMIC-NO-PIC-64: "-mrelocation-model" "dynamic-no-pic"
39// CHECK-DYNAMIC-NO-PIC-64: "-pic-level" "2"
40// CHECK-DYNAMIC-NO-PIC-64-NOT: "-pic-is-pie"
41//
42// CHECK-NON-DARWIN-DYNAMIC-NO-PIC: error: unsupported option '-mdynamic-no-pic' for target 'i386-unknown-unknown'
43//
44// CHECK-NO-PIE-NOT: "-pie"
45//
46// CHECK-NO-UNUSED-ARG-NOT: argument unused during compilation
47//
48// RUN: %clang -c %s -target i386-unknown-unknown -### 2>&1 \
49// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
50// RUN: %clang -c %s -target i386-unknown-unknown -fpic -### 2>&1 \
51// RUN:   | FileCheck %s --check-prefix=CHECK-PIC1
52// RUN: %clang -c %s -target i386-unknown-unknown -fPIC -### 2>&1 \
53// RUN:   | FileCheck %s --check-prefix=CHECK-PIC2
54// RUN: %clang -c %s -target i386-unknown-unknown -fpie -### 2>&1 \
55// RUN:   | FileCheck %s --check-prefix=CHECK-PIE1
56// RUN: %clang -c %s -target i386-unknown-unknown -fPIE -### 2>&1 \
57// RUN:   | FileCheck %s --check-prefix=CHECK-PIE2
58//
59// Check that PIC and PIE flags obey last-match-wins. If the last flag is
60// a no-* variant, regardless of which variant or which flags precede it, we
61// get no PIC.
62// RUN: %clang -c %s -target i386-unknown-unknown -fpic -fno-pic -### 2>&1 \
63// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
64// RUN: %clang -c %s -target i386-unknown-unknown -fPIC -fno-pic -### 2>&1 \
65// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
66// RUN: %clang -c %s -target i386-unknown-unknown -fpie -fno-pic -### 2>&1 \
67// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
68// RUN: %clang -c %s -target i386-unknown-unknown -fPIE -fno-pic -### 2>&1 \
69// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
70// RUN: %clang -c %s -target i386-unknown-unknown -fpic -fno-PIC -### 2>&1 \
71// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
72// RUN: %clang -c %s -target i386-unknown-unknown -fPIC -fno-PIC -### 2>&1 \
73// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
74// RUN: %clang -c %s -target i386-unknown-unknown -fpie -fno-PIC -### 2>&1 \
75// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
76// RUN: %clang -c %s -target i386-unknown-unknown -fPIE -fno-PIC -### 2>&1 \
77// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
78// RUN: %clang -c %s -target i386-unknown-unknown -fpic -fno-pie -### 2>&1 \
79// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
80// RUN: %clang -c %s -target i386-unknown-unknown -fPIC -fno-pie -### 2>&1 \
81// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
82// RUN: %clang -c %s -target i386-unknown-unknown -fpie -fno-pie -### 2>&1 \
83// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
84// RUN: %clang -c %s -target i386-unknown-unknown -fPIE -fno-pie -### 2>&1 \
85// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
86// RUN: %clang -c %s -target i386-unknown-unknown -fpic -fno-PIE -### 2>&1 \
87// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
88// RUN: %clang -c %s -target i386-unknown-unknown -fPIC -fno-PIE -### 2>&1 \
89// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
90// RUN: %clang -c %s -target i386-unknown-unknown -fpie -fno-PIE -### 2>&1 \
91// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
92// RUN: %clang -c %s -target i386-unknown-unknown -fPIE -fno-PIE -### 2>&1 \
93// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
94//
95// Last-match-wins where both pic and pie are specified.
96// RUN: %clang -c %s -target i386-unknown-unknown -fpie -fpic -### 2>&1 \
97// RUN:   | FileCheck %s --check-prefix=CHECK-PIC1
98// RUN: %clang -c %s -target i386-unknown-unknown -fPIE -fpic -### 2>&1 \
99// RUN:   | FileCheck %s --check-prefix=CHECK-PIC1
100// RUN: %clang -c %s -target i386-unknown-unknown -fpie -fPIC -### 2>&1 \
101// RUN:   | FileCheck %s --check-prefix=CHECK-PIC2
102// RUN: %clang -c %s -target i386-unknown-unknown -fPIE -fPIC -### 2>&1 \
103// RUN:   | FileCheck %s --check-prefix=CHECK-PIC2
104// RUN: %clang -c %s -target i386-unknown-unknown -fpic -fpie -### 2>&1 \
105// RUN:   | FileCheck %s --check-prefix=CHECK-PIE1
106// RUN: %clang -c %s -target i386-unknown-unknown -fPIC -fpie -### 2>&1 \
107// RUN:   | FileCheck %s --check-prefix=CHECK-PIE1
108// RUN: %clang -c %s -target i386-unknown-unknown -fpic -fPIE -### 2>&1 \
109// RUN:   | FileCheck %s --check-prefix=CHECK-PIE2
110// RUN: %clang -c %s -target i386-unknown-unknown -fPIC -fPIE -### 2>&1 \
111// RUN:   | FileCheck %s --check-prefix=CHECK-PIE2
112//
113// Last-match-wins when selecting level 1 vs. level 2.
114// RUN: %clang -c %s -target i386-unknown-unknown -fpic -fPIC -### 2>&1 \
115// RUN:   | FileCheck %s --check-prefix=CHECK-PIC2
116// RUN: %clang -c %s -target i386-unknown-unknown -fPIC -fpic -### 2>&1 \
117// RUN:   | FileCheck %s --check-prefix=CHECK-PIC1
118// RUN: %clang -c %s -target i386-unknown-unknown -fpic -fPIE -fpie -### 2>&1 \
119// RUN:   | FileCheck %s --check-prefix=CHECK-PIE1
120// RUN: %clang -c %s -target i386-unknown-unknown -fpie -fPIC -fPIE -### 2>&1 \
121// RUN:   | FileCheck %s --check-prefix=CHECK-PIE2
122//
123// Make sure -pie is passed to along to ld and that the right *crt* files
124// are linked in.
125// RUN: %clang %s -target i386-unknown-freebsd -fPIE -pie -### \
126// RUN: --gcc-toolchain="" \
127// RUN: --sysroot=%S/Inputs/basic_freebsd_tree 2>&1 \
128// RUN:   | FileCheck %s --check-prefix=CHECK-PIE-LD
129// RUN: %clang %s -target i386-linux-gnu -fPIE -pie -### \
130// RUN: --gcc-toolchain="" \
131// RUN: --sysroot=%S/Inputs/basic_linux_tree 2>&1 \
132// RUN:   | FileCheck %s --check-prefix=CHECK-PIE-LD
133// RUN: %clang %s -target i386-linux-gnu -fPIC -pie -### \
134// RUN: --gcc-toolchain="" \
135// RUN: --sysroot=%S/Inputs/basic_linux_tree 2>&1 \
136// RUN:   | FileCheck %s --check-prefix=CHECK-PIE-LD
137//
138// Disregard any of the PIC-specific flags if we have a trump-card flag.
139// RUN: %clang -c %s -target i386-unknown-unknown -mkernel -fPIC -### 2>&1 \
140// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
141
142// The -static argument *doesn't* override PIC: -static only affects
143// linking, and -fPIC only affects code generation.
144// RUN: %clang -c %s -target i386-unknown-unknown -static -fPIC -### 2>&1 \
145// RUN:   | FileCheck %s --check-prefix=CHECK-PIC2
146// RUN: %clang %s -target i386-linux-gnu -static -fPIC -### \
147// RUN: --gcc-toolchain="" \
148// RUN: --sysroot=%S/Inputs/basic_linux_tree 2>&1 \
149// RUN:   | FileCheck %s --check-prefix=CHECK-STATIC
150//
151// On Linux, disregard -pie if we have -shared.
152// RUN: %clang %s -target i386-unknown-linux -shared -pie -### 2>&1 \
153// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIE
154//
155// On Musl Linux, PIE is enabled by default, but can be disabled.
156// RUN: %clang -c %s -target x86_64-linux-musl -### 2>&1 \
157// RUN:   | FileCheck %s --check-prefix=CHECK-PIE2
158// RUN: %clang -c %s -target i686-linux-musl -### 2>&1 \
159// RUN:   | FileCheck %s --check-prefix=CHECK-PIE2
160// RUN: %clang -c %s -target armv6-linux-musleabihf -### 2>&1 \
161// RUN:   | FileCheck %s --check-prefix=CHECK-PIE2
162// RUN: %clang -c %s -target armv7-linux-musleabihf -### 2>&1 \
163// RUN:   | FileCheck %s --check-prefix=CHECK-PIE2
164// RUN: %clang %s -target x86_64-linux-musl -nopie -### 2>&1 \
165// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIE
166// RUN: %clang %s -target x86_64-linux-musl -pie -nopie -### 2>&1 \
167// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIE
168// RUN: %clang %s -target x86_64-linux-musl -nopie -pie -### 2>&1 \
169// RUN:   | FileCheck %s --check-prefix=CHECK-PIE2
170//
171// Darwin is a beautiful and unique snowflake when it comes to these flags.
172// When targeting a 32-bit darwin system, only level 2 is supported. On 64-bit
173// targets, there is simply nothing you can do, there is no PIE, there is only
174// PIC when it comes to compilation.
175// RUN: %clang -c %s -target i386-apple-darwin -### 2>&1 \
176// RUN:   | FileCheck %s --check-prefix=CHECK-PIC2
177// RUN: %clang -c %s -target i386-apple-darwin -fpic -### 2>&1 \
178// RUN:   | FileCheck %s --check-prefix=CHECK-PIC2
179// RUN: %clang -c %s -target i386-apple-darwin -fPIC -### 2>&1 \
180// RUN:   | FileCheck %s --check-prefix=CHECK-PIC2
181// RUN: %clang -c %s -target i386-apple-darwin -fpie -### 2>&1 \
182// RUN:   | FileCheck %s --check-prefix=CHECK-PIE2
183// RUN: %clang -c %s -target i386-apple-darwin -fPIE -### 2>&1 \
184// RUN:   | FileCheck %s --check-prefix=CHECK-PIE2
185// RUN: %clang -c %s -target i386-apple-darwin -fno-PIC -### 2>&1 \
186// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
187// RUN: %clang -c %s -target i386-apple-darwin -fno-PIE -### 2>&1 \
188// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
189// RUN: %clang -c %s -target i386-apple-darwin -fno-PIC -fpic -### 2>&1 \
190// RUN:   | FileCheck %s --check-prefix=CHECK-PIC2
191// RUN: %clang -c %s -target i386-apple-darwin -fno-PIC -fPIE -### 2>&1 \
192// RUN:   | FileCheck %s --check-prefix=CHECK-PIE2
193// RUN: %clang -c %s -target x86_64-apple-darwin -fno-PIC -### 2>&1 \
194// RUN:   | FileCheck %s --check-prefix=CHECK-PIC2
195// RUN: %clang -c %s -target x86_64-apple-darwin -fno-PIE -### 2>&1 \
196// RUN:   | FileCheck %s --check-prefix=CHECK-PIC2
197// RUN: %clang -c %s -target x86_64-apple-darwin -fpic -### 2>&1 \
198// RUN:   | FileCheck %s --check-prefix=CHECK-PIC2
199// RUN: %clang -c %s -target x86_64-apple-darwin -fPIE -### 2>&1 \
200// RUN:   | FileCheck %s --check-prefix=CHECK-PIC2
201// RUN: %clang -c %s -target x86_64-apple-darwin -fPIC -### 2>&1 \
202// RUN:   | FileCheck %s --check-prefix=CHECK-NO-UNUSED-ARG
203//
204// Darwin gets even more special with '-mdynamic-no-pic'. This flag is only
205// valid on Darwin, and it's behavior is very strange but needs to remain
206// consistent for compatibility.
207// RUN: %clang -c %s -target i386-unknown-unknown -mdynamic-no-pic -### 2>&1 \
208// RUN:   | FileCheck %s --check-prefix=CHECK-NON-DARWIN-DYNAMIC-NO-PIC
209// RUN: %clang -c %s -target i386-apple-darwin -mdynamic-no-pic -### 2>&1 \
210// RUN:   | FileCheck %s --check-prefix=CHECK-DYNAMIC-NO-PIC-32
211// RUN: %clang -c %s -target i386-apple-darwin -mdynamic-no-pic -fno-pic -### 2>&1 \
212// RUN:   | FileCheck %s --check-prefix=CHECK-DYNAMIC-NO-PIC-32
213// RUN: %clang -c %s -target i386-apple-darwin -mdynamic-no-pic -fpie -### 2>&1 \
214// RUN:   | FileCheck %s --check-prefix=CHECK-DYNAMIC-NO-PIC-32
215// RUN: %clang -c %s -target x86_64-apple-darwin -mdynamic-no-pic -### 2>&1 \
216// RUN:   | FileCheck %s --check-prefix=CHECK-DYNAMIC-NO-PIC-64
217// RUN: %clang -c %s -target x86_64-apple-darwin -mdynamic-no-pic -fno-pic -### 2>&1 \
218// RUN:   | FileCheck %s --check-prefix=CHECK-DYNAMIC-NO-PIC-64
219// RUN: %clang -c %s -target x86_64-apple-darwin -mdynamic-no-pic -fpie -### 2>&1 \
220// RUN:   | FileCheck %s --check-prefix=CHECK-DYNAMIC-NO-PIC-64
221//
222// Checks for ARM+Apple+IOS including -fapple-kext, -mkernel, and iphoneos
223// version boundaries.
224// RUN: %clang -c %s -target armv7-apple-ios6 -fapple-kext -### 2>&1 \
225// RUN:   | FileCheck %s --check-prefix=CHECK-PIC2
226// RUN: %clang -c %s -target armv7-apple-ios6 -mkernel -### 2>&1 \
227// RUN:   | FileCheck %s --check-prefix=CHECK-PIC2
228// RUN: %clang -c %s -target arm64-apple-ios7 -mkernel -### 2>&1 \
229// RUN:   | FileCheck %s --check-prefix=CHECK-PIC2
230// RUN: %clang -x assembler -c %s -target arm64-apple-ios7 -mkernel -no-integrated-as -### 2>&1 \
231// RUN:   | FileCheck %s --check-prefix=CHECK-NO-STATIC
232// RUN: %clang -c %s -target armv7k-apple-watchos1 -fapple-kext -### 2>&1 \
233// RUN:   | FileCheck %s --check-prefix=CHECK-PIC2
234// RUN: %clang -c %s -target armv7-apple-ios5 -fapple-kext -### 2>&1 \
235// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
236// RUN: %clang -c %s -target armv7-apple-ios6 -fapple-kext -static -### 2>&1 \
237// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
238// RUN: %clang -c %s -target armv7-apple-unknown-macho -static -### 2>&1 \
239// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
240//
241// On OpenBSD, PIE is enabled by default, but can be disabled.
242// RUN: %clang -c %s -target amd64-pc-openbsd -### 2>&1 \
243// RUN:   | FileCheck %s --check-prefix=CHECK-PIE1
244// RUN: %clang -c %s -target i386-pc-openbsd -### 2>&1 \
245// RUN:   | FileCheck %s --check-prefix=CHECK-PIE1
246// RUN: %clang -c %s -target aarch64-unknown-openbsd -### 2>&1 \
247// RUN:   | FileCheck %s --check-prefix=CHECK-PIE1
248// RUN: %clang -c %s -target arm-unknown-openbsd -### 2>&1 \
249// RUN:   | FileCheck %s --check-prefix=CHECK-PIE1
250// RUN: %clang -c %s -target mips64-unknown-openbsd -### 2>&1 \
251// RUN:   | FileCheck %s --check-prefix=CHECK-PIE1
252// RUN: %clang -c %s -target mips64el-unknown-openbsd -### 2>&1 \
253// RUN:   | FileCheck %s --check-prefix=CHECK-PIE1
254// RUN: %clang -c %s -target powerpc-unknown-openbsd -### 2>&1 \
255// RUN:   | FileCheck %s --check-prefix=CHECK-PIE2
256// RUN: %clang -c %s -target sparc-unknown-openbsd -### 2>&1 \
257// RUN:   | FileCheck %s --check-prefix=CHECK-PIE2
258// RUN: %clang -c %s -target sparc64-unknown-openbsd -### 2>&1 \
259// RUN:   | FileCheck %s --check-prefix=CHECK-PIE2
260// RUN: %clang -c %s -target i386-pc-openbsd -fno-pie -### 2>&1 \
261// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
262//
263// On OpenBSD, -nopie needs to be passed through to the linker.
264// RUN: %clang %s -target i386-pc-openbsd -nopie -### 2>&1 \
265// RUN:   | FileCheck %s --check-prefix=CHECK-NOPIE-LD
266// Try with the alias
267// RUN: %clang %s -target i386-pc-openbsd -no-pie -### 2>&1 \
268// RUN:   | FileCheck %s --check-prefix=CHECK-NOPIE-LD
269//
270// On Android PIC is enabled by default, and PIE is enabled by default starting
271// with API16.
272// RUN: %clang -c %s -target i686-linux-android -### 2>&1 \
273// RUN:   | FileCheck %s --check-prefix=CHECK-PIC2
274// RUN: %clang -c %s -target i686-linux-android14 -### 2>&1 \
275// RUN:   | FileCheck %s --check-prefix=CHECK-PIC2
276// RUN: %clang -c %s -target i686-linux-android16 -### 2>&1 \
277// RUN:   | FileCheck %s --check-prefix=CHECK-PIE2
278// RUN: %clang -c %s -target i686-linux-android24 -### 2>&1 \
279// RUN:   | FileCheck %s --check-prefix=CHECK-PIE2
280//
281// RUN: %clang -c %s -target arm-linux-androideabi -### 2>&1 \
282// RUN:   | FileCheck %s --check-prefix=CHECK-PIC1
283// RUN: %clang -c %s -target arm-linux-androideabi14 -### 2>&1 \
284// RUN:   | FileCheck %s --check-prefix=CHECK-PIC1
285// RUN: %clang -c %s -target arm-linux-androideabi16 -### 2>&1 \
286// RUN:   | FileCheck %s --check-prefix=CHECK-PIE2
287// RUN: %clang -c %s -target arm-linux-androideabi24 -### 2>&1 \
288// RUN:   | FileCheck %s --check-prefix=CHECK-PIE2
289//
290// RUN: %clang -c %s -target mipsel-linux-android -### 2>&1 \
291// RUN:   | FileCheck %s --check-prefix=CHECK-PIC1
292// RUN: %clang -c %s -target mipsel-linux-android14 -### 2>&1 \
293// RUN:   | FileCheck %s --check-prefix=CHECK-PIC1
294// RUN: %clang -c %s -target mipsel-linux-android16 -### 2>&1 \
295// RUN:   | FileCheck %s --check-prefix=CHECK-PIE1
296// RUN: %clang -c %s -target mipsel-linux-android24 -### 2>&1 \
297// RUN:   | FileCheck %s --check-prefix=CHECK-PIE1
298//
299// 64-bit Android targets are always PIE.
300// RUN: %clang -c %s -target aarch64-linux-android -### 2>&1 \
301// RUN:   | FileCheck %s --check-prefix=CHECK-PIE2
302// RUN: %clang -c %s -target aarch64-linux-android24 -### 2>&1 \
303// RUN:   | FileCheck %s --check-prefix=CHECK-PIE2
304// RUN: %clang -c %s -target arm64-linux-android -### 2>&1 \
305// RUN:   | FileCheck %s --check-prefix=CHECK-PIE2
306//
307// Default value of PIE can be overwritten, even on 64-bit targets.
308// RUN: %clang -c %s -target arm-linux-androideabi -fPIE -### 2>&1 \
309// RUN:   | FileCheck %s --check-prefix=CHECK-PIE2
310// RUN: %clang -c %s -target i686-linux-android14 -fPIE -### 2>&1 \
311// RUN:   | FileCheck %s --check-prefix=CHECK-PIE2
312// RUN: %clang -c %s -target i686-linux-android16 -fno-PIE -### 2>&1 \
313// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
314// RUN: %clang -c %s -target aarch64-linux-android -fno-PIE -### 2>&1 \
315// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
316// RUN: %clang -c %s -target aarch64-linux-android24 -fno-PIE -### 2>&1 \
317// RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
318//
319// On Windows-X64 PIC is enabled by default
320// RUN: %clang -c %s -target x86_64-pc-windows-msvc18.0.0 -### 2>&1 \
321// RUN:   | FileCheck %s --check-prefix=CHECK-PIC2
322// RUN: %clang -c %s -target x86_64-pc-windows-gnu -### 2>&1 \
323// RUN:   | FileCheck %s --check-prefix=CHECK-PIC2
324