authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2026-08-30 06:24:21-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2026-09-03 12:59:38-04:00
logf9ae1f808b9e8c1d9a0394505be222f8becf4259
tree974c16daf179bb5382149969d516008f1f1dd137
parenteecbd183ccca14f904eada0b909b70812a2c9cb8

Elf2: replace `trimStart` calls with an unconditional padding node

The trim strategy came from the old dwarf impl, but it does not work with the overaligned section node optimization.

2 files changed, 9 insertions(+), 34 deletions(-)

src/link/Elf2.zig+9-17
...@@ -7189,7 +7189,6 @@ fn prelinkInner(elf: *Elf) Error!void {...@@ -7189,7 +7189,6 @@ fn prelinkInner(elf: *Elf) Error!void {
7189 }) |debug_shndx| {7189 }) |debug_shndx| {
7190 if (debug_shndx == .UNDEF) continue;7190 if (debug_shndx == .UNDEF) continue;
7191 const debug_ni = debug_shndx.get(elf).ni;7191 const debug_ni = debug_shndx.get(elf).ni;
7192 _ = debug_ni.last(&elf.mf).unwrap() orelse continue;
7193 const frame_format = debug_shndx.debugFrameFormat(elf);7192 const frame_format = debug_shndx.debugFrameFormat(elf);
7194 const unit_padding_ni = elf.addNodeAssumeCapacity(7193 const unit_padding_ni = elf.addNodeAssumeCapacity(
7195 try debug_ni.addFloatingChild(gpa, &elf.mf, .{7194 try debug_ni.addFloatingChild(gpa, &elf.mf, .{
...@@ -8941,7 +8940,6 @@ fn updateFuncInner(...@@ -8941,7 +8940,6 @@ fn updateFuncInner(
8941 .debug_frame => elf.shndx.debug_frame,8940 .debug_frame => elf.shndx.debug_frame,
8942 .eh_frame => elf.shndx.eh_frame,8941 .eh_frame => elf.shndx.eh_frame,
8943 }.get(elf).ni;8942 }.get(elf).ni;
8944 try frame_ni.trimStart(gpa, &elf.mf);
8945 switch (wip_nav.frame_format) {8943 switch (wip_nav.frame_format) {
8946 .debug_frame => {},8944 .debug_frame => {},
8947 .eh_frame => {8945 .eh_frame => {
...@@ -8953,11 +8951,6 @@ fn updateFuncInner(...@@ -8953,11 +8951,6 @@ fn updateFuncInner(
8953 },8951 },
8954 .dwarf2 => |debug| {8952 .dwarf2 => |debug| {
8955 try debug.finishFunc(func_length);8953 try debug.finishFunc(func_length);
8956 for ([3]Section.Index{
8957 elf.shndx.debug_info,
8958 elf.shndx.debug_line,
8959 elf.shndx.debug_rnglists,
8960 }) |debug_shndx| try debug_shndx.get(elf).ni.trimStart(gpa, &elf.mf);
8961 const unit = debug.wip_nav.unit.get(debug.wip_nav.dwarf);8954 const unit = debug.wip_nav.unit.get(debug.wip_nav.dwarf);
8962 {8955 {
8963 const debug_info_ni = unit.debug_info_ni.unwrap().?;8956 const debug_info_ni = unit.debug_info_ni.unwrap().?;
...@@ -10928,19 +10921,21 @@ pub fn printNode(...@@ -10928,19 +10921,21 @@ pub fn printNode(
10928 const zcu = elf.base.comp.zcu.?;10921 const zcu = elf.base.comp.zcu.?;
10929 const ip = &zcu.intern_pool;10922 const ip = &zcu.intern_pool;
10930 const nav = ip.getNav(gi.nav(&elf.dwarf));10923 const nav = ip.getNav(gi.nav(&elf.dwarf));
10931 try w.print("({f}, {f})", .{10924 try w.writeByte('(');
10932 Type.fromInterned(nav.resolved.?.type).fmt(.{ .zcu = zcu, .tid = tid }),10925 if (nav.resolved) |resolved| try w.print("{f}, ", .{
10933 nav.fqn.fmt(ip),10926 Type.fromInterned(resolved.type).fmt(.{ .zcu = zcu, .tid = tid }),
10934 });10927 });
10928 try w.print("{f})", .{nav.fqn.fmt(ip)});
10935 },10929 },
10936 .func_frame_fde, .func_debug_info, .func_debug_line => |fi| {10930 .func_frame_fde, .func_debug_info, .func_debug_line => |fi| {
10937 const zcu = elf.base.comp.zcu.?;10931 const zcu = elf.base.comp.zcu.?;
10938 const ip = &zcu.intern_pool;10932 const ip = &zcu.intern_pool;
10939 const nav = ip.getNav(fi.nav(&elf.dwarf));10933 const nav = ip.getNav(fi.nav(&elf.dwarf));
10940 try w.print("({f}, {f})", .{10934 try w.writeByte('(');
10941 Type.fromInterned(nav.resolved.?.type).fmt(.{ .zcu = zcu, .tid = tid }),10935 if (nav.resolved) |resolved| try w.print("{f}, ", .{
10942 nav.fqn.fmt(ip),10936 Type.fromInterned(resolved.type).fmt(.{ .zcu = zcu, .tid = tid }),
10943 });10937 });
10938 try w.print("{f})", .{nav.fqn.fmt(ip)});
10944 },10939 },
10945 .decl_debug_info => |di| {10940 .decl_debug_info => |di| {
10946 const comp = elf.base.comp;10941 const comp = elf.base.comp;
...@@ -10948,10 +10943,7 @@ pub fn printNode(...@@ -10948,10 +10943,7 @@ pub fn printNode(
10948 const ip = &zcu.intern_pool;10943 const ip = &zcu.intern_pool;
10949 const src_inst = di.srcInst(&elf.dwarf);10944 const src_inst = di.srcInst(&elf.dwarf);
10950 try w.print("({f}, ", .{zcu.fileByIndex(src_inst.resolveFile(ip)).path.fmt(comp)});10945 try w.print("({f}, ", .{zcu.fileByIndex(src_inst.resolveFile(ip)).path.fmt(comp)});
10951 if (src_inst.resolve(ip)) |inst|10946 if (src_inst.resolve(ip)) |inst| try w.print("%{d}", .{inst}) else try w.writeAll("lost");
10952 try w.print("%{d}", .{inst})
10953 else
10954 try w.writeAll("lost");
10955 try w.writeByte(')');10947 try w.writeByte(')');
10956 },10948 },
10957 }10949 }
src/link/MappedFile.zig-17
...@@ -664,23 +664,6 @@ pub const Node = extern struct {...@@ -664,23 +664,6 @@ pub const Node = extern struct {
664 return true;664 return true;
665 }665 }
666666
667 pub fn trimStart(ni: Node.Index, gpa: Allocator, mf: *MappedFile) Allocator.Error!void {
668 mf.nodes_lock.assertUnlocked();
669 const node = ni.get(mf);
670 const first_ni = node.first.unwrap() orelse return;
671 const shift, _ = first_ni.location(mf).resolve(mf);
672 if (shift == 0) return;
673 const offset, const size = node.location().resolve(mf);
674 try ni.setLocation(gpa, mf, offset + shift, size - shift);
675 var child_oni = node.first;
676 while (child_oni.unwrap()) |child_ni| {
677 const child_node = child_ni.get(mf);
678 const child_offset, const child_size = child_node.location().resolve(mf);
679 try child_ni.setLocation(gpa, mf, child_offset - shift, child_size);
680 child_oni = child_node.next;
681 }
682 }
683
684 /// Ensures that the size of `ni` is at least `min_size`. Valid for any node.667 /// Ensures that the size of `ni` is at least `min_size`. Valid for any node.
685 ///668 ///
686 /// Applies `growth_factor` if necessary (so the caller should *not* apply `growth_factor`).669 /// Applies `growth_factor` if necessary (so the caller should *not* apply `growth_factor`).