commit 7d9edff11d4c0dd6793bfd4bb2d7e3c2d32c88cd (tree)
parent 93cb44c80582dd02b63b02e7bb7e54d7ad8a4ebc
Author: Alex Rønne Petersen <alex@alexrp.com>
Date: Wed, 28 Aug 2024 03:02:04 +0200
llvm: Set PIC level 1 for MIPS.
For hysterical raisins, MIPS always uses 1, regardless of `-fpic` vs `-fPIC`.
Diffstat:
5 files changed, 21 insertions(+), 12 deletions(-)
diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig
@@ -1259,8 +1259,11 @@ pub const Object = struct {
);
errdefer target_machine.dispose();
- if (pic) module.setModulePICLevel();
- if (comp.config.pie) module.setModulePIELevel();
+ const large_pic = target_util.usesLargePIC(comp.root_mod.resolved_target.result);
+
+ if (pic) module.setModulePICLevel(large_pic);
+ if (comp.config.pie) module.setModulePIELevel(large_pic);
+
if (code_model != .Default) module.setModuleCodeModel(code_model);
if (comp.llvm_opt_bisect_limit >= 0) {
diff --git a/src/codegen/llvm/bindings.zig b/src/codegen/llvm/bindings.zig
@@ -53,10 +53,10 @@ pub const Module = opaque {
extern fn LLVMDisposeModule(*Module) void;
pub const setModulePICLevel = ZigLLVMSetModulePICLevel;
- extern fn ZigLLVMSetModulePICLevel(module: *Module) void;
+ extern fn ZigLLVMSetModulePICLevel(module: *Module, big: bool) void;
pub const setModulePIELevel = ZigLLVMSetModulePIELevel;
- extern fn ZigLLVMSetModulePIELevel(module: *Module) void;
+ extern fn ZigLLVMSetModulePIELevel(module: *Module, large: bool) void;
pub const setModuleCodeModel = ZigLLVMSetModuleCodeModel;
extern fn ZigLLVMSetModuleCodeModel(module: *Module, code_model: CodeModel) void;
diff --git a/src/target.zig b/src/target.zig
@@ -49,6 +49,12 @@ pub fn requiresPIC(target: std.Target, linking_libc: bool) bool {
(target.abi == .ohos and target.cpu.arch == .aarch64);
}
+pub fn usesLargePIC(target: std.Target) bool {
+ // MIPS always uses PIC level 1; other platforms vary in their default PIC levels, but they
+ // support both level 1 and 2, in which case we prefer 2.
+ return !target.cpu.arch.isMIPS();
+}
+
/// This is not whether the target supports Position Independent Code, but whether the -fPIC
/// C compiler argument is valid to Clang.
pub fn supports_fpic(target: std.Target) bool {
diff --git a/src/zig_llvm.cpp b/src/zig_llvm.cpp
@@ -81,7 +81,7 @@ static const bool assertions_on = false;
LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,
const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
- LLVMCodeModel CodeModel, bool function_sections, bool data_sections, ZigLLVMABIType float_abi,
+ LLVMCodeModel CodeModel, bool function_sections, bool data_sections, ZigLLVMABIType float_abi,
const char *abi_name)
{
std::optional<Reloc::Model> RM;
@@ -430,12 +430,12 @@ void ZigLLVMParseCommandLineOptions(size_t argc, const char *const *argv) {
cl::ParseCommandLineOptions(argc, argv);
}
-void ZigLLVMSetModulePICLevel(LLVMModuleRef module) {
- unwrap(module)->setPICLevel(PICLevel::Level::BigPIC);
+void ZigLLVMSetModulePICLevel(LLVMModuleRef module, bool big) {
+ unwrap(module)->setPICLevel(big ? PICLevel::Level::BigPIC : PICLevel::Level::SmallPIC);
}
-void ZigLLVMSetModulePIELevel(LLVMModuleRef module) {
- unwrap(module)->setPIELevel(PIELevel::Level::Large);
+void ZigLLVMSetModulePIELevel(LLVMModuleRef module, bool large) {
+ unwrap(module)->setPIELevel(large ? PIELevel::Level::Large : PIELevel::Level::Small);
}
void ZigLLVMSetModuleCodeModel(LLVMModuleRef module, LLVMCodeModel code_model) {
diff --git a/src/zig_llvm.h b/src/zig_llvm.h
@@ -77,7 +77,7 @@ enum ZigLLVMABIType {
ZIG_EXTERN_C LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple,
const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
- LLVMCodeModel CodeModel, bool function_sections, bool data_sections, enum ZigLLVMABIType float_abi,
+ LLVMCodeModel CodeModel, bool function_sections, bool data_sections, enum ZigLLVMABIType float_abi,
const char *abi_name);
ZIG_EXTERN_C void ZigLLVMSetOptBisectLimit(LLVMContextRef context_ref, int limit);
@@ -154,8 +154,8 @@ enum ZigLLVM_CallingConv {
ZigLLVM_MaxID = 1023,
};
-ZIG_EXTERN_C void ZigLLVMSetModulePICLevel(LLVMModuleRef module);
-ZIG_EXTERN_C void ZigLLVMSetModulePIELevel(LLVMModuleRef module);
+ZIG_EXTERN_C void ZigLLVMSetModulePICLevel(LLVMModuleRef module, bool big);
+ZIG_EXTERN_C void ZigLLVMSetModulePIELevel(LLVMModuleRef module, bool large);
ZIG_EXTERN_C void ZigLLVMSetModuleCodeModel(LLVMModuleRef module, LLVMCodeModel code_model);
ZIG_EXTERN_C void ZigLLVMParseCommandLineOptions(size_t argc, const char *const *argv);