| ... | ... | @@ -11,7 +11,12 @@ const leb = std.leb; |
| 11 | 11 | const Allocator = mem.Allocator; |
| 12 | 12 | |
| 13 | 13 | const trace = @import("../../tracy.zig").trace; |
| 14 | const Module = @import("../../Module.zig"); |
| 15 | const Type = @import("../../type.zig").Type; |
| 16 | const link = @import("../../link.zig"); |
| 14 | 17 | const MachO = @import("../MachO.zig"); |
| 18 | const SrcFn = MachO.SrcFn; |
| 19 | const TextBlock = MachO.TextBlock; |
| 15 | 20 | const satMul = MachO.satMul; |
| 16 | 21 | const alloc_num = MachO.alloc_num; |
| 17 | 22 | const alloc_den = MachO.alloc_den; |
| ... | ... | @@ -58,6 +63,21 @@ debug_line_section_index: ?u16 = null, |
| 58 | 63 | |
| 59 | 64 | debug_abbrev_table_offset: ?u64 = null, |
| 60 | 65 | |
| 66 | /// A list of `SrcFn` whose Line Number Programs have surplus capacity. |
| 67 | /// This is the same concept as `text_block_free_list`; see those doc comments. |
| 68 | dbg_line_fn_free_list: std.AutoHashMapUnmanaged(*SrcFn, void) = .{}, |
| 69 | dbg_line_fn_first: ?*SrcFn = null, |
| 70 | dbg_line_fn_last: ?*SrcFn = null, |
| 71 | |
| 72 | /// A list of `TextBlock` whose corresponding .debug_info tags have surplus capacity. |
| 73 | /// This is the same concept as `text_block_free_list`; see those doc comments. |
| 74 | dbg_info_decl_free_list: std.AutoHashMapUnmanaged(*TextBlock, void) = .{}, |
| 75 | dbg_info_decl_first: ?*TextBlock = null, |
| 76 | dbg_info_decl_last: ?*TextBlock = null, |
| 77 | |
| 78 | /// Table of debug symbol names aka the debug string table. |
| 79 | debug_string_table: std.ArrayListUnmanaged(u8) = .{}, |
| 80 | |
| 61 | 81 | header_dirty: bool = false, |
| 62 | 82 | load_commands_dirty: bool = false, |
| 63 | 83 | string_table_dirty: bool = false, |
| ... | ... | @@ -67,6 +87,13 @@ debug_aranges_section_dirty: bool = false, |
| 67 | 87 | debug_info_header_dirty: bool = false, |
| 68 | 88 | debug_line_header_dirty: bool = false, |
| 69 | 89 | |
| 90 | pub const abbrev_compile_unit = 1; |
| 91 | pub const abbrev_subprogram = 2; |
| 92 | pub const abbrev_subprogram_retvoid = 3; |
| 93 | pub const abbrev_base_type = 4; |
| 94 | pub const abbrev_pad1 = 5; |
| 95 | pub const abbrev_parameter = 6; |
| 96 | |
| 70 | 97 | /// You must call this function *after* `MachO.populateMissingMetadata()` |
| 71 | 98 | /// has been called to get a viable debug symbols output. |
| 72 | 99 | pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void { |
| ... | ... | @@ -186,20 +213,14 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void |
| 186 | 213 | if (self.debug_str_section_index == null) { |
| 187 | 214 | const dwarf_segment = &self.load_commands.items[self.dwarf_segment_cmd_index.?].Segment; |
| 188 | 215 | self.debug_str_section_index = @intCast(u16, dwarf_segment.sections.items.len); |
| 189 | | assert(self.base.debug_string_table.items.len == 0); |
| 190 | | |
| 191 | | const file_size_hint = 200; |
| 192 | | const p_align = 1; |
| 193 | | const off = dwarf_segment.findFreeSpace(file_size_hint, p_align, null); |
| 194 | | |
| 195 | | log.debug("found dSym __debug_strtab free space 0x{x} to 0x{x}", .{ off, off + file_size_hint }); |
| 216 | assert(self.debug_string_table.items.len == 0); |
| 196 | 217 | |
| 197 | 218 | try dwarf_segment.addSection(allocator, .{ |
| 198 | 219 | .sectname = makeStaticString("__debug_str"), |
| 199 | 220 | .segname = makeStaticString("__DWARF"), |
| 200 | | .addr = dwarf_segment.inner.vmaddr + off, |
| 201 | | .size = @intCast(u32, self.base.debug_string_table.items.len), |
| 202 | | .offset = @intCast(u32, off), |
| 221 | .addr = dwarf_segment.inner.vmaddr, |
| 222 | .size = @intCast(u32, self.debug_string_table.items.len), |
| 223 | .offset = @intCast(u32, dwarf_segment.inner.fileoff), |
| 203 | 224 | .@"align" = 1, |
| 204 | 225 | .reloff = 0, |
| 205 | 226 | .nreloc = 0, |
| ... | ... | @@ -225,7 +246,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void |
| 225 | 246 | try dwarf_segment.addSection(allocator, .{ |
| 226 | 247 | .sectname = makeStaticString("__debug_info"), |
| 227 | 248 | .segname = makeStaticString("__DWARF"), |
| 228 | | .addr = dwarf_segment.inner.vmaddr + off, |
| 249 | .addr = dwarf_segment.inner.vmaddr + off - dwarf_segment.inner.fileoff, |
| 229 | 250 | .size = file_size_hint, |
| 230 | 251 | .offset = @intCast(u32, off), |
| 231 | 252 | .@"align" = p_align, |
| ... | ... | @@ -253,7 +274,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void |
| 253 | 274 | try dwarf_segment.addSection(allocator, .{ |
| 254 | 275 | .sectname = makeStaticString("__debug_abbrev"), |
| 255 | 276 | .segname = makeStaticString("__DWARF"), |
| 256 | | .addr = dwarf_segment.inner.vmaddr + off, |
| 277 | .addr = dwarf_segment.inner.vmaddr + off - dwarf_segment.inner.fileoff, |
| 257 | 278 | .size = file_size_hint, |
| 258 | 279 | .offset = @intCast(u32, off), |
| 259 | 280 | .@"align" = p_align, |
| ... | ... | @@ -281,7 +302,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void |
| 281 | 302 | try dwarf_segment.addSection(allocator, .{ |
| 282 | 303 | .sectname = makeStaticString("__debug_aranges"), |
| 283 | 304 | .segname = makeStaticString("__DWARF"), |
| 284 | | .addr = dwarf_segment.inner.vmaddr + off, |
| 305 | .addr = dwarf_segment.inner.vmaddr + off - dwarf_segment.inner.fileoff, |
| 285 | 306 | .size = file_size_hint, |
| 286 | 307 | .offset = @intCast(u32, off), |
| 287 | 308 | .@"align" = p_align, |
| ... | ... | @@ -309,7 +330,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void |
| 309 | 330 | try dwarf_segment.addSection(allocator, .{ |
| 310 | 331 | .sectname = makeStaticString("__debug_line"), |
| 311 | 332 | .segname = makeStaticString("__DWARF"), |
| 312 | | .addr = dwarf_segment.inner.vmaddr + off, |
| 333 | .addr = dwarf_segment.inner.vmaddr + off - dwarf_segment.inner.fileoff, |
| 313 | 334 | .size = file_size_hint, |
| 314 | 335 | .offset = @intCast(u32, off), |
| 315 | 336 | .@"align" = p_align, |
| ... | ... | @@ -326,17 +347,346 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: *Allocator) !void |
| 326 | 347 | } |
| 327 | 348 | } |
| 328 | 349 | |
| 329 | | pub fn flush(self: *DebugSymbols, allocator: *Allocator) !void { |
| 350 | pub fn flushModule(self: *DebugSymbols, allocator: *Allocator, options: link.Options) !void { |
| 351 | // TODO This linker code currently assumes there is only 1 compilation unit and it corresponds to the |
| 352 | // Zig source code. |
| 353 | const module = options.module orelse return error.LinkingWithoutZigSourceUnimplemented; |
| 354 | const init_len_size: usize = 12; |
| 355 | |
| 356 | if (self.debug_abbrev_section_dirty) { |
| 357 | const dwarf_segment = &self.load_commands.items[self.dwarf_segment_cmd_index.?].Segment; |
| 358 | const debug_abbrev_sect = &dwarf_segment.sections.items[self.debug_abbrev_section_index.?]; |
| 359 | |
| 360 | // These are LEB encoded but since the values are all less than 127 |
| 361 | // we can simply append these bytes. |
| 362 | const abbrev_buf = [_]u8{ |
| 363 | abbrev_compile_unit, DW.TAG_compile_unit, DW.CHILDREN_yes, // header |
| 364 | DW.AT_stmt_list, DW.FORM_sec_offset, DW.AT_low_pc, |
| 365 | DW.FORM_addr, DW.AT_high_pc, DW.FORM_addr, |
| 366 | DW.AT_name, DW.FORM_strp, DW.AT_comp_dir, |
| 367 | DW.FORM_strp, DW.AT_producer, DW.FORM_strp, |
| 368 | DW.AT_language, DW.FORM_data2, 0, |
| 369 | 0, // table sentinel |
| 370 | abbrev_subprogram, |
| 371 | DW.TAG_subprogram, |
| 372 | DW.CHILDREN_yes, // header |
| 373 | DW.AT_low_pc, |
| 374 | DW.FORM_addr, |
| 375 | DW.AT_high_pc, |
| 376 | DW.FORM_data4, |
| 377 | DW.AT_type, |
| 378 | DW.FORM_ref4, |
| 379 | DW.AT_name, |
| 380 | DW.FORM_string, |
| 381 | 0, 0, // table sentinel |
| 382 | abbrev_subprogram_retvoid, |
| 383 | DW.TAG_subprogram, DW.CHILDREN_yes, // header |
| 384 | DW.AT_low_pc, DW.FORM_addr, |
| 385 | DW.AT_high_pc, DW.FORM_data4, |
| 386 | DW.AT_name, DW.FORM_string, |
| 387 | 0, |
| 388 | 0, // table sentinel |
| 389 | abbrev_base_type, |
| 390 | DW.TAG_base_type, |
| 391 | DW.CHILDREN_no, // header |
| 392 | DW.AT_encoding, |
| 393 | DW.FORM_data1, |
| 394 | DW.AT_byte_size, |
| 395 | DW.FORM_data1, |
| 396 | DW.AT_name, |
| 397 | DW.FORM_string, 0, 0, // table sentinel |
| 398 | abbrev_pad1, DW.TAG_unspecified_type, DW.CHILDREN_no, // header |
| 399 | 0, 0, // table sentinel |
| 400 | abbrev_parameter, |
| 401 | DW.TAG_formal_parameter, DW.CHILDREN_no, // header |
| 402 | DW.AT_location, DW.FORM_exprloc, |
| 403 | DW.AT_type, DW.FORM_ref4, |
| 404 | DW.AT_name, DW.FORM_string, |
| 405 | 0, |
| 406 | 0, // table sentinel |
| 407 | 0, |
| 408 | 0, |
| 409 | 0, // section sentinel |
| 410 | }; |
| 411 | |
| 412 | const needed_size = abbrev_buf.len; |
| 413 | const allocated_size = dwarf_segment.allocatedSize(debug_abbrev_sect.offset); |
| 414 | if (needed_size > allocated_size) { |
| 415 | debug_abbrev_sect.size = 0; // free the space |
| 416 | debug_abbrev_sect.offset = @intCast(u32, dwarf_segment.findFreeSpace(needed_size, 1, null)); |
| 417 | } |
| 418 | debug_abbrev_sect.size = needed_size; |
| 419 | log.debug("__debug_abbrev start=0x{x} end=0x{x}", .{ |
| 420 | debug_abbrev_sect.offset, |
| 421 | debug_abbrev_sect.offset + needed_size, |
| 422 | }); |
| 423 | |
| 424 | const abbrev_offset = 0; |
| 425 | self.debug_abbrev_table_offset = abbrev_offset; |
| 426 | try self.file.pwriteAll(&abbrev_buf, debug_abbrev_sect.offset + abbrev_offset); |
| 427 | self.load_commands_dirty = true; |
| 428 | self.debug_abbrev_section_dirty = false; |
| 429 | } |
| 430 | |
| 431 | if (self.debug_info_header_dirty) debug_info: { |
| 432 | // If this value is null it means there is an error in the module; |
| 433 | // leave debug_info_header_dirty=true. |
| 434 | const first_dbg_info_decl = self.dbg_info_decl_first orelse break :debug_info; |
| 435 | const last_dbg_info_decl = self.dbg_info_decl_last.?; |
| 436 | const dwarf_segment = &self.load_commands.items[self.dwarf_segment_cmd_index.?].Segment; |
| 437 | const debug_info_sect = &dwarf_segment.sections.items[self.debug_info_section_index.?]; |
| 438 | |
| 439 | var di_buf = std.ArrayList(u8).init(allocator); |
| 440 | defer di_buf.deinit(); |
| 441 | |
| 442 | // We have a function to compute the upper bound size, because it's needed |
| 443 | // for determining where to put the offset of the first `LinkBlock`. |
| 444 | try di_buf.ensureCapacity(self.dbgInfoNeededHeaderBytes()); |
| 445 | |
| 446 | // initial length - length of the .debug_info contribution for this compilation unit, |
| 447 | // not including the initial length itself. |
| 448 | // We have to come back and write it later after we know the size. |
| 449 | const after_init_len = di_buf.items.len + init_len_size; |
| 450 | // +1 for the final 0 that ends the compilation unit children. |
| 451 | const dbg_info_end = last_dbg_info_decl.dbg_info_off + last_dbg_info_decl.dbg_info_len + 1; |
| 452 | const init_len = dbg_info_end - after_init_len; |
| 453 | di_buf.appendNTimesAssumeCapacity(0xff, 4); |
| 454 | mem.writeIntLittle(u64, di_buf.addManyAsArrayAssumeCapacity(8), init_len); |
| 455 | mem.writeIntLittle(u16, di_buf.addManyAsArrayAssumeCapacity(2), 4); // DWARF version |
| 456 | const abbrev_offset = self.debug_abbrev_table_offset.?; |
| 457 | mem.writeIntLittle(u64, di_buf.addManyAsArrayAssumeCapacity(8), abbrev_offset); |
| 458 | di_buf.appendAssumeCapacity(8); // address size |
| 459 | // Write the form for the compile unit, which must match the abbrev table above. |
| 460 | const name_strp = try self.makeDebugString(allocator, module.root_pkg.root_src_path); |
| 461 | const comp_dir_strp = try self.makeDebugString(allocator, module.root_pkg.root_src_directory.path orelse "."); |
| 462 | const producer_strp = try self.makeDebugString(allocator, link.producer_string); |
| 463 | // Currently only one compilation unit is supported, so the address range is simply |
| 464 | // identical to the main program header virtual address and memory size. |
| 465 | const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment; |
| 466 | const text_section = text_segment.sections.items[self.text_section_index.?]; |
| 467 | const low_pc = text_section.addr; |
| 468 | const high_pc = text_section.addr + text_section.size; |
| 469 | |
| 470 | di_buf.appendAssumeCapacity(abbrev_compile_unit); |
| 471 | mem.writeIntLittle(u64, di_buf.addManyAsArrayAssumeCapacity(8), 0); // DW.AT_stmt_list, DW.FORM_sec_offset |
| 472 | mem.writeIntLittle(u64, di_buf.addManyAsArrayAssumeCapacity(8), low_pc); |
| 473 | mem.writeIntLittle(u64, di_buf.addManyAsArrayAssumeCapacity(8), high_pc); |
| 474 | mem.writeIntLittle(u64, di_buf.addManyAsArrayAssumeCapacity(8), name_strp); |
| 475 | mem.writeIntLittle(u64, di_buf.addManyAsArrayAssumeCapacity(8), comp_dir_strp); |
| 476 | mem.writeIntLittle(u64, di_buf.addManyAsArrayAssumeCapacity(8), producer_strp); |
| 477 | // We are still waiting on dwarf-std.org to assign DW_LANG_Zig a number: |
| 478 | // http://dwarfstd.org/ShowIssue.php?issue=171115.1 |
| 479 | // Until then we say it is C99. |
| 480 | mem.writeIntLittle(u16, di_buf.addManyAsArrayAssumeCapacity(2), DW.LANG_C99); |
| 481 | |
| 482 | if (di_buf.items.len > first_dbg_info_decl.dbg_info_off) { |
| 483 | // Move the first N decls to the end to make more padding for the header. |
| 484 | @panic("TODO: handle __zdebug_info header exceeding its padding"); |
| 485 | } |
| 486 | const jmp_amt = first_dbg_info_decl.dbg_info_off - di_buf.items.len; |
| 487 | try self.pwriteDbgInfoNops(0, di_buf.items, jmp_amt, false, debug_info_sect.offset); |
| 488 | self.debug_info_header_dirty = false; |
| 489 | } |
| 490 | |
| 491 | if (self.debug_aranges_section_dirty) { |
| 492 | const dwarf_segment = &self.load_commands.items[self.dwarf_segment_cmd_index.?].Segment; |
| 493 | const debug_aranges_sect = &dwarf_segment.sections.items[self.debug_aranges_section_index.?]; |
| 494 | const debug_info_sect = dwarf_segment.sections.items[self.debug_info_section_index.?]; |
| 495 | |
| 496 | var di_buf = std.ArrayList(u8).init(allocator); |
| 497 | defer di_buf.deinit(); |
| 498 | |
| 499 | // Enough for all the data without resizing. When support for more compilation units |
| 500 | // is added, the size of this section will become more variable. |
| 501 | try di_buf.ensureCapacity(100); |
| 502 | |
| 503 | // initial length - length of the .debug_aranges contribution for this compilation unit, |
| 504 | // not including the initial length itself. |
| 505 | // We have to come back and write it later after we know the size. |
| 506 | const init_len_index = di_buf.items.len; |
| 507 | di_buf.items.len += init_len_size; |
| 508 | const after_init_len = di_buf.items.len; |
| 509 | mem.writeIntLittle(u16, di_buf.addManyAsArrayAssumeCapacity(2), 2); // version |
| 510 | // When more than one compilation unit is supported, this will be the offset to it. |
| 511 | // For now it is always at offset 0 in .debug_info. |
| 512 | mem.writeIntLittle(u64, di_buf.addManyAsArrayAssumeCapacity(8), debug_info_sect.addr); // __debug_info offset |
| 513 | di_buf.appendAssumeCapacity(@sizeOf(u64)); // address_size |
| 514 | di_buf.appendAssumeCapacity(0); // segment_selector_size |
| 515 | |
| 516 | const end_header_offset = di_buf.items.len; |
| 517 | const begin_entries_offset = mem.alignForward(end_header_offset, @sizeOf(u64) * 2); |
| 518 | di_buf.appendNTimesAssumeCapacity(0, begin_entries_offset - end_header_offset); |
| 519 | |
| 520 | // Currently only one compilation unit is supported, so the address range is simply |
| 521 | // identical to the main program header virtual address and memory size. |
| 522 | const text_segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment; |
| 523 | const text_section = text_segment.sections.items[self.text_section_index.?]; |
| 524 | mem.writeIntLittle(u64, di_buf.addManyAsArrayAssumeCapacity(8), text_section.addr); |
| 525 | mem.writeIntLittle(u64, di_buf.addManyAsArrayAssumeCapacity(8), text_section.size); |
| 526 | |
| 527 | // Sentinel. |
| 528 | mem.writeIntLittle(u64, di_buf.addManyAsArrayAssumeCapacity(8), 0); |
| 529 | mem.writeIntLittle(u64, di_buf.addManyAsArrayAssumeCapacity(8), 0); |
| 530 | |
| 531 | // Go back and populate the initial length. |
| 532 | const init_len = di_buf.items.len - after_init_len; |
| 533 | // initial length - length of the .debug_aranges contribution for this compilation unit, |
| 534 | // not including the initial length itself. |
| 535 | di_buf.items[init_len_index..][0..4].* = [_]u8{ 0xff, 0xff, 0xff, 0xff }; |
| 536 | mem.writeIntLittle(u64, di_buf.items[init_len_index + 4 ..][0..8], init_len); |
| 537 | |
| 538 | const needed_size = di_buf.items.len; |
| 539 | const allocated_size = dwarf_segment.allocatedSize(debug_aranges_sect.offset); |
| 540 | if (needed_size > allocated_size) { |
| 541 | debug_aranges_sect.size = 0; // free the space |
| 542 | const offset = dwarf_segment.findFreeSpace(needed_size, 16, null); |
| 543 | debug_aranges_sect.offset = @intCast(u32, offset); |
| 544 | debug_aranges_sect.addr = dwarf_segment.inner.vmaddr + offset - dwarf_segment.inner.fileoff; |
| 545 | } |
| 546 | debug_aranges_sect.size = needed_size; |
| 547 | log.debug("__debug_aranges start=0x{x} end=0x{x}", .{ |
| 548 | debug_aranges_sect.offset, |
| 549 | debug_aranges_sect.offset + needed_size, |
| 550 | }); |
| 551 | |
| 552 | try self.file.pwriteAll(di_buf.items, debug_aranges_sect.offset); |
| 553 | self.load_commands_dirty = true; |
| 554 | self.debug_aranges_section_dirty = false; |
| 555 | } |
| 556 | if (self.debug_line_header_dirty) debug_line: { |
| 557 | if (self.dbg_line_fn_first == null) { |
| 558 | break :debug_line; // Error in module; leave debug_line_header_dirty=true. |
| 559 | } |
| 560 | const dbg_line_prg_off = self.getDebugLineProgramOff(); |
| 561 | const dbg_line_prg_end = self.getDebugLineProgramEnd(); |
| 562 | assert(dbg_line_prg_end != 0); |
| 563 | |
| 564 | const dwarf_segment = &self.load_commands.items[self.dwarf_segment_cmd_index.?].Segment; |
| 565 | const debug_line_sect = &dwarf_segment.sections.items[self.debug_line_section_index.?]; |
| 566 | |
| 567 | var di_buf = std.ArrayList(u8).init(allocator); |
| 568 | defer di_buf.deinit(); |
| 569 | |
| 570 | // The size of this header is variable, depending on the number of directories, |
| 571 | // files, and padding. We have a function to compute the upper bound size, however, |
| 572 | // because it's needed for determining where to put the offset of the first `SrcFn`. |
| 573 | try di_buf.ensureCapacity(self.dbgLineNeededHeaderBytes(module)); |
| 574 | |
| 575 | // initial length - length of the .debug_line contribution for this compilation unit, |
| 576 | // not including the initial length itself. |
| 577 | const after_init_len = di_buf.items.len + init_len_size; |
| 578 | const init_len = dbg_line_prg_end - after_init_len; |
| 579 | di_buf.appendNTimesAssumeCapacity(0xff, 4); |
| 580 | mem.writeIntLittle(u64, di_buf.addManyAsArrayAssumeCapacity(8), init_len); |
| 581 | mem.writeIntLittle(u16, di_buf.addManyAsArrayAssumeCapacity(2), 4); // version |
| 582 | |
| 583 | // Empirically, debug info consumers do not respect this field, or otherwise |
| 584 | // consider it to be an error when it does not point exactly to the end of the header. |
| 585 | // Therefore we rely on the NOP jump at the beginning of the Line Number Program for |
| 586 | // padding rather than this field. |
| 587 | const before_header_len = di_buf.items.len; |
| 588 | di_buf.items.len += @sizeOf(u64); // We will come back and write this. |
| 589 | const after_header_len = di_buf.items.len; |
| 590 | |
| 591 | const opcode_base = DW.LNS_set_isa + 1; |
| 592 | di_buf.appendSliceAssumeCapacity(&[_]u8{ |
| 593 | 1, // minimum_instruction_length |
| 594 | 1, // maximum_operations_per_instruction |
| 595 | 1, // default_is_stmt |
| 596 | 1, // line_base (signed) |
| 597 | 1, // line_range |
| 598 | opcode_base, |
| 599 | |
| 600 | // Standard opcode lengths. The number of items here is based on `opcode_base`. |
| 601 | // The value is the number of LEB128 operands the instruction takes. |
| 602 | 0, // `DW.LNS_copy` |
| 603 | 1, // `DW.LNS_advance_pc` |
| 604 | 1, // `DW.LNS_advance_line` |
| 605 | 1, // `DW.LNS_set_file` |
| 606 | 1, // `DW.LNS_set_column` |
| 607 | 0, // `DW.LNS_negate_stmt` |
| 608 | 0, // `DW.LNS_set_basic_block` |
| 609 | 0, // `DW.LNS_const_add_pc` |
| 610 | 1, // `DW.LNS_fixed_advance_pc` |
| 611 | 0, // `DW.LNS_set_prologue_end` |
| 612 | 0, // `DW.LNS_set_epilogue_begin` |
| 613 | 1, // `DW.LNS_set_isa` |
| 614 | 0, // include_directories (none except the compilation unit cwd) |
| 615 | }); |
| 616 | // file_names[0] |
| 617 | di_buf.appendSliceAssumeCapacity(module.root_pkg.root_src_path); // relative path name |
| 618 | di_buf.appendSliceAssumeCapacity(&[_]u8{ |
| 619 | 0, // null byte for the relative path name |
| 620 | 0, // directory_index |
| 621 | 0, // mtime (TODO supply this) |
| 622 | 0, // file size bytes (TODO supply this) |
| 623 | 0, // file_names sentinel |
| 624 | }); |
| 625 | |
| 626 | const header_len = di_buf.items.len - after_header_len; |
| 627 | mem.writeIntLittle(u64, di_buf.items[before_header_len..][0..8], header_len); |
| 628 | |
| 629 | // We use NOPs because consumers empirically do not respect the header length field. |
| 630 | if (di_buf.items.len > dbg_line_prg_off) { |
| 631 | // Move the first N files to the end to make more padding for the header. |
| 632 | @panic("TODO: handle __debug_line header exceeding its padding"); |
| 633 | } |
| 634 | const jmp_amt = dbg_line_prg_off - di_buf.items.len; |
| 635 | try self.pwriteDbgLineNops(0, di_buf.items, jmp_amt, debug_line_sect.offset); |
| 636 | self.debug_line_header_dirty = false; |
| 637 | } |
| 638 | { |
| 639 | const dwarf_segment = &self.load_commands.items[self.dwarf_segment_cmd_index.?].Segment; |
| 640 | const debug_strtab_sect = &dwarf_segment.sections.items[self.debug_str_section_index.?]; |
| 641 | if (self.debug_string_table_dirty or self.debug_string_table.items.len != debug_strtab_sect.size) { |
| 642 | const allocated_size = dwarf_segment.allocatedSize(debug_strtab_sect.offset); |
| 643 | const needed_size = self.debug_string_table.items.len; |
| 644 | |
| 645 | if (needed_size > allocated_size) { |
| 646 | debug_strtab_sect.size = 0; // free the space |
| 647 | const new_offset = dwarf_segment.findFreeSpace(needed_size, 1, null); |
| 648 | debug_strtab_sect.addr = dwarf_segment.inner.vmaddr + new_offset - dwarf_segment.inner.fileoff; |
| 649 | debug_strtab_sect.offset = @intCast(u32, new_offset); |
| 650 | } |
| 651 | debug_strtab_sect.size = @intCast(u32, needed_size); |
| 652 | |
| 653 | log.debug("__debug_strtab start=0x{x} end=0x{x}", .{ |
| 654 | debug_strtab_sect.offset, |
| 655 | debug_strtab_sect.offset + needed_size, |
| 656 | }); |
| 657 | |
| 658 | try self.file.pwriteAll(self.debug_string_table.items, debug_strtab_sect.offset); |
| 659 | self.load_commands_dirty = true; |
| 660 | self.debug_string_table_dirty = false; |
| 661 | } |
| 662 | } |
| 663 | |
| 330 | 664 | try self.writeStringTable(); |
| 665 | |
| 666 | { |
| 667 | const dwarf_segment = &self.load_commands.items[self.dwarf_segment_cmd_index.?].Segment; |
| 668 | var file_size: u64 = 0; |
| 669 | for (dwarf_segment.sections.items) |sect| { |
| 670 | file_size += sect.size; |
| 671 | } |
| 672 | dwarf_segment.inner.filesize = file_size; |
| 673 | } |
| 674 | |
| 331 | 675 | try self.writeLoadCommands(allocator); |
| 332 | 676 | try self.writeHeader(); |
| 333 | 677 | |
| 334 | 678 | assert(!self.header_dirty); |
| 335 | 679 | assert(!self.load_commands_dirty); |
| 336 | 680 | assert(!self.string_table_dirty); |
| 681 | assert(!self.debug_abbrev_section_dirty); |
| 682 | assert(!self.debug_aranges_section_dirty); |
| 683 | assert(!self.debug_string_table_dirty); |
| 337 | 684 | } |
| 338 | 685 | |
| 339 | 686 | pub fn deinit(self: *DebugSymbols, allocator: *Allocator) void { |
| 687 | self.dbg_info_decl_free_list.deinit(allocator); |
| 688 | self.dbg_line_fn_free_list.deinit(allocator); |
| 689 | self.debug_string_table.deinit(allocator); |
| 340 | 690 | for (self.load_commands.items) |*lc| { |
| 341 | 691 | lc.deinit(allocator); |
| 342 | 692 | } |
| ... | ... | @@ -535,3 +885,367 @@ pub fn writeStringTable(self: *DebugSymbols) !void { |
| 535 | 885 | self.load_commands_dirty = true; |
| 536 | 886 | self.string_table_dirty = false; |
| 537 | 887 | } |
| 888 | |
| 889 | /// Asserts the type has codegen bits. |
| 890 | pub fn addDbgInfoType( |
| 891 | self: *DebugSymbols, |
| 892 | ty: Type, |
| 893 | dbg_info_buffer: *std.ArrayList(u8), |
| 894 | target: std.Target, |
| 895 | ) !void { |
| 896 | switch (ty.zigTypeTag()) { |
| 897 | .Void => unreachable, |
| 898 | .NoReturn => unreachable, |
| 899 | .Bool => { |
| 900 | try dbg_info_buffer.appendSlice(&[_]u8{ |
| 901 | abbrev_base_type, |
| 902 | DW.ATE_boolean, // DW.AT_encoding , DW.FORM_data1 |
| 903 | 1, // DW.AT_byte_size, DW.FORM_data1 |
| 904 | 'b', |
| 905 | 'o', |
| 906 | 'o', |
| 907 | 'l', |
| 908 | 0, // DW.AT_name, DW.FORM_string |
| 909 | }); |
| 910 | }, |
| 911 | .Int => { |
| 912 | const info = ty.intInfo(target); |
| 913 | try dbg_info_buffer.ensureCapacity(dbg_info_buffer.items.len + 12); |
| 914 | dbg_info_buffer.appendAssumeCapacity(abbrev_base_type); |
| 915 | // DW.AT_encoding, DW.FORM_data1 |
| 916 | dbg_info_buffer.appendAssumeCapacity(switch (info.signedness) { |
| 917 | .signed => DW.ATE_signed, |
| 918 | .unsigned => DW.ATE_unsigned, |
| 919 | }); |
| 920 | // DW.AT_byte_size, DW.FORM_data1 |
| 921 | dbg_info_buffer.appendAssumeCapacity(@intCast(u8, ty.abiSize(target))); |
| 922 | // DW.AT_name, DW.FORM_string |
| 923 | try dbg_info_buffer.writer().print("{}\x00", .{ty}); |
| 924 | }, |
| 925 | else => { |
| 926 | std.log.scoped(.compiler).err("TODO implement .debug_info for type '{}'", .{ty}); |
| 927 | try dbg_info_buffer.append(abbrev_pad1); |
| 928 | }, |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | pub fn updateDeclDebugInfoAllocation( |
| 933 | self: *DebugSymbols, |
| 934 | allocator: *Allocator, |
| 935 | text_block: *TextBlock, |
| 936 | len: u32, |
| 937 | ) !void { |
| 938 | const tracy = trace(@src()); |
| 939 | defer tracy.end(); |
| 940 | |
| 941 | // This logic is nearly identical to the logic above in `updateDecl` for |
| 942 | // `SrcFn` and the line number programs. If you are editing this logic, you |
| 943 | // probably need to edit that logic too. |
| 944 | |
| 945 | const dwarf_segment = &self.load_commands.items[self.dwarf_segment_cmd_index.?].Segment; |
| 946 | const debug_info_sect = &dwarf_segment.sections.items[self.debug_info_section_index.?]; |
| 947 | text_block.dbg_info_len = len; |
| 948 | if (self.dbg_info_decl_last) |last| { |
| 949 | if (text_block.dbg_info_next) |next| { |
| 950 | // Update existing Decl - non-last item. |
| 951 | if (text_block.dbg_info_off + text_block.dbg_info_len + min_nop_size > next.dbg_info_off) { |
| 952 | // It grew too big, so we move it to a new location. |
| 953 | if (text_block.dbg_info_prev) |prev| { |
| 954 | _ = self.dbg_info_decl_free_list.put(allocator, prev, {}) catch {}; |
| 955 | prev.dbg_info_next = text_block.dbg_info_next; |
| 956 | } |
| 957 | next.dbg_info_prev = text_block.dbg_info_prev; |
| 958 | text_block.dbg_info_next = null; |
| 959 | // Populate where it used to be with NOPs. |
| 960 | const file_pos = debug_info_sect.offset + text_block.dbg_info_off; |
| 961 | try self.pwriteDbgInfoNops(0, &[0]u8{}, text_block.dbg_info_len, false, file_pos); |
| 962 | // TODO Look at the free list before appending at the end. |
| 963 | text_block.dbg_info_prev = last; |
| 964 | last.dbg_info_next = text_block; |
| 965 | self.dbg_info_decl_last = text_block; |
| 966 | |
| 967 | text_block.dbg_info_off = last.dbg_info_off + (last.dbg_info_len * alloc_num / alloc_den); |
| 968 | } |
| 969 | } else if (text_block.dbg_info_prev == null) { |
| 970 | // Append new Decl. |
| 971 | // TODO Look at the free list before appending at the end. |
| 972 | text_block.dbg_info_prev = last; |
| 973 | last.dbg_info_next = text_block; |
| 974 | self.dbg_info_decl_last = text_block; |
| 975 | |
| 976 | text_block.dbg_info_off = last.dbg_info_off + (last.dbg_info_len * alloc_num / alloc_den); |
| 977 | } |
| 978 | } else { |
| 979 | // This is the first Decl of the .debug_info |
| 980 | self.dbg_info_decl_first = text_block; |
| 981 | self.dbg_info_decl_last = text_block; |
| 982 | |
| 983 | text_block.dbg_info_off = self.dbgInfoNeededHeaderBytes() * alloc_num / alloc_den; |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | pub fn writeDeclDebugInfo(self: *DebugSymbols, text_block: *TextBlock, dbg_info_buf: []const u8) !void { |
| 988 | const tracy = trace(@src()); |
| 989 | defer tracy.end(); |
| 990 | |
| 991 | // This logic is nearly identical to the logic above in `updateDecl` for |
| 992 | // `SrcFn` and the line number programs. If you are editing this logic, you |
| 993 | // probably need to edit that logic too. |
| 994 | |
| 995 | const dwarf_segment = &self.load_commands.items[self.dwarf_segment_cmd_index.?].Segment; |
| 996 | const debug_info_sect = &dwarf_segment.sections.items[self.debug_info_section_index.?]; |
| 997 | |
| 998 | const last_decl = self.dbg_info_decl_last.?; |
| 999 | // +1 for a trailing zero to end the children of the decl tag. |
| 1000 | const needed_size = last_decl.dbg_info_off + last_decl.dbg_info_len + 1; |
| 1001 | if (needed_size != debug_info_sect.size) { |
| 1002 | if (needed_size > dwarf_segment.allocatedSize(debug_info_sect.offset)) { |
| 1003 | const new_offset = dwarf_segment.findFreeSpace(needed_size, 1, null); |
| 1004 | const existing_size = last_decl.dbg_info_off; |
| 1005 | |
| 1006 | // TODO |
| 1007 | assert(dwarf_segment.inner.fileoff + dwarf_segment.inner.filesize >= new_offset + needed_size); |
| 1008 | |
| 1009 | log.debug("moving _debug_info section: {} bytes from 0x{x} to 0x{x}", .{ |
| 1010 | existing_size, |
| 1011 | debug_info_sect.offset, |
| 1012 | new_offset, |
| 1013 | }); |
| 1014 | |
| 1015 | const amt = try self.file.copyRangeAll(debug_info_sect.offset, self.file, new_offset, existing_size); |
| 1016 | if (amt != existing_size) return error.InputOutput; |
| 1017 | debug_info_sect.offset = @intCast(u32, new_offset); |
| 1018 | debug_info_sect.addr = dwarf_segment.inner.vmaddr + new_offset - dwarf_segment.inner.fileoff; |
| 1019 | } |
| 1020 | debug_info_sect.size = needed_size; |
| 1021 | self.load_commands_dirty = true; // TODO look into making only the one section dirty |
| 1022 | self.debug_info_header_dirty = true; |
| 1023 | } |
| 1024 | const prev_padding_size: u32 = if (text_block.dbg_info_prev) |prev| |
| 1025 | text_block.dbg_info_off - (prev.dbg_info_off + prev.dbg_info_len) |
| 1026 | else |
| 1027 | 0; |
| 1028 | const next_padding_size: u32 = if (text_block.dbg_info_next) |next| |
| 1029 | next.dbg_info_off - (text_block.dbg_info_off + text_block.dbg_info_len) |
| 1030 | else |
| 1031 | 0; |
| 1032 | |
| 1033 | // To end the children of the decl tag. |
| 1034 | const trailing_zero = text_block.dbg_info_next == null; |
| 1035 | |
| 1036 | // We only have support for one compilation unit so far, so the offsets are directly |
| 1037 | // from the .debug_info section. |
| 1038 | const file_pos = debug_info_sect.offset + text_block.dbg_info_off; |
| 1039 | try self.pwriteDbgInfoNops(prev_padding_size, dbg_info_buf, next_padding_size, trailing_zero, file_pos); |
| 1040 | } |
| 1041 | |
| 1042 | fn getDebugLineProgramOff(self: DebugSymbols) u32 { |
| 1043 | return self.dbg_line_fn_first.?.off; |
| 1044 | } |
| 1045 | |
| 1046 | fn getDebugLineProgramEnd(self: DebugSymbols) u32 { |
| 1047 | return self.dbg_line_fn_last.?.off + self.dbg_line_fn_last.?.len; |
| 1048 | } |
| 1049 | |
| 1050 | /// TODO Improve this to use a table. |
| 1051 | fn makeDebugString(self: *DebugSymbols, allocator: *Allocator, bytes: []const u8) !u32 { |
| 1052 | try self.debug_string_table.ensureCapacity(allocator, self.debug_string_table.items.len + bytes.len + 1); |
| 1053 | const result = self.debug_string_table.items.len; |
| 1054 | self.debug_string_table.appendSliceAssumeCapacity(bytes); |
| 1055 | self.debug_string_table.appendAssumeCapacity(0); |
| 1056 | return @intCast(u32, result); |
| 1057 | } |
| 1058 | |
| 1059 | /// The reloc offset for the virtual address of a function in its Line Number Program. |
| 1060 | /// Size is a virtual address integer. |
| 1061 | pub const dbg_line_vaddr_reloc_index = 3; |
| 1062 | /// The reloc offset for the virtual address of a function in its .debug_info TAG_subprogram. |
| 1063 | /// Size is a virtual address integer. |
| 1064 | pub const dbg_info_low_pc_reloc_index = 1; |
| 1065 | |
| 1066 | /// The reloc offset for the line offset of a function from the previous function's line. |
| 1067 | /// It's a fixed-size 4-byte ULEB128. |
| 1068 | pub fn getRelocDbgLineOff() usize { |
| 1069 | return dbg_line_vaddr_reloc_index + @sizeOf(u64) + 1; |
| 1070 | } |
| 1071 | |
| 1072 | pub fn getRelocDbgFileIndex() usize { |
| 1073 | return getRelocDbgLineOff() + 5; |
| 1074 | } |
| 1075 | |
| 1076 | pub fn getRelocDbgInfoSubprogramHighPC() u32 { |
| 1077 | return dbg_info_low_pc_reloc_index + @sizeOf(u64); |
| 1078 | } |
| 1079 | |
| 1080 | pub fn dbgLineNeededHeaderBytes(self: DebugSymbols, module: *Module) u32 { |
| 1081 | const directory_entry_format_count = 1; |
| 1082 | const file_name_entry_format_count = 1; |
| 1083 | const directory_count = 1; |
| 1084 | const file_name_count = 1; |
| 1085 | const root_src_dir_path_len = if (module.root_pkg.root_src_directory.path) |p| p.len else 1; // "." |
| 1086 | return @intCast(u32, 53 + directory_entry_format_count * 2 + file_name_entry_format_count * 2 + |
| 1087 | directory_count * 8 + file_name_count * 8 + |
| 1088 | // These are encoded as DW.FORM_string rather than DW.FORM_strp as we would like |
| 1089 | // because of a workaround for readelf and gdb failing to understand DWARFv5 correctly. |
| 1090 | root_src_dir_path_len + |
| 1091 | module.root_pkg.root_src_path.len); |
| 1092 | } |
| 1093 | |
| 1094 | fn dbgInfoNeededHeaderBytes(self: DebugSymbols) u32 { |
| 1095 | return 120; |
| 1096 | } |
| 1097 | |
| 1098 | pub const min_nop_size = 2; |
| 1099 | |
| 1100 | /// Writes to the file a buffer, prefixed and suffixed by the specified number of |
| 1101 | /// bytes of NOPs. Asserts each padding size is at least `min_nop_size` and total padding bytes |
| 1102 | /// are less than 126,976 bytes (if this limit is ever reached, this function can be |
| 1103 | /// improved to make more than one pwritev call, or the limit can be raised by a fixed |
| 1104 | /// amount by increasing the length of `vecs`). |
| 1105 | pub fn pwriteDbgLineNops( |
| 1106 | self: *DebugSymbols, |
| 1107 | prev_padding_size: usize, |
| 1108 | buf: []const u8, |
| 1109 | next_padding_size: usize, |
| 1110 | offset: u64, |
| 1111 | ) !void { |
| 1112 | const tracy = trace(@src()); |
| 1113 | defer tracy.end(); |
| 1114 | |
| 1115 | const page_of_nops = [1]u8{DW.LNS_negate_stmt} ** 4096; |
| 1116 | const three_byte_nop = [3]u8{ DW.LNS_advance_pc, 0b1000_0000, 0 }; |
| 1117 | var vecs: [32]std.os.iovec_const = undefined; |
| 1118 | var vec_index: usize = 0; |
| 1119 | { |
| 1120 | var padding_left = prev_padding_size; |
| 1121 | if (padding_left % 2 != 0) { |
| 1122 | vecs[vec_index] = .{ |
| 1123 | .iov_base = &three_byte_nop, |
| 1124 | .iov_len = three_byte_nop.len, |
| 1125 | }; |
| 1126 | vec_index += 1; |
| 1127 | padding_left -= three_byte_nop.len; |
| 1128 | } |
| 1129 | while (padding_left > page_of_nops.len) { |
| 1130 | vecs[vec_index] = .{ |
| 1131 | .iov_base = &page_of_nops, |
| 1132 | .iov_len = page_of_nops.len, |
| 1133 | }; |
| 1134 | vec_index += 1; |
| 1135 | padding_left -= page_of_nops.len; |
| 1136 | } |
| 1137 | if (padding_left > 0) { |
| 1138 | vecs[vec_index] = .{ |
| 1139 | .iov_base = &page_of_nops, |
| 1140 | .iov_len = padding_left, |
| 1141 | }; |
| 1142 | vec_index += 1; |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | vecs[vec_index] = .{ |
| 1147 | .iov_base = buf.ptr, |
| 1148 | .iov_len = buf.len, |
| 1149 | }; |
| 1150 | vec_index += 1; |
| 1151 | |
| 1152 | { |
| 1153 | var padding_left = next_padding_size; |
| 1154 | if (padding_left % 2 != 0) { |
| 1155 | vecs[vec_index] = .{ |
| 1156 | .iov_base = &three_byte_nop, |
| 1157 | .iov_len = three_byte_nop.len, |
| 1158 | }; |
| 1159 | vec_index += 1; |
| 1160 | padding_left -= three_byte_nop.len; |
| 1161 | } |
| 1162 | while (padding_left > page_of_nops.len) { |
| 1163 | vecs[vec_index] = .{ |
| 1164 | .iov_base = &page_of_nops, |
| 1165 | .iov_len = page_of_nops.len, |
| 1166 | }; |
| 1167 | vec_index += 1; |
| 1168 | padding_left -= page_of_nops.len; |
| 1169 | } |
| 1170 | if (padding_left > 0) { |
| 1171 | vecs[vec_index] = .{ |
| 1172 | .iov_base = &page_of_nops, |
| 1173 | .iov_len = padding_left, |
| 1174 | }; |
| 1175 | vec_index += 1; |
| 1176 | } |
| 1177 | } |
| 1178 | try self.file.pwritevAll(vecs[0..vec_index], offset - prev_padding_size); |
| 1179 | } |
| 1180 | |
| 1181 | /// Writes to the file a buffer, prefixed and suffixed by the specified number of |
| 1182 | /// bytes of padding. |
| 1183 | pub fn pwriteDbgInfoNops( |
| 1184 | self: *DebugSymbols, |
| 1185 | prev_padding_size: usize, |
| 1186 | buf: []const u8, |
| 1187 | next_padding_size: usize, |
| 1188 | trailing_zero: bool, |
| 1189 | offset: u64, |
| 1190 | ) !void { |
| 1191 | const tracy = trace(@src()); |
| 1192 | defer tracy.end(); |
| 1193 | |
| 1194 | const page_of_nops = [1]u8{abbrev_pad1} ** 4096; |
| 1195 | var vecs: [32]std.os.iovec_const = undefined; |
| 1196 | var vec_index: usize = 0; |
| 1197 | { |
| 1198 | var padding_left = prev_padding_size; |
| 1199 | while (padding_left > page_of_nops.len) { |
| 1200 | vecs[vec_index] = .{ |
| 1201 | .iov_base = &page_of_nops, |
| 1202 | .iov_len = page_of_nops.len, |
| 1203 | }; |
| 1204 | vec_index += 1; |
| 1205 | padding_left -= page_of_nops.len; |
| 1206 | } |
| 1207 | if (padding_left > 0) { |
| 1208 | vecs[vec_index] = .{ |
| 1209 | .iov_base = &page_of_nops, |
| 1210 | .iov_len = padding_left, |
| 1211 | }; |
| 1212 | vec_index += 1; |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | vecs[vec_index] = .{ |
| 1217 | .iov_base = buf.ptr, |
| 1218 | .iov_len = buf.len, |
| 1219 | }; |
| 1220 | vec_index += 1; |
| 1221 | |
| 1222 | { |
| 1223 | var padding_left = next_padding_size; |
| 1224 | while (padding_left > page_of_nops.len) { |
| 1225 | vecs[vec_index] = .{ |
| 1226 | .iov_base = &page_of_nops, |
| 1227 | .iov_len = page_of_nops.len, |
| 1228 | }; |
| 1229 | vec_index += 1; |
| 1230 | padding_left -= page_of_nops.len; |
| 1231 | } |
| 1232 | if (padding_left > 0) { |
| 1233 | vecs[vec_index] = .{ |
| 1234 | .iov_base = &page_of_nops, |
| 1235 | .iov_len = padding_left, |
| 1236 | }; |
| 1237 | vec_index += 1; |
| 1238 | } |
| 1239 | } |
| 1240 | |
| 1241 | if (trailing_zero) { |
| 1242 | var zbuf = [1]u8{0}; |
| 1243 | vecs[vec_index] = .{ |
| 1244 | .iov_base = &zbuf, |
| 1245 | .iov_len = zbuf.len, |
| 1246 | }; |
| 1247 | vec_index += 1; |
| 1248 | } |
| 1249 | |
| 1250 | try self.file.pwritevAll(vecs[0..vec_index], offset - prev_padding_size); |
| 1251 | } |