aboutsummaryrefslogtreecommitdiff
path: root/src/link/Elf/relocation.zig
blob: 3c8afa3c12c13b34fb93c0f540ff988deefe616e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
pub const Kind = enum {
    none,
    other,
    abs,
    copy,
    rel,
    irel,
    glob_dat,
    jump_slot,
    dtpmod,
    dtpoff,
    tpoff,
    tlsdesc,
};

fn Table(comptime len: comptime_int, comptime RelType: type, comptime mapping: [len]struct { Kind, RelType }) type {
    return struct {
        fn decode(r_type: u32) Kind {
            inline for (mapping) |entry| {
                if (@intFromEnum(entry[1]) == r_type) return entry[0];
            }
            return .other;
        }

        fn encode(comptime kind: Kind) u32 {
            inline for (mapping) |entry| {
                if (entry[0] == kind) return @intFromEnum(entry[1]);
            }
            @panic("encoding .other is ambiguous");
        }
    };
}

const x86_64_relocs = Table(11, elf.R_X86_64, .{
    .{ .none, .NONE },
    .{ .abs, .@"64" },
    .{ .copy, .COPY },
    .{ .rel, .RELATIVE },
    .{ .irel, .IRELATIVE },
    .{ .glob_dat, .GLOB_DAT },
    .{ .jump_slot, .JUMP_SLOT },
    .{ .dtpmod, .DTPMOD64 },
    .{ .dtpoff, .DTPOFF64 },
    .{ .tpoff, .TPOFF64 },
    .{ .tlsdesc, .TLSDESC },
});

const aarch64_relocs = Table(11, elf.R_AARCH64, .{
    .{ .none, .NONE },
    .{ .abs, .ABS64 },
    .{ .copy, .COPY },
    .{ .rel, .RELATIVE },
    .{ .irel, .IRELATIVE },
    .{ .glob_dat, .GLOB_DAT },
    .{ .jump_slot, .JUMP_SLOT },
    .{ .dtpmod, .TLS_DTPMOD },
    .{ .dtpoff, .TLS_DTPREL },
    .{ .tpoff, .TLS_TPREL },
    .{ .tlsdesc, .TLSDESC },
});

const riscv64_relocs = Table(11, elf.R_RISCV, .{
    .{ .none, .NONE },
    .{ .abs, .@"64" },
    .{ .copy, .COPY },
    .{ .rel, .RELATIVE },
    .{ .irel, .IRELATIVE },
    .{ .glob_dat, .@"64" },
    .{ .jump_slot, .JUMP_SLOT },
    .{ .dtpmod, .TLS_DTPMOD64 },
    .{ .dtpoff, .TLS_DTPREL64 },
    .{ .tpoff, .TLS_TPREL64 },
    .{ .tlsdesc, .TLSDESC },
});

pub fn decode(r_type: u32, cpu_arch: std.Target.Cpu.Arch) ?Kind {
    return switch (cpu_arch) {
        .x86_64 => x86_64_relocs.decode(r_type),
        .aarch64 => aarch64_relocs.decode(r_type),
        .riscv64 => riscv64_relocs.decode(r_type),
        else => @panic("TODO unhandled cpu arch"),
    };
}

pub fn encode(comptime kind: Kind, cpu_arch: std.Target.Cpu.Arch) u32 {
    return switch (cpu_arch) {
        .x86_64 => x86_64_relocs.encode(kind),
        .aarch64 => aarch64_relocs.encode(kind),
        .riscv64 => riscv64_relocs.encode(kind),
        else => @panic("TODO unhandled cpu arch"),
    };
}

const FormatRelocTypeCtx = struct {
    r_type: u32,
    cpu_arch: std.Target.Cpu.Arch,
};

pub fn fmtRelocType(r_type: u32, cpu_arch: std.Target.Cpu.Arch) std.fmt.Formatter(formatRelocType) {
    return .{ .data = .{
        .r_type = r_type,
        .cpu_arch = cpu_arch,
    } };
}

fn formatRelocType(
    ctx: FormatRelocTypeCtx,
    comptime unused_fmt_string: []const u8,
    options: std.fmt.FormatOptions,
    writer: anytype,
) !void {
    _ = unused_fmt_string;
    _ = options;
    const r_type = ctx.r_type;
    switch (r_type) {
        Elf.R_ZIG_GOT32 => try writer.writeAll("R_ZIG_GOT32"),
        Elf.R_ZIG_GOTPCREL => try writer.writeAll("R_ZIG_GOTPCREL"),
        else => switch (ctx.cpu_arch) {
            .x86_64 => try writer.print("R_X86_64_{s}", .{@tagName(@as(elf.R_X86_64, @enumFromInt(r_type)))}),
            .aarch64 => try writer.print("R_AARCH64_{s}", .{@tagName(@as(elf.R_AARCH64, @enumFromInt(r_type)))}),
            .riscv64 => try writer.print("R_RISCV_{s}", .{@tagName(@as(elf.R_RISCV, @enumFromInt(r_type)))}),
            else => unreachable,
        },
    }
}

const assert = std.debug.assert;
const elf = std.elf;
const std = @import("std");

const Elf = @import("../Elf.zig");