blob: 3a14d61cf1c6533f99c6c1e178abb1402a4a1cc5 [file] [log] [blame]
Chandler Carruth5e219cf2012-04-08 16:40:35 +00001// 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: "-pie-level"
7//
Chandler Carruth7ce816a2012-11-19 03:52:03 +00008// CHECK-PIC1: "-mrelocation-model" "pic"
Chandler Carruth5e219cf2012-04-08 16:40:35 +00009// CHECK-PIC1: "-pic-level" "1"
10//
Chandler Carruth7ce816a2012-11-19 03:52:03 +000011// CHECK-PIC2: "-mrelocation-model" "pic"
Chandler Carruth5e219cf2012-04-08 16:40:35 +000012// CHECK-PIC2: "-pic-level" "2"
13//
Stephen Hines0e2c34f2015-03-23 12:09:02 -070014// CHECK-NO-STATIC-NOT: "-static"
15//
Chandler Carruth7ce816a2012-11-19 03:52:03 +000016// CHECK-PIE1: "-mrelocation-model" "pic"
17// CHECK-PIE1: "-pic-level" "1"
Chandler Carruth5e219cf2012-04-08 16:40:35 +000018// CHECK-PIE1: "-pie-level" "1"
19//
Chandler Carruth7ce816a2012-11-19 03:52:03 +000020// CHECK-PIE2: "-mrelocation-model" "pic"
21// CHECK-PIE2: "-pic-level" "2"
Chandler Carruth5e219cf2012-04-08 16:40:35 +000022// CHECK-PIE2: "-pie-level" "2"
23//
Chandler Carruth7ce816a2012-11-19 03:52:03 +000024// CHECK-PIE-LD: "{{.*}}ld{{(.exe)?}}"
25// CHECK-PIE-LD: "-pie"
26// CHECK-PIE-LD: "Scrt1.o" "crti.o" "crtbeginS.o"
27// CHECK-PIE-LD: "crtendS.o" "crtn.o"
28//
Rafael Espindola9adba392013-06-05 04:28:55 +000029// CHECK-NOPIE-LD: "-nopie"
30//
Chandler Carruth7ce816a2012-11-19 03:52:03 +000031// CHECK-DYNAMIC-NO-PIC-32: "-mrelocation-model" "dynamic-no-pic"
32// CHECK-DYNAMIC-NO-PIC-32-NOT: "-pic-level"
33// CHECK-DYNAMIC-NO-PIC-32-NOT: "-pie-level"
34//
35// CHECK-DYNAMIC-NO-PIC-64: "-mrelocation-model" "dynamic-no-pic"
36// CHECK-DYNAMIC-NO-PIC-64: "-pic-level" "2"
37// CHECK-DYNAMIC-NO-PIC-64-NOT: "-pie-level"
38//
39// CHECK-NON-DARWIN-DYNAMIC-NO-PIC: error: unsupported option '-mdynamic-no-pic' for target 'i386-unknown-unknown'
Roman Divackyf6513812012-09-07 13:36:21 +000040//
Peter Collingbourne17d481f2013-01-17 20:17:16 +000041// CHECK-NO-PIE-NOT: "-pie"
42//
Alexey Samsonovdb68e5a2013-04-09 12:28:19 +000043// CHECK-NO-UNUSED-ARG-NOT: argument unused during compilation
44//
Chandler Carruth5e219cf2012-04-08 16:40:35 +000045// RUN: %clang -c %s -target i386-unknown-unknown -### 2>&1 \
46// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
47// RUN: %clang -c %s -target i386-unknown-unknown -fpic -### 2>&1 \
48// RUN: | FileCheck %s --check-prefix=CHECK-PIC1
49// RUN: %clang -c %s -target i386-unknown-unknown -fPIC -### 2>&1 \
50// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
51// RUN: %clang -c %s -target i386-unknown-unknown -fpie -### 2>&1 \
52// RUN: | FileCheck %s --check-prefix=CHECK-PIE1
53// RUN: %clang -c %s -target i386-unknown-unknown -fPIE -### 2>&1 \
54// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
Chandler Carruth7ce816a2012-11-19 03:52:03 +000055//
56// Check that PIC and PIE flags obey last-match-wins. If the last flag is
57// a no-* variant, regardless of which variant or which flags precede it, we
58// get no PIC.
Chandler Carruth5e219cf2012-04-08 16:40:35 +000059// RUN: %clang -c %s -target i386-unknown-unknown -fpic -fno-pic -### 2>&1 \
60// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
Chandler Carruth5e219cf2012-04-08 16:40:35 +000061// RUN: %clang -c %s -target i386-unknown-unknown -fPIC -fno-pic -### 2>&1 \
62// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
Chandler Carruth7ce816a2012-11-19 03:52:03 +000063// RUN: %clang -c %s -target i386-unknown-unknown -fpie -fno-pic -### 2>&1 \
64// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
65// RUN: %clang -c %s -target i386-unknown-unknown -fPIE -fno-pic -### 2>&1 \
66// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
67// RUN: %clang -c %s -target i386-unknown-unknown -fpic -fno-PIC -### 2>&1 \
68// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
69// RUN: %clang -c %s -target i386-unknown-unknown -fPIC -fno-PIC -### 2>&1 \
70// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
71// RUN: %clang -c %s -target i386-unknown-unknown -fpie -fno-PIC -### 2>&1 \
72// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
73// RUN: %clang -c %s -target i386-unknown-unknown -fPIE -fno-PIC -### 2>&1 \
74// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
75// RUN: %clang -c %s -target i386-unknown-unknown -fpic -fno-pie -### 2>&1 \
76// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
77// RUN: %clang -c %s -target i386-unknown-unknown -fPIC -fno-pie -### 2>&1 \
78// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
Chandler Carruth5e219cf2012-04-08 16:40:35 +000079// RUN: %clang -c %s -target i386-unknown-unknown -fpie -fno-pie -### 2>&1 \
80// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
Chandler Carruth5e219cf2012-04-08 16:40:35 +000081// RUN: %clang -c %s -target i386-unknown-unknown -fPIE -fno-pie -### 2>&1 \
82// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
Chandler Carruth7ce816a2012-11-19 03:52:03 +000083// RUN: %clang -c %s -target i386-unknown-unknown -fpic -fno-PIE -### 2>&1 \
84// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
85// RUN: %clang -c %s -target i386-unknown-unknown -fPIC -fno-PIE -### 2>&1 \
86// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
87// RUN: %clang -c %s -target i386-unknown-unknown -fpie -fno-PIE -### 2>&1 \
88// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
89// RUN: %clang -c %s -target i386-unknown-unknown -fPIE -fno-PIE -### 2>&1 \
90// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
91//
92// Last-match-wins where both pic and pie are specified.
93// RUN: %clang -c %s -target i386-unknown-unknown -fpie -fpic -### 2>&1 \
Benjamin Kramerb12ecd32012-11-13 15:32:35 +000094// RUN: | FileCheck %s --check-prefix=CHECK-PIC1
Chandler Carruth7ce816a2012-11-19 03:52:03 +000095// RUN: %clang -c %s -target i386-unknown-unknown -fPIE -fpic -### 2>&1 \
96// RUN: | FileCheck %s --check-prefix=CHECK-PIC1
97// RUN: %clang -c %s -target i386-unknown-unknown -fpie -fPIC -### 2>&1 \
98// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
99// RUN: %clang -c %s -target i386-unknown-unknown -fPIE -fPIC -### 2>&1 \
100// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
101// RUN: %clang -c %s -target i386-unknown-unknown -fpic -fpie -### 2>&1 \
Benjamin Kramerb12ecd32012-11-13 15:32:35 +0000102// RUN: | FileCheck %s --check-prefix=CHECK-PIE1
Chandler Carruth7ce816a2012-11-19 03:52:03 +0000103// RUN: %clang -c %s -target i386-unknown-unknown -fPIC -fpie -### 2>&1 \
104// RUN: | FileCheck %s --check-prefix=CHECK-PIE1
105// RUN: %clang -c %s -target i386-unknown-unknown -fpic -fPIE -### 2>&1 \
106// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
107// RUN: %clang -c %s -target i386-unknown-unknown -fPIC -fPIE -### 2>&1 \
108// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
109//
110// Last-match-wins when selecting level 1 vs. level 2.
Chandler Carruth5e219cf2012-04-08 16:40:35 +0000111// RUN: %clang -c %s -target i386-unknown-unknown -fpic -fPIC -### 2>&1 \
112// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
113// RUN: %clang -c %s -target i386-unknown-unknown -fPIC -fpic -### 2>&1 \
114// RUN: | FileCheck %s --check-prefix=CHECK-PIC1
115// RUN: %clang -c %s -target i386-unknown-unknown -fpic -fPIE -fpie -### 2>&1 \
116// RUN: | FileCheck %s --check-prefix=CHECK-PIE1
117// RUN: %clang -c %s -target i386-unknown-unknown -fpie -fPIC -fPIE -### 2>&1 \
118// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
119//
Roman Divackyf6513812012-09-07 13:36:21 +0000120// Make sure -pie is passed to along to ld and that the right *crt* files
121// are linked in.
122// RUN: %clang %s -target i386-unknown-freebsd -fPIE -pie -### \
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700123// RUN: --gcc-toolchain="" \
Roman Divackyf6513812012-09-07 13:36:21 +0000124// RUN: --sysroot=%S/Inputs/basic_freebsd_tree 2>&1 \
Chandler Carruth7ce816a2012-11-19 03:52:03 +0000125// RUN: | FileCheck %s --check-prefix=CHECK-PIE-LD
Roman Divackyf6513812012-09-07 13:36:21 +0000126// RUN: %clang %s -target i386-linux-gnu -fPIE -pie -### \
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700127// RUN: --gcc-toolchain="" \
Roman Divackyf6513812012-09-07 13:36:21 +0000128// RUN: --sysroot=%S/Inputs/basic_linux_tree 2>&1 \
Chandler Carruth7ce816a2012-11-19 03:52:03 +0000129// RUN: | FileCheck %s --check-prefix=CHECK-PIE-LD
130// RUN: %clang %s -target i386-linux-gnu -fPIC -pie -### \
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700131// RUN: --gcc-toolchain="" \
Chandler Carruth7ce816a2012-11-19 03:52:03 +0000132// RUN: --sysroot=%S/Inputs/basic_linux_tree 2>&1 \
133// RUN: | FileCheck %s --check-prefix=CHECK-PIE-LD
Roman Divackyf6513812012-09-07 13:36:21 +0000134//
Chandler Carruth5e219cf2012-04-08 16:40:35 +0000135// Disregard any of the PIC-specific flags if we have a trump-card flag.
136// RUN: %clang -c %s -target i386-unknown-unknown -mkernel -fPIC -### 2>&1 \
137// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
138// RUN: %clang -c %s -target i386-unknown-unknown -static -fPIC -### 2>&1 \
139// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
Chandler Carruth7ce816a2012-11-19 03:52:03 +0000140//
Peter Collingbourne17d481f2013-01-17 20:17:16 +0000141// On Linux, disregard -pie if we have -shared.
142// RUN: %clang %s -target i386-unknown-linux -shared -pie -### 2>&1 \
143// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIE
144//
Chandler Carruth7ce816a2012-11-19 03:52:03 +0000145// Darwin is a beautiful and unique snowflake when it comes to these flags.
Stephen Hines651f13c2014-04-23 16:59:28 -0700146// When targeting a 32-bit darwin system, the -fno-* flag variants work and
Chandler Carruth7ce816a2012-11-19 03:52:03 +0000147// disable PIC, but any other flag enables PIC (*not* PIE) even if the flag
148// specifies PIE. On 64-bit targets, there is simply nothing you can do, there
149// is no PIE, there is only PIC when it comes to compilation.
150// RUN: %clang -c %s -target i386-apple-darwin -### 2>&1 \
151// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
152// RUN: %clang -c %s -target i386-apple-darwin -fpic -### 2>&1 \
153// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
154// RUN: %clang -c %s -target i386-apple-darwin -fPIC -### 2>&1 \
155// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
156// RUN: %clang -c %s -target i386-apple-darwin -fpie -### 2>&1 \
157// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
158// RUN: %clang -c %s -target i386-apple-darwin -fPIE -### 2>&1 \
159// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
160// RUN: %clang -c %s -target i386-apple-darwin -fno-PIC -### 2>&1 \
161// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
162// RUN: %clang -c %s -target i386-apple-darwin -fno-PIE -### 2>&1 \
163// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
164// RUN: %clang -c %s -target i386-apple-darwin -fno-PIC -fpic -### 2>&1 \
165// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
166// RUN: %clang -c %s -target i386-apple-darwin -fno-PIC -fPIE -### 2>&1 \
167// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
168// RUN: %clang -c %s -target x86_64-apple-darwin -fno-PIC -### 2>&1 \
169// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
170// RUN: %clang -c %s -target x86_64-apple-darwin -fno-PIE -### 2>&1 \
171// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
172// RUN: %clang -c %s -target x86_64-apple-darwin -fpic -### 2>&1 \
173// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
174// RUN: %clang -c %s -target x86_64-apple-darwin -fPIE -### 2>&1 \
175// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
Alexey Samsonovdb68e5a2013-04-09 12:28:19 +0000176// RUN: %clang -c %s -target x86_64-apple-darwin -fPIC -### 2>&1 \
177// RUN: | FileCheck %s --check-prefix=CHECK-NO-UNUSED-ARG
Chandler Carruth7ce816a2012-11-19 03:52:03 +0000178//
179// Darwin gets even more special with '-mdynamic-no-pic'. This flag is only
180// valid on Darwin, and it's behavior is very strange but needs to remain
181// consistent for compatibility.
182// RUN: %clang -c %s -target i386-unknown-unknown -mdynamic-no-pic -### 2>&1 \
183// RUN: | FileCheck %s --check-prefix=CHECK-NON-DARWIN-DYNAMIC-NO-PIC
184// RUN: %clang -c %s -target i386-apple-darwin -mdynamic-no-pic -### 2>&1 \
185// RUN: | FileCheck %s --check-prefix=CHECK-DYNAMIC-NO-PIC-32
186// RUN: %clang -c %s -target i386-apple-darwin -mdynamic-no-pic -fno-pic -### 2>&1 \
187// RUN: | FileCheck %s --check-prefix=CHECK-DYNAMIC-NO-PIC-32
188// RUN: %clang -c %s -target i386-apple-darwin -mdynamic-no-pic -fpie -### 2>&1 \
189// RUN: | FileCheck %s --check-prefix=CHECK-DYNAMIC-NO-PIC-32
190// RUN: %clang -c %s -target x86_64-apple-darwin -mdynamic-no-pic -### 2>&1 \
191// RUN: | FileCheck %s --check-prefix=CHECK-DYNAMIC-NO-PIC-64
192// RUN: %clang -c %s -target x86_64-apple-darwin -mdynamic-no-pic -fno-pic -### 2>&1 \
193// RUN: | FileCheck %s --check-prefix=CHECK-DYNAMIC-NO-PIC-64
194// RUN: %clang -c %s -target x86_64-apple-darwin -mdynamic-no-pic -fpie -### 2>&1 \
195// RUN: | FileCheck %s --check-prefix=CHECK-DYNAMIC-NO-PIC-64
196//
197// Checks for ARM+Apple+IOS including -fapple-kext, -mkernel, and iphoneos
198// version boundaries.
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000199// RUN: %clang -c %s -target armv7-apple-ios -fapple-kext -miphoneos-version-min=6.0.0 -### 2>&1 \
200// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
201// RUN: %clang -c %s -target armv7-apple-ios -mkernel -miphoneos-version-min=6.0.0 -### 2>&1 \
202// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700203// RUN: %clang -c %s -target arm64-apple-ios -mkernel -miphoneos-version-min=7.0.0 -### 2>&1 \
204// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
205// RUN: %clang -x assembler -c %s -target arm64-apple-ios -mkernel -miphoneos-version-min=7.0.0 -no-integrated-as -### 2>&1 \
206// RUN: | FileCheck %s --check-prefix=CHECK-NO-STATIC
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000207// RUN: %clang -c %s -target armv7-apple-ios -fapple-kext -miphoneos-version-min=5.0.0 -### 2>&1 \
208// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
209// RUN: %clang -c %s -target armv7-apple-ios -fapple-kext -miphoneos-version-min=6.0.0 -static -### 2>&1 \
210// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
Rafael Espindola9adba392013-06-05 04:28:55 +0000211//
212// On OpenBSD, PIE is enabled by default, but can be disabled.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700213// RUN: %clang -c %s -target amd64-pc-openbsd -### 2>&1 \
214// RUN: | FileCheck %s --check-prefix=CHECK-PIE1
Rafael Espindola9adba392013-06-05 04:28:55 +0000215// RUN: %clang -c %s -target i386-pc-openbsd -### 2>&1 \
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700216// RUN: | FileCheck %s --check-prefix=CHECK-PIE1
217// RUN: %clang -c %s -target mips64-unknown-openbsd -### 2>&1 \
218// RUN: | FileCheck %s --check-prefix=CHECK-PIE1
219// RUN: %clang -c %s -target mips64el-unknown-openbsd -### 2>&1 \
220// RUN: | FileCheck %s --check-prefix=CHECK-PIE1
221// RUN: %clang -c %s -target powerpc-unknown-openbsd -### 2>&1 \
222// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
223// RUN: %clang -c %s -target sparc64-unknown-openbsd -### 2>&1 \
Rafael Espindola9adba392013-06-05 04:28:55 +0000224// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
225// RUN: %clang -c %s -target i386-pc-openbsd -fno-pie -### 2>&1 \
226// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
227//
228// On OpenBSD, -nopie needs to be passed through to the linker.
229// RUN: %clang %s -target i386-pc-openbsd -nopie -### 2>&1 \
230// RUN: | FileCheck %s --check-prefix=CHECK-NOPIE-LD
Stephen Hines651f13c2014-04-23 16:59:28 -0700231//
232// On Android PIC is enabled by default
233// RUN: %clang -c %s -target i686-linux-android -### 2>&1 \
234// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
235// RUN: %clang -c %s -target arm-linux-androideabi -### 2>&1 \
236// RUN: | FileCheck %s --check-prefix=CHECK-PIC1
237// RUN: %clang -c %s -target mipsel-linux-android -### 2>&1 \
238// RUN: | FileCheck %s --check-prefix=CHECK-PIC1
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700239// RUN: %clang -c %s -target aarch64-linux-android -### 2>&1 \
240// RUN: | FileCheck %s --check-prefix=CHECK-PIC1
241// RUN: %clang -c %s -target arm64-linux-android -### 2>&1 \
242// RUN: | FileCheck %s --check-prefix=CHECK-PIC1