authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-17 12:35:12+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-02-17 12:35:16+01:00
log509c7149b9f30e3271f2f8ba7669b95d2d240b9b
treeaa434782cf9b3d02dcaf8b7e7e712f1a615f713f
parent5fb54736dffc6c991312e4e57ae00688bbf8e801

elf: fix formatting of relocs when reloc can be Zig specific


2 files changed, 34 insertions(+), 28 deletions(-)

src/link/Elf/Atom.zig+23-23
...@@ -336,7 +336,7 @@ pub fn writeRelocs(self: Atom, elf_file: *Elf, out_relocs: *std.ArrayList(elf.El...@@ -336,7 +336,7 @@ pub fn writeRelocs(self: Atom, elf_file: *Elf, out_relocs: *std.ArrayList(elf.El
336 }336 }
337337
338 relocs_log.debug(" {s}: [{x} => {d}({s})] + {x}", .{338 relocs_log.debug(" {s}: [{x} => {d}({s})] + {x}", .{
339 relocation.fmtRelocType(r_type, cpu_arch),339 relocation.fmtRelocType(rel.r_type(), cpu_arch),
340 r_offset,340 r_offset,
341 r_sym,341 r_sym,
342 target.name(elf_file),342 target.name(elf_file),
...@@ -1028,7 +1028,7 @@ const x86_64 = struct {...@@ -1028,7 +1028,7 @@ const x86_64 = struct {
1028 const DTP = @as(i64, @intCast(elf_file.dtpAddress()));1028 const DTP = @as(i64, @intCast(elf_file.dtpAddress()));
10291029
1030 relocs_log.debug(" {s}: {x}: [{x} => {x}] G({x}) ZG({x}) ({s})", .{1030 relocs_log.debug(" {s}: {x}: [{x} => {x}] G({x}) ZG({x}) ({s})", .{
1031 @tagName(r_type),1031 relocation.fmtRelocType(rel.r_type(), .x86_64),
1032 r_offset,1032 r_offset,
1033 P,1033 P,
1034 S + A,1034 S + A,
...@@ -1218,8 +1218,8 @@ const x86_64 = struct {...@@ -1218,8 +1218,8 @@ const x86_64 = struct {
1218 // Address of the dynamic thread pointer.1218 // Address of the dynamic thread pointer.
1219 const DTP = @as(i64, @intCast(elf_file.dtpAddress()));1219 const DTP = @as(i64, @intCast(elf_file.dtpAddress()));
12201220
1221 relocs_log.debug(" {s}: {x}: [{x} => {x}] ({s})", .{1221 relocs_log.debug(" {}: {x}: [{x} => {x}] ({s})", .{
1222 @tagName(r_type),1222 relocation.fmtRelocType(rel.r_type(), .x86_64),
1223 rel.r_offset,1223 rel.r_offset,
1224 P,1224 P,
1225 S + A,1225 S + A,
...@@ -1291,9 +1291,8 @@ const x86_64 = struct {...@@ -1291,9 +1291,8 @@ const x86_64 = struct {
1291 ) !void {1291 ) !void {
1292 assert(rels.len == 2);1292 assert(rels.len == 2);
1293 const writer = stream.writer();1293 const writer = stream.writer();
1294 const rel_0: elf.R_X86_64 = @enumFromInt(rels[0].r_type());1294 const rel: elf.R_X86_64 = @enumFromInt(rels[1].r_type());
1295 const rel_1: elf.R_X86_64 = @enumFromInt(rels[1].r_type());1295 switch (rel) {
1296 switch (rel_1) {
1297 .R_X86_64_PC32,1296 .R_X86_64_PC32,
1298 .R_X86_64_PLT32,1297 .R_X86_64_PLT32,
1299 => {1298 => {
...@@ -1308,9 +1307,9 @@ const x86_64 = struct {...@@ -1308,9 +1307,9 @@ const x86_64 = struct {
13081307
1309 else => {1308 else => {
1310 var err = try elf_file.addErrorWithNotes(1);1309 var err = try elf_file.addErrorWithNotes(1);
1311 try err.addMsg(elf_file, "fatal linker error: rewrite {s} when followed by {s}", .{1310 try err.addMsg(elf_file, "fatal linker error: rewrite {} when followed by {}", .{
1312 @tagName(rel_0),1311 relocation.fmtRelocType(rels[0].r_type(), .x86_64),
1313 @tagName(rel_1),1312 relocation.fmtRelocType(rels[1].r_type(), .x86_64),
1314 });1313 });
1315 try err.addNote(elf_file, "in {}:{s} at offset 0x{x}", .{1314 try err.addNote(elf_file, "in {}:{s} at offset 0x{x}", .{
1316 self.file(elf_file).?.fmtPath(),1315 self.file(elf_file).?.fmtPath(),
...@@ -1330,9 +1329,8 @@ const x86_64 = struct {...@@ -1330,9 +1329,8 @@ const x86_64 = struct {
1330 ) !void {1329 ) !void {
1331 assert(rels.len == 2);1330 assert(rels.len == 2);
1332 const writer = stream.writer();1331 const writer = stream.writer();
1333 const rel_0: elf.R_X86_64 = @enumFromInt(rels[0].r_type());1332 const rel: elf.R_X86_64 = @enumFromInt(rels[1].r_type());
1334 const rel_1: elf.R_X86_64 = @enumFromInt(rels[1].r_type());1333 switch (rel) {
1335 switch (rel_1) {
1336 .R_X86_64_PC32,1334 .R_X86_64_PC32,
1337 .R_X86_64_PLT32,1335 .R_X86_64_PLT32,
1338 => {1336 => {
...@@ -1362,9 +1360,9 @@ const x86_64 = struct {...@@ -1362,9 +1360,9 @@ const x86_64 = struct {
13621360
1363 else => {1361 else => {
1364 var err = try elf_file.addErrorWithNotes(1);1362 var err = try elf_file.addErrorWithNotes(1);
1365 try err.addMsg(elf_file, "fatal linker error: rewrite {s} when followed by {s}", .{1363 try err.addMsg(elf_file, "fatal linker error: rewrite {} when followed by {}", .{
1366 @tagName(rel_0),1364 relocation.fmtRelocType(rels[0].r_type(), .x86_64),
1367 @tagName(rel_1),1365 relocation.fmtRelocType(rels[1].r_type(), .x86_64),
1368 });1366 });
1369 try err.addNote(elf_file, "in {}:{s} at offset 0x{x}", .{1367 try err.addNote(elf_file, "in {}:{s} at offset 0x{x}", .{
1370 self.file(elf_file).?.fmtPath(),1368 self.file(elf_file).?.fmtPath(),
...@@ -1431,9 +1429,8 @@ const x86_64 = struct {...@@ -1431,9 +1429,8 @@ const x86_64 = struct {
1431 ) !void {1429 ) !void {
1432 assert(rels.len == 2);1430 assert(rels.len == 2);
1433 const writer = stream.writer();1431 const writer = stream.writer();
1434 const rel_0: elf.R_X86_64 = @enumFromInt(rels[0].r_type());1432 const rel: elf.R_X86_64 = @enumFromInt(rels[1].r_type());
1435 const rel_1: elf.R_X86_64 = @enumFromInt(rels[1].r_type());1433 switch (rel) {
1436 switch (rel_1) {
1437 .R_X86_64_PC32,1434 .R_X86_64_PC32,
1438 .R_X86_64_PLT32,1435 .R_X86_64_PLT32,
1439 .R_X86_64_GOTPCREL,1436 .R_X86_64_GOTPCREL,
...@@ -1446,14 +1443,17 @@ const x86_64 = struct {...@@ -1446,14 +1443,17 @@ const x86_64 = struct {
1446 std.mem.writeInt(i32, insts[12..][0..4], value, .little);1443 std.mem.writeInt(i32, insts[12..][0..4], value, .little);
1447 try stream.seekBy(-4);1444 try stream.seekBy(-4);
1448 try writer.writeAll(&insts);1445 try writer.writeAll(&insts);
1449 relocs_log.debug(" relaxing {s} and {s}", .{ @tagName(rel_0), @tagName(rel_1) });1446 relocs_log.debug(" relaxing {} and {}", .{
1447 relocation.fmtRelocType(rels[0].r_type(), .x86_64),
1448 relocation.fmtRelocType(rels[1].r_type(), .x86_64),
1449 });
1450 },1450 },
14511451
1452 else => {1452 else => {
1453 var err = try elf_file.addErrorWithNotes(1);1453 var err = try elf_file.addErrorWithNotes(1);
1454 try err.addMsg(elf_file, "fatal linker error: rewrite {s} when followed by {s}", .{1454 try err.addMsg(elf_file, "fatal linker error: rewrite {} when followed by {}", .{
1455 @tagName(rel_0),1455 relocation.fmtRelocType(rels[0].r_type(), .x86_64),
1456 @tagName(rel_1),1456 relocation.fmtRelocType(rels[1].r_type(), .x86_64),
1457 });1457 });
1458 try err.addNote(elf_file, "in {}:{s} at offset 0x{x}", .{1458 try err.addNote(elf_file, "in {}:{s} at offset 0x{x}", .{
1459 self.file(elf_file).?.fmtPath(),1459 self.file(elf_file).?.fmtPath(),
src/link/Elf/relocation.zig+11-5
...@@ -106,11 +106,15 @@ fn formatRelocType(...@@ -106,11 +106,15 @@ fn formatRelocType(
106 _ = unused_fmt_string;106 _ = unused_fmt_string;
107 _ = options;107 _ = options;
108 const r_type = ctx.r_type;108 const r_type = ctx.r_type;
109 const str = switch (ctx.cpu_arch) {109 const str = switch (r_type) {
110 .x86_64 => @tagName(@as(elf.R_X86_64, @enumFromInt(r_type))),110 Elf.R_ZIG_GOT32 => "R_ZIG_GOT32",
111 .aarch64 => @tagName(@as(elf.R_AARCH64, @enumFromInt(r_type))),111 Elf.R_ZIG_GOTPCREL => "R_ZIG_GOTPCREL",
112 .riscv64 => @tagName(@as(elf.R_RISCV, @enumFromInt(r_type))),112 else => switch (ctx.cpu_arch) {
113 else => unreachable,113 .x86_64 => @tagName(@as(elf.R_X86_64, @enumFromInt(r_type))),
114 .aarch64 => @tagName(@as(elf.R_AARCH64, @enumFromInt(r_type))),
115 .riscv64 => @tagName(@as(elf.R_RISCV, @enumFromInt(r_type))),
116 else => unreachable,
117 },
114 };118 };
115 try writer.print("{s}", .{str});119 try writer.print("{s}", .{str});
116}120}
...@@ -118,3 +122,5 @@ fn formatRelocType(...@@ -118,3 +122,5 @@ fn formatRelocType(
118const assert = std.debug.assert;122const assert = std.debug.assert;
119const elf = std.elf;123const elf = std.elf;
120const std = @import("std");124const std = @import("std");
125
126const Elf = @import("../Elf.zig");