| ... | ... | @@ -16,7 +16,6 @@ const elf = std.elf; |
| 16 | 16 | const mem = std.mem; |
| 17 | 17 | const DW = std.dwarf; |
| 18 | 18 | const AT = DW.AT; |
| 19 | | const EH = DW.EH; |
| 20 | 19 | const FORM = DW.FORM; |
| 21 | 20 | const Format = DW.Format; |
| 22 | 21 | const RLE = DW.RLE; |
| ... | ... | @@ -34,13 +33,12 @@ const Dwarf = @This(); |
| 34 | 33 | pub const expression = @import("Dwarf/expression.zig"); |
| 35 | 34 | pub const abi = @import("Dwarf/abi.zig"); |
| 36 | 35 | pub const call_frame = @import("Dwarf/call_frame.zig"); |
| 36 | pub const Unwind = @import("Dwarf/Unwind.zig"); |
| 37 | 37 | |
| 38 | 38 | /// Useful to temporarily enable while working on this file. |
| 39 | 39 | const debug_debug_mode = false; |
| 40 | 40 | |
| 41 | | endian: Endian, |
| 42 | | sections: SectionArray = null_section_array, |
| 43 | | is_macho: bool, |
| 41 | sections: SectionArray = @splat(null), |
| 44 | 42 | |
| 45 | 43 | /// Filled later by the initializer |
| 46 | 44 | abbrev_table_list: ArrayList(Abbrev.Table) = .empty, |
| ... | ... | @@ -49,14 +47,6 @@ compile_unit_list: ArrayList(CompileUnit) = .empty, |
| 49 | 47 | /// Filled later by the initializer |
| 50 | 48 | func_list: ArrayList(Func) = .empty, |
| 51 | 49 | |
| 52 | | /// Starts out non-`null` if the `.eh_frame_hdr` section is present. May become `null` later if we |
| 53 | | /// find that `.eh_frame_hdr` is incomplete. |
| 54 | | eh_frame_hdr: ?ExceptionFrameHeader = null, |
| 55 | | /// These lookup tables are only used if `eh_frame_hdr` is null |
| 56 | | cie_map: std.AutoArrayHashMapUnmanaged(u64, CommonInformationEntry) = .empty, |
| 57 | | /// Sorted by start_pc |
| 58 | | fde_list: ArrayList(FrameDescriptionEntry) = .empty, |
| 59 | | |
| 60 | 50 | /// Populated by `populateRanges`. |
| 61 | 51 | ranges: ArrayList(Range) = .empty, |
| 62 | 52 | |
| ... | ... | @@ -87,9 +77,6 @@ pub const Section = struct { |
| 87 | 77 | debug_rnglists, |
| 88 | 78 | debug_addr, |
| 89 | 79 | debug_names, |
| 90 | | debug_frame, |
| 91 | | eh_frame, |
| 92 | | eh_frame_hdr, |
| 93 | 80 | }; |
| 94 | 81 | |
| 95 | 82 | // For sections that are not memory mapped by the loader, this is an offset |
| ... | ... | @@ -258,13 +245,14 @@ pub const Die = struct { |
| 258 | 245 | fn getAttrAddr( |
| 259 | 246 | self: *const Die, |
| 260 | 247 | di: *const Dwarf, |
| 248 | endian: Endian, |
| 261 | 249 | id: u64, |
| 262 | | compile_unit: CompileUnit, |
| 250 | compile_unit: *const CompileUnit, |
| 263 | 251 | ) error{ InvalidDebugInfo, MissingDebugInfo }!u64 { |
| 264 | 252 | const form_value = self.getAttr(id) orelse return error.MissingDebugInfo; |
| 265 | 253 | return switch (form_value.*) { |
| 266 | 254 | .addr => |value| value, |
| 267 | | .addrx => |index| di.readDebugAddr(compile_unit, index), |
| 255 | .addrx => |index| di.readDebugAddr(endian, compile_unit, index), |
| 268 | 256 | else => bad(), |
| 269 | 257 | }; |
| 270 | 258 | } |
| ... | ... | @@ -294,9 +282,10 @@ pub const Die = struct { |
| 294 | 282 | pub fn getAttrString( |
| 295 | 283 | self: *const Die, |
| 296 | 284 | di: *Dwarf, |
| 285 | endian: Endian, |
| 297 | 286 | id: u64, |
| 298 | 287 | opt_str: ?[]const u8, |
| 299 | | compile_unit: CompileUnit, |
| 288 | compile_unit: *const CompileUnit, |
| 300 | 289 | ) error{ InvalidDebugInfo, MissingDebugInfo }![]const u8 { |
| 301 | 290 | const form_value = self.getAttr(id) orelse return error.MissingDebugInfo; |
| 302 | 291 | switch (form_value.*) { |
| ... | ... | @@ -309,13 +298,13 @@ pub const Die = struct { |
| 309 | 298 | .@"32" => { |
| 310 | 299 | const byte_offset = compile_unit.str_offsets_base + 4 * index; |
| 311 | 300 | if (byte_offset + 4 > debug_str_offsets.len) return bad(); |
| 312 | | const offset = mem.readInt(u32, debug_str_offsets[byte_offset..][0..4], di.endian); |
| 301 | const offset = mem.readInt(u32, debug_str_offsets[byte_offset..][0..4], endian); |
| 313 | 302 | return getStringGeneric(opt_str, offset); |
| 314 | 303 | }, |
| 315 | 304 | .@"64" => { |
| 316 | 305 | const byte_offset = compile_unit.str_offsets_base + 8 * index; |
| 317 | 306 | if (byte_offset + 8 > debug_str_offsets.len) return bad(); |
| 318 | | const offset = mem.readInt(u64, debug_str_offsets[byte_offset..][0..8], di.endian); |
| 307 | const offset = mem.readInt(u64, debug_str_offsets[byte_offset..][0..8], endian); |
| 319 | 308 | return getStringGeneric(opt_str, offset); |
| 320 | 309 | }, |
| 321 | 310 | } |
| ... | ... | @@ -326,440 +315,17 @@ pub const Die = struct { |
| 326 | 315 | } |
| 327 | 316 | }; |
| 328 | 317 | |
| 329 | | /// This represents the decoded .eh_frame_hdr header |
| 330 | | pub const ExceptionFrameHeader = struct { |
| 331 | | eh_frame_ptr: usize, |
| 332 | | table_enc: u8, |
| 333 | | fde_count: usize, |
| 334 | | entries: []const u8, |
| 335 | | |
| 336 | | pub fn entrySize(table_enc: u8) !u8 { |
| 337 | | return switch (table_enc & EH.PE.type_mask) { |
| 338 | | EH.PE.udata2, |
| 339 | | EH.PE.sdata2, |
| 340 | | => 4, |
| 341 | | EH.PE.udata4, |
| 342 | | EH.PE.sdata4, |
| 343 | | => 8, |
| 344 | | EH.PE.udata8, |
| 345 | | EH.PE.sdata8, |
| 346 | | => 16, |
| 347 | | // This is a binary search table, so all entries must be the same length |
| 348 | | else => return bad(), |
| 349 | | }; |
| 350 | | } |
| 351 | | |
| 352 | | pub fn findEntry( |
| 353 | | self: ExceptionFrameHeader, |
| 354 | | eh_frame_len: usize, |
| 355 | | eh_frame_hdr_ptr: usize, |
| 356 | | pc: usize, |
| 357 | | cie: *CommonInformationEntry, |
| 358 | | fde: *FrameDescriptionEntry, |
| 359 | | endian: Endian, |
| 360 | | ) !void { |
| 361 | | const entry_size = try entrySize(self.table_enc); |
| 362 | | |
| 363 | | var left: usize = 0; |
| 364 | | var len: usize = self.fde_count; |
| 365 | | var fbr: Reader = .fixed(self.entries); |
| 366 | | |
| 367 | | while (len > 1) { |
| 368 | | const mid = left + len / 2; |
| 369 | | |
| 370 | | fbr.seek = mid * entry_size; |
| 371 | | const pc_begin = try readEhPointer(&fbr, self.table_enc, @sizeOf(usize), .{ |
| 372 | | .pc_rel_base = @intFromPtr(&self.entries[fbr.seek]), |
| 373 | | .follow_indirect = true, |
| 374 | | .data_rel_base = eh_frame_hdr_ptr, |
| 375 | | }, endian) orelse return bad(); |
| 376 | | |
| 377 | | if (pc < pc_begin) { |
| 378 | | len /= 2; |
| 379 | | } else { |
| 380 | | left = mid; |
| 381 | | if (pc == pc_begin) break; |
| 382 | | len -= len / 2; |
| 383 | | } |
| 384 | | } |
| 385 | | |
| 386 | | if (len == 0) return missing(); |
| 387 | | fbr.seek = left * entry_size; |
| 388 | | |
| 389 | | // Read past the pc_begin field of the entry |
| 390 | | _ = try readEhPointer(&fbr, self.table_enc, @sizeOf(usize), .{ |
| 391 | | .pc_rel_base = @intFromPtr(&self.entries[fbr.seek]), |
| 392 | | .follow_indirect = true, |
| 393 | | .data_rel_base = eh_frame_hdr_ptr, |
| 394 | | }, endian) orelse return bad(); |
| 395 | | |
| 396 | | const fde_ptr = cast(usize, try readEhPointer(&fbr, self.table_enc, @sizeOf(usize), .{ |
| 397 | | .pc_rel_base = @intFromPtr(&self.entries[fbr.seek]), |
| 398 | | .follow_indirect = true, |
| 399 | | .data_rel_base = eh_frame_hdr_ptr, |
| 400 | | }, endian) orelse return bad()) orelse return bad(); |
| 401 | | |
| 402 | | if (fde_ptr < self.eh_frame_ptr) return bad(); |
| 403 | | |
| 404 | | const eh_frame = @as([*]const u8, @ptrFromInt(self.eh_frame_ptr))[0..eh_frame_len]; |
| 405 | | |
| 406 | | const fde_offset = fde_ptr - self.eh_frame_ptr; |
| 407 | | var eh_frame_fbr: Reader = .fixed(eh_frame); |
| 408 | | eh_frame_fbr.seek = fde_offset; |
| 409 | | |
| 410 | | const fde_entry_header = try EntryHeader.read(&eh_frame_fbr, .eh_frame, endian); |
| 411 | | if (fde_entry_header.type != .fde) return bad(); |
| 412 | | |
| 413 | | // CIEs always come before FDEs (the offset is a subtraction), so we can assume this memory is readable |
| 414 | | const cie_offset = fde_entry_header.type.fde; |
| 415 | | eh_frame_fbr.seek = @intCast(cie_offset); |
| 416 | | const cie_entry_header = try EntryHeader.read(&eh_frame_fbr, .eh_frame, endian); |
| 417 | | if (cie_entry_header.type != .cie) return bad(); |
| 418 | | |
| 419 | | cie.* = try CommonInformationEntry.parse( |
| 420 | | cie_entry_header.entry_bytes, |
| 421 | | 0, |
| 422 | | true, |
| 423 | | cie_entry_header.format, |
| 424 | | .eh_frame, |
| 425 | | cie_entry_header.length_offset, |
| 426 | | @sizeOf(usize), |
| 427 | | endian, |
| 428 | | ); |
| 429 | | |
| 430 | | fde.* = try FrameDescriptionEntry.parse( |
| 431 | | fde_entry_header.entry_bytes, |
| 432 | | 0, |
| 433 | | true, |
| 434 | | cie.*, |
| 435 | | @sizeOf(usize), |
| 436 | | endian, |
| 437 | | ); |
| 438 | | |
| 439 | | if (pc < fde.pc_begin or pc >= fde.pc_begin + fde.pc_range) return missing(); |
| 440 | | } |
| 441 | | }; |
| 442 | | |
| 443 | | pub const EntryHeader = struct { |
| 444 | | /// Offset of the length field in the backing buffer |
| 445 | | length_offset: usize, |
| 446 | | format: Format, |
| 447 | | type: union(enum) { |
| 448 | | cie, |
| 449 | | /// Value is the offset of the corresponding CIE |
| 450 | | fde: u64, |
| 451 | | terminator, |
| 452 | | }, |
| 453 | | /// The entry's contents, not including the ID field |
| 454 | | entry_bytes: []const u8, |
| 455 | | |
| 456 | | /// The length of the entry including the ID field, but not the length field itself |
| 457 | | pub fn entryLength(self: EntryHeader) usize { |
| 458 | | return self.entry_bytes.len + @as(u8, if (self.format == .@"64") 8 else 4); |
| 459 | | } |
| 460 | | |
| 461 | | /// Reads a header for either an FDE or a CIE, then advances the fbr to the |
| 462 | | /// position after the trailing structure. |
| 463 | | /// |
| 464 | | /// `fbr` must be backed by either the .eh_frame or .debug_frame sections. |
| 465 | | /// |
| 466 | | /// TODO that's a bad API, don't do that. this function should neither require |
| 467 | | /// a fixed reader nor depend on seeking. |
| 468 | | pub fn read(fbr: *Reader, dwarf_section: Section.Id, endian: Endian) !EntryHeader { |
| 469 | | assert(dwarf_section == .eh_frame or dwarf_section == .debug_frame); |
| 470 | | |
| 471 | | const length_offset = fbr.seek; |
| 472 | | const unit_header = try readUnitHeader(fbr, endian); |
| 473 | | const unit_length = cast(usize, unit_header.unit_length) orelse return bad(); |
| 474 | | if (unit_length == 0) return .{ |
| 475 | | .length_offset = length_offset, |
| 476 | | .format = unit_header.format, |
| 477 | | .type = .terminator, |
| 478 | | .entry_bytes = &.{}, |
| 479 | | }; |
| 480 | | const start_offset = fbr.seek; |
| 481 | | const end_offset = start_offset + unit_length; |
| 482 | | defer fbr.seek = end_offset; |
| 483 | | |
| 484 | | const id = try readAddress(fbr, unit_header.format, endian); |
| 485 | | const entry_bytes = fbr.buffer[fbr.seek..end_offset]; |
| 486 | | const cie_id: u64 = switch (dwarf_section) { |
| 487 | | .eh_frame => CommonInformationEntry.eh_id, |
| 488 | | .debug_frame => switch (unit_header.format) { |
| 489 | | .@"32" => CommonInformationEntry.dwarf32_id, |
| 490 | | .@"64" => CommonInformationEntry.dwarf64_id, |
| 491 | | }, |
| 492 | | else => unreachable, |
| 493 | | }; |
| 494 | | |
| 495 | | return .{ |
| 496 | | .length_offset = length_offset, |
| 497 | | .format = unit_header.format, |
| 498 | | .type = if (id == cie_id) .cie else .{ .fde = switch (dwarf_section) { |
| 499 | | .eh_frame => try std.math.sub(u64, start_offset, id), |
| 500 | | .debug_frame => id, |
| 501 | | else => unreachable, |
| 502 | | } }, |
| 503 | | .entry_bytes = entry_bytes, |
| 504 | | }; |
| 505 | | } |
| 506 | | }; |
| 507 | | |
| 508 | | pub const CommonInformationEntry = struct { |
| 509 | | // Used in .eh_frame |
| 510 | | pub const eh_id = 0; |
| 511 | | |
| 512 | | // Used in .debug_frame (DWARF32) |
| 513 | | pub const dwarf32_id = maxInt(u32); |
| 514 | | |
| 515 | | // Used in .debug_frame (DWARF64) |
| 516 | | pub const dwarf64_id = maxInt(u64); |
| 517 | | |
| 518 | | // Offset of the length field of this entry in the eh_frame section. |
| 519 | | // This is the key that FDEs use to reference CIEs. |
| 520 | | length_offset: u64, |
| 521 | | version: u8, |
| 522 | | address_size: u8, |
| 523 | | format: Format, |
| 524 | | |
| 525 | | // Only present in version 4 |
| 526 | | segment_selector_size: ?u8, |
| 527 | | |
| 528 | | code_alignment_factor: u32, |
| 529 | | data_alignment_factor: i32, |
| 530 | | return_address_register: u8, |
| 531 | | |
| 532 | | aug_str: []const u8, |
| 533 | | aug_data: []const u8, |
| 534 | | lsda_pointer_enc: u8, |
| 535 | | personality_enc: ?u8, |
| 536 | | personality_routine_pointer: ?u64, |
| 537 | | fde_pointer_enc: u8, |
| 538 | | initial_instructions: []const u8, |
| 539 | | |
| 540 | | pub fn isSignalFrame(self: CommonInformationEntry) bool { |
| 541 | | for (self.aug_str) |c| if (c == 'S') return true; |
| 542 | | return false; |
| 543 | | } |
| 544 | | |
| 545 | | pub fn addressesSignedWithBKey(self: CommonInformationEntry) bool { |
| 546 | | for (self.aug_str) |c| if (c == 'B') return true; |
| 547 | | return false; |
| 548 | | } |
| 549 | | |
| 550 | | pub fn mteTaggedFrame(self: CommonInformationEntry) bool { |
| 551 | | for (self.aug_str) |c| if (c == 'G') return true; |
| 552 | | return false; |
| 553 | | } |
| 554 | | |
| 555 | | /// This function expects to read the CIE starting with the version field. |
| 556 | | /// The returned struct references memory backed by cie_bytes. |
| 557 | | /// |
| 558 | | /// See the FrameDescriptionEntry.parse documentation for the description |
| 559 | | /// of `pc_rel_offset` and `is_runtime`. |
| 560 | | /// |
| 561 | | /// `length_offset` specifies the offset of this CIE's length field in the |
| 562 | | /// .eh_frame / .debug_frame section. |
| 563 | | pub fn parse( |
| 564 | | cie_bytes: []const u8, |
| 565 | | pc_rel_offset: i64, |
| 566 | | is_runtime: bool, |
| 567 | | format: Format, |
| 568 | | dwarf_section: Section.Id, |
| 569 | | length_offset: u64, |
| 570 | | addr_size_bytes: u8, |
| 571 | | endian: Endian, |
| 572 | | ) !CommonInformationEntry { |
| 573 | | if (addr_size_bytes > 8) return error.UnsupportedAddrSize; |
| 574 | | |
| 575 | | var fbr: Reader = .fixed(cie_bytes); |
| 576 | | |
| 577 | | const version = try fbr.takeByte(); |
| 578 | | switch (dwarf_section) { |
| 579 | | .eh_frame => if (version != 1 and version != 3) return error.UnsupportedDwarfVersion, |
| 580 | | .debug_frame => if (version != 4) return error.UnsupportedDwarfVersion, |
| 581 | | else => return error.UnsupportedDwarfSection, |
| 582 | | } |
| 583 | | |
| 584 | | var has_eh_data = false; |
| 585 | | var has_aug_data = false; |
| 586 | | |
| 587 | | var aug_str_len: usize = 0; |
| 588 | | const aug_str_start = fbr.seek; |
| 589 | | var aug_byte = try fbr.takeByte(); |
| 590 | | while (aug_byte != 0) : (aug_byte = try fbr.takeByte()) { |
| 591 | | switch (aug_byte) { |
| 592 | | 'z' => { |
| 593 | | if (aug_str_len != 0) return bad(); |
| 594 | | has_aug_data = true; |
| 595 | | }, |
| 596 | | 'e' => { |
| 597 | | if (has_aug_data or aug_str_len != 0) return bad(); |
| 598 | | if (try fbr.takeByte() != 'h') return bad(); |
| 599 | | has_eh_data = true; |
| 600 | | }, |
| 601 | | else => if (has_eh_data) return bad(), |
| 602 | | } |
| 603 | | |
| 604 | | aug_str_len += 1; |
| 605 | | } |
| 606 | | |
| 607 | | if (has_eh_data) { |
| 608 | | // legacy data created by older versions of gcc - unsupported here |
| 609 | | for (0..addr_size_bytes) |_| _ = try fbr.takeByte(); |
| 610 | | } |
| 611 | | |
| 612 | | const address_size = if (version == 4) try fbr.takeByte() else addr_size_bytes; |
| 613 | | const segment_selector_size = if (version == 4) try fbr.takeByte() else null; |
| 614 | | |
| 615 | | const code_alignment_factor = try fbr.takeLeb128(u32); |
| 616 | | const data_alignment_factor = try fbr.takeLeb128(i32); |
| 617 | | const return_address_register = if (version == 1) try fbr.takeByte() else try fbr.takeLeb128(u8); |
| 618 | | |
| 619 | | var lsda_pointer_enc: u8 = EH.PE.omit; |
| 620 | | var personality_enc: ?u8 = null; |
| 621 | | var personality_routine_pointer: ?u64 = null; |
| 622 | | var fde_pointer_enc: u8 = EH.PE.absptr; |
| 623 | | |
| 624 | | var aug_data: []const u8 = &[_]u8{}; |
| 625 | | const aug_str = if (has_aug_data) blk: { |
| 626 | | const aug_data_len = try fbr.takeLeb128(usize); |
| 627 | | const aug_data_start = fbr.seek; |
| 628 | | aug_data = cie_bytes[aug_data_start..][0..aug_data_len]; |
| 629 | | |
| 630 | | const aug_str = cie_bytes[aug_str_start..][0..aug_str_len]; |
| 631 | | for (aug_str[1..]) |byte| { |
| 632 | | switch (byte) { |
| 633 | | 'L' => { |
| 634 | | lsda_pointer_enc = try fbr.takeByte(); |
| 635 | | }, |
| 636 | | 'P' => { |
| 637 | | personality_enc = try fbr.takeByte(); |
| 638 | | personality_routine_pointer = try readEhPointer(&fbr, personality_enc.?, addr_size_bytes, .{ |
| 639 | | .pc_rel_base = try pcRelBase(@intFromPtr(&cie_bytes[fbr.seek]), pc_rel_offset), |
| 640 | | .follow_indirect = is_runtime, |
| 641 | | }, endian); |
| 642 | | }, |
| 643 | | 'R' => { |
| 644 | | fde_pointer_enc = try fbr.takeByte(); |
| 645 | | }, |
| 646 | | 'S', 'B', 'G' => {}, |
| 647 | | else => return bad(), |
| 648 | | } |
| 649 | | } |
| 650 | | |
| 651 | | // aug_data_len can include padding so the CIE ends on an address boundary |
| 652 | | fbr.seek = aug_data_start + aug_data_len; |
| 653 | | break :blk aug_str; |
| 654 | | } else &[_]u8{}; |
| 655 | | |
| 656 | | const initial_instructions = cie_bytes[fbr.seek..]; |
| 657 | | return .{ |
| 658 | | .length_offset = length_offset, |
| 659 | | .version = version, |
| 660 | | .address_size = address_size, |
| 661 | | .format = format, |
| 662 | | .segment_selector_size = segment_selector_size, |
| 663 | | .code_alignment_factor = code_alignment_factor, |
| 664 | | .data_alignment_factor = data_alignment_factor, |
| 665 | | .return_address_register = return_address_register, |
| 666 | | .aug_str = aug_str, |
| 667 | | .aug_data = aug_data, |
| 668 | | .lsda_pointer_enc = lsda_pointer_enc, |
| 669 | | .personality_enc = personality_enc, |
| 670 | | .personality_routine_pointer = personality_routine_pointer, |
| 671 | | .fde_pointer_enc = fde_pointer_enc, |
| 672 | | .initial_instructions = initial_instructions, |
| 673 | | }; |
| 674 | | } |
| 675 | | }; |
| 676 | | |
| 677 | | pub const FrameDescriptionEntry = struct { |
| 678 | | // Offset into eh_frame where the CIE for this FDE is stored |
| 679 | | cie_length_offset: u64, |
| 680 | | |
| 681 | | pc_begin: u64, |
| 682 | | pc_range: u64, |
| 683 | | lsda_pointer: ?u64, |
| 684 | | aug_data: []const u8, |
| 685 | | instructions: []const u8, |
| 686 | | |
| 687 | | /// This function expects to read the FDE starting at the PC Begin field. |
| 688 | | /// The returned struct references memory backed by `fde_bytes`. |
| 689 | | /// |
| 690 | | /// `pc_rel_offset` specifies an offset to be applied to pc_rel_base values |
| 691 | | /// used when decoding pointers. This should be set to zero if fde_bytes is |
| 692 | | /// backed by the memory of a .eh_frame / .debug_frame section in the running executable. |
| 693 | | /// Otherwise, it should be the relative offset to translate addresses from |
| 694 | | /// where the section is currently stored in memory, to where it *would* be |
| 695 | | /// stored at runtime: section base addr - backing data base ptr. |
| 696 | | /// |
| 697 | | /// Similarly, `is_runtime` specifies this function is being called on a runtime |
| 698 | | /// section, and so indirect pointers can be followed. |
| 699 | | pub fn parse( |
| 700 | | fde_bytes: []const u8, |
| 701 | | pc_rel_offset: i64, |
| 702 | | is_runtime: bool, |
| 703 | | cie: CommonInformationEntry, |
| 704 | | addr_size_bytes: u8, |
| 705 | | endian: Endian, |
| 706 | | ) !FrameDescriptionEntry { |
| 707 | | if (addr_size_bytes > 8) return error.InvalidAddrSize; |
| 708 | | |
| 709 | | var fbr: Reader = .fixed(fde_bytes); |
| 710 | | |
| 711 | | const pc_begin = try readEhPointer(&fbr, cie.fde_pointer_enc, addr_size_bytes, .{ |
| 712 | | .pc_rel_base = try pcRelBase(@intFromPtr(&fde_bytes[fbr.seek]), pc_rel_offset), |
| 713 | | .follow_indirect = is_runtime, |
| 714 | | }, endian) orelse return bad(); |
| 715 | | |
| 716 | | const pc_range = try readEhPointer(&fbr, cie.fde_pointer_enc, addr_size_bytes, .{ |
| 717 | | .pc_rel_base = 0, |
| 718 | | .follow_indirect = false, |
| 719 | | }, endian) orelse return bad(); |
| 720 | | |
| 721 | | var aug_data: []const u8 = &[_]u8{}; |
| 722 | | const lsda_pointer = if (cie.aug_str.len > 0) blk: { |
| 723 | | const aug_data_len = try fbr.takeLeb128(usize); |
| 724 | | const aug_data_start = fbr.seek; |
| 725 | | aug_data = fde_bytes[aug_data_start..][0..aug_data_len]; |
| 726 | | |
| 727 | | const lsda_pointer = if (cie.lsda_pointer_enc != EH.PE.omit) |
| 728 | | try readEhPointer(&fbr, cie.lsda_pointer_enc, addr_size_bytes, .{ |
| 729 | | .pc_rel_base = try pcRelBase(@intFromPtr(&fde_bytes[fbr.seek]), pc_rel_offset), |
| 730 | | .follow_indirect = is_runtime, |
| 731 | | }, endian) |
| 732 | | else |
| 733 | | null; |
| 734 | | |
| 735 | | fbr.seek = aug_data_start + aug_data_len; |
| 736 | | break :blk lsda_pointer; |
| 737 | | } else null; |
| 738 | | |
| 739 | | const instructions = fde_bytes[fbr.seek..]; |
| 740 | | return .{ |
| 741 | | .cie_length_offset = cie.length_offset, |
| 742 | | .pc_begin = pc_begin, |
| 743 | | .pc_range = pc_range, |
| 744 | | .lsda_pointer = lsda_pointer, |
| 745 | | .aug_data = aug_data, |
| 746 | | .instructions = instructions, |
| 747 | | }; |
| 748 | | } |
| 749 | | }; |
| 750 | | |
| 751 | 318 | const num_sections = std.enums.directEnumArrayLen(Section.Id, 0); |
| 752 | 319 | pub const SectionArray = [num_sections]?Section; |
| 753 | | pub const null_section_array = [_]?Section{null} ** num_sections; |
| 754 | 320 | |
| 755 | 321 | pub const OpenError = ScanError; |
| 756 | 322 | |
| 757 | 323 | /// Initialize DWARF info. The caller has the responsibility to initialize most |
| 758 | 324 | /// the `Dwarf` fields before calling. `binary_mem` is the raw bytes of the |
| 759 | 325 | /// main binary file (not the secondary debug info file). |
| 760 | | pub fn open(d: *Dwarf, gpa: Allocator) OpenError!void { |
| 761 | | try d.scanAllFunctions(gpa); |
| 762 | | try d.scanAllCompileUnits(gpa); |
| 326 | pub fn open(d: *Dwarf, gpa: Allocator, endian: Endian) OpenError!void { |
| 327 | try d.scanAllFunctions(gpa, endian); |
| 328 | try d.scanAllCompileUnits(gpa, endian); |
| 763 | 329 | } |
| 764 | 330 | |
| 765 | 331 | const PcRange = struct { |
| ... | ... | @@ -825,31 +391,30 @@ pub const ScanError = error{ |
| 825 | 391 | StreamTooLong, |
| 826 | 392 | } || Allocator.Error; |
| 827 | 393 | |
| 828 | | fn scanAllFunctions(di: *Dwarf, allocator: Allocator) ScanError!void { |
| 829 | | const endian = di.endian; |
| 830 | | var fbr: Reader = .fixed(di.section(.debug_info).?); |
| 394 | fn scanAllFunctions(di: *Dwarf, allocator: Allocator, endian: Endian) ScanError!void { |
| 395 | var fr: Reader = .fixed(di.section(.debug_info).?); |
| 831 | 396 | var this_unit_offset: u64 = 0; |
| 832 | 397 | |
| 833 | | while (this_unit_offset < fbr.buffer.len) { |
| 834 | | fbr.seek = @intCast(this_unit_offset); |
| 398 | while (this_unit_offset < fr.buffer.len) { |
| 399 | fr.seek = @intCast(this_unit_offset); |
| 835 | 400 | |
| 836 | | const unit_header = try readUnitHeader(&fbr, endian); |
| 401 | const unit_header = try readUnitHeader(&fr, endian); |
| 837 | 402 | if (unit_header.unit_length == 0) return; |
| 838 | 403 | const next_offset = unit_header.header_length + unit_header.unit_length; |
| 839 | 404 | |
| 840 | | const version = try fbr.takeInt(u16, endian); |
| 405 | const version = try fr.takeInt(u16, endian); |
| 841 | 406 | if (version < 2 or version > 5) return bad(); |
| 842 | 407 | |
| 843 | 408 | var address_size: u8 = undefined; |
| 844 | 409 | var debug_abbrev_offset: u64 = undefined; |
| 845 | 410 | if (version >= 5) { |
| 846 | | const unit_type = try fbr.takeByte(); |
| 411 | const unit_type = try fr.takeByte(); |
| 847 | 412 | if (unit_type != DW.UT.compile) return bad(); |
| 848 | | address_size = try fbr.takeByte(); |
| 849 | | debug_abbrev_offset = try readAddress(&fbr, unit_header.format, endian); |
| 413 | address_size = try fr.takeByte(); |
| 414 | debug_abbrev_offset = try readAddress(&fr, unit_header.format, endian); |
| 850 | 415 | } else { |
| 851 | | debug_abbrev_offset = try readAddress(&fbr, unit_header.format, endian); |
| 852 | | address_size = try fbr.takeByte(); |
| 416 | debug_abbrev_offset = try readAddress(&fr, unit_header.format, endian); |
| 417 | address_size = try fr.takeByte(); |
| 853 | 418 | } |
| 854 | 419 | if (address_size != @sizeOf(usize)) return bad(); |
| 855 | 420 | |
| ... | ... | @@ -890,12 +455,12 @@ fn scanAllFunctions(di: *Dwarf, allocator: Allocator) ScanError!void { |
| 890 | 455 | }; |
| 891 | 456 | |
| 892 | 457 | while (true) { |
| 893 | | fbr.seek = std.mem.indexOfNonePos(u8, fbr.buffer, fbr.seek, &.{ |
| 458 | fr.seek = std.mem.indexOfNonePos(u8, fr.buffer, fr.seek, &.{ |
| 894 | 459 | zig_padding_abbrev_code, 0, |
| 895 | | }) orelse fbr.buffer.len; |
| 896 | | if (fbr.seek >= next_unit_pos) break; |
| 460 | }) orelse fr.buffer.len; |
| 461 | if (fr.seek >= next_unit_pos) break; |
| 897 | 462 | var die_obj = (try parseDie( |
| 898 | | &fbr, |
| 463 | &fr, |
| 899 | 464 | attrs_bufs[0], |
| 900 | 465 | abbrev_table, |
| 901 | 466 | unit_header.format, |
| ... | ... | @@ -920,30 +485,30 @@ fn scanAllFunctions(di: *Dwarf, allocator: Allocator) ScanError!void { |
| 920 | 485 | // Prevent endless loops |
| 921 | 486 | for (0..3) |_| { |
| 922 | 487 | if (this_die_obj.getAttr(AT.name)) |_| { |
| 923 | | break :x try this_die_obj.getAttrString(di, AT.name, di.section(.debug_str), compile_unit); |
| 488 | break :x try this_die_obj.getAttrString(di, endian, AT.name, di.section(.debug_str), &compile_unit); |
| 924 | 489 | } else if (this_die_obj.getAttr(AT.abstract_origin)) |_| { |
| 925 | | const after_die_offset = fbr.seek; |
| 926 | | defer fbr.seek = after_die_offset; |
| 490 | const after_die_offset = fr.seek; |
| 491 | defer fr.seek = after_die_offset; |
| 927 | 492 | |
| 928 | 493 | // Follow the DIE it points to and repeat |
| 929 | 494 | const ref_offset = try this_die_obj.getAttrRef(AT.abstract_origin, this_unit_offset, next_offset); |
| 930 | | fbr.seek = @intCast(ref_offset); |
| 495 | fr.seek = @intCast(ref_offset); |
| 931 | 496 | this_die_obj = (try parseDie( |
| 932 | | &fbr, |
| 497 | &fr, |
| 933 | 498 | attrs_bufs[2], |
| 934 | 499 | abbrev_table, // wrong abbrev table for different cu |
| 935 | 500 | unit_header.format, |
| 936 | 501 | endian, |
| 937 | 502 | )) orelse return bad(); |
| 938 | 503 | } else if (this_die_obj.getAttr(AT.specification)) |_| { |
| 939 | | const after_die_offset = fbr.seek; |
| 940 | | defer fbr.seek = after_die_offset; |
| 504 | const after_die_offset = fr.seek; |
| 505 | defer fr.seek = after_die_offset; |
| 941 | 506 | |
| 942 | 507 | // Follow the DIE it points to and repeat |
| 943 | 508 | const ref_offset = try this_die_obj.getAttrRef(AT.specification, this_unit_offset, next_offset); |
| 944 | | fbr.seek = @intCast(ref_offset); |
| 509 | fr.seek = @intCast(ref_offset); |
| 945 | 510 | this_die_obj = (try parseDie( |
| 946 | | &fbr, |
| 511 | &fr, |
| 947 | 512 | attrs_bufs[2], |
| 948 | 513 | abbrev_table, // wrong abbrev table for different cu |
| 949 | 514 | unit_header.format, |
| ... | ... | @@ -957,7 +522,7 @@ fn scanAllFunctions(di: *Dwarf, allocator: Allocator) ScanError!void { |
| 957 | 522 | break :x null; |
| 958 | 523 | }; |
| 959 | 524 | |
| 960 | | var range_added = if (die_obj.getAttrAddr(di, AT.low_pc, compile_unit)) |low_pc| blk: { |
| 525 | var range_added = if (die_obj.getAttrAddr(di, endian, AT.low_pc, &compile_unit)) |low_pc| blk: { |
| 961 | 526 | if (die_obj.getAttr(AT.high_pc)) |high_pc_value| { |
| 962 | 527 | const pc_end = switch (high_pc_value.*) { |
| 963 | 528 | .addr => |value| value, |
| ... | ... | @@ -983,7 +548,7 @@ fn scanAllFunctions(di: *Dwarf, allocator: Allocator) ScanError!void { |
| 983 | 548 | }; |
| 984 | 549 | |
| 985 | 550 | if (die_obj.getAttr(AT.ranges)) |ranges_value| blk: { |
| 986 | | var iter = DebugRangeIterator.init(ranges_value, di, &compile_unit) catch |err| { |
| 551 | var iter = DebugRangeIterator.init(ranges_value, di, endian, &compile_unit) catch |err| { |
| 987 | 552 | if (err != error.MissingDebugInfo) return err; |
| 988 | 553 | break :blk; |
| 989 | 554 | }; |
| ... | ... | @@ -1015,34 +580,33 @@ fn scanAllFunctions(di: *Dwarf, allocator: Allocator) ScanError!void { |
| 1015 | 580 | } |
| 1016 | 581 | } |
| 1017 | 582 | |
| 1018 | | fn scanAllCompileUnits(di: *Dwarf, allocator: Allocator) ScanError!void { |
| 1019 | | const endian = di.endian; |
| 1020 | | var fbr: Reader = .fixed(di.section(.debug_info).?); |
| 583 | fn scanAllCompileUnits(di: *Dwarf, allocator: Allocator, endian: Endian) ScanError!void { |
| 584 | var fr: Reader = .fixed(di.section(.debug_info).?); |
| 1021 | 585 | var this_unit_offset: u64 = 0; |
| 1022 | 586 | |
| 1023 | 587 | var attrs_buf = std.array_list.Managed(Die.Attr).init(allocator); |
| 1024 | 588 | defer attrs_buf.deinit(); |
| 1025 | 589 | |
| 1026 | | while (this_unit_offset < fbr.buffer.len) { |
| 1027 | | fbr.seek = @intCast(this_unit_offset); |
| 590 | while (this_unit_offset < fr.buffer.len) { |
| 591 | fr.seek = @intCast(this_unit_offset); |
| 1028 | 592 | |
| 1029 | | const unit_header = try readUnitHeader(&fbr, endian); |
| 593 | const unit_header = try readUnitHeader(&fr, endian); |
| 1030 | 594 | if (unit_header.unit_length == 0) return; |
| 1031 | 595 | const next_offset = unit_header.header_length + unit_header.unit_length; |
| 1032 | 596 | |
| 1033 | | const version = try fbr.takeInt(u16, endian); |
| 597 | const version = try fr.takeInt(u16, endian); |
| 1034 | 598 | if (version < 2 or version > 5) return bad(); |
| 1035 | 599 | |
| 1036 | 600 | var address_size: u8 = undefined; |
| 1037 | 601 | var debug_abbrev_offset: u64 = undefined; |
| 1038 | 602 | if (version >= 5) { |
| 1039 | | const unit_type = try fbr.takeByte(); |
| 603 | const unit_type = try fr.takeByte(); |
| 1040 | 604 | if (unit_type != UT.compile) return bad(); |
| 1041 | | address_size = try fbr.takeByte(); |
| 1042 | | debug_abbrev_offset = try readAddress(&fbr, unit_header.format, endian); |
| 605 | address_size = try fr.takeByte(); |
| 606 | debug_abbrev_offset = try readAddress(&fr, unit_header.format, endian); |
| 1043 | 607 | } else { |
| 1044 | | debug_abbrev_offset = try readAddress(&fbr, unit_header.format, endian); |
| 1045 | | address_size = try fbr.takeByte(); |
| 608 | debug_abbrev_offset = try readAddress(&fr, unit_header.format, endian); |
| 609 | address_size = try fr.takeByte(); |
| 1046 | 610 | } |
| 1047 | 611 | if (address_size != @sizeOf(usize)) return bad(); |
| 1048 | 612 | |
| ... | ... | @@ -1055,7 +619,7 @@ fn scanAllCompileUnits(di: *Dwarf, allocator: Allocator) ScanError!void { |
| 1055 | 619 | try attrs_buf.resize(max_attrs); |
| 1056 | 620 | |
| 1057 | 621 | var compile_unit_die = (try parseDie( |
| 1058 | | &fbr, |
| 622 | &fr, |
| 1059 | 623 | attrs_buf.items, |
| 1060 | 624 | abbrev_table, |
| 1061 | 625 | unit_header.format, |
| ... | ... | @@ -1080,7 +644,7 @@ fn scanAllCompileUnits(di: *Dwarf, allocator: Allocator) ScanError!void { |
| 1080 | 644 | }; |
| 1081 | 645 | |
| 1082 | 646 | compile_unit.pc_range = x: { |
| 1083 | | if (compile_unit_die.getAttrAddr(di, AT.low_pc, compile_unit)) |low_pc| { |
| 647 | if (compile_unit_die.getAttrAddr(di, endian, AT.low_pc, &compile_unit)) |low_pc| { |
| 1084 | 648 | if (compile_unit_die.getAttr(AT.high_pc)) |high_pc_value| { |
| 1085 | 649 | const pc_end = switch (high_pc_value.*) { |
| 1086 | 650 | .addr => |value| value, |
| ... | ... | @@ -1144,10 +708,11 @@ const DebugRangeIterator = struct { |
| 1144 | 708 | base_address: u64, |
| 1145 | 709 | section_type: Section.Id, |
| 1146 | 710 | di: *const Dwarf, |
| 711 | endian: Endian, |
| 1147 | 712 | compile_unit: *const CompileUnit, |
| 1148 | | fbr: Reader, |
| 713 | fr: Reader, |
| 1149 | 714 | |
| 1150 | | pub fn init(ranges_value: *const FormValue, di: *const Dwarf, compile_unit: *const CompileUnit) !@This() { |
| 715 | pub fn init(ranges_value: *const FormValue, di: *const Dwarf, endian: Endian, compile_unit: *const CompileUnit) !@This() { |
| 1151 | 716 | const section_type = if (compile_unit.version >= 5) Section.Id.debug_rnglists else Section.Id.debug_ranges; |
| 1152 | 717 | const debug_ranges = di.section(section_type) orelse return error.MissingDebugInfo; |
| 1153 | 718 | |
| ... | ... | @@ -1158,13 +723,13 @@ const DebugRangeIterator = struct { |
| 1158 | 723 | .@"32" => { |
| 1159 | 724 | const offset_loc = @as(usize, @intCast(compile_unit.rnglists_base + 4 * idx)); |
| 1160 | 725 | if (offset_loc + 4 > debug_ranges.len) return bad(); |
| 1161 | | const offset = mem.readInt(u32, debug_ranges[offset_loc..][0..4], di.endian); |
| 726 | const offset = mem.readInt(u32, debug_ranges[offset_loc..][0..4], endian); |
| 1162 | 727 | break :off compile_unit.rnglists_base + offset; |
| 1163 | 728 | }, |
| 1164 | 729 | .@"64" => { |
| 1165 | 730 | const offset_loc = @as(usize, @intCast(compile_unit.rnglists_base + 8 * idx)); |
| 1166 | 731 | if (offset_loc + 8 > debug_ranges.len) return bad(); |
| 1167 | | const offset = mem.readInt(u64, debug_ranges[offset_loc..][0..8], di.endian); |
| 732 | const offset = mem.readInt(u64, debug_ranges[offset_loc..][0..8], endian); |
| 1168 | 733 | break :off compile_unit.rnglists_base + offset; |
| 1169 | 734 | }, |
| 1170 | 735 | } |
| ... | ... | @@ -1176,42 +741,43 @@ const DebugRangeIterator = struct { |
| 1176 | 741 | // specified by DW_AT.low_pc or to some other value encoded |
| 1177 | 742 | // in the list itself. |
| 1178 | 743 | // If no starting value is specified use zero. |
| 1179 | | const base_address = compile_unit.die.getAttrAddr(di, AT.low_pc, compile_unit.*) catch |err| switch (err) { |
| 744 | const base_address = compile_unit.die.getAttrAddr(di, endian, AT.low_pc, compile_unit) catch |err| switch (err) { |
| 1180 | 745 | error.MissingDebugInfo => 0, |
| 1181 | 746 | else => return err, |
| 1182 | 747 | }; |
| 1183 | 748 | |
| 1184 | | var fbr: Reader = .fixed(debug_ranges); |
| 1185 | | fbr.seek = cast(usize, ranges_offset) orelse return bad(); |
| 749 | var fr: Reader = .fixed(debug_ranges); |
| 750 | fr.seek = cast(usize, ranges_offset) orelse return bad(); |
| 1186 | 751 | |
| 1187 | 752 | return .{ |
| 1188 | 753 | .base_address = base_address, |
| 1189 | 754 | .section_type = section_type, |
| 1190 | 755 | .di = di, |
| 756 | .endian = endian, |
| 1191 | 757 | .compile_unit = compile_unit, |
| 1192 | | .fbr = fbr, |
| 758 | .fr = fr, |
| 1193 | 759 | }; |
| 1194 | 760 | } |
| 1195 | 761 | |
| 1196 | 762 | // Returns the next range in the list, or null if the end was reached. |
| 1197 | 763 | pub fn next(self: *@This()) !?PcRange { |
| 1198 | | const endian = self.di.endian; |
| 764 | const endian = self.endian; |
| 1199 | 765 | switch (self.section_type) { |
| 1200 | 766 | .debug_rnglists => { |
| 1201 | | const kind = try self.fbr.takeByte(); |
| 767 | const kind = try self.fr.takeByte(); |
| 1202 | 768 | switch (kind) { |
| 1203 | 769 | RLE.end_of_list => return null, |
| 1204 | 770 | RLE.base_addressx => { |
| 1205 | | const index = try self.fbr.takeLeb128(usize); |
| 1206 | | self.base_address = try self.di.readDebugAddr(self.compile_unit.*, index); |
| 771 | const index = try self.fr.takeLeb128(usize); |
| 772 | self.base_address = try self.di.readDebugAddr(endian, self.compile_unit, index); |
| 1207 | 773 | return try self.next(); |
| 1208 | 774 | }, |
| 1209 | 775 | RLE.startx_endx => { |
| 1210 | | const start_index = try self.fbr.takeLeb128(usize); |
| 1211 | | const start_addr = try self.di.readDebugAddr(self.compile_unit.*, start_index); |
| 776 | const start_index = try self.fr.takeLeb128(usize); |
| 777 | const start_addr = try self.di.readDebugAddr(endian, self.compile_unit, start_index); |
| 1212 | 778 | |
| 1213 | | const end_index = try self.fbr.takeLeb128(usize); |
| 1214 | | const end_addr = try self.di.readDebugAddr(self.compile_unit.*, end_index); |
| 779 | const end_index = try self.fr.takeLeb128(usize); |
| 780 | const end_addr = try self.di.readDebugAddr(endian, self.compile_unit, end_index); |
| 1215 | 781 | |
| 1216 | 782 | return .{ |
| 1217 | 783 | .start = start_addr, |
| ... | ... | @@ -1219,10 +785,10 @@ const DebugRangeIterator = struct { |
| 1219 | 785 | }; |
| 1220 | 786 | }, |
| 1221 | 787 | RLE.startx_length => { |
| 1222 | | const start_index = try self.fbr.takeLeb128(usize); |
| 1223 | | const start_addr = try self.di.readDebugAddr(self.compile_unit.*, start_index); |
| 788 | const start_index = try self.fr.takeLeb128(usize); |
| 789 | const start_addr = try self.di.readDebugAddr(endian, self.compile_unit, start_index); |
| 1224 | 790 | |
| 1225 | | const len = try self.fbr.takeLeb128(usize); |
| 791 | const len = try self.fr.takeLeb128(usize); |
| 1226 | 792 | const end_addr = start_addr + len; |
| 1227 | 793 | |
| 1228 | 794 | return .{ |
| ... | ... | @@ -1231,8 +797,8 @@ const DebugRangeIterator = struct { |
| 1231 | 797 | }; |
| 1232 | 798 | }, |
| 1233 | 799 | RLE.offset_pair => { |
| 1234 | | const start_addr = try self.fbr.takeLeb128(usize); |
| 1235 | | const end_addr = try self.fbr.takeLeb128(usize); |
| 800 | const start_addr = try self.fr.takeLeb128(usize); |
| 801 | const end_addr = try self.fr.takeLeb128(usize); |
| 1236 | 802 | |
| 1237 | 803 | // This is the only kind that uses the base address |
| 1238 | 804 | return .{ |
| ... | ... | @@ -1241,12 +807,12 @@ const DebugRangeIterator = struct { |
| 1241 | 807 | }; |
| 1242 | 808 | }, |
| 1243 | 809 | RLE.base_address => { |
| 1244 | | self.base_address = try self.fbr.takeInt(usize, endian); |
| 810 | self.base_address = try self.fr.takeInt(usize, endian); |
| 1245 | 811 | return try self.next(); |
| 1246 | 812 | }, |
| 1247 | 813 | RLE.start_end => { |
| 1248 | | const start_addr = try self.fbr.takeInt(usize, endian); |
| 1249 | | const end_addr = try self.fbr.takeInt(usize, endian); |
| 814 | const start_addr = try self.fr.takeInt(usize, endian); |
| 815 | const end_addr = try self.fr.takeInt(usize, endian); |
| 1250 | 816 | |
| 1251 | 817 | return .{ |
| 1252 | 818 | .start = start_addr, |
| ... | ... | @@ -1254,8 +820,8 @@ const DebugRangeIterator = struct { |
| 1254 | 820 | }; |
| 1255 | 821 | }, |
| 1256 | 822 | RLE.start_length => { |
| 1257 | | const start_addr = try self.fbr.takeInt(usize, endian); |
| 1258 | | const len = try self.fbr.takeLeb128(usize); |
| 823 | const start_addr = try self.fr.takeInt(usize, endian); |
| 824 | const len = try self.fr.takeLeb128(usize); |
| 1259 | 825 | const end_addr = start_addr + len; |
| 1260 | 826 | |
| 1261 | 827 | return .{ |
| ... | ... | @@ -1267,8 +833,8 @@ const DebugRangeIterator = struct { |
| 1267 | 833 | } |
| 1268 | 834 | }, |
| 1269 | 835 | .debug_ranges => { |
| 1270 | | const start_addr = try self.fbr.takeInt(usize, endian); |
| 1271 | | const end_addr = try self.fbr.takeInt(usize, endian); |
| 836 | const start_addr = try self.fr.takeInt(usize, endian); |
| 837 | const end_addr = try self.fr.takeInt(usize, endian); |
| 1272 | 838 | if (start_addr == 0 and end_addr == 0) return null; |
| 1273 | 839 | |
| 1274 | 840 | // This entry selects a new value for the base address |
| ... | ... | @@ -1288,14 +854,14 @@ const DebugRangeIterator = struct { |
| 1288 | 854 | }; |
| 1289 | 855 | |
| 1290 | 856 | /// TODO: change this to binary searching the sorted compile unit list |
| 1291 | | pub fn findCompileUnit(di: *const Dwarf, target_address: u64) !*CompileUnit { |
| 857 | pub fn findCompileUnit(di: *const Dwarf, endian: Endian, target_address: u64) !*CompileUnit { |
| 1292 | 858 | for (di.compile_unit_list.items) |*compile_unit| { |
| 1293 | 859 | if (compile_unit.pc_range) |range| { |
| 1294 | 860 | if (target_address >= range.start and target_address < range.end) return compile_unit; |
| 1295 | 861 | } |
| 1296 | 862 | |
| 1297 | 863 | const ranges_value = compile_unit.die.getAttr(AT.ranges) orelse continue; |
| 1298 | | var iter = DebugRangeIterator.init(ranges_value, di, compile_unit) catch continue; |
| 864 | var iter = DebugRangeIterator.init(ranges_value, di, endian, compile_unit) catch continue; |
| 1299 | 865 | while (try iter.next()) |range| { |
| 1300 | 866 | if (target_address >= range.start and target_address < range.end) return compile_unit; |
| 1301 | 867 | } |
| ... | ... | @@ -1320,8 +886,8 @@ fn getAbbrevTable(di: *Dwarf, allocator: Allocator, abbrev_offset: u64) !*const |
| 1320 | 886 | } |
| 1321 | 887 | |
| 1322 | 888 | fn parseAbbrevTable(di: *Dwarf, allocator: Allocator, offset: u64) !Abbrev.Table { |
| 1323 | | var fbr: Reader = .fixed(di.section(.debug_abbrev).?); |
| 1324 | | fbr.seek = cast(usize, offset) orelse return bad(); |
| 889 | var fr: Reader = .fixed(di.section(.debug_abbrev).?); |
| 890 | fr.seek = cast(usize, offset) orelse return bad(); |
| 1325 | 891 | |
| 1326 | 892 | var abbrevs = std.array_list.Managed(Abbrev).init(allocator); |
| 1327 | 893 | defer { |
| ... | ... | @@ -1335,20 +901,20 @@ fn parseAbbrevTable(di: *Dwarf, allocator: Allocator, offset: u64) !Abbrev.Table |
| 1335 | 901 | defer attrs.deinit(); |
| 1336 | 902 | |
| 1337 | 903 | while (true) { |
| 1338 | | const code = try fbr.takeLeb128(u64); |
| 904 | const code = try fr.takeLeb128(u64); |
| 1339 | 905 | if (code == 0) break; |
| 1340 | | const tag_id = try fbr.takeLeb128(u64); |
| 1341 | | const has_children = (try fbr.takeByte()) == DW.CHILDREN.yes; |
| 906 | const tag_id = try fr.takeLeb128(u64); |
| 907 | const has_children = (try fr.takeByte()) == DW.CHILDREN.yes; |
| 1342 | 908 | |
| 1343 | 909 | while (true) { |
| 1344 | | const attr_id = try fbr.takeLeb128(u64); |
| 1345 | | const form_id = try fbr.takeLeb128(u64); |
| 910 | const attr_id = try fr.takeLeb128(u64); |
| 911 | const form_id = try fr.takeLeb128(u64); |
| 1346 | 912 | if (attr_id == 0 and form_id == 0) break; |
| 1347 | 913 | try attrs.append(.{ |
| 1348 | 914 | .id = attr_id, |
| 1349 | 915 | .form_id = form_id, |
| 1350 | 916 | .payload = switch (form_id) { |
| 1351 | | FORM.implicit_const => try fbr.takeLeb128(i64), |
| 917 | FORM.implicit_const => try fr.takeLeb128(i64), |
| 1352 | 918 | else => undefined, |
| 1353 | 919 | }, |
| 1354 | 920 | }); |
| ... | ... | @@ -1369,20 +935,20 @@ fn parseAbbrevTable(di: *Dwarf, allocator: Allocator, offset: u64) !Abbrev.Table |
| 1369 | 935 | } |
| 1370 | 936 | |
| 1371 | 937 | fn parseDie( |
| 1372 | | fbr: *Reader, |
| 938 | fr: *Reader, |
| 1373 | 939 | attrs_buf: []Die.Attr, |
| 1374 | 940 | abbrev_table: *const Abbrev.Table, |
| 1375 | 941 | format: Format, |
| 1376 | 942 | endian: Endian, |
| 1377 | 943 | ) ScanError!?Die { |
| 1378 | | const abbrev_code = try fbr.takeLeb128(u64); |
| 944 | const abbrev_code = try fr.takeLeb128(u64); |
| 1379 | 945 | if (abbrev_code == 0) return null; |
| 1380 | 946 | const table_entry = abbrev_table.get(abbrev_code) orelse return bad(); |
| 1381 | 947 | |
| 1382 | 948 | const attrs = attrs_buf[0..table_entry.attrs.len]; |
| 1383 | 949 | for (attrs, table_entry.attrs) |*result_attr, attr| result_attr.* = .{ |
| 1384 | 950 | .id = attr.id, |
| 1385 | | .value = try parseFormValue(fbr, attr.form_id, format, endian, attr.payload), |
| 951 | .value = try parseFormValue(fr, attr.form_id, format, endian, attr.payload), |
| 1386 | 952 | }; |
| 1387 | 953 | return .{ |
| 1388 | 954 | .tag_id = table_entry.tag_id, |
| ... | ... | @@ -1392,25 +958,24 @@ fn parseDie( |
| 1392 | 958 | } |
| 1393 | 959 | |
| 1394 | 960 | /// Ensures that addresses in the returned LineTable are monotonically increasing. |
| 1395 | | fn runLineNumberProgram(d: *Dwarf, gpa: Allocator, compile_unit: *CompileUnit) !CompileUnit.SrcLocCache { |
| 1396 | | const endian = d.endian; |
| 1397 | | const compile_unit_cwd = try compile_unit.die.getAttrString(d, AT.comp_dir, d.section(.debug_line_str), compile_unit.*); |
| 961 | fn runLineNumberProgram(d: *Dwarf, gpa: Allocator, endian: Endian, compile_unit: *const CompileUnit) !CompileUnit.SrcLocCache { |
| 962 | const compile_unit_cwd = try compile_unit.die.getAttrString(d, endian, AT.comp_dir, d.section(.debug_line_str), compile_unit); |
| 1398 | 963 | const line_info_offset = try compile_unit.die.getAttrSecOffset(AT.stmt_list); |
| 1399 | 964 | |
| 1400 | | var fbr: Reader = .fixed(d.section(.debug_line).?); |
| 1401 | | fbr.seek = @intCast(line_info_offset); |
| 965 | var fr: Reader = .fixed(d.section(.debug_line).?); |
| 966 | fr.seek = @intCast(line_info_offset); |
| 1402 | 967 | |
| 1403 | | const unit_header = try readUnitHeader(&fbr, endian); |
| 968 | const unit_header = try readUnitHeader(&fr, endian); |
| 1404 | 969 | if (unit_header.unit_length == 0) return missing(); |
| 1405 | 970 | |
| 1406 | 971 | const next_offset = unit_header.header_length + unit_header.unit_length; |
| 1407 | 972 | |
| 1408 | | const version = try fbr.takeInt(u16, endian); |
| 973 | const version = try fr.takeInt(u16, endian); |
| 1409 | 974 | if (version < 2) return bad(); |
| 1410 | 975 | |
| 1411 | 976 | const addr_size: u8, const seg_size: u8 = if (version >= 5) .{ |
| 1412 | | try fbr.takeByte(), |
| 1413 | | try fbr.takeByte(), |
| 977 | try fr.takeByte(), |
| 978 | try fr.takeByte(), |
| 1414 | 979 | } else .{ |
| 1415 | 980 | switch (unit_header.format) { |
| 1416 | 981 | .@"32" => 4, |
| ... | ... | @@ -1421,26 +986,26 @@ fn runLineNumberProgram(d: *Dwarf, gpa: Allocator, compile_unit: *CompileUnit) ! |
| 1421 | 986 | _ = addr_size; |
| 1422 | 987 | _ = seg_size; |
| 1423 | 988 | |
| 1424 | | const prologue_length = try readAddress(&fbr, unit_header.format, endian); |
| 1425 | | const prog_start_offset = fbr.seek + prologue_length; |
| 989 | const prologue_length = try readAddress(&fr, unit_header.format, endian); |
| 990 | const prog_start_offset = fr.seek + prologue_length; |
| 1426 | 991 | |
| 1427 | | const minimum_instruction_length = try fbr.takeByte(); |
| 992 | const minimum_instruction_length = try fr.takeByte(); |
| 1428 | 993 | if (minimum_instruction_length == 0) return bad(); |
| 1429 | 994 | |
| 1430 | 995 | if (version >= 4) { |
| 1431 | | const maximum_operations_per_instruction = try fbr.takeByte(); |
| 996 | const maximum_operations_per_instruction = try fr.takeByte(); |
| 1432 | 997 | _ = maximum_operations_per_instruction; |
| 1433 | 998 | } |
| 1434 | 999 | |
| 1435 | | const default_is_stmt = (try fbr.takeByte()) != 0; |
| 1436 | | const line_base = try fbr.takeByteSigned(); |
| 1000 | const default_is_stmt = (try fr.takeByte()) != 0; |
| 1001 | const line_base = try fr.takeByteSigned(); |
| 1437 | 1002 | |
| 1438 | | const line_range = try fbr.takeByte(); |
| 1003 | const line_range = try fr.takeByte(); |
| 1439 | 1004 | if (line_range == 0) return bad(); |
| 1440 | 1005 | |
| 1441 | | const opcode_base = try fbr.takeByte(); |
| 1006 | const opcode_base = try fr.takeByte(); |
| 1442 | 1007 | |
| 1443 | | const standard_opcode_lengths = try fbr.take(opcode_base - 1); |
| 1008 | const standard_opcode_lengths = try fr.take(opcode_base - 1); |
| 1444 | 1009 | |
| 1445 | 1010 | var directories: ArrayList(FileEntry) = .empty; |
| 1446 | 1011 | defer directories.deinit(gpa); |
| ... | ... | @@ -1451,17 +1016,17 @@ fn runLineNumberProgram(d: *Dwarf, gpa: Allocator, compile_unit: *CompileUnit) ! |
| 1451 | 1016 | try directories.append(gpa, .{ .path = compile_unit_cwd }); |
| 1452 | 1017 | |
| 1453 | 1018 | while (true) { |
| 1454 | | const dir = try fbr.takeSentinel(0); |
| 1019 | const dir = try fr.takeSentinel(0); |
| 1455 | 1020 | if (dir.len == 0) break; |
| 1456 | 1021 | try directories.append(gpa, .{ .path = dir }); |
| 1457 | 1022 | } |
| 1458 | 1023 | |
| 1459 | 1024 | while (true) { |
| 1460 | | const file_name = try fbr.takeSentinel(0); |
| 1025 | const file_name = try fr.takeSentinel(0); |
| 1461 | 1026 | if (file_name.len == 0) break; |
| 1462 | | const dir_index = try fbr.takeLeb128(u32); |
| 1463 | | const mtime = try fbr.takeLeb128(u64); |
| 1464 | | const size = try fbr.takeLeb128(u64); |
| 1027 | const dir_index = try fr.takeLeb128(u32); |
| 1028 | const mtime = try fr.takeLeb128(u64); |
| 1029 | const size = try fr.takeLeb128(u64); |
| 1465 | 1030 | try file_entries.append(gpa, .{ |
| 1466 | 1031 | .path = file_name, |
| 1467 | 1032 | .dir_index = dir_index, |
| ... | ... | @@ -1476,21 +1041,21 @@ fn runLineNumberProgram(d: *Dwarf, gpa: Allocator, compile_unit: *CompileUnit) ! |
| 1476 | 1041 | }; |
| 1477 | 1042 | { |
| 1478 | 1043 | var dir_ent_fmt_buf: [10]FileEntFmt = undefined; |
| 1479 | | const directory_entry_format_count = try fbr.takeByte(); |
| 1044 | const directory_entry_format_count = try fr.takeByte(); |
| 1480 | 1045 | if (directory_entry_format_count > dir_ent_fmt_buf.len) return bad(); |
| 1481 | 1046 | for (dir_ent_fmt_buf[0..directory_entry_format_count]) |*ent_fmt| { |
| 1482 | 1047 | ent_fmt.* = .{ |
| 1483 | | .content_type_code = try fbr.takeLeb128(u8), |
| 1484 | | .form_code = try fbr.takeLeb128(u16), |
| 1048 | .content_type_code = try fr.takeLeb128(u8), |
| 1049 | .form_code = try fr.takeLeb128(u16), |
| 1485 | 1050 | }; |
| 1486 | 1051 | } |
| 1487 | 1052 | |
| 1488 | | const directories_count = try fbr.takeLeb128(usize); |
| 1053 | const directories_count = try fr.takeLeb128(usize); |
| 1489 | 1054 | |
| 1490 | 1055 | for (try directories.addManyAsSlice(gpa, directories_count)) |*e| { |
| 1491 | 1056 | e.* = .{ .path = &.{} }; |
| 1492 | 1057 | for (dir_ent_fmt_buf[0..directory_entry_format_count]) |ent_fmt| { |
| 1493 | | const form_value = try parseFormValue(&fbr, ent_fmt.form_code, unit_header.format, endian, null); |
| 1058 | const form_value = try parseFormValue(&fr, ent_fmt.form_code, unit_header.format, endian, null); |
| 1494 | 1059 | switch (ent_fmt.content_type_code) { |
| 1495 | 1060 | DW.LNCT.path => e.path = try form_value.getString(d.*), |
| 1496 | 1061 | DW.LNCT.directory_index => e.dir_index = try form_value.getUInt(u32), |
| ... | ... | @@ -1507,22 +1072,22 @@ fn runLineNumberProgram(d: *Dwarf, gpa: Allocator, compile_unit: *CompileUnit) ! |
| 1507 | 1072 | } |
| 1508 | 1073 | |
| 1509 | 1074 | var file_ent_fmt_buf: [10]FileEntFmt = undefined; |
| 1510 | | const file_name_entry_format_count = try fbr.takeByte(); |
| 1075 | const file_name_entry_format_count = try fr.takeByte(); |
| 1511 | 1076 | if (file_name_entry_format_count > file_ent_fmt_buf.len) return bad(); |
| 1512 | 1077 | for (file_ent_fmt_buf[0..file_name_entry_format_count]) |*ent_fmt| { |
| 1513 | 1078 | ent_fmt.* = .{ |
| 1514 | | .content_type_code = try fbr.takeLeb128(u16), |
| 1515 | | .form_code = try fbr.takeLeb128(u16), |
| 1079 | .content_type_code = try fr.takeLeb128(u16), |
| 1080 | .form_code = try fr.takeLeb128(u16), |
| 1516 | 1081 | }; |
| 1517 | 1082 | } |
| 1518 | 1083 | |
| 1519 | | const file_names_count = try fbr.takeLeb128(usize); |
| 1084 | const file_names_count = try fr.takeLeb128(usize); |
| 1520 | 1085 | try file_entries.ensureUnusedCapacity(gpa, file_names_count); |
| 1521 | 1086 | |
| 1522 | 1087 | for (try file_entries.addManyAsSlice(gpa, file_names_count)) |*e| { |
| 1523 | 1088 | e.* = .{ .path = &.{} }; |
| 1524 | 1089 | for (file_ent_fmt_buf[0..file_name_entry_format_count]) |ent_fmt| { |
| 1525 | | const form_value = try parseFormValue(&fbr, ent_fmt.form_code, unit_header.format, endian, null); |
| 1090 | const form_value = try parseFormValue(&fr, ent_fmt.form_code, unit_header.format, endian, null); |
| 1526 | 1091 | switch (ent_fmt.content_type_code) { |
| 1527 | 1092 | DW.LNCT.path => e.path = try form_value.getString(d.*), |
| 1528 | 1093 | DW.LNCT.directory_index => e.dir_index = try form_value.getUInt(u32), |
| ... | ... | @@ -1542,17 +1107,17 @@ fn runLineNumberProgram(d: *Dwarf, gpa: Allocator, compile_unit: *CompileUnit) ! |
| 1542 | 1107 | var line_table: CompileUnit.SrcLocCache.LineTable = .{}; |
| 1543 | 1108 | errdefer line_table.deinit(gpa); |
| 1544 | 1109 | |
| 1545 | | fbr.seek = @intCast(prog_start_offset); |
| 1110 | fr.seek = @intCast(prog_start_offset); |
| 1546 | 1111 | |
| 1547 | 1112 | const next_unit_pos = line_info_offset + next_offset; |
| 1548 | 1113 | |
| 1549 | | while (fbr.seek < next_unit_pos) { |
| 1550 | | const opcode = try fbr.takeByte(); |
| 1114 | while (fr.seek < next_unit_pos) { |
| 1115 | const opcode = try fr.takeByte(); |
| 1551 | 1116 | |
| 1552 | 1117 | if (opcode == DW.LNS.extended_op) { |
| 1553 | | const op_size = try fbr.takeLeb128(u64); |
| 1118 | const op_size = try fr.takeLeb128(u64); |
| 1554 | 1119 | if (op_size < 1) return bad(); |
| 1555 | | const sub_op = try fbr.takeByte(); |
| 1120 | const sub_op = try fr.takeByte(); |
| 1556 | 1121 | switch (sub_op) { |
| 1557 | 1122 | DW.LNE.end_sequence => { |
| 1558 | 1123 | // The row being added here is an "end" address, meaning |
| ... | ... | @@ -1571,14 +1136,14 @@ fn runLineNumberProgram(d: *Dwarf, gpa: Allocator, compile_unit: *CompileUnit) ! |
| 1571 | 1136 | prog.reset(); |
| 1572 | 1137 | }, |
| 1573 | 1138 | DW.LNE.set_address => { |
| 1574 | | const addr = try fbr.takeInt(usize, endian); |
| 1139 | const addr = try fr.takeInt(usize, endian); |
| 1575 | 1140 | prog.address = addr; |
| 1576 | 1141 | }, |
| 1577 | 1142 | DW.LNE.define_file => { |
| 1578 | | const path = try fbr.takeSentinel(0); |
| 1579 | | const dir_index = try fbr.takeLeb128(u32); |
| 1580 | | const mtime = try fbr.takeLeb128(u64); |
| 1581 | | const size = try fbr.takeLeb128(u64); |
| 1143 | const path = try fr.takeSentinel(0); |
| 1144 | const dir_index = try fr.takeLeb128(u32); |
| 1145 | const mtime = try fr.takeLeb128(u64); |
| 1146 | const size = try fr.takeLeb128(u64); |
| 1582 | 1147 | try file_entries.append(gpa, .{ |
| 1583 | 1148 | .path = path, |
| 1584 | 1149 | .dir_index = dir_index, |
| ... | ... | @@ -1586,7 +1151,7 @@ fn runLineNumberProgram(d: *Dwarf, gpa: Allocator, compile_unit: *CompileUnit) ! |
| 1586 | 1151 | .size = size, |
| 1587 | 1152 | }); |
| 1588 | 1153 | }, |
| 1589 | | else => try fbr.discardAll64(op_size - 1), |
| 1154 | else => try fr.discardAll64(op_size - 1), |
| 1590 | 1155 | } |
| 1591 | 1156 | } else if (opcode >= opcode_base) { |
| 1592 | 1157 | // special opcodes |
| ... | ... | @@ -1604,19 +1169,19 @@ fn runLineNumberProgram(d: *Dwarf, gpa: Allocator, compile_unit: *CompileUnit) ! |
| 1604 | 1169 | prog.basic_block = false; |
| 1605 | 1170 | }, |
| 1606 | 1171 | DW.LNS.advance_pc => { |
| 1607 | | const arg = try fbr.takeLeb128(usize); |
| 1172 | const arg = try fr.takeLeb128(usize); |
| 1608 | 1173 | prog.address += arg * minimum_instruction_length; |
| 1609 | 1174 | }, |
| 1610 | 1175 | DW.LNS.advance_line => { |
| 1611 | | const arg = try fbr.takeLeb128(i64); |
| 1176 | const arg = try fr.takeLeb128(i64); |
| 1612 | 1177 | prog.line += arg; |
| 1613 | 1178 | }, |
| 1614 | 1179 | DW.LNS.set_file => { |
| 1615 | | const arg = try fbr.takeLeb128(usize); |
| 1180 | const arg = try fr.takeLeb128(usize); |
| 1616 | 1181 | prog.file = arg; |
| 1617 | 1182 | }, |
| 1618 | 1183 | DW.LNS.set_column => { |
| 1619 | | const arg = try fbr.takeLeb128(u64); |
| 1184 | const arg = try fr.takeLeb128(u64); |
| 1620 | 1185 | prog.column = arg; |
| 1621 | 1186 | }, |
| 1622 | 1187 | DW.LNS.negate_stmt => { |
| ... | ... | @@ -1630,13 +1195,13 @@ fn runLineNumberProgram(d: *Dwarf, gpa: Allocator, compile_unit: *CompileUnit) ! |
| 1630 | 1195 | prog.address += inc_addr; |
| 1631 | 1196 | }, |
| 1632 | 1197 | DW.LNS.fixed_advance_pc => { |
| 1633 | | const arg = try fbr.takeInt(u16, endian); |
| 1198 | const arg = try fr.takeInt(u16, endian); |
| 1634 | 1199 | prog.address += arg; |
| 1635 | 1200 | }, |
| 1636 | 1201 | DW.LNS.set_prologue_end => {}, |
| 1637 | 1202 | else => { |
| 1638 | 1203 | if (opcode - 1 >= standard_opcode_lengths.len) return bad(); |
| 1639 | | try fbr.discardAll(standard_opcode_lengths[opcode - 1]); |
| 1204 | try fr.discardAll(standard_opcode_lengths[opcode - 1]); |
| 1640 | 1205 | }, |
| 1641 | 1206 | } |
| 1642 | 1207 | } |
| ... | ... | @@ -1661,18 +1226,19 @@ fn runLineNumberProgram(d: *Dwarf, gpa: Allocator, compile_unit: *CompileUnit) ! |
| 1661 | 1226 | }; |
| 1662 | 1227 | } |
| 1663 | 1228 | |
| 1664 | | pub fn populateSrcLocCache(d: *Dwarf, gpa: Allocator, cu: *CompileUnit) ScanError!void { |
| 1229 | pub fn populateSrcLocCache(d: *Dwarf, gpa: Allocator, endian: Endian, cu: *CompileUnit) ScanError!void { |
| 1665 | 1230 | if (cu.src_loc_cache != null) return; |
| 1666 | | cu.src_loc_cache = try runLineNumberProgram(d, gpa, cu); |
| 1231 | cu.src_loc_cache = try d.runLineNumberProgram(gpa, endian, cu); |
| 1667 | 1232 | } |
| 1668 | 1233 | |
| 1669 | 1234 | pub fn getLineNumberInfo( |
| 1670 | 1235 | d: *Dwarf, |
| 1671 | 1236 | gpa: Allocator, |
| 1237 | endian: Endian, |
| 1672 | 1238 | compile_unit: *CompileUnit, |
| 1673 | 1239 | target_address: u64, |
| 1674 | 1240 | ) !std.debug.SourceLocation { |
| 1675 | | try populateSrcLocCache(d, gpa, compile_unit); |
| 1241 | try d.populateSrcLocCache(gpa, endian, compile_unit); |
| 1676 | 1242 | const slc = &compile_unit.src_loc_cache.?; |
| 1677 | 1243 | const entry = try slc.findSource(target_address); |
| 1678 | 1244 | const file_index = entry.file - @intFromBool(slc.version < 5); |
| ... | ... | @@ -1696,7 +1262,7 @@ fn getLineString(di: Dwarf, offset: u64) ![:0]const u8 { |
| 1696 | 1262 | return getStringGeneric(di.section(.debug_line_str), offset); |
| 1697 | 1263 | } |
| 1698 | 1264 | |
| 1699 | | fn readDebugAddr(di: Dwarf, compile_unit: CompileUnit, index: u64) !u64 { |
| 1265 | fn readDebugAddr(di: Dwarf, endian: Endian, compile_unit: *const CompileUnit, index: u64) !u64 { |
| 1700 | 1266 | const debug_addr = di.section(.debug_addr) orelse return bad(); |
| 1701 | 1267 | |
| 1702 | 1268 | // addr_base points to the first item after the header, however we |
| ... | ... | @@ -1705,7 +1271,7 @@ fn readDebugAddr(di: Dwarf, compile_unit: CompileUnit, index: u64) !u64 { |
| 1705 | 1271 | // The header is 8 or 12 bytes depending on is_64. |
| 1706 | 1272 | if (compile_unit.addr_base < 8) return bad(); |
| 1707 | 1273 | |
| 1708 | | const version = mem.readInt(u16, debug_addr[compile_unit.addr_base - 4 ..][0..2], di.endian); |
| 1274 | const version = mem.readInt(u16, debug_addr[compile_unit.addr_base - 4 ..][0..2], endian); |
| 1709 | 1275 | if (version != 5) return bad(); |
| 1710 | 1276 | |
| 1711 | 1277 | const addr_size = debug_addr[compile_unit.addr_base - 2]; |
| ... | ... | @@ -1715,113 +1281,13 @@ fn readDebugAddr(di: Dwarf, compile_unit: CompileUnit, index: u64) !u64 { |
| 1715 | 1281 | if (byte_offset + addr_size > debug_addr.len) return bad(); |
| 1716 | 1282 | return switch (addr_size) { |
| 1717 | 1283 | 1 => debug_addr[byte_offset], |
| 1718 | | 2 => mem.readInt(u16, debug_addr[byte_offset..][0..2], di.endian), |
| 1719 | | 4 => mem.readInt(u32, debug_addr[byte_offset..][0..4], di.endian), |
| 1720 | | 8 => mem.readInt(u64, debug_addr[byte_offset..][0..8], di.endian), |
| 1284 | 2 => mem.readInt(u16, debug_addr[byte_offset..][0..2], endian), |
| 1285 | 4 => mem.readInt(u32, debug_addr[byte_offset..][0..4], endian), |
| 1286 | 8 => mem.readInt(u64, debug_addr[byte_offset..][0..8], endian), |
| 1721 | 1287 | else => bad(), |
| 1722 | 1288 | }; |
| 1723 | 1289 | } |
| 1724 | 1290 | |
| 1725 | | /// If `.eh_frame_hdr` is present, then only the header needs to be parsed. Otherwise, `.eh_frame` |
| 1726 | | /// and `.debug_frame` are scanned and a sorted list of FDEs is built for binary searching during |
| 1727 | | /// unwinding. Even if `.eh_frame_hdr` is used, we may find during unwinding that it's incomplete, |
| 1728 | | /// in which case we build the sorted list of FDEs at that point. |
| 1729 | | /// |
| 1730 | | /// See also `scanCieFdeInfo`. |
| 1731 | | pub fn scanAllUnwindInfo(di: *Dwarf, allocator: Allocator, base_address: usize) !void { |
| 1732 | | const endian = di.endian; |
| 1733 | | |
| 1734 | | if (di.section(.eh_frame_hdr)) |eh_frame_hdr| blk: { |
| 1735 | | var fbr: Reader = .fixed(eh_frame_hdr); |
| 1736 | | |
| 1737 | | const version = try fbr.takeByte(); |
| 1738 | | if (version != 1) break :blk; |
| 1739 | | |
| 1740 | | const eh_frame_ptr_enc = try fbr.takeByte(); |
| 1741 | | if (eh_frame_ptr_enc == EH.PE.omit) break :blk; |
| 1742 | | const fde_count_enc = try fbr.takeByte(); |
| 1743 | | if (fde_count_enc == EH.PE.omit) break :blk; |
| 1744 | | const table_enc = try fbr.takeByte(); |
| 1745 | | if (table_enc == EH.PE.omit) break :blk; |
| 1746 | | |
| 1747 | | const eh_frame_ptr = cast(usize, try readEhPointer(&fbr, eh_frame_ptr_enc, @sizeOf(usize), .{ |
| 1748 | | .pc_rel_base = @intFromPtr(&eh_frame_hdr[fbr.seek]), |
| 1749 | | .follow_indirect = true, |
| 1750 | | }, endian) orelse return bad()) orelse return bad(); |
| 1751 | | |
| 1752 | | const fde_count = cast(usize, try readEhPointer(&fbr, fde_count_enc, @sizeOf(usize), .{ |
| 1753 | | .pc_rel_base = @intFromPtr(&eh_frame_hdr[fbr.seek]), |
| 1754 | | .follow_indirect = true, |
| 1755 | | }, endian) orelse return bad()) orelse return bad(); |
| 1756 | | |
| 1757 | | const entry_size = try ExceptionFrameHeader.entrySize(table_enc); |
| 1758 | | const entries_len = fde_count * entry_size; |
| 1759 | | if (entries_len > eh_frame_hdr.len - fbr.seek) return bad(); |
| 1760 | | |
| 1761 | | di.eh_frame_hdr = .{ |
| 1762 | | .eh_frame_ptr = eh_frame_ptr, |
| 1763 | | .table_enc = table_enc, |
| 1764 | | .fde_count = fde_count, |
| 1765 | | .entries = eh_frame_hdr[fbr.seek..][0..entries_len], |
| 1766 | | }; |
| 1767 | | |
| 1768 | | // No need to scan .eh_frame, we have a binary search table already |
| 1769 | | return; |
| 1770 | | } |
| 1771 | | |
| 1772 | | try di.scanCieFdeInfo(allocator, base_address); |
| 1773 | | } |
| 1774 | | |
| 1775 | | /// Scan `.eh_frame` and `.debug_frame` and build a sorted list of FDEs for binary searching during |
| 1776 | | /// unwinding. |
| 1777 | | pub fn scanCieFdeInfo(di: *Dwarf, allocator: Allocator, base_address: usize) !void { |
| 1778 | | const endian = di.endian; |
| 1779 | | const frame_sections = [2]Section.Id{ .eh_frame, .debug_frame }; |
| 1780 | | for (frame_sections) |frame_section| { |
| 1781 | | if (di.section(frame_section)) |section_data| { |
| 1782 | | var fbr: Reader = .fixed(section_data); |
| 1783 | | while (fbr.seek < fbr.buffer.len) { |
| 1784 | | const entry_header = try EntryHeader.read(&fbr, frame_section, endian); |
| 1785 | | switch (entry_header.type) { |
| 1786 | | .cie => { |
| 1787 | | const cie = try CommonInformationEntry.parse( |
| 1788 | | entry_header.entry_bytes, |
| 1789 | | di.sectionVirtualOffset(frame_section, base_address).?, |
| 1790 | | true, |
| 1791 | | entry_header.format, |
| 1792 | | frame_section, |
| 1793 | | entry_header.length_offset, |
| 1794 | | @sizeOf(usize), |
| 1795 | | di.endian, |
| 1796 | | ); |
| 1797 | | try di.cie_map.put(allocator, entry_header.length_offset, cie); |
| 1798 | | }, |
| 1799 | | .fde => |cie_offset| { |
| 1800 | | const cie = di.cie_map.get(cie_offset) orelse return bad(); |
| 1801 | | const fde = try FrameDescriptionEntry.parse( |
| 1802 | | entry_header.entry_bytes, |
| 1803 | | di.sectionVirtualOffset(frame_section, base_address).?, |
| 1804 | | true, |
| 1805 | | cie, |
| 1806 | | @sizeOf(usize), |
| 1807 | | di.endian, |
| 1808 | | ); |
| 1809 | | try di.fde_list.append(allocator, fde); |
| 1810 | | }, |
| 1811 | | .terminator => break, |
| 1812 | | } |
| 1813 | | } |
| 1814 | | |
| 1815 | | std.mem.sortUnstable(FrameDescriptionEntry, di.fde_list.items, {}, struct { |
| 1816 | | fn lessThan(ctx: void, a: FrameDescriptionEntry, b: FrameDescriptionEntry) bool { |
| 1817 | | _ = ctx; |
| 1818 | | return a.pc_begin < b.pc_begin; |
| 1819 | | } |
| 1820 | | }.lessThan); |
| 1821 | | } |
| 1822 | | } |
| 1823 | | } |
| 1824 | | |
| 1825 | 1291 | fn parseFormValue( |
| 1826 | 1292 | r: *Reader, |
| 1827 | 1293 | form_id: u64, |
| ... | ... | @@ -1946,7 +1412,7 @@ const UnitHeader = struct { |
| 1946 | 1412 | unit_length: u64, |
| 1947 | 1413 | }; |
| 1948 | 1414 | |
| 1949 | | fn readUnitHeader(r: *Reader, endian: Endian) ScanError!UnitHeader { |
| 1415 | pub fn readUnitHeader(r: *Reader, endian: Endian) ScanError!UnitHeader { |
| 1950 | 1416 | return switch (try r.takeInt(u32, endian)) { |
| 1951 | 1417 | 0...0xfffffff0 - 1 => |unit_length| .{ |
| 1952 | 1418 | .format = .@"32", |
| ... | ... | @@ -1986,7 +1452,7 @@ fn invalidDebugInfoDetected() void { |
| 1986 | 1452 | if (debug_debug_mode) @panic("bad dwarf"); |
| 1987 | 1453 | } |
| 1988 | 1454 | |
| 1989 | | fn missing() error{MissingDebugInfo} { |
| 1455 | pub fn missing() error{MissingDebugInfo} { |
| 1990 | 1456 | if (debug_debug_mode) @panic("missing dwarf"); |
| 1991 | 1457 | return error.MissingDebugInfo; |
| 1992 | 1458 | } |
| ... | ... | @@ -2000,94 +1466,39 @@ fn getStringGeneric(opt_str: ?[]const u8, offset: u64) ![:0]const u8 { |
| 2000 | 1466 | return str[casted_offset..last :0]; |
| 2001 | 1467 | } |
| 2002 | 1468 | |
| 2003 | | const EhPointerContext = struct { |
| 2004 | | // The address of the pointer field itself |
| 2005 | | pc_rel_base: u64, |
| 2006 | | |
| 2007 | | // Whether or not to follow indirect pointers. This should only be |
| 2008 | | // used when decoding pointers at runtime using the current process's |
| 2009 | | // debug info |
| 2010 | | follow_indirect: bool, |
| 2011 | | |
| 2012 | | // These relative addressing modes are only used in specific cases, and |
| 2013 | | // might not be available / required in all parsing contexts |
| 2014 | | data_rel_base: ?u64 = null, |
| 2015 | | text_rel_base: ?u64 = null, |
| 2016 | | function_rel_base: ?u64 = null, |
| 2017 | | }; |
| 2018 | | |
| 2019 | | fn readEhPointer(fbr: *Reader, enc: u8, addr_size_bytes: u8, ctx: EhPointerContext, endian: Endian) !?u64 { |
| 2020 | | if (enc == EH.PE.omit) return null; |
| 2021 | | |
| 2022 | | const value: union(enum) { |
| 2023 | | signed: i64, |
| 2024 | | unsigned: u64, |
| 2025 | | } = switch (enc & EH.PE.type_mask) { |
| 2026 | | EH.PE.absptr => .{ |
| 2027 | | .unsigned = switch (addr_size_bytes) { |
| 2028 | | 2 => try fbr.takeInt(u16, endian), |
| 2029 | | 4 => try fbr.takeInt(u32, endian), |
| 2030 | | 8 => try fbr.takeInt(u64, endian), |
| 2031 | | else => return error.InvalidAddrSize, |
| 2032 | | }, |
| 2033 | | }, |
| 2034 | | EH.PE.uleb128 => .{ .unsigned = try fbr.takeLeb128(u64) }, |
| 2035 | | EH.PE.udata2 => .{ .unsigned = try fbr.takeInt(u16, endian) }, |
| 2036 | | EH.PE.udata4 => .{ .unsigned = try fbr.takeInt(u32, endian) }, |
| 2037 | | EH.PE.udata8 => .{ .unsigned = try fbr.takeInt(u64, endian) }, |
| 2038 | | EH.PE.sleb128 => .{ .signed = try fbr.takeLeb128(i64) }, |
| 2039 | | EH.PE.sdata2 => .{ .signed = try fbr.takeInt(i16, endian) }, |
| 2040 | | EH.PE.sdata4 => .{ .signed = try fbr.takeInt(i32, endian) }, |
| 2041 | | EH.PE.sdata8 => .{ .signed = try fbr.takeInt(i64, endian) }, |
| 2042 | | else => return bad(), |
| 2043 | | }; |
| 2044 | | |
| 2045 | | const base = switch (enc & EH.PE.rel_mask) { |
| 2046 | | EH.PE.pcrel => ctx.pc_rel_base, |
| 2047 | | EH.PE.textrel => ctx.text_rel_base orelse return error.PointerBaseNotSpecified, |
| 2048 | | EH.PE.datarel => ctx.data_rel_base orelse return error.PointerBaseNotSpecified, |
| 2049 | | EH.PE.funcrel => ctx.function_rel_base orelse return error.PointerBaseNotSpecified, |
| 2050 | | else => null, |
| 2051 | | }; |
| 1469 | pub const ElfModule = struct { |
| 1470 | unwind: Dwarf.Unwind, |
| 1471 | dwarf: Dwarf, |
| 1472 | mapped_memory: ?[]align(std.heap.page_size_min) const u8, |
| 1473 | external_mapped_memory: ?[]align(std.heap.page_size_min) const u8, |
| 2052 | 1474 | |
| 2053 | | const ptr: u64 = if (base) |b| switch (value) { |
| 2054 | | .signed => |s| @intCast(try std.math.add(i64, s, @as(i64, @intCast(b)))), |
| 2055 | | // absptr can actually contain signed values in some cases (aarch64 MachO) |
| 2056 | | .unsigned => |u| u +% b, |
| 2057 | | } else switch (value) { |
| 2058 | | .signed => |s| @as(u64, @intCast(s)), |
| 2059 | | .unsigned => |u| u, |
| 1475 | pub const Lookup = struct { |
| 1476 | base_address: usize, |
| 1477 | name: []const u8, |
| 1478 | build_id: ?[]const u8, |
| 1479 | gnu_eh_frame: ?[]const u8, |
| 2060 | 1480 | }; |
| 2061 | 1481 | |
| 2062 | | if ((enc & EH.PE.indirect) > 0 and ctx.follow_indirect) { |
| 2063 | | if (@sizeOf(usize) != addr_size_bytes) { |
| 2064 | | // See the documentation for `follow_indirect` |
| 2065 | | return error.NonNativeIndirection; |
| 2066 | | } |
| 2067 | | |
| 2068 | | const native_ptr = cast(usize, ptr) orelse return error.PointerOverflow; |
| 2069 | | return switch (addr_size_bytes) { |
| 2070 | | 2, 4, 8 => return @as(*const usize, @ptrFromInt(native_ptr)).*, |
| 2071 | | else => return error.UnsupportedAddrSize, |
| 1482 | pub fn init(lookup: *const Lookup) ElfModule { |
| 1483 | var em: ElfModule = .{ |
| 1484 | .unwind = .{ |
| 1485 | .sections = @splat(null), |
| 1486 | }, |
| 1487 | .dwarf = .{}, |
| 1488 | .mapped_memory = null, |
| 1489 | .external_mapped_memory = null, |
| 2072 | 1490 | }; |
| 2073 | | } else { |
| 2074 | | return ptr; |
| 2075 | | } |
| 2076 | | } |
| 2077 | | |
| 2078 | | fn pcRelBase(field_ptr: usize, pc_rel_offset: i64) !usize { |
| 2079 | | if (pc_rel_offset < 0) { |
| 2080 | | return std.math.sub(usize, field_ptr, @as(usize, @intCast(-pc_rel_offset))); |
| 2081 | | } else { |
| 2082 | | return std.math.add(usize, field_ptr, @as(usize, @intCast(pc_rel_offset))); |
| 1491 | if (lookup.gnu_eh_frame) |eh_frame_hdr| { |
| 1492 | // This is a special case - pointer offsets inside .eh_frame_hdr |
| 1493 | // are encoded relative to its base address, so we must use the |
| 1494 | // version that is already memory mapped, and not the one that |
| 1495 | // will be mapped separately from the ELF file. |
| 1496 | em.unwind.sections[@intFromEnum(Dwarf.Unwind.Section.Id.eh_frame_hdr)] = .{ |
| 1497 | .data = eh_frame_hdr, |
| 1498 | }; |
| 1499 | } |
| 1500 | return em; |
| 2083 | 1501 | } |
| 2084 | | } |
| 2085 | | |
| 2086 | | pub const ElfModule = struct { |
| 2087 | | base_address: usize, |
| 2088 | | dwarf: Dwarf, |
| 2089 | | mapped_memory: []align(std.heap.page_size_min) const u8, |
| 2090 | | external_mapped_memory: ?[]align(std.heap.page_size_min) const u8, |
| 2091 | 1502 | |
| 2092 | 1503 | pub fn deinit(self: *@This(), allocator: Allocator) void { |
| 2093 | 1504 | self.dwarf.deinit(allocator); |
| ... | ... | @@ -2095,16 +1506,16 @@ pub const ElfModule = struct { |
| 2095 | 1506 | if (self.external_mapped_memory) |m| std.posix.munmap(m); |
| 2096 | 1507 | } |
| 2097 | 1508 | |
| 2098 | | pub fn getSymbolAtAddress(self: *@This(), allocator: Allocator, address: usize) !std.debug.Symbol { |
| 1509 | pub fn getSymbolAtAddress(self: *@This(), allocator: Allocator, endian: Endian, base_address: usize, address: usize) !std.debug.Symbol { |
| 2099 | 1510 | // Translate the VA into an address into this object |
| 2100 | | const relocated_address = address - self.base_address; |
| 2101 | | return self.dwarf.getSymbol(allocator, relocated_address); |
| 1511 | const relocated_address = address - base_address; |
| 1512 | return self.dwarf.getSymbol(allocator, endian, relocated_address); |
| 2102 | 1513 | } |
| 2103 | 1514 | |
| 2104 | | pub fn getDwarfInfoForAddress(self: *@This(), allocator: Allocator, address: usize) !?*Dwarf { |
| 1515 | pub fn getDwarfUnwindForAddress(self: *@This(), allocator: Allocator, address: usize) !?*Dwarf.Unwind { |
| 2105 | 1516 | _ = allocator; |
| 2106 | 1517 | _ = address; |
| 2107 | | return &self.dwarf; |
| 1518 | return &self.unwind; |
| 2108 | 1519 | } |
| 2109 | 1520 | |
| 2110 | 1521 | pub const LoadError = error{ |
| ... | ... | @@ -2132,6 +1543,7 @@ pub const ElfModule = struct { |
| 2132 | 1543 | /// info is, then this this function will recurse to attempt to load the debug |
| 2133 | 1544 | /// sections from an external file. |
| 2134 | 1545 | pub fn load( |
| 1546 | em: *ElfModule, |
| 2135 | 1547 | gpa: Allocator, |
| 2136 | 1548 | mapped_mem: []align(std.heap.page_size_min) const u8, |
| 2137 | 1549 | build_id: ?[]const u8, |
| ... | ... | @@ -2139,7 +1551,7 @@ pub const ElfModule = struct { |
| 2139 | 1551 | parent_sections: *Dwarf.SectionArray, |
| 2140 | 1552 | parent_mapped_mem: ?[]align(std.heap.page_size_min) const u8, |
| 2141 | 1553 | elf_filename: ?[]const u8, |
| 2142 | | ) LoadError!Dwarf.ElfModule { |
| 1554 | ) LoadError!void { |
| 2143 | 1555 | if (expected_crc) |crc| if (crc != std.hash.crc.Crc32.hash(mapped_mem)) return error.InvalidDebugInfo; |
| 2144 | 1556 | |
| 2145 | 1557 | const hdr: *const elf.Ehdr = @ptrCast(&mapped_mem[0]); |
| ... | ... | @@ -2162,7 +1574,7 @@ pub const ElfModule = struct { |
| 2162 | 1574 | @ptrCast(@alignCast(&mapped_mem[shoff])), |
| 2163 | 1575 | )[0..hdr.e_shnum]; |
| 2164 | 1576 | |
| 2165 | | var sections: Dwarf.SectionArray = Dwarf.null_section_array; |
| 1577 | var sections: Dwarf.SectionArray = @splat(null); |
| 2166 | 1578 | |
| 2167 | 1579 | // Combine section list. This takes ownership over any owned sections from the parent scope. |
| 2168 | 1580 | for (parent_sections, &sections) |*parent, *section_elem| { |
| ... | ... | @@ -2276,7 +1688,7 @@ pub const ElfModule = struct { |
| 2276 | 1688 | .sub_path = filename, |
| 2277 | 1689 | }; |
| 2278 | 1690 | |
| 2279 | | return loadPath(gpa, path, null, separate_debug_crc, &sections, mapped_mem) catch break :blk; |
| 1691 | return em.loadPath(gpa, path, null, separate_debug_crc, &sections, mapped_mem) catch break :blk; |
| 2280 | 1692 | } |
| 2281 | 1693 | |
| 2282 | 1694 | const global_debug_directories = [_][]const u8{ |
| ... | ... | @@ -2304,7 +1716,7 @@ pub const ElfModule = struct { |
| 2304 | 1716 | }; |
| 2305 | 1717 | defer gpa.free(path.sub_path); |
| 2306 | 1718 | |
| 2307 | | return loadPath(gpa, path, null, separate_debug_crc, &sections, mapped_mem) catch continue; |
| 1719 | return em.loadPath(gpa, path, null, separate_debug_crc, &sections, mapped_mem) catch continue; |
| 2308 | 1720 | } |
| 2309 | 1721 | } |
| 2310 | 1722 | |
| ... | ... | @@ -2320,7 +1732,7 @@ pub const ElfModule = struct { |
| 2320 | 1732 | defer exe_dir.close(); |
| 2321 | 1733 | |
| 2322 | 1734 | // <exe_dir>/<gnu_debuglink> |
| 2323 | | if (loadPath( |
| 1735 | if (em.loadPath( |
| 2324 | 1736 | gpa, |
| 2325 | 1737 | .{ |
| 2326 | 1738 | .root_dir = .{ .path = null, .handle = exe_dir }, |
| ... | ... | @@ -2341,7 +1753,7 @@ pub const ElfModule = struct { |
| 2341 | 1753 | }; |
| 2342 | 1754 | defer gpa.free(path.sub_path); |
| 2343 | 1755 | |
| 2344 | | if (loadPath(gpa, path, null, separate_debug_crc, &sections, mapped_mem)) |debug_info| return debug_info else |_| {} |
| 1756 | if (em.loadPath(gpa, path, null, separate_debug_crc, &sections, mapped_mem)) |debug_info| return debug_info else |_| {} |
| 2345 | 1757 | } |
| 2346 | 1758 | |
| 2347 | 1759 | var cwd_buf: [std.fs.max_path_bytes]u8 = undefined; |
| ... | ... | @@ -2354,37 +1766,27 @@ pub const ElfModule = struct { |
| 2354 | 1766 | .sub_path = try std.fs.path.join(gpa, &.{ global_directory, cwd_path, separate_filename }), |
| 2355 | 1767 | }; |
| 2356 | 1768 | defer gpa.free(path.sub_path); |
| 2357 | | if (loadPath(gpa, path, null, separate_debug_crc, &sections, mapped_mem)) |debug_info| return debug_info else |_| {} |
| 1769 | if (em.loadPath(gpa, path, null, separate_debug_crc, &sections, mapped_mem)) |debug_info| return debug_info else |_| {} |
| 2358 | 1770 | } |
| 2359 | 1771 | } |
| 2360 | 1772 | |
| 2361 | 1773 | return error.MissingDebugInfo; |
| 2362 | 1774 | } |
| 2363 | 1775 | |
| 2364 | | var di: Dwarf = .{ |
| 2365 | | .endian = endian, |
| 2366 | | .sections = sections, |
| 2367 | | .is_macho = false, |
| 2368 | | }; |
| 2369 | | |
| 2370 | | try Dwarf.open(&di, gpa); |
| 2371 | | |
| 2372 | | return .{ |
| 2373 | | .base_address = 0, |
| 2374 | | .dwarf = di, |
| 2375 | | .mapped_memory = parent_mapped_mem orelse mapped_mem, |
| 2376 | | .external_mapped_memory = if (parent_mapped_mem != null) mapped_mem else null, |
| 2377 | | }; |
| 1776 | em.mapped_memory = parent_mapped_mem orelse mapped_mem; |
| 1777 | em.external_mapped_memory = if (parent_mapped_mem != null) mapped_mem else null; |
| 1778 | try em.dwarf.open(gpa, endian); |
| 2378 | 1779 | } |
| 2379 | 1780 | |
| 2380 | 1781 | pub fn loadPath( |
| 1782 | em: *ElfModule, |
| 2381 | 1783 | gpa: Allocator, |
| 2382 | 1784 | elf_file_path: Path, |
| 2383 | 1785 | build_id: ?[]const u8, |
| 2384 | 1786 | expected_crc: ?u32, |
| 2385 | 1787 | parent_sections: *Dwarf.SectionArray, |
| 2386 | 1788 | parent_mapped_mem: ?[]align(std.heap.page_size_min) const u8, |
| 2387 | | ) LoadError!Dwarf.ElfModule { |
| 1789 | ) LoadError!void { |
| 2388 | 1790 | const elf_file = elf_file_path.root_dir.handle.openFile(elf_file_path.sub_path, .{}) catch |err| switch (err) { |
| 2389 | 1791 | error.FileNotFound => return missing(), |
| 2390 | 1792 | else => return err, |
| ... | ... | @@ -2407,7 +1809,7 @@ pub const ElfModule = struct { |
| 2407 | 1809 | }; |
| 2408 | 1810 | errdefer std.posix.munmap(mapped_mem); |
| 2409 | 1811 | |
| 2410 | | return load( |
| 1812 | return em.load( |
| 2411 | 1813 | gpa, |
| 2412 | 1814 | mapped_mem, |
| 2413 | 1815 | build_id, |
| ... | ... | @@ -2419,22 +1821,21 @@ pub const ElfModule = struct { |
| 2419 | 1821 | } |
| 2420 | 1822 | }; |
| 2421 | 1823 | |
| 2422 | | pub fn getSymbol(di: *Dwarf, allocator: Allocator, address: u64) !std.debug.Symbol { |
| 2423 | | if (di.findCompileUnit(address)) |compile_unit| { |
| 2424 | | return .{ |
| 2425 | | .name = di.getSymbolName(address) orelse "???", |
| 2426 | | .compile_unit_name = compile_unit.die.getAttrString(di, std.dwarf.AT.name, di.section(.debug_str), compile_unit.*) catch |err| switch (err) { |
| 2427 | | error.MissingDebugInfo, error.InvalidDebugInfo => "???", |
| 2428 | | }, |
| 2429 | | .source_location = di.getLineNumberInfo(allocator, compile_unit, address) catch |err| switch (err) { |
| 2430 | | error.MissingDebugInfo, error.InvalidDebugInfo => null, |
| 2431 | | else => return err, |
| 2432 | | }, |
| 2433 | | }; |
| 2434 | | } else |err| switch (err) { |
| 1824 | pub fn getSymbol(di: *Dwarf, allocator: Allocator, endian: Endian, address: u64) !std.debug.Symbol { |
| 1825 | const compile_unit = di.findCompileUnit(endian, address) catch |err| switch (err) { |
| 2435 | 1826 | error.MissingDebugInfo, error.InvalidDebugInfo => return .{}, |
| 2436 | 1827 | else => return err, |
| 2437 | | } |
| 1828 | }; |
| 1829 | return .{ |
| 1830 | .name = di.getSymbolName(address) orelse "???", |
| 1831 | .compile_unit_name = compile_unit.die.getAttrString(di, endian, std.dwarf.AT.name, di.section(.debug_str), compile_unit) catch |err| switch (err) { |
| 1832 | error.MissingDebugInfo, error.InvalidDebugInfo => "???", |
| 1833 | }, |
| 1834 | .source_location = di.getLineNumberInfo(allocator, endian, compile_unit, address) catch |err| switch (err) { |
| 1835 | error.MissingDebugInfo, error.InvalidDebugInfo => null, |
| 1836 | else => return err, |
| 1837 | }, |
| 1838 | }; |
| 2438 | 1839 | } |
| 2439 | 1840 | |
| 2440 | 1841 | pub fn chopSlice(ptr: []const u8, offset: u64, size: u64) error{Overflow}![]const u8 { |
| ... | ... | @@ -2443,7 +1844,7 @@ pub fn chopSlice(ptr: []const u8, offset: u64, size: u64) error{Overflow}![]cons |
| 2443 | 1844 | return ptr[start..end]; |
| 2444 | 1845 | } |
| 2445 | 1846 | |
| 2446 | | fn readAddress(r: *Reader, format: std.dwarf.Format, endian: Endian) !u64 { |
| 1847 | pub fn readAddress(r: *Reader, format: std.dwarf.Format, endian: Endian) !u64 { |
| 2447 | 1848 | return switch (format) { |
| 2448 | 1849 | .@"32" => try r.takeInt(u32, endian), |
| 2449 | 1850 | .@"64" => try r.takeInt(u64, endian), |