Commit d98fc53b8f

Jakub Konka <kubkon@jakubkonka.com>
2023-02-01 11:49:07
link: use strtab.StringTable in Dwarf
1 parent b3277c8
Changed files (3)
src/link/MachO/DebugSymbols.zig
@@ -82,11 +82,11 @@ pub fn populateMissingMetadata(self: *DebugSymbols) !void {
     }
 
     if (self.debug_str_section_index == null) {
-        assert(self.dwarf.strtab.items.len == 0);
-        try self.dwarf.strtab.append(self.allocator, 0);
+        assert(self.dwarf.strtab.buffer.items.len == 0);
+        try self.dwarf.strtab.buffer.append(self.allocator, 0);
         self.debug_str_section_index = try self.allocateSection(
             "__debug_str",
-            @intCast(u32, self.dwarf.strtab.items.len),
+            @intCast(u32, self.dwarf.strtab.buffer.items.len),
             0,
         );
         self.debug_string_table_dirty = true;
@@ -291,10 +291,10 @@ pub fn flushModule(self: *DebugSymbols, macho_file: *MachO) !void {
 
     {
         const sect_index = self.debug_str_section_index.?;
-        if (self.debug_string_table_dirty or self.dwarf.strtab.items.len != self.getSection(sect_index).size) {
-            const needed_size = @intCast(u32, self.dwarf.strtab.items.len);
+        if (self.debug_string_table_dirty or self.dwarf.strtab.buffer.items.len != self.getSection(sect_index).size) {
+            const needed_size = @intCast(u32, self.dwarf.strtab.buffer.items.len);
             try self.growSection(sect_index, needed_size, false);
-            try self.file.pwriteAll(self.dwarf.strtab.items, self.getSection(sect_index).offset);
+            try self.file.pwriteAll(self.dwarf.strtab.buffer.items, self.getSection(sect_index).offset);
             self.debug_string_table_dirty = false;
         }
     }
src/link/Dwarf.zig
@@ -18,8 +18,9 @@ const LinkBlock = File.LinkBlock;
 const LinkFn = File.LinkFn;
 const LinkerLoad = @import("../codegen.zig").LinkerLoad;
 const Module = @import("../Module.zig");
-const Value = @import("../value.zig").Value;
+const StringTable = @import("strtab.zig").StringTable;
 const Type = @import("../type.zig").Type;
+const Value = @import("../value.zig").Value;
 
 allocator: Allocator,
 bin_file: *File,
@@ -42,7 +43,7 @@ abbrev_table_offset: ?u64 = null,
 
 /// TODO replace with InternPool
 /// Table of debug symbol names.
-strtab: std.ArrayListUnmanaged(u8) = .{},
+strtab: StringTable(.strtab) = .{},
 
 /// Quick lookup array of all defined source files referenced by at least one Decl.
 /// They will end up in the DWARF debug_line header as two lists:
@@ -1770,11 +1771,11 @@ pub fn writeDbgInfoHeader(self: *Dwarf, module: *Module, low_pc: u64, high_pc: u
         },
     }
     // Write the form for the compile unit, which must match the abbrev table above.
-    const name_strp = try self.makeString(module.root_pkg.root_src_path);
+    const name_strp = try self.strtab.insert(self.allocator, module.root_pkg.root_src_path);
     var compile_unit_dir_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
     const compile_unit_dir = resolveCompilationDir(module, &compile_unit_dir_buffer);
-    const comp_dir_strp = try self.makeString(compile_unit_dir);
-    const producer_strp = try self.makeString(link.producer_string);
+    const comp_dir_strp = try self.strtab.insert(self.allocator, compile_unit_dir);
+    const producer_strp = try self.strtab.insert(self.allocator, link.producer_string);
 
     di_buf.appendAssumeCapacity(@enumToInt(AbbrevKind.compile_unit));
     if (self.bin_file.tag == .macho) {
@@ -2435,15 +2436,6 @@ fn getRelocDbgInfoSubprogramHighPC(self: Dwarf) u32 {
     return dbg_info_low_pc_reloc_index + self.ptrWidthBytes();
 }
 
-/// TODO Improve this to use a table.
-fn makeString(self: *Dwarf, bytes: []const u8) !u32 {
-    try self.strtab.ensureUnusedCapacity(self.allocator, bytes.len + 1);
-    const result = self.strtab.items.len;
-    self.strtab.appendSliceAssumeCapacity(bytes);
-    self.strtab.appendAssumeCapacity(0);
-    return @intCast(u32, result);
-}
-
 fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) {
     return actual_size +| (actual_size / ideal_factor);
 }
src/link/Elf.zig
@@ -688,8 +688,8 @@ pub fn populateMissingMetadata(self: *Elf) !void {
     // if (self.dwarf) |*dw| {
     //     if (self.debug_str_section_index == null) {
     //         self.debug_str_section_index = @intCast(u16, self.sections.slice().len);
-    //         assert(dw.strtab.items.len == 0);
-    //         try dw.strtab.append(gpa, 0);
+    //         assert(dw.strtab.buffer.items.len == 0);
+    //         try dw.strtab.buffer.append(gpa, 0);
     //         try self.sections.append(gpa, .{
     //             .shdr = .{
     //                 .sh_name = try self.shstrtab.insert(gpa, ".debug_str"),
@@ -1164,10 +1164,10 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
 
     // if (self.dwarf) |dwarf| {
     //     const shdr_index = self.debug_str_section_index.?;
-    //     if (self.debug_strtab_dirty or dwarf.strtab.items.len != self.sections.items(.shdr)[shdr_index].sh_size) {
-    //         try self.growNonAllocSection(shdr_index, dwarf.strtab.items.len, 1, false);
+    //     if (self.debug_strtab_dirty or dwarf.strtab.buffer.items.len != self.sections.items(.shdr)[shdr_index].sh_size) {
+    //         try self.growNonAllocSection(shdr_index, dwarf.strtab.buffer.items.len, 1, false);
     //         const debug_strtab_sect = self.sections.items(.shdr)[shdr_index];
-    //         try self.base.file.?.pwriteAll(dwarf.strtab.items, debug_strtab_sect.sh_offset);
+    //         try self.base.file.?.pwriteAll(dwarf.strtab.buffer.items, debug_strtab_sect.sh_offset);
     //         self.debug_strtab_dirty = false;
     //     }
     // }