authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-18 16:43:13+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-19 21:56:47+02:00
log4f66efdc7f2158330b73f40a44b1f16b43c5318a
tree80a8004c7afc818452e371be88d34985c90b97bb
parent349dc05452299a998e7caf4f7afe7bab3620c247

dwarf: clean up and fix writing include dirs and files to debug line header


1 files changed, 21 insertions(+), 27 deletions(-)

src/link/Dwarf.zig+21-27
......@@ -2265,7 +2265,6 @@ pub fn writeDbgAranges(self: *Dwarf, addr: u64, size: u64) !void {
22652265pub fn writeDbgLineHeader(self: *Dwarf) !void {
22662266 const gpa = self.allocator;
22672267
2268 const ptr_width_bytes: u8 = self.ptrWidthBytes();
22692268 const target_endian = self.target.cpu.arch.endian();
22702269 const init_len_size: usize = if (self.bin_file.tag == .macho)
22712270 4
......@@ -2289,11 +2288,6 @@ pub fn writeDbgLineHeader(self: *Dwarf) !void {
22892288 var di_buf = try std.ArrayList(u8).initCapacity(gpa, needed_bytes);
22902289 defer di_buf.deinit();
22912290
2292 // initial length - length of the .debug_line contribution for this compilation unit,
2293 // not including the initial length itself.
2294 // We will backpatch this value later so just remember where we need to write it.
2295 const before_init_len = di_buf.items.len;
2296
22972291 switch (self.bin_file.tag) {
22982292 .macho => {
22992293 mem.writeIntLittle(u32, di_buf.addManyAsArrayAssumeCapacity(4), @as(u32, 0));
......@@ -2317,10 +2311,14 @@ pub fn writeDbgLineHeader(self: *Dwarf) !void {
23172311 // padding rather than this field.
23182312 const before_header_len = di_buf.items.len;
23192313
2320 di_buf.items.len += switch (self.bin_file.tag) { // We will come back and write this.
2321 .macho => @sizeOf(u32),
2322 else => ptr_width_bytes,
2323 };
2314 // We will come back and write this.
2315 switch (self.bin_file.tag) {
2316 .macho => di_buf.appendNTimesAssumeCapacity(0, 4),
2317 else => switch (self.ptr_width) {
2318 .p32 => di_buf.appendNTimesAssumeCapacity(0, 4),
2319 .p64 => di_buf.appendNTimesAssumeCapacity(0, 8),
2320 },
2321 }
23242322
23252323 const after_header_len = di_buf.items.len;
23262324
......@@ -2358,7 +2356,11 @@ pub fn writeDbgLineHeader(self: *Dwarf) !void {
23582356
23592357 for (paths.files, 0..) |file, i| {
23602358 const dir_index = paths.files_dirs_indexes[i];
2361 log.debug("adding new file name at {d} of '{s}' referencing directory {d}", .{ i + 1, file, dir_index + 1 });
2359 log.debug("adding new file name at {d} of '{s}' referencing directory {d}", .{
2360 i + 1,
2361 file,
2362 dir_index + 1,
2363 });
23622364 di_buf.appendSliceAssumeCapacity(file);
23632365 di_buf.appendSliceAssumeCapacity(&[_]u8{
23642366 0, // null byte for the relative path name
......@@ -2450,17 +2452,17 @@ pub fn writeDbgLineHeader(self: *Dwarf) !void {
24502452 }
24512453
24522454 // Backpatch actual length of the debug line program
2453 const init_len = self.getDebugLineProgramEnd().? - before_init_len - init_len_size;
2455 const init_len = self.getDebugLineProgramEnd().? - init_len_size;
24542456 switch (self.bin_file.tag) {
24552457 .macho => {
2456 mem.writeIntLittle(u32, di_buf.items[before_init_len..][0..4], @as(u32, @intCast(init_len)));
2458 mem.writeIntLittle(u32, di_buf.items[0..4], @as(u32, @intCast(init_len)));
24572459 },
24582460 else => switch (self.ptr_width) {
24592461 .p32 => {
2460 mem.writeInt(u32, di_buf.items[before_init_len..][0..4], @as(u32, @intCast(init_len)), target_endian);
2462 mem.writeInt(u32, di_buf.items[0..4], @as(u32, @intCast(init_len)), target_endian);
24612463 },
24622464 .p64 => {
2463 mem.writeInt(u64, di_buf.items[before_init_len + 4 ..][0..8], init_len, target_endian);
2465 mem.writeInt(u64, di_buf.items[4..][0..8], init_len, target_endian);
24642466 },
24652467 },
24662468 }
......@@ -2668,18 +2670,10 @@ fn genIncludeDirsAndFileNames(self: *Dwarf, arena: Allocator) !struct {
26682670 try files_dir_indexes.ensureTotalCapacity(self.di_files.count());
26692671
26702672 for (self.di_files.keys()) |dif| {
2671 const dir_path = d: {
2672 var buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
2673 const dir_path = try dif.mod.root.joinString(arena, dif.mod.root.sub_path);
2674 const abs_dir_path = if (std.fs.path.isAbsolute(dir_path))
2675 dir_path
2676 else
2677 std.os.realpath(dir_path, &buffer) catch dir_path; // If realpath fails, fallback to whatever dir_path was
2678 break :d try std.fs.path.join(arena, &.{
2679 abs_dir_path, std.fs.path.dirname(dif.sub_file_path) orelse "",
2680 });
2681 };
2682 const sub_file_path = try arena.dupe(u8, std.fs.path.basename(dif.sub_file_path));
2673 const full_path = try dif.mod.root.joinString(arena, dif.sub_file_path);
2674 // TODO re-investigate if realpath is needed here
2675 const dir_path = std.fs.path.dirname(full_path) orelse ".";
2676 const sub_file_path = std.fs.path.basename(full_path);
26832677
26842678 const dir_index: u28 = blk: {
26852679 const dirs_gop = dirs.getOrPutAssumeCapacity(dir_path);