authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-08-28 03:02:04+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-08-28 06:18:06+02:00
log7d9edff11d4c0dd6793bfd4bb2d7e3c2d32c88cd
tree82c547eab6f1cd93cef5905e88c14b0438cc5a12
parent93cb44c80582dd02b63b02e7bb7e54d7ad8a4ebc
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

llvm: Set PIC level 1 for MIPS.

For hysterical raisins, MIPS always uses 1, regardless of `-fpic` vs `-fPIC`.

5 files changed, 21 insertions(+), 12 deletions(-)

src/codegen/llvm.zig+5-2
...@@ -1259,8 +1259,11 @@ pub const Object = struct {...@@ -1259,8 +1259,11 @@ pub const Object = struct {
1259 );1259 );
1260 errdefer target_machine.dispose();1260 errdefer target_machine.dispose();
12611261
1262 if (pic) module.setModulePICLevel();1262 const large_pic = target_util.usesLargePIC(comp.root_mod.resolved_target.result);
1263 if (comp.config.pie) module.setModulePIELevel();1263
1264 if (pic) module.setModulePICLevel(large_pic);
1265 if (comp.config.pie) module.setModulePIELevel(large_pic);
1266
1264 if (code_model != .Default) module.setModuleCodeModel(code_model);1267 if (code_model != .Default) module.setModuleCodeModel(code_model);
12651268
1266 if (comp.llvm_opt_bisect_limit >= 0) {1269 if (comp.llvm_opt_bisect_limit >= 0) {
src/codegen/llvm/bindings.zig+2-2
...@@ -53,10 +53,10 @@ pub const Module = opaque {...@@ -53,10 +53,10 @@ pub const Module = opaque {
53 extern fn LLVMDisposeModule(*Module) void;53 extern fn LLVMDisposeModule(*Module) void;
5454
55 pub const setModulePICLevel = ZigLLVMSetModulePICLevel;55 pub const setModulePICLevel = ZigLLVMSetModulePICLevel;
56 extern fn ZigLLVMSetModulePICLevel(module: *Module) void;56 extern fn ZigLLVMSetModulePICLevel(module: *Module, big: bool) void;
5757
58 pub const setModulePIELevel = ZigLLVMSetModulePIELevel;58 pub const setModulePIELevel = ZigLLVMSetModulePIELevel;
59 extern fn ZigLLVMSetModulePIELevel(module: *Module) void;59 extern fn ZigLLVMSetModulePIELevel(module: *Module, large: bool) void;
6060
61 pub const setModuleCodeModel = ZigLLVMSetModuleCodeModel;61 pub const setModuleCodeModel = ZigLLVMSetModuleCodeModel;
62 extern fn ZigLLVMSetModuleCodeModel(module: *Module, code_model: CodeModel) void;62 extern fn ZigLLVMSetModuleCodeModel(module: *Module, code_model: CodeModel) void;
src/target.zig+6
...@@ -49,6 +49,12 @@ pub fn requiresPIC(target: std.Target, linking_libc: bool) bool {...@@ -49,6 +49,12 @@ pub fn requiresPIC(target: std.Target, linking_libc: bool) bool {
49 (target.abi == .ohos and target.cpu.arch == .aarch64);49 (target.abi == .ohos and target.cpu.arch == .aarch64);
50}50}
5151
52pub fn usesLargePIC(target: std.Target) bool {
53 // MIPS always uses PIC level 1; other platforms vary in their default PIC levels, but they
54 // support both level 1 and 2, in which case we prefer 2.
55 return !target.cpu.arch.isMIPS();
56}
57
52/// This is not whether the target supports Position Independent Code, but whether the -fPIC58/// This is not whether the target supports Position Independent Code, but whether the -fPIC
53/// C compiler argument is valid to Clang.59/// C compiler argument is valid to Clang.
54pub fn supports_fpic(target: std.Target) bool {60pub fn supports_fpic(target: std.Target) bool {
src/zig_llvm.cpp+5-5
...@@ -81,7 +81,7 @@ static const bool assertions_on = false;...@@ -81,7 +81,7 @@ static const bool assertions_on = false;
8181
82LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,82LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,
83 const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,83 const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
84 LLVMCodeModel CodeModel, bool function_sections, bool data_sections, ZigLLVMABIType float_abi, 84 LLVMCodeModel CodeModel, bool function_sections, bool data_sections, ZigLLVMABIType float_abi,
85 const char *abi_name)85 const char *abi_name)
86{86{
87 std::optional<Reloc::Model> RM;87 std::optional<Reloc::Model> RM;
...@@ -430,12 +430,12 @@ void ZigLLVMParseCommandLineOptions(size_t argc, const char *const *argv) {...@@ -430,12 +430,12 @@ void ZigLLVMParseCommandLineOptions(size_t argc, const char *const *argv) {
430 cl::ParseCommandLineOptions(argc, argv);430 cl::ParseCommandLineOptions(argc, argv);
431}431}
432432
433void ZigLLVMSetModulePICLevel(LLVMModuleRef module) {433void ZigLLVMSetModulePICLevel(LLVMModuleRef module, bool big) {
434 unwrap(module)->setPICLevel(PICLevel::Level::BigPIC);434 unwrap(module)->setPICLevel(big ? PICLevel::Level::BigPIC : PICLevel::Level::SmallPIC);
435}435}
436436
437void ZigLLVMSetModulePIELevel(LLVMModuleRef module) {437void ZigLLVMSetModulePIELevel(LLVMModuleRef module, bool large) {
438 unwrap(module)->setPIELevel(PIELevel::Level::Large);438 unwrap(module)->setPIELevel(large ? PIELevel::Level::Large : PIELevel::Level::Small);
439}439}
440440
441void ZigLLVMSetModuleCodeModel(LLVMModuleRef module, LLVMCodeModel code_model) {441void ZigLLVMSetModuleCodeModel(LLVMModuleRef module, LLVMCodeModel code_model) {
src/zig_llvm.h+3-3
...@@ -77,7 +77,7 @@ enum ZigLLVMABIType {...@@ -77,7 +77,7 @@ enum ZigLLVMABIType {
7777
78ZIG_EXTERN_C LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,78ZIG_EXTERN_C LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,
79 const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,79 const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
80 LLVMCodeModel CodeModel, bool function_sections, bool data_sections, enum ZigLLVMABIType float_abi, 80 LLVMCodeModel CodeModel, bool function_sections, bool data_sections, enum ZigLLVMABIType float_abi,
81 const char *abi_name);81 const char *abi_name);
8282
83ZIG_EXTERN_C void ZigLLVMSetOptBisectLimit(LLVMContextRef context_ref, int limit);83ZIG_EXTERN_C void ZigLLVMSetOptBisectLimit(LLVMContextRef context_ref, int limit);
...@@ -154,8 +154,8 @@ enum ZigLLVM_CallingConv {...@@ -154,8 +154,8 @@ enum ZigLLVM_CallingConv {
154 ZigLLVM_MaxID = 1023,154 ZigLLVM_MaxID = 1023,
155};155};
156156
157ZIG_EXTERN_C void ZigLLVMSetModulePICLevel(LLVMModuleRef module);157ZIG_EXTERN_C void ZigLLVMSetModulePICLevel(LLVMModuleRef module, bool big);
158ZIG_EXTERN_C void ZigLLVMSetModulePIELevel(LLVMModuleRef module);158ZIG_EXTERN_C void ZigLLVMSetModulePIELevel(LLVMModuleRef module, bool large);
159ZIG_EXTERN_C void ZigLLVMSetModuleCodeModel(LLVMModuleRef module, LLVMCodeModel code_model);159ZIG_EXTERN_C void ZigLLVMSetModuleCodeModel(LLVMModuleRef module, LLVMCodeModel code_model);
160160
161ZIG_EXTERN_C void ZigLLVMParseCommandLineOptions(size_t argc, const char *const *argv);161ZIG_EXTERN_C void ZigLLVMParseCommandLineOptions(size_t argc, const char *const *argv);