authorgravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2023-10-18 15:54:46-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-20 01:59:55-04:00
logaa76ca2931d7adbc8ee3b33cdaa2cb1de1adda8a
tree7d9d01dceab74371da65f0eee94c84f216866c79
parent5d8bc56ab67c01f9c1b6de6227356c1c74852464

llvm: set PIE only for executables

closes #17575

3 files changed, 22 insertions(+), 11 deletions(-)

src/Compilation.zig+19-8
......@@ -1037,13 +1037,24 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
10371037
10381038 const sysroot = options.sysroot orelse libc_dirs.sysroot;
10391039
1040 const must_pie = target_util.requiresPIE(options.target);
1041 const pie: bool = if (options.want_pie) |explicit| pie: {
1042 if (!explicit and must_pie) {
1043 return error.TargetRequiresPIE;
1040 const pie: bool = pie: {
1041 if (options.output_mode != .Exe) {
1042 if (options.want_pie == true) return error.OutputModeForbidsPie;
1043 break :pie false;
10441044 }
1045 break :pie explicit;
1046 } else must_pie or tsan;
1045 if (target_util.requiresPIE(options.target)) {
1046 if (options.want_pie == false) return error.TargetRequiresPie;
1047 break :pie true;
1048 }
1049 if (tsan) {
1050 if (options.want_pie == false) return error.TsanRequiresPie;
1051 break :pie true;
1052 }
1053 if (options.want_pie) |want_pie| {
1054 break :pie want_pie;
1055 }
1056 break :pie false;
1057 };
10471058
10481059 const must_pic: bool = b: {
10491060 if (target_util.requiresPIC(options.target, link_libc))
......@@ -6533,7 +6544,7 @@ fn buildOutputFromZig(
65336544 .want_tsan = false,
65346545 .want_unwind_tables = comp.bin_file.options.eh_frame_hdr,
65356546 .want_pic = comp.bin_file.options.pic,
6536 .want_pie = comp.bin_file.options.pie,
6547 .want_pie = null,
65376548 .emit_h = null,
65386549 .strip = comp.compilerRtStrip(),
65396550 .is_native_os = comp.bin_file.options.is_native_os,
......@@ -6608,7 +6619,7 @@ pub fn build_crt_file(
66086619 .want_valgrind = false,
66096620 .want_tsan = false,
66106621 .want_pic = comp.bin_file.options.pic,
6611 .want_pie = comp.bin_file.options.pie,
6622 .want_pie = null,
66126623 .want_lto = switch (output_mode) {
66136624 .Lib => comp.bin_file.options.lto,
66146625 .Obj, .Exe => false,
src/libcxx.zig+2-2
......@@ -248,7 +248,7 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: *std.Progress.Node) !void {
248248 .want_valgrind = false,
249249 .want_tsan = comp.bin_file.options.tsan,
250250 .want_pic = comp.bin_file.options.pic,
251 .want_pie = comp.bin_file.options.pie,
251 .want_pie = null,
252252 .want_lto = comp.bin_file.options.lto,
253253 .function_sections = comp.bin_file.options.function_sections,
254254 .emit_h = null,
......@@ -411,7 +411,7 @@ pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void {
411411 .want_valgrind = false,
412412 .want_tsan = comp.bin_file.options.tsan,
413413 .want_pic = comp.bin_file.options.pic,
414 .want_pie = comp.bin_file.options.pie,
414 .want_pie = null,
415415 .want_lto = comp.bin_file.options.lto,
416416 .function_sections = comp.bin_file.options.function_sections,
417417 .emit_h = null,
src/libunwind.zig+1-1
......@@ -104,7 +104,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: *std.Progress.Node) !void {
104104 .want_valgrind = false,
105105 .want_tsan = false,
106106 .want_pic = comp.bin_file.options.pic,
107 .want_pie = comp.bin_file.options.pie,
107 .want_pie = null,
108108 // Disable LTO to avoid https://github.com/llvm/llvm-project/issues/56825
109109 .want_lto = false,
110110 .function_sections = comp.bin_file.options.function_sections,