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 {
12591259 );
12601260 errdefer target_machine.dispose();
12611261
1262 if (pic) module.setModulePICLevel();
1263 if (comp.config.pie) module.setModulePIELevel();
1262 const large_pic = target_util.usesLargePIC(comp.root_mod.resolved_target.result);
1263
1264 if (pic) module.setModulePICLevel(large_pic);
1265 if (comp.config.pie) module.setModulePIELevel(large_pic);
1266
12641267 if (code_model != .Default) module.setModuleCodeModel(code_model);
12651268
12661269 if (comp.llvm_opt_bisect_limit >= 0) {
src/codegen/llvm/bindings.zig+2-2
......@@ -53,10 +53,10 @@ pub const Module = opaque {
5353 extern fn LLVMDisposeModule(*Module) void;
5454
5555 pub const setModulePICLevel = ZigLLVMSetModulePICLevel;
56 extern fn ZigLLVMSetModulePICLevel(module: *Module) void;
56 extern fn ZigLLVMSetModulePICLevel(module: *Module, big: bool) void;
5757
5858 pub const setModulePIELevel = ZigLLVMSetModulePIELevel;
59 extern fn ZigLLVMSetModulePIELevel(module: *Module) void;
59 extern fn ZigLLVMSetModulePIELevel(module: *Module, large: bool) void;
6060
6161 pub const setModuleCodeModel = ZigLLVMSetModuleCodeModel;
6262 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 {
4949 (target.abi == .ohos and target.cpu.arch == .aarch64);
5050}
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
5258/// This is not whether the target supports Position Independent Code, but whether the -fPIC
5359/// C compiler argument is valid to Clang.
5460pub fn supports_fpic(target: std.Target) bool {
src/zig_llvm.cpp+5-5
......@@ -81,7 +81,7 @@ static const bool assertions_on = false;
8181
8282LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,
8383 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,
8585 const char *abi_name)
8686{
8787 std::optional<Reloc::Model> RM;
......@@ -430,12 +430,12 @@ void ZigLLVMParseCommandLineOptions(size_t argc, const char *const *argv) {
430430 cl::ParseCommandLineOptions(argc, argv);
431431}
432432
433void ZigLLVMSetModulePICLevel(LLVMModuleRef module) {
434 unwrap(module)->setPICLevel(PICLevel::Level::BigPIC);
433void ZigLLVMSetModulePICLevel(LLVMModuleRef module, bool big) {
434 unwrap(module)->setPICLevel(big ? PICLevel::Level::BigPIC : PICLevel::Level::SmallPIC);
435435}
436436
437void ZigLLVMSetModulePIELevel(LLVMModuleRef module) {
438 unwrap(module)->setPIELevel(PIELevel::Level::Large);
437void ZigLLVMSetModulePIELevel(LLVMModuleRef module, bool large) {
438 unwrap(module)->setPIELevel(large ? PIELevel::Level::Large : PIELevel::Level::Small);
439439}
440440
441441void ZigLLVMSetModuleCodeModel(LLVMModuleRef module, LLVMCodeModel code_model) {
src/zig_llvm.h+3-3
......@@ -77,7 +77,7 @@ enum ZigLLVMABIType {
7777
7878ZIG_EXTERN_C LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,
7979 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,
8181 const char *abi_name);
8282
8383ZIG_EXTERN_C void ZigLLVMSetOptBisectLimit(LLVMContextRef context_ref, int limit);
......@@ -154,8 +154,8 @@ enum ZigLLVM_CallingConv {
154154 ZigLLVM_MaxID = 1023,
155155};
156156
157ZIG_EXTERN_C void ZigLLVMSetModulePICLevel(LLVMModuleRef module);
158ZIG_EXTERN_C void ZigLLVMSetModulePIELevel(LLVMModuleRef module);
157ZIG_EXTERN_C void ZigLLVMSetModulePICLevel(LLVMModuleRef module, bool big);
158ZIG_EXTERN_C void ZigLLVMSetModulePIELevel(LLVMModuleRef module, bool large);
159159ZIG_EXTERN_C void ZigLLVMSetModuleCodeModel(LLVMModuleRef module, LLVMCodeModel code_model);
160160
161161ZIG_EXTERN_C void ZigLLVMParseCommandLineOptions(size_t argc, const char *const *argv);