| ... | @@ -0,0 +1,231 @@ |
| 1 | const std = @import("std"); |
| 2 | const Allocator = std.mem.Allocator; |
| 3 | const target_util = @import("target.zig"); |
| 4 | const mem = std.mem; |
| 5 | |
| 6 | pub const Lib = struct { |
| 7 | name: []const u8, |
| 8 | sover: u8, |
| 9 | }; |
| 10 | |
| 11 | pub const Fn = struct { |
| 12 | name: []const u8, |
| 13 | lib: *const Lib, |
| 14 | }; |
| 15 | |
| 16 | pub const VerList = struct { |
| 17 | /// 7 is just the max number, we know statically it's big enough. |
| 18 | versions: [7]u8, |
| 19 | len: u8, |
| 20 | }; |
| 21 | |
| 22 | pub const ABI = struct { |
| 23 | all_versions: []const std.builtin.Version, |
| 24 | all_functions: []const Fn, |
| 25 | /// The value is a pointer to all_functions.len items and each item is an index into all_functions. |
| 26 | version_table: std.AutoHashMapUnmanaged(target_util.ArchOsAbi, [*]VerList), |
| 27 | arena_state: std.heap.ArenaAllocator.State, |
| 28 | |
| 29 | pub fn destroy(abi: *ABI, gpa: *Allocator) void { |
| 30 | abi.version_table.deinit(gpa); |
| 31 | abi.arena_state.promote(gpa).deinit(); // Frees the ABI memory too. |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | pub const libs = [_]Lib{ |
| 36 | .{ .name = "c", .sover = 6 }, |
| 37 | .{ .name = "m", .sover = 6 }, |
| 38 | .{ .name = "pthread", .sover = 0 }, |
| 39 | .{ .name = "dl", .sover = 2 }, |
| 40 | .{ .name = "rt", .sover = 1 }, |
| 41 | .{ .name = "ld", .sover = 2 }, |
| 42 | .{ .name = "util", .sover = 1 }, |
| 43 | }; |
| 44 | |
| 45 | pub const LoadMetaDataError = error{ |
| 46 | /// The files that ship with the Zig compiler were unable to be read, or otherwise had malformed data. |
| 47 | ZigInstallationCorrupt, |
| 48 | OutOfMemory, |
| 49 | }; |
| 50 | |
| 51 | /// This function will emit a log error when there is a problem with the zig installation and then return |
| 52 | /// `error.ZigInstallationCorrupt`. |
| 53 | pub fn loadMetaData(gpa: *Allocator, zig_lib_dir: std.fs.Dir) LoadMetaDataError!*ABI { |
| 54 | var arena_allocator = std.heap.ArenaAllocator.init(gpa); |
| 55 | errdefer arena_allocator.deinit(); |
| 56 | const arena = &arena_allocator.allocator; |
| 57 | |
| 58 | var all_versions = std.ArrayListUnmanaged(std.builtin.Version){}; |
| 59 | var all_functions = std.ArrayListUnmanaged(Fn){}; |
| 60 | var version_table = std.AutoHashMapUnmanaged(target_util.ArchOsAbi, [*]VerList){}; |
| 61 | errdefer version_table.deinit(gpa); |
| 62 | |
| 63 | var glibc_dir = zig_lib_dir.openDir("libc" ++ std.fs.path.sep_str ++ "glibc", .{}) catch |err| { |
| 64 | std.log.err("unable to open glibc dir: {}", .{@errorName(err)}); |
| 65 | return error.ZigInstallationCorrupt; |
| 66 | }; |
| 67 | defer glibc_dir.close(); |
| 68 | |
| 69 | const max_txt_size = 500 * 1024; // Bigger than this and something is definitely borked. |
| 70 | const vers_txt_contents = glibc_dir.readFileAlloc(gpa, "vers.txt", max_txt_size) catch |err| switch (err) { |
| 71 | error.OutOfMemory => return error.OutOfMemory, |
| 72 | else => { |
| 73 | std.log.err("unable to read vers.txt: {}", .{@errorName(err)}); |
| 74 | return error.ZigInstallationCorrupt; |
| 75 | }, |
| 76 | }; |
| 77 | defer gpa.free(vers_txt_contents); |
| 78 | |
| 79 | const fns_txt_contents = glibc_dir.readFileAlloc(gpa, "fns.txt", max_txt_size) catch |err| switch (err) { |
| 80 | error.OutOfMemory => return error.OutOfMemory, |
| 81 | else => { |
| 82 | std.log.err("unable to read fns.txt: {}", .{@errorName(err)}); |
| 83 | return error.ZigInstallationCorrupt; |
| 84 | }, |
| 85 | }; |
| 86 | defer gpa.free(fns_txt_contents); |
| 87 | |
| 88 | const abi_txt_contents = glibc_dir.readFileAlloc(gpa, "abi.txt", max_txt_size) catch |err| switch (err) { |
| 89 | error.OutOfMemory => return error.OutOfMemory, |
| 90 | else => { |
| 91 | std.log.err("unable to read abi.txt: {}", .{@errorName(err)}); |
| 92 | return error.ZigInstallationCorrupt; |
| 93 | }, |
| 94 | }; |
| 95 | defer gpa.free(abi_txt_contents); |
| 96 | |
| 97 | { |
| 98 | var it = mem.tokenize(vers_txt_contents, "\r\n"); |
| 99 | var line_i: usize = 1; |
| 100 | while (it.next()) |line| : (line_i += 1) { |
| 101 | const prefix = "GLIBC_"; |
| 102 | if (!mem.startsWith(u8, line, prefix)) { |
| 103 | std.log.err("vers.txt:{}: expected 'GLIBC_' prefix", .{line_i}); |
| 104 | return error.ZigInstallationCorrupt; |
| 105 | } |
| 106 | const adjusted_line = line[prefix.len..]; |
| 107 | const ver = std.builtin.Version.parse(adjusted_line) catch |err| { |
| 108 | std.log.err("vers.txt:{}: unable to parse glibc version '{}': {}", .{ line_i, line, @errorName(err) }); |
| 109 | return error.ZigInstallationCorrupt; |
| 110 | }; |
| 111 | try all_versions.append(arena, ver); |
| 112 | } |
| 113 | } |
| 114 | { |
| 115 | var file_it = mem.tokenize(fns_txt_contents, "\r\n"); |
| 116 | var line_i: usize = 1; |
| 117 | while (file_it.next()) |line| : (line_i += 1) { |
| 118 | var line_it = mem.tokenize(line, " "); |
| 119 | const fn_name = line_it.next() orelse { |
| 120 | std.log.err("fns.txt:{}: expected function name", .{line_i}); |
| 121 | return error.ZigInstallationCorrupt; |
| 122 | }; |
| 123 | const lib_name = line_it.next() orelse { |
| 124 | std.log.err("fns.txt:{}: expected library name", .{line_i}); |
| 125 | return error.ZigInstallationCorrupt; |
| 126 | }; |
| 127 | const lib = findLib(lib_name) orelse { |
| 128 | std.log.err("fns.txt:{}: unknown library name: {}", .{ line_i, lib_name }); |
| 129 | return error.ZigInstallationCorrupt; |
| 130 | }; |
| 131 | try all_functions.append(arena, .{ |
| 132 | .name = fn_name, |
| 133 | .lib = lib, |
| 134 | }); |
| 135 | } |
| 136 | } |
| 137 | { |
| 138 | var file_it = mem.split(abi_txt_contents, "\n"); |
| 139 | var line_i: usize = 0; |
| 140 | while (true) { |
| 141 | const ver_list_base: []VerList = blk: { |
| 142 | const line = file_it.next() orelse break; |
| 143 | if (line.len == 0) break; |
| 144 | line_i += 1; |
| 145 | const ver_list_base = try arena.alloc(VerList, all_functions.items.len); |
| 146 | var line_it = mem.tokenize(line, " "); |
| 147 | while (line_it.next()) |target_string| { |
| 148 | var component_it = mem.tokenize(target_string, "-"); |
| 149 | const arch_name = component_it.next() orelse { |
| 150 | std.log.err("abi.txt:{}: expected arch name", .{line_i}); |
| 151 | return error.ZigInstallationCorrupt; |
| 152 | }; |
| 153 | const os_name = component_it.next() orelse { |
| 154 | std.log.err("abi.txt:{}: expected OS name", .{line_i}); |
| 155 | return error.ZigInstallationCorrupt; |
| 156 | }; |
| 157 | const abi_name = component_it.next() orelse { |
| 158 | std.log.err("abi.txt:{}: expected ABI name", .{line_i}); |
| 159 | return error.ZigInstallationCorrupt; |
| 160 | }; |
| 161 | const arch_tag = std.meta.stringToEnum(std.Target.Cpu.Arch, arch_name) orelse { |
| 162 | std.log.err("abi.txt:{}: unrecognized arch: '{}'", .{ line_i, arch_name }); |
| 163 | return error.ZigInstallationCorrupt; |
| 164 | }; |
| 165 | if (!mem.eql(u8, os_name, "linux")) { |
| 166 | std.log.err("abi.txt:{}: expected OS 'linux', found '{}'", .{ line_i, os_name }); |
| 167 | return error.ZigInstallationCorrupt; |
| 168 | } |
| 169 | const abi_tag = std.meta.stringToEnum(std.Target.Abi, abi_name) orelse { |
| 170 | std.log.err("abi.txt:{}: unrecognized ABI: '{}'", .{ line_i, abi_name }); |
| 171 | return error.ZigInstallationCorrupt; |
| 172 | }; |
| 173 | |
| 174 | const triple = target_util.ArchOsAbi{ |
| 175 | .arch = arch_tag, |
| 176 | .os = .linux, |
| 177 | .abi = abi_tag, |
| 178 | }; |
| 179 | try version_table.put(arena, triple, ver_list_base.ptr); |
| 180 | } |
| 181 | break :blk ver_list_base; |
| 182 | }; |
| 183 | for (ver_list_base) |*ver_list| { |
| 184 | const line = file_it.next() orelse { |
| 185 | std.log.err("abi.txt:{}: missing version number line", .{line_i}); |
| 186 | return error.ZigInstallationCorrupt; |
| 187 | }; |
| 188 | line_i += 1; |
| 189 | |
| 190 | ver_list.* = .{ |
| 191 | .versions = undefined, |
| 192 | .len = 0, |
| 193 | }; |
| 194 | var line_it = mem.tokenize(line, " "); |
| 195 | while (line_it.next()) |version_index_string| { |
| 196 | if (ver_list.len >= ver_list.versions.len) { |
| 197 | // If this happens with legit data, increase the array len in the type. |
| 198 | std.log.err("abi.txt:{}: too many versions", .{line_i}); |
| 199 | return error.ZigInstallationCorrupt; |
| 200 | } |
| 201 | const version_index = std.fmt.parseInt(u8, version_index_string, 10) catch |err| { |
| 202 | // If this happens with legit data, increase the size of the integer type in the struct. |
| 203 | std.log.err("abi.txt:{}: unable to parse version: {}", .{ line_i, @errorName(err) }); |
| 204 | return error.ZigInstallationCorrupt; |
| 205 | }; |
| 206 | |
| 207 | ver_list.versions[ver_list.len] = version_index; |
| 208 | ver_list.len += 1; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | const abi = try arena.create(ABI); |
| 215 | abi.* = .{ |
| 216 | .all_versions = all_versions.items, |
| 217 | .all_functions = all_functions.items, |
| 218 | .version_table = version_table, |
| 219 | .arena_state = arena_allocator.state, |
| 220 | }; |
| 221 | return abi; |
| 222 | } |
| 223 | |
| 224 | fn findLib(name: []const u8) ?*const Lib { |
| 225 | for (libs) |*lib| { |
| 226 | if (mem.eql(u8, lib.name, name)) { |
| 227 | return lib; |
| 228 | } |
| 229 | } |
| 230 | return null; |
| 231 | } |