authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-08-13 10:28:01+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-08-13 13:30:24+02:00
logce8886d57d1593a7eb4909e518fc49e1c95360dd
treef9000ced65ae45021af02da0474dac13a03e77c6
parentd25c93a868585e4e2f5941fcff15fa49c90510e9

elf: make zig jump table indirection implicit via Symbol.address


3 files changed, 35 insertions(+), 38 deletions(-)

src/link/Elf/Atom.zig+12-23
......@@ -750,8 +750,6 @@ 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 zig jump table entry if any.
754 const ZJT = target.zigJumpTableAddress(elf_file);
755753 // Relative offset to the start of the global offset table.
756754 const G = target.gotAddress(elf_file) - GOT;
757755 // // Address of the thread pointer.
......@@ -759,19 +757,18 @@ pub fn resolveRelocsAlloc(self: Atom, elf_file: *Elf, code: []u8) RelocError!voi
759757 // Address of the dynamic thread pointer.
760758 const DTP = elf_file.dtpAddress();
761759
762 relocs_log.debug(" {s}: {x}: [{x} => {x}] GOT({x}) ZJT({x}) ({s})", .{
760 relocs_log.debug(" {s}: {x}: [{x} => {x}] GOT({x}) ({s})", .{
763761 relocation.fmtRelocType(rel.r_type(), cpu_arch),
764762 r_offset,
765763 P,
766764 S + A,
767765 G + GOT + A,
768 ZJT + A,
769766 target.name(elf_file),
770767 });
771768
772769 try stream.seekTo(r_offset);
773770
774 const args = ResolveArgs{ P, A, S, GOT, G, TP, DTP, ZJT };
771 const args = ResolveArgs{ P, A, S, GOT, G, TP, DTP };
775772
776773 switch (cpu_arch) {
777774 .x86_64 => x86_64.resolveRelocAlloc(self, elf_file, rel, target, args, &it, code, &stream) catch |err| switch (err) {
......@@ -956,7 +953,7 @@ pub fn resolveRelocsNonAlloc(self: Atom, elf_file: *Elf, code: []u8, undefs: any
956953 // Address of the dynamic thread pointer.
957954 const DTP = elf_file.dtpAddress();
958955
959 const args = ResolveArgs{ P, A, S, GOT, 0, 0, DTP, 0 };
956 const args = ResolveArgs{ P, A, S, GOT, 0, 0, DTP };
960957
961958 relocs_log.debug(" {}: {x}: [{x} => {x}] ({s})", .{
962959 relocation.fmtRelocType(rel.r_type(), cpu_arch),
......@@ -1200,7 +1197,7 @@ const x86_64 = struct {
12001197
12011198 const cwriter = stream.writer();
12021199
1203 const P, const A, const S, const GOT, const G, const TP, const DTP, const ZJT = args;
1200 const P, const A, const S, const GOT, const G, const TP, const DTP = args;
12041201
12051202 switch (r_type) {
12061203 .NONE => unreachable,
......@@ -1215,10 +1212,7 @@ const x86_64 = struct {
12151212 );
12161213 },
12171214
1218 .PLT32 => {
1219 const S_ = if (target.flags.zig_jump_table) ZJT else S;
1220 try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .little);
1221 },
1215 .PLT32 => try cwriter.writeInt(i32, @as(i32, @intCast(S + A - P)), .little),
12221216 .PC32 => try cwriter.writeInt(i32, @as(i32, @intCast(S + A - P)), .little),
12231217
12241218 .GOTPCREL => try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A - P)), .little),
......@@ -1243,10 +1237,7 @@ const x86_64 = struct {
12431237 try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A - P)), .little);
12441238 },
12451239
1246 .@"32" => {
1247 const S_ = if (target.flags.zig_jump_table) ZJT else S;
1248 try cwriter.writeInt(u32, @as(u32, @truncate(@as(u64, @intCast(S_ + A)))), .little);
1249 },
1240 .@"32" => try cwriter.writeInt(u32, @as(u32, @truncate(@as(u64, @intCast(S + A)))), .little),
12501241 .@"32S" => try cwriter.writeInt(i32, @as(i32, @truncate(S + A)), .little),
12511242
12521243 .TPOFF32 => try cwriter.writeInt(i32, @as(i32, @truncate(S + A - TP)), .little),
......@@ -1345,7 +1336,7 @@ const x86_64 = struct {
13451336 const r_type: elf.R_X86_64 = @enumFromInt(rel.r_type());
13461337 const cwriter = stream.writer();
13471338
1348 _, const A, const S, const GOT, _, _, const DTP, _ = args;
1339 _, const A, const S, const GOT, _, _, const DTP = args;
13491340
13501341 switch (r_type) {
13511342 .NONE => unreachable,
......@@ -1728,9 +1719,8 @@ const aarch64 = struct {
17281719 const code = code_buffer[r_offset..][0..4];
17291720 const file_ptr = atom.file(elf_file).?;
17301721
1731 const P, const A, const S, const GOT, const G, const TP, const DTP, const ZJT = args;
1722 const P, const A, const S, const GOT, const G, const TP, const DTP = args;
17321723 _ = DTP;
1733 _ = ZJT;
17341724
17351725 switch (r_type) {
17361726 .NONE => unreachable,
......@@ -1932,7 +1922,7 @@ const aarch64 = struct {
19321922 const r_type: elf.R_AARCH64 = @enumFromInt(rel.r_type());
19331923 const cwriter = stream.writer();
19341924
1935 _, const A, const S, _, _, _, _, _ = args;
1925 _, const A, const S, _, _, _, _ = args;
19361926
19371927 switch (r_type) {
19381928 .NONE => unreachable,
......@@ -2012,10 +2002,9 @@ const riscv = struct {
20122002 const r_offset = std.math.cast(usize, rel.r_offset) orelse return error.Overflow;
20132003 const cwriter = stream.writer();
20142004
2015 const P, const A, const S, const GOT, const G, const TP, const DTP, const ZJT = args;
2005 const P, const A, const S, const GOT, const G, const TP, const DTP = args;
20162006 _ = TP;
20172007 _ = DTP;
2018 _ = ZJT;
20192008
20202009 switch (r_type) {
20212010 .NONE => unreachable,
......@@ -2167,7 +2156,7 @@ const riscv = struct {
21672156 const r_offset = std.math.cast(usize, rel.r_offset) orelse return error.Overflow;
21682157 const cwriter = stream.writer();
21692158
2170 _, const A, const S, const GOT, _, _, const DTP, _ = args;
2159 _, const A, const S, const GOT, _, _, const DTP = args;
21712160 _ = GOT;
21722161 _ = DTP;
21732162
......@@ -2203,7 +2192,7 @@ const riscv = struct {
22032192 const riscv_util = @import("../riscv.zig");
22042193};
22052194
2206const ResolveArgs = struct { i64, i64, i64, i64, i64, i64, i64, i64 };
2195const ResolveArgs = struct { i64, i64, i64, i64, i64, i64, i64 };
22072196
22082197const RelocError = error{
22092198 Overflow,
src/link/Elf/Symbol.zig+12-9
......@@ -101,7 +101,7 @@ pub fn symbolRank(symbol: Symbol, elf_file: *Elf) u32 {
101101 return file_ptr.symbolRank(sym, in_archive);
102102}
103103
104pub fn address(symbol: Symbol, opts: struct { plt: bool = true }, elf_file: *Elf) i64 {
104pub fn address(symbol: Symbol, opts: struct { plt: bool = true, zjt: bool = true }, elf_file: *Elf) i64 {
105105 if (symbol.mergeSubsection(elf_file)) |msub| {
106106 if (!msub.alive) return 0;
107107 return msub.address(elf_file) + symbol.value;
......@@ -109,6 +109,9 @@ pub fn address(symbol: Symbol, opts: struct { plt: bool = true }, elf_file: *Elf
109109 if (symbol.flags.has_copy_rel) {
110110 return symbol.copyRelAddress(elf_file);
111111 }
112 if (symbol.flags.has_zjt and opts.zjt) {
113 return symbol.zjtAddress(elf_file);
114 }
112115 if (symbol.flags.has_plt and opts.plt) {
113116 if (!symbol.flags.is_canonical and symbol.flags.has_got) {
114117 // We have a non-lazy bound function pointer, use that!
......@@ -217,12 +220,12 @@ pub fn tlsDescAddress(symbol: Symbol, elf_file: *Elf) i64 {
217220 return entry.address(elf_file);
218221}
219222
220pub fn zigJumpTableAddress(symbol: Symbol, elf_file: *Elf) i64 {
221 if (!symbol.flags.zig_jump_table) return 0;
223pub fn zjtAddress(symbol: Symbol, elf_file: *Elf) i64 {
224 if (!symbol.flags.has_zjt) return 0;
222225 const zo = elf_file.zigObjectPtr().?;
223226 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);
227 const index = symbol.extra(elf_file).zjt;
228 return jump_table.entryAddress(index, zo, elf_file);
226229}
227230
228231pub fn dsoAlignment(symbol: Symbol, elf_file: *Elf) !u64 {
......@@ -248,7 +251,7 @@ const AddExtraOpts = struct {
248251 tlsgd: ?u32 = null,
249252 gottp: ?u32 = null,
250253 tlsdesc: ?u32 = null,
251 zig_jump_table: ?u32 = null,
254 zjt: ?u32 = null,
252255};
253256
254257pub fn addExtra(symbol: *Symbol, opts: AddExtraOpts, elf_file: *Elf) void {
......@@ -377,7 +380,7 @@ fn format2(
377380 try writer.print("%{d} : {s} : @{x}", .{
378381 symbol.esym_index,
379382 symbol.fmtName(elf_file),
380 symbol.address(.{}, elf_file),
383 symbol.address(.{ .plt = false, .zjt = false }, elf_file),
381384 });
382385 if (symbol.file(elf_file)) |file_ptr| {
383386 if (symbol.isAbs(elf_file)) {
......@@ -454,7 +457,7 @@ pub const Flags = packed struct {
454457 merge_subsection: bool = false,
455458
456459 /// Whether the symbol has __zig_jump_table indirection.
457 zig_jump_table: bool = false,
460 has_zjt: bool = false,
458461};
459462
460463pub const Extra = struct {
......@@ -468,7 +471,7 @@ pub const Extra = struct {
468471 gottp: u32 = 0,
469472 tlsdesc: u32 = 0,
470473 merge_section: u32 = 0,
471 zig_jump_table: u32 = 0,
474 zjt: u32 = 0,
472475};
473476
474477pub const Index = u32;
src/link/Elf/ZigObject.zig+11-6
......@@ -918,7 +918,7 @@ fn updateNavCode(
918918 if (stt_bits == elf.STT_FUNC) {
919919 const extra = sym.extra(elf_file);
920920 const jump_table = self.jumpTablePtr().?;
921 jump_table.entries.items(.dirty)[extra.zig_jump_table] = true;
921 jump_table.entries.items(.dirty)[extra.zjt] = true;
922922 }
923923 }
924924 } else if (code.len < old_size) {
......@@ -1045,10 +1045,10 @@ pub fn updateFunc(
10451045
10461046 {
10471047 const sym = self.symbol(sym_index);
1048 if (!sym.flags.zig_jump_table) {
1048 if (!sym.flags.has_zjt) {
10491049 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);
1050 sym.flags.has_zjt = true;
1051 sym.addExtra(.{ .zjt = index }, elf_file);
10521052 try jump_table.updateSize(self, elf_file);
10531053 const old_vaddr = jump_table.address(self, elf_file);
10541054 try self.symbol(jump_table.sym_index).atom(elf_file).?.allocate(elf_file);
......@@ -1108,7 +1108,7 @@ pub fn updateFunc(
11081108 }
11091109 } else {
11101110 const sym = self.symbol(sym_index);
1111 const jt_index = sym.extra(elf_file).zig_jump_table;
1111 const jt_index = sym.extra(elf_file).zjt;
11121112 var jt_entry = jump_table.entries.get(jt_index);
11131113 if (jt_entry.dirty) {
11141114 try jump_table.writeEntry(jt_index, self, elf_file);
......@@ -1896,7 +1896,12 @@ pub const JumpTable = struct {
18961896 try writer.print(" @{x} : size({x})\n", .{ jt.address(zo, ef), jt.size(zo, ef) });
18971897 for (jt.entries.items(.sym_index), jt.entries.items(.dirty)) |sym_index, dirty| {
18981898 const sym = zo.symbol(sym_index);
1899 try writer.print(" %{d} : {s} : @{x}", .{ sym_index, sym.name(ef), sym.address(.{}, ef) });
1899 try writer.print(" {x} => {x} : %{d} : {s}", .{
1900 sym.address(.{}, ef),
1901 sym.address(.{ .zjt = false }, ef),
1902 sym_index,
1903 sym.name(ef),
1904 });
19001905 if (dirty) try writer.writeAll(" : [!]");
19011906 try writer.writeByte('\n');
19021907 }