Skip to content

Commit 8b3f15f

Browse files
authored
Merge pull request #11863 from motiejus/compress-debug-sections
ELF: understand --compress-debug-sections
2 parents da75eb0 + 2b99182 commit 8b3f15f

File tree

6 files changed

+69
-18
lines changed

6 files changed

+69
-18
lines changed

src/Compilation.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@ pub const InitOptions = struct {
881881
linker_nxcompat: bool = false,
882882
linker_dynamicbase: bool = false,
883883
linker_optimization: ?u8 = null,
884+
linker_compress_debug_sections: ?link.CompressDebugSections = null,
884885
major_subsystem_version: ?u32 = null,
885886
minor_subsystem_version: ?u32 = null,
886887
clang_passthrough_mode: bool = false,
@@ -1708,6 +1709,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
17081709
.no_builtin = options.no_builtin,
17091710
.allow_shlib_undefined = options.linker_allow_shlib_undefined,
17101711
.bind_global_refs_locally = options.linker_bind_global_refs_locally orelse false,
1712+
.compress_debug_sections = options.linker_compress_debug_sections orelse .none,
17111713
.import_memory = options.linker_import_memory orelse false,
17121714
.import_table = options.linker_import_table,
17131715
.export_table = options.linker_export_table,
@@ -2480,6 +2482,7 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
24802482
man.hash.add(comp.bin_file.options.z_now);
24812483
man.hash.add(comp.bin_file.options.z_relro);
24822484
man.hash.add(comp.bin_file.options.hash_style);
2485+
man.hash.add(comp.bin_file.options.compress_debug_sections);
24832486
man.hash.add(comp.bin_file.options.include_compiler_rt);
24842487
if (comp.bin_file.options.link_libc) {
24852488
man.hash.add(comp.bin_file.options.libc_installation != null);

src/clang_options_data.zig

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,7 +2308,7 @@ flagpd1("compiler-options-dump"),
23082308
.{
23092309
.name = "compress-debug-sections",
23102310
.syntax = .flag,
2311-
.zig_equivalent = .other,
2311+
.zig_equivalent = .compress_debug_sections,
23122312
.pd1 = true,
23132313
.pd2 = true,
23142314
.psl = false,
@@ -3834,7 +3834,7 @@ m("mavx512cd"),
38343834
m("mavx512dq"),
38353835
m("mavx512er"),
38363836
m("mavx512f"),
3837-
flagpd1("mavx512fp16"),
3837+
m("mavx512fp16"),
38383838
m("mavx512ifma"),
38393839
m("mavx512pf"),
38403840
m("mavx512vbmi"),
@@ -3867,7 +3867,7 @@ flagpd1("mconstructor-aliases"),
38673867
flagpd1("mcpu=?"),
38683868
m("mcrbits"),
38693869
m("mcrc"),
3870-
flagpd1("mcrc32"),
3870+
m("mcrc32"),
38713871
m("mcumode"),
38723872
m("mcx16"),
38733873
sepd1("mdebug-pass"),
@@ -3892,8 +3892,8 @@ flagpd1("mfancy-math-387"),
38923892
flagpd1("mfentry"),
38933893
flagpd1("mfix4300"),
38943894
flagpd1("mfix-and-continue"),
3895-
flagpd1("mfix-cmse-cve-2021-35465"),
3896-
flagpd1("mfix-cortex-a53-835769"),
3895+
m("mfix-cmse-cve-2021-35465"),
3896+
m("mfix-cortex-a53-835769"),
38973897
m("mfloat128"),
38983898
sepd1("mfloat-abi"),
38993899
m("mfma"),
@@ -3913,8 +3913,8 @@ flagpd1("mglobal-merge"),
39133913
flagpd1("mgpopt"),
39143914
m("mhard-float"),
39153915
m("mhvx"),
3916-
flagpd1("mhvx-ieee-fp"),
3917-
flagpd1("mhvx-qfloat"),
3916+
m("mhvx-ieee-fp"),
3917+
m("mhvx-qfloat"),
39183918
m("mhreset"),
39193919
m("mhtm"),
39203920
flagpd1("miamcu"),
@@ -4005,7 +4005,7 @@ m("mno-avx512cd"),
40054005
m("mno-avx512dq"),
40064006
m("mno-avx512er"),
40074007
m("mno-avx512f"),
4008-
flagpd1("mno-avx512fp16"),
4008+
m("mno-avx512fp16"),
40094009
m("mno-avx512ifma"),
40104010
m("mno-avx512pf"),
40114011
m("mno-avx512vbmi"),
@@ -4019,7 +4019,7 @@ flagpd1("mno-backchain"),
40194019
m("mno-bmi"),
40204020
m("mno-bmi2"),
40214021
flagpd1("mno-branch-likely"),
4022-
flagpd1("mno-bti-at-return-twice"),
4022+
m("mno-bti-at-return-twice"),
40234023
m("mno-bulk-memory"),
40244024
flagpd1("mno-check-zero-division"),
40254025
m("mno-cldemote"),
@@ -4032,7 +4032,7 @@ flagpd1("mno-constant-cfstrings"),
40324032
flagpd1("mno-constructor-aliases"),
40334033
m("mno-crbits"),
40344034
m("mno-crc"),
4035-
flagpd1("mno-crc32"),
4035+
m("mno-crc32"),
40364036
m("mno-cumode"),
40374037
m("mno-cx16"),
40384038
m("mno-dsp"),
@@ -4044,8 +4044,8 @@ flagpd1("mnoexecstack"),
40444044
m("mno-execute-only"),
40454045
flagpd1("mno-extern-sdata"),
40464046
m("mno-f16c"),
4047-
flagpd1("mno-fix-cmse-cve-2021-35465"),
4048-
flagpd1("mno-fix-cortex-a53-835769"),
4047+
m("mno-fix-cmse-cve-2021-35465"),
4048+
m("mno-fix-cortex-a53-835769"),
40494049
m("mno-float128"),
40504050
m("mno-fma"),
40514051
m("mno-fma4"),
@@ -4058,8 +4058,8 @@ m("mno-ginv"),
40584058
flagpd1("mno-global-merge"),
40594059
flagpd1("mno-gpopt"),
40604060
m("mno-hvx"),
4061-
flagpd1("mno-hvx-ieee-fp"),
4062-
flagpd1("mno-hvx-qfloat"),
4061+
m("mno-hvx-ieee-fp"),
4062+
m("mno-hvx-qfloat"),
40634063
m("mno-hreset"),
40644064
m("mno-htm"),
40654065
flagpd1("mno-iamcu"),
@@ -4137,7 +4137,7 @@ m("mno-reference-types"),
41374137
m("mno-relax"),
41384138
flagpd1("mno-relax-all"),
41394139
flagpd1("mno-relax-pic-calls"),
4140-
flagpd1("mno-relaxed-simd"),
4140+
m("mno-relaxed-simd"),
41414141
flagpd1("mno-restrict-it"),
41424142
m("mno-retpoline"),
41434143
m("mno-retpoline-external-thunk"),
@@ -4259,7 +4259,7 @@ flagpd1("mrelax-pic-calls"),
42594259
.pd2 = true,
42604260
.psl = false,
42614261
},
4262-
flagpd1("mrelaxed-simd"),
4262+
m("mrelaxed-simd"),
42634263
sepd1("mrelocation-model"),
42644264
flagpd1("mrestrict-it"),
42654265
m("mretpoline"),
@@ -4323,7 +4323,7 @@ m("mv66"),
43234323
m("mv67"),
43244324
flagpd1("mv67t"),
43254325
m("mv68"),
4326-
flagpd1("mv69"),
4326+
m("mv69"),
43274327
m("mvaes"),
43284328
m("mvirt"),
43294329
m("mvpclmulqdq"),
@@ -5087,7 +5087,7 @@ joinpd1("msign-return-address-key="),
50875087
.{
50885088
.name = "compress-debug-sections=",
50895089
.syntax = .joined,
5090-
.zig_equivalent = .other,
5090+
.zig_equivalent = .compress_debug_sections,
50915091
.pd1 = true,
50925092
.pd2 = true,
50935093
.psl = false,

src/link.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ pub const Options = struct {
123123
nxcompat: bool,
124124
dynamicbase: bool,
125125
linker_optimization: u8,
126+
compress_debug_sections: CompressDebugSections,
126127
bind_global_refs_locally: bool,
127128
import_memory: bool,
128129
import_table: bool,
@@ -219,6 +220,8 @@ pub const Options = struct {
219220

220221
pub const HashStyle = enum { sysv, gnu, both };
221222

223+
pub const CompressDebugSections = enum { none, zlib };
224+
222225
pub const File = struct {
223226
tag: Tag,
224227
options: Options,

src/link/Elf.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
13511351
link.hashAddSystemLibs(&man.hash, self.base.options.system_libs);
13521352
man.hash.add(allow_shlib_undefined);
13531353
man.hash.add(self.base.options.bind_global_refs_locally);
1354+
man.hash.add(self.base.options.compress_debug_sections);
13541355
man.hash.add(self.base.options.tsan);
13551356
man.hash.addOptionalBytes(self.base.options.sysroot);
13561357
man.hash.add(self.base.options.linker_optimization);
@@ -1754,6 +1755,11 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
17541755
try argv.append("--allow-shlib-undefined");
17551756
}
17561757

1758+
switch (self.base.options.compress_debug_sections) {
1759+
.none => {},
1760+
.zlib => try argv.append("--compress-debug-sections=zlib"),
1761+
}
1762+
17571763
if (self.base.options.bind_global_refs_locally) {
17581764
try argv.append("-Bsymbolic");
17591765
}

src/main.zig

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,9 @@ const usage_build_generic =
443443
\\ -dynamic Force output to be dynamically linked
444444
\\ -static Force output to be statically linked
445445
\\ -Bsymbolic Bind global references locally
446+
\\ --compress-debug-sections=[e] Debug section compression settings
447+
\\ none No compression
448+
\\ zlib Compression with deflate/inflate
446449
\\ --subsystem [subsystem] (Windows) /SUBSYSTEM:<subsystem> to the linker
447450
\\ --stack [size] Override default stack size
448451
\\ --image-base [addr] Set base address for executable image
@@ -657,6 +660,7 @@ fn buildOutputType(
657660
var version_script: ?[]const u8 = null;
658661
var disable_c_depfile = false;
659662
var linker_gc_sections: ?bool = null;
663+
var linker_compress_debug_sections: ?link.CompressDebugSections = null;
660664
var linker_allow_shlib_undefined: ?bool = null;
661665
var linker_bind_global_refs_locally: ?bool = null;
662666
var linker_import_memory: ?bool = null;
@@ -938,6 +942,13 @@ fn buildOutputType(
938942
install_name = args_iter.next() orelse {
939943
fatal("expected parameter after {s}", .{arg});
940944
};
945+
} else if (mem.startsWith(u8, arg, "--compress-debug-sections=")) {
946+
const param = arg["--compress-debug-sections=".len..];
947+
linker_compress_debug_sections = std.meta.stringToEnum(link.CompressDebugSections, param) orelse {
948+
fatal("expected --compress-debug-sections=[none|zlib], found '{s}'", .{param});
949+
};
950+
} else if (mem.eql(u8, arg, "--compress-debug-sections")) {
951+
linker_compress_debug_sections = link.CompressDebugSections.zlib;
941952
} else if (mem.eql(u8, arg, "-pagezero_size")) {
942953
const next_arg = args_iter.next() orelse {
943954
fatal("expected parameter after {s}", .{arg});
@@ -1640,6 +1651,15 @@ fn buildOutputType(
16401651
.weak_library => try system_libs.put(it.only_arg, .{ .weak = true }),
16411652
.weak_framework => try frameworks.put(gpa, it.only_arg, .{ .weak = true }),
16421653
.headerpad_max_install_names => headerpad_max_install_names = true,
1654+
.compress_debug_sections => {
1655+
if (it.only_arg.len == 0) {
1656+
linker_compress_debug_sections = .zlib;
1657+
} else {
1658+
linker_compress_debug_sections = std.meta.stringToEnum(link.CompressDebugSections, it.only_arg) orelse {
1659+
fatal("expected [none|zlib] after --compress-debug-sections, found '{s}'", .{it.only_arg});
1660+
};
1661+
}
1662+
},
16431663
}
16441664
}
16451665
// Parse linker args.
@@ -1776,6 +1796,15 @@ fn buildOutputType(
17761796
linker_global_base = parseIntSuffix(arg, "--global-base=".len);
17771797
} else if (mem.startsWith(u8, arg, "--export=")) {
17781798
try linker_export_symbol_names.append(arg["--export=".len..]);
1799+
} else if (mem.eql(u8, arg, "--compress-debug-sections")) {
1800+
i += 1;
1801+
if (i >= linker_args.items.len) {
1802+
fatal("expected linker arg after '{s}'", .{arg});
1803+
}
1804+
const arg1 = linker_args.items[i];
1805+
linker_compress_debug_sections = std.meta.stringToEnum(link.CompressDebugSections, arg1) orelse {
1806+
fatal("expected [none|zlib] after --compress-debug-sections, found '{s}'", .{arg1});
1807+
};
17791808
} else if (mem.eql(u8, arg, "-z")) {
17801809
i += 1;
17811810
if (i >= linker_args.items.len) {
@@ -2849,6 +2878,7 @@ fn buildOutputType(
28492878
.linker_nxcompat = linker_nxcompat,
28502879
.linker_dynamicbase = linker_dynamicbase,
28512880
.linker_optimization = linker_optimization,
2881+
.linker_compress_debug_sections = linker_compress_debug_sections,
28522882
.major_subsystem_version = major_subsystem_version,
28532883
.minor_subsystem_version = minor_subsystem_version,
28542884
.link_eh_frame_hdr = link_eh_frame_hdr,
@@ -4599,6 +4629,7 @@ pub const ClangArgIterator = struct {
45994629
weak_library,
46004630
weak_framework,
46014631
headerpad_max_install_names,
4632+
compress_debug_sections,
46024633
};
46034634

46044635
const Args = struct {

tools/update_clang_options.zig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,14 @@ const known_options = [_]KnownOpt{
460460
.name = "headerpad_max_install_names",
461461
.ident = "headerpad_max_install_names",
462462
},
463+
.{
464+
.name = "compress-debug-sections",
465+
.ident = "compress_debug_sections",
466+
},
467+
.{
468+
.name = "compress-debug-sections=",
469+
.ident = "compress_debug_sections",
470+
},
463471
};
464472

465473
const blacklisted_options = [_][]const u8{};

0 commit comments

Comments
 (0)