Skip to content

Commit 20d603b

Browse files
Aleksandar BeserminjiAleksandar Beserminji
Aleksandar Beserminji
authored and
Aleksandar Beserminji
committedMay 7, 2018
[mips] Improve handling of -fno-[pic/PIC] option
In order to disable PIC and to match GCC behaviour, -mno-abicalls option is neccessary. When -fno-[pic/PIC] is used witout -mno-abicalls, warning is reported. An error is reported when -fno-pic or -fno-PIC is used in combination with -mabicalls. In this commit, test case is added. Depends on D44381. Differential Revision: https://reviews.llvm.org/D44684 llvm-svn: 331640
1 parent 535f5ae commit 20d603b

File tree

7 files changed

+61
-19
lines changed

7 files changed

+61
-19
lines changed
 

‎clang/include/clang/Basic/DiagnosticDriverKinds.td

+5-3
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,12 @@ def warn_drv_unsupported_longcalls : Warning<
339339
"ignoring '-mlong-calls' option as it is not currently supported with "
340340
"%select{|the implicit usage of }0-mabicalls">,
341341
InGroup<OptionIgnored>;
342-
def warn_drv_unsupported_abicalls : Warning<
343-
"ignoring '-mabicalls' option as it cannot be used with "
344-
"non position-independent code and the N64 ABI">,
342+
def warn_drv_unsupported_pic_with_mabicalls : Warning<
343+
"ignoring '%0' option as it cannot be used with "
344+
"%select{implicit usage of|}1 -mabicalls and the N64 ABI">,
345345
InGroup<OptionIgnored>;
346+
def err_drv_unsupported_noabicalls_pic : Error<
347+
"position-independent code requires ‘-mabicalls’">;
346348
def err_drv_unsupported_indirect_jump_opt : Error<
347349
"'-mindirect-jump=%0' is unsupported with the '%1' architecture">;
348350
def err_drv_unknown_indirect_jump_opt : Error<

‎clang/lib/Driver/ToolChains/Arch/Mips.cpp

+12-3
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ void mips::getMIPSTargetFeatures(const Driver &D, const llvm::Triple &Triple,
214214
// For case (a) we need to add +noabicalls for N64.
215215

216216
bool IsN64 = ABIName == "64";
217+
bool IsPIC = false;
217218
bool NonPIC = false;
218219

219220
Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC,
@@ -225,6 +226,9 @@ void mips::getMIPSTargetFeatures(const Driver &D, const llvm::Triple &Triple,
225226
NonPIC =
226227
(O.matches(options::OPT_fno_PIC) || O.matches(options::OPT_fno_pic) ||
227228
O.matches(options::OPT_fno_PIE) || O.matches(options::OPT_fno_pie));
229+
IsPIC =
230+
(O.matches(options::OPT_fPIC) || O.matches(options::OPT_fpic) ||
231+
O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie));
228232
}
229233

230234
bool UseAbiCalls = false;
@@ -234,9 +238,14 @@ void mips::getMIPSTargetFeatures(const Driver &D, const llvm::Triple &Triple,
234238
UseAbiCalls =
235239
!ABICallsArg || ABICallsArg->getOption().matches(options::OPT_mabicalls);
236240

237-
if (UseAbiCalls && IsN64 && NonPIC) {
238-
D.Diag(diag::warn_drv_unsupported_abicalls);
239-
UseAbiCalls = false;
241+
if (IsN64 && NonPIC && (!ABICallsArg || UseAbiCalls)) {
242+
D.Diag(diag::warn_drv_unsupported_pic_with_mabicalls)
243+
<< LastPICArg->getAsString(Args) << (!ABICallsArg ? 0 : 1);
244+
NonPIC = false;
245+
}
246+
247+
if (ABICallsArg && !UseAbiCalls && IsPIC) {
248+
D.Diag(diag::err_drv_unsupported_noabicalls_pic);
240249
}
241250

242251
if (!UseAbiCalls)

‎clang/lib/Driver/ToolChains/CommonArgs.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,14 @@ tools::ParsePICArgs(const ToolChain &ToolChain, const ArgList &Args) {
10181018
Triple.getArch() == llvm::Triple::mipsel ||
10191019
Triple.getArch() == llvm::Triple::mips64 ||
10201020
Triple.getArch() == llvm::Triple::mips64el) {
1021+
StringRef CPUName;
1022+
StringRef ABIName;
1023+
mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
1024+
// When targeting the N64 ABI, PIC is the default, except in the case
1025+
// when the -mno-abicalls option is used. In that case we exit
1026+
// at next check regardless of PIC being set below.
1027+
if (ABIName == "n64")
1028+
PIC = true;
10211029
// When targettng MIPS with -mno-abicalls, it's always static.
10221030
if(Args.hasArg(options::OPT_mno_abicalls))
10231031
return std::make_tuple(llvm::Reloc::Static, 0U, false);
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// RUN: not %clang -c -target mips64-linux-gnu -fPIC -mno-abicalls %s 2>&1 | FileCheck %s
2+
// CHECK: error: position-independent code requires ‘-mabicalls’

‎clang/test/Driver/mips-abicalls-warning.c

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
// REQUIRES: mips-registered-target
2-
// RUN: %clang -### -c -target mips64-mti-elf -fno-PIC -mabicalls %s 2>&1 | FileCheck %s
3-
// CHECK: warning: ignoring '-mabicalls' option as it cannot be used with non position-independent code and the N64 ABI
2+
// RUN: %clang -### -c -target mips64-mti-elf -fno-pic %s 2>&1 | FileCheck -check-prefix=CHECK-PIC1-IMPLICIT %s
3+
// CHECK-PIC1-IMPLICIT: warning: ignoring '-fno-pic' option as it cannot be used with implicit usage of -mabicalls and the N64 ABI
4+
5+
// RUN: %clang -### -c -target mips64-mti-elf -fno-pic -mabicalls %s 2>&1 | FileCheck -check-prefix=CHECK-PIC1-EXPLICIT %s
6+
// CHECK-PIC1-EXPLICIT: warning: ignoring '-fno-pic' option as it cannot be used with -mabicalls and the N64 ABI
7+
8+
// RUN: %clang -### -c -target mips64-mti-elf -fno-PIC %s 2>&1 | FileCheck -check-prefix=CHECK-PIC2-IMPLICIT %s
9+
// CHECK-PIC2-IMPLICIT: warning: ignoring '-fno-PIC' option as it cannot be used with implicit usage of -mabicalls and the N64 ABI
10+
11+
// RUN: %clang -### -c -target mips64-mti-elf -fno-PIC -mabicalls %s 2>&1 | FileCheck -check-prefix=CHECK-PIC2-EXPLICIT %s
12+
// CHECK-PIC2-EXPLICIT: warning: ignoring '-fno-PIC' option as it cannot be used with -mabicalls and the N64 ABI
13+
14+
// RUN: %clang -### -c -target mips64-mti-elf -fno-pie %s 2>&1 | FileCheck -check-prefix=CHECK-PIE1-IMPLICIT %s
15+
// CHECK-PIE1-IMPLICIT: warning: ignoring '-fno-pie' option as it cannot be used with implicit usage of -mabicalls and the N64 ABI
16+
17+
// RUN: %clang -### -c -target mips64-mti-elf -fno-pie -mabicalls %s 2>&1 | FileCheck -check-prefix=CHECK-PIE1-EXPLICIT %s
18+
// CHECK-PIE1-EXPLICIT: warning: ignoring '-fno-pie' option as it cannot be used with -mabicalls and the N64 ABI
19+
20+
// RUN: %clang -### -c -target mips64-mti-elf -fno-PIE %s 2>&1 | FileCheck -check-prefix=CHECK-PIE2-IMPLICIT %s
21+
// CHECK-PIE2-IMPLICIT: warning: ignoring '-fno-PIE' option as it cannot be used with implicit usage of -mabicalls and the N64 ABI
22+
23+
// RUN: %clang -### -c -target mips64-mti-elf -fno-PIE -mabicalls %s 2>&1 | FileCheck -check-prefix=CHECK-PIE2-EXPLICIT %s
24+
// CHECK-PIE2-EXPLICIT: warning: ignoring '-fno-PIE' option as it cannot be used with -mabicalls and the N64 ABI
425

526
// RUN: %clang -### -c -target mips-mti-elf -mlong-calls %s 2>&1 | FileCheck -check-prefix=LONGCALL-IMP %s
627
// LONGCALL-IMP: warning: ignoring '-mlong-calls' option as it is not currently supported with the implicit usage of -mabicalls

‎clang/test/Driver/mips-as.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// MIPS32R2-DEF-EL-AS: as{{(.exe)?}}" "-march" "mips32r2" "-mabi" "32" "-mno-shared" "-call_nonpic" "-EL"
2222
//
2323
// RUN: %clang -target mips64-linux-gnu -### \
24-
// RUN: -no-integrated-as -fno-pic -c %s 2>&1 \
24+
// RUN: -no-integrated-as -fno-pic -mno-abicalls -c %s 2>&1 \
2525
// RUN: | FileCheck -check-prefix=MIPS64R2-EB-AS %s
2626
// MIPS64R2-EB-AS: as{{(.exe)?}}" "-march" "mips64r2" "-mabi" "64" "-mno-shared" "-EB"
2727
//
@@ -31,7 +31,7 @@
3131
// MIPS64R2-EB-AS-PIC: as{{(.exe)?}}" "-march" "mips64r2" "-mabi" "64" "-EB" "-KPIC"
3232
//
3333
// RUN: %clang -target mips64el-linux-gnu -### \
34-
// RUN: -no-integrated-as -c -fno-pic %s 2>&1 \
34+
// RUN: -no-integrated-as -c -fno-pic -mno-abicalls %s 2>&1 \
3535
// RUN: | FileCheck -check-prefix=MIPS64R2-DEF-EL-AS %s
3636
// MIPS64R2-DEF-EL-AS: as{{(.exe)?}}" "-march" "mips64r2" "-mabi" "64" "-mno-shared" "-EL"
3737
//
@@ -64,7 +64,7 @@
6464
// MIPS64R2-EL-AS-PIC: as{{(.exe)?}}" "-march" "mips64r2" "-mabi" "64" "-EL" "-KPIC"
6565
//
6666
// RUN: %clang -target mips64el-linux-gnu -mabi=64 -### \
67-
// RUN: -no-integrated-as -c %s -fno-pic 2>&1 \
67+
// RUN: -no-integrated-as -c %s -fno-pic -mno-abicalls 2>&1 \
6868
// RUN: | FileCheck -check-prefix=MIPS64R2-EL-AS %s
6969
// MIPS64R2-EL-AS: as{{(.exe)?}}" "-march" "mips64r2" "-mabi" "64" "-mno-shared" "-EL"
7070
//
@@ -84,7 +84,7 @@
8484
// MIPS-OCTEON-PIC: as{{(.exe)?}}" "-march" "octeon" "-mabi" "64" "-EB" "-KPIC"
8585
//
8686
// RUN: %clang -target mips64-linux-gnu -march=octeon -### \
87-
// RUN: -no-integrated-as -c %s -fno-pic 2>&1 \
87+
// RUN: -no-integrated-as -c %s -fno-pic -mno-abicalls 2>&1 \
8888
// RUN: | FileCheck -check-prefix=MIPS-OCTEON %s
8989
// MIPS-OCTEON: as{{(.exe)?}}" "-march" "octeon" "-mabi" "64" "-mno-shared" "-EB"
9090
//
@@ -144,7 +144,7 @@
144144
// MIPS-ALIAS-64-PIC: as{{(.exe)?}}" "-march" "mips64" "-mabi" "64" "-EB" "-KPIC"
145145
//
146146
// RUN: %clang -target mips64-linux-gnu -mips64 -### \
147-
// RUN: -no-integrated-as -c -fno-pic %s 2>&1 \
147+
// RUN: -no-integrated-as -c -fno-pic -mno-abicalls %s 2>&1 \
148148
// RUN: | FileCheck -check-prefix=MIPS-ALIAS-64 %s
149149
// MIPS-ALIAS-64: as{{(.exe)?}}" "-march" "mips64" "-mabi" "64" "-mno-shared" "-EB"
150150
//
@@ -159,7 +159,7 @@
159159
// MIPS-ALIAS-64R3-PIC: as{{(.exe)?}}" "-march" "mips64r3" "-mabi" "64" "-EB" "-KPIC"
160160
//
161161
// RUN: %clang -target mips64-linux-gnu -mips64r3 -### \
162-
// RUN: -no-integrated-as -c %s -fno-pic 2>&1 \
162+
// RUN: -no-integrated-as -c %s -fno-pic -mno-abicalls 2>&1 \
163163
// RUN: | FileCheck -check-prefix=MIPS-ALIAS-64R3 %s
164164
// MIPS-ALIAS-64R3: as{{(.exe)?}}" "-march" "mips64r3" "-mabi" "64" "-mno-shared" "-EB"
165165
//
@@ -169,7 +169,7 @@
169169
// MIPS-ALIAS-64R5-PIC: as{{(.exe)?}}" "-march" "mips64r5" "-mabi" "64" "-EB" "-KPIC"
170170
//
171171
// RUN: %clang -target mips64-linux-gnu -mips64r5 -### \
172-
// RUN: -no-integrated-as -c %s -fno-pic 2>&1 \
172+
// RUN: -no-integrated-as -c %s -fno-pic -mno-abicalls 2>&1 \
173173
// RUN: | FileCheck -check-prefix=MIPS-ALIAS-64R5 %s
174174
// MIPS-ALIAS-64R5: as{{(.exe)?}}" "-march" "mips64r5" "-mabi" "64" "-mno-shared" "-EB"
175175
//
@@ -179,7 +179,7 @@
179179
// MIPS-ALIAS-64R6-PIC: as{{(.exe)?}}" "-march" "mips64r6" "-mabi" "64" "-EB" "-KPIC"
180180
//
181181
// RUN: %clang -target mips64-linux-gnu -mips64r6 -### \
182-
// RUN: -no-integrated-as -c %s -fno-pic 2>&1 \
182+
// RUN: -no-integrated-as -c %s -fno-pic -mno-abicalls 2>&1 \
183183
// RUN: | FileCheck -check-prefix=MIPS-ALIAS-64R6 %s
184184
// MIPS-ALIAS-64R6: as{{(.exe)?}}" "-march" "mips64r6" "-mabi" "64" "-mno-shared" "-EB"
185185
//

‎clang/test/Driver/mips-features.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// CHECK-MNOABICALLS: "-target-feature" "+noabicalls"
1212
//
1313
// -mno-abicalls non-PIC N64
14-
// RUN: %clang -target mips64-linux-gnu -### -c -fno-PIC %s 2>&1 \
14+
// RUN: %clang -target mips64-linux-gnu -### -c -fno-PIC -mno-abicalls %s 2>&1 \
1515
// RUN: | FileCheck --check-prefix=CHECK-MNOABICALLS-N64NPIC %s
1616
// CHECK-MNOABICALLS-N64NPIC: "-target-feature" "+noabicalls"
1717
//
@@ -86,13 +86,13 @@
8686
// CHECK-MEMBEDDEDDATADEF-NOT: "-mllvm" "-membedded-data"
8787
//
8888
// MIPS64 + N64: -fno-pic -> -mno-abicalls -mgpopt
89-
// RUN: %clang -target mips64-mti-elf -mabi=64 -### -c %s -fno-pic 2>&1 \
89+
// RUN: %clang -target mips64-mti-elf -mabi=64 -### -c %s -fno-pic -mno-abicalls 2>&1 \
9090
// RUN: | FileCheck --check-prefix=CHECK-N64-GPOPT %s
9191
// CHECK-N64-GPOPT: "-target-feature" "+noabicalls"
9292
// CHECK-N64-GPOPT: "-mllvm" "-mgpopt"
9393
//
9494
// MIPS64 + N64: -fno-pic -mno-gpopt
95-
// RUN: %clang -target mips64-mti-elf -mabi=64 -### -c %s -fno-pic -mno-gpopt 2>&1 \
95+
// RUN: %clang -target mips64-mti-elf -mabi=64 -### -c %s -fno-pic -mno-abicalls -mno-gpopt 2>&1 \
9696
// RUN: | FileCheck --check-prefix=CHECK-N64-MNO-GPOPT %s
9797
// CHECK-N64-MNO-GPOPT: "-target-feature" "+noabicalls"
9898
// CHECK-N64-MNO-GPOPT-NOT: "-mllvm" "-mgpopt"

0 commit comments

Comments
 (0)
Please sign in to comment.