Commit aa76ca2931

Michael Dusan <michael.dusan@gmail.com>
2023-10-18 21:54:46
llvm: set PIE only for executables
closes #17575
1 parent 5d8bc56
src/Compilation.zig
@@ -1037,13 +1037,24 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
 
         const sysroot = options.sysroot orelse libc_dirs.sysroot;
 
-        const must_pie = target_util.requiresPIE(options.target);
-        const pie: bool = if (options.want_pie) |explicit| pie: {
-            if (!explicit and must_pie) {
-                return error.TargetRequiresPIE;
+        const pie: bool = pie: {
+            if (options.output_mode != .Exe) {
+                if (options.want_pie == true) return error.OutputModeForbidsPie;
+                break :pie false;
             }
-            break :pie explicit;
-        } else must_pie or tsan;
+            if (target_util.requiresPIE(options.target)) {
+                if (options.want_pie == false) return error.TargetRequiresPie;
+                break :pie true;
+            }
+            if (tsan) {
+                if (options.want_pie == false) return error.TsanRequiresPie;
+                break :pie true;
+            }
+            if (options.want_pie) |want_pie| {
+                break :pie want_pie;
+            }
+            break :pie false;
+        };
 
         const must_pic: bool = b: {
             if (target_util.requiresPIC(options.target, link_libc))
@@ -6533,7 +6544,7 @@ fn buildOutputFromZig(
         .want_tsan = false,
         .want_unwind_tables = comp.bin_file.options.eh_frame_hdr,
         .want_pic = comp.bin_file.options.pic,
-        .want_pie = comp.bin_file.options.pie,
+        .want_pie = null,
         .emit_h = null,
         .strip = comp.compilerRtStrip(),
         .is_native_os = comp.bin_file.options.is_native_os,
@@ -6608,7 +6619,7 @@ pub fn build_crt_file(
         .want_valgrind = false,
         .want_tsan = false,
         .want_pic = comp.bin_file.options.pic,
-        .want_pie = comp.bin_file.options.pie,
+        .want_pie = null,
         .want_lto = switch (output_mode) {
             .Lib => comp.bin_file.options.lto,
             .Obj, .Exe => false,
src/libcxx.zig
@@ -248,7 +248,7 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: *std.Progress.Node) !void {
         .want_valgrind = false,
         .want_tsan = comp.bin_file.options.tsan,
         .want_pic = comp.bin_file.options.pic,
-        .want_pie = comp.bin_file.options.pie,
+        .want_pie = null,
         .want_lto = comp.bin_file.options.lto,
         .function_sections = comp.bin_file.options.function_sections,
         .emit_h = null,
@@ -411,7 +411,7 @@ pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void {
         .want_valgrind = false,
         .want_tsan = comp.bin_file.options.tsan,
         .want_pic = comp.bin_file.options.pic,
-        .want_pie = comp.bin_file.options.pie,
+        .want_pie = null,
         .want_lto = comp.bin_file.options.lto,
         .function_sections = comp.bin_file.options.function_sections,
         .emit_h = null,
src/libunwind.zig
@@ -104,7 +104,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: *std.Progress.Node) !void {
         .want_valgrind = false,
         .want_tsan = false,
         .want_pic = comp.bin_file.options.pic,
-        .want_pie = comp.bin_file.options.pie,
+        .want_pie = null,
         // Disable LTO to avoid https://github.com/llvm/llvm-project/issues/56825
         .want_lto = false,
         .function_sections = comp.bin_file.options.function_sections,