| ... | ... | @@ -261,7 +261,6 @@ pub const Section = struct { |
| 261 | 261 | index: u32, |
| 262 | 262 | first: Unit.Index.Optional, |
| 263 | 263 | last: Unit.Index.Optional, |
| 264 | | off: u64, |
| 265 | 264 | len: u64, |
| 266 | 265 | units: std.ArrayListUnmanaged(Unit), |
| 267 | 266 | |
| ... | ... | @@ -284,9 +283,8 @@ pub const Section = struct { |
| 284 | 283 | .index = std.math.maxInt(u32), |
| 285 | 284 | .first = .none, |
| 286 | 285 | .last = .none, |
| 287 | | .off = 0, |
| 288 | | .len = 0, |
| 289 | 286 | .units = .{}, |
| 287 | .len = 0, |
| 290 | 288 | }; |
| 291 | 289 | |
| 292 | 290 | fn deinit(sec: *Section, gpa: std.mem.Allocator) void { |
| ... | ... | @@ -295,6 +293,20 @@ pub const Section = struct { |
| 295 | 293 | sec.* = undefined; |
| 296 | 294 | } |
| 297 | 295 | |
| 296 | fn off(sec: Section, dwarf: *Dwarf) u64 { |
| 297 | if (dwarf.bin_file.cast(.elf)) |elf_file| { |
| 298 | const zo = elf_file.zigObjectPtr().?; |
| 299 | const atom = zo.symbol(sec.index).atom(elf_file).?; |
| 300 | return atom.offset(elf_file); |
| 301 | } else if (dwarf.bin_file.cast(.macho)) |macho_file| { |
| 302 | const header = if (macho_file.d_sym) |d_sym| |
| 303 | d_sym.sections.items[sec.index] |
| 304 | else |
| 305 | macho_file.sections.items(.header)[sec.index]; |
| 306 | return header.offset; |
| 307 | } else unreachable; |
| 308 | } |
| 309 | |
| 298 | 310 | fn addUnit(sec: *Section, header_len: u32, trailer_len: u32, dwarf: *Dwarf) UpdateError!Unit.Index { |
| 299 | 311 | const unit: Unit.Index = @enumFromInt(sec.units.items.len); |
| 300 | 312 | const unit_ptr = try sec.units.addOne(dwarf.gpa); |
| ... | ... | @@ -306,9 +318,9 @@ pub const Section = struct { |
| 306 | 318 | .next = .none, |
| 307 | 319 | .first = .none, |
| 308 | 320 | .last = .none, |
| 309 | | .off = 0, |
| 310 | 321 | .header_len = aligned_header_len, |
| 311 | 322 | .trailer_len = aligned_trailer_len, |
| 323 | .off = 0, |
| 312 | 324 | .len = aligned_header_len + aligned_trailer_len, |
| 313 | 325 | .entries = .{}, |
| 314 | 326 | .cross_unit_relocs = .{}, |
| ... | ... | @@ -385,7 +397,6 @@ pub const Section = struct { |
| 385 | 397 | const shdr = elf_file.sections.items(.shdr)[shndx]; |
| 386 | 398 | atom.size = shdr.sh_size; |
| 387 | 399 | atom.alignment = InternPool.Alignment.fromNonzeroByteUnits(shdr.sh_addralign); |
| 388 | | sec.off = shdr.sh_offset + @as(u64, @intCast(atom.value)); |
| 389 | 400 | sec.len = shdr.sh_size; |
| 390 | 401 | } else if (dwarf.bin_file.cast(.macho)) |macho_file| { |
| 391 | 402 | const header = if (macho_file.d_sym) |*d_sym| header: { |
| ... | ... | @@ -395,7 +406,6 @@ pub const Section = struct { |
| 395 | 406 | try macho_file.growSection(@intCast(sec.index), len); |
| 396 | 407 | break :header &macho_file.sections.items(.header)[sec.index]; |
| 397 | 408 | }; |
| 398 | | sec.off = header.offset; |
| 399 | 409 | sec.len = header.size; |
| 400 | 410 | } |
| 401 | 411 | } |
| ... | ... | @@ -404,7 +414,6 @@ pub const Section = struct { |
| 404 | 414 | const len = sec.getUnit(sec.first.unwrap() orelse return).off; |
| 405 | 415 | if (len == 0) return; |
| 406 | 416 | for (sec.units.items) |*unit| unit.off -= len; |
| 407 | | sec.off += len; |
| 408 | 417 | sec.len -= len; |
| 409 | 418 | if (dwarf.bin_file.cast(.elf)) |elf_file| { |
| 410 | 419 | const zo = elf_file.zigObjectPtr().?; |
| ... | ... | @@ -412,14 +421,14 @@ pub const Section = struct { |
| 412 | 421 | const shndx = atom.output_section_index; |
| 413 | 422 | const shdr = &elf_file.sections.items(.shdr)[shndx]; |
| 414 | 423 | atom.size = sec.len; |
| 415 | | shdr.sh_offset = sec.off; |
| 424 | shdr.sh_offset += len; |
| 416 | 425 | shdr.sh_size = sec.len; |
| 417 | 426 | } else if (dwarf.bin_file.cast(.macho)) |macho_file| { |
| 418 | 427 | const header = if (macho_file.d_sym) |*d_sym| |
| 419 | 428 | &d_sym.sections.items[sec.index] |
| 420 | 429 | else |
| 421 | 430 | &macho_file.sections.items(.header)[sec.index]; |
| 422 | | header.offset = @intCast(sec.off); |
| 431 | header.offset += @intCast(len); |
| 423 | 432 | header.size = sec.len; |
| 424 | 433 | } |
| 425 | 434 | } |
| ... | ... | @@ -548,9 +557,9 @@ const Unit = struct { |
| 548 | 557 | fn move(unit: *Unit, sec: *Section, dwarf: *Dwarf, new_off: u32) UpdateError!void { |
| 549 | 558 | if (unit.off == new_off) return; |
| 550 | 559 | if (try dwarf.getFile().?.copyRangeAll( |
| 551 | | sec.off + unit.off, |
| 560 | sec.off(dwarf) + unit.off, |
| 552 | 561 | dwarf.getFile().?, |
| 553 | | sec.off + new_off, |
| 562 | sec.off(dwarf) + new_off, |
| 554 | 563 | unit.len, |
| 555 | 564 | ) != unit.len) return error.InputOutput; |
| 556 | 565 | unit.off = new_off; |
| ... | ... | @@ -582,7 +591,7 @@ const Unit = struct { |
| 582 | 591 | |
| 583 | 592 | fn replaceHeader(unit: *Unit, sec: *Section, dwarf: *Dwarf, contents: []const u8) UpdateError!void { |
| 584 | 593 | assert(contents.len == unit.header_len); |
| 585 | | try dwarf.getFile().?.pwriteAll(contents, sec.off + unit.off); |
| 594 | try dwarf.getFile().?.pwriteAll(contents, sec.off(dwarf) + unit.off); |
| 586 | 595 | } |
| 587 | 596 | |
| 588 | 597 | fn writeTrailer(unit: *Unit, sec: *Section, dwarf: *Dwarf) UpdateError!void { |
| ... | ... | @@ -614,7 +623,7 @@ const Unit = struct { |
| 614 | 623 | assert(fbs.pos == extended_op_bytes + op_len_bytes); |
| 615 | 624 | writer.writeByte(DW.LNE.padding) catch unreachable; |
| 616 | 625 | assert(fbs.pos >= unit.trailer_len and fbs.pos <= len); |
| 617 | | return dwarf.getFile().?.pwriteAll(fbs.getWritten(), sec.off + start); |
| 626 | return dwarf.getFile().?.pwriteAll(fbs.getWritten(), sec.off(dwarf) + start); |
| 618 | 627 | } |
| 619 | 628 | var trailer = try std.ArrayList(u8).initCapacity(dwarf.gpa, len); |
| 620 | 629 | defer trailer.deinit(); |
| ... | ... | @@ -673,11 +682,11 @@ const Unit = struct { |
| 673 | 682 | assert(trailer.items.len == unit.trailer_len); |
| 674 | 683 | trailer.appendNTimesAssumeCapacity(fill_byte, len - unit.trailer_len); |
| 675 | 684 | assert(trailer.items.len == len); |
| 676 | | try dwarf.getFile().?.pwriteAll(trailer.items, sec.off + start); |
| 685 | try dwarf.getFile().?.pwriteAll(trailer.items, sec.off(dwarf) + start); |
| 677 | 686 | } |
| 678 | 687 | |
| 679 | 688 | fn resolveRelocs(unit: *Unit, sec: *Section, dwarf: *Dwarf) RelocError!void { |
| 680 | | const unit_off = sec.off + unit.off; |
| 689 | const unit_off = sec.off(dwarf) + unit.off; |
| 681 | 690 | for (unit.cross_unit_relocs.items) |reloc| { |
| 682 | 691 | const target_unit = sec.getUnit(reloc.target_unit); |
| 683 | 692 | try dwarf.resolveReloc( |
| ... | ... | @@ -764,12 +773,12 @@ const Entry = struct { |
| 764 | 773 | dwarf.writeInt(unit_len[0..dwarf.sectionOffsetBytes()], len - dwarf.unitLengthBytes()); |
| 765 | 774 | try dwarf.getFile().?.pwriteAll( |
| 766 | 775 | unit_len[0..dwarf.sectionOffsetBytes()], |
| 767 | | sec.off + unit.off + unit.header_len + entry.off, |
| 776 | sec.off(dwarf) + unit.off + unit.header_len + entry.off, |
| 768 | 777 | ); |
| 769 | 778 | const buf = try dwarf.gpa.alloc(u8, len - entry.len); |
| 770 | 779 | defer dwarf.gpa.free(buf); |
| 771 | 780 | @memset(buf, DW.CFA.nop); |
| 772 | | try dwarf.getFile().?.pwriteAll(buf, sec.off + unit.off + unit.header_len + start); |
| 781 | try dwarf.getFile().?.pwriteAll(buf, sec.off(dwarf) + unit.off + unit.header_len + start); |
| 773 | 782 | return; |
| 774 | 783 | } |
| 775 | 784 | const len = unit.getEntry(entry.next.unwrap() orelse return).off - start; |
| ... | ... | @@ -825,7 +834,7 @@ const Entry = struct { |
| 825 | 834 | }, |
| 826 | 835 | } else assert(!sec.pad_to_ideal and len == 0); |
| 827 | 836 | assert(fbs.pos <= len); |
| 828 | | try dwarf.getFile().?.pwriteAll(fbs.getWritten(), sec.off + unit.off + unit.header_len + start); |
| 837 | try dwarf.getFile().?.pwriteAll(fbs.getWritten(), sec.off(dwarf) + unit.off + unit.header_len + start); |
| 829 | 838 | } |
| 830 | 839 | |
| 831 | 840 | fn resize(entry_ptr: *Entry, unit: *Unit, sec: *Section, dwarf: *Dwarf, len: u32) UpdateError!void { |
| ... | ... | @@ -860,15 +869,15 @@ const Entry = struct { |
| 860 | 869 | |
| 861 | 870 | fn replace(entry_ptr: *Entry, unit: *Unit, sec: *Section, dwarf: *Dwarf, contents: []const u8) UpdateError!void { |
| 862 | 871 | assert(contents.len == entry_ptr.len); |
| 863 | | try dwarf.getFile().?.pwriteAll(contents, sec.off + unit.off + unit.header_len + entry_ptr.off); |
| 872 | try dwarf.getFile().?.pwriteAll(contents, sec.off(dwarf) + unit.off + unit.header_len + entry_ptr.off); |
| 864 | 873 | if (false) { |
| 865 | 874 | const buf = try dwarf.gpa.alloc(u8, sec.len); |
| 866 | 875 | defer dwarf.gpa.free(buf); |
| 867 | | _ = try dwarf.getFile().?.preadAll(buf, sec.off); |
| 876 | _ = try dwarf.getFile().?.preadAll(buf, sec.off(dwarf)); |
| 868 | 877 | log.info("Section{{ .first = {}, .last = {}, .off = 0x{x}, .len = 0x{x} }}", .{ |
| 869 | 878 | @intFromEnum(sec.first), |
| 870 | 879 | @intFromEnum(sec.last), |
| 871 | | sec.off, |
| 880 | sec.off(dwarf), |
| 872 | 881 | sec.len, |
| 873 | 882 | }); |
| 874 | 883 | for (sec.units.items) |*unit_ptr| { |
| ... | ... | @@ -935,7 +944,7 @@ const Entry = struct { |
| 935 | 944 | } |
| 936 | 945 | |
| 937 | 946 | fn resolveRelocs(entry: *Entry, unit: *Unit, sec: *Section, dwarf: *Dwarf) RelocError!void { |
| 938 | | const entry_off = sec.off + unit.off + unit.header_len + entry.off; |
| 947 | const entry_off = sec.off(dwarf) + unit.off + unit.header_len + entry.off; |
| 939 | 948 | for (entry.cross_entry_relocs.items) |reloc| { |
| 940 | 949 | try dwarf.resolveReloc( |
| 941 | 950 | entry_off + reloc.source_off, |
| ... | ... | @@ -973,7 +982,7 @@ const Entry = struct { |
| 973 | 982 | .eh_frame => return if (dwarf.bin_file.cast(.elf)) |elf_file| { |
| 974 | 983 | const zo = elf_file.zigObjectPtr().?; |
| 975 | 984 | const shndx = zo.symbol(sec.index).atom(elf_file).?.output_section_index; |
| 976 | | const entry_addr: i64 = @intCast(entry_off - sec.off + elf_file.shdrs.items[shndx].sh_addr); |
| 985 | const entry_addr: i64 = @intCast(entry_off - sec.off(dwarf) + elf_file.shdrs.items[shndx].sh_addr); |
| 977 | 986 | for (entry.external_relocs.items) |reloc| { |
| 978 | 987 | const symbol = zo.symbol(reloc.target_sym); |
| 979 | 988 | try dwarf.resolveReloc( |
| ... | ... | @@ -1912,7 +1921,6 @@ pub fn reloadSectionMetadata(dwarf: *Dwarf) void { |
| 1912 | 1921 | }) |sec, sect_index| { |
| 1913 | 1922 | const header = &d_sym.sections.items[sect_index]; |
| 1914 | 1923 | sec.index = sect_index; |
| 1915 | | sec.off = header.offset; |
| 1916 | 1924 | sec.len = header.size; |
| 1917 | 1925 | } |
| 1918 | 1926 | } else { |
| ... | ... | @@ -1937,7 +1945,6 @@ pub fn reloadSectionMetadata(dwarf: *Dwarf) void { |
| 1937 | 1945 | }) |sec, sect_index| { |
| 1938 | 1946 | const header = &macho_file.sections.items(.header)[sect_index]; |
| 1939 | 1947 | sec.index = sect_index; |
| 1940 | | sec.off = header.offset; |
| 1941 | 1948 | sec.len = header.size; |
| 1942 | 1949 | } |
| 1943 | 1950 | } |
| ... | ... | @@ -2534,7 +2541,7 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool |
| 2534 | 2541 | var abbrev_code_buf: [AbbrevCode.decl_bytes]u8 = undefined; |
| 2535 | 2542 | if (try dwarf.getFile().?.preadAll( |
| 2536 | 2543 | &abbrev_code_buf, |
| 2537 | | dwarf.debug_info.section.off + unit_ptr.off + unit_ptr.header_len + entry_ptr.off, |
| 2544 | dwarf.debug_info.section.off(dwarf) + unit_ptr.off + unit_ptr.header_len + entry_ptr.off, |
| 2538 | 2545 | ) != abbrev_code_buf.len) return error.InputOutput; |
| 2539 | 2546 | var abbrev_code_fbs = std.io.fixedBufferStream(&abbrev_code_buf); |
| 2540 | 2547 | const abbrev_code: AbbrevCode = @enumFromInt( |
| ... | ... | @@ -3945,7 +3952,7 @@ pub fn flushModule(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void { |
| 3945 | 3952 | if (dwarf.debug_str.section.dirty) { |
| 3946 | 3953 | const contents = dwarf.debug_str.contents.items; |
| 3947 | 3954 | try dwarf.debug_str.section.resize(dwarf, contents.len); |
| 3948 | | try dwarf.getFile().?.pwriteAll(contents, dwarf.debug_str.section.off); |
| 3955 | try dwarf.getFile().?.pwriteAll(contents, dwarf.debug_str.section.off(dwarf)); |
| 3949 | 3956 | dwarf.debug_str.section.dirty = false; |
| 3950 | 3957 | } |
| 3951 | 3958 | if (dwarf.debug_line.section.dirty) { |
| ... | ... | @@ -4051,7 +4058,7 @@ pub fn flushModule(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void { |
| 4051 | 4058 | if (dwarf.debug_line_str.section.dirty) { |
| 4052 | 4059 | const contents = dwarf.debug_line_str.contents.items; |
| 4053 | 4060 | try dwarf.debug_line_str.section.resize(dwarf, contents.len); |
| 4054 | | try dwarf.getFile().?.pwriteAll(contents, dwarf.debug_line_str.section.off); |
| 4061 | try dwarf.getFile().?.pwriteAll(contents, dwarf.debug_line_str.section.off(dwarf)); |
| 4055 | 4062 | dwarf.debug_line_str.section.dirty = false; |
| 4056 | 4063 | } |
| 4057 | 4064 | if (dwarf.debug_loclists.section.dirty) { |