authorgravatar for davidgm94.work@protonmail.comDavid Gonzalez Martin <davidgm94.work@protonmail.com> 2023-04-06 10:16:20+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-20 14:46:53-07:00
logd026202a26e56e7e2ea20ca49509fdcf8b937020
tree12f872577f617312e9c6e68e0d53a29f1986d06e
parentfac120bc3ad58a10ab80952e42becd0084aec059

Expose an option for producing 64-bit DWARF format

This commit enables producing 64-bit DWARF format for Zig executables that are produced through the LLVM backend. This is achieved by exposing both command-line flags and CompileStep flags. The production of the 64-bit format only affects binaries that use the DWARF format and it is disabled on MacOS due to it being problematic. This commit, despite generating the interface for the Zig user to be able to tell the compile which format is wanted, is just implemented for the LLVM backend, so clang and the self-hosted backends will need this to be implemented in a future commit. This is an effort to work around #7962, since the emission of the 64-bit format automatically produces 64-bit relocations. Further investigation will be needed to make DWARF 32-bit format to emit bigger relocations when needed and not make the linker angry.

9 files changed, 39 insertions(+), 4 deletions(-)

lib/std/Build/CompileStep.zig+9
......@@ -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.
......@@ -1449,6 +1450,14 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
14491450
14501451 try addFlag(&zig_args, "strip", self.strip);
14511452 try addFlag(&zig_args, "unwind-tables", self.unwind_tables);
1453 if (!self.producesPdbFile()) {
1454 if (self.dwarf_format) |dwarf_format| {
1455 try zig_args.append(switch (dwarf_format) {
1456 .dwarf32 => "-gdwarf32",
1457 .dwarf64 => "-gdwarf64",
1458 });
1459 }
1460 }
14521461
14531462 switch (self.compress_debug_sections) {
14541463 .none => {},
lib/std/dwarf.zig+5
......@@ -147,6 +147,11 @@ pub const CC = enum(u8) {
147147 GNU_borland_fastcall_i386 = 0x41,
148148};
149149
150pub const Format = enum {
151 dwarf32,
152 dwarf64,
153};
154
150155const PcRange = struct {
151156 start: u64,
152157 end: u64,
src/Compilation.zig+2
......@@ -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.
......@@ -1517,6 +1518,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
15171518 .disable_lld_caching = options.disable_lld_caching or cache_mode == .whole,
15181519 .subsystem = options.subsystem,
15191520 .is_test = options.is_test,
1521 .dwarf_format = options.dwarf_format,
15201522 .wasi_exec_model = wasi_exec_model,
15211523 .hash_style = options.hash_style,
15221524 .enable_link_snapshots = options.enable_link_snapshots,
src/codegen/llvm.zig+8-1
......@@ -433,7 +433,14 @@ 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 => {
437 const dwarf_format = options.dwarf_format orelse .dwarf32;
438 const produce_dwarf64 = switch (dwarf_format) {
439 .dwarf32 => false,
440 .dwarf64 => true,
441 };
442 llvm_module.addModuleDebugInfoFlag(produce_dwarf64);
443 },
437444 }
438445 const di_builder = llvm_module.createDIBuilder(true);
439446 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 = null,
204
203205 /// WASI-only. Type of WASI execution model ("command" or "reactor").
204206 wasi_exec_model: std.builtin.WasiExecModel = undefined,
205207
src/main.zig+6
......@@ -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 = .dwarf32;
1361 } else if (mem.eql(u8, arg, "-gdwarf64")) {
1362 dwarf_format = .dwarf64;
13581363 } else if (mem.eql(u8, arg, "-fformatted-panics")) {
13591364 formatted_panics = true;
13601365 } else if (mem.eql(u8, arg, "-fno-formatted-panics")) {
......@@ -3145,6 +3150,7 @@ fn buildOutputType(
31453150 .test_runner_path = test_runner_path,
31463151 .disable_lld_caching = !output_to_cache,
31473152 .subsystem = subsystem,
3153 .dwarf_format = dwarf_format,
31483154 .wasi_exec_model = wasi_exec_model,
31493155 .debug_compile_errors = debug_compile_errors,
31503156 .enable_link_snapshots = enable_link_snapshots,
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);