From 02f38d7749e5260dc2f48caef10390dd8d3cede5 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Wed, 7 Aug 2024 06:50:54 +0200 Subject: [PATCH] codegen: fix Elf symbol refs --- src/arch/riscv64/CodeGen.zig | 16 ++++------------ src/arch/x86_64/CodeGen.zig | 12 +++++------- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/src/arch/riscv64/CodeGen.zig b/src/arch/riscv64/CodeGen.zig index 979f76f50d..c4adb82ee6 100644 --- a/src/arch/riscv64/CodeGen.zig +++ b/src/arch/riscv64/CodeGen.zig @@ -1415,9 +1415,8 @@ fn genLazy(func: *Func, lazy_sym: link.File.LazySymbol) InnerError!void { .ty = enum_ty, }) catch |err| return func.fail("{s} creating lazy symbol", .{@errorName(err)}); - const sym = zo.symbol(sym_index); - try func.genSetReg(Type.u64, data_reg, .{ .lea_symbol = .{ .sym = sym.esym_index } }); + try func.genSetReg(Type.u64, data_reg, .{ .lea_symbol = .{ .sym = sym_index } }); const cmp_reg, const cmp_lock = try func.allocReg(.int); defer func.register_manager.unlockReg(cmp_lock); @@ -4949,12 +4948,11 @@ fn genCall( if (func.bin_file.cast(link.File.Elf)) |elf_file| { const zo = elf_file.zigObjectPtr().?; const sym_index = try zo.getOrCreateMetadataForDecl(elf_file, func_val.owner_decl); - const sym = zo.symbol(sym_index); if (func.mod.pic) { return func.fail("TODO: genCall pic", .{}); } else { - try func.genSetReg(Type.u64, .ra, .{ .load_symbol = .{ .sym = sym.esym_index } }); + try func.genSetReg(Type.u64, .ra, .{ .load_symbol = .{ .sym = sym_index } }); _ = try func.addInst(.{ .tag = .jalr, .data = .{ .i_type = .{ @@ -7827,12 +7825,11 @@ fn airTagName(func: *Func, inst: Air.Inst.Index) !void { const zo = elf_file.zigObjectPtr().?; const sym_index = zo.getOrCreateMetadataForLazySymbol(elf_file, pt, lazy_sym) catch |err| return func.fail("{s} creating lazy symbol", .{@errorName(err)}); - const sym = zo.symbol(sym_index); if (func.mod.pic) { return func.fail("TODO: airTagName pic", .{}); } else { - try func.genSetReg(Type.u64, .ra, .{ .load_symbol = .{ .sym = sym.esym_index } }); + try func.genSetReg(Type.u64, .ra, .{ .load_symbol = .{ .sym = sym_index } }); _ = try func.addInst(.{ .tag = .jalr, .data = .{ .i_type = .{ @@ -8050,12 +8047,7 @@ fn genTypedValue(func: *Func, val: Value) InnerError!MCValue { return error.CodegenFail; }; switch (lf.tag) { - .elf => { - const elf_file = lf.cast(link.File.Elf).?; - const zo = elf_file.zigObjectPtr().?; - const local = zo.symbol(local_sym_index); - return MCValue{ .undef = local.esym_index }; - }, + .elf => return MCValue{ .undef = local_sym_index }, else => unreachable, } } diff --git a/src/arch/x86_64/CodeGen.zig b/src/arch/x86_64/CodeGen.zig index 4543cce33c..924de9d25a 100644 --- a/src/arch/x86_64/CodeGen.zig +++ b/src/arch/x86_64/CodeGen.zig @@ -12329,7 +12329,6 @@ fn genCall(self: *Self, info: union(enum) { if (self.bin_file.cast(link.File.Elf)) |elf_file| { const zo = elf_file.zigObjectPtr().?; const sym_index = try zo.getOrCreateMetadataForDecl(elf_file, func.owner_decl); - const sym = zo.symbol(sym_index); if (self.mod.pic) { const callee_reg: Register = switch (resolved_cc) { .SysV => callee: { @@ -12346,14 +12345,14 @@ fn genCall(self: *Self, info: union(enum) { try self.genSetReg( callee_reg, Type.usize, - .{ .load_symbol = .{ .sym = sym.esym_index } }, + .{ .load_symbol = .{ .sym = sym_index } }, .{}, ); try self.asmRegister(.{ ._, .call }, callee_reg); } else try self.asmMemory(.{ ._, .call }, .{ .base = .{ .reloc = .{ .atom_index = try self.owner.getSymbolIndex(self), - .sym_index = sym.esym_index, + .sym_index = sym_index, } }, .mod = .{ .rm = .{ .size = .qword } }, }); @@ -15324,14 +15323,13 @@ fn genLazySymbolRef( const zo = elf_file.zigObjectPtr().?; const sym_index = zo.getOrCreateMetadataForLazySymbol(elf_file, pt, lazy_sym) catch |err| return self.fail("{s} creating lazy symbol", .{@errorName(err)}); - const sym = zo.symbol(sym_index); if (self.mod.pic) { switch (tag) { .lea, .call => try self.genSetReg(reg, Type.usize, .{ - .load_symbol = .{ .sym = sym.esym_index }, + .load_symbol = .{ .sym = sym_index }, }, .{}), .mov => try self.genSetReg(reg, Type.usize, .{ - .load_symbol = .{ .sym = sym.esym_index }, + .load_symbol = .{ .sym = sym_index }, }, .{}), else => unreachable, } @@ -15343,7 +15341,7 @@ fn genLazySymbolRef( } else { const reloc = bits.Symbol{ .atom_index = try self.owner.getSymbolIndex(self), - .sym_index = sym.esym_index, + .sym_index = sym_index, }; switch (tag) { .lea, .mov => try self.asmRegisterMemory(.{ ._, .mov }, reg.to64(), .{