aboutsummaryrefslogtreecommitdiff
path: root/src/link/MachO/Dylib.zig
blob: 8d864454f838257feeec1d92ec37b9551f82c75d (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
const Dylib = @This();

const std = @import("std");
const fs = std.fs;
const log = std.log.scoped(.dylib);
const macho = std.macho;
const mem = std.mem;

const Allocator = mem.Allocator;
const Symbol = @import("Symbol.zig");

usingnamespace @import("commands.zig");

allocator: *Allocator,
arch: ?std.Target.Cpu.Arch = null,
header: ?macho.mach_header_64 = null,
file: ?fs.File = null,
name: ?[]const u8 = null,

ordinal: ?u16 = null,

load_commands: std.ArrayListUnmanaged(LoadCommand) = .{},

symtab_cmd_index: ?u16 = null,
dysymtab_cmd_index: ?u16 = null,
id_cmd_index: ?u16 = null,

id: ?Id = null,

symbols: std.StringArrayHashMapUnmanaged(*Symbol) = .{},

pub const Id = struct {
    name: []const u8,
    timestamp: u32,
    current_version: u32,
    compatibility_version: u32,

    pub fn deinit(id: *Id, allocator: *Allocator) void {
        allocator.free(id.name);
    }
};

pub fn init(allocator: *Allocator) Dylib {
    return .{ .allocator = allocator };
}

pub fn deinit(self: *Dylib) void {
    for (self.load_commands.items) |*lc| {
        lc.deinit(self.allocator);
    }
    self.load_commands.deinit(self.allocator);

    for (self.symbols.items()) |entry| {
        entry.value.deinit(self.allocator);
        self.allocator.destroy(entry.value);
    }
    self.symbols.deinit(self.allocator);

    if (self.name) |name| {
        self.allocator.free(name);
    }

    if (self.id) |*id| {
        id.deinit(self.allocator);
    }
}

pub fn closeFile(self: Dylib) void {
    if (self.file) |file| {
        file.close();
    }
}

pub fn parse(self: *Dylib) !void {
    log.debug("parsing shared library '{s}'", .{self.name.?});

    var reader = self.file.?.reader();
    self.header = try reader.readStruct(macho.mach_header_64);

    if (self.header.?.filetype != macho.MH_DYLIB) {
        log.err("invalid filetype: expected 0x{x}, found 0x{x}", .{ macho.MH_DYLIB, self.header.?.filetype });
        return error.MalformedDylib;
    }

    const this_arch: std.Target.Cpu.Arch = switch (self.header.?.cputype) {
        macho.CPU_TYPE_ARM64 => .aarch64,
        macho.CPU_TYPE_X86_64 => .x86_64,
        else => |value| {
            log.err("unsupported cpu architecture 0x{x}", .{value});
            return error.UnsupportedCpuArchitecture;
        },
    };
    if (this_arch != self.arch.?) {
        log.err("mismatched cpu architecture: expected {s}, found {s}", .{ self.arch.?, this_arch });
        return error.MismatchedCpuArchitecture;
    }

    try self.readLoadCommands(reader);
    try self.parseId();
    try self.parseSymbols();
}

pub fn readLoadCommands(self: *Dylib, reader: anytype) !void {
    try self.load_commands.ensureCapacity(self.allocator, self.header.?.ncmds);

    var i: u16 = 0;
    while (i < self.header.?.ncmds) : (i += 1) {
        var cmd = try LoadCommand.read(self.allocator, reader);
        switch (cmd.cmd()) {
            macho.LC_SYMTAB => {
                self.symtab_cmd_index = i;
            },
            macho.LC_DYSYMTAB => {
                self.dysymtab_cmd_index = i;
            },
            macho.LC_ID_DYLIB => {
                self.id_cmd_index = i;
            },
            else => {
                log.debug("Unknown load command detected: 0x{x}.", .{cmd.cmd()});
            },
        }
        self.load_commands.appendAssumeCapacity(cmd);
    }
}

pub fn parseId(self: *Dylib) !void {
    const index = self.id_cmd_index orelse {
        log.debug("no LC_ID_DYLIB load command found; using hard-coded defaults...", .{});
        self.id = .{
            .name = try self.allocator.dupe(u8, self.name.?),
            .timestamp = 2,
            .current_version = 0,
            .compatibility_version = 0,
        };
        return;
    };
    const id_cmd = self.load_commands.items[index].Dylib;
    const dylib = id_cmd.inner.dylib;

    // TODO should we compare the name from the dylib's id with the user-specified one?
    const dylib_name = @ptrCast([*:0]const u8, id_cmd.data[dylib.name - @sizeOf(macho.dylib_command) ..]);
    const name = try self.allocator.dupe(u8, mem.spanZ(dylib_name));

    self.id = .{
        .name = name,
        .timestamp = dylib.timestamp,
        .current_version = dylib.current_version,
        .compatibility_version = dylib.compatibility_version,
    };
}

pub fn parseSymbols(self: *Dylib) !void {
    const index = self.symtab_cmd_index orelse return;
    const symtab_cmd = self.load_commands.items[index].Symtab;

    var symtab = try self.allocator.alloc(u8, @sizeOf(macho.nlist_64) * symtab_cmd.nsyms);
    defer self.allocator.free(symtab);
    _ = try self.file.?.preadAll(symtab, symtab_cmd.symoff);
    const slice = @alignCast(@alignOf(macho.nlist_64), mem.bytesAsSlice(macho.nlist_64, symtab));

    var strtab = try self.allocator.alloc(u8, symtab_cmd.strsize);
    defer self.allocator.free(strtab);
    _ = try self.file.?.preadAll(strtab, symtab_cmd.stroff);

    for (slice) |sym| {
        const sym_name = mem.spanZ(@ptrCast([*:0]const u8, strtab.ptr + sym.n_strx));

        if (!(Symbol.isSect(sym) and Symbol.isExt(sym))) continue;

        const name = try self.allocator.dupe(u8, sym_name);
        const proxy = try self.allocator.create(Symbol.Proxy);
        errdefer self.allocator.destroy(proxy);

        proxy.* = .{
            .base = .{
                .@"type" = .proxy,
                .name = name,
            },
            .dylib = self,
        };

        try self.symbols.putNoClobber(self.allocator, name, &proxy.base);
    }
}