From 8d5d44fa64007c2fc696129fffefa9217732caaa Mon Sep 17 00:00:00 2001 From: Ali Chraghi Date: Wed, 5 Oct 2022 14:33:11 +0330 Subject: [PATCH] Compilation: strip debug info from ReleaseSmall by default --- build.zig | 6 +++--- lib/std/build.zig | 14 +++++++++----- src/Compilation.zig | 4 ++-- src/main.zig | 13 +++++++++---- test/cli.zig | 2 +- test/link/wasm/archive/build.zig | 1 + test/link/wasm/bss/build.zig | 1 + test/link/wasm/segments/build.zig | 1 + test/link/wasm/stack_pointer/build.zig | 1 + test/link/wasm/type/build.zig | 1 + 10 files changed, 29 insertions(+), 15 deletions(-) diff --git a/build.zig b/build.zig index fcdf569e487a..3c106bd3144a 100644 --- a/build.zig +++ b/build.zig @@ -309,15 +309,15 @@ pub fn build(b: *Builder) !void { .Debug => {}, .ReleaseFast => { zig1_obj.addArg("-OReleaseFast"); - zig1_obj.addArg("--strip"); + zig1_obj.addArg("-fstrip"); }, .ReleaseSafe => { zig1_obj.addArg("-OReleaseSafe"); - zig1_obj.addArg("--strip"); + zig1_obj.addArg("-fstrip"); }, .ReleaseSmall => { zig1_obj.addArg("-OReleaseSmall"); - zig1_obj.addArg("--strip"); + zig1_obj.addArg("-fstrip"); }, } if (single_threaded orelse false) { diff --git a/lib/std/build.zig b/lib/std/build.zig index ee9bc019d2c1..881fc13cd046 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -1468,7 +1468,7 @@ pub const LibExeObjStep = struct { kind: Kind, major_only_filename: ?[]const u8, name_only_filename: ?[]const u8, - strip: bool, + strip: ?bool, // keep in sync with src/link.zig:CompressDebugSections compress_debug_sections: enum { none, zlib } = .none, lib_paths: ArrayList([]const u8), @@ -1738,7 +1738,7 @@ pub const LibExeObjStep = struct { const self = builder.allocator.create(LibExeObjStep) catch unreachable; self.* = LibExeObjStep{ - .strip = false, + .strip = null, .builder = builder, .verbose_link = false, .verbose_cc = false, @@ -1953,7 +1953,7 @@ pub const LibExeObjStep = struct { pub fn producesPdbFile(self: *LibExeObjStep) bool { if (!self.target.isWindows() and !self.target.isUefi()) return false; - if (self.strip) return false; + if (self.strip != null and self.strip.?) return false; return self.isDynamicLibrary() or self.kind == .exe or self.kind == .test_exe; } @@ -2690,8 +2690,12 @@ pub const LibExeObjStep = struct { if (self.emit_h) try zig_args.append("-femit-h"); - if (self.strip) { - try zig_args.append("--strip"); + if (self.strip) |strip| { + if (strip) { + try zig_args.append("-fstrip"); + } else { + try zig_args.append("-fno-strip"); + } } switch (self.compress_debug_sections) { diff --git a/src/Compilation.zig b/src/Compilation.zig index 7c4c369a6ba1..0ff94818756d 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -916,8 +916,8 @@ pub const InitOptions = struct { use_clang: ?bool = null, use_stage1: ?bool = null, single_threaded: ?bool = null, + strip: ?bool = null, rdynamic: bool = false, - strip: bool = false, function_sections: bool = false, no_builtin: bool = false, is_native_os: bool, @@ -1422,7 +1422,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { break :blk buf.items[0 .. buf.items.len - 1 :0].ptr; } else null; - const strip = options.strip or !target_util.hasDebugInfo(options.target); + const strip = options.strip orelse !target_util.hasDebugInfo(options.target); const red_zone = options.want_red_zone orelse target_util.hasRedZone(options.target); const omit_frame_pointer = options.omit_frame_pointer orelse (options.optimize_mode != .Debug); const linker_optimization: u8 = options.linker_optimization orelse switch (options.optimize_mode) { diff --git a/src/main.zig b/src/main.zig index 2f005b57a4af..f98643bf0711 100644 --- a/src/main.zig +++ b/src/main.zig @@ -404,7 +404,8 @@ const usage_build_generic = \\ -fno-builtin Disable implicit builtin knowledge of functions \\ -ffunction-sections Places each function in a separate section \\ -fno-function-sections All functions go into same section - \\ --strip Omit debug symbols + \\ -fstrip Omit debug symbols + \\ -fno-strip Keep debug symbols \\ -ofmt=[mode] Override target object format \\ elf Executable and Linking Format \\ c C source code @@ -630,7 +631,7 @@ fn buildOutputType( var version: std.builtin.Version = .{ .major = 0, .minor = 0, .patch = 0 }; var have_version = false; var compatibility_version: ?std.builtin.Version = null; - var strip = false; + var strip: ?bool = null; var function_sections = false; var no_builtin = false; var watch = false; @@ -1296,8 +1297,10 @@ fn buildOutputType( } else if (mem.eql(u8, arg, "--show-builtin")) { show_builtin = true; emit_bin = .no; - } else if (mem.eql(u8, arg, "--strip")) { + } else if (mem.eql(u8, arg, "-fstrip")) { strip = true; + } else if (mem.eql(u8, arg, "-fno-strip")) { + strip = false; } else if (mem.eql(u8, arg, "-fsingle-threaded")) { single_threaded = true; } else if (mem.eql(u8, arg, "-fno-single-threaded")) { @@ -1432,7 +1435,6 @@ fn buildOutputType( .cc, .cpp => { emit_h = .no; soname = .no; - strip = false; ensure_libc_on_non_freestanding = true; ensure_libcpp_on_non_freestanding = arg_mode == .cpp; want_native_include_dirs = true; @@ -2186,6 +2188,9 @@ fn buildOutputType( }, } + if (arg_mode == .build and optimize_mode == .ReleaseSmall and strip == null) + strip = true; + if (arg_mode == .translate_c and c_source_files.items.len != 1) { fatal("translate-c expects exactly 1 source file (found {d})", .{c_source_files.items.len}); } diff --git a/test/cli.zig b/test/cli.zig index df1a1497e122..57f26f73d78b 100644 --- a/test/cli.zig +++ b/test/cli.zig @@ -133,7 +133,7 @@ fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void { "--cache-dir", dir_path, "--name", "example", "-fno-emit-bin", "-fno-emit-h", - "--strip", "-OReleaseFast", + "-fstrip", "-OReleaseFast", example_zig_path, }); diff --git a/test/link/wasm/archive/build.zig b/test/link/wasm/archive/build.zig index 95ce44465975..0ee67102ac90 100644 --- a/test/link/wasm/archive/build.zig +++ b/test/link/wasm/archive/build.zig @@ -15,6 +15,7 @@ pub fn build(b: *Builder) void { lib.use_llvm = false; lib.use_stage1 = false; lib.use_lld = false; + lib.strip = false; const check = lib.checkObject(.wasm); check.checkStart("Section import"); diff --git a/test/link/wasm/bss/build.zig b/test/link/wasm/bss/build.zig index f813bd379f5d..0a8d652338c2 100644 --- a/test/link/wasm/bss/build.zig +++ b/test/link/wasm/bss/build.zig @@ -13,6 +13,7 @@ pub fn build(b: *Builder) void { lib.use_llvm = false; lib.use_stage1 = false; lib.use_lld = false; + lib.strip = false; // to make sure the bss segment is emitted, we must import memory lib.import_memory = true; lib.install(); diff --git a/test/link/wasm/segments/build.zig b/test/link/wasm/segments/build.zig index 7b3b08d860b3..06ca55300fbb 100644 --- a/test/link/wasm/segments/build.zig +++ b/test/link/wasm/segments/build.zig @@ -13,6 +13,7 @@ pub fn build(b: *Builder) void { lib.use_llvm = false; lib.use_stage1 = false; lib.use_lld = false; + lib.strip = false; lib.install(); const check_lib = lib.checkObject(.wasm); diff --git a/test/link/wasm/stack_pointer/build.zig b/test/link/wasm/stack_pointer/build.zig index 761ce423eca4..1f50a60d1989 100644 --- a/test/link/wasm/stack_pointer/build.zig +++ b/test/link/wasm/stack_pointer/build.zig @@ -13,6 +13,7 @@ pub fn build(b: *Builder) void { lib.use_llvm = false; lib.use_stage1 = false; lib.use_lld = false; + lib.strip = false; lib.stack_size = std.wasm.page_size * 2; // set an explicit stack size lib.install(); diff --git a/test/link/wasm/type/build.zig b/test/link/wasm/type/build.zig index f39448c7aa58..2c926eced3e5 100644 --- a/test/link/wasm/type/build.zig +++ b/test/link/wasm/type/build.zig @@ -13,6 +13,7 @@ pub fn build(b: *Builder) void { lib.use_llvm = false; lib.use_stage1 = false; lib.use_lld = false; + lib.strip = false; lib.install(); const check_lib = lib.checkObject(.wasm);