authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-08-16 14:59:34+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-10-07 09:50:37+02:00
log399eaa3d410da35de6f5d1869932a7c0e8ca46b2
treeef86d479fe1207e67ac6109fa1a60855d8d7d97b
parentc19cd5b8920ae242e582bbb1f843c9c255318ea8
signature Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

compiler: default to PIC for objects and static libraries

This makes it so that objects and static libraries produced by Zig can be linked into PIEs and shared libraries by default. This matches how almost all C/C++ toolchains are configured in Linux/BSD distros today. For cases where the user knows better and wants to squeeze out some (usually very minor) extra performance, they can just explicitly disable PIC when producing the object or static library.

1 files changed, 3 insertions(+), 4 deletions(-)

src/Module.zig+3-4
......@@ -195,10 +195,9 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Module {
195195 if (options.inherited.pic) |x| break :b x;
196196 if (options.parent) |p| break :b p.pic;
197197
198 // Default to PIC on targets where we default to producing PIEs to make
199 // the common case of linking objects and static libraries into an
200 // executable work out of the box.
201 break :b target_util.defaultPie(target);
198 // Default to PIC for objects and static libraries to make the common
199 // cases of linking those into a PIE or a shared library just work.
200 break :b options.global.output_mode != .Exe;
202201 };
203202
204203 const red_zone = b: {