| ... | @@ -2265,7 +2265,6 @@ pub fn writeDbgAranges(self: *Dwarf, addr: u64, size: u64) !void { | ... | @@ -2265,7 +2265,6 @@ pub fn writeDbgAranges(self: *Dwarf, addr: u64, size: u64) !void { |
| 2265 | pub fn writeDbgLineHeader(self: *Dwarf) !void { | 2265 | pub fn writeDbgLineHeader(self: *Dwarf) !void { |
| 2266 | const gpa = self.allocator; | 2266 | const gpa = self.allocator; |
| 2267 | | 2267 | |
| 2268 | const ptr_width_bytes: u8 = self.ptrWidthBytes(); | | |
| 2269 | const target_endian = self.target.cpu.arch.endian(); | 2268 | const target_endian = self.target.cpu.arch.endian(); |
| 2270 | const init_len_size: usize = if (self.bin_file.tag == .macho) | 2269 | const init_len_size: usize = if (self.bin_file.tag == .macho) |
| 2271 | 4 | 2270 | 4 |
| ... | @@ -2289,11 +2288,6 @@ pub fn writeDbgLineHeader(self: *Dwarf) !void { | ... | @@ -2289,11 +2288,6 @@ pub fn writeDbgLineHeader(self: *Dwarf) !void { |
| 2289 | var di_buf = try std.ArrayList(u8).initCapacity(gpa, needed_bytes); | 2288 | var di_buf = try std.ArrayList(u8).initCapacity(gpa, needed_bytes); |
| 2290 | defer di_buf.deinit(); | 2289 | defer di_buf.deinit(); |
| 2291 | | 2290 | |
| 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 | | | |
| 2297 | switch (self.bin_file.tag) { | 2291 | switch (self.bin_file.tag) { |
| 2298 | .macho => { | 2292 | .macho => { |
| 2299 | mem.writeIntLittle(u32, di_buf.addManyAsArrayAssumeCapacity(4), @as(u32, 0)); | 2293 | mem.writeIntLittle(u32, di_buf.addManyAsArrayAssumeCapacity(4), @as(u32, 0)); |
| ... | @@ -2317,10 +2311,14 @@ pub fn writeDbgLineHeader(self: *Dwarf) !void { | ... | @@ -2317,10 +2311,14 @@ pub fn writeDbgLineHeader(self: *Dwarf) !void { |
| 2317 | // padding rather than this field. | 2311 | // padding rather than this field. |
| 2318 | const before_header_len = di_buf.items.len; | 2312 | const before_header_len = di_buf.items.len; |
| 2319 | | 2313 | |
| 2320 | di_buf.items.len += switch (self.bin_file.tag) { // We will come back and write this. | 2314 | // We will come back and write this. |
| 2321 | .macho => @sizeOf(u32), | 2315 | switch (self.bin_file.tag) { |
| 2322 | else => ptr_width_bytes, | 2316 | .macho => di_buf.appendNTimesAssumeCapacity(0, 4), |
| 2323 | }; | 2317 | else => switch (self.ptr_width) { |
| | 2318 | .p32 => di_buf.appendNTimesAssumeCapacity(0, 4), |
| | 2319 | .p64 => di_buf.appendNTimesAssumeCapacity(0, 8), |
| | 2320 | }, |
| | 2321 | } |
| 2324 | | 2322 | |
| 2325 | const after_header_len = di_buf.items.len; | 2323 | const after_header_len = di_buf.items.len; |
| 2326 | | 2324 | |
| ... | @@ -2358,7 +2356,11 @@ pub fn writeDbgLineHeader(self: *Dwarf) !void { | ... | @@ -2358,7 +2356,11 @@ pub fn writeDbgLineHeader(self: *Dwarf) !void { |
| 2358 | | 2356 | |
| 2359 | for (paths.files, 0..) |file, i| { | 2357 | for (paths.files, 0..) |file, i| { |
| 2360 | const dir_index = paths.files_dirs_indexes[i]; | 2358 | 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 | }); |
| 2362 | di_buf.appendSliceAssumeCapacity(file); | 2364 | di_buf.appendSliceAssumeCapacity(file); |
| 2363 | di_buf.appendSliceAssumeCapacity(&[_]u8{ | 2365 | di_buf.appendSliceAssumeCapacity(&[_]u8{ |
| 2364 | 0, // null byte for the relative path name | 2366 | 0, // null byte for the relative path name |
| ... | @@ -2450,17 +2452,17 @@ pub fn writeDbgLineHeader(self: *Dwarf) !void { | ... | @@ -2450,17 +2452,17 @@ pub fn writeDbgLineHeader(self: *Dwarf) !void { |
| 2450 | } | 2452 | } |
| 2451 | | 2453 | |
| 2452 | // Backpatch actual length of the debug line program | 2454 | // 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; |
| 2454 | switch (self.bin_file.tag) { | 2456 | switch (self.bin_file.tag) { |
| 2455 | .macho => { | 2457 | .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))); |
| 2457 | }, | 2459 | }, |
| 2458 | else => switch (self.ptr_width) { | 2460 | else => switch (self.ptr_width) { |
| 2459 | .p32 => { | 2461 | .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); |
| 2461 | }, | 2463 | }, |
| 2462 | .p64 => { | 2464 | .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); |
| 2464 | }, | 2466 | }, |
| 2465 | }, | 2467 | }, |
| 2466 | } | 2468 | } |
| ... | @@ -2668,18 +2670,10 @@ fn genIncludeDirsAndFileNames(self: *Dwarf, arena: Allocator) !struct { | ... | @@ -2668,18 +2670,10 @@ fn genIncludeDirsAndFileNames(self: *Dwarf, arena: Allocator) !struct { |
| 2668 | try files_dir_indexes.ensureTotalCapacity(self.di_files.count()); | 2670 | try files_dir_indexes.ensureTotalCapacity(self.di_files.count()); |
| 2669 | | 2671 | |
| 2670 | for (self.di_files.keys()) |dif| { | 2672 | for (self.di_files.keys()) |dif| { |
| 2671 | const dir_path = d: { | 2673 | const full_path = try dif.mod.root.joinString(arena, dif.sub_file_path); |
| 2672 | var buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined; | 2674 | // TODO re-investigate if realpath is needed here |
| 2673 | const dir_path = try dif.mod.root.joinString(arena, dif.mod.root.sub_path); | 2675 | const dir_path = std.fs.path.dirname(full_path) orelse "."; |
| 2674 | const abs_dir_path = if (std.fs.path.isAbsolute(dir_path)) | 2676 | const sub_file_path = std.fs.path.basename(full_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)); | | |
| 2683 | | 2677 | |
| 2684 | const dir_index: u28 = blk: { | 2678 | const dir_index: u28 = blk: { |
| 2685 | const dirs_gop = dirs.getOrPutAssumeCapacity(dir_path); | 2679 | const dirs_gop = dirs.getOrPutAssumeCapacity(dir_path); |