authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-08-12 23:23:39+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-08-13 13:30:24+02:00
log78b1c736026443c937145ba85e1de52c11c9a44e
tree78dc76d86639d1f0604667c787ac1b78b2b98b63
parente1ce9a7065e92e3e9b61229c7903f82c3684f489

elf: rename OffsetTable to JumpTable


4 files changed, 91 insertions(+), 91 deletions(-)

src/link/Elf.zig+2-2
......@@ -5599,8 +5599,8 @@ fn fmtDumpState(
55995599 zig_object.fmtAtoms(self),
56005600 zig_object.fmtSymtab(self),
56015601 });
5602 if (zig_object.offset_table) |ot| {
5603 try writer.print("{}", .{ot.fmt(zig_object, self)});
5602 if (zig_object.jump_table) |jt| {
5603 try writer.print("{}", .{jt.fmt(zig_object, self)});
56045604 }
56055605 try writer.writeByte('\n');
56065606 }
src/link/Elf/Atom.zig+12-12
......@@ -750,8 +750,8 @@ pub fn resolveRelocsAlloc(self: Atom, elf_file: *Elf, code: []u8) RelocError!voi
750750 const S = target.address(.{}, elf_file);
751751 // Address of the global offset table.
752752 const GOT = elf_file.gotAddress();
753 // Address of the offset table entry if any.
754 const ZIG_GOT = target.zigOffsetTableAddress(elf_file);
753 // Address of the zig jump table entry if any.
754 const ZJT = target.zigJumpTableAddress(elf_file);
755755 // Relative offset to the start of the global offset table.
756756 const G = target.gotAddress(elf_file) - GOT;
757757 // // Address of the thread pointer.
......@@ -759,19 +759,19 @@ pub fn resolveRelocsAlloc(self: Atom, elf_file: *Elf, code: []u8) RelocError!voi
759759 // Address of the dynamic thread pointer.
760760 const DTP = elf_file.dtpAddress();
761761
762 relocs_log.debug(" {s}: {x}: [{x} => {x}] G({x}) ZG({x}) ({s})", .{
762 relocs_log.debug(" {s}: {x}: [{x} => {x}] GOT({x}) ZJT({x}) ({s})", .{
763763 relocation.fmtRelocType(rel.r_type(), cpu_arch),
764764 r_offset,
765765 P,
766766 S + A,
767767 G + GOT + A,
768 ZIG_GOT + A,
768 ZJT + A,
769769 target.name(elf_file),
770770 });
771771
772772 try stream.seekTo(r_offset);
773773
774 const args = ResolveArgs{ P, A, S, GOT, G, TP, DTP, ZIG_GOT };
774 const args = ResolveArgs{ P, A, S, GOT, G, TP, DTP, ZJT };
775775
776776 switch (cpu_arch) {
777777 .x86_64 => x86_64.resolveRelocAlloc(self, elf_file, rel, target, args, &it, code, &stream) catch |err| switch (err) {
......@@ -1200,7 +1200,7 @@ const x86_64 = struct {
12001200
12011201 const cwriter = stream.writer();
12021202
1203 const P, const A, const S, const GOT, const G, const TP, const DTP, const ZIG_GOT = args;
1203 const P, const A, const S, const GOT, const G, const TP, const DTP, const ZJT = args;
12041204
12051205 switch (r_type) {
12061206 .NONE => unreachable,
......@@ -1218,7 +1218,7 @@ const x86_64 = struct {
12181218 .PLT32 => try cwriter.writeInt(i32, @as(i32, @intCast(S + A - P)), .little),
12191219
12201220 .PC32 => {
1221 const S_ = if (target.flags.zig_offset_table) ZIG_GOT else S;
1221 const S_ = if (target.flags.zig_jump_table) ZJT else S;
12221222 try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .little);
12231223 },
12241224
......@@ -1245,7 +1245,7 @@ const x86_64 = struct {
12451245 },
12461246
12471247 .@"32" => {
1248 const S_ = if (target.flags.zig_offset_table) ZIG_GOT else S;
1248 const S_ = if (target.flags.zig_jump_table) ZJT else S;
12491249 try cwriter.writeInt(u32, @as(u32, @truncate(@as(u64, @intCast(S_ + A)))), .little);
12501250 },
12511251 .@"32S" => try cwriter.writeInt(i32, @as(i32, @truncate(S + A)), .little),
......@@ -1729,9 +1729,9 @@ const aarch64 = struct {
17291729 const code = code_buffer[r_offset..][0..4];
17301730 const file_ptr = atom.file(elf_file).?;
17311731
1732 const P, const A, const S, const GOT, const G, const TP, const DTP, const ZIG_GOT = args;
1732 const P, const A, const S, const GOT, const G, const TP, const DTP, const ZJT = args;
17331733 _ = DTP;
1734 _ = ZIG_GOT;
1734 _ = ZJT;
17351735
17361736 switch (r_type) {
17371737 .NONE => unreachable,
......@@ -2013,10 +2013,10 @@ const riscv = struct {
20132013 const r_offset = std.math.cast(usize, rel.r_offset) orelse return error.Overflow;
20142014 const cwriter = stream.writer();
20152015
2016 const P, const A, const S, const GOT, const G, const TP, const DTP, const ZIG_GOT = args;
2016 const P, const A, const S, const GOT, const G, const TP, const DTP, const ZJT = args;
20172017 _ = TP;
20182018 _ = DTP;
2019 _ = ZIG_GOT;
2019 _ = ZJT;
20202020
20212021 switch (r_type) {
20222022 .NONE => unreachable,
src/link/Elf/Symbol.zig+9-9
......@@ -217,12 +217,12 @@ pub fn tlsDescAddress(symbol: Symbol, elf_file: *Elf) i64 {
217217 return entry.address(elf_file);
218218}
219219
220pub fn zigOffsetTableAddress(symbol: Symbol, elf_file: *Elf) i64 {
221 if (!symbol.flags.zig_offset_table) return 0;
220pub fn zigJumpTableAddress(symbol: Symbol, elf_file: *Elf) i64 {
221 if (!symbol.flags.zig_jump_table) return 0;
222222 const zo = elf_file.zigObjectPtr().?;
223 const offset_table = zo.offsetTablePtr().?;
224 const ot_index = symbol.extra(elf_file).zig_offset_table;
225 return offset_table.entryAddress(ot_index, zo, elf_file);
223 const jump_table = zo.jumpTablePtr().?;
224 const jt_index = symbol.extra(elf_file).zig_jump_table;
225 return jump_table.entryAddress(jt_index, zo, elf_file);
226226}
227227
228228pub fn dsoAlignment(symbol: Symbol, elf_file: *Elf) !u64 {
......@@ -248,7 +248,7 @@ const AddExtraOpts = struct {
248248 tlsgd: ?u32 = null,
249249 gottp: ?u32 = null,
250250 tlsdesc: ?u32 = null,
251 zig_offset_table: ?u32 = null,
251 zig_jump_table: ?u32 = null,
252252};
253253
254254pub fn addExtra(symbol: *Symbol, opts: AddExtraOpts, elf_file: *Elf) void {
......@@ -453,8 +453,8 @@ pub const Flags = packed struct {
453453 /// Whether the symbol is a merge subsection.
454454 merge_subsection: bool = false,
455455
456 /// Whether the symbol has __zig_offset_table indirection.
457 zig_offset_table: bool = false,
456 /// Whether the symbol has __zig_jump_table indirection.
457 zig_jump_table: bool = false,
458458};
459459
460460pub const Extra = struct {
......@@ -468,7 +468,7 @@ pub const Extra = struct {
468468 gottp: u32 = 0,
469469 tlsdesc: u32 = 0,
470470 merge_section: u32 = 0,
471 zig_offset_table: u32 = 0,
471 zig_jump_table: u32 = 0,
472472};
473473
474474pub const Index = u32;
src/link/Elf/ZigObject.zig+68-68
......@@ -55,10 +55,10 @@ debug_str_section_zig_size: u64 = 0,
5555debug_aranges_section_zig_size: u64 = 0,
5656debug_line_section_zig_size: u64 = 0,
5757
58/// Function offset table containing pointers to Zig generated functions.
58/// Function jump table containing trampolines to Zcu functions.
5959/// The table is used for Zig's incremental compilation and is embedded with
6060/// the machine code section.
61offset_table: ?OffsetTable = null,
61jump_table: ?JumpTable = null,
6262
6363pub const global_symbol_bit: u32 = 0x80000000;
6464pub const symbol_mask: u32 = 0x7fffffff;
......@@ -128,8 +128,8 @@ pub fn deinit(self: *ZigObject, allocator: Allocator) void {
128128 dw.deinit();
129129 }
130130
131 if (self.offset_table) |*ot| {
132 ot.deinit(allocator);
131 if (self.jump_table) |*jt| {
132 jt.deinit(allocator);
133133 }
134134}
135135
......@@ -917,8 +917,8 @@ fn updateNavCode(
917917
918918 if (stt_bits == elf.STT_FUNC) {
919919 const extra = sym.extra(elf_file);
920 const offset_table = self.offsetTablePtr().?;
921 offset_table.entries.items(.dirty)[extra.zig_offset_table] = true;
920 const jump_table = self.jumpTablePtr().?;
921 jump_table.entries.items(.dirty)[extra.zig_jump_table] = true;
922922 }
923923 }
924924 } else if (code.len < old_size) {
......@@ -1036,7 +1036,7 @@ pub fn updateFunc(
10361036 const ip = &zcu.intern_pool;
10371037 const gpa = elf_file.base.comp.gpa;
10381038 const func = zcu.funcInfo(func_index);
1039 const offset_table = self.offsetTablePtr() orelse try self.initOffsetTable(gpa, elf_file);
1039 const jump_table = self.jumpTablePtr() orelse try self.initJumpTable(gpa, elf_file);
10401040
10411041 log.debug("updateFunc {}({d})", .{ ip.getNav(func.owner_nav).fqn.fmt(ip), func.owner_nav });
10421042
......@@ -1045,16 +1045,16 @@ pub fn updateFunc(
10451045
10461046 {
10471047 const sym = self.symbol(sym_index);
1048 if (!sym.flags.zig_offset_table) {
1049 const index = try offset_table.addSymbol(gpa, sym_index);
1050 sym.flags.zig_offset_table = true;
1051 sym.addExtra(.{ .zig_offset_table = index }, elf_file);
1052 try offset_table.updateSize(self, elf_file);
1053 const old_vaddr = offset_table.address(self, elf_file);
1054 try self.symbol(offset_table.sym_index).atom(elf_file).?.allocate(elf_file);
1055 const new_vaddr = offset_table.address(self, elf_file);
1048 if (!sym.flags.zig_jump_table) {
1049 const index = try jump_table.addSymbol(gpa, sym_index);
1050 sym.flags.zig_jump_table = true;
1051 sym.addExtra(.{ .zig_jump_table = index }, elf_file);
1052 try jump_table.updateSize(self, elf_file);
1053 const old_vaddr = jump_table.address(self, elf_file);
1054 try self.symbol(jump_table.sym_index).atom(elf_file).?.allocate(elf_file);
1055 const new_vaddr = jump_table.address(self, elf_file);
10561056 if (old_vaddr != new_vaddr) {
1057 offset_table.dirty = true;
1057 jump_table.dirty = true;
10581058 }
10591059 }
10601060 }
......@@ -1100,21 +1100,21 @@ pub fn updateFunc(
11001100
11011101 // Exports will be updated by `Zcu.processExports` after the update.
11021102
1103 if (offset_table.dirty) {
1103 if (jump_table.dirty) {
11041104 // TODO write in bulk
1105 for (offset_table.entries.items(.dirty), 0..) |*dirty, i| {
1106 try offset_table.writeEntry(@intCast(i), self, elf_file);
1105 for (jump_table.entries.items(.dirty), 0..) |*dirty, i| {
1106 try jump_table.writeEntry(@intCast(i), self, elf_file);
11071107 dirty.* = false;
11081108 }
11091109 } else {
11101110 const sym = self.symbol(sym_index);
1111 const ot_index = sym.extra(elf_file).zig_offset_table;
1112 var ot_entry = offset_table.entries.get(ot_index);
1113 if (ot_entry.dirty) {
1114 try offset_table.writeEntry(ot_index, self, elf_file);
1115 ot_entry.dirty = false;
1111 const jt_index = sym.extra(elf_file).zig_jump_table;
1112 var jt_entry = jump_table.entries.get(jt_index);
1113 if (jt_entry.dirty) {
1114 try jump_table.writeEntry(jt_index, self, elf_file);
1115 jt_entry.dirty = false;
11161116 }
1117 offset_table.entries.set(ot_index, ot_entry);
1117 jump_table.entries.set(jt_index, jt_entry);
11181118 }
11191119}
11201120
......@@ -1470,22 +1470,22 @@ pub fn getGlobalSymbol(self: *ZigObject, elf_file: *Elf, name: []const u8, lib_n
14701470 return lookup_gop.value_ptr.*;
14711471}
14721472
1473pub fn offsetTablePtr(self: *ZigObject) ?*OffsetTable {
1474 return if (self.offset_table) |*ot| ot else null;
1473pub fn jumpTablePtr(self: *ZigObject) ?*JumpTable {
1474 return if (self.jump_table) |*jt| jt else null;
14751475}
14761476
1477fn initOffsetTable(self: *ZigObject, allocator: Allocator, elf_file: *Elf) error{OutOfMemory}!*OffsetTable {
1478 const name_off = try self.addString(allocator, "__zig_offset_table");
1477fn initJumpTable(self: *ZigObject, allocator: Allocator, elf_file: *Elf) error{OutOfMemory}!*JumpTable {
1478 const name_off = try self.addString(allocator, "__zig_jump_table");
14791479 const sym_index = try self.newSymbolWithAtom(allocator, name_off);
14801480 const sym = self.symbol(sym_index);
14811481 const esym = &self.symtab.items(.elf_sym)[sym.esym_index];
14821482 esym.st_info |= elf.STT_OBJECT;
14831483 const atom_ptr = sym.atom(elf_file).?;
14841484 atom_ptr.alive = true;
1485 atom_ptr.alignment = Atom.Alignment.fromNonzeroByteUnits(OffsetTable.alignment(elf_file.getTarget().cpu.arch));
1485 atom_ptr.alignment = Atom.Alignment.fromNonzeroByteUnits(JumpTable.alignment(elf_file.getTarget().cpu.arch));
14861486 atom_ptr.output_section_index = elf_file.zig_text_section_index.?;
1487 self.offset_table = OffsetTable{ .sym_index = sym_index };
1488 return &(self.offset_table.?);
1487 self.jump_table = JumpTable{ .sym_index = sym_index };
1488 return &(self.jump_table.?);
14891489}
14901490
14911491pub fn asFile(self: *ZigObject) File {
......@@ -1763,28 +1763,28 @@ const UavTable = std.AutoArrayHashMapUnmanaged(InternPool.Index, AvMetadata);
17631763const LazySymbolTable = std.AutoArrayHashMapUnmanaged(InternPool.Index, LazySymbolMetadata);
17641764const TlsTable = std.AutoArrayHashMapUnmanaged(Atom.Index, TlsVariable);
17651765
1766pub const OffsetTable = struct {
1766pub const JumpTable = struct {
17671767 sym_index: Symbol.Index,
17681768 entries: std.MultiArrayList(Entry) = .{},
17691769 dirty: bool = false,
17701770
1771 pub fn deinit(ot: *OffsetTable, allocator: Allocator) void {
1772 ot.entries.deinit(allocator);
1771 pub fn deinit(jt: *JumpTable, allocator: Allocator) void {
1772 jt.entries.deinit(allocator);
17731773 }
17741774
1775 pub fn addSymbol(ot: *OffsetTable, allocator: Allocator, sym_index: Symbol.Index) !Index {
1776 const index: Index = @intCast(try ot.entries.addOne(allocator));
1777 ot.entries.set(index, .{ .sym_index = sym_index });
1775 pub fn addSymbol(jt: *JumpTable, allocator: Allocator, sym_index: Symbol.Index) !Index {
1776 const index: Index = @intCast(try jt.entries.addOne(allocator));
1777 jt.entries.set(index, .{ .sym_index = sym_index });
17781778 return index;
17791779 }
17801780
1781 pub fn address(ot: OffsetTable, zo: *ZigObject, elf_file: *Elf) i64 {
1782 const sym = zo.symbol(ot.sym_index);
1781 pub fn address(jt: JumpTable, zo: *ZigObject, elf_file: *Elf) i64 {
1782 const sym = zo.symbol(jt.sym_index);
17831783 return sym.address(.{}, elf_file);
17841784 }
17851785
1786 pub fn size(ot: OffsetTable, zo: *ZigObject, elf_file: *Elf) u64 {
1787 const sym = zo.symbol(ot.sym_index);
1786 pub fn size(jt: JumpTable, zo: *ZigObject, elf_file: *Elf) u64 {
1787 const sym = zo.symbol(jt.sym_index);
17881788 return sym.atom(elf_file).?.size;
17891789 }
17901790
......@@ -1795,12 +1795,12 @@ pub const OffsetTable = struct {
17951795 };
17961796 }
17971797
1798 pub fn entryAddress(ot: OffsetTable, index: Index, zo: *ZigObject, elf_file: *Elf) i64 {
1799 return ot.address(zo, elf_file) + @as(i64, @intCast(index * entrySize(elf_file.getTarget().cpu.arch)));
1798 pub fn entryAddress(jt: JumpTable, index: Index, zo: *ZigObject, elf_file: *Elf) i64 {
1799 return jt.address(zo, elf_file) + @as(i64, @intCast(index * entrySize(elf_file.getTarget().cpu.arch)));
18001800 }
18011801
1802 pub fn entryOffset(ot: OffsetTable, index: Index, zo: *ZigObject, elf_file: *Elf) u64 {
1803 const sym = zo.symbol(ot.sym_index);
1802 pub fn entryOffset(jt: JumpTable, index: Index, zo: *ZigObject, elf_file: *Elf) u64 {
1803 const sym = zo.symbol(jt.sym_index);
18041804 const atom_ptr = sym.atom(elf_file).?;
18051805 const shdr = elf_file.shdrs.items[atom_ptr.output_section_index];
18061806 return shdr.sh_offset + @as(u64, @intCast(atom_ptr.value)) + index * entrySize(elf_file.getTarget().cpu.arch);
......@@ -1815,17 +1815,17 @@ pub const OffsetTable = struct {
18151815 return seq_len;
18161816 }
18171817
1818 pub fn targetAddress(ot: OffsetTable, index: Index, zo: *ZigObject, elf_file: *Elf) i64 {
1819 const sym_index = ot.entries.items(.sym_index)[index];
1818 pub fn targetAddress(jt: JumpTable, index: Index, zo: *ZigObject, elf_file: *Elf) i64 {
1819 const sym_index = jt.entries.items(.sym_index)[index];
18201820 return zo.symbol(sym_index).address(.{}, elf_file);
18211821 }
18221822
18231823 const max_jump_seq_len = 12;
18241824
1825 pub fn writeEntry(ot: OffsetTable, index: Index, zo: *ZigObject, elf_file: *Elf) !void {
1826 const fileoff = ot.entryOffset(index, zo, elf_file);
1827 const source_addr = ot.entryAddress(index, zo, elf_file);
1828 const target_addr = @as(i64, @intCast(ot.targetAddress(index, zo, elf_file)));
1825 pub fn writeEntry(jt: JumpTable, index: Index, zo: *ZigObject, elf_file: *Elf) !void {
1826 const fileoff = jt.entryOffset(index, zo, elf_file);
1827 const source_addr = jt.entryAddress(index, zo, elf_file);
1828 const target_addr = @as(i64, @intCast(jt.targetAddress(index, zo, elf_file)));
18291829 var buf: [max_jump_seq_len]u8 = undefined;
18301830 const out = switch (elf_file.getTarget().cpu.arch) {
18311831 .x86_64 => try x86_64.writeEntry(source_addr, target_addr, &buf),
......@@ -1855,46 +1855,46 @@ pub const OffsetTable = struct {
18551855 }
18561856 }
18571857
1858 pub fn updateSize(ot: OffsetTable, zo: *ZigObject, elf_file: *Elf) !void {
1859 const ot_size: u64 = @intCast(ot.entries.items(.sym_index).len * entrySize(elf_file.getTarget().cpu.arch));
1860 const sym = zo.symbol(ot.sym_index);
1858 pub fn updateSize(jt: JumpTable, zo: *ZigObject, elf_file: *Elf) !void {
1859 const jt_size: u64 = @intCast(jt.entries.items(.sym_index).len * entrySize(elf_file.getTarget().cpu.arch));
1860 const sym = zo.symbol(jt.sym_index);
18611861 const esym = &zo.symtab.items(.elf_sym)[sym.esym_index];
1862 esym.st_size = ot_size;
1862 esym.st_size = jt_size;
18631863 const atom_ptr = sym.atom(elf_file).?;
1864 atom_ptr.size = ot_size;
1864 atom_ptr.size = jt_size;
18651865 }
18661866
1867 const OffsetTableFormatContext = struct { OffsetTable, *ZigObject, *Elf };
1867 const JumpTableFormatContext = struct { JumpTable, *ZigObject, *Elf };
18681868
18691869 pub fn format(
1870 ot: OffsetTable,
1870 jt: JumpTable,
18711871 comptime unused_fmt_string: []const u8,
18721872 options: std.fmt.FormatOptions,
18731873 writer: anytype,
18741874 ) !void {
1875 _ = ot;
1875 _ = jt;
18761876 _ = unused_fmt_string;
18771877 _ = options;
18781878 _ = writer;
1879 @compileError("do not format OffsetTable directly");
1879 @compileError("do not format JumpTable directly");
18801880 }
18811881
1882 pub fn fmt(ot: OffsetTable, zo: *ZigObject, elf_file: *Elf) std.fmt.Formatter(format2) {
1883 return .{ .data = .{ ot, zo, elf_file } };
1882 pub fn fmt(jt: JumpTable, zo: *ZigObject, elf_file: *Elf) std.fmt.Formatter(format2) {
1883 return .{ .data = .{ jt, zo, elf_file } };
18841884 }
18851885
18861886 fn format2(
1887 ctx: OffsetTableFormatContext,
1887 ctx: JumpTableFormatContext,
18881888 comptime unused_fmt_string: []const u8,
18891889 options: std.fmt.FormatOptions,
18901890 writer: anytype,
18911891 ) !void {
18921892 _ = options;
18931893 _ = unused_fmt_string;
1894 const ot, const zo, const ef = ctx;
1895 try writer.writeAll("offset table\n");
1896 try writer.print(" @{x} : size({x})\n", .{ ot.address(zo, ef), ot.size(zo, ef) });
1897 for (ot.entries.items(.sym_index), ot.entries.items(.dirty)) |sym_index, dirty| {
1894 const jt, const zo, const ef = ctx;
1895 try writer.writeAll("__zig_jump_table\n");
1896 try writer.print(" @{x} : size({x})\n", .{ jt.address(zo, ef), jt.size(zo, ef) });
1897 for (jt.entries.items(.sym_index), jt.entries.items(.dirty)) |sym_index, dirty| {
18981898 const sym = zo.symbol(sym_index);
18991899 try writer.print(" %{d} : {s} : @{x}", .{ sym_index, sym.name(ef), sym.address(.{}, ef) });
19001900 if (dirty) try writer.writeAll(" : [!]");