authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-04-14 02:01:16-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-04-14 02:01:16-04:00
log59fa548be8944962d5882e2ac38f6013ac00e84a
tree767aa3d1d895975f7d162c07423ccf30b8bc39c1
parent9c509f1526b4560b4e97367a18755a9d1ae6fcf7
parentedb428fae42ea82c49347fce6d48d80f1fed6ef1
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #11429 from ziglang/stage2-alive-decls

stage2: fix (recursively) marking decls as alive, and improve DWARF for local vars

5 files changed, 218 insertions(+), 46 deletions(-)

src/arch/x86_64/CodeGen.zig+81-43
......@@ -3903,17 +3903,17 @@ fn airDbgVar(self: *Self, inst: Air.Inst.Index) !void {
39033903 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
39043904 const operand = pl_op.operand;
39053905 const ty = self.air.typeOf(operand);
3906 const mcv = try self.resolveInst(operand);
39063907
3907 if (!self.liveness.operandDies(inst, 0)) {
3908 const mcv = try self.resolveInst(operand);
3909 const name = self.air.nullTerminatedString(pl_op.payload);
3908 log.debug("airDbgVar: %{d}: {}, {}", .{ inst, ty.fmtDebug(), mcv });
39103909
3911 const tag = self.air.instructions.items(.tag)[inst];
3912 switch (tag) {
3913 .dbg_var_ptr => try self.genVarDbgInfo(ty.childType(), mcv, name),
3914 .dbg_var_val => try self.genVarDbgInfo(ty, mcv, name),
3915 else => unreachable,
3916 }
3910 const name = self.air.nullTerminatedString(pl_op.payload);
3911
3912 const tag = self.air.instructions.items(.tag)[inst];
3913 switch (tag) {
3914 .dbg_var_ptr => try self.genVarDbgInfo(tag, ty.childType(), mcv, name),
3915 .dbg_var_val => try self.genVarDbgInfo(tag, ty, mcv, name),
3916 else => unreachable,
39173917 }
39183918
39193919 return self.finishAir(inst, .dead, .{ operand, .none, .none });
......@@ -3921,36 +3921,27 @@ fn airDbgVar(self: *Self, inst: Air.Inst.Index) !void {
39213921
39223922fn genVarDbgInfo(
39233923 self: *Self,
3924 tag: Air.Inst.Tag,
39243925 ty: Type,
39253926 mcv: MCValue,
39263927 name: [:0]const u8,
39273928) !void {
39283929 const name_with_null = name.ptr[0 .. name.len + 1];
3929 switch (mcv) {
3930 .register => |reg| {
3931 switch (self.debug_output) {
3932 .dwarf => |dw| {
3933 const dbg_info = &dw.dbg_info;
3934 try dbg_info.ensureUnusedCapacity(3);
3935 dbg_info.appendAssumeCapacity(@enumToInt(link.File.Dwarf.AbbrevKind.variable));
3930 switch (self.debug_output) {
3931 .dwarf => |dw| {
3932 const dbg_info = &dw.dbg_info;
3933 try dbg_info.append(@enumToInt(link.File.Dwarf.AbbrevKind.variable));
3934
3935 switch (mcv) {
3936 .register => |reg| {
3937 try dbg_info.ensureUnusedCapacity(2);
39363938 dbg_info.appendSliceAssumeCapacity(&[2]u8{ // DW.AT.location, DW.FORM.exprloc
39373939 1, // ULEB128 dwarf expression length
39383940 reg.dwarfLocOp(),
39393941 });
3940 try dbg_info.ensureUnusedCapacity(5 + name_with_null.len);
3941 try self.addDbgInfoTypeReloc(ty); // DW.AT.type, DW.FORM.ref4
3942 dbg_info.appendSliceAssumeCapacity(name_with_null); // DW.AT.name, DW.FORM.string
39433942 },
3944 .plan9 => {},
3945 .none => {},
3946 }
3947 },
3948 .ptr_stack_offset, .stack_offset => |off| {
3949 switch (self.debug_output) {
3950 .dwarf => |dw| {
3951 const dbg_info = &dw.dbg_info;
3952 try dbg_info.ensureUnusedCapacity(8);
3953 dbg_info.appendAssumeCapacity(@enumToInt(link.File.Dwarf.AbbrevKind.variable));
3943 .ptr_stack_offset, .stack_offset => |off| {
3944 try dbg_info.ensureUnusedCapacity(7);
39543945 const fixup = dbg_info.items.len;
39553946 dbg_info.appendSliceAssumeCapacity(&[2]u8{ // DW.AT.location, DW.FORM.exprloc
39563947 1, // we will backpatch it after we encode the displacement in LEB128
......@@ -3958,18 +3949,54 @@ fn genVarDbgInfo(
39583949 });
39593950 leb128.writeILEB128(dbg_info.writer(), -off) catch unreachable;
39603951 dbg_info.items[fixup] += @intCast(u8, dbg_info.items.len - fixup - 2);
3961 try dbg_info.ensureUnusedCapacity(5 + name_with_null.len);
3962 try self.addDbgInfoTypeReloc(ty); // DW.AT.type, DW.FORM.ref4
3963 dbg_info.appendSliceAssumeCapacity(name_with_null); // DW.AT.name, DW.FORM.string
3964
39653952 },
3966 .plan9 => {},
3967 .none => {},
3953 .memory, .got_load, .direct_load => {
3954 const endian = self.target.cpu.arch.endian();
3955 const ptr_width = @intCast(u8, @divExact(self.target.cpu.arch.ptrBitWidth(), 8));
3956 const is_ptr = switch (tag) {
3957 .dbg_var_ptr => true,
3958 .dbg_var_val => false,
3959 else => unreachable,
3960 };
3961 try dbg_info.ensureUnusedCapacity(2 + ptr_width);
3962 dbg_info.appendSliceAssumeCapacity(&[2]u8{ // DW.AT.location, DW.FORM.exprloc
3963 1 + ptr_width + @boolToInt(is_ptr),
3964 DW.OP.addr, // literal address
3965 });
3966 const offset = @intCast(u32, dbg_info.items.len);
3967 const addr = switch (mcv) {
3968 .memory => |addr| addr,
3969 else => 0,
3970 };
3971 switch (ptr_width) {
3972 0...4 => {
3973 try dbg_info.writer().writeInt(u32, @intCast(u32, addr), endian);
3974 },
3975 5...8 => {
3976 try dbg_info.writer().writeInt(u64, addr, endian);
3977 },
3978 else => unreachable,
3979 }
3980 if (is_ptr) {
3981 // We need deref the address as we point to the value via GOT entry.
3982 try dbg_info.append(DW.OP.deref);
3983 }
3984 switch (mcv) {
3985 .got_load, .direct_load => |index| try dw.addExprlocReloc(index, offset, is_ptr),
3986 else => {},
3987 }
3988 },
3989 else => {
3990 log.debug("TODO generate debug info for {}", .{mcv});
3991 },
39683992 }
3993
3994 try dbg_info.ensureUnusedCapacity(5 + name_with_null.len);
3995 try self.addDbgInfoTypeReloc(ty); // DW.AT.type, DW.FORM.ref4
3996 dbg_info.appendSliceAssumeCapacity(name_with_null); // DW.AT.name, DW.FORM.string
39693997 },
3970 else => {
3971 log.debug("TODO generate debug info for {}", .{mcv});
3972 },
3998 .plan9 => {},
3999 .none => {},
39734000 }
39744001}
39754002
......@@ -6089,6 +6116,7 @@ fn limitImmediateType(self: *Self, operand: Air.Inst.Ref, comptime T: type) !MCV
60896116}
60906117
60916118fn lowerDeclRef(self: *Self, tv: TypedValue, decl: *Module.Decl) InnerError!MCValue {
6119 log.debug("lowerDeclRef: ty = {}, val = {}", .{ tv.ty.fmtDebug(), tv.val.fmtDebug() });
60926120 const ptr_bits = self.target.cpu.arch.ptrBitWidth();
60936121 const ptr_bytes: u64 = @divExact(ptr_bits, 8);
60946122
......@@ -6100,7 +6128,8 @@ fn lowerDeclRef(self: *Self, tv: TypedValue, decl: *Module.Decl) InnerError!MCVa
61006128 }
61016129 }
61026130
6103 decl.alive = true;
6131 decl.markAlive();
6132
61046133 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
61056134 const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];
61066135 const got_addr = got.p_vaddr + decl.link.elf.offset_table_index * ptr_bytes;
......@@ -6120,8 +6149,6 @@ fn lowerDeclRef(self: *Self, tv: TypedValue, decl: *Module.Decl) InnerError!MCVa
61206149 } else {
61216150 return self.fail("TODO codegen non-ELF const Decl pointer", .{});
61226151 }
6123
6124 _ = tv;
61256152}
61266153
61276154fn lowerUnnamedConst(self: *Self, tv: TypedValue) InnerError!MCValue {
......@@ -6144,6 +6171,7 @@ fn lowerUnnamedConst(self: *Self, tv: TypedValue) InnerError!MCValue {
61446171}
61456172
61466173fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue {
6174 log.debug("genTypedValue: ty = {}, val = {}", .{ typed_value.ty.fmtDebug(), typed_value.val.fmtDebug() });
61476175 if (typed_value.val.isUndef())
61486176 return MCValue{ .undef = {} };
61496177 const ptr_bits = self.target.cpu.arch.ptrBitWidth();
......@@ -6181,8 +6209,6 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue {
61816209 .Bool => {
61826210 return MCValue{ .immediate = @boolToInt(typed_value.val.toBool()) };
61836211 },
6184 .ComptimeInt => unreachable, // semantic analysis prevents this
6185 .ComptimeFloat => unreachable, // semantic analysis prevents this
61866212 .Optional => {
61876213 if (typed_value.ty.isPtrLikeOptional()) {
61886214 if (typed_value.val.isNull())
......@@ -6243,6 +6269,18 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue {
62436269 }
62446270 }
62456271 },
6272
6273 .ComptimeInt => unreachable,
6274 .ComptimeFloat => unreachable,
6275 .Type => unreachable,
6276 .EnumLiteral => unreachable,
6277 .Void => unreachable,
6278 .NoReturn => unreachable,
6279 .Undefined => unreachable,
6280 .Null => unreachable,
6281 .BoundFn => unreachable,
6282 .Opaque => unreachable,
6283
62466284 else => {},
62476285 }
62486286
src/codegen.zig-2
......@@ -203,7 +203,6 @@ pub fn generateSymbol(
203203 },
204204 .Array => switch (typed_value.val.tag()) {
205205 .bytes => {
206 // TODO populate .debug_info for the array
207206 const payload = typed_value.val.castTag(.bytes).?;
208207 const len = @intCast(usize, typed_value.ty.arrayLenIncludingSentinel());
209208 // The bytes payload already includes the sentinel, if any
......@@ -212,7 +211,6 @@ pub fn generateSymbol(
212211 return Result{ .appended = {} };
213212 },
214213 .aggregate => {
215 // TODO populate .debug_info for the array
216214 const elem_vals = typed_value.val.castTag(.aggregate).?.data;
217215 const elem_ty = typed_value.ty.elemType();
218216 const len = @intCast(usize, typed_value.ty.arrayLenIncludingSentinel());
src/link/Dwarf.zig+78
......@@ -79,6 +79,7 @@ pub const DeclState = struct {
7979 std.hash_map.default_max_load_percentage,
8080 ) = .{},
8181 abbrev_relocs: std.ArrayListUnmanaged(AbbrevRelocation) = .{},
82 exprloc_relocs: std.ArrayListUnmanaged(ExprlocRelocation) = .{},
8283
8384 fn init(gpa: Allocator, target: std.Target) DeclState {
8485 return .{
......@@ -97,6 +98,16 @@ pub const DeclState = struct {
9798 self.abbrev_table.deinit(self.gpa);
9899 self.abbrev_resolver.deinit(self.gpa);
99100 self.abbrev_relocs.deinit(self.gpa);
101 self.exprloc_relocs.deinit(self.gpa);
102 }
103
104 pub fn addExprlocReloc(self: *DeclState, target: u32, offset: u32, is_ptr: bool) !void {
105 log.debug("{x}: target sym @{d}, via GOT {}", .{ offset, target, is_ptr });
106 try self.exprloc_relocs.append(self.gpa, .{
107 .@"type" = if (is_ptr) .got_load else .direct_load,
108 .target = target,
109 .offset = offset,
110 });
100111 }
101112
102113 pub fn addTypeReloc(
......@@ -270,6 +281,27 @@ pub const DeclState = struct {
270281 try self.addTypeReloc(atom, ty.childType(), @intCast(u32, index), null);
271282 }
272283 },
284 .Array => {
285 // DW.AT.array_type
286 try dbg_info_buffer.append(@enumToInt(AbbrevKind.array_type));
287 // DW.AT.name, DW.FORM.string
288 try dbg_info_buffer.writer().print("{}\x00", .{ty.fmt(target)});
289 // DW.AT.type, DW.FORM.ref4
290 var index = dbg_info_buffer.items.len;
291 try dbg_info_buffer.resize(index + 4);
292 try self.addTypeReloc(atom, ty.childType(), @intCast(u32, index), null);
293 // DW.AT.subrange_type
294 try dbg_info_buffer.append(@enumToInt(AbbrevKind.array_dim));
295 // DW.AT.type, DW.FORM.ref4
296 index = dbg_info_buffer.items.len;
297 try dbg_info_buffer.resize(index + 4);
298 try self.addTypeReloc(atom, Type.usize, @intCast(u32, index), null);
299 // DW.AT.count, DW.FORM.udata
300 const len = ty.arrayLenIncludingSentinel();
301 try leb128.writeULEB128(dbg_info_buffer.writer(), len);
302 // DW.AT.array_type delimit children
303 try dbg_info_buffer.append(0);
304 },
273305 .Struct => blk: {
274306 // DW.AT.structure_type
275307 try dbg_info_buffer.append(@enumToInt(AbbrevKind.struct_type));
......@@ -528,6 +560,18 @@ pub const AbbrevRelocation = struct {
528560 addend: u32,
529561};
530562
563pub const ExprlocRelocation = struct {
564 /// Type of the relocation: direct load ref, or GOT load ref (via GOT table)
565 @"type": enum {
566 direct_load,
567 got_load,
568 },
569 /// Index of the target in the linker's locals symbol table.
570 target: u32,
571 /// Offset within the debug info buffer where to patch up the address value.
572 offset: u32,
573};
574
531575pub const SrcFn = struct {
532576 /// Offset from the beginning of the Debug Line Program header that contains this function.
533577 off: u32,
......@@ -564,6 +608,8 @@ pub const AbbrevKind = enum(u8) {
564608 pad1,
565609 parameter,
566610 variable,
611 array_type,
612 array_dim,
567613};
568614
569615/// The reloc offset for the virtual address of a function in its Line Number Program.
......@@ -986,6 +1032,26 @@ pub fn commitDeclState(
9861032 }
9871033 }
9881034
1035 while (decl_state.exprloc_relocs.popOrNull()) |reloc| {
1036 switch (self.tag) {
1037 .macho => {
1038 const macho_file = file.cast(File.MachO).?;
1039 const d_sym = &macho_file.d_sym.?;
1040 try d_sym.relocs.append(d_sym.base.base.allocator, .{
1041 .@"type" = switch (reloc.@"type") {
1042 .direct_load => .direct_load,
1043 .got_load => .got_load,
1044 },
1045 .target = reloc.target,
1046 .offset = reloc.offset + atom.off,
1047 .addend = 0,
1048 .prev_vaddr = 0,
1049 });
1050 },
1051 else => unreachable,
1052 }
1053 }
1054
9891055 try self.writeDeclDebugInfo(file, atom, dbg_info_buffer.items);
9901056}
9911057
......@@ -1357,6 +1423,18 @@ pub fn writeDbgAbbrev(self: *Dwarf, file: *File) !void {
13571423 DW.AT.name, DW.FORM.string,
13581424 0,
13591425 0, // table sentinel
1426 @enumToInt(AbbrevKind.array_type),
1427 DW.TAG.array_type, DW.CHILDREN.yes, // header
1428 DW.AT.name, DW.FORM.string,
1429 DW.AT.type, DW.FORM.ref4,
1430 0,
1431 0, // table sentinel
1432 @enumToInt(AbbrevKind.array_dim),
1433 DW.TAG.subrange_type, DW.CHILDREN.no, // header
1434 DW.AT.type, DW.FORM.ref4,
1435 DW.AT.count, DW.FORM.udata,
1436 0,
1437 0, // table sentinel
13601438 0,
13611439 0,
13621440 0, // section sentinel
src/link/MachO.zig+8
......@@ -3472,6 +3472,9 @@ pub fn closeFiles(self: MachO) void {
34723472 for (self.dylibs.items) |dylib| {
34733473 dylib.file.close();
34743474 }
3475 if (self.d_sym) |ds| {
3476 ds.file.close();
3477 }
34753478}
34763479
34773480fn freeAtom(self: *MachO, atom: *Atom, match: MatchingSection, owns_atom: bool) void {
......@@ -4274,6 +4277,11 @@ pub fn freeDecl(self: *MachO, decl: *Module.Decl) void {
42744277 self.got_entries_free_list.append(self.base.allocator, @intCast(u32, got_index)) catch {};
42754278 self.got_entries.items[got_index] = .{ .target = .{ .local = 0 }, .atom = undefined };
42764279 _ = self.got_entries_table.swapRemove(.{ .local = decl.link.macho.local_sym_index });
4280
4281 if (self.d_sym) |*d_sym| {
4282 d_sym.swapRemoveRelocs(decl.link.macho.local_sym_index);
4283 }
4284
42774285 log.debug(" adding GOT index {d} to free list (target local@{d})", .{
42784286 got_index,
42794287 decl.link.macho.local_sym_index,
src/link/MachO/DebugSymbols.zig+51-1
......@@ -59,6 +59,19 @@ debug_aranges_section_dirty: bool = false,
5959debug_info_header_dirty: bool = false,
6060debug_line_header_dirty: bool = false,
6161
62relocs: std.ArrayListUnmanaged(Reloc) = .{},
63
64pub const Reloc = struct {
65 @"type": enum {
66 direct_load,
67 got_load,
68 },
69 target: u32,
70 offset: u64,
71 addend: u32,
72 prev_vaddr: u64,
73};
74
6275/// You must call this function *after* `MachO.populateMissingMetadata()`
6376/// has been called to get a viable debug symbols output.
6477pub fn populateMissingMetadata(self: *DebugSymbols, allocator: Allocator) !void {
......@@ -254,6 +267,30 @@ pub fn flushModule(self: *DebugSymbols, allocator: Allocator, options: link.Opti
254267 // Zig source code.
255268 const module = options.module orelse return error.LinkingWithoutZigSourceUnimplemented;
256269
270 for (self.relocs.items) |*reloc| {
271 const sym = switch (reloc.@"type") {
272 .direct_load => self.base.locals.items[reloc.target],
273 .got_load => blk: {
274 const got_index = self.base.got_entries_table.get(.{ .local = reloc.target }).?;
275 const got_entry = self.base.got_entries.items[got_index];
276 break :blk self.base.locals.items[got_entry.atom.local_sym_index];
277 },
278 };
279 if (sym.n_value == reloc.prev_vaddr) continue;
280
281 const seg = &self.load_commands.items[self.dwarf_segment_cmd_index.?].segment;
282 const sect = &seg.sections.items[self.debug_info_section_index.?];
283 const file_offset = sect.offset + reloc.offset;
284 log.debug("resolving relocation: {d}@{x} ('{s}') at offset {x}", .{
285 reloc.target,
286 sym.n_value,
287 self.base.getString(sym.n_strx),
288 file_offset,
289 });
290 try self.file.pwriteAll(mem.asBytes(&sym.n_value), file_offset);
291 reloc.prev_vaddr = sym.n_value;
292 }
293
257294 if (self.debug_abbrev_section_dirty) {
258295 try self.dwarf.writeDbgAbbrev(&self.base.base);
259296 self.load_commands_dirty = true;
......@@ -330,7 +367,20 @@ pub fn deinit(self: *DebugSymbols, allocator: Allocator) void {
330367 }
331368 self.load_commands.deinit(allocator);
332369 self.dwarf.deinit();
333 self.file.close();
370 self.relocs.deinit(allocator);
371}
372
373pub fn swapRemoveRelocs(self: *DebugSymbols, target: u32) void {
374 // TODO re-implement using a hashmap with free lists
375 var last_index: usize = 0;
376 while (last_index < self.relocs.items.len) {
377 const reloc = self.relocs.items[last_index];
378 if (reloc.target == target) {
379 _ = self.relocs.swapRemove(last_index);
380 } else {
381 last_index += 1;
382 }
383 }
334384}
335385
336386fn copySegmentCommand(