authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-08-08 07:45:05+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-08-13 13:30:23+02:00
log27e1e636710ca72fb4508e2ea731dcb39cedbc95
tree52a9779f0bcce41a5138ce796c8d3eed52975c68
parenteadc0c01ccb86db442c35bc7ab51f62b570b27b5

elf: introduce OffsetTable in ZigObject for funcs only


2 files changed, 63 insertions(+), 1 deletions(-)

src/link/Elf.zig+5-1
...@@ -5657,10 +5657,14 @@ fn fmtDumpState(...@@ -5657,10 +5657,14 @@ fn fmtDumpState(
56575657
5658 if (self.zigObjectPtr()) |zig_object| {5658 if (self.zigObjectPtr()) |zig_object| {
5659 try writer.print("zig_object({d}) : {s}\n", .{ zig_object.index, zig_object.path });5659 try writer.print("zig_object({d}) : {s}\n", .{ zig_object.index, zig_object.path });
5660 try writer.print("{}{}\n", .{5660 try writer.print("{}{}", .{
5661 zig_object.fmtAtoms(self),5661 zig_object.fmtAtoms(self),
5662 zig_object.fmtSymtab(self),5662 zig_object.fmtSymtab(self),
5663 });5663 });
5664 if (zig_object.func_offset_table) |ot| {
5665 try writer.print("{}", .{ot.fmt(zig_object, self)});
5666 }
5667 try writer.writeByte('\n');
5664 }5668 }
56655669
5666 for (self.objects.items) |index| {5670 for (self.objects.items) |index| {
src/link/Elf/ZigObject.zig+58
...@@ -55,6 +55,8 @@ debug_str_section_zig_size: u64 = 0,...@@ -55,6 +55,8 @@ debug_str_section_zig_size: u64 = 0,
55debug_aranges_section_zig_size: u64 = 0,55debug_aranges_section_zig_size: u64 = 0,
56debug_line_section_zig_size: u64 = 0,56debug_line_section_zig_size: u64 = 0,
5757
58func_offset_table: ?OffsetTable = null,
59
58pub const global_symbol_bit: u32 = 0x80000000;60pub const global_symbol_bit: u32 = 0x80000000;
59pub const symbol_mask: u32 = 0x7fffffff;61pub const symbol_mask: u32 = 0x7fffffff;
60pub const SHN_ATOM: u16 = 0x100;62pub const SHN_ATOM: u16 = 0x100;
...@@ -128,6 +130,10 @@ pub fn deinit(self: *ZigObject, allocator: Allocator) void {...@@ -128,6 +130,10 @@ pub fn deinit(self: *ZigObject, allocator: Allocator) void {
128 if (self.dwarf) |*dw| {130 if (self.dwarf) |*dw| {
129 dw.deinit();131 dw.deinit();
130 }132 }
133
134 if (self.func_offset_table) |*ot| {
135 ot.deinit(allocator);
136 }
131}137}
132138
133pub fn flushModule(self: *ZigObject, elf_file: *Elf, tid: Zcu.PerThread.Id) !void {139pub fn flushModule(self: *ZigObject, elf_file: *Elf, tid: Zcu.PerThread.Id) !void {
...@@ -1687,6 +1693,58 @@ const UavTable = std.AutoHashMapUnmanaged(InternPool.Index, AvMetadata);...@@ -1687,6 +1693,58 @@ const UavTable = std.AutoHashMapUnmanaged(InternPool.Index, AvMetadata);
1687const LazySymbolTable = std.AutoArrayHashMapUnmanaged(InternPool.Index, LazySymbolMetadata);1693const LazySymbolTable = std.AutoArrayHashMapUnmanaged(InternPool.Index, LazySymbolMetadata);
1688const TlsTable = std.AutoArrayHashMapUnmanaged(Atom.Index, TlsVariable);1694const TlsTable = std.AutoArrayHashMapUnmanaged(Atom.Index, TlsVariable);
16891695
1696pub const OffsetTable = struct {
1697 atom_index: Atom.Index,
1698 entries: std.ArrayListUnmanaged(Symbol.Index) = .{},
1699
1700 pub fn deinit(ot: *OffsetTable, allocator: Allocator) void {
1701 ot.entries.deinit(allocator);
1702 }
1703
1704 pub fn size(ot: OffsetTable, elf_file: *Elf) usize {
1705 return ot.entries.items.len * switch (elf_file.ptr_width) {
1706 .p32 => @as(usize, 4),
1707 .p64 => 8,
1708 };
1709 }
1710
1711 const OffsetTableFormatContext = struct { OffsetTable, *ZigObject, *Elf };
1712
1713 pub fn format(
1714 ot: OffsetTable,
1715 comptime unused_fmt_string: []const u8,
1716 options: std.fmt.FormatOptions,
1717 writer: anytype,
1718 ) !void {
1719 _ = ot;
1720 _ = unused_fmt_string;
1721 _ = options;
1722 _ = writer;
1723 @compileError("do not format OffsetTable directly");
1724 }
1725
1726 pub fn fmt(ot: OffsetTable, zo: *ZigObject, elf_file: *Elf) std.fmt.Formatter(format2) {
1727 return .{ .data = .{ ot, zo, elf_file } };
1728 }
1729
1730 fn format2(
1731 ctx: OffsetTableFormatContext,
1732 comptime unused_fmt_string: []const u8,
1733 options: std.fmt.FormatOptions,
1734 writer: anytype,
1735 ) !void {
1736 _ = options;
1737 _ = unused_fmt_string;
1738 const ot, const zo, const ef = ctx;
1739 const atom_ptr = zo.atom(ot.atom_index).?;
1740 try writer.print("@{x} : size({x})\n", .{ atom_ptr.address(ef), ot.size(ef) });
1741 for (ot.entries.items) |sym_index| {
1742 const sym = zo.symbol(sym_index);
1743 try writer.print(" %{d} : {s} : @{x}\n", .{ sym_index, sym.name(ef), sym.value });
1744 }
1745 }
1746};
1747
1690const assert = std.debug.assert;1748const assert = std.debug.assert;
1691const builtin = @import("builtin");1749const builtin = @import("builtin");
1692const codegen = @import("../../codegen.zig");1750const codegen = @import("../../codegen.zig");