authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-03-26 05:41:15+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-03-26 05:50:51+01:00
log4219037faf573da8094ae8083d5ae01da6bdff6e
tree1620c5c6b4c5f5dbd529a362e36e33983ec0edcb
parent558a5c7913434547c55355af0075a9d34db0d4bc
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

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.

3 files changed, 15 insertions(+), 9 deletions(-)

src/Compilation/Config.zig+1-6
......@@ -472,12 +472,7 @@ pub fn resolve(options: Options) ResolveError!Config {
472472 break :b true;
473473 }
474474 if (options.pie) |pie| break :b pie;
475 break :b if (options.output_mode == .Exe) switch (target.os.tag) {
476 .fuchsia,
477 .openbsd,
478 => true,
479 else => target.os.tag.isDarwin(),
480 } else false;
475 break :b if (options.output_mode == .Exe) target_util.defaultPie(target) else false;
481476 };
482477
483478 const lto: std.zig.LtoMode = b: {
src/Package/Module.zig+6-2
......@@ -168,7 +168,7 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
168168 };
169169
170170 const pic = b: {
171 if (target_util.requiresPIC(target, options.global.link_libc)) {
171 if (target_util.requiresPic(target, options.global.link_libc)) {
172172 if (options.inherited.pic == false)
173173 return error.TargetRequiresPic;
174174 break :b true;
......@@ -185,7 +185,11 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
185185 }
186186 if (options.inherited.pic) |x| break :b x;
187187 if (options.parent) |p| break :b p.pic;
188 break :b false;
188
189 // Default to PIC on targets where we default to producing PIEs to make
190 // the common case of linking objects and static libraries into an
191 // executable work out of the box.
192 break :b target_util.defaultPie(target);
189193 };
190194
191195 const red_zone = b: {
src/target.zig+8-1
......@@ -53,7 +53,7 @@ pub fn libCxxNeedsLibUnwind(target: *const std.Target) bool {
5353}
5454
5555/// This function returns whether non-pic code is completely invalid on the given target.
56pub fn requiresPIC(target: *const std.Target, linking_libc: bool) bool {
56pub fn requiresPic(target: *const std.Target, linking_libc: bool) bool {
5757 return target.abi.isAndroid() or
5858 ((target.os.tag == .windows or target.os.tag == .uefi) and (target.cpu.arch == .aarch64 or target.cpu.arch == .x86_64)) or
5959 target.requiresLibC() or
......@@ -84,6 +84,13 @@ pub fn supports_fpic(target: *const std.Target) bool {
8484 };
8585}
8686
87pub fn defaultPie(target: *const std.Target) bool {
88 return switch (target.os.tag) {
89 .openbsd => true,
90 else => target.os.tag.isDarwin(),
91 };
92}
93
8794pub fn alwaysSingleThreaded(target: *const std.Target) bool {
8895 _ = target;
8996 return false;