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
336336 }
337337
338338 relocs_log.debug(" {s}: [{x} => {d}({s})] + {x}", .{
339 relocation.fmtRelocType(r_type, cpu_arch),
339 relocation.fmtRelocType(rel.r_type(), cpu_arch),
340340 r_offset,
341341 r_sym,
342342 target.name(elf_file),
......@@ -1028,7 +1028,7 @@ const x86_64 = struct {
10281028 const DTP = @as(i64, @intCast(elf_file.dtpAddress()));
10291029
10301030 relocs_log.debug(" {s}: {x}: [{x} => {x}] G({x}) ZG({x}) ({s})", .{
1031 @tagName(r_type),
1031 relocation.fmtRelocType(rel.r_type(), .x86_64),
10321032 r_offset,
10331033 P,
10341034 S + A,
......@@ -1218,8 +1218,8 @@ const x86_64 = struct {
12181218 // Address of the dynamic thread pointer.
12191219 const DTP = @as(i64, @intCast(elf_file.dtpAddress()));
12201220
1221 relocs_log.debug(" {s}: {x}: [{x} => {x}] ({s})", .{
1222 @tagName(r_type),
1221 relocs_log.debug(" {}: {x}: [{x} => {x}] ({s})", .{
1222 relocation.fmtRelocType(rel.r_type(), .x86_64),
12231223 rel.r_offset,
12241224 P,
12251225 S + A,
......@@ -1291,9 +1291,8 @@ const x86_64 = struct {
12911291 ) !void {
12921292 assert(rels.len == 2);
12931293 const writer = stream.writer();
1294 const rel_0: elf.R_X86_64 = @enumFromInt(rels[0].r_type());
1295 const rel_1: elf.R_X86_64 = @enumFromInt(rels[1].r_type());
1296 switch (rel_1) {
1294 const rel: elf.R_X86_64 = @enumFromInt(rels[1].r_type());
1295 switch (rel) {
12971296 .R_X86_64_PC32,
12981297 .R_X86_64_PLT32,
12991298 => {
......@@ -1308,9 +1307,9 @@ const x86_64 = struct {
13081307
13091308 else => {
13101309 var err = try elf_file.addErrorWithNotes(1);
1311 try err.addMsg(elf_file, "fatal linker error: rewrite {s} when followed by {s}", .{
1312 @tagName(rel_0),
1313 @tagName(rel_1),
1310 try err.addMsg(elf_file, "fatal linker error: rewrite {} when followed by {}", .{
1311 relocation.fmtRelocType(rels[0].r_type(), .x86_64),
1312 relocation.fmtRelocType(rels[1].r_type(), .x86_64),
13141313 });
13151314 try err.addNote(elf_file, "in {}:{s} at offset 0x{x}", .{
13161315 self.file(elf_file).?.fmtPath(),
......@@ -1330,9 +1329,8 @@ const x86_64 = struct {
13301329 ) !void {
13311330 assert(rels.len == 2);
13321331 const writer = stream.writer();
1333 const rel_0: elf.R_X86_64 = @enumFromInt(rels[0].r_type());
1334 const rel_1: elf.R_X86_64 = @enumFromInt(rels[1].r_type());
1335 switch (rel_1) {
1332 const rel: elf.R_X86_64 = @enumFromInt(rels[1].r_type());
1333 switch (rel) {
13361334 .R_X86_64_PC32,
13371335 .R_X86_64_PLT32,
13381336 => {
......@@ -1362,9 +1360,9 @@ const x86_64 = struct {
13621360
13631361 else => {
13641362 var err = try elf_file.addErrorWithNotes(1);
1365 try err.addMsg(elf_file, "fatal linker error: rewrite {s} when followed by {s}", .{
1366 @tagName(rel_0),
1367 @tagName(rel_1),
1363 try err.addMsg(elf_file, "fatal linker error: rewrite {} when followed by {}", .{
1364 relocation.fmtRelocType(rels[0].r_type(), .x86_64),
1365 relocation.fmtRelocType(rels[1].r_type(), .x86_64),
13681366 });
13691367 try err.addNote(elf_file, "in {}:{s} at offset 0x{x}", .{
13701368 self.file(elf_file).?.fmtPath(),
......@@ -1431,9 +1429,8 @@ const x86_64 = struct {
14311429 ) !void {
14321430 assert(rels.len == 2);
14331431 const writer = stream.writer();
1434 const rel_0: elf.R_X86_64 = @enumFromInt(rels[0].r_type());
1435 const rel_1: elf.R_X86_64 = @enumFromInt(rels[1].r_type());
1436 switch (rel_1) {
1432 const rel: elf.R_X86_64 = @enumFromInt(rels[1].r_type());
1433 switch (rel) {
14371434 .R_X86_64_PC32,
14381435 .R_X86_64_PLT32,
14391436 .R_X86_64_GOTPCREL,
......@@ -1446,14 +1443,17 @@ const x86_64 = struct {
14461443 std.mem.writeInt(i32, insts[12..][0..4], value, .little);
14471444 try stream.seekBy(-4);
14481445 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 });
14501450 },
14511451
14521452 else => {
14531453 var err = try elf_file.addErrorWithNotes(1);
1454 try err.addMsg(elf_file, "fatal linker error: rewrite {s} when followed by {s}", .{
1455 @tagName(rel_0),
1456 @tagName(rel_1),
1454 try err.addMsg(elf_file, "fatal linker error: rewrite {} when followed by {}", .{
1455 relocation.fmtRelocType(rels[0].r_type(), .x86_64),
1456 relocation.fmtRelocType(rels[1].r_type(), .x86_64),
14571457 });
14581458 try err.addNote(elf_file, "in {}:{s} at offset 0x{x}", .{
14591459 self.file(elf_file).?.fmtPath(),
src/link/Elf/relocation.zig+11-5
......@@ -106,11 +106,15 @@ fn formatRelocType(
106106 _ = unused_fmt_string;
107107 _ = options;
108108 const r_type = ctx.r_type;
109 const str = switch (ctx.cpu_arch) {
110 .x86_64 => @tagName(@as(elf.R_X86_64, @enumFromInt(r_type))),
111 .aarch64 => @tagName(@as(elf.R_AARCH64, @enumFromInt(r_type))),
112 .riscv64 => @tagName(@as(elf.R_RISCV, @enumFromInt(r_type))),
113 else => unreachable,
109 const str = switch (r_type) {
110 Elf.R_ZIG_GOT32 => "R_ZIG_GOT32",
111 Elf.R_ZIG_GOTPCREL => "R_ZIG_GOTPCREL",
112 else => switch (ctx.cpu_arch) {
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 },
114118 };
115119 try writer.print("{s}", .{str});
116120}
......@@ -118,3 +122,5 @@ fn formatRelocType(
118122const assert = std.debug.assert;
119123const elf = std.elf;
120124const std = @import("std");
125
126const Elf = @import("../Elf.zig");