| ... | ... | @@ -2303,7 +2303,6 @@ pub fn writeDbgLineHeader(self: *Dwarf, module: *Module) !void { |
| 2303 | 2303 | // files, and padding. We have a function to compute the upper bound size, however, |
| 2304 | 2304 | // because it's needed for determining where to put the offset of the first `SrcFn`. |
| 2305 | 2305 | const needed_bytes = self.dbgLineNeededHeaderBytes(paths.dirs, paths.files); |
| 2306 | | log.debug("dbg_line_prg_off = {x}, needed_bytes = {x}", .{ dbg_line_prg_off, needed_bytes }); |
| 2307 | 2306 | var di_buf = try std.ArrayList(u8).initCapacity(self.allocator, needed_bytes); |
| 2308 | 2307 | defer di_buf.deinit(); |
| 2309 | 2308 | |
| ... | ... | @@ -2390,6 +2389,8 @@ pub fn writeDbgLineHeader(self: *Dwarf, module: *Module) !void { |
| 2390 | 2389 | }, |
| 2391 | 2390 | } |
| 2392 | 2391 | |
| 2392 | assert(needed_bytes == di_buf.items.len); |
| 2393 | |
| 2393 | 2394 | // We use NOPs because consumers empirically do not respect the header length field. |
| 2394 | 2395 | if (di_buf.items.len > dbg_line_prg_off) { |
| 2395 | 2396 | // Move the first N files to the end to make more padding for the header. |
| ... | ... | @@ -2448,27 +2449,31 @@ fn ptrWidthBytes(self: Dwarf) u8 { |
| 2448 | 2449 | } |
| 2449 | 2450 | |
| 2450 | 2451 | fn dbgLineNeededHeaderBytes(self: Dwarf, dirs: []const []const u8, files: []const []const u8) u32 { |
| 2451 | | _ = self; |
| 2452 | | const directory_entry_format_count = 1; |
| 2453 | | const file_name_entry_format_count = 1; |
| 2454 | | const directory_count = dirs.len + 1; |
| 2455 | | const file_name_count = files.len; |
| 2456 | | |
| 2457 | | var dir_names_len: usize = 0; |
| 2458 | | for (dirs) |dir| { |
| 2459 | | dir_names_len += dir.len + 1; |
| 2452 | var size = switch (self.bin_file.tag) { // length field |
| 2453 | .macho => @sizeOf(u32), |
| 2454 | else => switch (self.ptr_width) { |
| 2455 | .p32 => @as(usize, @sizeOf(u32)), |
| 2456 | .p64 => @sizeOf(u32) + @sizeOf(u64), |
| 2457 | }, |
| 2458 | }; |
| 2459 | size += @sizeOf(u16); // version field |
| 2460 | size += switch (self.bin_file.tag) { // offset to end-of-header |
| 2461 | .macho => @sizeOf(u32), |
| 2462 | else => self.ptrWidthBytes(), |
| 2463 | }; |
| 2464 | size += 18; // opcodes |
| 2465 | |
| 2466 | for (dirs) |dir| { // include dirs |
| 2467 | size += dir.len + 1; |
| 2460 | 2468 | } |
| 2469 | size += 1; // include dirs sentinel |
| 2461 | 2470 | |
| 2462 | | var file_names_len: usize = 0; |
| 2463 | | for (files) |file| { |
| 2464 | | file_names_len += file.len + 1; |
| 2471 | for (files) |file| { // file names |
| 2472 | size += file.len + 1 + 1 + 1 + 1; |
| 2465 | 2473 | } |
| 2474 | size += 1; // file names sentinel |
| 2466 | 2475 | |
| 2467 | | return @intCast(u32, 53 + directory_entry_format_count * 2 + file_name_entry_format_count * 2 + |
| 2468 | | directory_count * 8 + file_name_count * 8 + |
| 2469 | | // These are encoded as DW.FORM.string rather than DW.FORM.strp as we would like |
| 2470 | | // because of a workaround for readelf and gdb failing to understand DWARFv5 correctly. |
| 2471 | | dir_names_len + file_names_len); |
| 2476 | return @intCast(u32, size); |
| 2472 | 2477 | } |
| 2473 | 2478 | |
| 2474 | 2479 | /// The reloc offset for the line offset of a function from the previous function's line. |