llvm: Set PIC level 1 for MIPS.

For hysterical raisins, MIPS always uses 1, regardless of `-fpic` vs `-fPIC`.
This commit is contained in:
Alex Rønne Petersen 2024-08-28 03:02:04 +02:00
parent 93cb44c805
commit 7d9edff11d
No known key found for this signature in database
5 changed files with 21 additions and 12 deletions

View File

@ -1259,8 +1259,11 @@ pub const Object = struct {
); );
errdefer target_machine.dispose(); errdefer target_machine.dispose();
if (pic) module.setModulePICLevel(); const large_pic = target_util.usesLargePIC(comp.root_mod.resolved_target.result);
if (comp.config.pie) module.setModulePIELevel();
if (pic) module.setModulePICLevel(large_pic);
if (comp.config.pie) module.setModulePIELevel(large_pic);
if (code_model != .Default) module.setModuleCodeModel(code_model); if (code_model != .Default) module.setModuleCodeModel(code_model);
if (comp.llvm_opt_bisect_limit >= 0) { if (comp.llvm_opt_bisect_limit >= 0) {

View File

@ -53,10 +53,10 @@ pub const Module = opaque {
extern fn LLVMDisposeModule(*Module) void; extern fn LLVMDisposeModule(*Module) void;
pub const setModulePICLevel = ZigLLVMSetModulePICLevel; pub const setModulePICLevel = ZigLLVMSetModulePICLevel;
extern fn ZigLLVMSetModulePICLevel(module: *Module) void; extern fn ZigLLVMSetModulePICLevel(module: *Module, big: bool) void;
pub const setModulePIELevel = ZigLLVMSetModulePIELevel; pub const setModulePIELevel = ZigLLVMSetModulePIELevel;
extern fn ZigLLVMSetModulePIELevel(module: *Module) void; extern fn ZigLLVMSetModulePIELevel(module: *Module, large: bool) void;
pub const setModuleCodeModel = ZigLLVMSetModuleCodeModel; pub const setModuleCodeModel = ZigLLVMSetModuleCodeModel;
extern fn ZigLLVMSetModuleCodeModel(module: *Module, code_model: CodeModel) void; extern fn ZigLLVMSetModuleCodeModel(module: *Module, code_model: CodeModel) void;

View File

@ -49,6 +49,12 @@ pub fn requiresPIC(target: std.Target, linking_libc: bool) bool {
(target.abi == .ohos and target.cpu.arch == .aarch64); (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 /// This is not whether the target supports Position Independent Code, but whether the -fPIC
/// C compiler argument is valid to Clang. /// C compiler argument is valid to Clang.
pub fn supports_fpic(target: std.Target) bool { pub fn supports_fpic(target: std.Target) bool {

View File

@ -430,12 +430,12 @@ void ZigLLVMParseCommandLineOptions(size_t argc, const char *const *argv) {
cl::ParseCommandLineOptions(argc, argv); cl::ParseCommandLineOptions(argc, argv);
} }
void ZigLLVMSetModulePICLevel(LLVMModuleRef module) { void ZigLLVMSetModulePICLevel(LLVMModuleRef module, bool big) {
unwrap(module)->setPICLevel(PICLevel::Level::BigPIC); unwrap(module)->setPICLevel(big ? PICLevel::Level::BigPIC : PICLevel::Level::SmallPIC);
} }
void ZigLLVMSetModulePIELevel(LLVMModuleRef module) { void ZigLLVMSetModulePIELevel(LLVMModuleRef module, bool large) {
unwrap(module)->setPIELevel(PIELevel::Level::Large); unwrap(module)->setPIELevel(large ? PIELevel::Level::Large : PIELevel::Level::Small);
} }
void ZigLLVMSetModuleCodeModel(LLVMModuleRef module, LLVMCodeModel code_model) { void ZigLLVMSetModuleCodeModel(LLVMModuleRef module, LLVMCodeModel code_model) {

View File

@ -154,8 +154,8 @@ enum ZigLLVM_CallingConv {
ZigLLVM_MaxID = 1023, ZigLLVM_MaxID = 1023,
}; };
ZIG_EXTERN_C void ZigLLVMSetModulePICLevel(LLVMModuleRef module); ZIG_EXTERN_C void ZigLLVMSetModulePICLevel(LLVMModuleRef module, bool big);
ZIG_EXTERN_C void ZigLLVMSetModulePIELevel(LLVMModuleRef module); ZIG_EXTERN_C void ZigLLVMSetModulePIELevel(LLVMModuleRef module, bool large);
ZIG_EXTERN_C void ZigLLVMSetModuleCodeModel(LLVMModuleRef module, LLVMCodeModel code_model); ZIG_EXTERN_C void ZigLLVMSetModuleCodeModel(LLVMModuleRef module, LLVMCodeModel code_model);
ZIG_EXTERN_C void ZigLLVMParseCommandLineOptions(size_t argc, const char *const *argv); ZIG_EXTERN_C void ZigLLVMParseCommandLineOptions(size_t argc, const char *const *argv);