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 {...@@ -1037,13 +1037,24 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
10371037
1038 const sysroot = options.sysroot orelse libc_dirs.sysroot;1038 const sysroot = options.sysroot orelse libc_dirs.sysroot;
10391039
1040 const must_pie = target_util.requiresPIE(options.target);1040 const pie: bool = pie: {
1041 const pie: bool = if (options.want_pie) |explicit| pie: {1041 if (options.output_mode != .Exe) {
1042 if (!explicit and must_pie) {1042 if (options.want_pie == true) return error.OutputModeForbidsPie;
1043 return error.TargetRequiresPIE;1043 break :pie false;
1044 }1044 }
1045 break :pie explicit;1045 if (target_util.requiresPIE(options.target)) {
1046 } else must_pie or tsan;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
1048 const must_pic: bool = b: {1059 const must_pic: bool = b: {
1049 if (target_util.requiresPIC(options.target, link_libc))1060 if (target_util.requiresPIC(options.target, link_libc))
...@@ -6533,7 +6544,7 @@ fn buildOutputFromZig(...@@ -6533,7 +6544,7 @@ fn buildOutputFromZig(
6533 .want_tsan = false,6544 .want_tsan = false,
6534 .want_unwind_tables = comp.bin_file.options.eh_frame_hdr,6545 .want_unwind_tables = comp.bin_file.options.eh_frame_hdr,
6535 .want_pic = comp.bin_file.options.pic,6546 .want_pic = comp.bin_file.options.pic,
6536 .want_pie = comp.bin_file.options.pie,6547 .want_pie = null,
6537 .emit_h = null,6548 .emit_h = null,
6538 .strip = comp.compilerRtStrip(),6549 .strip = comp.compilerRtStrip(),
6539 .is_native_os = comp.bin_file.options.is_native_os,6550 .is_native_os = comp.bin_file.options.is_native_os,
...@@ -6608,7 +6619,7 @@ pub fn build_crt_file(...@@ -6608,7 +6619,7 @@ pub fn build_crt_file(
6608 .want_valgrind = false,6619 .want_valgrind = false,
6609 .want_tsan = false,6620 .want_tsan = false,
6610 .want_pic = comp.bin_file.options.pic,6621 .want_pic = comp.bin_file.options.pic,
6611 .want_pie = comp.bin_file.options.pie,6622 .want_pie = null,
6612 .want_lto = switch (output_mode) {6623 .want_lto = switch (output_mode) {
6613 .Lib => comp.bin_file.options.lto,6624 .Lib => comp.bin_file.options.lto,
6614 .Obj, .Exe => false,6625 .Obj, .Exe => false,
src/libcxx.zig+2-2
...@@ -248,7 +248,7 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: *std.Progress.Node) !void {...@@ -248,7 +248,7 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: *std.Progress.Node) !void {
248 .want_valgrind = false,248 .want_valgrind = false,
249 .want_tsan = comp.bin_file.options.tsan,249 .want_tsan = comp.bin_file.options.tsan,
250 .want_pic = comp.bin_file.options.pic,250 .want_pic = comp.bin_file.options.pic,
251 .want_pie = comp.bin_file.options.pie,251 .want_pie = null,
252 .want_lto = comp.bin_file.options.lto,252 .want_lto = comp.bin_file.options.lto,
253 .function_sections = comp.bin_file.options.function_sections,253 .function_sections = comp.bin_file.options.function_sections,
254 .emit_h = null,254 .emit_h = null,
...@@ -411,7 +411,7 @@ pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void {...@@ -411,7 +411,7 @@ pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void {
411 .want_valgrind = false,411 .want_valgrind = false,
412 .want_tsan = comp.bin_file.options.tsan,412 .want_tsan = comp.bin_file.options.tsan,
413 .want_pic = comp.bin_file.options.pic,413 .want_pic = comp.bin_file.options.pic,
414 .want_pie = comp.bin_file.options.pie,414 .want_pie = null,
415 .want_lto = comp.bin_file.options.lto,415 .want_lto = comp.bin_file.options.lto,
416 .function_sections = comp.bin_file.options.function_sections,416 .function_sections = comp.bin_file.options.function_sections,
417 .emit_h = null,417 .emit_h = null,
src/libunwind.zig+1-1
...@@ -104,7 +104,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: *std.Progress.Node) !void {...@@ -104,7 +104,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: *std.Progress.Node) !void {
104 .want_valgrind = false,104 .want_valgrind = false,
105 .want_tsan = false,105 .want_tsan = false,
106 .want_pic = comp.bin_file.options.pic,106 .want_pic = comp.bin_file.options.pic,
107 .want_pie = comp.bin_file.options.pie,107 .want_pie = null,
108 // Disable LTO to avoid https://github.com/llvm/llvm-project/issues/56825108 // Disable LTO to avoid https://github.com/llvm/llvm-project/issues/56825
109 .want_lto = false,109 .want_lto = false,
110 .function_sections = comp.bin_file.options.function_sections,110 .function_sections = comp.bin_file.options.function_sections,