authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-05-04 21:25:33+02:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-05-09 18:51:46+02:00
log2ae2ac33d9ddd1fb181e08a811d97b1bf238bced
treea9b3d1f461ef443259d947f3840cb4e3da5e19b6
parent9b6b7034c20a0089b88e58b5142c91b2d26bcef8

wasm: Emit debug sections

This commit adds the ability to emit the following debug sections: .debug_info .debug_abbrev .debug_line .debug_str Line information and files are now being loaded correctly by browser debuggers.

3 files changed, 99 insertions(+), 27 deletions(-)

src/link/Dwarf.zig+45-11
......@@ -963,21 +963,18 @@ pub fn commitDeclState(
963963 const wasm_file = file.cast(File.Wasm).?;
964964 const segment_index = try wasm_file.getDebugLineIndex();
965965 const segment = &wasm_file.segments.items[segment_index];
966 const debug_atom = wasm_file.atoms.get(segment_index).?;
966 const debug_line = &wasm_file.debug_line;
967967 if (needed_size != segment.size) {
968968 log.debug(" needed size does not equal allocated size: {d}", .{needed_size});
969969 if (needed_size > segment.size) {
970 log.debug(" allocating {d} bytes for debug line information", .{needed_size - segment.size});
971 try debug_atom.code.resize(self.allocator, needed_size);
972 std.mem.set(u8, debug_atom.code.items[segment.size..], 0);
970 log.debug(" allocating {d} bytes for 'debug line' information", .{needed_size - segment.size});
971 try debug_line.resize(self.allocator, needed_size);
972 mem.set(u8, debug_line.items[segment.size..], 0);
973973 }
974 debug_atom.size = needed_size;
975974 segment.size = needed_size;
976975 }
977 // since we can tighly pack the debug lines, wasm does not require
978 // us to pad with Nops.
979976 const offset = segment.offset + src_fn.off;
980 std.mem.copy(u8, debug_atom.code.items[offset..], dbg_line_buffer.items);
977 mem.copy(u8, debug_line.items[offset..], dbg_line_buffer.items);
981978 },
982979 else => unreachable,
983980 }
......@@ -1116,7 +1113,9 @@ fn updateDeclDebugInfoAllocation(self: *Dwarf, file: *File, atom: *Atom, len: u3
11161113 const file_pos = debug_info_sect.offset + atom.off;
11171114 try pwriteDbgInfoNops(d_sym.file, file_pos, 0, &[0]u8{}, atom.len, false);
11181115 },
1119 .wasm => {},
1116 .wasm => {
1117 log.debug(" todo: updateDeclDebugInfoAllocation for Wasm: {d}", .{atom.len});
1118 },
11201119 else => unreachable,
11211120 }
11221121 // TODO Look at the free list before appending at the end.
......@@ -1241,6 +1240,23 @@ fn writeDeclDebugInfo(self: *Dwarf, file: *File, atom: *Atom, dbg_info_buf: []co
12411240 trailing_zero,
12421241 );
12431242 },
1243 .wasm => {
1244 const wasm_file = file.cast(File.Wasm).?;
1245 const segment_index = try wasm_file.getDebugInfoIndex();
1246 const segment = &wasm_file.segments.items[segment_index];
1247 const debug_info = &wasm_file.debug_info;
1248 if (needed_size != segment.size) {
1249 log.debug(" needed size does not equal allocated size: {d}", .{needed_size});
1250 if (needed_size > segment.size) {
1251 log.debug(" allocating {d} bytes for 'debug info' information", .{needed_size - segment.size});
1252 try debug_info.resize(self.allocator, needed_size);
1253 mem.set(u8, debug_info.items[segment.size..], 0);
1254 }
1255 segment.size = needed_size;
1256 }
1257 const offset = segment.offset + atom.off;
1258 mem.copy(u8, debug_info.items[offset..], dbg_info_buf);
1259 },
12441260 else => unreachable,
12451261 }
12461262}
......@@ -1279,8 +1295,7 @@ pub fn updateDeclLineNumber(self: *Dwarf, file: *File, decl: *const Module.Decl)
12791295 const segment_index = wasm_file.getDebugLineIndex() catch unreachable;
12801296 const segment = wasm_file.segments.items[segment_index];
12811297 const offset = segment.offset + decl.fn_link.wasm.src_fn.off + self.getRelocDbgLineOff();
1282 const debug_atom = wasm_file.atoms.get(segment_index).?;
1283 std.mem.copy(u8, debug_atom.code.items[offset..], &data);
1298 mem.copy(u8, wasm_file.debug_line.items[offset..], &data);
12841299 },
12851300 else => unreachable,
12861301 }
......@@ -1514,6 +1529,11 @@ pub fn writeDbgAbbrev(self: *Dwarf, file: *File) !void {
15141529 const file_pos = debug_abbrev_sect.offset + abbrev_offset;
15151530 try d_sym.file.pwriteAll(&abbrev_buf, file_pos);
15161531 },
1532 .wasm => {
1533 const wasm_file = file.cast(File.Wasm).?;
1534 try wasm_file.debug_abbrev.resize(wasm_file.base.allocator, needed_size);
1535 mem.copy(u8, wasm_file.debug_abbrev.items, &abbrev_buf);
1536 },
15171537 else => unreachable,
15181538 }
15191539}
......@@ -1621,6 +1641,10 @@ pub fn writeDbgInfoHeader(self: *Dwarf, file: *File, module: *Module, low_pc: u6
16211641 const file_pos = debug_info_sect.offset;
16221642 try pwriteDbgInfoNops(d_sym.file, file_pos, 0, di_buf.items, jmp_amt, false);
16231643 },
1644 .wasm => {
1645 const wasm_file = file.cast(File.Wasm).?;
1646 mem.copy(u8, wasm_file.debug_info.items, di_buf.items);
1647 },
16241648 else => unreachable,
16251649 }
16261650}
......@@ -2004,6 +2028,10 @@ pub fn writeDbgLineHeader(self: *Dwarf, file: *File, module: *Module) !void {
20042028 const file_pos = debug_line_sect.offset;
20052029 try pwriteDbgLineNops(d_sym.file, file_pos, 0, di_buf.items, jmp_amt);
20062030 },
2031 .wasm => {
2032 const wasm_file = file.cast(File.Wasm).?;
2033 mem.copy(u8, wasm_file.debug_line.items, di_buf.items);
2034 },
20072035 else => unreachable,
20082036 }
20092037}
......@@ -2127,6 +2155,8 @@ pub fn flushModule(self: *Dwarf, file: *File, module: *Module) !void {
21272155 const debug_info_sect = &dwarf_segment.sections.items[d_sym.debug_info_section_index.?];
21282156 break :blk debug_info_sect.offset;
21292157 },
2158 // for wasm, the offset is always 0 as we write to memory first
2159 .wasm => break :blk @as(u32, 0),
21302160 else => unreachable,
21312161 }
21322162 };
......@@ -2145,6 +2175,10 @@ pub fn flushModule(self: *Dwarf, file: *File, module: *Module) !void {
21452175 const d_sym = &macho_file.d_sym.?;
21462176 try d_sym.file.pwriteAll(&buf, file_pos + reloc.atom.off + reloc.offset);
21472177 },
2178 .wasm => {
2179 const wasm_file = file.cast(File.Wasm).?;
2180 mem.copy(u8, wasm_file.debug_info.items[reloc.atom.off + reloc.offset ..], &buf);
2181 },
21482182 else => unreachable,
21492183 }
21502184 }
src/link/Wasm.zig+54-7
......@@ -90,6 +90,14 @@ string_table: StringTable = .{},
9090/// Debug information for wasm
9191dwarf: ?Dwarf = null,
9292
93// *debug information* //
94/// Contains all bytes for the '.debug_info' section
95debug_info: std.ArrayListUnmanaged(u8) = .{},
96/// Contains all bytes for the '.debug_line' section
97debug_line: std.ArrayListUnmanaged(u8) = .{},
98/// Contains all bytes for the '.debug_abbrev' section
99debug_abbrev: std.ArrayListUnmanaged(u8) = .{},
100
93101// Output sections
94102/// Output type section
95103func_types: std.ArrayListUnmanaged(wasm.Type) = .{},
......@@ -501,6 +509,10 @@ pub fn deinit(self: *Wasm) void {
501509 if (self.dwarf) |*dwarf| {
502510 dwarf.deinit();
503511 }
512
513 self.debug_info.deinit(gpa);
514 self.debug_line.deinit(gpa);
515 self.debug_abbrev.deinit(gpa);
504516}
505517
506518pub fn allocateDeclIndexes(self: *Wasm, decl_index: Module.Decl.Index) !void {
......@@ -576,7 +588,9 @@ pub fn updateFunc(self: *Wasm, mod: *Module, func: *Module.Fn, air: Air, livenes
576588 &self.base,
577589 mod,
578590 decl,
579 // Actual value will be written after relocation
591 // Actual value will be written after relocation.
592 // For Wasm, this is the offset relative to the code section
593 // which isn't known until flush().
580594 0,
581595 code.len,
582596 &decl_state.?,
......@@ -1016,10 +1030,12 @@ fn parseAtom(self: *Wasm, atom: *Atom, kind: Kind) !void {
10161030 // segment indexes can be off by 1 due to also containing a segment
10171031 // for the code section, so we must check if the existing segment
10181032 // is larger than that of the code section, and substract the index by 1 in such case.
1019 const info_add = if (self.code_section_index) |idx| blk: {
1033 var info_add = if (self.code_section_index) |idx| blk: {
10201034 if (idx < index) break :blk @as(u32, 1);
10211035 break :blk 0;
10221036 } else @as(u32, 0);
1037 if (self.debug_info_index != null) info_add += 1;
1038 if (self.debug_line_index != null) info_add += 1;
10231039 symbol.index = index - info_add;
10241040 // segment info already exists, so free its memory
10251041 self.base.allocator.free(segment_name);
......@@ -1320,11 +1336,8 @@ fn setupMemory(self: *Wasm) !void {
13201336 }
13211337
13221338 var offset: u32 = @intCast(u32, memory_ptr);
1323 for (self.segments.items) |*segment, i| {
1324 // skip 'code' segments
1325 if (self.code_section_index) |index| {
1326 if (index == i) continue;
1327 }
1339 for (self.data_segments.values()) |segment_index| {
1340 const segment = &self.segments.items[segment_index];
13281341 memory_ptr = std.mem.alignForwardGeneric(u64, memory_ptr, segment.alignment);
13291342 memory_ptr += segment.size;
13301343 segment.offset = offset;
......@@ -1588,6 +1601,7 @@ fn resetState(self: *Wasm) void {
15881601 self.symbol_atom.clearRetainingCapacity();
15891602 self.code_section_index = null;
15901603 self.debug_info_index = null;
1604 self.debug_line_index = null;
15911605}
15921606
15931607pub fn flush(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !void {
......@@ -2016,10 +2030,43 @@ pub fn flushModule(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod
20162030 try self.emitDataRelocations(file, arena, data_index, symbol_table);
20172031 }
20182032 } else if (!self.base.options.strip) {
2033 if (self.dwarf) |*dwarf| {
2034 if (self.debug_info_index != null) {
2035 _ = dwarf;
2036 try dwarf.writeDbgAbbrev(&self.base);
2037 try dwarf.writeDbgInfoHeader(&self.base, mod, 0, 0);
2038 try dwarf.writeDbgLineHeader(&self.base, mod);
2039
2040 try emitDebugSection(file, self.debug_info.items, ".debug_info");
2041 try emitDebugSection(file, self.debug_abbrev.items, ".debug_abbrev"); // TODO
2042 try emitDebugSection(file, self.debug_line.items, ".debug_line");
2043 try emitDebugSection(file, dwarf.strtab.items, ".debug_str");
2044 }
2045 }
20192046 try self.emitNameSection(file, arena);
20202047 }
20212048}
20222049
2050fn emitDebugSection(file: fs.File, data: []const u8, name: []const u8) !void {
2051 const header_offset = try reserveCustomSectionHeader(file);
2052 const writer = file.writer();
2053 try leb.writeULEB128(writer, @intCast(u32, name.len));
2054 try writer.writeAll(name);
2055
2056 try file.writevAll(&[_]std.os.iovec_const{.{
2057 .iov_base = data.ptr,
2058 .iov_len = data.len,
2059 }});
2060 const start = header_offset + 6 + name.len + getULEB128Size(@intCast(u32, name.len));
2061 log.debug("Emit debug section: '{s}' start=0x{x:0>8} end=0x{x:0>8}", .{ name, start, start + data.len });
2062
2063 try writeCustomSectionHeader(
2064 file,
2065 header_offset,
2066 @intCast(u32, (try file.getPos()) - header_offset - 6),
2067 );
2068}
2069
20232070fn emitNameSection(self: *Wasm, file: fs.File, arena: Allocator) !void {
20242071 const Name = struct {
20252072 index: u32,
src/link/Wasm/Atom.zig-9
......@@ -90,15 +90,6 @@ pub fn getFirst(self: *Atom) *Atom {
9090 return tmp;
9191}
9292
93/// Returns the atom for the given `symbol_index`.
94/// This can be either the `Atom` itself, or one of its locals.
95pub fn symbolAtom(self: *Atom, symbol_index: u32) *Atom {
96 if (self.sym_index == symbol_index) return self;
97 return for (self.locals.items) |*local_atom| {
98 if (local_atom.sym_index == symbol_index) break local_atom;
99 } else unreachable; // Used a symbol index not present in this atom or its children.
100}
101
10293/// Returns the location of the symbol that represents this `Atom`
10394pub fn symbolLoc(self: Atom) Wasm.SymbolLoc {
10495 return .{ .file = self.file, .index = self.sym_index };