From 4219037faf573da8094ae8083d5ae01da6bdff6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Thu, 26 Mar 2026 05:41:15 +0100 Subject: [PATCH] compiler: default to PIC if the target defaults to PIE Otherwise, by default and with no special flags, we produce objects and static libraries that cannot be linked into executables. --- src/Compilation/Config.zig | 7 +------ src/Package/Module.zig | 8 ++++++-- src/target.zig | 9 ++++++++- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/Compilation/Config.zig b/src/Compilation/Config.zig index e4f85b7a48094d82911c1831ca00e30b5f75c85d..7f966725371adc0873fe4e0744c34aaf958bad7b 100644 --- a/src/Compilation/Config.zig +++ b/src/Compilation/Config.zig @@ -472,12 +472,7 @@ pub fn resolve(options: Options) ResolveError!Config { break :b true; } if (options.pie) |pie| break :b pie; - break :b if (options.output_mode == .Exe) switch (target.os.tag) { - .fuchsia, - .openbsd, - => true, - else => target.os.tag.isDarwin(), - } else false; + break :b if (options.output_mode == .Exe) target_util.defaultPie(target) else false; }; const lto: std.zig.LtoMode = b: { diff --git a/src/Package/Module.zig b/src/Package/Module.zig index a922af2da5c26e2c2f2001889c1212d3af57893d..d6ec7f8aafb232d7e619944c4891829d154e55df 100644 --- a/src/Package/Module.zig +++ b/src/Package/Module.zig @@ -168,7 +168,7 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module { }; const pic = b: { - if (target_util.requiresPIC(target, options.global.link_libc)) { + if (target_util.requiresPic(target, options.global.link_libc)) { if (options.inherited.pic == false) return error.TargetRequiresPic; break :b true; @@ -185,7 +185,11 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module { } if (options.inherited.pic) |x| break :b x; if (options.parent) |p| break :b p.pic; - break :b false; + + // Default to PIC on targets where we default to producing PIEs to make + // the common case of linking objects and static libraries into an + // executable work out of the box. + break :b target_util.defaultPie(target); }; const red_zone = b: { diff --git a/src/target.zig b/src/target.zig index ca65239a0950bb4d52508655b9040d1cf2b62872..e5df8c86a7a0a409011cac1d7303eb4f8d54e794 100644 --- a/src/target.zig +++ b/src/target.zig @@ -53,7 +53,7 @@ pub fn libCxxNeedsLibUnwind(target: *const std.Target) bool { } /// This function returns whether non-pic code is completely invalid on the given target. -pub fn requiresPIC(target: *const std.Target, linking_libc: bool) bool { +pub fn requiresPic(target: *const std.Target, linking_libc: bool) bool { return target.abi.isAndroid() or ((target.os.tag == .windows or target.os.tag == .uefi) and (target.cpu.arch == .aarch64 or target.cpu.arch == .x86_64)) or target.requiresLibC() or @@ -84,6 +84,13 @@ pub fn supports_fpic(target: *const std.Target) bool { }; } +pub fn defaultPie(target: *const std.Target) bool { + return switch (target.os.tag) { + .openbsd => true, + else => target.os.tag.isDarwin(), + }; +} + pub fn alwaysSingleThreaded(target: *const std.Target) bool { _ = target; return false; -- 2.54.0