Skip to content

Commit

Permalink
object: format relocs for x86_64 and aarch64
Browse files Browse the repository at this point in the history
  • Loading branch information
kubkon committed Feb 14, 2024
1 parent 7e89ec1 commit 18090ce
Showing 1 changed file with 38 additions and 52 deletions.
90 changes: 38 additions & 52 deletions src/Object.zig
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ pub fn printRelocs(self: Object, writer: anytype) !void {
try writer.print("{x:0>12} {x:0>12} {s: <24} {x:0>16} {s} ", .{
reloc.r_offset,
reloc.r_info,
fmtRelocType(reloc.r_type()),
fmtRelocType(reloc.r_type(), self),
sym.st_value,
sym_name,
});
Expand All @@ -558,66 +558,52 @@ pub fn printRelocs(self: Object, writer: anytype) !void {
}
}

fn fmtRelocType(r_type: u32) std.fmt.Formatter(formatRelocType) {
return .{ .data = r_type };
const FmtRelocTypeCtx = struct {
r_type: u32,
object: Object,
};

fn fmtRelocType(r_type: u32, object: Object) std.fmt.Formatter(formatRelocType) {
return .{ .data = .{
.r_type = r_type,
.object = object,
} };
}

fn formatRelocType(
r_type: u32,
ctx: FmtRelocTypeCtx,
comptime unused_fmt_string: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) !void {
_ = unused_fmt_string;
const str = switch (r_type) {
elf.R_X86_64_NONE => "R_X86_64_NONE",
elf.R_X86_64_64 => "R_X86_64_64",
elf.R_X86_64_PC32 => "R_X86_64_PC32",
elf.R_X86_64_GOT32 => "R_X86_64_GOT32",
elf.R_X86_64_PLT32 => "R_X86_64_PLT32",
elf.R_X86_64_COPY => "R_X86_64_COPY",
elf.R_X86_64_GLOB_DAT => "R_X86_64_GLOB_DAT",
elf.R_X86_64_JUMP_SLOT => "R_X86_64_JUMP_SLOT",
elf.R_X86_64_RELATIVE => "R_X86_64_RELATIVE",
elf.R_X86_64_GOTPCREL => "R_X86_64_GOTPCREL",
elf.R_X86_64_32 => "R_X86_64_32",
elf.R_X86_64_32S => "R_X86_64_32S",
elf.R_X86_64_16 => "R_X86_64_16",
elf.R_X86_64_PC16 => "R_X86_64_PC16",
elf.R_X86_64_8 => "R_X86_64_8",
elf.R_X86_64_PC8 => "R_X86_64_PC8",
elf.R_X86_64_DTPMOD64 => "R_X86_64_DTPMOD64",
elf.R_X86_64_DTPOFF64 => "R_X86_64_DTPOFF64",
elf.R_X86_64_TPOFF64 => "R_X86_64_TPOFF64",
elf.R_X86_64_TLSGD => "R_X86_64_TLSGD",
elf.R_X86_64_TLSLD => "R_X86_64_TLSLD",
elf.R_X86_64_DTPOFF32 => "R_X86_64_DTPOFF32",
elf.R_X86_64_GOTTPOFF => "R_X86_64_GOTTPOFF",
elf.R_X86_64_TPOFF32 => "R_X86_64_TPOFF32",
elf.R_X86_64_PC64 => "R_X86_64_PC64",
elf.R_X86_64_GOTOFF64 => "R_X86_64_GOTOFF64",
elf.R_X86_64_GOTPC32 => "R_X86_64_GOTPC32",
elf.R_X86_64_GOT64 => "R_X86_64_GOT64",
elf.R_X86_64_GOTPCREL64 => "R_X86_64_GOTPCREL64",
elf.R_X86_64_GOTPC64 => "R_X86_64_GOTPC64",
elf.R_X86_64_GOTPLT64 => "R_X86_64_GOTPLT64",
elf.R_X86_64_PLTOFF64 => "R_X86_64_PLTOFF64",
elf.R_X86_64_SIZE32 => "R_X86_64_SIZE32",
elf.R_X86_64_SIZE64 => "R_X86_64_SIZE64",
elf.R_X86_64_GOTPC32_TLSDESC => "R_X86_64_GOTPC32_TLSDESC",
elf.R_X86_64_TLSDESC_CALL => "R_X86_64_TLSDESC_CALL",
elf.R_X86_64_TLSDESC => "R_X86_64_TLSDESC",
elf.R_X86_64_IRELATIVE => "R_X86_64_IRELATIVE",
elf.R_X86_64_RELATIVE64 => "R_X86_64_RELATIVE64",
elf.R_X86_64_GOTPCRELX => "R_X86_64_GOTPCRELX",
elf.R_X86_64_REX_GOTPCRELX => "R_X86_64_REX_GOTPCRELX",
elf.R_X86_64_NUM => "R_X86_64_NUM",
else => "R_X86_64_UNKNOWN",
const object = ctx.object;
const e_machine = switch (object.header.e_machine) {
.NONE => "R_NONE",
.AARCH64 => "R_AARCH64_",
.X86_64 => "R_X86_64_",
else => "R_UNKNOWN",
};
try writer.print("{s}", .{str});
if (options.width) |width| {
if (str.len > width) return error.NoSpaceLeft; // TODO how should we actually handle this here?
const padding = width - str.len;
try writer.writeAll(e_machine);
switch (object.header.e_machine) {
.AARCH64, .X86_64 => {},
else => return,
}
const r_type = switch (object.header.e_machine) {
.AARCH64 => @tagName(@as(elf.R_AARCH64, @enumFromInt(ctx.r_type))),
.X86_64 => @tagName(@as(elf.R_X86_64, @enumFromInt(ctx.r_type))),
else => unreachable,
};
const total_len = r_type.len + e_machine.len;
const width = options.width orelse return writer.print("{s}", .{r_type});
if (object.opts.wide) {
return writer.print("{s}", .{r_type});
}
if (total_len > width) {
try writer.print("{s}", .{r_type[0..@min(r_type.len, width - e_machine.len)]});
} else {
try writer.print("{s}", .{r_type});
const padding = width - total_len;
if (padding > 0) {
// TODO I have no idea what I'm doing here!
var fill_buffer: [4]u8 = undefined;
Expand Down

0 comments on commit 18090ce

Please sign in to comment.