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 {...@@ -472,12 +472,7 @@ pub fn resolve(options: Options) ResolveError!Config {
472 break :b true;472 break :b true;
473 }473 }
474 if (options.pie) |pie| break :b pie;474 if (options.pie) |pie| break :b pie;
475 break :b if (options.output_mode == .Exe) switch (target.os.tag) {475 break :b if (options.output_mode == .Exe) target_util.defaultPie(target) else false;
476 .fuchsia,
477 .openbsd,
478 => true,
479 else => target.os.tag.isDarwin(),
480 } else false;
481 };476 };
482477
483 const lto: std.zig.LtoMode = b: {478 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 {...@@ -168,7 +168,7 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
168 };168 };
169169
170 const pic = b: {170 const pic = b: {
171 if (target_util.requiresPIC(target, options.global.link_libc)) {171 if (target_util.requiresPic(target, options.global.link_libc)) {
172 if (options.inherited.pic == false)172 if (options.inherited.pic == false)
173 return error.TargetRequiresPic;173 return error.TargetRequiresPic;
174 break :b true;174 break :b true;
...@@ -185,7 +185,11 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {...@@ -185,7 +185,11 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
185 }185 }
186 if (options.inherited.pic) |x| break :b x;186 if (options.inherited.pic) |x| break :b x;
187 if (options.parent) |p| break :b p.pic;187 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);
189 };193 };
190194
191 const red_zone = b: {195 const red_zone = b: {
src/target.zig+8-1
...@@ -53,7 +53,7 @@ pub fn libCxxNeedsLibUnwind(target: *const std.Target) bool {...@@ -53,7 +53,7 @@ pub fn libCxxNeedsLibUnwind(target: *const std.Target) bool {
53}53}
5454
55/// This function returns whether non-pic code is completely invalid on the given target.55/// 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 {
57 return target.abi.isAndroid() or57 return target.abi.isAndroid() or
58 ((target.os.tag == .windows or target.os.tag == .uefi) and (target.cpu.arch == .aarch64 or target.cpu.arch == .x86_64)) or58 ((target.os.tag == .windows or target.os.tag == .uefi) and (target.cpu.arch == .aarch64 or target.cpu.arch == .x86_64)) or
59 target.requiresLibC() or59 target.requiresLibC() or
...@@ -84,6 +84,13 @@ pub fn supports_fpic(target: *const std.Target) bool {...@@ -84,6 +84,13 @@ pub fn supports_fpic(target: *const std.Target) bool {
84 };84 };
85}85}
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
87pub fn alwaysSingleThreaded(target: *const std.Target) bool {94pub fn alwaysSingleThreaded(target: *const std.Target) bool {
88 _ = target;95 _ = target;
89 return false;96 return false;