aboutsummaryrefslogtreecommitdiff
path: root/src/link/MachO/eh_frame.zig
blob: 1d3afaa3ac87c6c66f6fbfd6fdcc512380aa85f9 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
pub const Cie = struct {
    /// Includes 4byte size cell.
    offset: u32,
    out_offset: u32 = 0,
    size: u32,
    lsda_size: ?enum { p32, p64 } = null,
    personality: ?Personality = null,
    file: File.Index = 0,
    alive: bool = false,

    pub fn parse(cie: *Cie, macho_file: *MachO) !void {
        const tracy = trace(@src());
        defer tracy.end();

        const data = cie.getData(macho_file);
        const aug = std.mem.sliceTo(@as([*:0]const u8, @ptrCast(data.ptr + 9)), 0);

        if (aug[0] != 'z') return; // TODO should we error out?

        var reader: std.Io.Reader = .fixed(data[9 + aug.len + 1 ..]);

        _ = try reader.takeLeb128(u64); // code alignment factor
        _ = try reader.takeLeb128(u64); // data alignment factor
        _ = try reader.takeLeb128(u64); // return address register
        _ = try reader.takeLeb128(u64); // augmentation data length

        for (aug[1..]) |ch| switch (ch) {
            'R' => {
                const enc: DW.EH.PE = @bitCast(try reader.takeByte());
                if (enc != @as(DW.EH.PE, .{ .type = .absptr, .rel = .pcrel })) {
                    @panic("unexpected pointer encoding"); // TODO error
                }
            },
            'P' => {
                const enc: DW.EH.PE = @bitCast(try reader.takeByte());
                if (enc != @as(DW.EH.PE, .{ .type = .sdata4, .rel = .pcrel, .indirect = true })) {
                    @panic("unexpected personality pointer encoding"); // TODO error
                }
                _ = try reader.takeInt(u32, .little); // personality pointer
            },
            'L' => {
                const enc: DW.EH.PE = @bitCast(try reader.takeByte());
                switch (enc.type) {
                    .sdata4 => cie.lsda_size = .p32,
                    .absptr => cie.lsda_size = .p64,
                    else => @panic("unexpected lsda encoding"), // TODO error
                }
            },
            else => @panic("unexpected augmentation string"), // TODO error
        };
    }

    pub inline fn getSize(cie: Cie) u32 {
        return cie.size + 4;
    }

    pub fn getObject(cie: Cie, macho_file: *MachO) *Object {
        const file = macho_file.getFile(cie.file).?;
        return file.object;
    }

    pub fn getData(cie: Cie, macho_file: *MachO) []const u8 {
        const object = cie.getObject(macho_file);
        return object.eh_frame_data.items[cie.offset..][0..cie.getSize()];
    }

    pub fn getPersonality(cie: Cie, macho_file: *MachO) ?*Symbol {
        const personality = cie.personality orelse return null;
        const object = cie.getObject(macho_file);
        return object.getSymbolRef(personality.index, macho_file).getSymbol(macho_file);
    }

    pub fn eql(cie: Cie, other: Cie, macho_file: *MachO) bool {
        if (!std.mem.eql(u8, cie.getData(macho_file), other.getData(macho_file))) return false;
        if (cie.personality != null and other.personality != null) {
            if (cie.personality.?.index != other.personality.?.index) return false;
        }
        if (cie.personality != null or other.personality != null) return false;
        return true;
    }

    pub fn fmt(cie: Cie, macho_file: *MachO) std.fmt.Alt(Format, Format.default) {
        return .{ .data = .{
            .cie = cie,
            .macho_file = macho_file,
        } };
    }

    const Format = struct {
        cie: Cie,
        macho_file: *MachO,

        fn default(f: Format, w: *Writer) Writer.Error!void {
            const cie = f.cie;
            try w.print("@{x} : size({x})", .{
                cie.offset,
                cie.getSize(),
            });
            if (!cie.alive) try w.writeAll(" : [*]");
        }
    };

    pub const Index = u32;

    pub const Personality = struct {
        index: Symbol.Index = 0,
        offset: u32 = 0,
    };
};

pub const Fde = struct {
    /// Includes 4byte size cell.
    offset: u32,
    out_offset: u32 = 0,
    size: u32,
    cie: Cie.Index,
    atom: Atom.Index = 0,
    atom_offset: u32 = 0,
    lsda: Atom.Index = 0,
    lsda_offset: u32 = 0,
    lsda_ptr_offset: u32 = 0,
    file: File.Index = 0,
    alive: bool = true,

    pub fn parse(fde: *Fde, macho_file: *MachO) !void {
        const tracy = trace(@src());
        defer tracy.end();

        const data = fde.getData(macho_file);
        const object = fde.getObject(macho_file);
        const sect = object.sections.items(.header)[object.eh_frame_sect_index.?];

        // Parse target atom index
        const pc_begin = std.mem.readInt(i64, data[8..][0..8], .little);
        const taddr: u64 = @intCast(@as(i64, @intCast(sect.addr + fde.offset + 8)) + pc_begin);
        fde.atom = object.findAtom(taddr) orelse {
            try macho_file.reportParseError2(object.index, "{s},{s}: 0x{x}: invalid function reference in FDE", .{
                sect.segName(), sect.sectName(), fde.offset + 8,
            });
            return error.MalformedObject;
        };
        const atom = fde.getAtom(macho_file);
        fde.atom_offset = @intCast(taddr - atom.getInputAddress(macho_file));

        // Associate with a CIE
        const cie_ptr = std.mem.readInt(u32, data[4..8], .little);
        const cie_offset = fde.offset + 4 - cie_ptr;
        const cie_index = for (object.cies.items, 0..) |cie, cie_index| {
            if (cie.offset == cie_offset) break @as(Cie.Index, @intCast(cie_index));
        } else null;
        if (cie_index) |cie| {
            fde.cie = cie;
        } else {
            try macho_file.reportParseError2(object.index, "no matching CIE found for FDE at offset {x}", .{
                fde.offset,
            });
            return error.MalformedObject;
        }

        const cie = fde.getCie(macho_file);

        // Parse LSDA atom index if any
        if (cie.lsda_size) |lsda_size| {
            var reader: std.Io.Reader = .fixed(data);
            reader.seek = 24;
            _ = try reader.takeLeb128(u64); // augmentation length
            fde.lsda_ptr_offset = @intCast(reader.seek);
            const lsda_ptr = switch (lsda_size) {
                .p32 => try reader.takeInt(i32, .little),
                .p64 => try reader.takeInt(i64, .little),
            };
            const lsda_addr: u64 = @intCast(@as(i64, @intCast(sect.addr + fde.offset + fde.lsda_ptr_offset)) + lsda_ptr);
            fde.lsda = object.findAtom(lsda_addr) orelse {
                try macho_file.reportParseError2(object.index, "{s},{s}: 0x{x}: invalid LSDA reference in FDE", .{
                    sect.segName(), sect.sectName(), fde.offset + fde.lsda_ptr_offset,
                });
                return error.MalformedObject;
            };
            const lsda_atom = fde.getLsdaAtom(macho_file).?;
            fde.lsda_offset = @intCast(lsda_addr - lsda_atom.getInputAddress(macho_file));
        }
    }

    pub inline fn getSize(fde: Fde) u32 {
        return fde.size + 4;
    }

    pub fn getObject(fde: Fde, macho_file: *MachO) *Object {
        const file = macho_file.getFile(fde.file).?;
        return file.object;
    }

    pub fn getData(fde: Fde, macho_file: *MachO) []const u8 {
        const object = fde.getObject(macho_file);
        return object.eh_frame_data.items[fde.offset..][0..fde.getSize()];
    }

    pub fn getCie(fde: Fde, macho_file: *MachO) *const Cie {
        const object = fde.getObject(macho_file);
        return &object.cies.items[fde.cie];
    }

    pub fn getAtom(fde: Fde, macho_file: *MachO) *Atom {
        return fde.getObject(macho_file).getAtom(fde.atom).?;
    }

    pub fn getLsdaAtom(fde: Fde, macho_file: *MachO) ?*Atom {
        return fde.getObject(macho_file).getAtom(fde.lsda);
    }

    pub fn fmt(fde: Fde, macho_file: *MachO) std.fmt.Alt(Format, Format.default) {
        return .{ .data = .{
            .fde = fde,
            .macho_file = macho_file,
        } };
    }

    const Format = struct {
        fde: Fde,
        macho_file: *MachO,

        fn default(f: Format, writer: *Writer) Writer.Error!void {
            const fde = f.fde;
            const macho_file = f.macho_file;
            try writer.print("@{x} : size({x}) : cie({d}) : {s}", .{
                fde.offset,
                fde.getSize(),
                fde.cie,
                fde.getAtom(macho_file).getName(macho_file),
            });
            if (!fde.alive) try writer.writeAll(" : [*]");
        }
    };

    pub const Index = u32;
};

pub const Iterator = struct {
    data: []const u8,
    pos: u32 = 0,

    pub const Record = struct {
        tag: enum { fde, cie },
        offset: u32,
        size: u32,
    };

    pub fn next(it: *Iterator) !?Record {
        if (it.pos >= it.data.len) return null;

        var reader: std.Io.Reader = .fixed(it.data[it.pos..]);

        const size = try reader.takeInt(u32, .little);
        if (size == 0xFFFFFFFF) @panic("DWARF CFI is 32bit on macOS");

        const id = try reader.takeInt(u32, .little);
        const record = Record{
            .tag = if (id == 0) .cie else .fde,
            .offset = it.pos,
            .size = size,
        };
        it.pos += size + 4;

        return record;
    }
};

pub fn calcSize(macho_file: *MachO) !u32 {
    const tracy = trace(@src());
    defer tracy.end();

    var offset: u32 = 0;

    var cies = std.array_list.Managed(Cie).init(macho_file.base.comp.gpa);
    defer cies.deinit();

    for (macho_file.objects.items) |index| {
        const object = macho_file.getFile(index).?.object;

        outer: for (object.cies.items) |*cie| {
            for (cies.items) |other| {
                if (other.eql(cie.*, macho_file)) {
                    // We already have a CIE record that has the exact same contents, so instead of
                    // duplicating them, we mark this one dead and set its output offset to be
                    // equal to that of the alive record. This way, we won't have to rewrite
                    // Fde.cie_index field when committing the records to file.
                    cie.out_offset = other.out_offset;
                    continue :outer;
                }
            }
            cie.alive = true;
            cie.out_offset = offset;
            offset += cie.getSize();
            try cies.append(cie.*);
        }
    }

    for (macho_file.objects.items) |index| {
        const object = macho_file.getFile(index).?.object;
        for (object.fdes.items) |*fde| {
            if (!fde.alive) continue;
            fde.out_offset = offset;
            offset += fde.getSize();
        }
    }

    return offset;
}

pub fn calcNumRelocs(macho_file: *MachO) u32 {
    const tracy = trace(@src());
    defer tracy.end();

    var nreloc: u32 = 0;

    for (macho_file.objects.items) |index| {
        const object = macho_file.getFile(index).?.object;
        for (object.cies.items) |cie| {
            if (!cie.alive) continue;
            if (cie.getPersonality(macho_file)) |_| {
                nreloc += 1; // personality
            }
        }
    }

    return nreloc;
}

pub fn write(macho_file: *MachO, buffer: []u8) void {
    const tracy = trace(@src());
    defer tracy.end();

    const sect = macho_file.sections.items(.header)[macho_file.eh_frame_sect_index.?];
    const addend: i64 = switch (macho_file.getTarget().cpu.arch) {
        .x86_64 => 4,
        else => 0,
    };

    for (macho_file.objects.items) |index| {
        const object = macho_file.getFile(index).?.object;
        for (object.cies.items) |cie| {
            if (!cie.alive) continue;

            @memcpy(buffer[cie.out_offset..][0..cie.getSize()], cie.getData(macho_file));

            if (cie.getPersonality(macho_file)) |sym| {
                const offset = cie.out_offset + cie.personality.?.offset;
                const saddr = sect.addr + offset;
                const taddr = sym.getGotAddress(macho_file);
                std.mem.writeInt(
                    i32,
                    buffer[offset..][0..4],
                    @intCast(@as(i64, @intCast(taddr)) - @as(i64, @intCast(saddr)) + addend),
                    .little,
                );
            }
        }
    }

    for (macho_file.objects.items) |index| {
        const object = macho_file.getFile(index).?.object;
        for (object.fdes.items) |fde| {
            if (!fde.alive) continue;

            @memcpy(buffer[fde.out_offset..][0..fde.getSize()], fde.getData(macho_file));

            {
                const offset = fde.out_offset + 4;
                const value = offset - fde.getCie(macho_file).out_offset;
                std.mem.writeInt(u32, buffer[offset..][0..4], value, .little);
            }

            {
                const offset = fde.out_offset + 8;
                const saddr = sect.addr + offset;
                const taddr = fde.getAtom(macho_file).getAddress(macho_file);
                std.mem.writeInt(
                    i64,
                    buffer[offset..][0..8],
                    @as(i64, @intCast(taddr)) - @as(i64, @intCast(saddr)),
                    .little,
                );
            }

            if (fde.getLsdaAtom(macho_file)) |atom| {
                const offset = fde.out_offset + fde.lsda_ptr_offset;
                const saddr = sect.addr + offset;
                const taddr = atom.getAddress(macho_file) + fde.lsda_offset;
                switch (fde.getCie(macho_file).lsda_size.?) {
                    .p32 => std.mem.writeInt(
                        i32,
                        buffer[offset..][0..4],
                        @intCast(@as(i64, @intCast(taddr)) - @as(i64, @intCast(saddr)) + addend),
                        .little,
                    ),
                    .p64 => std.mem.writeInt(
                        i64,
                        buffer[offset..][0..8],
                        @as(i64, @intCast(taddr)) - @as(i64, @intCast(saddr)),
                        .little,
                    ),
                }
            }
        }
    }
}

pub fn writeRelocs(macho_file: *MachO, code: []u8, relocs: []macho.relocation_info) error{Overflow}!void {
    const tracy = trace(@src());
    defer tracy.end();

    const cpu_arch = macho_file.getTarget().cpu.arch;
    const sect = macho_file.sections.items(.header)[macho_file.eh_frame_sect_index.?];
    const addend: i64 = switch (cpu_arch) {
        .x86_64 => 4,
        else => 0,
    };

    var i: usize = 0;
    for (macho_file.objects.items) |index| {
        const object = macho_file.getFile(index).?.object;
        for (object.cies.items) |cie| {
            if (!cie.alive) continue;

            @memcpy(code[cie.out_offset..][0..cie.getSize()], cie.getData(macho_file));

            if (cie.getPersonality(macho_file)) |sym| {
                const r_address = math.cast(i32, cie.out_offset + cie.personality.?.offset) orelse return error.Overflow;
                const r_symbolnum = math.cast(u24, sym.getOutputSymtabIndex(macho_file).?) orelse return error.Overflow;
                relocs[i] = .{
                    .r_address = r_address,
                    .r_symbolnum = r_symbolnum,
                    .r_length = 2,
                    .r_extern = 1,
                    .r_pcrel = 1,
                    .r_type = switch (cpu_arch) {
                        .aarch64 => @intFromEnum(macho.reloc_type_arm64.ARM64_RELOC_POINTER_TO_GOT),
                        .x86_64 => @intFromEnum(macho.reloc_type_x86_64.X86_64_RELOC_GOT),
                        else => unreachable,
                    },
                };
                i += 1;
            }
        }
    }

    for (macho_file.objects.items) |index| {
        const object = macho_file.getFile(index).?.object;
        for (object.fdes.items) |fde| {
            if (!fde.alive) continue;

            @memcpy(code[fde.out_offset..][0..fde.getSize()], fde.getData(macho_file));

            {
                const offset = fde.out_offset + 4;
                const value = offset - fde.getCie(macho_file).out_offset;
                std.mem.writeInt(u32, code[offset..][0..4], value, .little);
            }

            {
                const offset = fde.out_offset + 8;
                const saddr = sect.addr + offset;
                const taddr = fde.getAtom(macho_file).getAddress(macho_file);
                std.mem.writeInt(
                    i64,
                    code[offset..][0..8],
                    @as(i64, @intCast(taddr)) - @as(i64, @intCast(saddr)),
                    .little,
                );
            }

            if (fde.getLsdaAtom(macho_file)) |atom| {
                const offset = fde.out_offset + fde.lsda_ptr_offset;
                const saddr = sect.addr + offset;
                const taddr = atom.getAddress(macho_file) + fde.lsda_offset;
                switch (fde.getCie(macho_file).lsda_size.?) {
                    .p32 => std.mem.writeInt(
                        i32,
                        code[offset..][0..4],
                        @intCast(@as(i64, @intCast(taddr)) - @as(i64, @intCast(saddr)) + addend),
                        .little,
                    ),
                    .p64 => std.mem.writeInt(
                        i64,
                        code[offset..][0..8],
                        @as(i64, @intCast(taddr)) - @as(i64, @intCast(saddr)),
                        .little,
                    ),
                }
            }
        }
    }

    assert(relocs.len == i);
}

const assert = std.debug.assert;
const leb = std.leb;
const macho = std.macho;
const math = std.math;
const mem = std.mem;
const std = @import("std");
const trace = @import("../../tracy.zig").trace;
const Writer = std.Io.Writer;

const Allocator = std.mem.Allocator;
const Atom = @import("Atom.zig");
const DW = std.dwarf;
const File = @import("file.zig").File;
const MachO = @import("../MachO.zig");
const Object = @import("Object.zig");
const Symbol = @import("Symbol.zig");