From 113f80bcf793b2841635d136e561bcc0bbe89086 Mon Sep 17 00:00:00 2001 From: kcbanner Date: Thu, 2 Mar 2023 22:15:31 -0500 Subject: [PATCH 1/4] coff: change dynamicbase to default to true (to match lld), change it to pass the negation to lld, and add --no-dynamicbase build: expose linker_dynamicbase on CompileStep and map it to emit --no-dynamicbase --- lib/std/Build/CompileStep.zig | 5 +++++ src/link/Coff/lld.zig | 4 ++-- src/main.zig | 8 +++++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/std/Build/CompileStep.zig b/lib/std/Build/CompileStep.zig index 2747f56af2..96fd9deb9c 100644 --- a/lib/std/Build/CompileStep.zig +++ b/lib/std/Build/CompileStep.zig @@ -140,6 +140,8 @@ link_function_sections: bool = false, /// exported symbols. link_gc_sections: ?bool = null, +linker_dynamicbase: ?bool = null, + linker_allow_shlib_undefined: ?bool = null, /// Permit read-only relocations in read-only segments. Disallowed by default. @@ -1474,6 +1476,9 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { if (self.link_gc_sections) |x| { try zig_args.append(if (x) "--gc-sections" else "--no-gc-sections"); } + if (self.linker_dynamicbase) |x| { + if (!x) try zig_args.append("--no-dynamicbase"); + } if (self.linker_allow_shlib_undefined) |x| { try zig_args.append(if (x) "-fallow-shlib-undefined" else "-fno-allow-shlib-undefined"); } diff --git a/src/link/Coff/lld.zig b/src/link/Coff/lld.zig index c308ff5989..358c905b2c 100644 --- a/src/link/Coff/lld.zig +++ b/src/link/Coff/lld.zig @@ -223,8 +223,8 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod if (self.base.options.nxcompat) { try argv.append("-nxcompat"); } - if (self.base.options.dynamicbase) { - try argv.append("-dynamicbase"); + if (!self.base.options.dynamicbase) { + try argv.append("-dynamicbase:NO"); } try argv.append(try allocPrint(arena, "-OUT:{s}", .{full_out_path})); diff --git a/src/main.zig b/src/main.zig index 961d649d38..479c7e518c 100644 --- a/src/main.zig +++ b/src/main.zig @@ -778,7 +778,7 @@ fn buildOutputType( var linker_z_max_page_size: ?u64 = null; var linker_tsaware = false; var linker_nxcompat = false; - var linker_dynamicbase = false; + var linker_dynamicbase = true; var linker_optimization: ?u8 = null; var linker_module_definition_file: ?[]const u8 = null; var test_evented_io = false; @@ -1371,6 +1371,8 @@ fn buildOutputType( linker_opt_bisect_limit = std.math.lossyCast(i32, parseIntSuffix(arg, "-fopt-bisect-limit=".len)); } else if (mem.eql(u8, arg, "--eh-frame-hdr")) { link_eh_frame_hdr = true; + } else if (mem.eql(u8, arg, "--no-dynamicbase")) { + linker_dynamicbase = false; } else if (mem.eql(u8, arg, "--emit-relocs")) { link_emit_relocs = true; } else if (mem.eql(u8, arg, "-fallow-shlib-undefined")) { @@ -2105,8 +2107,8 @@ fn buildOutputType( linker_tsaware = true; } else if (mem.eql(u8, arg, "--nxcompat")) { linker_nxcompat = true; - } else if (mem.eql(u8, arg, "--dynamicbase")) { - linker_dynamicbase = true; + } else if (mem.eql(u8, arg, "--no-dynamicbase")) { + linker_dynamicbase = false; } else if (mem.eql(u8, arg, "--high-entropy-va")) { // This option does not do anything. } else if (mem.eql(u8, arg, "--export-all-symbols")) { From c79073c176a909abce3feba96203caef1e6d8360 Mon Sep 17 00:00:00 2001 From: kcbanner Date: Thu, 2 Mar 2023 22:54:35 -0500 Subject: [PATCH 2/4] compilation: fixup linker_dynamicbase default in InitOptions --- src/Compilation.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Compilation.zig b/src/Compilation.zig index 858afb6ca3..2c9b4e4063 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -575,7 +575,7 @@ pub const InitOptions = struct { linker_z_max_page_size: ?u64 = null, linker_tsaware: bool = false, linker_nxcompat: bool = false, - linker_dynamicbase: bool = false, + linker_dynamicbase: bool = true, linker_optimization: ?u8 = null, linker_compress_debug_sections: ?link.CompressDebugSections = null, linker_module_definition_file: ?[]const u8 = null, From 4569a28ea35e7152f01db1d36c493506b6ca71b6 Mon Sep 17 00:00:00 2001 From: kcbanner Date: Sat, 25 Mar 2023 16:33:25 -0400 Subject: [PATCH 3/4] build: fixes from review --- lib/std/Build/CompileStep.zig | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/std/Build/CompileStep.zig b/lib/std/Build/CompileStep.zig index 96fd9deb9c..b438331991 100644 --- a/lib/std/Build/CompileStep.zig +++ b/lib/std/Build/CompileStep.zig @@ -140,7 +140,8 @@ link_function_sections: bool = false, /// exported symbols. link_gc_sections: ?bool = null, -linker_dynamicbase: ?bool = null, +/// (Windows) Whether or not to enable ASLR. Maps to the /DYNAMICBASE[:NO] linker argument. +linker_dynamicbase: bool = true, linker_allow_shlib_undefined: ?bool = null, @@ -1476,8 +1477,8 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { if (self.link_gc_sections) |x| { try zig_args.append(if (x) "--gc-sections" else "--no-gc-sections"); } - if (self.linker_dynamicbase) |x| { - if (!x) try zig_args.append("--no-dynamicbase"); + if (!self.linker_dynamicbase) { + try zig_args.append("--no-dynamicbase"); } if (self.linker_allow_shlib_undefined) |x| { try zig_args.append(if (x) "-fallow-shlib-undefined" else "-fno-allow-shlib-undefined"); From 43ff6c14f96187ec93bbc23432cdbb885f5ab8f1 Mon Sep 17 00:00:00 2001 From: kcbanner Date: Sun, 26 Mar 2023 13:10:36 -0400 Subject: [PATCH 4/4] main: recognize --dynamicbase --- src/main.zig | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main.zig b/src/main.zig index 479c7e518c..c96fd25766 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1371,6 +1371,8 @@ fn buildOutputType( linker_opt_bisect_limit = std.math.lossyCast(i32, parseIntSuffix(arg, "-fopt-bisect-limit=".len)); } else if (mem.eql(u8, arg, "--eh-frame-hdr")) { link_eh_frame_hdr = true; + } else if (mem.eql(u8, arg, "--dynamicbase")) { + linker_dynamicbase = true; } else if (mem.eql(u8, arg, "--no-dynamicbase")) { linker_dynamicbase = false; } else if (mem.eql(u8, arg, "--emit-relocs")) {