diff --git a/src/Compilation.zig b/src/Compilation.zig index b00f135813..8abe2770ec 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -860,6 +860,7 @@ pub const InitOptions = struct { linker_nxcompat: bool = false, linker_dynamicbase: bool = false, linker_optimization: ?u8 = null, + linker_compress_debug_sections: ?link.CompressDebugSections = null, major_subsystem_version: ?u32 = null, minor_subsystem_version: ?u32 = null, clang_passthrough_mode: bool = false, @@ -1687,6 +1688,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { .no_builtin = options.no_builtin, .allow_shlib_undefined = options.linker_allow_shlib_undefined, .bind_global_refs_locally = options.linker_bind_global_refs_locally orelse false, + .compress_debug_sections = options.linker_compress_debug_sections, .import_memory = options.linker_import_memory orelse false, .import_table = options.linker_import_table, .export_table = options.linker_export_table, @@ -2459,6 +2461,7 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes man.hash.add(comp.bin_file.options.z_now); man.hash.add(comp.bin_file.options.z_relro); man.hash.add(comp.bin_file.options.hash_style); + man.hash.addOptional(comp.bin_file.options.compress_debug_sections); man.hash.add(comp.bin_file.options.include_compiler_rt); if (comp.bin_file.options.link_libc) { man.hash.add(comp.bin_file.options.libc_installation != null); diff --git a/src/link.zig b/src/link.zig index 871ceded6c..7975684263 100644 --- a/src/link.zig +++ b/src/link.zig @@ -123,6 +123,7 @@ pub const Options = struct { nxcompat: bool, dynamicbase: bool, linker_optimization: u8, + compress_debug_sections: ?CompressDebugSections, bind_global_refs_locally: bool, import_memory: bool, import_table: bool, @@ -219,6 +220,8 @@ pub const Options = struct { pub const HashStyle = enum { sysv, gnu, both }; +pub const CompressDebugSections = enum { none, zlib }; + pub const File = struct { tag: Tag, options: Options, diff --git a/src/link/Elf.zig b/src/link/Elf.zig index f7d582bae7..bab7fb44cc 100644 --- a/src/link/Elf.zig +++ b/src/link/Elf.zig @@ -1351,6 +1351,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v link.hashAddSystemLibs(&man.hash, self.base.options.system_libs); man.hash.add(allow_shlib_undefined); man.hash.add(self.base.options.bind_global_refs_locally); + man.hash.addOptional(self.base.options.compress_debug_sections); man.hash.add(self.base.options.tsan); man.hash.addOptionalBytes(self.base.options.sysroot); man.hash.add(self.base.options.linker_optimization); @@ -1754,6 +1755,11 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v try argv.append("--allow-shlib-undefined"); } + if (self.base.options.compress_debug_sections) |how| { + const arg = try std.fmt.allocPrint(arena, "--compress-debug-sections={s}", .{@tagName(how)}); + try argv.append(arg); + } + if (self.base.options.bind_global_refs_locally) { try argv.append("-Bsymbolic"); } diff --git a/src/main.zig b/src/main.zig index 4fe048bb93..c23aa3111a 100644 --- a/src/main.zig +++ b/src/main.zig @@ -443,6 +443,8 @@ const usage_build_generic = \\ -dynamic Force output to be dynamically linked \\ -static Force output to be statically linked \\ -Bsymbolic Bind global references locally + \\ --compress-debug-sections= Compress DWARF debug sections + \\ none|zlib \\ --subsystem [subsystem] (Windows) /SUBSYSTEM: to the linker \\ --stack [size] Override default stack size \\ --image-base [addr] Set base address for executable image @@ -657,6 +659,7 @@ fn buildOutputType( var version_script: ?[]const u8 = null; var disable_c_depfile = false; var linker_gc_sections: ?bool = null; + var linker_compress_debug_sections: ?link.CompressDebugSections = null; var linker_allow_shlib_undefined: ?bool = null; var linker_bind_global_refs_locally: ?bool = null; var linker_import_memory: ?bool = null; @@ -938,6 +941,11 @@ fn buildOutputType( install_name = args_iter.next() orelse { fatal("expected parameter after {s}", .{arg}); }; + } else if (mem.startsWith(u8, arg, "--compress-debug-sections=")) { + const param = arg["--compress-debug-sections=".len..]; + linker_compress_debug_sections = std.meta.stringToEnum(link.CompressDebugSections, param) orelse { + fatal("expected --compress-debug-sections=[none|zlib], found '{s}'", .{param}); + }; } else if (mem.eql(u8, arg, "-pagezero_size")) { const next_arg = args_iter.next() orelse { fatal("expected parameter after {s}", .{arg}); @@ -1776,6 +1784,15 @@ fn buildOutputType( linker_global_base = parseIntSuffix(arg, "--global-base=".len); } else if (mem.startsWith(u8, arg, "--export=")) { try linker_export_symbol_names.append(arg["--export=".len..]); + } else if (mem.eql(u8, arg, "--compress-debug-sections")) { + i += 1; + if (i >= linker_args.items.len) { + fatal("expected linker arg after '{s}'", .{arg}); + } + const arg1 = linker_args.items[i]; + linker_compress_debug_sections = std.meta.stringToEnum(link.CompressDebugSections, arg1) orelse { + fatal("expected [none|zlib] after --compress-debug-sections, found '{s}'", .{arg1}); + }; } else if (mem.eql(u8, arg, "-z")) { i += 1; if (i >= linker_args.items.len) { @@ -2849,6 +2866,7 @@ fn buildOutputType( .linker_nxcompat = linker_nxcompat, .linker_dynamicbase = linker_dynamicbase, .linker_optimization = linker_optimization, + .linker_compress_debug_sections = linker_compress_debug_sections, .major_subsystem_version = major_subsystem_version, .minor_subsystem_version = minor_subsystem_version, .link_eh_frame_hdr = link_eh_frame_hdr,