authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-20 15:20:12-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-04-20 15:20:12-07:00
log304e4082a0a50c2a6dfbba2a1985cf772edb9e73
tree959bfcee9176e487dfcd4488ada99364658f9a37
parentfac120bc3ad58a10ab80952e42becd0084aec059
parentceff2782029672714ac2e04a7b3e25af23eb9a9b
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #15193 from davidgm94/dwarf-64-bit-format

Expose an option for producing 64-bit DWARF format

11 files changed, 115 insertions(+), 17 deletions(-)

lib/std/Build/CompileStep.zig+8
......@@ -69,6 +69,7 @@ disable_stack_probing: bool,
6969disable_sanitize_c: bool,
7070sanitize_thread: bool,
7171rdynamic: bool,
72dwarf_format: ?std.dwarf.Format = null,
7273import_memory: bool = false,
7374/// For WebAssembly targets, this will allow for undefined symbols to
7475/// be imported from the host environment.
......@@ -1450,6 +1451,13 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
14501451 try addFlag(&zig_args, "strip", self.strip);
14511452 try addFlag(&zig_args, "unwind-tables", self.unwind_tables);
14521453
1454 if (self.dwarf_format) |dwarf_format| {
1455 try zig_args.append(switch (dwarf_format) {
1456 .@"32" => "-gdwarf32",
1457 .@"64" => "-gdwarf64",
1458 });
1459 }
1460
14531461 switch (self.compress_debug_sections) {
14541462 .none => {},
14551463 .zlib => try zig_args.append("--compress-debug-sections=zlib"),
lib/std/dwarf.zig+2
......@@ -147,6 +147,8 @@ pub const CC = enum(u8) {
147147 GNU_borland_fastcall_i386 = 0x41,
148148};
149149
150pub const Format = enum { @"32", @"64" };
151
150152const PcRange = struct {
151153 start: u64,
152154 end: u64,
src/Compilation.zig+10-1
......@@ -620,6 +620,7 @@ pub const InitOptions = struct {
620620 test_name_prefix: ?[]const u8 = null,
621621 test_runner_path: ?[]const u8 = null,
622622 subsystem: ?std.Target.SubSystem = null,
623 dwarf_format: ?std.dwarf.Format = null,
623624 /// WASI-only. Type of WASI execution model ("command" or "reactor").
624625 wasi_exec_model: ?std.builtin.WasiExecModel = null,
625626 /// (Zig compiler development) Enable dumping linker's state as JSON.
......@@ -1111,6 +1112,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
11111112 cache.hash.add(link_libunwind);
11121113 cache.hash.add(options.output_mode);
11131114 cache.hash.add(options.machine_code_model);
1115 cache.hash.addOptional(options.dwarf_format);
11141116 cache_helpers.addOptionalEmitLoc(&cache.hash, options.emit_bin);
11151117 cache_helpers.addOptionalEmitLoc(&cache.hash, options.emit_implib);
11161118 cache.hash.addBytes(options.root_name);
......@@ -1517,6 +1519,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
15171519 .disable_lld_caching = options.disable_lld_caching or cache_mode == .whole,
15181520 .subsystem = options.subsystem,
15191521 .is_test = options.is_test,
1522 .dwarf_format = options.dwarf_format,
15201523 .wasi_exec_model = wasi_exec_model,
15211524 .hash_style = options.hash_style,
15221525 .enable_link_snapshots = options.enable_link_snapshots,
......@@ -4488,7 +4491,13 @@ pub fn addCCArgs(
44884491 // generation, it only changes the type of information generated.
44894492 try argv.appendSlice(&.{ "-g", "-gcodeview" });
44904493 },
4491 .elf, .macho => try argv.append("-gdwarf-4"),
4494 .elf, .macho => {
4495 try argv.append("-gdwarf-4");
4496 if (comp.bin_file.options.dwarf_format) |f| switch (f) {
4497 .@"32" => try argv.append("-gdwarf32"),
4498 .@"64" => try argv.append("-gdwarf64"),
4499 };
4500 },
44924501 else => try argv.append("-g"),
44934502 }
44944503 }
src/clang_options_data.zig+56-7
......@@ -3870,13 +3870,62 @@ flagpd1("gcodeview-command-line"),
38703870flagpd1("gcodeview-ghash"),
38713871flagpd1("gcolumn-info"),
38723872flagpd1("gdbx"),
3873flagpd1("gdwarf"),
3874flagpd1("gdwarf32"),
3875flagpd1("gdwarf64"),
3876flagpd1("gdwarf-2"),
3877flagpd1("gdwarf-3"),
3878flagpd1("gdwarf-4"),
3879flagpd1("gdwarf-5"),
3873.{
3874 .name = "gdwarf",
3875 .syntax = .flag,
3876 .zig_equivalent = .debug,
3877 .pd1 = true,
3878 .pd2 = false,
3879 .psl = false,
3880},
3881.{
3882 .name = "gdwarf32",
3883 .syntax = .flag,
3884 .zig_equivalent = .gdwarf32,
3885 .pd1 = true,
3886 .pd2 = false,
3887 .psl = false,
3888},
3889.{
3890 .name = "gdwarf64",
3891 .syntax = .flag,
3892 .zig_equivalent = .gdwarf64,
3893 .pd1 = true,
3894 .pd2 = false,
3895 .psl = false,
3896},
3897.{
3898 .name = "gdwarf-2",
3899 .syntax = .flag,
3900 .zig_equivalent = .debug,
3901 .pd1 = true,
3902 .pd2 = false,
3903 .psl = false,
3904},
3905.{
3906 .name = "gdwarf-3",
3907 .syntax = .flag,
3908 .zig_equivalent = .debug,
3909 .pd1 = true,
3910 .pd2 = false,
3911 .psl = false,
3912},
3913.{
3914 .name = "gdwarf-4",
3915 .syntax = .flag,
3916 .zig_equivalent = .debug,
3917 .pd1 = true,
3918 .pd2 = false,
3919 .psl = false,
3920},
3921.{
3922 .name = "gdwarf-5",
3923 .syntax = .flag,
3924 .zig_equivalent = .debug,
3925 .pd1 = true,
3926 .pd2 = false,
3927 .psl = false,
3928},
38803929flagpd1("gdwarf-aranges"),
38813930flagpd1("gembed-source"),
38823931sepd1("gen-cdb-fragment-path"),
src/codegen/llvm.zig+1-1
......@@ -433,7 +433,7 @@ pub const Object = struct {
433433 if (!options.strip) {
434434 switch (options.target.ofmt) {
435435 .coff => llvm_module.addModuleCodeViewFlag(),
436 else => llvm_module.addModuleDebugInfoFlag(),
436 else => llvm_module.addModuleDebugInfoFlag(options.dwarf_format == std.dwarf.Format.@"64"),
437437 }
438438 const di_builder = llvm_module.createDIBuilder(true);
439439 opt_di_builder = di_builder;
src/codegen/llvm/bindings.zig+1-1
......@@ -409,7 +409,7 @@ pub const Module = opaque {
409409 extern fn LLVMSetTarget(M: *Module, Triple: [*:0]const u8) void;
410410
411411 pub const addModuleDebugInfoFlag = ZigLLVMAddModuleDebugInfoFlag;
412 extern fn ZigLLVMAddModuleDebugInfoFlag(module: *Module) void;
412 extern fn ZigLLVMAddModuleDebugInfoFlag(module: *Module, dwarf64: bool) void;
413413
414414 pub const addModuleCodeViewFlag = ZigLLVMAddModuleCodeViewFlag;
415415 extern fn ZigLLVMAddModuleCodeViewFlag(module: *Module) void;
src/link.zig+2
......@@ -200,6 +200,8 @@ pub const Options = struct {
200200 compatibility_version: ?std.builtin.Version,
201201 libc_installation: ?*const LibCInstallation,
202202
203 dwarf_format: ?std.dwarf.Format,
204
203205 /// WASI-only. Type of WASI execution model ("command" or "reactor").
204206 wasi_exec_model: std.builtin.WasiExecModel = undefined,
205207
src/main.zig+16
......@@ -844,6 +844,7 @@ fn buildOutputType(
844844 var reference_trace: ?u32 = null;
845845 var error_tracing: ?bool = null;
846846 var pdb_out_path: ?[]const u8 = null;
847 var dwarf_format: ?std.dwarf.Format = null;
847848
848849 // e.g. -m3dnow or -mno-outline-atomics. They correspond to std.Target llvm cpu feature names.
849850 // This array is populated by zig cc frontend and then has to be converted to zig-style
......@@ -1355,6 +1356,10 @@ fn buildOutputType(
13551356 strip = true;
13561357 } else if (mem.eql(u8, arg, "-fno-strip")) {
13571358 strip = false;
1359 } else if (mem.eql(u8, arg, "-gdwarf32")) {
1360 dwarf_format = .@"32";
1361 } else if (mem.eql(u8, arg, "-gdwarf64")) {
1362 dwarf_format = .@"64";
13581363 } else if (mem.eql(u8, arg, "-fformatted-panics")) {
13591364 formatted_panics = true;
13601365 } else if (mem.eql(u8, arg, "-fno-formatted-panics")) {
......@@ -1762,6 +1767,14 @@ fn buildOutputType(
17621767 try clang_argv.appendSlice(it.other_args);
17631768 }
17641769 },
1770 .gdwarf32 => {
1771 strip = false;
1772 dwarf_format = .@"32";
1773 },
1774 .gdwarf64 => {
1775 strip = false;
1776 dwarf_format = .@"64";
1777 },
17651778 .sanitize => {
17661779 if (mem.eql(u8, it.only_arg, "undefined")) {
17671780 want_sanitize_c = true;
......@@ -3145,6 +3158,7 @@ fn buildOutputType(
31453158 .test_runner_path = test_runner_path,
31463159 .disable_lld_caching = !output_to_cache,
31473160 .subsystem = subsystem,
3161 .dwarf_format = dwarf_format,
31483162 .wasi_exec_model = wasi_exec_model,
31493163 .debug_compile_errors = debug_compile_errors,
31503164 .enable_link_snapshots = enable_link_snapshots,
......@@ -5102,6 +5116,8 @@ pub const ClangArgIterator = struct {
51025116 asm_only,
51035117 optimize,
51045118 debug,
5119 gdwarf32,
5120 gdwarf64,
51055121 sanitize,
51065122 linker_script,
51075123 dry_run,
src/zig_llvm.cpp+5-1
......@@ -1163,9 +1163,13 @@ void ZigLLVMGetNativeTarget(ZigLLVM_ArchType *arch_type,
11631163 free(native_triple);
11641164}
11651165
1166void ZigLLVMAddModuleDebugInfoFlag(LLVMModuleRef module) {
1166void ZigLLVMAddModuleDebugInfoFlag(LLVMModuleRef module, bool produce_dwarf64) {
11671167 unwrap(module)->addModuleFlag(Module::Warning, "Debug Info Version", DEBUG_METADATA_VERSION);
11681168 unwrap(module)->addModuleFlag(Module::Warning, "Dwarf Version", 4);
1169
1170 if (produce_dwarf64) {
1171 unwrap(module)->addModuleFlag(Module::Warning, "DWARF64", 1);
1172 }
11691173}
11701174
11711175void ZigLLVMAddModuleCodeViewFlag(LLVMModuleRef module) {
src/zig_llvm.h+1-1
......@@ -238,7 +238,7 @@ ZIG_EXTERN_C unsigned ZigLLVMTag_DW_union_type(void);
238238
239239ZIG_EXTERN_C struct ZigLLVMDIBuilder *ZigLLVMCreateDIBuilder(LLVMModuleRef module, bool allow_unresolved);
240240ZIG_EXTERN_C void ZigLLVMDisposeDIBuilder(struct ZigLLVMDIBuilder *dbuilder);
241ZIG_EXTERN_C void ZigLLVMAddModuleDebugInfoFlag(LLVMModuleRef module);
241ZIG_EXTERN_C void ZigLLVMAddModuleDebugInfoFlag(LLVMModuleRef module, bool produce_dwarf64);
242242ZIG_EXTERN_C void ZigLLVMAddModuleCodeViewFlag(LLVMModuleRef module);
243243ZIG_EXTERN_C void ZigLLVMSetModulePICLevel(LLVMModuleRef module);
244244ZIG_EXTERN_C void ZigLLVMSetModulePIELevel(LLVMModuleRef module);
tools/update_clang_options.zig+13-5
......@@ -241,23 +241,31 @@ const known_options = [_]KnownOpt{
241241 .ident = "debug",
242242 },
243243 .{
244 .name = "g-dwarf",
244 .name = "gdwarf32",
245 .ident = "gdwarf32",
246 },
247 .{
248 .name = "gdwarf64",
249 .ident = "gdwarf64",
250 },
251 .{
252 .name = "gdwarf",
245253 .ident = "debug",
246254 },
247255 .{
248 .name = "g-dwarf-2",
256 .name = "gdwarf-2",
249257 .ident = "debug",
250258 },
251259 .{
252 .name = "g-dwarf-3",
260 .name = "gdwarf-3",
253261 .ident = "debug",
254262 },
255263 .{
256 .name = "g-dwarf-4",
264 .name = "gdwarf-4",
257265 .ident = "debug",
258266 },
259267 .{
260 .name = "g-dwarf-5",
268 .name = "gdwarf-5",
261269 .ident = "debug",
262270 },
263271 .{