authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-02-01 11:49:07+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-02-01 11:49:07+01:00
logd98fc53b8fbe479f828114b0276d5290146cc2a3
tree446b6e4a430ee44d10fca677af4add27a804614a
parentb3277c893691c462ec2e82577a78e7baafb42bf6

link: use strtab.StringTable in Dwarf


3 files changed, 17 insertions(+), 25 deletions(-)

src/link/Dwarf.zig+6-14
...@@ -18,8 +18,9 @@ const LinkBlock = File.LinkBlock;...@@ -18,8 +18,9 @@ const LinkBlock = File.LinkBlock;
18const LinkFn = File.LinkFn;18const LinkFn = File.LinkFn;
19const LinkerLoad = @import("../codegen.zig").LinkerLoad;19const LinkerLoad = @import("../codegen.zig").LinkerLoad;
20const Module = @import("../Module.zig");20const Module = @import("../Module.zig");
21const Value = @import("../value.zig").Value;21const StringTable = @import("strtab.zig").StringTable;
22const Type = @import("../type.zig").Type;22const Type = @import("../type.zig").Type;
23const Value = @import("../value.zig").Value;
2324
24allocator: Allocator,25allocator: Allocator,
25bin_file: *File,26bin_file: *File,
...@@ -42,7 +43,7 @@ abbrev_table_offset: ?u64 = null,...@@ -42,7 +43,7 @@ abbrev_table_offset: ?u64 = null,
4243
43/// TODO replace with InternPool44/// TODO replace with InternPool
44/// Table of debug symbol names.45/// Table of debug symbol names.
45strtab: std.ArrayListUnmanaged(u8) = .{},46strtab: StringTable(.strtab) = .{},
4647
47/// Quick lookup array of all defined source files referenced by at least one Decl.48/// Quick lookup array of all defined source files referenced by at least one Decl.
48/// They will end up in the DWARF debug_line header as two lists:49/// 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...@@ -1770,11 +1771,11 @@ pub fn writeDbgInfoHeader(self: *Dwarf, module: *Module, low_pc: u64, high_pc: u
1770 },1771 },
1771 }1772 }
1772 // Write the form for the compile unit, which must match the abbrev table above.1773 // Write the form for the compile unit, which must match the abbrev table above.
1773 const name_strp = try self.makeString(module.root_pkg.root_src_path);1774 const name_strp = try self.strtab.insert(self.allocator, module.root_pkg.root_src_path);
1774 var compile_unit_dir_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;1775 var compile_unit_dir_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
1775 const compile_unit_dir = resolveCompilationDir(module, &compile_unit_dir_buffer);1776 const compile_unit_dir = resolveCompilationDir(module, &compile_unit_dir_buffer);
1776 const comp_dir_strp = try self.makeString(compile_unit_dir);1777 const comp_dir_strp = try self.strtab.insert(self.allocator, compile_unit_dir);
1777 const producer_strp = try self.makeString(link.producer_string);1778 const producer_strp = try self.strtab.insert(self.allocator, link.producer_string);
17781779
1779 di_buf.appendAssumeCapacity(@enumToInt(AbbrevKind.compile_unit));1780 di_buf.appendAssumeCapacity(@enumToInt(AbbrevKind.compile_unit));
1780 if (self.bin_file.tag == .macho) {1781 if (self.bin_file.tag == .macho) {
...@@ -2435,15 +2436,6 @@ fn getRelocDbgInfoSubprogramHighPC(self: Dwarf) u32 {...@@ -2435,15 +2436,6 @@ fn getRelocDbgInfoSubprogramHighPC(self: Dwarf) u32 {
2435 return dbg_info_low_pc_reloc_index + self.ptrWidthBytes();2436 return dbg_info_low_pc_reloc_index + self.ptrWidthBytes();
2436}2437}
24372438
2438/// TODO Improve this to use a table.
2439fn makeString(self: *Dwarf, bytes: []const u8) !u32 {
2440 try self.strtab.ensureUnusedCapacity(self.allocator, bytes.len + 1);
2441 const result = self.strtab.items.len;
2442 self.strtab.appendSliceAssumeCapacity(bytes);
2443 self.strtab.appendAssumeCapacity(0);
2444 return @intCast(u32, result);
2445}
2446
2447fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) {2439fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) {
2448 return actual_size +| (actual_size / ideal_factor);2440 return actual_size +| (actual_size / ideal_factor);
2449}2441}
src/link/Elf.zig+5-5
...@@ -688,8 +688,8 @@ pub fn populateMissingMetadata(self: *Elf) !void {...@@ -688,8 +688,8 @@ pub fn populateMissingMetadata(self: *Elf) !void {
688 // if (self.dwarf) |*dw| {688 // if (self.dwarf) |*dw| {
689 // if (self.debug_str_section_index == null) {689 // if (self.debug_str_section_index == null) {
690 // self.debug_str_section_index = @intCast(u16, self.sections.slice().len);690 // self.debug_str_section_index = @intCast(u16, self.sections.slice().len);
691 // assert(dw.strtab.items.len == 0);691 // assert(dw.strtab.buffer.items.len == 0);
692 // try dw.strtab.append(gpa, 0);692 // try dw.strtab.buffer.append(gpa, 0);
693 // try self.sections.append(gpa, .{693 // try self.sections.append(gpa, .{
694 // .shdr = .{694 // .shdr = .{
695 // .sh_name = try self.shstrtab.insert(gpa, ".debug_str"),695 // .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...@@ -1164,10 +1164,10 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
11641164
1165 // if (self.dwarf) |dwarf| {1165 // if (self.dwarf) |dwarf| {
1166 // const shdr_index = self.debug_str_section_index.?;1166 // const shdr_index = self.debug_str_section_index.?;
1167 // if (self.debug_strtab_dirty or dwarf.strtab.items.len != self.sections.items(.shdr)[shdr_index].sh_size) {1167 // if (self.debug_strtab_dirty or dwarf.strtab.buffer.items.len != self.sections.items(.shdr)[shdr_index].sh_size) {
1168 // try self.growNonAllocSection(shdr_index, dwarf.strtab.items.len, 1, false);1168 // try self.growNonAllocSection(shdr_index, dwarf.strtab.buffer.items.len, 1, false);
1169 // const debug_strtab_sect = self.sections.items(.shdr)[shdr_index];1169 // const debug_strtab_sect = self.sections.items(.shdr)[shdr_index];
1170 // try self.base.file.?.pwriteAll(dwarf.strtab.items, debug_strtab_sect.sh_offset);1170 // try self.base.file.?.pwriteAll(dwarf.strtab.buffer.items, debug_strtab_sect.sh_offset);
1171 // self.debug_strtab_dirty = false;1171 // self.debug_strtab_dirty = false;
1172 // }1172 // }
1173 // }1173 // }
src/link/MachO/DebugSymbols.zig+6-6
...@@ -82,11 +82,11 @@ pub fn populateMissingMetadata(self: *DebugSymbols) !void {...@@ -82,11 +82,11 @@ pub fn populateMissingMetadata(self: *DebugSymbols) !void {
82 }82 }
8383
84 if (self.debug_str_section_index == null) {84 if (self.debug_str_section_index == null) {
85 assert(self.dwarf.strtab.items.len == 0);85 assert(self.dwarf.strtab.buffer.items.len == 0);
86 try self.dwarf.strtab.append(self.allocator, 0);86 try self.dwarf.strtab.buffer.append(self.allocator, 0);
87 self.debug_str_section_index = try self.allocateSection(87 self.debug_str_section_index = try self.allocateSection(
88 "__debug_str",88 "__debug_str",
89 @intCast(u32, self.dwarf.strtab.items.len),89 @intCast(u32, self.dwarf.strtab.buffer.items.len),
90 0,90 0,
91 );91 );
92 self.debug_string_table_dirty = true;92 self.debug_string_table_dirty = true;
...@@ -291,10 +291,10 @@ pub fn flushModule(self: *DebugSymbols, macho_file: *MachO) !void {...@@ -291,10 +291,10 @@ pub fn flushModule(self: *DebugSymbols, macho_file: *MachO) !void {
291291
292 {292 {
293 const sect_index = self.debug_str_section_index.?;293 const sect_index = self.debug_str_section_index.?;
294 if (self.debug_string_table_dirty or self.dwarf.strtab.items.len != self.getSection(sect_index).size) {294 if (self.debug_string_table_dirty or self.dwarf.strtab.buffer.items.len != self.getSection(sect_index).size) {
295 const needed_size = @intCast(u32, self.dwarf.strtab.items.len);295 const needed_size = @intCast(u32, self.dwarf.strtab.buffer.items.len);
296 try self.growSection(sect_index, needed_size, false);296 try self.growSection(sect_index, needed_size, false);
297 try self.file.pwriteAll(self.dwarf.strtab.items, self.getSection(sect_index).offset);297 try self.file.pwriteAll(self.dwarf.strtab.buffer.items, self.getSection(sect_index).offset);
298 self.debug_string_table_dirty = false;298 self.debug_string_table_dirty = false;
299 }299 }
300 }300 }