| ... | ... | @@ -21,8 +21,9 @@ pub const UpdateError = |
| 21 | 21 | std.fs.File.OpenError || |
| 22 | 22 | std.fs.File.SetEndPosError || |
| 23 | 23 | std.fs.File.CopyRangeError || |
| 24 | std.fs.File.PReadError || |
| 24 | 25 | std.fs.File.PWriteError || |
| 25 | | error{ Overflow, Underflow, UnexpectedEndOfFile }; |
| 26 | error{ EndOfStream, Overflow, Underflow, UnexpectedEndOfFile }; |
| 26 | 27 | |
| 27 | 28 | pub const FlushError = |
| 28 | 29 | UpdateError || |
| ... | ... | @@ -253,10 +254,8 @@ const Section = struct { |
| 253 | 254 | .trailer_len = trailer_len, |
| 254 | 255 | .len = header_len + trailer_len, |
| 255 | 256 | .entries = .{}, |
| 256 | | .cross_entry_relocs = .{}, |
| 257 | 257 | .cross_unit_relocs = .{}, |
| 258 | 258 | .cross_section_relocs = .{}, |
| 259 | | .external_relocs = .{}, |
| 260 | 259 | }; |
| 261 | 260 | if (sec.last.unwrap()) |last_unit| { |
| 262 | 261 | const last_unit_ptr = sec.getUnit(last_unit); |
| ... | ... | @@ -358,10 +357,8 @@ const Unit = struct { |
| 358 | 357 | /// data length in bytes |
| 359 | 358 | len: u32, |
| 360 | 359 | entries: std.ArrayListUnmanaged(Entry), |
| 361 | | cross_entry_relocs: std.ArrayListUnmanaged(CrossEntryReloc), |
| 362 | 360 | cross_unit_relocs: std.ArrayListUnmanaged(CrossUnitReloc), |
| 363 | 361 | cross_section_relocs: std.ArrayListUnmanaged(CrossSectionReloc), |
| 364 | | external_relocs: std.ArrayListUnmanaged(ExternalReloc), |
| 365 | 362 | |
| 366 | 363 | const Index = enum(u32) { |
| 367 | 364 | main, |
| ... | ... | @@ -381,12 +378,16 @@ const Unit = struct { |
| 381 | 378 | } |
| 382 | 379 | }; |
| 383 | 380 | |
| 381 | fn clear(unit: *Unit) void { |
| 382 | unit.cross_unit_relocs.clearRetainingCapacity(); |
| 383 | unit.cross_section_relocs.clearRetainingCapacity(); |
| 384 | } |
| 385 | |
| 384 | 386 | fn deinit(unit: *Unit, gpa: std.mem.Allocator) void { |
| 387 | for (unit.entries.items) |*entry| entry.deinit(gpa); |
| 385 | 388 | unit.entries.deinit(gpa); |
| 386 | | unit.cross_entry_relocs.deinit(gpa); |
| 387 | 389 | unit.cross_unit_relocs.deinit(gpa); |
| 388 | 390 | unit.cross_section_relocs.deinit(gpa); |
| 389 | | unit.external_relocs.deinit(gpa); |
| 390 | 391 | unit.* = undefined; |
| 391 | 392 | } |
| 392 | 393 | |
| ... | ... | @@ -398,6 +399,10 @@ const Unit = struct { |
| 398 | 399 | .next = .none, |
| 399 | 400 | .off = 0, |
| 400 | 401 | .len = 0, |
| 402 | .cross_entry_relocs = .{}, |
| 403 | .cross_unit_relocs = .{}, |
| 404 | .cross_section_relocs = .{}, |
| 405 | .external_relocs = .{}, |
| 401 | 406 | }; |
| 402 | 407 | if (unit.last.unwrap()) |last_entry| { |
| 403 | 408 | const last_entry_ptr = unit.getEntry(last_entry); |
| ... | ... | @@ -451,6 +456,14 @@ const Unit = struct { |
| 451 | 456 | unit_ptr.len = len; |
| 452 | 457 | } |
| 453 | 458 | |
| 459 | fn trim(unit: *Unit) void { |
| 460 | const len = unit.getEntry(unit.first.unwrap() orelse return).off; |
| 461 | if (len == 0) return; |
| 462 | for (unit.entries.items) |*entry| entry.off -= len; |
| 463 | unit.off += len; |
| 464 | unit.len -= len; |
| 465 | } |
| 466 | |
| 454 | 467 | fn move(unit: *Unit, sec: *Section, dwarf: *Dwarf, new_off: u32) UpdateError!void { |
| 455 | 468 | if (unit.off == new_off) return; |
| 456 | 469 | if (try dwarf.getFile().?.copyRangeAll( |
| ... | ... | @@ -463,6 +476,7 @@ const Unit = struct { |
| 463 | 476 | } |
| 464 | 477 | |
| 465 | 478 | fn resizeHeader(unit: *Unit, sec: *Section, dwarf: *Dwarf, len: u32) UpdateError!void { |
| 479 | unit.trim(); |
| 466 | 480 | if (unit.header_len == len) return; |
| 467 | 481 | const available_len = if (unit.prev.unwrap()) |prev_unit| prev_excess: { |
| 468 | 482 | const prev_unit_ptr = sec.getUnit(prev_unit); |
| ... | ... | @@ -535,23 +549,11 @@ const Unit = struct { |
| 535 | 549 | } |
| 536 | 550 | |
| 537 | 551 | fn resolveRelocs(unit: *Unit, sec: *Section, dwarf: *Dwarf) RelocError!void { |
| 538 | | for (unit.cross_entry_relocs.items) |reloc| { |
| 539 | | try dwarf.resolveReloc( |
| 540 | | sec.off + unit.off + (if (reloc.source_entry.unwrap()) |source_entry| |
| 541 | | unit.header_len + unit.getEntry(source_entry).off |
| 542 | | else |
| 543 | | 0) + reloc.source_off, |
| 544 | | unit.off + unit.header_len + unit.getEntry(reloc.target_entry).assertNonEmpty(unit, sec, dwarf).off + reloc.target_off, |
| 545 | | dwarf.sectionOffsetBytes(), |
| 546 | | ); |
| 547 | | } |
| 552 | const unit_off = sec.off + unit.off; |
| 548 | 553 | for (unit.cross_unit_relocs.items) |reloc| { |
| 549 | 554 | const target_unit = sec.getUnit(reloc.target_unit); |
| 550 | 555 | try dwarf.resolveReloc( |
| 551 | | sec.off + unit.off + (if (reloc.source_entry.unwrap()) |source_entry| |
| 552 | | unit.header_len + unit.getEntry(source_entry).off |
| 553 | | else |
| 554 | | 0) + reloc.source_off, |
| 556 | unit_off + reloc.source_off, |
| 555 | 557 | target_unit.off + (if (reloc.target_entry.unwrap()) |target_entry| |
| 556 | 558 | target_unit.header_len + target_unit.getEntry(target_entry).assertNonEmpty(unit, sec, dwarf).off |
| 557 | 559 | else |
| ... | ... | @@ -565,10 +567,7 @@ const Unit = struct { |
| 565 | 567 | }; |
| 566 | 568 | const target_unit = target_sec.getUnit(reloc.target_unit); |
| 567 | 569 | try dwarf.resolveReloc( |
| 568 | | sec.off + unit.off + (if (reloc.source_entry.unwrap()) |source_entry| |
| 569 | | unit.header_len + unit.getEntry(source_entry).off |
| 570 | | else |
| 571 | | 0) + reloc.source_off, |
| 570 | unit_off + reloc.source_off, |
| 572 | 571 | target_unit.off + (if (reloc.target_entry.unwrap()) |target_entry| |
| 573 | 572 | target_unit.header_len + target_unit.getEntry(target_entry).assertNonEmpty(unit, sec, dwarf).off |
| 574 | 573 | else |
| ... | ... | @@ -576,57 +575,8 @@ const Unit = struct { |
| 576 | 575 | dwarf.sectionOffsetBytes(), |
| 577 | 576 | ); |
| 578 | 577 | } |
| 579 | | if (dwarf.bin_file.cast(.elf)) |elf_file| { |
| 580 | | const zo = elf_file.zigObjectPtr().?; |
| 581 | | for (unit.external_relocs.items) |reloc| { |
| 582 | | const symbol = zo.symbol(reloc.target_sym); |
| 583 | | try dwarf.resolveReloc( |
| 584 | | sec.off + unit.off + unit.header_len + unit.getEntry(reloc.source_entry).off + reloc.source_off, |
| 585 | | @bitCast(symbol.address(.{}, elf_file) + @as(i64, @intCast(reloc.target_off)) - |
| 586 | | if (symbol.flags.is_tls) elf_file.dtpAddress() else 0), |
| 587 | | @intFromEnum(dwarf.address_size), |
| 588 | | ); |
| 589 | | } |
| 590 | | } else if (dwarf.bin_file.cast(.macho)) |macho_file| { |
| 591 | | const zo = macho_file.getZigObject().?; |
| 592 | | for (unit.external_relocs.items) |reloc| { |
| 593 | | const ref = zo.getSymbolRef(reloc.target_sym, macho_file); |
| 594 | | try dwarf.resolveReloc( |
| 595 | | sec.off + unit.off + unit.header_len + unit.getEntry(reloc.source_entry).off + reloc.source_off, |
| 596 | | ref.getSymbol(macho_file).?.getAddress(.{}, macho_file), |
| 597 | | @intFromEnum(dwarf.address_size), |
| 598 | | ); |
| 599 | | } |
| 600 | | } |
| 578 | for (unit.entries.items) |*entry| try entry.resolveRelocs(unit, sec, dwarf); |
| 601 | 579 | } |
| 602 | | |
| 603 | | const CrossEntryReloc = struct { |
| 604 | | source_entry: Entry.Index.Optional = .none, |
| 605 | | source_off: u32 = 0, |
| 606 | | target_entry: Entry.Index, |
| 607 | | target_off: u32 = 0, |
| 608 | | }; |
| 609 | | const CrossUnitReloc = struct { |
| 610 | | source_entry: Entry.Index.Optional = .none, |
| 611 | | source_off: u32 = 0, |
| 612 | | target_unit: Unit.Index, |
| 613 | | target_entry: Entry.Index.Optional = .none, |
| 614 | | target_off: u32 = 0, |
| 615 | | }; |
| 616 | | const CrossSectionReloc = struct { |
| 617 | | source_entry: Entry.Index.Optional = .none, |
| 618 | | source_off: u32 = 0, |
| 619 | | target_sec: Section.Index, |
| 620 | | target_unit: Unit.Index, |
| 621 | | target_entry: Entry.Index.Optional = .none, |
| 622 | | target_off: u32 = 0, |
| 623 | | }; |
| 624 | | const ExternalReloc = struct { |
| 625 | | source_entry: Entry.Index, |
| 626 | | source_off: u32 = 0, |
| 627 | | target_sym: u32, |
| 628 | | target_off: u64 = 0, |
| 629 | | }; |
| 630 | 580 | }; |
| 631 | 581 | |
| 632 | 582 | /// An indivisible entry within a `Unit` containing section-specific data. |
| ... | ... | @@ -637,6 +587,25 @@ const Entry = struct { |
| 637 | 587 | off: u32, |
| 638 | 588 | /// data length in bytes |
| 639 | 589 | len: u32, |
| 590 | cross_entry_relocs: std.ArrayListUnmanaged(CrossEntryReloc), |
| 591 | cross_unit_relocs: std.ArrayListUnmanaged(CrossUnitReloc), |
| 592 | cross_section_relocs: std.ArrayListUnmanaged(CrossSectionReloc), |
| 593 | external_relocs: std.ArrayListUnmanaged(ExternalReloc), |
| 594 | |
| 595 | fn clear(entry: *Entry) void { |
| 596 | entry.cross_entry_relocs.clearRetainingCapacity(); |
| 597 | entry.cross_unit_relocs.clearRetainingCapacity(); |
| 598 | entry.cross_section_relocs.clearRetainingCapacity(); |
| 599 | entry.external_relocs.clearRetainingCapacity(); |
| 600 | } |
| 601 | |
| 602 | fn deinit(entry: *Entry, gpa: std.mem.Allocator) void { |
| 603 | entry.cross_entry_relocs.deinit(gpa); |
| 604 | entry.cross_unit_relocs.deinit(gpa); |
| 605 | entry.cross_section_relocs.deinit(gpa); |
| 606 | entry.external_relocs.deinit(gpa); |
| 607 | entry.* = undefined; |
| 608 | } |
| 640 | 609 | |
| 641 | 610 | const Index = enum(u32) { |
| 642 | 611 | _, |
| ... | ... | @@ -803,6 +772,88 @@ const Entry = struct { |
| 803 | 772 | } |
| 804 | 773 | @panic("missing dwarf relocation target"); |
| 805 | 774 | } |
| 775 | |
| 776 | fn resolveRelocs(entry: *Entry, unit: *Unit, sec: *Section, dwarf: *Dwarf) RelocError!void { |
| 777 | const entry_off = sec.off + unit.off + unit.header_len + entry.off; |
| 778 | for (entry.cross_entry_relocs.items) |reloc| { |
| 779 | try dwarf.resolveReloc( |
| 780 | entry_off + reloc.source_off, |
| 781 | unit.off + unit.header_len + unit.getEntry(reloc.target_entry).assertNonEmpty(unit, sec, dwarf).off + reloc.target_off, |
| 782 | dwarf.sectionOffsetBytes(), |
| 783 | ); |
| 784 | } |
| 785 | for (entry.cross_unit_relocs.items) |reloc| { |
| 786 | const target_unit = sec.getUnit(reloc.target_unit); |
| 787 | try dwarf.resolveReloc( |
| 788 | entry_off + reloc.source_off, |
| 789 | target_unit.off + (if (reloc.target_entry.unwrap()) |target_entry| |
| 790 | target_unit.header_len + target_unit.getEntry(target_entry).assertNonEmpty(unit, sec, dwarf).off |
| 791 | else |
| 792 | 0) + reloc.target_off, |
| 793 | dwarf.sectionOffsetBytes(), |
| 794 | ); |
| 795 | } |
| 796 | for (entry.cross_section_relocs.items) |reloc| { |
| 797 | const target_sec = switch (reloc.target_sec) { |
| 798 | inline else => |target_sec| &@field(dwarf, @tagName(target_sec)).section, |
| 799 | }; |
| 800 | const target_unit = target_sec.getUnit(reloc.target_unit); |
| 801 | try dwarf.resolveReloc( |
| 802 | entry_off + reloc.source_off, |
| 803 | target_unit.off + (if (reloc.target_entry.unwrap()) |target_entry| |
| 804 | target_unit.header_len + target_unit.getEntry(target_entry).assertNonEmpty(unit, sec, dwarf).off |
| 805 | else |
| 806 | 0) + reloc.target_off, |
| 807 | dwarf.sectionOffsetBytes(), |
| 808 | ); |
| 809 | } |
| 810 | if (dwarf.bin_file.cast(.elf)) |elf_file| { |
| 811 | const zo = elf_file.zigObjectPtr().?; |
| 812 | for (entry.external_relocs.items) |reloc| { |
| 813 | const symbol = zo.symbol(reloc.target_sym); |
| 814 | try dwarf.resolveReloc( |
| 815 | entry_off + reloc.source_off, |
| 816 | @bitCast(symbol.address(.{}, elf_file) + @as(i64, @intCast(reloc.target_off)) - |
| 817 | if (symbol.flags.is_tls) elf_file.dtpAddress() else 0), |
| 818 | @intFromEnum(dwarf.address_size), |
| 819 | ); |
| 820 | } |
| 821 | } else if (dwarf.bin_file.cast(.macho)) |macho_file| { |
| 822 | const zo = macho_file.getZigObject().?; |
| 823 | for (entry.external_relocs.items) |reloc| { |
| 824 | const ref = zo.getSymbolRef(reloc.target_sym, macho_file); |
| 825 | try dwarf.resolveReloc( |
| 826 | entry_off + reloc.source_off, |
| 827 | ref.getSymbol(macho_file).?.getAddress(.{}, macho_file), |
| 828 | @intFromEnum(dwarf.address_size), |
| 829 | ); |
| 830 | } |
| 831 | } |
| 832 | } |
| 833 | }; |
| 834 | |
| 835 | const CrossEntryReloc = struct { |
| 836 | source_off: u32 = 0, |
| 837 | target_entry: Entry.Index, |
| 838 | target_off: u32 = 0, |
| 839 | }; |
| 840 | const CrossUnitReloc = struct { |
| 841 | source_off: u32 = 0, |
| 842 | target_unit: Unit.Index, |
| 843 | target_entry: Entry.Index.Optional = .none, |
| 844 | target_off: u32 = 0, |
| 845 | }; |
| 846 | const CrossSectionReloc = struct { |
| 847 | source_off: u32 = 0, |
| 848 | target_sec: Section.Index, |
| 849 | target_unit: Unit.Index, |
| 850 | target_entry: Entry.Index.Optional = .none, |
| 851 | target_off: u32 = 0, |
| 852 | }; |
| 853 | const ExternalReloc = struct { |
| 854 | source_off: u32 = 0, |
| 855 | target_sym: u32, |
| 856 | target_off: u64 = 0, |
| 806 | 857 | }; |
| 807 | 858 | |
| 808 | 859 | pub const Loc = union(enum) { |
| ... | ... | @@ -1087,14 +1138,13 @@ pub const WipNav = struct { |
| 1087 | 1138 | const diw = wip_nav.debug_info.writer(dwarf.gpa); |
| 1088 | 1139 | try wip_nav.inlined_funcs_high_reloc.ensureUnusedCapacity(dwarf.gpa, 1); |
| 1089 | 1140 | |
| 1090 | | const external_relocs = &dwarf.debug_info.section.getUnit(wip_nav.unit).external_relocs; |
| 1091 | | try external_relocs.ensureUnusedCapacity(dwarf.gpa, 2); |
| 1092 | 1141 | try uleb128(diw, @intFromEnum(AbbrevCode.inlined_func)); |
| 1093 | 1142 | try wip_nav.refNav(zcu.funcInfo(func).owner_nav); |
| 1094 | 1143 | try uleb128(diw, zcu.navSrcLine(zcu.funcInfo(wip_nav.func).owner_nav) + line + 1); |
| 1095 | | try uleb128(diw, column); |
| 1144 | try uleb128(diw, column + 1); |
| 1145 | const external_relocs = &dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).external_relocs; |
| 1146 | try external_relocs.ensureUnusedCapacity(dwarf.gpa, 2); |
| 1096 | 1147 | external_relocs.appendAssumeCapacity(.{ |
| 1097 | | .source_entry = wip_nav.entry, |
| 1098 | 1148 | .source_off = @intCast(wip_nav.debug_info.items.len), |
| 1099 | 1149 | .target_sym = wip_nav.func_sym_index, |
| 1100 | 1150 | .target_off = code_off, |
| ... | ... | @@ -1102,7 +1152,6 @@ pub const WipNav = struct { |
| 1102 | 1152 | try diw.writeByteNTimes(0, @intFromEnum(dwarf.address_size)); |
| 1103 | 1153 | wip_nav.inlined_funcs_high_reloc.appendAssumeCapacity(@intCast(external_relocs.items.len)); |
| 1104 | 1154 | external_relocs.appendAssumeCapacity(.{ |
| 1105 | | .source_entry = wip_nav.entry, |
| 1106 | 1155 | .source_off = @intCast(wip_nav.debug_info.items.len), |
| 1107 | 1156 | .target_sym = wip_nav.func_sym_index, |
| 1108 | 1157 | .target_off = undefined, |
| ... | ... | @@ -1112,7 +1161,7 @@ pub const WipNav = struct { |
| 1112 | 1161 | } |
| 1113 | 1162 | |
| 1114 | 1163 | pub fn leaveInlineFunc(wip_nav: *WipNav, func: InternPool.Index, code_off: u64) UpdateError!void { |
| 1115 | | const external_relocs = &wip_nav.dwarf.debug_info.section.getUnit(wip_nav.unit).external_relocs; |
| 1164 | const external_relocs = &wip_nav.dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).external_relocs; |
| 1116 | 1165 | external_relocs.items[wip_nav.inlined_funcs_high_reloc.pop()].target_off = code_off; |
| 1117 | 1166 | try uleb128(wip_nav.debug_info.writer(wip_nav.dwarf.gpa), @intFromEnum(AbbrevCode.null)); |
| 1118 | 1167 | try wip_nav.setInlineFunc(func); |
| ... | ... | @@ -1137,8 +1186,7 @@ pub const WipNav = struct { |
| 1137 | 1186 | try dlw.writeByte(DW.LNS.extended_op); |
| 1138 | 1187 | try uleb128(dlw, 1 + dwarf.sectionOffsetBytes()); |
| 1139 | 1188 | try dlw.writeByte(DW.LNE.ZIG_set_decl); |
| 1140 | | try dwarf.debug_line.section.getUnit(wip_nav.unit).cross_section_relocs.append(dwarf.gpa, .{ |
| 1141 | | .source_entry = wip_nav.entry.toOptional(), |
| 1189 | try dwarf.debug_line.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).cross_section_relocs.append(dwarf.gpa, .{ |
| 1142 | 1190 | .source_off = @intCast(wip_nav.debug_line.items.len), |
| 1143 | 1191 | .target_sec = .debug_info, |
| 1144 | 1192 | .target_unit = new_unit, |
| ... | ... | @@ -1174,9 +1222,9 @@ pub const WipNav = struct { |
| 1174 | 1222 | fn infoSectionOffset(wip_nav: *WipNav, sec: Section.Index, unit: Unit.Index, entry: Entry.Index, off: u32) UpdateError!void { |
| 1175 | 1223 | const dwarf = wip_nav.dwarf; |
| 1176 | 1224 | const gpa = dwarf.gpa; |
| 1225 | const entry_ptr = dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry); |
| 1177 | 1226 | if (sec != .debug_info) { |
| 1178 | | try dwarf.debug_info.section.getUnit(wip_nav.unit).cross_section_relocs.append(gpa, .{ |
| 1179 | | .source_entry = wip_nav.entry.toOptional(), |
| 1227 | try entry_ptr.cross_section_relocs.append(gpa, .{ |
| 1180 | 1228 | .source_off = @intCast(wip_nav.debug_info.items.len), |
| 1181 | 1229 | .target_sec = sec, |
| 1182 | 1230 | .target_unit = unit, |
| ... | ... | @@ -1184,16 +1232,14 @@ pub const WipNav = struct { |
| 1184 | 1232 | .target_off = off, |
| 1185 | 1233 | }); |
| 1186 | 1234 | } else if (unit != wip_nav.unit) { |
| 1187 | | try dwarf.debug_info.section.getUnit(wip_nav.unit).cross_unit_relocs.append(gpa, .{ |
| 1188 | | .source_entry = wip_nav.entry.toOptional(), |
| 1235 | try entry_ptr.cross_unit_relocs.append(gpa, .{ |
| 1189 | 1236 | .source_off = @intCast(wip_nav.debug_info.items.len), |
| 1190 | 1237 | .target_unit = unit, |
| 1191 | 1238 | .target_entry = entry.toOptional(), |
| 1192 | 1239 | .target_off = off, |
| 1193 | 1240 | }); |
| 1194 | 1241 | } else { |
| 1195 | | try dwarf.debug_info.section.getUnit(wip_nav.unit).cross_entry_relocs.append(gpa, .{ |
| 1196 | | .source_entry = wip_nav.entry.toOptional(), |
| 1242 | try entry_ptr.cross_entry_relocs.append(gpa, .{ |
| 1197 | 1243 | .source_off = @intCast(wip_nav.debug_info.items.len), |
| 1198 | 1244 | .target_entry = entry, |
| 1199 | 1245 | .target_off = off, |
| ... | ... | @@ -1208,8 +1254,7 @@ pub const WipNav = struct { |
| 1208 | 1254 | |
| 1209 | 1255 | fn addrSym(wip_nav: *WipNav, sym_index: u32) UpdateError!void { |
| 1210 | 1256 | const dwarf = wip_nav.dwarf; |
| 1211 | | try dwarf.debug_info.section.getUnit(wip_nav.unit).external_relocs.append(dwarf.gpa, .{ |
| 1212 | | .source_entry = wip_nav.entry, |
| 1257 | try dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).external_relocs.append(dwarf.gpa, .{ |
| 1213 | 1258 | .source_off = @intCast(wip_nav.debug_info.items.len), |
| 1214 | 1259 | .target_sym = sym_index, |
| 1215 | 1260 | }); |
| ... | ... | @@ -1269,10 +1314,9 @@ pub const WipNav = struct { |
| 1269 | 1314 | |
| 1270 | 1315 | fn refForward(wip_nav: *WipNav) std.mem.Allocator.Error!u32 { |
| 1271 | 1316 | const dwarf = wip_nav.dwarf; |
| 1272 | | const cross_entry_relocs = &dwarf.debug_info.section.getUnit(wip_nav.unit).cross_entry_relocs; |
| 1317 | const cross_entry_relocs = &dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).cross_entry_relocs; |
| 1273 | 1318 | const reloc_index: u32 = @intCast(cross_entry_relocs.items.len); |
| 1274 | 1319 | try cross_entry_relocs.append(dwarf.gpa, .{ |
| 1275 | | .source_entry = wip_nav.entry.toOptional(), |
| 1276 | 1320 | .source_off = @intCast(wip_nav.debug_info.items.len), |
| 1277 | 1321 | .target_entry = undefined, |
| 1278 | 1322 | .target_off = undefined, |
| ... | ... | @@ -1282,7 +1326,7 @@ pub const WipNav = struct { |
| 1282 | 1326 | } |
| 1283 | 1327 | |
| 1284 | 1328 | fn finishForward(wip_nav: *WipNav, reloc_index: u32) void { |
| 1285 | | const reloc = &wip_nav.dwarf.debug_info.section.getUnit(wip_nav.unit).cross_entry_relocs.items[reloc_index]; |
| 1329 | const reloc = &wip_nav.dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).cross_entry_relocs.items[reloc_index]; |
| 1286 | 1330 | reloc.target_entry = wip_nav.entry; |
| 1287 | 1331 | reloc.target_off = @intCast(wip_nav.debug_info.items.len); |
| 1288 | 1332 | } |
| ... | ... | @@ -1595,7 +1639,15 @@ pub fn initWipNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool.Nav.In |
| 1595 | 1639 | const unit = try dwarf.getUnit(file.mod); |
| 1596 | 1640 | const nav_gop = try dwarf.navs.getOrPut(dwarf.gpa, nav_index); |
| 1597 | 1641 | errdefer _ = dwarf.navs.pop(); |
| 1598 | | if (!nav_gop.found_existing) nav_gop.value_ptr.* = try dwarf.addCommonEntry(unit); |
| 1642 | if (nav_gop.found_existing) { |
| 1643 | for ([_]*Section{ |
| 1644 | &dwarf.debug_aranges.section, |
| 1645 | &dwarf.debug_info.section, |
| 1646 | &dwarf.debug_line.section, |
| 1647 | &dwarf.debug_loclists.section, |
| 1648 | &dwarf.debug_rnglists.section, |
| 1649 | }) |sec| sec.getUnit(unit).getEntry(nav_gop.value_ptr.*).clear(); |
| 1650 | } else nav_gop.value_ptr.* = try dwarf.addCommonEntry(unit); |
| 1599 | 1651 | const nav_val = zcu.navValue(nav_index); |
| 1600 | 1652 | var wip_nav: WipNav = .{ |
| 1601 | 1653 | .dwarf = dwarf, |
| ... | ... | @@ -1759,17 +1811,15 @@ pub fn initWipNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool.Nav.In |
| 1759 | 1811 | try wip_nav.strp(nav.name.toSlice(ip)); |
| 1760 | 1812 | try wip_nav.strp(nav.fqn.toSlice(ip)); |
| 1761 | 1813 | try wip_nav.refType(Type.fromInterned(func_type.return_type)); |
| 1762 | | const external_relocs = &dwarf.debug_info.section.getUnit(unit).external_relocs; |
| 1814 | const external_relocs = &dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).external_relocs; |
| 1763 | 1815 | try external_relocs.ensureUnusedCapacity(dwarf.gpa, 2); |
| 1764 | 1816 | external_relocs.appendAssumeCapacity(.{ |
| 1765 | | .source_entry = wip_nav.entry, |
| 1766 | 1817 | .source_off = @intCast(wip_nav.debug_info.items.len), |
| 1767 | 1818 | .target_sym = sym_index, |
| 1768 | 1819 | }); |
| 1769 | 1820 | try diw.writeByteNTimes(0, @intFromEnum(dwarf.address_size)); |
| 1770 | 1821 | wip_nav.func_high_reloc = @intCast(external_relocs.items.len); |
| 1771 | 1822 | external_relocs.appendAssumeCapacity(.{ |
| 1772 | | .source_entry = wip_nav.entry, |
| 1773 | 1823 | .source_off = @intCast(wip_nav.debug_info.items.len), |
| 1774 | 1824 | .target_sym = sym_index, |
| 1775 | 1825 | .target_off = undefined, |
| ... | ... | @@ -1785,8 +1835,7 @@ pub fn initWipNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool.Nav.In |
| 1785 | 1835 | if (dwarf.incremental()) { |
| 1786 | 1836 | try uleb128(dlw, 1 + dwarf.sectionOffsetBytes()); |
| 1787 | 1837 | try dlw.writeByte(DW.LNE.ZIG_set_decl); |
| 1788 | | try dwarf.debug_line.section.getUnit(wip_nav.unit).cross_section_relocs.append(dwarf.gpa, .{ |
| 1789 | | .source_entry = wip_nav.entry.toOptional(), |
| 1838 | try dwarf.debug_line.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).cross_section_relocs.append(dwarf.gpa, .{ |
| 1790 | 1839 | .source_off = @intCast(wip_nav.debug_line.items.len), |
| 1791 | 1840 | .target_sec = .debug_info, |
| 1792 | 1841 | .target_unit = wip_nav.unit, |
| ... | ... | @@ -1801,8 +1850,7 @@ pub fn initWipNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool.Nav.In |
| 1801 | 1850 | } else { |
| 1802 | 1851 | try uleb128(dlw, 1 + @intFromEnum(dwarf.address_size)); |
| 1803 | 1852 | try dlw.writeByte(DW.LNE.set_address); |
| 1804 | | try dwarf.debug_line.section.getUnit(wip_nav.unit).external_relocs.append(dwarf.gpa, .{ |
| 1805 | | .source_entry = wip_nav.entry, |
| 1853 | try dwarf.debug_line.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).external_relocs.append(dwarf.gpa, .{ |
| 1806 | 1854 | .source_off = @intCast(wip_nav.debug_line.items.len), |
| 1807 | 1855 | .target_sym = sym_index, |
| 1808 | 1856 | }); |
| ... | ... | @@ -1835,7 +1883,7 @@ pub fn finishWipNav( |
| 1835 | 1883 | log.debug("finishWipNav({})", .{nav.fqn.fmt(ip)}); |
| 1836 | 1884 | |
| 1837 | 1885 | if (wip_nav.func != .none) { |
| 1838 | | const external_relocs = &dwarf.debug_info.section.getUnit(wip_nav.unit).external_relocs; |
| 1886 | const external_relocs = &dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).external_relocs; |
| 1839 | 1887 | external_relocs.items[wip_nav.func_high_reloc].target_off = sym.size; |
| 1840 | 1888 | if (wip_nav.any_children) { |
| 1841 | 1889 | const diw = wip_nav.debug_info.writer(dwarf.gpa); |
| ... | ... | @@ -1847,8 +1895,7 @@ pub fn finishWipNav( |
| 1847 | 1895 | ); |
| 1848 | 1896 | |
| 1849 | 1897 | var aranges_entry = [1]u8{0} ** (8 + 8); |
| 1850 | | try dwarf.debug_aranges.section.getUnit(wip_nav.unit).external_relocs.append(dwarf.gpa, .{ |
| 1851 | | .source_entry = wip_nav.entry, |
| 1898 | try dwarf.debug_aranges.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).external_relocs.append(dwarf.gpa, .{ |
| 1852 | 1899 | .target_sym = sym.index, |
| 1853 | 1900 | }); |
| 1854 | 1901 | dwarf.writeInt(aranges_entry[0..@intFromEnum(dwarf.address_size)], 0); |
| ... | ... | @@ -1862,14 +1909,12 @@ pub fn finishWipNav( |
| 1862 | 1909 | aranges_entry[0 .. @intFromEnum(dwarf.address_size) * 2], |
| 1863 | 1910 | ); |
| 1864 | 1911 | |
| 1865 | | try dwarf.debug_rnglists.section.getUnit(wip_nav.unit).external_relocs.appendSlice(dwarf.gpa, &.{ |
| 1912 | try dwarf.debug_rnglists.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).external_relocs.appendSlice(dwarf.gpa, &.{ |
| 1866 | 1913 | .{ |
| 1867 | | .source_entry = wip_nav.entry, |
| 1868 | 1914 | .source_off = 1, |
| 1869 | 1915 | .target_sym = sym.index, |
| 1870 | 1916 | }, |
| 1871 | 1917 | .{ |
| 1872 | | .source_entry = wip_nav.entry, |
| 1873 | 1918 | .source_off = 1 + @intFromEnum(dwarf.address_size), |
| 1874 | 1919 | .target_sym = sym.index, |
| 1875 | 1920 | .target_off = sym.size, |
| ... | ... | @@ -1935,6 +1980,29 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool |
| 1935 | 1980 | errdefer _ = dwarf.navs.pop(); |
| 1936 | 1981 | switch (ip.indexToKey(nav_val.toIntern())) { |
| 1937 | 1982 | .func => |func| { |
| 1983 | if (nav_gop.found_existing) { |
| 1984 | const unit_ptr = dwarf.debug_info.section.getUnit(unit); |
| 1985 | const entry_ptr = unit_ptr.getEntry(nav_gop.value_ptr.*); |
| 1986 | if (entry_ptr.len >= AbbrevCode.decl_bytes) { |
| 1987 | var abbrev_code_buf: [AbbrevCode.decl_bytes]u8 = undefined; |
| 1988 | if (try dwarf.getFile().?.preadAll( |
| 1989 | &abbrev_code_buf, |
| 1990 | dwarf.debug_info.section.off + unit_ptr.off + unit_ptr.header_len + entry_ptr.off, |
| 1991 | ) != abbrev_code_buf.len) return error.InputOutput; |
| 1992 | var abbrev_code_fbs = std.io.fixedBufferStream(&abbrev_code_buf); |
| 1993 | const abbrev_code: AbbrevCode = @enumFromInt( |
| 1994 | try std.leb.readUleb128(@typeInfo(AbbrevCode).Enum.tag_type, abbrev_code_fbs.reader()), |
| 1995 | ); |
| 1996 | switch (abbrev_code) { |
| 1997 | else => unreachable, |
| 1998 | .decl_func, .decl_func_empty => return, |
| 1999 | .decl_func_generic, .decl_func_generic_empty => {}, |
| 2000 | } |
| 2001 | } |
| 2002 | entry_ptr.clear(); |
| 2003 | } else nav_gop.value_ptr.* = try dwarf.addCommonEntry(unit); |
| 2004 | wip_nav.entry = nav_gop.value_ptr.*; |
| 2005 | |
| 1938 | 2006 | const parent_type, const accessibility: u8 = if (nav.analysis_owner.unwrap()) |cau| parent: { |
| 1939 | 2007 | const parent_namespace_ptr = ip.namespacePtr(ip.getCau(cau).namespace); |
| 1940 | 2008 | break :parent .{ |
| ... | ... | @@ -1948,12 +2016,9 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool |
| 1948 | 2016 | }; |
| 1949 | 2017 | } else .{ zcu.fileRootType(inst_info.file), DW.ACCESS.private }; |
| 1950 | 2018 | |
| 1951 | | if (!nav_gop.found_existing) nav_gop.value_ptr.* = try dwarf.addCommonEntry(unit); |
| 1952 | | wip_nav.entry = nav_gop.value_ptr.*; |
| 1953 | | |
| 1954 | 2019 | const func_type = ip.indexToKey(func.ty).func_type; |
| 1955 | 2020 | const diw = wip_nav.debug_info.writer(dwarf.gpa); |
| 1956 | | try uleb128(diw, @intFromEnum(@as(AbbrevCode, if (func_type.param_types.len > 0 and func_type.is_var_args) .decl_func_generic else .decl_func_generic_empty))); |
| 2021 | try uleb128(diw, @intFromEnum(@as(AbbrevCode, if (func_type.param_types.len > 0 or func_type.is_var_args) .decl_func_generic else .decl_func_generic_empty))); |
| 1957 | 2022 | try wip_nav.refType(Type.fromInterned(parent_type)); |
| 1958 | 2023 | assert(wip_nav.debug_info.items.len == DebugInfo.declEntryLineOff(dwarf)); |
| 1959 | 2024 | try diw.writeInt(u32, @intCast(loc.line + 1), dwarf.endian); |
| ... | ... | @@ -1961,12 +2026,14 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool |
| 1961 | 2026 | try diw.writeByte(accessibility); |
| 1962 | 2027 | try wip_nav.strp(nav.name.toSlice(ip)); |
| 1963 | 2028 | try wip_nav.refType(Type.fromInterned(func_type.return_type)); |
| 1964 | | for (0..func_type.param_types.len) |param_index| { |
| 1965 | | try uleb128(diw, @intFromEnum(AbbrevCode.func_type_param)); |
| 1966 | | try wip_nav.refType(Type.fromInterned(func_type.param_types.get(ip)[param_index])); |
| 2029 | if (func_type.param_types.len > 0 or func_type.is_var_args) { |
| 2030 | for (0..func_type.param_types.len) |param_index| { |
| 2031 | try uleb128(diw, @intFromEnum(AbbrevCode.func_type_param)); |
| 2032 | try wip_nav.refType(Type.fromInterned(func_type.param_types.get(ip)[param_index])); |
| 2033 | } |
| 2034 | if (func_type.is_var_args) try uleb128(diw, @intFromEnum(AbbrevCode.is_var_args)); |
| 2035 | try uleb128(diw, @intFromEnum(AbbrevCode.null)); |
| 1967 | 2036 | } |
| 1968 | | if (func_type.is_var_args) try uleb128(diw, @intFromEnum(AbbrevCode.is_var_args)); |
| 1969 | | try uleb128(diw, @intFromEnum(AbbrevCode.null)); |
| 1970 | 2037 | }, |
| 1971 | 2038 | .struct_type => done: { |
| 1972 | 2039 | const loaded_struct = ip.loadStructType(nav_val.toIntern()); |
| ... | ... | @@ -2535,13 +2602,25 @@ fn updateType( |
| 2535 | 2602 | .error_union_type => |error_union_type| { |
| 2536 | 2603 | const error_union_error_set_type = Type.fromInterned(error_union_type.error_set_type); |
| 2537 | 2604 | const error_union_payload_type = Type.fromInterned(error_union_type.payload_type); |
| 2538 | | const error_union_error_set_offset = codegen.errUnionErrorOffset(error_union_payload_type, pt); |
| 2539 | | const error_union_payload_offset = codegen.errUnionPayloadOffset(error_union_payload_type, pt); |
| 2605 | const error_union_error_set_offset, const error_union_payload_offset = switch (error_union_type.payload_type) { |
| 2606 | .generic_poison_type => .{ 0, 0 }, |
| 2607 | else => .{ |
| 2608 | codegen.errUnionErrorOffset(error_union_payload_type, pt), |
| 2609 | codegen.errUnionPayloadOffset(error_union_payload_type, pt), |
| 2610 | }, |
| 2611 | }; |
| 2540 | 2612 | |
| 2541 | 2613 | try uleb128(diw, @intFromEnum(AbbrevCode.union_type)); |
| 2542 | 2614 | try wip_nav.strp(name); |
| 2543 | | try uleb128(diw, ty.abiSize(pt)); |
| 2544 | | try uleb128(diw, ty.abiAlignment(pt).toByteUnits().?); |
| 2615 | if (error_union_type.error_set_type != .generic_poison_type and |
| 2616 | error_union_type.payload_type != .generic_poison_type) |
| 2617 | { |
| 2618 | try uleb128(diw, ty.abiSize(pt)); |
| 2619 | try uleb128(diw, ty.abiAlignment(pt).toByteUnits().?); |
| 2620 | } else { |
| 2621 | try uleb128(diw, 0); |
| 2622 | try uleb128(diw, 1); |
| 2623 | } |
| 2545 | 2624 | { |
| 2546 | 2625 | try uleb128(diw, @intFromEnum(AbbrevCode.tagged_union)); |
| 2547 | 2626 | try wip_nav.infoSectionOffset( |
| ... | ... | @@ -2723,10 +2802,16 @@ fn updateType( |
| 2723 | 2802 | } |
| 2724 | 2803 | if (error_set_type.names.len > 0) try uleb128(diw, @intFromEnum(AbbrevCode.null)); |
| 2725 | 2804 | }, |
| 2726 | | .inferred_error_set_type => |func| { |
| 2727 | | try uleb128(diw, @intFromEnum(AbbrevCode.inferred_error_set_type)); |
| 2728 | | try wip_nav.strp(name); |
| 2729 | | try wip_nav.refType(Type.fromInterned(ip.funcIesResolvedUnordered(func))); |
| 2805 | .inferred_error_set_type => |func| switch (ip.funcIesResolvedUnordered(func)) { |
| 2806 | .none => { |
| 2807 | try uleb128(diw, @intFromEnum(AbbrevCode.void_type)); |
| 2808 | try wip_nav.strp(name); |
| 2809 | }, |
| 2810 | else => |ies| { |
| 2811 | try uleb128(diw, @intFromEnum(AbbrevCode.inferred_error_set_type)); |
| 2812 | try wip_nav.strp(name); |
| 2813 | try wip_nav.refType(Type.fromInterned(ies)); |
| 2814 | }, |
| 2730 | 2815 | }, |
| 2731 | 2816 | |
| 2732 | 2817 | // values, not types |
| ... | ... | @@ -3072,7 +3157,8 @@ pub fn flushModule(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void { |
| 3072 | 3157 | if (dwarf.debug_aranges.section.dirty) { |
| 3073 | 3158 | for (dwarf.debug_aranges.section.units.items, 0..) |*unit_ptr, unit_index| { |
| 3074 | 3159 | const unit: Unit.Index = @enumFromInt(unit_index); |
| 3075 | | try unit_ptr.cross_section_relocs.ensureUnusedCapacity(dwarf.gpa, 1); |
| 3160 | unit_ptr.clear(); |
| 3161 | try unit_ptr.cross_section_relocs.ensureTotalCapacity(dwarf.gpa, 1); |
| 3076 | 3162 | header.clearRetainingCapacity(); |
| 3077 | 3163 | try header.ensureTotalCapacity(unit_ptr.header_len); |
| 3078 | 3164 | const unit_len = (if (unit_ptr.next.unwrap()) |next_unit| |
| ... | ... | @@ -3103,8 +3189,9 @@ pub fn flushModule(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void { |
| 3103 | 3189 | if (dwarf.debug_info.section.dirty) { |
| 3104 | 3190 | for (dwarf.mods.keys(), dwarf.mods.values(), dwarf.debug_info.section.units.items, 0..) |mod, mod_info, *unit_ptr, unit_index| { |
| 3105 | 3191 | const unit: Unit.Index = @enumFromInt(unit_index); |
| 3106 | | try unit_ptr.cross_unit_relocs.ensureUnusedCapacity(dwarf.gpa, 1); |
| 3107 | | try unit_ptr.cross_section_relocs.ensureUnusedCapacity(dwarf.gpa, 7); |
| 3192 | unit_ptr.clear(); |
| 3193 | try unit_ptr.cross_unit_relocs.ensureTotalCapacity(dwarf.gpa, 1); |
| 3194 | try unit_ptr.cross_section_relocs.ensureTotalCapacity(dwarf.gpa, 7); |
| 3108 | 3195 | header.clearRetainingCapacity(); |
| 3109 | 3196 | try header.ensureTotalCapacity(unit_ptr.header_len); |
| 3110 | 3197 | const unit_len = (if (unit_ptr.next.unwrap()) |next_unit| |
| ... | ... | @@ -3195,7 +3282,8 @@ pub fn flushModule(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void { |
| 3195 | 3282 | for (dwarf.mods.values(), dwarf.debug_line.section.units.items) |mod_info, *unit| |
| 3196 | 3283 | try unit.resizeHeader(&dwarf.debug_line.section, dwarf, DebugLine.headerBytes(dwarf, @intCast(mod_info.dirs.count()), @intCast(mod_info.files.count()))); |
| 3197 | 3284 | for (dwarf.mods.values(), dwarf.debug_line.section.units.items) |mod_info, *unit| { |
| 3198 | | try unit.cross_section_relocs.ensureUnusedCapacity(dwarf.gpa, 2 * (1 + mod_info.files.count())); |
| 3285 | unit.clear(); |
| 3286 | try unit.cross_section_relocs.ensureTotalCapacity(dwarf.gpa, 2 * (1 + mod_info.files.count())); |
| 3199 | 3287 | header.clearRetainingCapacity(); |
| 3200 | 3288 | try header.ensureTotalCapacity(unit.header_len); |
| 3201 | 3289 | const unit_len = (if (unit.next.unwrap()) |next_unit| |