authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-01-19 19:02:48+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-01-19 22:39:49+01:00
loge726868b020f2dc8b731b7b2ddee1616478cfac8
tree112cc7c21d4585be9f386c8816ab52b544838290
parent7d3aa58e16dc90852316324efddae5ffc11d0606

macho: reuse existing names from the string table


1 files changed, 21 insertions(+), 1 deletions(-)

src/link/MachO.zig+21-1
......@@ -120,6 +120,7 @@ stub_helper_stubs_start_off: ?u64 = null,
120120
121121/// Table of symbol names aka the string table.
122122string_table: std.ArrayListUnmanaged(u8) = .{},
123string_table_directory: std.StringHashMapUnmanaged(u32) = .{},
123124
124125/// Table of trampolines to the actual symbols in __text section.
125126offset_table: std.ArrayListUnmanaged(u64) = .{},
......@@ -1023,6 +1024,13 @@ pub fn deinit(self: *MachO) void {
10231024 self.text_block_free_list.deinit(self.base.allocator);
10241025 self.offset_table.deinit(self.base.allocator);
10251026 self.offset_table_free_list.deinit(self.base.allocator);
1027 {
1028 var it = self.string_table_directory.iterator();
1029 while (it.next()) |entry| {
1030 self.base.allocator.free(entry.key);
1031 }
1032 }
1033 self.string_table_directory.deinit(self.base.allocator);
10261034 self.string_table.deinit(self.base.allocator);
10271035 self.global_symbols.deinit(self.base.allocator);
10281036 self.global_symbol_free_list.deinit(self.base.allocator);
......@@ -2235,14 +2243,26 @@ pub fn makeStaticString(comptime bytes: []const u8) [16]u8 {
22352243}
22362244
22372245fn makeString(self: *MachO, bytes: []const u8) !u32 {
2246 if (self.string_table_directory.get(bytes)) |offset| {
2247 log.debug("reusing '{s}' from string table at offset 0x{x}", .{ bytes, offset });
2248 return offset;
2249 }
2250
22382251 try self.string_table.ensureCapacity(self.base.allocator, self.string_table.items.len + bytes.len + 1);
22392252 const offset = @intCast(u32, self.string_table.items.len);
2240 log.debug("writing '{s}' into the string table at offset 0x{x}", .{ bytes, offset });
2253 log.debug("writing new string '{s}' into string table at offset 0x{x}", .{ bytes, offset });
22412254 self.string_table.appendSliceAssumeCapacity(bytes);
22422255 self.string_table.appendAssumeCapacity(0);
2256 try self.string_table_directory.putNoClobber(
2257 self.base.allocator,
2258 try self.base.allocator.dupe(u8, bytes),
2259 offset,
2260 );
2261
22432262 self.string_table_dirty = true;
22442263 if (self.d_sym) |*ds|
22452264 ds.string_table_dirty = true;
2265
22462266 return offset;
22472267}
22482268