Skip to content

Commit

Permalink
target: make isPICDefault, isPIEDefault functions independent of …
Browse files Browse the repository at this point in the history
…driver
  • Loading branch information
Organ1sm committed Sep 17, 2024
1 parent 6979af2 commit 3959d5e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/aro/Driver.zig
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ pub fn parseArgs(
}
d.comp.langopts.gnuc_version = version.toUnsigned();
const wants_pie: ?bool = if (d.desired_pie_level) |level| level != .none else null;
const pic_level, const is_pie = target_util.getPICMode(d.comp.target, d.desired_pic_level, wants_pie);
const pic_level, const is_pie = target_util.getPICMode(d.comp.target, d.use_linker, d.desired_pic_level, wants_pie);
d.comp.code_gen_options.pic_level = pic_level;
d.comp.code_gen_options.is_pie = is_pie;
return false;
Expand Down
105 changes: 62 additions & 43 deletions src/aro/target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const LangOpts = @import("LangOpts.zig");
const Type = @import("Type.zig");
const TargetSet = @import("Builtins/Properties.zig").TargetSet;
const Driver = @import("Driver.zig");
const system_defaults = @import("system_defaults");
const backend = @import("backend");

/// intmax_t for this target
Expand Down Expand Up @@ -724,8 +725,9 @@ pub fn toLLVMTriple(target: std.Target, buf: []u8) []const u8 {
return stream.getWritten();
}

fn isPIEDefault(d: *const Driver) bool {
const target = d.comp.target;
pub const DefaultPIStatus = enum { yes, no, depends_on_linker };

fn isPIEDefault(target: std.Target) DefaultPIStatus {
return switch (target.os.tag) {
.aix,
.haiku,
Expand Down Expand Up @@ -753,55 +755,51 @@ fn isPIEDefault(d: *const Driver) bool {
.hurd,
.zos,
.shadermodel,
=> false,
=> .no,

.openbsd,
.fuchsia,
=> true,
=> .yes,

.linux,
.elfiamcu,
=> {
if (target.abi == .ohos)
return true;
return .yes;

switch (target.cpu.arch) {
.ve => return false,
else => return target.os.tag == .linux or target.isAndroid() or target.isMusl(),
.ve => return .no,
else => return if (target.os.tag == .linux or target.isAndroid() or target.isMusl()) .yes else .no,
}
},

.windows => {
if (target.isMinGW())
return false;
return .no;

if (target.abi == .itanium)
return target.cpu.arch == .x86_64;
return if (target.cpu.arch == .x86_64) .yes else .no;

if (target.abi == .msvc or target.abi == .none) {
if (std.mem.eql(u8, d.use_linker.?, "bfd"))
return target.cpu.arch == .x86_64 // CrossWindows
else
return false; //MSVC
}
if (target.abi == .msvc or target.abi == .none)
return .depends_on_linker;

return false;
return .no;
},

else => {
switch (target.cpu.arch) {
.hexagon => {
// CLANG_DEFAULT_PIE_ON_LINUX
return target.os.tag == .linux or target.isAndroid() or target.isMusl();
return if (target.os.tag == .linux or target.isAndroid() or target.isMusl()) .yes else .no;
},

else => return false,
else => return .no,
}
},
};
}
fn isPICdefault(d: *const Driver) bool {
const target = d.comp.target;

fn isPICdefault(target: std.Target) DefaultPIStatus {
return switch (target.os.tag) {
.aix,
.haiku,
Expand All @@ -819,13 +817,13 @@ fn isPICdefault(d: *const Driver) bool {

.ps4,
.ps5,
=> true,
=> .yes,

.fuchsia,
.cuda,
.zos,
.shadermodel,
=> false,
=> .no,

.dragonfly,
.openbsd,
Expand All @@ -835,60 +833,81 @@ fn isPICdefault(d: *const Driver) bool {
.hurd,
=> {
return switch (target.cpu.arch) {
.mips64, .mips64el => true,
else => false,
.mips64, .mips64el => .yes,
else => .no,
};
},

.linux,
.elfiamcu,
=> {
if (target.abi == .ohos)
return false;
return .no;

return switch (target.cpu.arch) {
.mips64, .mips64el => true,
else => false,
.mips64, .mips64el => .yes,
else => .no,
};
},

.windows => {
if (target.isMinGW())
return (target.cpu.arch == .x86_64 or target.cpu.arch == .aarch64);
return if (target.cpu.arch == .x86_64 or target.cpu.arch == .aarch64) .yes else .no;

if (target.abi == .itanium)
return target.cpu.arch == .x86_64;
return if (target.cpu.arch == .x86_64) .yes else .no;

if (target.abi == .msvc or target.abi == .none) {
if (std.mem.eql(u8, d.use_linker.?, "bfd"))
return target.cpu.arch == .x86_64
else
return (target.cpu.arch == .x86_64 or target.cpu.arch == .aarch64);
}
if (target.abi == .msvc or target.abi == .none)
return .depends_on_linker;

if (target.ofmt == .macho)
return true;
return .yes;

return switch (target.cpu.arch) {
.x86_64, .mips64, .mips64el => true,
else => false,
.x86_64, .mips64, .mips64el => .yes,
else => .no,
};
},

else => {
if (target.ofmt == .macho)
return true;
return .yes;

return switch (target.cpu.arch) {
.mips64, .mips64el => true,
else => false,
.mips64, .mips64el => .yes,
else => .no,
};
},
};
}
/// This currently just returns the desired settings without considering target defaults / requirements
pub fn getPICMode(target: std.Target, desired_pic_level: ?backend.CodeGenOptions.PicLevel, wants_pie: ?bool) struct { backend.CodeGenOptions.PicLevel, bool } {
_ = target;
pub fn getPICMode(
target: std.Target,
use_linker: ?[]const u8,
desired_pic_level: ?backend.CodeGenOptions.PicLevel,
wants_pie: ?bool,
) struct { backend.CodeGenOptions.PicLevel, bool } {
const linker = use_linker orelse system_defaults.linker;
const is_pie = switch (isPIEDefault(target)) {
.yes => true,
.no => false,
.depends_on_linker => if (std.mem.eql(u8, linker, "bfd"))
target.cpu.arch == .x86_64 // CrossWindows
else
false, //MSVC
};
const is_pic = switch (isPICdefault(target)) {
.yes => true,
.no => false,
.depends_on_linker => if (std.mem.eql(u8, linker, "bfd"))
target.cpu.arch == .x86_64
else
(target.cpu.arch == .x86_64 or target.cpu.arch == .aarch64),
};

_ = is_pic;
_ = is_pie;

return .{ desired_pic_level orelse .none, wants_pie orelse false };
}

Expand Down

0 comments on commit 3959d5e

Please sign in to comment.