Skip to content

Commit a81fb5f

Browse files
committed
compiler: Rework PIE option logic.
To my knowledge, the only platforms that actually *require* PIE are Fuchsia and Android, and the latter *only* when building a dynamically-linked executable. OpenBSD and macOS both strongly encourage using PIE by default, but it isn't technically required. So for the latter platforms, we enable it by default but don't enforce it. Also, importantly, if we're building an object file or a static library, and the user hasn't explicitly told us whether to build PIE or non-PIE code (and the target doesn't require PIE), we should *not* default to PIE. Doing so produces code that cannot be linked into non-PIE output. In other words, building an object file or a static library as PIE is an optimization only to be done when the user knows that it'll end up in a PIE executable in the end. Closes #21837.
1 parent 826e1c3 commit a81fb5f

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

src/Compilation/Config.zig

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -416,22 +416,29 @@ pub fn resolve(options: Options) ResolveError!Config {
416416

417417
const pie: bool = b: {
418418
switch (options.output_mode) {
419-
.Obj, .Exe => {},
419+
.Exe => if (target.os.tag == .fuchsia or
420+
(target.abi.isAndroid() and link_mode == .dynamic))
421+
{
422+
if (options.pie == false) return error.TargetRequiresPie;
423+
break :b true;
424+
},
420425
.Lib => if (link_mode == .dynamic) {
421426
if (options.pie == true) return error.DynamicLibraryPrecludesPie;
422427
break :b false;
423428
},
424-
}
425-
if (target_util.requiresPIE(target)) {
426-
if (options.pie == false) return error.TargetRequiresPie;
427-
break :b true;
429+
.Obj => {},
428430
}
429431
if (options.any_sanitize_thread) {
430432
if (options.pie == false) return error.SanitizeThreadRequiresPie;
431433
break :b true;
432434
}
433435
if (options.pie) |pie| break :b pie;
434-
break :b false;
436+
break :b if (options.output_mode == .Exe) switch (target.os.tag) {
437+
.fuchsia,
438+
.openbsd,
439+
=> true,
440+
else => target.os.tag.isDarwin(),
441+
} else false;
435442
};
436443

437444
const root_strip = b: {

src/target.zig

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ pub fn libCxxNeedsLibUnwind(target: std.Target) bool {
4343
};
4444
}
4545

46-
pub fn requiresPIE(target: std.Target) bool {
47-
return target.abi.isAndroid() or target.os.tag.isDarwin() or target.os.tag == .openbsd;
48-
}
49-
5046
/// This function returns whether non-pic code is completely invalid on the given target.
5147
pub fn requiresPIC(target: std.Target, linking_libc: bool) bool {
5248
return target.abi.isAndroid() or

0 commit comments

Comments
 (0)