authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2026-08-12 18:12:19-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2026-09-03 12:59:38-04:00
logfd9f45c358837b0091b795fad1c75f4efc8f4fc4
tree3b1851b1e8bb944839467fff775848224c61d6b3
parent8530e997ea6673ebdeb2c90d6d2eb2d777a99fcb

Elf2: rework lost handling


13 files changed, 451 insertions(+), 318 deletions(-)

lib/std/dwarf/TAG.zig+1
......@@ -120,3 +120,4 @@ pub const PGI_interface_block = 0xA020;
120120// ZIG extensions.
121121pub const ZIG_padding = 0xfdb1;
122122pub const ZIG_comptime_value = 0xfdb2;
123pub const ZIG_lost_declaration = 0xfdb3;
src/Zcu/PerThread.zig+4-1
......@@ -877,7 +877,10 @@ fn updateZirRefs(pt: Zcu.PerThread) (Io.Cancelable || Allocator.Error)!void {
877877 const new_line = new_zir.getDeclaration(new_inst).src_line;
878878 if (old_line != new_line) {
879879 comp.link_prog_node.increaseEstimatedTotalItems(1);
880 try comp.link_queue.enqueueZcu(comp, pt.tid, .{ .debug_update_line_number = tracked_inst_index });
880 try comp.link_queue.enqueueZcu(comp, pt.tid, .{ .debug_update_line_number = .{
881 .inst = tracked_inst_index,
882 .line = new_line,
883 } });
881884 }
882885 },
883886 else => {},
src/link.zig+9-5
......@@ -870,10 +870,11 @@ pub const File = struct {
870870 /// On an incremental update, fixup the line number of all `Nav`s at the given `TrackedInst`, because
871871 /// its line number has changed. The ZIR instruction `ti_id` has tag `.declaration`.
872872 /// Never called when LLVM is codegenning the ZCU.
873 fn updateLineNumber(base: *File, pt: Zcu.PerThread, ti_id: InternPool.TrackedInst.Index) Error!void {
873 fn updateLineNumber(base: *File, pt: Zcu.PerThread, ti_id: InternPool.TrackedInst.Index, line: u32) Error!void {
874874 assert(pt.zcu.llvm_object == null);
875875 {
876876 const ti = ti_id.resolveFull(&pt.zcu.intern_pool).?;
877 assert(ti.inst != .main_struct_inst);
877878 const file = pt.zcu.fileByIndex(ti.file);
878879 const inst = file.zir.?.instructions.get(@backingInt(ti.inst));
879880 assert(inst.tag == .declaration);
......@@ -885,7 +886,7 @@ pub const File = struct {
885886 .coff2 => {},
886887 inline else => |tag| {
887888 dev.check(tag.devFeature());
888 return @as(*tag.Type(), @fieldParentPtr("base", base)).updateLineNumber(pt, ti_id);
889 return @as(*tag.Type(), @fieldParentPtr("base", base)).updateLineNumber(pt, ti_id, line);
889890 },
890891 }
891892 }
......@@ -1452,7 +1453,10 @@ pub const ZcuTask = union(enum) {
14521453 ty: InternPool.Index,
14531454 success: bool,
14541455 },
1455 debug_update_line_number: InternPool.TrackedInst.Index,
1456 debug_update_line_number: struct {
1457 inst: InternPool.TrackedInst.Index,
1458 line: u32,
1459 },
14561460 lost_tracking: InternPool.TrackedInst.Index,
14571461};
14581462
......@@ -1713,12 +1717,12 @@ pub fn doZcuTask(comp: *Compilation, tid: Zcu.PerThread.Id, task: ZcuTask) void
17131717 };
17141718 break :nav null;
17151719 },
1716 .debug_update_line_number => |ti| nav: {
1720 .debug_update_line_number => |line_update| nav: {
17171721 const nav_prog_node = comp.link_prog_node.start("Update line number", 0);
17181722 defer nav_prog_node.end();
17191723 if (pt.zcu.llvm_object == null) {
17201724 if (comp.bin_file) |lf| {
1721 lf.updateLineNumber(pt, ti) catch |err| switch (err) {
1725 lf.updateLineNumber(pt, line_update.inst, line_update.line) catch |err| switch (err) {
17221726 error.OutOfMemory => diags.setAllocFailure(),
17231727 else => |e| log.err("update line number failed: {s}", .{@errorName(e)}),
17241728 };
src/link/C.zig+2-1
......@@ -721,12 +721,13 @@ fn updateUav(
721721 rendered_decl.ctype_deps = try c.addCTypeDependencies(pt, &dg.ctype_deps);
722722}
723723
724pub fn updateLineNumber(c: *C, pt: Zcu.PerThread, ti_id: InternPool.TrackedInst.Index) error{}!void {
724pub fn updateLineNumber(c: *C, pt: Zcu.PerThread, ti_id: InternPool.TrackedInst.Index, line: u32) error{}!void {
725725 // The C backend does not currently emit "#line" directives. Even if it did, it would not be
726726 // capable of updating those line numbers without re-generating the entire declaration.
727727 _ = c;
728728 _ = pt;
729729 _ = ti_id;
730 _ = line;
730731}
731732
732733pub fn flush(c: *C, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.Error!void {
src/link/Dwarf.zig+51-59
......@@ -2202,7 +2202,7 @@ pub const WipNav = struct {
22022202 wip_nav: *WipNav,
22032203 abbrev_code: struct {
22042204 decl: AbbrevCode,
2205 generic_decl: AbbrevCode,
2205 decl_abstract: AbbrevCode,
22062206 decl_instance: AbbrevCode,
22072207 },
22082208 nav: *const InternPool.Nav,
......@@ -2216,11 +2216,11 @@ pub const WipNav = struct {
22162216
22172217 const orig_entry = wip_nav.entry;
22182218 defer wip_nav.entry = orig_entry;
2219 const parent_type, const is_generic_decl = if (nav.analysis) |analysis| parent_info: {
2219 const parent_type, const is_abstract = if (nav.analysis) |analysis| parent_info: {
22202220 const parent_type: Type = .fromInterned(zcu.namespacePtr(analysis.namespace).owner_type);
22212221 const decl_gop = try dwarf.decls.getOrPut(dwarf.gpa, analysis.zir_index);
22222222 errdefer _ = if (!decl_gop.found_existing) dwarf.decls.pop();
2223 const was_generic_decl = decl_gop.found_existing and
2223 const was_abstract = decl_gop.found_existing and
22242224 switch (try dwarf.debug_info.declAbbrevCode(wip_nav.unit, decl_gop.value_ptr.*)) {
22252225 .null,
22262226 .decl_alias,
......@@ -2242,9 +2242,9 @@ pub const WipNav = struct {
22422242 .decl_extern_nullary_func,
22432243 .decl_extern_func,
22442244 => false,
2245 .generic_decl_var,
2246 .generic_decl_const,
2247 .generic_decl_func,
2245 .decl_abstract_var,
2246 .decl_abstract_const,
2247 .decl_abstract_func,
22482248 => true,
22492249
22502250 // This comes from a decl which was previously generated as an incomplete value
......@@ -2255,11 +2255,11 @@ pub const WipNav = struct {
22552255 else => |t| std.debug.panic("bad decl abbrev code: {t}", .{t}),
22562256 };
22572257 if (parent_type.getCaptures(zcu).len == 0) {
2258 if (was_generic_decl) try dwarf.freeCommonEntry(wip_nav.unit, decl_gop.value_ptr.*);
2258 if (was_abstract) try dwarf.freeCommonEntry(wip_nav.unit, decl_gop.value_ptr.*);
22592259 decl_gop.value_ptr.* = orig_entry;
22602260 break :parent_info .{ parent_type, false };
22612261 } else {
2262 if (was_generic_decl)
2262 if (was_abstract)
22632263 dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(decl_gop.value_ptr.*).clear()
22642264 else
22652265 decl_gop.value_ptr.* = try dwarf.addCommonEntry(wip_nav.unit);
......@@ -2268,8 +2268,8 @@ pub const WipNav = struct {
22682268 }
22692269 } else .{ null, false };
22702270
2271 try wip_nav.abbrevCode(if (is_generic_decl) abbrev_code.generic_decl else abbrev_code.decl);
2272 try wip_nav.refType((if (is_generic_decl) null else parent_type) orelse
2271 try wip_nav.abbrevCode(if (is_abstract) abbrev_code.decl_abstract else abbrev_code.decl);
2272 try wip_nav.refType((if (is_abstract) null else parent_type) orelse
22732273 .fromInterned(zcu.fileRootType(file)));
22742274 assert(diw.end == DebugInfo.declEntryLineOff(dwarf));
22752275 try diw.writeInt(u32, decl.src_line + 1, dwarf.endian);
......@@ -2277,14 +2277,14 @@ pub const WipNav = struct {
22772277 try diw.writeByte(if (decl.is_pub) DW.ACCESS.public else DW.ACCESS.private);
22782278 try wip_nav.strp(nav.name.toSlice(ip));
22792279
2280 if (!is_generic_decl) return;
2281 const generic_decl_entry = wip_nav.entry;
2282 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, generic_decl_entry, dwarf, wip_nav.debug_info.written());
2280 if (!is_abstract) return;
2281 const abstract_entry = wip_nav.entry;
2282 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, abstract_entry, dwarf, wip_nav.debug_info.written());
22832283 wip_nav.debug_info.clearRetainingCapacity();
22842284 wip_nav.entry = orig_entry;
22852285 try wip_nav.abbrevCode(abbrev_code.decl_instance);
22862286 try wip_nav.refType(parent_type.?);
2287 try wip_nav.infoSectionOffset(.debug_info, wip_nav.unit, generic_decl_entry, 0);
2287 try wip_nav.infoSectionOffset(.debug_info, wip_nav.unit, abstract_entry, 0);
22882288 }
22892289};
22902290
......@@ -2680,11 +2680,11 @@ fn initWipNavInner(
26802680 const diw = &wip_nav.debug_info.writer;
26812681 try wip_nav.declCommon(if (func_type.param_types.len > 0 or func_type.is_var_args) .{
26822682 .decl = .decl_extern_func,
2683 .generic_decl = .generic_decl_func,
2683 .decl_abstract = .decl_abstract_func,
26842684 .decl_instance = .decl_instance_extern_func,
26852685 } else .{
26862686 .decl = .decl_extern_nullary_func,
2687 .generic_decl = .generic_decl_func,
2687 .decl_abstract = .decl_abstract_func,
26882688 .decl_instance = .decl_instance_extern_nullary_func,
26892689 }, &nav, inst_info.file, &decl);
26902690 try wip_nav.strp(@"extern".name.toSlice(ip));
......@@ -2705,7 +2705,7 @@ fn initWipNavInner(
27052705 .func => |func| if (func.owner_nav != nav_index) {
27062706 try wip_nav.declCommon(.{
27072707 .decl = .decl_alias,
2708 .generic_decl = .generic_decl_const,
2708 .decl_abstract = .decl_abstract_const,
27092709 .decl_instance = .decl_instance_alias,
27102710 }, &nav, inst_info.file, &decl);
27112711 try wip_nav.refNav(func.owner_nav);
......@@ -2758,7 +2758,7 @@ fn initWipNavInner(
27582758 const diw = &wip_nav.debug_info.writer;
27592759 try wip_nav.declCommon(.{
27602760 .decl = .decl_func,
2761 .generic_decl = .generic_decl_func,
2761 .decl_abstract = .decl_abstract_func,
27622762 .decl_instance = .decl_instance_func,
27632763 }, &nav, inst_info.file, &decl);
27642764 try wip_nav.strp(switch (decl.linkage) {
......@@ -2817,10 +2817,10 @@ fn initWipNavInner(
28172817 const diw = &wip_nav.debug_info.writer;
28182818 try wip_nav.declCommon(.{
28192819 .decl = .decl_var,
2820 .generic_decl = switch (decl.kind) {
2820 .decl_abstract = switch (decl.kind) {
28212821 .unnamed_test, .@"test", .decltest, .@"comptime" => unreachable,
2822 .@"const" => .generic_decl_const,
2823 .@"var" => .generic_decl_var,
2822 .@"const" => .decl_abstract_const,
2823 .@"var" => .decl_abstract_var,
28242824 },
28252825 .decl_instance = .decl_instance_var,
28262826 }, &nav, inst_info.file, &decl);
......@@ -3181,7 +3181,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
31813181 .alias => {
31823182 try wip_nav.declCommon(.{
31833183 .decl = .decl_alias,
3184 .generic_decl = .generic_decl_const,
3184 .decl_abstract = .decl_abstract_const,
31853185 .decl_instance = .decl_instance_alias,
31863186 }, &nav, inst_info.file, &decl);
31873187 try wip_nav.refType(nav_val.toType());
......@@ -3189,7 +3189,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
31893189 .@"var" => {
31903190 try wip_nav.declCommon(.{
31913191 .decl = .decl_var,
3192 .generic_decl = .generic_decl_var,
3192 .decl_abstract = .decl_abstract_var,
31933193 .decl_instance = .decl_instance_var,
31943194 }, &nav, inst_info.file, &decl);
31953195 try wip_nav.strp(switch (decl.linkage) {
......@@ -3209,19 +3209,19 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
32093209 const has_comptime_state = nav_ty.comptimeOnly(zcu);
32103210 try wip_nav.declCommon(if (has_runtime_bits and has_comptime_state) .{
32113211 .decl = .decl_const_runtime_bits_comptime_state,
3212 .generic_decl = .generic_decl_const,
3212 .decl_abstract = .decl_abstract_const,
32133213 .decl_instance = .decl_instance_const_runtime_bits_comptime_state,
32143214 } else if (has_comptime_state) .{
32153215 .decl = .decl_const_comptime_state,
3216 .generic_decl = .generic_decl_const,
3216 .decl_abstract = .decl_abstract_const,
32173217 .decl_instance = .decl_instance_const_comptime_state,
32183218 } else if (has_runtime_bits) .{
32193219 .decl = .decl_const_runtime_bits,
3220 .generic_decl = .generic_decl_const,
3220 .decl_abstract = .decl_abstract_const,
32213221 .decl_instance = .decl_instance_const_runtime_bits,
32223222 } else .{
32233223 .decl = .decl_const,
3224 .generic_decl = .generic_decl_const,
3224 .decl_abstract = .decl_abstract_const,
32253225 .decl_instance = .decl_instance_const,
32263226 }, &nav, inst_info.file, &decl);
32273227 try wip_nav.strp(switch (decl.linkage) {
......@@ -3245,11 +3245,11 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
32453245 } else true;
32463246 try wip_nav.declCommon(if (is_nullary) .{
32473247 .decl = .decl_nullary_func_generic,
3248 .generic_decl = .generic_decl_func,
3248 .decl_abstract = .decl_abstract_func,
32493249 .decl_instance = .decl_instance_nullary_func_generic,
32503250 } else .{
32513251 .decl = .decl_func_generic,
3252 .generic_decl = .generic_decl_func,
3252 .decl_abstract = .decl_abstract_func,
32533253 .decl_instance = .decl_instance_func_generic,
32543254 }, &nav, inst_info.file, &decl);
32553255 try wip_nav.refType(.fromInterned(func_type.return_type));
......@@ -3267,7 +3267,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
32673267 .func_alias => |owner_nav| {
32683268 try wip_nav.declCommon(.{
32693269 .decl = .decl_alias,
3270 .generic_decl = .generic_decl_const,
3270 .decl_abstract = .decl_abstract_const,
32713271 .decl_instance = .decl_instance_alias,
32723272 }, &nav, inst_info.file, &decl);
32733273 try wip_nav.refNav(owner_nav);
......@@ -3463,7 +3463,7 @@ fn emitIncompleteContainerType(
34633463 const decl = zcu.fileByIndex(file).zir.?.getDeclaration(decl_inst);
34643464 try wip_nav.declCommon(.{
34653465 .decl = .decl_namespace_struct,
3466 .generic_decl = .generic_decl_const,
3466 .decl_abstract = .decl_abstract_const,
34673467 .decl_instance = .decl_instance_namespace_struct,
34683468 }, &nav, file, &decl);
34693469 try wip_nav.debug_info.writer.writeByte(@intFromBool(true));
......@@ -3881,11 +3881,11 @@ fn updateConstInner(dwarf: *Dwarf, pt: Zcu.PerThread, debug_const_index: link.Co
38813881 const decl = zcu.fileByIndex(file).zir.?.getDeclaration(decl_inst);
38823882 try wip_nav.declCommon(if (loaded_struct.field_types.len == 0) .{
38833883 .decl = .decl_namespace_struct,
3884 .generic_decl = .generic_decl_const,
3884 .decl_abstract = .decl_abstract_const,
38853885 .decl_instance = .decl_instance_namespace_struct,
38863886 } else .{
38873887 .decl = .decl_struct,
3888 .generic_decl = .generic_decl_const,
3888 .decl_abstract = .decl_abstract_const,
38893889 .decl_instance = .decl_instance_struct,
38903890 }, &nav, file, &decl);
38913891 } else {
......@@ -3960,7 +3960,7 @@ fn updateConstInner(dwarf: *Dwarf, pt: Zcu.PerThread, debug_const_index: link.Co
39603960 const decl = zcu.fileByIndex(file).zir.?.getDeclaration(decl_inst);
39613961 try wip_nav.declCommon(.{
39623962 .decl = .decl_packed_struct,
3963 .generic_decl = .generic_decl_const,
3963 .decl_abstract = .decl_abstract_const,
39643964 .decl_instance = .decl_instance_packed_struct,
39653965 }, &nav, file, &decl);
39663966 break :t true;
......@@ -3997,7 +3997,7 @@ fn updateConstInner(dwarf: *Dwarf, pt: Zcu.PerThread, debug_const_index: link.Co
39973997 const decl = zcu.fileByIndex(file).zir.?.getDeclaration(decl_inst);
39983998 try wip_nav.declCommon(.{
39993999 .decl = .decl_union,
4000 .generic_decl = .generic_decl_const,
4000 .decl_abstract = .decl_abstract_const,
40014001 .decl_instance = .decl_instance_union,
40024002 }, &nav, file, &decl);
40034003 break :t true;
......@@ -4059,7 +4059,7 @@ fn updateConstInner(dwarf: *Dwarf, pt: Zcu.PerThread, debug_const_index: link.Co
40594059 const decl = zcu.fileByIndex(file).zir.?.getDeclaration(decl_inst);
40604060 try wip_nav.declCommon(.{
40614061 .decl = .decl_packed_union,
4062 .generic_decl = .generic_decl_const,
4062 .decl_abstract = .decl_abstract_const,
40634063 .decl_instance = .decl_instance_packed_union,
40644064 }, &nav, file, &decl);
40654065 break :t true;
......@@ -4092,11 +4092,11 @@ fn updateConstInner(dwarf: *Dwarf, pt: Zcu.PerThread, debug_const_index: link.Co
40924092 const decl = zcu.fileByIndex(file).zir.?.getDeclaration(decl_inst);
40934093 try wip_nav.declCommon(if (loaded_enum.field_names.len > 0) .{
40944094 .decl = .decl_enum,
4095 .generic_decl = .generic_decl_const,
4095 .decl_abstract = .decl_abstract_const,
40964096 .decl_instance = .decl_instance_enum,
40974097 } else .{
40984098 .decl = .decl_empty_enum,
4099 .generic_decl = .generic_decl_const,
4099 .decl_abstract = .decl_abstract_const,
41004100 .decl_instance = .decl_instance_empty_enum,
41014101 }, &nav, file, &decl);
41024102 } else {
......@@ -4134,7 +4134,7 @@ fn updateConstInner(dwarf: *Dwarf, pt: Zcu.PerThread, debug_const_index: link.Co
41344134 const decl = zcu.fileByIndex(file).zir.?.getDeclaration(decl_inst);
41354135 try wip_nav.declCommon(.{
41364136 .decl = .decl_namespace_struct,
4137 .generic_decl = .generic_decl_const,
4137 .decl_abstract = .decl_abstract_const,
41384138 .decl_instance = .decl_instance_namespace_struct,
41394139 }, &nav, file, &decl);
41404140 } else {
......@@ -4645,7 +4645,7 @@ fn optRepr(opt_child_type: Type, zcu: *const Zcu) enum { unpacked, opv_null, err
46454645 };
46464646}
46474647
4648pub fn updateLineNumber(dwarf: *Dwarf, zcu: *Zcu, zir_index: InternPool.TrackedInst.Index) UpdateError!void {
4648pub fn updateLineNumber(dwarf: *Dwarf, zcu: *Zcu, zir_index: InternPool.TrackedInst.Index, line: u32) UpdateError!void {
46494649 const comp = dwarf.bin_file.comp;
46504650 const io = comp.io;
46514651 const ip = &zcu.intern_pool;
......@@ -4653,17 +4653,9 @@ pub fn updateLineNumber(dwarf: *Dwarf, zcu: *Zcu, zir_index: InternPool.TrackedI
46534653 const inst_info = zir_index.resolveFull(ip).?;
46544654 assert(inst_info.inst != .main_struct_inst);
46554655 const file = zcu.fileByIndex(inst_info.file);
4656 const decl = file.zir.?.getDeclaration(inst_info.inst);
4657 log.debug("updateLineNumber({s}:{d}:{d} %{d} = {s})", .{
4658 file.sub_file_path,
4659 decl.src_line + 1,
4660 decl.src_column + 1,
4661 @backingInt(inst_info.inst),
4662 file.zir.?.nullTerminatedString(decl.name),
4663 });
46644656
46654657 var line_buf: [4]u8 = undefined;
4666 std.mem.writeInt(u32, &line_buf, decl.src_line + 1, dwarf.endian);
4658 std.mem.writeInt(u32, &line_buf, line + 1, dwarf.endian);
46674659
46684660 const unit = dwarf.debug_info.section.getUnit(dwarf.getUnitIfExists(file.mod.?) orelse return);
46694661 const entry = unit.getEntry(dwarf.decls.get(zir_index) orelse return);
......@@ -5144,9 +5136,9 @@ const AbbrevCode = enum {
51445136 decl_func_generic,
51455137 decl_extern_nullary_func,
51465138 decl_extern_func,
5147 generic_decl_var,
5148 generic_decl_const,
5149 generic_decl_func,
5139 decl_abstract_var,
5140 decl_abstract_const,
5141 decl_abstract_func,
51505142 decl_instance_alias,
51515143 decl_instance_empty_enum,
51525144 decl_instance_enum,
......@@ -5270,7 +5262,7 @@ const AbbrevCode = enum {
52705262 .{ .accessibility, .data1 },
52715263 .{ .name, .strp },
52725264 };
5273 const generic_decl_abbrev_common_attrs = decl_abbrev_common_attrs ++ &[_]Attr{
5265 const decl_abstract_abbrev_common_attrs = decl_abbrev_common_attrs ++ &[_]Attr{
52745266 .{ .declaration, .flag_present },
52755267 };
52765268 const decl_instance_abbrev_common_attrs = &[_]Attr{
......@@ -5455,17 +5447,17 @@ const AbbrevCode = enum {
54555447 .{ .noreturn, .flag },
54565448 },
54575449 },
5458 .generic_decl_var = .{
5450 .decl_abstract_var = .{
54595451 .tag = .variable,
5460 .attrs = generic_decl_abbrev_common_attrs,
5452 .attrs = decl_abstract_abbrev_common_attrs,
54615453 },
5462 .generic_decl_const = .{
5454 .decl_abstract_const = .{
54635455 .tag = .constant,
5464 .attrs = generic_decl_abbrev_common_attrs,
5456 .attrs = decl_abstract_abbrev_common_attrs,
54655457 },
5466 .generic_decl_func = .{
5458 .decl_abstract_func = .{
54675459 .tag = .subprogram,
5468 .attrs = generic_decl_abbrev_common_attrs,
5460 .attrs = decl_abstract_abbrev_common_attrs,
54695461 },
54705462 .decl_instance_alias = .{
54715463 .tag = .imported_declaration,
src/link/Dwarf2.zig+182-130
......@@ -8,7 +8,8 @@ units: std.array_hash_map.Auto(*Module, Unit),
88/// Indices are `link.ConstPool.Index`.
99values: std.ArrayList(Value),
1010globals: std.array_hash_map.Auto(InternPool.Nav.Index, Global),
11funcs: std.array_hash_map.Auto(InternPool.TrackedInst.Index, Func),
11funcs: std.array_hash_map.Auto(InternPool.Nav.Index, Func),
12decls: std.array_hash_map.Auto(InternPool.TrackedInst.Index, MappedFile.Node.Index),
1213
1314debug_abbrev: Abbrev,
1415frame: Frame,
......@@ -32,7 +33,8 @@ pub const Unit = struct {
3233 debug_line_header_ni: MappedFile.Node.Index.Optional,
3334 debug_line_header_changed: bool,
3435 debug_rnglists_ni: MappedFile.Node.Index.Optional,
35 debug_rnglists_offset: usize,
36 debug_rnglists_offsets_table_offset: usize,
37 debug_rnglists_end: usize,
3638
3739 pub const Index = enum(u32) {
3840 _,
......@@ -123,7 +125,6 @@ pub const Global = struct {
123125};
124126
125127pub const Func = struct {
126 owner_nav: InternPool.Nav.Index,
127128 fde_ni: MappedFile.Node.Index.Optional,
128129 debug_info_ni: MappedFile.Node.Index.Optional,
129130 debug_line_ni: MappedFile.Node.Index.Optional,
......@@ -131,7 +132,7 @@ pub const Func = struct {
131132 pub const Index = enum(u32) {
132133 _,
133134
134 pub fn srcInst(fi: Func.Index, dwarf: *Dwarf) InternPool.TrackedInst.Index {
135 pub fn nav(fi: Func.Index, dwarf: *Dwarf) InternPool.Nav.Index {
135136 return dwarf.funcs.keys()[@backingInt(fi)];
136137 }
137138
......@@ -156,7 +157,7 @@ pub const Frame = struct {
156157
157158pub const Abbrev = struct {
158159 ni: MappedFile.Node.Index.Optional,
159 offset: usize,
160 end: usize,
160161 set: std.enums.EnumSet(AbbrevCode),
161162};
162163
......@@ -628,7 +629,7 @@ pub const WipNav = struct {
628629 pub fn startDebugInfo(debug: *Debug) link.Error!void {
629630 assert(debug.wip_nav.func != .none);
630631 debug.startDebugInfoInner() catch |err| switch (err) {
631 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.info_writer),
632 error.WriteFailed => return debug.wip_nav.dwarf.reportWriteError(&debug.info_writer),
632633 else => |e| return e,
633634 };
634635 }
......@@ -637,7 +638,8 @@ pub const WipNav = struct {
637638 const zcu = debug.pt.zcu;
638639 const ip = &zcu.intern_pool;
639640 const func = zcu.funcInfo(debug.wip_nav.func);
640 const inst_info = ip.getNav(func.owner_nav).srcInst(ip).resolveFull(ip).?;
641 const src_inst = ip.getNav(func.owner_nav).srcInst(ip);
642 const inst_info = src_inst.resolveFull(ip).?;
641643 const decl = zcu.fileByIndex(inst_info.file).zir.?.getDeclaration(inst_info.inst);
642644 const nav = ip.getNav(func.owner_nav);
643645 const diw = &debug.info_writer.interface;
......@@ -655,7 +657,7 @@ pub const WipNav = struct {
655657 pub fn startDebugLine(debug: *Debug) link.Error!void {
656658 assert(debug.wip_nav.func != .none);
657659 debug.startDebugLineInner() catch |err| switch (err) {
658 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.line_writer),
660 error.WriteFailed => return debug.wip_nav.dwarf.reportWriteError(&debug.line_writer),
659661 else => |e| return e,
660662 };
661663 }
......@@ -698,24 +700,25 @@ pub const WipNav = struct {
698700 pub fn finishFunc(debug: *Debug, func_length: u64) link.Error!void {
699701 assert(debug.wip_nav.func != .none);
700702 debug.finishDebugInfo(func_length) catch |err| switch (err) {
701 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.info_writer),
703 error.WriteFailed => return debug.wip_nav.dwarf.reportWriteError(&debug.info_writer),
702704 else => |e| return e,
703705 };
704706 debug.finishDebugLine() catch |err| switch (err) {
705 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.line_writer),
707 error.WriteFailed => return debug.wip_nav.dwarf.reportWriteError(&debug.line_writer),
706708 else => |e| return e,
707709 };
708710 }
709711 fn finishDebugInfo(debug: *Debug, func_length: u64) link.EmitError!void {
712 const dwarf = debug.wip_nav.dwarf;
710713 const diw = &debug.info_writer.interface;
711714 std.mem.writeInt(
712715 u32,
713716 diw.buffered()[debug.info_func_length_offset..][0..4],
714717 @intCast(func_length),
715 debug.wip_nav.dwarf.endian,
718 dwarf.endian,
716719 );
717720 try diw.writeUleb128(@backingInt(AbbrevCode.null));
718 try debug.wip_nav.dwarf.genDebugInfoPadding(diw, diw.unusedCapacityLen());
721 try dwarf.genDebugInfoPadding(diw, diw.unusedCapacityLen());
719722 }
720723 fn finishDebugLine(debug: *Debug) link.EmitError!void {
721724 const dlw = &debug.line_writer.interface;
......@@ -731,7 +734,7 @@ pub const WipNav = struct {
731734 loc: Loc,
732735 ) link.Error!void {
733736 return debug.genLocalVarDebugInfoInner(tag, opt_name, ty, loc) catch |err| switch (err) {
734 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.info_writer),
737 error.WriteFailed => return debug.wip_nav.dwarf.reportWriteError(&debug.info_writer),
735738 else => |e| e,
736739 };
737740 }
......@@ -761,7 +764,7 @@ pub const WipNav = struct {
761764 val: ZigValue,
762765 ) link.Error!void {
763766 return debug.genLocalConstDebugInfoInner(tag, opt_name, val) catch |err| switch (err) {
764 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.info_writer),
767 error.WriteFailed => return debug.wip_nav.dwarf.reportWriteError(&debug.info_writer),
765768 else => |e| e,
766769 };
767770 }
......@@ -798,7 +801,7 @@ pub const WipNav = struct {
798801
799802 pub fn genVarArgsDebugInfo(debug: *Debug) link.Error!void {
800803 return debug.genVarArgsDebugInfoInner() catch |err| switch (err) {
801 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.info_writer),
804 error.WriteFailed => return debug.wip_nav.dwarf.reportWriteError(&debug.info_writer),
802805 else => |e| e,
803806 };
804807 }
......@@ -815,7 +818,7 @@ pub const WipNav = struct {
815818 end: bool,
816819 ) link.Error!void {
817820 return debug.advanceLineAndPcInner(delta_line, delta_pc, end) catch |err| switch (err) {
818 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.line_writer),
821 error.WriteFailed => return debug.wip_nav.dwarf.reportWriteError(&debug.line_writer),
819822 };
820823 }
821824 fn advanceLineAndPcInner(
......@@ -870,7 +873,7 @@ pub const WipNav = struct {
870873
871874 pub fn setColumn(debug: *Debug, column: u32) link.Error!void {
872875 return debug.setColumnInner(column) catch |err| switch (err) {
873 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.line_writer),
876 error.WriteFailed => return debug.wip_nav.dwarf.reportWriteError(&debug.line_writer),
874877 };
875878 }
876879 fn setColumnInner(debug: *Debug, column: u32) Writer.Error!void {
......@@ -881,7 +884,7 @@ pub const WipNav = struct {
881884
882885 pub fn negateStmt(debug: *Debug) link.Error!void {
883886 return debug.negateStmtInner() catch |err| switch (err) {
884 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.line_writer),
887 error.WriteFailed => return debug.wip_nav.dwarf.reportWriteError(&debug.line_writer),
885888 };
886889 }
887890 fn negateStmtInner(debug: *Debug) Writer.Error!void {
......@@ -890,7 +893,7 @@ pub const WipNav = struct {
890893
891894 pub fn setPrologueEnd(debug: *Debug) link.Error!void {
892895 return debug.setPrologueEndInner() catch |err| switch (err) {
893 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.line_writer),
896 error.WriteFailed => return debug.wip_nav.dwarf.reportWriteError(&debug.line_writer),
894897 };
895898 }
896899 fn setPrologueEndInner(debug: *Debug) Writer.Error!void {
......@@ -899,7 +902,7 @@ pub const WipNav = struct {
899902
900903 pub fn setEpilogueBegin(debug: *Debug) link.Error!void {
901904 return debug.setEpilogueBeginInner() catch |err| switch (err) {
902 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.line_writer),
905 error.WriteFailed => return debug.wip_nav.dwarf.reportWriteError(&debug.line_writer),
903906 };
904907 }
905908 fn setEpilogueBeginInner(debug: *Debug) Writer.Error!void {
......@@ -908,7 +911,7 @@ pub const WipNav = struct {
908911
909912 pub fn enterBlock(debug: *Debug, code_off: usize) link.Error!void {
910913 return debug.enterBlockInner(code_off) catch |err| switch (err) {
911 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.info_writer),
914 error.WriteFailed => return debug.wip_nav.dwarf.reportWriteError(&debug.info_writer),
912915 else => |e| e,
913916 };
914917 }
......@@ -928,7 +931,7 @@ pub const WipNav = struct {
928931
929932 pub fn leaveBlock(debug: *Debug, code_off: usize) link.Error!void {
930933 return debug.leaveBlockInner(code_off) catch |err| switch (err) {
931 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.info_writer),
934 error.WriteFailed => return debug.wip_nav.dwarf.reportWriteError(&debug.info_writer),
932935 else => |e| e,
933936 };
934937 }
......@@ -961,7 +964,7 @@ pub const WipNav = struct {
961964 column: u32,
962965 ) link.Error!void {
963966 return debug.enterInlineFuncInner(func, code_off, line, column) catch |err| switch (err) {
964 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.info_writer),
967 error.WriteFailed => return debug.wip_nav.dwarf.reportWriteError(&debug.info_writer),
965968 else => |e| e,
966969 };
967970 }
......@@ -996,7 +999,7 @@ pub const WipNav = struct {
996999
9971000 pub fn leaveInlineFunc(debug: *Debug, func: InternPool.Index, code_off: usize) link.Error!void {
9981001 return debug.leaveInlineFuncInner(func, code_off) catch |err| switch (err) {
999 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.info_writer),
1002 error.WriteFailed => return debug.wip_nav.dwarf.reportWriteError(&debug.info_writer),
10001003 else => |e| e,
10011004 };
10021005 }
......@@ -1029,7 +1032,7 @@ pub const WipNav = struct {
10291032
10301033 pub fn setInlineFunc(debug: *Debug, func: InternPool.Index) link.Error!void {
10311034 return debug.setInlineFuncInner(func) catch |err| switch (err) {
1032 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.line_writer),
1035 error.WriteFailed => return debug.wip_nav.dwarf.reportWriteError(&debug.line_writer),
10331036 else => |e| e,
10341037 };
10351038 }
......@@ -1187,7 +1190,7 @@ pub const WipNav = struct {
11871190
11881191 pub fn genDebugFrameHeader(wip_nav: *WipNav) link.Error!void {
11891192 wip_nav.genDebugFrameHeaderInner() catch |err| switch (err) {
1190 error.WriteFailed => return wip_nav.reportWriteError(&wip_nav.fde_writer),
1193 error.WriteFailed => return wip_nav.dwarf.reportWriteError(&wip_nav.fde_writer),
11911194 else => |e| return e,
11921195 };
11931196 }
......@@ -1230,7 +1233,7 @@ pub const WipNav = struct {
12301233
12311234 pub fn genDebugFrame(wip_nav: *WipNav, loc: u32, cfa: Cfa) link.Error!void {
12321235 return wip_nav.genDebugFrameInner(loc, cfa) catch |err| switch (err) {
1233 error.WriteFailed => return wip_nav.reportWriteError(&wip_nav.fde_writer),
1236 error.WriteFailed => return wip_nav.dwarf.reportWriteError(&wip_nav.fde_writer),
12341237 else => |e| return e,
12351238 };
12361239 }
......@@ -1336,16 +1339,6 @@ pub const WipNav = struct {
13361339 },
13371340 );
13381341 }
1339
1340 fn reportWriteError(wip_nav: *WipNav, mfnw: *const MappedFile.Node.Writer) link.Error {
1341 switch (mfnw.err.?) {
1342 else => |e| return e,
1343 error.MappedFileIo => return wip_nav.dwarf.lf.comp.link_diags.fail(
1344 "failed to write output file: {t}",
1345 .{mfnw.mf.io_err.?},
1346 ),
1347 }
1348 }
13491342};
13501343
13511344pub fn init(lf: *link.File, format: DW.Format) Dwarf {
......@@ -1365,10 +1358,11 @@ pub fn init(lf: *link.File, format: DW.Format) Dwarf {
13651358 .values = .empty,
13661359 .globals = .empty,
13671360 .funcs = .empty,
1361 .decls = .empty,
13681362
13691363 .debug_abbrev = .{
13701364 .ni = .none,
1371 .offset = 0,
1365 .end = 0,
13721366 .set = .empty,
13731367 },
13741368 .frame = .{
......@@ -1443,7 +1437,7 @@ pub fn deinit(dwarf: *Dwarf) void {
14431437 dwarf.* = undefined;
14441438}
14451439
1446pub fn initUnits(dwarf: *Dwarf, zcu: *Zcu) std.mem.Allocator.Error!void {
1440pub fn updateUnits(dwarf: *Dwarf, zcu: *Zcu) std.mem.Allocator.Error!void {
14471441 try dwarf.units.ensureTotalCapacity(zcu.gpa, zcu.module_roots.count() - dwarf.units.count());
14481442 for (zcu.module_roots.keys(), zcu.module_roots.values()) |mod, root| if (root.unwrap()) |root_zfi| {
14491443 if (!zcu.alive_files.contains(root_zfi)) continue;
......@@ -1461,7 +1455,8 @@ pub fn initUnits(dwarf: *Dwarf, zcu: *Zcu) std.mem.Allocator.Error!void {
14611455 .debug_line_header_ni = .none,
14621456 .debug_line_header_changed = true,
14631457 .debug_rnglists_ni = .none,
1464 .debug_rnglists_offset = undefined,
1458 .debug_rnglists_offsets_table_offset = undefined,
1459 .debug_rnglists_end = undefined,
14651460 };
14661461 const root_di, const root_fi = try unit_gop.value_ptr.getFile(
14671462 zcu.gpa,
......@@ -1476,15 +1471,48 @@ pub fn getUnit(dwarf: *Dwarf, mod: *Module) Unit.Index {
14761471 return @fromBackingInt(@intCast(dwarf.units.getIndex(mod).?));
14771472}
14781473
1479pub fn getFunc(dwarf: *Dwarf, owner_nav: InternPool.Nav.Index) link.Error!Func.Index {
1474pub fn getGlobal(dwarf: *Dwarf, nav: InternPool.Nav.Index) link.Error!Global.Index {
14801475 const comp = dwarf.lf.comp;
14811476 const gpa = comp.gpa;
1482 const zcu = comp.zcu.?;
1483 const ip = &zcu.intern_pool;
1484 const src_inst = ip.getNav(owner_nav).srcInst(ip);
1485 const func_gop = try dwarf.funcs.getOrPut(gpa, src_inst);
1477 const global_gop = try dwarf.globals.getOrPut(gpa, nav);
1478 if (!global_gop.found_existing) global_gop.value_ptr.* = .{
1479 .debug_info_ni = .none,
1480 };
1481 const gi: Global.Index = @fromBackingInt(@intCast(global_gop.index));
1482 if (global_gop.value_ptr.debug_info_ni == .none) {
1483 const elf = dwarf.lf.cast(.elf2).?;
1484 try elf.nodes.ensureUnusedCapacity(gpa, 1);
1485 try elf.dwarf_globals.ensureUnusedCapacity(gpa, 1);
1486 const unit = dwarf.getUnit(comp.zcu.?.zcu.navFileScope(nav).mod.?).get(dwarf);
1487 assert(unit.debug_info_ni != .none);
1488 global_gop.value_ptr.debug_info_ni = elf.addNodeAssumeCapacity(
1489 elf.mf.addLastChildNode(gpa, unit.debug_info_ni, .{
1490 .enable_next_moved = true,
1491 }) catch |err| switch (err) {
1492 else => |e| return e,
1493 error.MappedFileIo => return comp.link_diags.fail("failed to write output file: {t}", .{
1494 elf.mf.io_err.?,
1495 }),
1496 },
1497 .{ .global_debug_info = gi },
1498 );
1499 elf.dwarf_globals.addOneAssumeCapacity().* = .{
1500 .debug_info_first_target_reloc = .none,
1501 .debug_info_first_node_reloc = .none,
1502 .debug_info_first_symbol_reloc = .none,
1503 };
1504 }
1505 return gi;
1506}
1507pub fn getGlobalIfExists(dwarf: *Dwarf, nav: InternPool.Nav.Index) ?Global.Index {
1508 return @fromBackingInt(@intCast(dwarf.globals.getIndex(nav) orelse return null));
1509}
1510
1511pub fn getFunc(dwarf: *Dwarf, nav: InternPool.Nav.Index) link.Error!Func.Index {
1512 const comp = dwarf.lf.comp;
1513 const gpa = comp.gpa;
1514 const func_gop = try dwarf.funcs.getOrPut(gpa, nav);
14861515 if (!func_gop.found_existing) func_gop.value_ptr.* = .{
1487 .owner_nav = owner_nav,
14881516 .fde_ni = .none,
14891517 .debug_info_ni = .none,
14901518 .debug_line_ni = .none,
......@@ -1494,7 +1522,7 @@ pub fn getFunc(dwarf: *Dwarf, owner_nav: InternPool.Nav.Index) link.Error!Func.I
14941522 const elf = dwarf.lf.cast(.elf2).?;
14951523 try elf.nodes.ensureUnusedCapacity(gpa, 1);
14961524 try elf.dwarf_funcs.ensureUnusedCapacity(gpa, 1);
1497 const unit = dwarf.getUnit(zcu.fileByIndex(src_inst.resolveFile(ip)).mod.?).get(dwarf);
1525 const unit = dwarf.getUnit(comp.zcu.?.navFileScope(nav).mod.?).get(dwarf);
14981526 func_gop.value_ptr.debug_info_ni = .wrap(elf.addNodeAssumeCapacity(
14991527 unit.debug_info_ni.unwrap().?.addFloatingChild(gpa, &elf.mf, .{
15001528 .enable_next_moved = true,
......@@ -1518,10 +1546,8 @@ pub fn getFunc(dwarf: *Dwarf, owner_nav: InternPool.Nav.Index) link.Error!Func.I
15181546 }
15191547 return fi;
15201548}
1521pub fn getFuncIfExists(dwarf: *Dwarf, owner_nav: InternPool.Nav.Index) ?Func.Index {
1522 const ip = &dwarf.lf.comp.zcu.?.intern_pool;
1523 return @fromBackingInt(@intCast(dwarf.funcs.getIndex(ip.getNav(owner_nav).srcInst(ip)) orelse
1524 return null));
1549pub fn getFuncIfExists(dwarf: *Dwarf, nav: InternPool.Nav.Index) ?Func.Index {
1550 return @fromBackingInt(@intCast(dwarf.funcs.getIndex(nav) orelse return null));
15251551}
15261552
15271553pub fn unitLengthSize(dwarf: *Dwarf) usize {
......@@ -1654,11 +1680,11 @@ pub fn updateEhFrameFde(dwarf: *Dwarf, fde: []u8, fde_offset: u64) void {
16541680
16551681pub fn genDebugInfoHeader(
16561682 dwarf: *Dwarf,
1683 zcu: *Zcu,
16571684 mod: *Module,
16581685 unit: *Unit,
16591686 dih_nw: *MappedFile.Node.Writer,
16601687 debug_rnglists_offsets_table_offset: usize,
1661 zcu: *Zcu,
16621688) link.EmitError!void {
16631689 const comp = zcu.comp;
16641690 const dihw = &dih_nw.interface;
......@@ -1695,20 +1721,22 @@ pub fn genDebugInfoHeader(
16951721 zcu.builtin_modules.get(mod.getBuiltinOptions(comp.config).hash()).?,
16961722 zcu.root_mod,
16971723 zcu.std_mod,
1698 }) |name, dep| try dwarf.genModuleDependency(dih_nw, name, dep, module_offset);
1724 }) |name, dep| try dwarf.genModuleDependency(zcu, dih_nw, name, dep, module_offset);
16991725 for (mod.deps.keys(), mod.deps.values()) |name, dep|
1700 try dwarf.genModuleDependency(dih_nw, name, dep, module_offset);
1726 try dwarf.genModuleDependency(zcu, dih_nw, name, dep, module_offset);
17011727 for ([2]AbbrevCode{ .pad_1, .pad_n }) |pad| _ = try dwarf.refAbbrevCode(pad);
17021728 try dwarf.genDebugInfoPadding(dihw, dihw.unusedCapacityLen());
17031729}
17041730
17051731fn genModuleDependency(
17061732 dwarf: *Dwarf,
1733 zcu: *Zcu,
17071734 nw: *MappedFile.Node.Writer,
17081735 name: []const u8,
17091736 dep: *Module,
17101737 module_offset: usize,
17111738) link.EmitError!void {
1739 if (!zcu.alive_files.contains(zcu.module_roots.get(dep).?.unwrap() orelse return)) return;
17121740 const diw = &nw.interface;
17131741 try diw.writeUleb128(try dwarf.refAbbrevCode(.module_dependency));
17141742 try diw.writeAll(name);
......@@ -1889,7 +1917,7 @@ pub fn genDebugRnglistsHeader(
18891917 .@"32" => try drhw.writeInt(u32, 4, dwarf.endian),
18901918 .@"64" => try drhw.writeInt(u64, 8, dwarf.endian),
18911919 }
1892 unit.debug_rnglists_offset = drhw.end;
1920 unit.debug_rnglists_end = drhw.end;
18931921 try drhw.writeByte(DW.RLE.end_of_list);
18941922 return offsets_table_offset;
18951923}
......@@ -1902,24 +1930,22 @@ pub fn genDebugRnglists(
19021930 func_length: u64,
19031931) link.EmitError!void {
19041932 const drw = &dr_nw.interface;
1905 drw.end = unit.debug_rnglists_offset;
1933 drw.end = unit.debug_rnglists_end;
19061934 try drw.writeByte(DW.RLE.start_length);
19071935 try dwarf.symbolAddress(dr_nw, func_si, 0);
19081936 try drw.writeUleb128(func_length);
1909 unit.debug_rnglists_offset = drw.end;
1937 unit.debug_rnglists_end = drw.end;
19101938 try drw.writeByte(DW.RLE.end_of_list);
19111939}
19121940
19131941pub fn updateLineNumber(
19141942 dwarf: *Dwarf,
1915 zcu: *Zcu,
1916 src_inst: InternPool.TrackedInst.Index,
1917 debug_info: []u8,
1943 mf: *MappedFile,
1944 inst: InternPool.TrackedInst.Index,
1945 line: u32,
19181946) void {
1919 const inst_info = src_inst.resolveFull(&zcu.intern_pool).?;
1920 assert(inst_info.inst != .main_struct_inst);
1921 const src_line = zcu.fileByIndex(inst_info.file).zir.?.getDeclaration(inst_info.inst).src_line;
1922 std.mem.writeInt(u32, debug_info[AbbrevCode.decl_bytes..][0..4], src_line + 1, dwarf.endian);
1947 const decl_ni = dwarf.decls.get(inst) orelse return;
1948 std.mem.writeInt(u32, decl_ni.slice(mf)[AbbrevCode.decl_bytes..][0..4], line + 1, dwarf.endian);
19231949}
19241950
19251951fn refAbbrevCodeIfExists(
......@@ -1930,36 +1956,45 @@ fn refAbbrevCodeIfExists(
19301956 return if (dwarf.debug_abbrev.set.contains(abbrev_code)) @backingInt(abbrev_code) else null;
19311957}
19321958
1933fn refAbbrevCode(
1959pub fn refAbbrevCode(
19341960 dwarf: *Dwarf,
19351961 abbrev_code: AbbrevCode,
1936) link.EmitError!@typeInfo(AbbrevCode).@"enum".tag_type {
1962) link.Error!@typeInfo(AbbrevCode).@"enum".tag_type {
19371963 if (dwarf.refAbbrevCodeIfExists(abbrev_code)) |backing_int| {
19381964 @branchHint(.likely);
19391965 return backing_int;
19401966 }
1941 const elf = dwarf.lf.cast(.elf2).?;
1942 const comp = elf.base.comp;
1943 var nw: MappedFile.Node.Writer = undefined;
1944 dwarf.debug_abbrev.ni.unwrap().?.writer(comp.gpa, &elf.mf, &nw);
1945 defer nw.deinit();
1967 var da_nw: MappedFile.Node.Writer = undefined;
1968 dwarf.debug_abbrev.ni.unwrap().?.writer(dwarf.lf.comp.gpa, &dwarf.lf.cast(.elf2).?.mf, &da_nw);
1969 defer da_nw.deinit();
1970 dwarf.genDebugAbbrev(&da_nw, abbrev_code) catch |err| switch (err) {
1971 else => |e| return e,
1972 error.WriteFailed => return dwarf.reportWriteError(&da_nw),
1973 };
1974 dwarf.debug_abbrev.set.insert(abbrev_code);
1975 return dwarf.refAbbrevCodeIfExists(abbrev_code).?;
1976}
1977
1978fn genDebugAbbrev(
1979 dwarf: *Dwarf,
1980 da_nw: *MappedFile.Node.Writer,
1981 abbrev_code: AbbrevCode,
1982) link.EmitError!void {
19461983 const abbrev = AbbrevCode.abbrevs.get(abbrev_code);
1947 const daw = &nw.interface;
1948 daw.end = dwarf.debug_abbrev.offset;
1984 const daw = &da_nw.interface;
1985 daw.end = dwarf.debug_abbrev.end;
19491986 try daw.writeUleb128(@backingInt(abbrev_code));
19501987 try daw.writeUleb128(@backingInt(abbrev.tag));
19511988 try daw.writeByte(if (abbrev.children) DW.CHILDREN.yes else DW.CHILDREN.no);
19521989 for (abbrev.attrs) |*attr| {
19531990 try daw.writeUleb128(@backingInt(switch (attr[0]) {
19541991 else => |at| at,
1955 .ZIG_call_line_relative => |at| if (comp.config.incremental) at else .call_line,
1992 .ZIG_call_line_relative => |at| if (dwarf.lf.comp.config.incremental) at else .call_line,
19561993 }));
19571994 try daw.writeUleb128(@backingInt(attr[1]));
19581995 }
19591996 for (0..2) |_| try daw.writeUleb128(0);
1960 dwarf.debug_abbrev.offset = daw.end;
1961 dwarf.debug_abbrev.set.insert(abbrev_code);
1962 return dwarf.refAbbrevCodeIfExists(abbrev_code).?;
1997 dwarf.debug_abbrev.end = daw.end;
19631998}
19641999
19652000fn sectionOffset(
......@@ -2007,6 +2042,16 @@ fn strp(dwarf: *Dwarf, s: *Str, nw: *MappedFile.Node.Writer, str: []const u8) li
20072042 });
20082043}
20092044
2045fn reportWriteError(dwarf: *Dwarf, nw: *const MappedFile.Node.Writer) link.Error {
2046 switch (nw.err.?) {
2047 else => |e| return e,
2048 error.MappedFileIo => return dwarf.lf.comp.link_diags.fail(
2049 "failed to write output file: {t}",
2050 .{nw.mf.io_err.?},
2051 ),
2052 }
2053}
2054
20102055fn DeclValEnum(comptime T: type) type {
20112056 const decl_names = @typeInfo(T).@"struct".decl_names;
20122057 @setEvalBranchQuota(10 * decl_names.len);
......@@ -2034,7 +2079,8 @@ pub const AbbrevCode = enum {
20342079 // padding codes must be one byte uleb128 values to function
20352080 pad_1,
20362081 pad_n,
2037 // decl, generic decl, and instance codes are assumed to all have the same uleb128 length
2082 // decl, specification, and instance codes are assumed to all have the same uleb128 size
2083 decl_lost,
20382084 decl_alias,
20392085 decl_empty_enum,
20402086 decl_enum,
......@@ -2054,9 +2100,9 @@ pub const AbbrevCode = enum {
20542100 decl_func_generic,
20552101 decl_extern_nullary_func,
20562102 decl_extern_func,
2057 generic_decl_var,
2058 generic_decl_const,
2059 generic_decl_func,
2103 decl_specification_struct,
2104 decl_specification_union,
2105 decl_specification_func,
20602106 decl_instance_alias,
20612107 decl_instance_empty_enum,
20622108 decl_instance_enum,
......@@ -2174,20 +2220,23 @@ pub const AbbrevCode = enum {
21742220 DeclValEnum(DW.AT),
21752221 DeclValEnum(DW.FORM),
21762222 };
2177 const decl_abbrev_common_attrs = &[_]Attr{
2223 const decl_attrs = &[_]Attr{
21782224 //.{ .ZIG_parent, .ref_addr },
21792225 .{ .decl_line, .data4 },
21802226 .{ .decl_column, .udata },
21812227 .{ .accessibility, .data1 },
21822228 .{ .name, .strp },
21832229 };
2184 const generic_decl_abbrev_common_attrs = decl_abbrev_common_attrs ++ &[_]Attr{
2230
2231 const decl_specification_attrs = decl_attrs ++ &[_]Attr{
21852232 .{ .declaration, .flag_present },
21862233 };
2187 const decl_instance_abbrev_common_attrs = &[_]Attr{
2188 .{ .ZIG_parent, .ref_addr },
2189 .{ .abstract_origin, .ref_addr },
2234
2235 const decl_instance_attrs = &[_]Attr{
2236 //.{ .ZIG_parent, .ref_addr },
2237 .{ .specification, .ref_addr },
21902238 };
2239
21912240 const abbrevs = std.EnumArray(AbbrevCode, struct {
21922241 tag: DeclValEnum(DW.TAG),
21932242 children: bool = false,
......@@ -2202,35 +2251,38 @@ pub const AbbrevCode = enum {
22022251 .{ .ZIG_padding, .block },
22032252 },
22042253 },
2254 .decl_lost = .{
2255 .tag = .ZIG_lost_declaration,
2256 },
22052257 .decl_alias = .{
22062258 .tag = .imported_declaration,
2207 .attrs = decl_abbrev_common_attrs ++ .{
2259 .attrs = decl_attrs ++ .{
22082260 .{ .import, .ref_addr },
22092261 },
22102262 },
22112263 .decl_empty_enum = .{
22122264 .tag = .enumeration_type,
2213 .attrs = decl_abbrev_common_attrs ++ .{
2265 .attrs = decl_attrs ++ .{
22142266 .{ .type, .ref_addr },
22152267 },
22162268 },
22172269 .decl_enum = .{
22182270 .tag = .enumeration_type,
22192271 .children = true,
2220 .attrs = decl_abbrev_common_attrs ++ .{
2272 .attrs = decl_attrs ++ .{
22212273 .{ .type, .ref_addr },
22222274 },
22232275 },
22242276 .decl_namespace_struct = .{
22252277 .tag = .structure_type,
2226 .attrs = decl_abbrev_common_attrs ++ .{
2278 .attrs = decl_attrs ++ .{
22272279 .{ .declaration, .flag },
22282280 },
22292281 },
22302282 .decl_struct = .{
22312283 .tag = .structure_type,
22322284 .children = true,
2233 .attrs = decl_abbrev_common_attrs ++ .{
2285 .attrs = decl_attrs ++ .{
22342286 .{ .byte_size, .udata },
22352287 .{ .alignment, .udata },
22362288 },
......@@ -2238,14 +2290,14 @@ pub const AbbrevCode = enum {
22382290 .decl_packed_struct = .{
22392291 .tag = .structure_type,
22402292 .children = true,
2241 .attrs = decl_abbrev_common_attrs ++ .{
2293 .attrs = decl_attrs ++ .{
22422294 .{ .type, .ref_addr },
22432295 },
22442296 },
22452297 .decl_union = .{
22462298 .tag = .union_type,
22472299 .children = true,
2248 .attrs = decl_abbrev_common_attrs ++ .{
2300 .attrs = decl_attrs ++ .{
22492301 .{ .byte_size, .udata },
22502302 .{ .alignment, .udata },
22512303 },
......@@ -2253,13 +2305,13 @@ pub const AbbrevCode = enum {
22532305 .decl_packed_union = .{
22542306 .tag = .union_type,
22552307 .children = true,
2256 .attrs = decl_abbrev_common_attrs ++ .{
2308 .attrs = decl_attrs ++ .{
22572309 .{ .type, .ref_addr },
22582310 },
22592311 },
22602312 .decl_var = .{
22612313 .tag = .variable,
2262 .attrs = decl_abbrev_common_attrs ++ .{
2314 .attrs = decl_attrs ++ .{
22632315 .{ .linkage_name, .strp },
22642316 .{ .type, .ref_addr },
22652317 .{ .location, .exprloc },
......@@ -2269,7 +2321,7 @@ pub const AbbrevCode = enum {
22692321 },
22702322 .decl_const = .{
22712323 .tag = .constant,
2272 .attrs = decl_abbrev_common_attrs ++ .{
2324 .attrs = decl_attrs ++ .{
22732325 .{ .linkage_name, .strp },
22742326 .{ .type, .ref_addr },
22752327 .{ .alignment, .udata },
......@@ -2278,7 +2330,7 @@ pub const AbbrevCode = enum {
22782330 },
22792331 .decl_const_runtime_bits = .{
22802332 .tag = .constant,
2281 .attrs = decl_abbrev_common_attrs ++ .{
2333 .attrs = decl_attrs ++ .{
22822334 .{ .linkage_name, .strp },
22832335 .{ .type, .ref_addr },
22842336 .{ .alignment, .udata },
......@@ -2288,7 +2340,7 @@ pub const AbbrevCode = enum {
22882340 },
22892341 .decl_const_comptime_state = .{
22902342 .tag = .constant,
2291 .attrs = decl_abbrev_common_attrs ++ .{
2343 .attrs = decl_attrs ++ .{
22922344 .{ .linkage_name, .strp },
22932345 .{ .type, .ref_addr },
22942346 .{ .alignment, .udata },
......@@ -2298,7 +2350,7 @@ pub const AbbrevCode = enum {
22982350 },
22992351 .decl_const_runtime_bits_comptime_state = .{
23002352 .tag = .constant,
2301 .attrs = decl_abbrev_common_attrs ++ .{
2353 .attrs = decl_attrs ++ .{
23022354 .{ .linkage_name, .strp },
23032355 .{ .type, .ref_addr },
23042356 .{ .alignment, .udata },
......@@ -2309,7 +2361,7 @@ pub const AbbrevCode = enum {
23092361 },
23102362 .decl_nullary_func = .{
23112363 .tag = .subprogram,
2312 .attrs = decl_abbrev_common_attrs ++ .{
2364 .attrs = decl_attrs ++ .{
23132365 .{ .linkage_name, .strp },
23142366 .{ .type, .ref_addr },
23152367 .{ .low_pc, .addr },
......@@ -2322,7 +2374,7 @@ pub const AbbrevCode = enum {
23222374 .decl_func = .{
23232375 .tag = .subprogram,
23242376 .children = true,
2325 .attrs = decl_abbrev_common_attrs ++ .{
2377 .attrs = decl_attrs ++ .{
23262378 .{ .linkage_name, .strp },
23272379 //.{ .type, .ref_addr },
23282380 .{ .low_pc, .addr },
......@@ -2334,20 +2386,20 @@ pub const AbbrevCode = enum {
23342386 },
23352387 .decl_nullary_func_generic = .{
23362388 .tag = .subprogram,
2337 .attrs = decl_abbrev_common_attrs ++ .{
2389 .attrs = decl_attrs ++ .{
23382390 .{ .type, .ref_addr },
23392391 },
23402392 },
23412393 .decl_func_generic = .{
23422394 .tag = .subprogram,
23432395 .children = true,
2344 .attrs = decl_abbrev_common_attrs ++ .{
2396 .attrs = decl_attrs ++ .{
23452397 .{ .type, .ref_addr },
23462398 },
23472399 },
23482400 .decl_extern_nullary_func = .{
23492401 .tag = .subprogram,
2350 .attrs = decl_abbrev_common_attrs ++ .{
2402 .attrs = decl_attrs ++ .{
23512403 .{ .linkage_name, .strp },
23522404 .{ .type, .ref_addr },
23532405 .{ .low_pc, .addr },
......@@ -2358,7 +2410,7 @@ pub const AbbrevCode = enum {
23582410 .decl_extern_func = .{
23592411 .tag = .subprogram,
23602412 .children = true,
2361 .attrs = decl_abbrev_common_attrs ++ .{
2413 .attrs = decl_attrs ++ .{
23622414 .{ .linkage_name, .strp },
23632415 .{ .type, .ref_addr },
23642416 .{ .low_pc, .addr },
......@@ -2366,47 +2418,47 @@ pub const AbbrevCode = enum {
23662418 .{ .noreturn, .flag },
23672419 },
23682420 },
2369 .generic_decl_var = .{
2421 .decl_specification_struct = .{
23702422 .tag = .variable,
2371 .attrs = generic_decl_abbrev_common_attrs,
2423 .attrs = decl_specification_attrs,
23722424 },
2373 .generic_decl_const = .{
2425 .decl_specification_union = .{
23742426 .tag = .constant,
2375 .attrs = generic_decl_abbrev_common_attrs,
2427 .attrs = decl_specification_attrs,
23762428 },
2377 .generic_decl_func = .{
2429 .decl_specification_func = .{
23782430 .tag = .subprogram,
2379 .attrs = generic_decl_abbrev_common_attrs,
2431 .attrs = decl_specification_attrs,
23802432 },
23812433 .decl_instance_alias = .{
23822434 .tag = .imported_declaration,
2383 .attrs = decl_instance_abbrev_common_attrs ++ .{
2435 .attrs = decl_instance_attrs ++ .{
23842436 .{ .import, .ref_addr },
23852437 },
23862438 },
23872439 .decl_instance_empty_enum = .{
23882440 .tag = .enumeration_type,
2389 .attrs = decl_instance_abbrev_common_attrs ++ .{
2441 .attrs = decl_instance_attrs ++ .{
23902442 .{ .type, .ref_addr },
23912443 },
23922444 },
23932445 .decl_instance_enum = .{
23942446 .tag = .enumeration_type,
23952447 .children = true,
2396 .attrs = decl_instance_abbrev_common_attrs ++ .{
2448 .attrs = decl_instance_attrs ++ .{
23972449 .{ .type, .ref_addr },
23982450 },
23992451 },
24002452 .decl_instance_namespace_struct = .{
24012453 .tag = .structure_type,
2402 .attrs = decl_instance_abbrev_common_attrs ++ .{
2454 .attrs = decl_instance_attrs ++ .{
24032455 .{ .declaration, .flag },
24042456 },
24052457 },
24062458 .decl_instance_struct = .{
24072459 .tag = .structure_type,
24082460 .children = true,
2409 .attrs = decl_instance_abbrev_common_attrs ++ .{
2461 .attrs = decl_instance_attrs ++ .{
24102462 .{ .byte_size, .udata },
24112463 .{ .alignment, .udata },
24122464 },
......@@ -2414,14 +2466,14 @@ pub const AbbrevCode = enum {
24142466 .decl_instance_packed_struct = .{
24152467 .tag = .structure_type,
24162468 .children = true,
2417 .attrs = decl_instance_abbrev_common_attrs ++ .{
2469 .attrs = decl_instance_attrs ++ .{
24182470 .{ .type, .ref_addr },
24192471 },
24202472 },
24212473 .decl_instance_union = .{
24222474 .tag = .union_type,
24232475 .children = true,
2424 .attrs = decl_instance_abbrev_common_attrs ++ .{
2476 .attrs = decl_instance_attrs ++ .{
24252477 .{ .byte_size, .udata },
24262478 .{ .alignment, .udata },
24272479 },
......@@ -2429,13 +2481,13 @@ pub const AbbrevCode = enum {
24292481 .decl_instance_packed_union = .{
24302482 .tag = .union_type,
24312483 .children = true,
2432 .attrs = decl_instance_abbrev_common_attrs ++ .{
2484 .attrs = decl_instance_attrs ++ .{
24332485 .{ .type, .ref_addr },
24342486 },
24352487 },
24362488 .decl_instance_var = .{
24372489 .tag = .variable,
2438 .attrs = decl_instance_abbrev_common_attrs ++ .{
2490 .attrs = decl_instance_attrs ++ .{
24392491 .{ .linkage_name, .strp },
24402492 .{ .type, .ref_addr },
24412493 .{ .location, .exprloc },
......@@ -2445,7 +2497,7 @@ pub const AbbrevCode = enum {
24452497 },
24462498 .decl_instance_const = .{
24472499 .tag = .constant,
2448 .attrs = decl_instance_abbrev_common_attrs ++ .{
2500 .attrs = decl_instance_attrs ++ .{
24492501 .{ .linkage_name, .strp },
24502502 .{ .type, .ref_addr },
24512503 .{ .alignment, .udata },
......@@ -2454,7 +2506,7 @@ pub const AbbrevCode = enum {
24542506 },
24552507 .decl_instance_const_runtime_bits = .{
24562508 .tag = .constant,
2457 .attrs = decl_instance_abbrev_common_attrs ++ .{
2509 .attrs = decl_instance_attrs ++ .{
24582510 .{ .linkage_name, .strp },
24592511 .{ .type, .ref_addr },
24602512 .{ .alignment, .udata },
......@@ -2464,7 +2516,7 @@ pub const AbbrevCode = enum {
24642516 },
24652517 .decl_instance_const_comptime_state = .{
24662518 .tag = .constant,
2467 .attrs = decl_instance_abbrev_common_attrs ++ .{
2519 .attrs = decl_instance_attrs ++ .{
24682520 .{ .linkage_name, .strp },
24692521 .{ .type, .ref_addr },
24702522 .{ .alignment, .udata },
......@@ -2474,7 +2526,7 @@ pub const AbbrevCode = enum {
24742526 },
24752527 .decl_instance_const_runtime_bits_comptime_state = .{
24762528 .tag = .constant,
2477 .attrs = decl_instance_abbrev_common_attrs ++ .{
2529 .attrs = decl_instance_attrs ++ .{
24782530 .{ .linkage_name, .strp },
24792531 .{ .type, .ref_addr },
24802532 .{ .alignment, .udata },
......@@ -2485,7 +2537,7 @@ pub const AbbrevCode = enum {
24852537 },
24862538 .decl_instance_nullary_func = .{
24872539 .tag = .subprogram,
2488 .attrs = decl_instance_abbrev_common_attrs ++ .{
2540 .attrs = decl_instance_attrs ++ .{
24892541 .{ .linkage_name, .strp },
24902542 .{ .type, .ref_addr },
24912543 .{ .low_pc, .addr },
......@@ -2498,7 +2550,7 @@ pub const AbbrevCode = enum {
24982550 .decl_instance_func = .{
24992551 .tag = .subprogram,
25002552 .children = true,
2501 .attrs = decl_instance_abbrev_common_attrs ++ .{
2553 .attrs = decl_instance_attrs ++ .{
25022554 .{ .linkage_name, .strp },
25032555 .{ .type, .ref_addr },
25042556 .{ .low_pc, .addr },
......@@ -2510,20 +2562,20 @@ pub const AbbrevCode = enum {
25102562 },
25112563 .decl_instance_nullary_func_generic = .{
25122564 .tag = .subprogram,
2513 .attrs = decl_instance_abbrev_common_attrs ++ .{
2565 .attrs = decl_instance_attrs ++ .{
25142566 .{ .type, .ref_addr },
25152567 },
25162568 },
25172569 .decl_instance_func_generic = .{
25182570 .tag = .subprogram,
25192571 .children = true,
2520 .attrs = decl_instance_abbrev_common_attrs ++ .{
2572 .attrs = decl_instance_attrs ++ .{
25212573 .{ .type, .ref_addr },
25222574 },
25232575 },
25242576 .decl_instance_extern_nullary_func = .{
25252577 .tag = .subprogram,
2526 .attrs = decl_instance_abbrev_common_attrs ++ .{
2578 .attrs = decl_instance_attrs ++ .{
25272579 .{ .linkage_name, .strp },
25282580 .{ .type, .ref_addr },
25292581 .{ .low_pc, .addr },
......@@ -2534,7 +2586,7 @@ pub const AbbrevCode = enum {
25342586 .decl_instance_extern_func = .{
25352587 .tag = .subprogram,
25362588 .children = true,
2537 .attrs = decl_instance_abbrev_common_attrs ++ .{
2589 .attrs = decl_instance_attrs ++ .{
25382590 .{ .linkage_name, .strp },
25392591 .{ .type, .ref_addr },
25402592 .{ .low_pc, .addr },
src/link/Elf.zig+2-2
......@@ -1688,8 +1688,8 @@ pub fn updateExports(
16881688 return self.zigObjectPtr().?.updateExports(self, pt, export_indices);
16891689}
16901690
1691pub fn updateLineNumber(self: *Elf, pt: Zcu.PerThread, ti_id: InternPool.TrackedInst.Index) link.Error!void {
1692 return self.zigObjectPtr().?.updateLineNumber(pt, ti_id);
1691pub fn updateLineNumber(self: *Elf, pt: Zcu.PerThread, inst: InternPool.TrackedInst.Index, line: u32) link.Error!void {
1692 return self.zigObjectPtr().?.updateLineNumber(pt, inst, line);
16931693}
16941694
16951695fn checkDuplicates(self: *Elf) !void {
src/link/Elf/ZigObject.zig+2-2
......@@ -1946,11 +1946,11 @@ pub fn updateExports(
19461946 }
19471947}
19481948
1949pub fn updateLineNumber(self: *ZigObject, pt: Zcu.PerThread, ti_id: InternPool.TrackedInst.Index) link.Error!void {
1949pub fn updateLineNumber(self: *ZigObject, pt: Zcu.PerThread, ti_id: InternPool.TrackedInst.Index, line: u32) link.Error!void {
19501950 if (self.dwarf) |*dwarf| {
19511951 const comp = dwarf.bin_file.comp;
19521952 const diags = &comp.link_diags;
1953 dwarf.updateLineNumber(pt.zcu, ti_id) catch |err| switch (err) {
1953 dwarf.updateLineNumber(pt.zcu, ti_id, line) catch |err| switch (err) {
19541954 error.OutOfMemory, error.Canceled, error.AlreadyReported => |e| return e,
19551955 else => |e| return diags.fail("failed to update dwarf line numbers: {s}", .{@errorName(e)}),
19561956 };
src/link/Elf2.zig+189-110
......@@ -501,7 +501,7 @@ const Section = struct {
501501 ni: MappedFile.Node.Index,
502502 /// A symbol which is exactly at the start of this section.
503503 ///
504 /// If the section does not have flag `std.elf.SHF.ALLOC`, this is `.null`.
504 /// When not emitting a relocatable, or for special section types, this is `.null`.
505505 lsi: Symbol.LocalIndex,
506506 rela: union {
507507 /// This field is active if and only if this section is *not* a `SHT_RELA` section.
......@@ -848,10 +848,10 @@ const Section = struct {
848848 }
849849 }
850850
851 /// Asserts that `rela_shndx` is a `SHT_RELA` section, and asserts that `index` refers to an
852 /// `R_*_RELATIVE` relocation inside of it; then, updates that relocation's addend (which is
853 /// an address in this DSO without the runtime load offset applied) to the given value.
854 fn relaSetRelativeOffset(rela_shndx: Index, elf: *Elf, index: RelaIndex, new_addend: u64) void {
851 /// Asserts that `rela_shndx` is a `SHT_RELA` section and updates the `addend` field of the
852 /// `ElfN.Rela` entry at the given index. Asserts that `index` is not in the free-list (i.e.
853 /// it is not deleted).
854 fn relaSetAddend(rela_shndx: Index, elf: *Elf, index: RelaIndex, new_addend: u64) void {
855855 switch (elf.shdrPtr(rela_shndx)) {
856856 inline else => |shdr, class| {
857857 assert(elf.targetLoad(&shdr.type) == .RELA);
......@@ -1603,6 +1603,20 @@ const SymbolReloc = struct {
16031603 }
16041604 };
16051605
1606 fn flushMovedNode(reloc: *SymbolReloc, elf: *Elf, node_vaddr: u64) void {
1607 if (reloc.rela_index.unwrap()) |rela_index| {
1608 // The node has moved, so the offset of the relocation within the section might have
1609 // changed, so update the `offset` field of the `ElfN.Rela` entry.
1610 reloc.relaSection(elf).relaSetOffset(elf, rela_index, node_vaddr + reloc.offset);
1611 }
1612 // This is not just the inverse of the above condition, because if `reloc` is relative
1613 // to the base of this DSO, then `rela_index` is an `R_*_RELATIVE` relocation, but we
1614 // still need to call `SymbolReloc.apply` to update that relocation's addend.
1615 if (elf.ehdrType() != .REL) {
1616 reloc.apply(elf);
1617 }
1618 }
1619
16061620 fn apply(reloc: *SymbolReloc, elf: *Elf) void {
16071621 assert(elf.ehdrType() != .REL);
16081622 if (reloc.node.hasMoved(&elf.mf) or reloc.target.hasMoved(elf)) {
......@@ -1688,7 +1702,7 @@ const SymbolReloc = struct {
16881702 }
16891703 assert(reloc.type.action.simple.cast == .unsigned);
16901704 assert(reloc.type.action.simple.shift == .@"0");
1691 elf.shndx.rela_dyn.relaSetRelativeOffset(elf, rela_index, target_val);
1705 elf.shndx.rela_dyn.relaSetAddend(elf, rela_index, target_val);
16921706 return;
16931707 },
16941708 };
......@@ -1767,30 +1781,58 @@ const NodeReloc = struct {
17671781 }
17681782 };
17691783
1770 fn apply(reloc: *NodeReloc, elf: *Elf) void {
1771 assert(elf.ehdrType() != .REL);
1772 if (reloc.node.hasMoved(&elf.mf) or reloc.target.hasMoved(&elf.mf)) {
1773 // There's no point applying the relocation now, because it will be re-applied by
1774 // `flushMoved` at some point anyway.
1775 return;
1784 fn flushMovedNode(reloc: *NodeReloc, elf: *Elf, node_vaddr: u64) void {
1785 if (reloc.rela_index.unwrap()) |rela_index| {
1786 assert(elf.ehdrType() == .REL);
1787 // The node has moved, so the offset of the relocation within the section might have
1788 // changed, so update the `offset` field of the `ElfN.Rela` entry.
1789 elf.getNodeShndx(reloc.node).get(elf).rela.shndx.relaSetOffset(elf, rela_index, node_vaddr + reloc.offset);
1790 } else {
1791 assert(elf.ehdrType() != .REL);
1792 reloc.apply(elf);
17761793 }
1777 switch (reloc.result) {
1778 .ok => {},
1779 .overflowed => elf.overflowed_reloc_count -= 1,
1780 .misaligned => elf.misaligned_reloc_count -= 1,
1794 }
1795
1796 fn flushMovedTarget(reloc: *NodeReloc, elf: *Elf, target_section_offset: u64) void {
1797 if (reloc.rela_index.unwrap()) |rela_index| {
1798 assert(elf.ehdrType() == .REL);
1799 // The target has moved, so the `addend` field of the `ElfN.Rela` entry needs to be updated.
1800 elf.getNodeShndx(reloc.node).get(elf).rela.shndx.relaSetAddend(elf, rela_index, target_section_offset +% @as(u64, @bitCast(reloc.addend)));
1801 } else {
1802 assert(elf.ehdrType() != .REL);
1803 reloc.apply(elf);
17811804 }
1782 if (reloc.applyInner(elf)) {
1783 @branchHint(.likely);
1784 reloc.result = .ok;
1785 } else |err| switch (err) {
1786 error.RelocationOverflow => {
1787 reloc.result = .overflowed;
1788 elf.overflowed_reloc_count += 1;
1789 },
1790 error.RelocationMisaligned => {
1791 reloc.result = .misaligned;
1792 elf.misaligned_reloc_count += 1;
1793 },
1805 }
1806
1807 fn apply(reloc: *NodeReloc, elf: *Elf) void {
1808 if (reloc.rela_index.unwrap()) |rela_index| {
1809 assert(elf.ehdrType() == .REL);
1810 _ = rela_index;
1811 } else {
1812 assert(elf.ehdrType() != .REL);
1813 if (reloc.node.hasMoved(&elf.mf) or reloc.target.hasMoved(&elf.mf)) {
1814 // There's no point applying the relocation now, because it will be re-applied by
1815 // `flushMoved` at some point anyway.
1816 return;
1817 }
1818 switch (reloc.result) {
1819 .ok => {},
1820 .overflowed => elf.overflowed_reloc_count -= 1,
1821 .misaligned => elf.misaligned_reloc_count -= 1,
1822 }
1823 if (reloc.applyInner(elf)) {
1824 @branchHint(.likely);
1825 reloc.result = .ok;
1826 } else |err| switch (err) {
1827 error.RelocationOverflow => {
1828 reloc.result = .overflowed;
1829 elf.overflowed_reloc_count += 1;
1830 },
1831 error.RelocationMisaligned => {
1832 reloc.result = .misaligned;
1833 elf.misaligned_reloc_count += 1;
1834 },
1835 }
17941836 }
17951837 }
17961838 fn applyInner(reloc: *const NodeReloc, elf: *Elf) error{ RelocationOverflow, RelocationMisaligned }!void {
......@@ -5220,7 +5262,42 @@ fn computeNodeVAddr(elf: *Elf, ni: MappedFile.Node.Index) u64 {
52205262 const offset, _ = ni.location(&elf.mf).resolve(&elf.mf);
52215263 return parent_vaddr + offset;
52225264}
5223fn getNodeElfOffset(elf: *Elf, ni: MappedFile.Node.Index) u64 {
5265fn computeNodeSectionOffset(elf: *Elf, ni: MappedFile.Node.Index) u64 {
5266 const parent_ni = ni.parent(&elf.mf).unwrap().?;
5267 const parent_section_offset = parent_section_offset: switch (elf.getNode(parent_ni)) {
5268 .deleted,
5269 .archive,
5270 .archive_header,
5271 .archive_input_member,
5272 .archive_elf_member_header,
5273 .elf,
5274 .ehdr,
5275 .shdr,
5276 .segment,
5277 => unreachable,
5278 .section, .section_manual_size => 0,
5279 .input_section, .copied_global => unreachable,
5280 .nav, .uav, .lazy_code, .lazy_const_data => unreachable,
5281 .debug_shared, .unit_padding => unreachable,
5282 .unit_frame, .unit_debug_info, .unit_debug_line => {
5283 const parent_section_offset, _ = parent_ni.location(&elf.mf).resolve(&elf.mf);
5284 break :parent_section_offset parent_section_offset;
5285 },
5286 .unit_frame_cie,
5287 .unit_debug_info_header,
5288 .unit_debug_line_header,
5289 .unit_debug_rnglists,
5290 .value_debug_info,
5291 .global_debug_info,
5292 .func_frame_fde,
5293 .func_debug_info,
5294 .func_debug_line,
5295 => unreachable,
5296 };
5297 const offset, _ = ni.location(&elf.mf).resolve(&elf.mf);
5298 return parent_section_offset + offset;
5299}
5300fn computeNodeElfOffset(elf: *Elf, ni: MappedFile.Node.Index) u64 {
52245301 return ni.fileLocation(&elf.mf, false).offset - elf.ni.elf.fileLocation(&elf.mf, false).offset;
52255302}
52265303
......@@ -5293,8 +5370,8 @@ fn resetNodeRelocs(elf: *Elf, ni: MappedFile.Node.Index) void {
52935370 },
52945371 .func_debug_info => |fi| .{
52955372 .first_symbol_reloc = &elf.dwarf_funcs.items[@backingInt(fi)].debug_info_first_symbol_reloc,
5296 .skip_symbol_relocs = if (elf.navs.getPtr(fi.get(&elf.dwarf).owner_nav)) |owner_nav|
5297 owner_nav.lsi.index().ptr(elf).node
5373 .skip_symbol_relocs = if (elf.navs.getPtr(fi.nav(&elf.dwarf))) |nav|
5374 nav.lsi.index().ptr(elf).node
52985375 else
52995376 .none,
53005377 .first_node_reloc = &elf.dwarf_funcs.items[@backingInt(fi)].debug_info_first_node_reloc,
......@@ -5358,17 +5435,7 @@ fn flushMovedNodeRelocs(
53585435 for (elf.symbol_relocs.items[@backingInt(opts.first_symbol_reloc)..]) |*reloc| {
53595436 if (reloc.node.toOptional() == opts.skip_symbol_relocs) continue;
53605437 if (reloc.node != node) break;
5361 if (reloc.rela_index.unwrap()) |rela_index| {
5362 // The node has moved, so the offset of the relocation within the section might have
5363 // changed, so update the `offset` field of the `ElfN.Rela` entry.
5364 reloc.relaSection(elf).relaSetOffset(elf, rela_index, node_vaddr + reloc.offset);
5365 }
5366 // This is not just the inverse of the above condition, because if `reloc` is relative
5367 // to the base of this DSO, then `rela_index` is an `R_*_RELATIVE` relocation, but we
5368 // still need to call `SymbolReloc.apply` to update that relocation's addend.
5369 if (elf.ehdrType() != .REL) {
5370 reloc.apply(elf);
5371 }
5438 reloc.flushMovedNode(elf, node_vaddr);
53725439 }
53735440 }
53745441
......@@ -5376,15 +5443,7 @@ fn flushMovedNodeRelocs(
53765443 for (elf.node_relocs.items[@backingInt(opts.first_node_reloc)..]) |*reloc| {
53775444 if (reloc.node.toOptional() == opts.skip_node_relocs) continue;
53785445 if (reloc.node != node) break;
5379 if (reloc.rela_index.unwrap()) |rela_index| {
5380 assert(elf.ehdrType() == .REL);
5381 // The node has moved, so the offset of the relocation within the section might have
5382 // changed, so update the `offset` field of the `ElfN.Rela` entry.
5383 elf.getNodeShndx(reloc.node).get(elf).rela.shndx.relaSetOffset(elf, rela_index, node_vaddr + reloc.offset);
5384 } else {
5385 assert(elf.ehdrType() != .REL);
5386 reloc.apply(elf);
5387 }
5446 reloc.flushMovedNode(elf, node_vaddr);
53885447 }
53895448 }
53905449
......@@ -7113,7 +7172,7 @@ pub fn zcuFilesReady(elf: *Elf, zcu: *Zcu) link.Error!void {
71137172fn zcuFilesReadyInner(elf: *Elf, zcu: *Zcu) Error!void {
71147173 const gpa = zcu.gpa;
71157174
7116 try elf.dwarf.initUnits(zcu);
7175 try elf.dwarf.updateUnits(zcu);
71177176 const old_units_len = elf.dwarf_units.items.len;
71187177 const new_units_len = elf.dwarf.units.count();
71197178 try elf.dwarf_units.appendNTimes(gpa, .{
......@@ -7204,11 +7263,11 @@ fn zcuFilesReadyInner(elf: *Elf, zcu: *Zcu) Error!void {
72047263 defer dih_nw.deinit();
72057264 elf.resetNodeRelocs(debug_info_header_ni);
72067265 elf.dwarf.genDebugInfoHeader(
7266 zcu,
72077267 mod,
72087268 unit,
72097269 &dih_nw,
72107270 debug_rnglists_offsets_table_offset,
7211 zcu,
72127271 ) catch |err| switch (err) {
72137272 else => |e| return e,
72147273 error.WriteFailed => return dih_nw.err.?,
......@@ -7397,7 +7456,11 @@ fn addSection(elf: *Elf, segment_ni: MappedFile.Node.Index, opts: struct {
73977456 const gpa = elf.base.comp.gpa;
73987457 try elf.nodes.ensureUnusedCapacity(gpa, 1);
73997458 try elf.shdrs.ensureUnusedCapacity(gpa, 1);
7400 if (opts.flags.ALLOC) try elf.ensureUnusedSymbolCapacity(1, .all_local);
7459 const want_symbol = opts.flags.ALLOC or switch (opts.type) {
7460 .NULL, .PROGBITS, .NOBITS, .X86_64_UNWIND => elf.ehdrType() == .REL,
7461 else => false,
7462 };
7463 if (want_symbol) try elf.ensureUnusedSymbolCapacity(1, .all_local);
74017464
74027465 const shstrtab_entry = try elf.string(.shstrtab, opts.name);
74037466 const shndx: Section.Index, const new_shdr_size = shndx: switch (elf.ehdrPtr()) {
......@@ -7443,19 +7506,22 @@ fn addSection(elf: *Elf, segment_ni: MappedFile.Node.Index, opts: struct {
74437506 true => .{ .section_manual_size = shndx },
74447507 });
74457508 const addr = elf.computeNodeVAddr(ni);
7446 const lsi: Symbol.LocalIndex = if (opts.flags.ALLOC) elf.addLocalSymbolAssumeCapacity(.{
7447 .node = .wrap(ni),
7448 .name = .empty,
7449 .value = addr,
7450 .size = 0,
7451 .type = .SECTION,
7452 .shndx = shndx,
7453 }) else .null;
7454 elf.shdrs.appendAssumeCapacity(.{ .lsi = lsi, .ni = ni, .rela = switch (opts.type) {
7455 .REL => unreachable,
7456 .RELA => .{ .free_head = .none },
7457 else => .{ .shndx = .UNDEF },
7458 } });
7509 elf.shdrs.appendAssumeCapacity(.{
7510 .lsi = if (want_symbol) elf.addLocalSymbolAssumeCapacity(.{
7511 .node = ni.toOptional(),
7512 .name = .empty,
7513 .value = addr,
7514 .size = 0,
7515 .type = .SECTION,
7516 .shndx = shndx,
7517 }) else .null,
7518 .ni = ni,
7519 .rela = switch (opts.type) {
7520 .REL => unreachable,
7521 .RELA => .{ .free_head = .none },
7522 else => .{ .shndx = .UNDEF },
7523 },
7524 });
74597525 switch (elf.shdrPtr(shndx)) {
74607526 inline else => |shdr, class| {
74617527 shdr.* = .{
......@@ -7463,7 +7529,7 @@ fn addSection(elf: *Elf, segment_ni: MappedFile.Node.Index, opts: struct {
74637529 .type = opts.type,
74647530 .flags = .{ .shf = opts.flags },
74657531 .addr = @intCast(addr),
7466 .offset = @intCast(elf.getNodeElfOffset(ni)),
7532 .offset = @intCast(elf.computeNodeElfOffset(ni)),
74677533 .size = @intCast(opts.size),
74687534 .link = opts.link,
74697535 .info = opts.info,
......@@ -8068,7 +8134,7 @@ fn addNodeRelocAssumeCapacity(
80688134 .abs32 => .UA32,
80698135 .abs64 => .UA64,
80708136 } },
8071 .X86_64 => .{ .SPARC = switch (@"type") {
8137 .X86_64 => .{ .X86_64 = switch (@"type") {
80728138 .abs32 => .@"32",
80738139 .abs64 => .@"64",
80748140 } },
......@@ -8078,8 +8144,11 @@ fn addNodeRelocAssumeCapacity(
80788144 // the section offset now, but there's no point, because `flushMovedNodeRelocs` will
80798145 // eventually do it for us anyway, so just init to 0.
80808146 .offset = 0,
8081 .raw_sym_index = @backingInt(shndx.get(elf).lsi.index()),
8082 .addend = addend,
8147 .raw_sym_index = @backingInt(switch (shndx.get(elf).lsi) {
8148 .null => unreachable,
8149 else => |lsi| lsi.index(),
8150 }),
8151 .addend = 0,
80838152 });
80848153 elf.node_relocs.appendAssumeCapacity(.{
80858154 .node = node,
......@@ -8576,7 +8645,8 @@ fn updateFuncInner(
85768645 const debug_output: link.File.DebugInfoOutput, const dwarf_func = debug_output: {
85778646 if (elf.ehdrMachine() != .X86_64) break :debug_output .{ .none, undefined };
85788647 const dwarf = &elf.dwarf;
8579 const mod = zcu.fileByIndex(nav.srcInst(ip).resolveFile(ip)).mod.?;
8648 const src_inst = nav.srcInst(ip);
8649 const mod = zcu.fileByIndex(src_inst.resolveFile(ip)).mod.?;
85808650 if (mod.strip and mod.unwind_tables == .none) break :debug_output .{ .none, undefined };
85818651
85828652 try elf.nodes.ensureUnusedCapacity(gpa, 4);
......@@ -8662,6 +8732,7 @@ fn updateFuncInner(
86628732 debug.blocks = .empty;
86638733
86648734 const debug_info_ni = dwarf_func.debug_info_ni.unwrap().?;
8735 try dwarf.decls.put(zcu.comp.gpa, src_inst, debug_info_ni);
86658736 try debug_info_ni.moved(gpa, &elf.mf);
86668737 try debug_info_ni.nextMoved(gpa, &elf.mf);
86678738 debug_info_ni.writer(gpa, &elf.mf, &debug.info_writer);
......@@ -8787,23 +8858,31 @@ fn updateFuncInner(
87878858 try elf.genPending(pt);
87888859}
87898860
8790pub fn updateLineNumber(elf: *Elf, pt: Zcu.PerThread, src_inst: InternPool.TrackedInst.Index) void {
8791 const func = elf.dwarf.funcs.getPtr(src_inst) orelse return;
8792 elf.dwarf.updateLineNumber(pt.zcu, src_inst, func.debug_info_ni.unwrap().?.slice(&elf.mf));
8861pub fn updateLineNumber(
8862 elf: *Elf,
8863 _: Zcu.PerThread,
8864 inst: InternPool.TrackedInst.Index,
8865 line: u32,
8866) void {
8867 elf.dwarf.updateLineNumber(&elf.mf, inst, line);
87938868}
87948869
87958870pub fn lostTracking(
87968871 elf: *Elf,
87978872 _: Zcu.PerThread,
8798 src_inst: InternPool.TrackedInst.Index,
8799) std.mem.Allocator.Error!void {
8800 const func = elf.dwarf.funcs.getPtr(src_inst) orelse return;
8801 elf.resetNodeRelocs(func.fde_ni.unwrap().?);
8802 elf.resetNodeRelocs(func.debug_info_ni.unwrap().?);
8803 elf.resetNodeRelocs(func.debug_line_ni.unwrap().?);
8804 try elf.deleteNode(&func.fde_ni);
8805 try elf.deleteNode(&func.debug_info_ni);
8806 try elf.deleteNode(&func.debug_line_ni);
8873 inst: InternPool.TrackedInst.Index,
8874) link.Error!void {
8875 const decl_ni = elf.dwarf.decls.get(inst) orelse return;
8876 const comp = elf.base.comp;
8877 var diw: std.Io.Writer = .fixed(decl_ni.slice(&elf.mf));
8878 elf.resetNodeRelocs(decl_ni);
8879 diw.writeUleb128(try elf.dwarf.refAbbrevCode(.decl_lost)) catch unreachable;
8880 decl_ni.resizeLeaf(comp.gpa, &elf.mf, diw.end) catch |err| switch (err) {
8881 error.MappedFileIo => return comp.link_diags.fail("failed to write output file: {t}", .{
8882 elf.mf.io_err.?,
8883 }),
8884 else => |e| return e,
8885 };
88078886}
88088887
88098888pub fn updateErrorData(elf: *Elf, pt: Zcu.PerThread) link.Error!void {
......@@ -9127,7 +9206,7 @@ fn idleProgNode(
91279206 .func_debug_info => "debug",
91289207 .func_debug_line => "line",
91299208 },
9130 ip.getNav(fi.get(&elf.dwarf).owner_nav).fqn.fmt(ip),
9209 ip.getNav(fi.nav(&elf.dwarf)).fqn.fmt(ip),
91319210 }) catch &name;
91329211 },
91339212 }, 0);
......@@ -9304,7 +9383,7 @@ fn flushInputSection(elf: *Elf, isi: InputSection.Index) Error!void {
93049383}
93059384
93069385fn flushElfOffset(elf: *Elf, ni: MappedFile.Node.Index) void {
9307 const elf_offset = elf.getNodeElfOffset(ni);
9386 const elf_offset = elf.computeNodeElfOffset(ni);
93089387 switch (elf.getNode(ni)) {
93099388 else => unreachable,
93109389 .ehdr => assert(elf_offset == 0),
......@@ -9508,32 +9587,34 @@ fn flushMoved(elf: *Elf, ni: MappedFile.Node.Index) std.mem.Allocator.Error!void
95089587 });
95099588 },
95109589 .debug_shared => |ss| {
9590 const target_section_offset = elf.computeNodeSectionOffset(ni);
95119591 var target_ri = elf.dwarf_shared.getPtr(ss).first_target_reloc;
95129592 while (target_ri != .none) {
95139593 const target_reloc = target_ri.get(elf);
95149594 assert(target_reloc.target == ni);
9515 target_reloc.apply(elf);
9595 target_reloc.flushMovedTarget(elf, target_section_offset);
95169596 target_ri = target_reloc.next;
95179597 }
95189598 },
95199599 .unit_padding, .unit_frame, .unit_debug_info, .unit_debug_line => {},
95209600 .unit_frame_cie => |ui| {
9521 const dwarf_unit = &elf.dwarf_units.items[@backingInt(ui)];
9522 var target_ri = dwarf_unit.frame_cie_first_target_reloc;
9601 const target_section_offset = elf.computeNodeSectionOffset(ni);
9602 var target_ri = elf.dwarf_units.items[@backingInt(ui)].frame_cie_first_target_reloc;
95239603 while (target_ri != .none) {
95249604 const target_reloc = target_ri.get(elf);
95259605 assert(target_reloc.target == ni);
9526 target_reloc.apply(elf);
9606 target_reloc.flushMovedTarget(elf, target_section_offset);
95279607 target_ri = target_reloc.next;
95289608 }
95299609 },
95309610 .unit_debug_info_header => |ui| {
95319611 const dwarf_unit = &elf.dwarf_units.items[@backingInt(ui)];
9612 const target_section_offset = elf.computeNodeSectionOffset(ni);
95329613 var target_ri = dwarf_unit.debug_info_header_first_target_reloc;
95339614 while (target_ri != .none) {
95349615 const target_reloc = target_ri.get(elf);
95359616 assert(target_reloc.target == ni);
9536 target_reloc.apply(elf);
9617 target_reloc.flushMovedTarget(elf, target_section_offset);
95379618 target_ri = target_reloc.next;
95389619 }
95399620 elf.flushMovedNodeRelocs(ni, elf.computeNodeVAddr(ni), .{
......@@ -9542,11 +9623,12 @@ fn flushMoved(elf: *Elf, ni: MappedFile.Node.Index) std.mem.Allocator.Error!void
95429623 },
95439624 .unit_debug_line_header => |ui| {
95449625 const dwarf_unit = &elf.dwarf_units.items[@backingInt(ui)];
9626 const target_section_offset = elf.computeNodeSectionOffset(ni);
95459627 var target_ri = dwarf_unit.debug_line_header_first_target_reloc;
95469628 while (target_ri != .none) {
95479629 const target_reloc = target_ri.get(elf);
95489630 assert(target_reloc.target == ni);
9549 target_reloc.apply(elf);
9631 target_reloc.flushMovedTarget(elf, target_section_offset);
95509632 target_ri = target_reloc.next;
95519633 }
95529634 elf.flushMovedNodeRelocs(ni, elf.computeNodeVAddr(ni), .{
......@@ -9555,34 +9637,29 @@ fn flushMoved(elf: *Elf, ni: MappedFile.Node.Index) std.mem.Allocator.Error!void
95559637 },
95569638 .unit_debug_rnglists => |ui| {
95579639 const dwarf_unit = &elf.dwarf_units.items[@backingInt(ui)];
9640 const target_section_offset = elf.computeNodeSectionOffset(ni);
95589641 var target_ri = dwarf_unit.debug_rnglists_first_target_reloc;
95599642 while (target_ri != .none) {
95609643 const target_reloc = target_ri.get(elf);
95619644 assert(target_reloc.target == ni);
9562 target_reloc.apply(elf);
9645 target_reloc.flushMovedTarget(elf, target_section_offset);
95639646 target_ri = target_reloc.next;
95649647 }
95659648 const node_vaddr = elf.computeNodeVAddr(ni);
95669649 for (dwarf_unit.debug_rnglists_symbol_relocs.keys()) |symbol_ri| {
95679650 const symbol_reloc = symbol_ri.get(elf);
95689651 assert(symbol_reloc.node == ni);
9569 if (symbol_reloc.rela_index.unwrap()) |rela_index| {
9570 // The node has moved, so the offset of the relocation within the section might have
9571 // changed, so update the `offset` field of the `ElfN.Rela` entry.
9572 symbol_reloc.relaSection(elf).relaSetOffset(elf, rela_index, node_vaddr + symbol_reloc.offset);
9573 }
9574 if (elf.ehdrType() != .REL) {
9575 symbol_reloc.apply(elf);
9576 }
9652 symbol_reloc.flushMovedNode(elf, node_vaddr);
95779653 }
95789654 },
95799655 .value_debug_info => |vi| {
95809656 const dwarf_value = &elf.dwarf_values.items[@backingInt(vi)];
9657 const target_section_offset = elf.computeNodeSectionOffset(ni);
95819658 var target_ri = dwarf_value.debug_info_first_target_reloc;
95829659 while (target_ri != .none) {
95839660 const target_reloc = target_ri.get(elf);
95849661 assert(target_reloc.target == ni);
9585 target_reloc.apply(elf);
9662 target_reloc.flushMovedTarget(elf, target_section_offset);
95869663 target_ri = target_reloc.next;
95879664 }
95889665 elf.flushMovedNodeRelocs(ni, elf.computeNodeVAddr(ni), .{
......@@ -9592,11 +9669,12 @@ fn flushMoved(elf: *Elf, ni: MappedFile.Node.Index) std.mem.Allocator.Error!void
95929669 },
95939670 .global_debug_info => |vi| {
95949671 const dwarf_global = &elf.dwarf_globals.items[@backingInt(vi)];
9672 const target_section_offset = elf.computeNodeSectionOffset(ni);
95959673 var target_ri = dwarf_global.debug_info_first_target_reloc;
95969674 while (target_ri != .none) {
95979675 const target_reloc = target_ri.get(elf);
95989676 assert(target_reloc.target == ni);
9599 target_reloc.apply(elf);
9677 target_reloc.flushMovedTarget(elf, target_section_offset);
96009678 target_ri = target_reloc.next;
96019679 }
96029680 elf.flushMovedNodeRelocs(ni, elf.computeNodeVAddr(ni), .{
......@@ -9607,7 +9685,7 @@ fn flushMoved(elf: *Elf, ni: MappedFile.Node.Index) std.mem.Allocator.Error!void
96079685 .func_frame_fde => |fi| {
96089686 const dwarf_func = &elf.dwarf_funcs.items[@backingInt(fi)];
96099687 const zcu = elf.base.comp.zcu.?;
9610 const mod = zcu.fileByIndex(fi.srcInst(&elf.dwarf).resolveFile(&zcu.intern_pool)).mod.?;
9688 const mod = zcu.navFileScope(fi.nav(&elf.dwarf)).mod.?;
96119689 switch (mod.unwind_tables) {
96129690 .none => {},
96139691 .sync, .async => {
......@@ -9622,17 +9700,18 @@ fn flushMoved(elf: *Elf, ni: MappedFile.Node.Index) std.mem.Allocator.Error!void
96229700 },
96239701 .func_debug_info => |fi| {
96249702 const dwarf_func = &elf.dwarf_funcs.items[@backingInt(fi)];
9703 const target_section_offset = elf.computeNodeSectionOffset(ni);
96259704 var target_ri = dwarf_func.debug_info_first_target_reloc;
96269705 while (target_ri != .none) {
96279706 const target_reloc = target_ri.get(elf);
96289707 assert(target_reloc.target == ni);
9629 target_reloc.apply(elf);
9708 target_reloc.flushMovedTarget(elf, target_section_offset);
96309709 target_ri = target_reloc.next;
96319710 }
96329711 elf.flushMovedNodeRelocs(ni, elf.computeNodeVAddr(ni), .{
96339712 .first_symbol_reloc = dwarf_func.debug_info_first_symbol_reloc,
9634 .skip_symbol_relocs = if (elf.navs.getPtr(fi.get(&elf.dwarf).owner_nav)) |owner_nav|
9635 owner_nav.lsi.index().ptr(elf).node
9713 .skip_symbol_relocs = if (elf.navs.getPtr(fi.nav(&elf.dwarf))) |nav|
9714 nav.lsi.index().ptr(elf).node
96369715 else
96379716 .none,
96389717 .first_node_reloc = dwarf_func.debug_info_first_node_reloc,
......@@ -10630,10 +10709,10 @@ pub fn printNode(
1063010709 .func_frame_fde, .func_debug_info, .func_debug_line => |fi| {
1063110710 const zcu = elf.base.comp.zcu.?;
1063210711 const ip = &zcu.intern_pool;
10633 const owner_nav = ip.getNav(fi.get(&elf.dwarf).owner_nav);
10712 const nav = ip.getNav(fi.nav(&elf.dwarf));
1063410713 try w.print("({f}, {f})", .{
10635 Type.fromInterned(owner_nav.resolved.?.type).fmt(.{ .zcu = zcu, .tid = tid }),
10636 owner_nav.fqn.fmt(ip),
10714 Type.fromInterned(nav.resolved.?.type).fmt(.{ .zcu = zcu, .tid = tid }),
10715 nav.fqn.fmt(ip),
1063710716 });
1063810717 },
1063910718 }
src/link/MachO.zig+2-2
......@@ -3095,8 +3095,8 @@ pub fn updateNav(self: *MachO, pt: Zcu.PerThread, nav: InternPool.Nav.Index) lin
30953095 return self.getZigObject().?.updateNav(self, pt, nav);
30963096}
30973097
3098pub fn updateLineNumber(self: *MachO, pt: Zcu.PerThread, ti_id: InternPool.TrackedInst.Index) link.Error!void {
3099 return self.getZigObject().?.updateLineNumber(pt, ti_id);
3098pub fn updateLineNumber(self: *MachO, pt: Zcu.PerThread, inst: InternPool.TrackedInst.Index, line: u32) link.Error!void {
3099 return self.getZigObject().?.updateLineNumber(pt, inst, line);
31003100}
31013101
31023102pub fn updateExports(
src/link/MachO/ZigObject.zig+2-2
......@@ -1412,11 +1412,11 @@ fn updateLazySymbol(
14121412 try macho_file.pwriteAll(code, file_offset);
14131413}
14141414
1415pub fn updateLineNumber(self: *ZigObject, pt: Zcu.PerThread, ti_id: InternPool.TrackedInst.Index) link.Error!void {
1415pub fn updateLineNumber(self: *ZigObject, pt: Zcu.PerThread, ti_id: InternPool.TrackedInst.Index, line: u32) link.Error!void {
14161416 if (self.dwarf) |*dwarf| {
14171417 const comp = dwarf.bin_file.comp;
14181418 const diags = &comp.link_diags;
1419 dwarf.updateLineNumber(pt.zcu, ti_id) catch |err| switch (err) {
1419 dwarf.updateLineNumber(pt.zcu, ti_id, line) catch |err| switch (err) {
14201420 error.OutOfMemory, error.Canceled, error.AlreadyReported => |e| return e,
14211421 else => |e| return diags.fail("failed to update dwarf line numbers: {s}", .{@errorName(e)}),
14221422 };
src/link/Spork8.zig+3-2
......@@ -168,10 +168,11 @@ pub fn updateNav(spork8: *Spork8, pt: Zcu.PerThread, nav_index: InternPool.Nav.I
168168 log.debug("updateNav {f}", .{nav.fqn.fmt(ip)});
169169}
170170
171pub fn updateLineNumber(spork8: *Spork8, pt: Zcu.PerThread, ti_id: InternPool.TrackedInst.Index) !void {
171pub fn updateLineNumber(spork8: *Spork8, pt: Zcu.PerThread, inst: InternPool.TrackedInst.Index, line: u32) !void {
172172 _ = spork8;
173173 _ = pt;
174 _ = ti_id;
174 _ = inst;
175 _ = line;
175176}
176177
177178pub fn deleteExport(
src/link/Wasm.zig+2-2
......@@ -3728,11 +3728,11 @@ pub fn updateNav(wasm: *Wasm, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index
37283728 }
37293729}
37303730
3731pub fn updateLineNumber(wasm: *Wasm, pt: Zcu.PerThread, ti_id: InternPool.TrackedInst.Index) link.Error!void {
3731pub fn updateLineNumber(wasm: *Wasm, pt: Zcu.PerThread, ti_id: InternPool.TrackedInst.Index, line: u32) link.Error!void {
37323732 const comp = wasm.base.comp;
37333733 const diags = &comp.link_diags;
37343734 if (wasm.dwarf) |*dw| {
3735 dw.updateLineNumber(pt.zcu, ti_id) catch |err| switch (err) {
3735 dw.updateLineNumber(pt.zcu, ti_id, line) catch |err| switch (err) {
37363736 error.OutOfMemory, error.Canceled, error.AlreadyReported => |e| return e,
37373737 else => |e| return diags.fail("failed to update dwarf line numbers: {s}", .{@errorName(e)}),
37383738 };