blob: c3b2bbe32f5d165a42d2e8ede2ec137e221c7d61 [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//
Chandler Carruth7ce816a2012-11-19 03:52:03 +000014// CHECK-PIE1: "-mrelocation-model" "pic"
15// CHECK-PIE1: "-pic-level" "1"
Chandler Carruth5e219cf2012-04-08 16:40:35 +000016// CHECK-PIE1: "-pie-level" "1"
17//
Chandler Carruth7ce816a2012-11-19 03:52:03 +000018// CHECK-PIE2: "-mrelocation-model" "pic"
19// CHECK-PIE2: "-pic-level" "2"
Chandler Carruth5e219cf2012-04-08 16:40:35 +000020// CHECK-PIE2: "-pie-level" "2"
21//
Chandler Carruth7ce816a2012-11-19 03:52:03 +000022// CHECK-PIE-LD: "{{.*}}ld{{(.exe)?}}"
23// CHECK-PIE-LD: "-pie"
24// CHECK-PIE-LD: "Scrt1.o" "crti.o" "crtbeginS.o"
25// CHECK-PIE-LD: "crtendS.o" "crtn.o"
26//
Rafael Espindola9adba392013-06-05 04:28:55 +000027// CHECK-NOPIE-LD: "-nopie"
28//
Chandler Carruth7ce816a2012-11-19 03:52:03 +000029// CHECK-DYNAMIC-NO-PIC-32: "-mrelocation-model" "dynamic-no-pic"
30// CHECK-DYNAMIC-NO-PIC-32-NOT: "-pic-level"
31// CHECK-DYNAMIC-NO-PIC-32-NOT: "-pie-level"
32//
33// CHECK-DYNAMIC-NO-PIC-64: "-mrelocation-model" "dynamic-no-pic"
34// CHECK-DYNAMIC-NO-PIC-64: "-pic-level" "2"
35// CHECK-DYNAMIC-NO-PIC-64-NOT: "-pie-level"
36//
37// CHECK-NON-DARWIN-DYNAMIC-NO-PIC: error: unsupported option '-mdynamic-no-pic' for target 'i386-unknown-unknown'
Roman Divackyf6513812012-09-07 13:36:21 +000038//
Peter Collingbourne17d481f2013-01-17 20:17:16 +000039// CHECK-NO-PIE-NOT: "-pie"
40//
Alexey Samsonovdb68e5a2013-04-09 12:28:19 +000041// CHECK-NO-UNUSED-ARG-NOT: argument unused during compilation
42//
Chandler Carruth5e219cf2012-04-08 16:40:35 +000043// RUN: %clang -c %s -target i386-unknown-unknown -### 2>&1 \
44// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
45// RUN: %clang -c %s -target i386-unknown-unknown -fpic -### 2>&1 \
46// RUN: | FileCheck %s --check-prefix=CHECK-PIC1
47// RUN: %clang -c %s -target i386-unknown-unknown -fPIC -### 2>&1 \
48// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
49// RUN: %clang -c %s -target i386-unknown-unknown -fpie -### 2>&1 \
50// RUN: | FileCheck %s --check-prefix=CHECK-PIE1
51// RUN: %clang -c %s -target i386-unknown-unknown -fPIE -### 2>&1 \
52// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
Chandler Carruth7ce816a2012-11-19 03:52:03 +000053//
54// Check that PIC and PIE flags obey last-match-wins. If the last flag is
55// a no-* variant, regardless of which variant or which flags precede it, we
56// get no PIC.
Chandler Carruth5e219cf2012-04-08 16:40:35 +000057// RUN: %clang -c %s -target i386-unknown-unknown -fpic -fno-pic -### 2>&1 \
58// RUN: | FileCheck %s --check-prefix=CHECK-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 Carruth7ce816a2012-11-19 03:52:03 +000061// RUN: %clang -c %s -target i386-unknown-unknown -fpie -fno-pic -### 2>&1 \
62// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
63// 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 -fpic -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 -fpie -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 -fpic -fno-pie -### 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
Chandler Carruth5e219cf2012-04-08 16:40:35 +000077// RUN: %clang -c %s -target i386-unknown-unknown -fpie -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 Carruth7ce816a2012-11-19 03:52:03 +000081// RUN: %clang -c %s -target i386-unknown-unknown -fpic -fno-PIE -### 2>&1 \
82// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
83// 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 -fpie -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//
90// Last-match-wins where both pic and pie are specified.
91// RUN: %clang -c %s -target i386-unknown-unknown -fpie -fpic -### 2>&1 \
Benjamin Kramerb12ecd32012-11-13 15:32:35 +000092// RUN: | FileCheck %s --check-prefix=CHECK-PIC1
Chandler Carruth7ce816a2012-11-19 03:52:03 +000093// RUN: %clang -c %s -target i386-unknown-unknown -fPIE -fpic -### 2>&1 \
94// RUN: | FileCheck %s --check-prefix=CHECK-PIC1
95// RUN: %clang -c %s -target i386-unknown-unknown -fpie -fPIC -### 2>&1 \
96// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
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 -fpic -fpie -### 2>&1 \
Benjamin Kramerb12ecd32012-11-13 15:32:35 +0000100// RUN: | FileCheck %s --check-prefix=CHECK-PIE1
Chandler Carruth7ce816a2012-11-19 03:52:03 +0000101// RUN: %clang -c %s -target i386-unknown-unknown -fPIC -fpie -### 2>&1 \
102// RUN: | FileCheck %s --check-prefix=CHECK-PIE1
103// RUN: %clang -c %s -target i386-unknown-unknown -fpic -fPIE -### 2>&1 \
104// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
105// RUN: %clang -c %s -target i386-unknown-unknown -fPIC -fPIE -### 2>&1 \
106// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
107//
108// Last-match-wins when selecting level 1 vs. level 2.
Chandler Carruth5e219cf2012-04-08 16:40:35 +0000109// RUN: %clang -c %s -target i386-unknown-unknown -fpic -fPIC -### 2>&1 \
110// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
111// RUN: %clang -c %s -target i386-unknown-unknown -fPIC -fpic -### 2>&1 \
112// RUN: | FileCheck %s --check-prefix=CHECK-PIC1
113// RUN: %clang -c %s -target i386-unknown-unknown -fpic -fPIE -fpie -### 2>&1 \
114// RUN: | FileCheck %s --check-prefix=CHECK-PIE1
115// RUN: %clang -c %s -target i386-unknown-unknown -fpie -fPIC -fPIE -### 2>&1 \
116// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
117//
Roman Divackyf6513812012-09-07 13:36:21 +0000118// Make sure -pie is passed to along to ld and that the right *crt* files
119// are linked in.
120// RUN: %clang %s -target i386-unknown-freebsd -fPIE -pie -### \
121// RUN: --sysroot=%S/Inputs/basic_freebsd_tree 2>&1 \
Chandler Carruth7ce816a2012-11-19 03:52:03 +0000122// RUN: | FileCheck %s --check-prefix=CHECK-PIE-LD
Roman Divackyf6513812012-09-07 13:36:21 +0000123// RUN: %clang %s -target i386-linux-gnu -fPIE -pie -### \
124// RUN: --sysroot=%S/Inputs/basic_linux_tree 2>&1 \
Chandler Carruth7ce816a2012-11-19 03:52:03 +0000125// RUN: | FileCheck %s --check-prefix=CHECK-PIE-LD
126// RUN: %clang %s -target i386-linux-gnu -fPIC -pie -### \
127// RUN: --sysroot=%S/Inputs/basic_linux_tree 2>&1 \
128// RUN: | FileCheck %s --check-prefix=CHECK-PIE-LD
Roman Divackyf6513812012-09-07 13:36:21 +0000129//
Chandler Carruth5e219cf2012-04-08 16:40:35 +0000130// Disregard any of the PIC-specific flags if we have a trump-card flag.
131// RUN: %clang -c %s -target i386-unknown-unknown -mkernel -fPIC -### 2>&1 \
132// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
133// RUN: %clang -c %s -target i386-unknown-unknown -static -fPIC -### 2>&1 \
134// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
Chandler Carruth7ce816a2012-11-19 03:52:03 +0000135//
Peter Collingbourne17d481f2013-01-17 20:17:16 +0000136// On Linux, disregard -pie if we have -shared.
137// RUN: %clang %s -target i386-unknown-linux -shared -pie -### 2>&1 \
138// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIE
139//
Chandler Carruth7ce816a2012-11-19 03:52:03 +0000140// Darwin is a beautiful and unique snowflake when it comes to these flags.
Stephen Hines651f13c2014-04-23 16:59:28 -0700141// When targeting a 32-bit darwin system, the -fno-* flag variants work and
Chandler Carruth7ce816a2012-11-19 03:52:03 +0000142// disable PIC, but any other flag enables PIC (*not* PIE) even if the flag
143// specifies PIE. On 64-bit targets, there is simply nothing you can do, there
144// is no PIE, there is only PIC when it comes to compilation.
145// RUN: %clang -c %s -target i386-apple-darwin -### 2>&1 \
146// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
147// RUN: %clang -c %s -target i386-apple-darwin -fpic -### 2>&1 \
148// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
149// RUN: %clang -c %s -target i386-apple-darwin -fPIC -### 2>&1 \
150// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
151// RUN: %clang -c %s -target i386-apple-darwin -fpie -### 2>&1 \
152// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
153// RUN: %clang -c %s -target i386-apple-darwin -fPIE -### 2>&1 \
154// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
155// RUN: %clang -c %s -target i386-apple-darwin -fno-PIC -### 2>&1 \
156// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
157// RUN: %clang -c %s -target i386-apple-darwin -fno-PIE -### 2>&1 \
158// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
159// RUN: %clang -c %s -target i386-apple-darwin -fno-PIC -fpic -### 2>&1 \
160// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
161// RUN: %clang -c %s -target i386-apple-darwin -fno-PIC -fPIE -### 2>&1 \
162// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
163// RUN: %clang -c %s -target x86_64-apple-darwin -fno-PIC -### 2>&1 \
164// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
165// RUN: %clang -c %s -target x86_64-apple-darwin -fno-PIE -### 2>&1 \
166// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
167// RUN: %clang -c %s -target x86_64-apple-darwin -fpic -### 2>&1 \
168// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
169// RUN: %clang -c %s -target x86_64-apple-darwin -fPIE -### 2>&1 \
170// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
Alexey Samsonovdb68e5a2013-04-09 12:28:19 +0000171// RUN: %clang -c %s -target x86_64-apple-darwin -fPIC -### 2>&1 \
172// RUN: | FileCheck %s --check-prefix=CHECK-NO-UNUSED-ARG
Chandler Carruth7ce816a2012-11-19 03:52:03 +0000173//
174// Darwin gets even more special with '-mdynamic-no-pic'. This flag is only
175// valid on Darwin, and it's behavior is very strange but needs to remain
176// consistent for compatibility.
177// RUN: %clang -c %s -target i386-unknown-unknown -mdynamic-no-pic -### 2>&1 \
178// RUN: | FileCheck %s --check-prefix=CHECK-NON-DARWIN-DYNAMIC-NO-PIC
179// RUN: %clang -c %s -target i386-apple-darwin -mdynamic-no-pic -### 2>&1 \
180// RUN: | FileCheck %s --check-prefix=CHECK-DYNAMIC-NO-PIC-32
181// RUN: %clang -c %s -target i386-apple-darwin -mdynamic-no-pic -fno-pic -### 2>&1 \
182// RUN: | FileCheck %s --check-prefix=CHECK-DYNAMIC-NO-PIC-32
183// RUN: %clang -c %s -target i386-apple-darwin -mdynamic-no-pic -fpie -### 2>&1 \
184// RUN: | FileCheck %s --check-prefix=CHECK-DYNAMIC-NO-PIC-32
185// RUN: %clang -c %s -target x86_64-apple-darwin -mdynamic-no-pic -### 2>&1 \
186// RUN: | FileCheck %s --check-prefix=CHECK-DYNAMIC-NO-PIC-64
187// RUN: %clang -c %s -target x86_64-apple-darwin -mdynamic-no-pic -fno-pic -### 2>&1 \
188// RUN: | FileCheck %s --check-prefix=CHECK-DYNAMIC-NO-PIC-64
189// RUN: %clang -c %s -target x86_64-apple-darwin -mdynamic-no-pic -fpie -### 2>&1 \
190// RUN: | FileCheck %s --check-prefix=CHECK-DYNAMIC-NO-PIC-64
191//
192// Checks for ARM+Apple+IOS including -fapple-kext, -mkernel, and iphoneos
193// version boundaries.
Daniel Dunbar7a0c0642012-10-15 22:23:53 +0000194// RUN: %clang -c %s -target armv7-apple-ios -fapple-kext -miphoneos-version-min=6.0.0 -### 2>&1 \
195// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
196// RUN: %clang -c %s -target armv7-apple-ios -mkernel -miphoneos-version-min=6.0.0 -### 2>&1 \
197// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
198// RUN: %clang -c %s -target armv7-apple-ios -fapple-kext -miphoneos-version-min=5.0.0 -### 2>&1 \
199// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
200// RUN: %clang -c %s -target armv7-apple-ios -fapple-kext -miphoneos-version-min=6.0.0 -static -### 2>&1 \
201// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
Rafael Espindola9adba392013-06-05 04:28:55 +0000202//
203// On OpenBSD, PIE is enabled by default, but can be disabled.
204// RUN: %clang -c %s -target i386-pc-openbsd -### 2>&1 \
205// RUN: | FileCheck %s --check-prefix=CHECK-PIE2
206// RUN: %clang -c %s -target i386-pc-openbsd -fno-pie -### 2>&1 \
207// RUN: | FileCheck %s --check-prefix=CHECK-NO-PIC
208//
209// On OpenBSD, -nopie needs to be passed through to the linker.
210// RUN: %clang %s -target i386-pc-openbsd -nopie -### 2>&1 \
211// RUN: | FileCheck %s --check-prefix=CHECK-NOPIE-LD
Stephen Hines651f13c2014-04-23 16:59:28 -0700212//
213// On Android PIC is enabled by default
214// RUN: %clang -c %s -target i686-linux-android -### 2>&1 \
215// RUN: | FileCheck %s --check-prefix=CHECK-PIC2
216// RUN: %clang -c %s -target arm-linux-androideabi -### 2>&1 \
217// RUN: | FileCheck %s --check-prefix=CHECK-PIC1
218// RUN: %clang -c %s -target mipsel-linux-android -### 2>&1 \
219// RUN: | FileCheck %s --check-prefix=CHECK-PIC1
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700220// RUN: %clang -c %s -target aarch64-linux-android -### 2>&1 \
221// RUN: | FileCheck %s --check-prefix=CHECK-PIC1
222// RUN: %clang -c %s -target arm64-linux-android -### 2>&1 \
223// RUN: | FileCheck %s --check-prefix=CHECK-PIC1