| ... | @@ -37,6 +37,8 @@ const LlvmObject = @import("../codegen/llvm.zig").Object; | ... | @@ -37,6 +37,8 @@ const LlvmObject = @import("../codegen/llvm.zig").Object; |
| 37 | const LoadCommand = commands.LoadCommand; | 37 | const LoadCommand = commands.LoadCommand; |
| 38 | const Module = @import("../Module.zig"); | 38 | const Module = @import("../Module.zig"); |
| 39 | const SegmentCommand = commands.SegmentCommand; | 39 | const SegmentCommand = commands.SegmentCommand; |
| | 40 | const StringIndexAdapter = std.hash_map.StringIndexAdapter; |
| | 41 | const StringIndexContext = std.hash_map.StringIndexContext; |
| 40 | pub const TextBlock = @import("MachO/TextBlock.zig"); | 42 | pub const TextBlock = @import("MachO/TextBlock.zig"); |
| 41 | const Trie = @import("MachO/Trie.zig"); | 43 | const Trie = @import("MachO/Trie.zig"); |
| 42 | | 44 | |
| ... | @@ -224,33 +226,6 @@ decls: std.AutoArrayHashMapUnmanaged(*Module.Decl, void) = .{}, | ... | @@ -224,33 +226,6 @@ decls: std.AutoArrayHashMapUnmanaged(*Module.Decl, void) = .{}, |
| 224 | /// somewhere else in the codegen. | 226 | /// somewhere else in the codegen. |
| 225 | active_decl: ?*Module.Decl = null, | 227 | active_decl: ?*Module.Decl = null, |
| 226 | | 228 | |
| 227 | const StringIndexContext = struct { | | |
| 228 | strtab: *std.ArrayListUnmanaged(u8), | | |
| 229 | | | |
| 230 | pub fn eql(_: StringIndexContext, a: u32, b: u32) bool { | | |
| 231 | return a == b; | | |
| 232 | } | | |
| 233 | | | |
| 234 | pub fn hash(self: StringIndexContext, x: u32) u64 { | | |
| 235 | const x_slice = mem.spanZ(@ptrCast([*:0]const u8, self.strtab.items.ptr) + x); | | |
| 236 | return std.hash_map.hashString(x_slice); | | |
| 237 | } | | |
| 238 | }; | | |
| 239 | | | |
| 240 | pub const StringSliceAdapter = struct { | | |
| 241 | strtab: *std.ArrayListUnmanaged(u8), | | |
| 242 | | | |
| 243 | pub fn eql(self: StringSliceAdapter, a_slice: []const u8, b: u32) bool { | | |
| 244 | const b_slice = mem.spanZ(@ptrCast([*:0]const u8, self.strtab.items.ptr) + b); | | |
| 245 | return mem.eql(u8, a_slice, b_slice); | | |
| 246 | } | | |
| 247 | | | |
| 248 | pub fn hash(self: StringSliceAdapter, adapted_key: []const u8) u64 { | | |
| 249 | _ = self; | | |
| 250 | return std.hash_map.hashString(adapted_key); | | |
| 251 | } | | |
| 252 | }; | | |
| 253 | | | |
| 254 | const SymbolWithLoc = struct { | 229 | const SymbolWithLoc = struct { |
| 255 | // Table where the symbol can be found. | 230 | // Table where the symbol can be found. |
| 256 | where: enum { | 231 | where: enum { |
| ... | @@ -938,8 +913,8 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void { | ... | @@ -938,8 +913,8 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void { |
| 938 | | 913 | |
| 939 | { | 914 | { |
| 940 | // Add dyld_stub_binder as the final GOT entry. | 915 | // Add dyld_stub_binder as the final GOT entry. |
| 941 | const n_strx = self.strtab_dir.getKeyAdapted(@as([]const u8, "dyld_stub_binder"), StringSliceAdapter{ | 916 | const n_strx = self.strtab_dir.getKeyAdapted(@as([]const u8, "dyld_stub_binder"), StringIndexAdapter{ |
| 942 | .strtab = &self.strtab, | 917 | .bytes = &self.strtab, |
| 943 | }) orelse unreachable; | 918 | }) orelse unreachable; |
| 944 | const resolv = self.symbol_resolver.get(n_strx) orelse unreachable; | 919 | const resolv = self.symbol_resolver.get(n_strx) orelse unreachable; |
| 945 | const got_index = @intCast(u32, self.got_entries.items.len); | 920 | const got_index = @intCast(u32, self.got_entries.items.len); |
| ... | @@ -1966,8 +1941,8 @@ fn writeStubHelperCommon(self: *MachO) !void { | ... | @@ -1966,8 +1941,8 @@ fn writeStubHelperCommon(self: *MachO) !void { |
| 1966 | code[9] = 0xff; | 1941 | code[9] = 0xff; |
| 1967 | code[10] = 0x25; | 1942 | code[10] = 0x25; |
| 1968 | { | 1943 | { |
| 1969 | const n_strx = self.strtab_dir.getKeyAdapted(@as([]const u8, "dyld_stub_binder"), StringSliceAdapter{ | 1944 | const n_strx = self.strtab_dir.getKeyAdapted(@as([]const u8, "dyld_stub_binder"), StringIndexAdapter{ |
| 1970 | .strtab = &self.strtab, | 1945 | .bytes = &self.strtab, |
| 1971 | }) orelse unreachable; | 1946 | }) orelse unreachable; |
| 1972 | const resolv = self.symbol_resolver.get(n_strx) orelse unreachable; | 1947 | const resolv = self.symbol_resolver.get(n_strx) orelse unreachable; |
| 1973 | const got_index = self.got_entries_map.get(.{ | 1948 | const got_index = self.got_entries_map.get(.{ |
| ... | @@ -2017,8 +1992,8 @@ fn writeStubHelperCommon(self: *MachO) !void { | ... | @@ -2017,8 +1992,8 @@ fn writeStubHelperCommon(self: *MachO) !void { |
| 2017 | code[10] = 0xbf; | 1992 | code[10] = 0xbf; |
| 2018 | code[11] = 0xa9; | 1993 | code[11] = 0xa9; |
| 2019 | binder_blk_outer: { | 1994 | binder_blk_outer: { |
| 2020 | const n_strx = self.strtab_dir.getKeyAdapted(@as([]const u8, "dyld_stub_binder"), StringSliceAdapter{ | 1995 | const n_strx = self.strtab_dir.getKeyAdapted(@as([]const u8, "dyld_stub_binder"), StringIndexAdapter{ |
| 2021 | .strtab = &self.strtab, | 1996 | .bytes = &self.strtab, |
| 2022 | }) orelse unreachable; | 1997 | }) orelse unreachable; |
| 2023 | const resolv = self.symbol_resolver.get(n_strx) orelse unreachable; | 1998 | const resolv = self.symbol_resolver.get(n_strx) orelse unreachable; |
| 2024 | const got_index = self.got_entries_map.get(.{ | 1999 | const got_index = self.got_entries_map.get(.{ |
| ... | @@ -2435,8 +2410,8 @@ fn resolveSymbols(self: *MachO) !void { | ... | @@ -2435,8 +2410,8 @@ fn resolveSymbols(self: *MachO) !void { |
| 2435 | } | 2410 | } |
| 2436 | | 2411 | |
| 2437 | // Fourth pass, handle synthetic symbols and flag any undefined references. | 2412 | // Fourth pass, handle synthetic symbols and flag any undefined references. |
| 2438 | if (self.strtab_dir.getKeyAdapted(@as([]const u8, "___dso_handle"), StringSliceAdapter{ | 2413 | if (self.strtab_dir.getKeyAdapted(@as([]const u8, "___dso_handle"), StringIndexAdapter{ |
| 2439 | .strtab = &self.strtab, | 2414 | .bytes = &self.strtab, |
| 2440 | })) |n_strx| blk: { | 2415 | })) |n_strx| blk: { |
| 2441 | const resolv = self.symbol_resolver.getPtr(n_strx) orelse break :blk; | 2416 | const resolv = self.symbol_resolver.getPtr(n_strx) orelse break :blk; |
| 2442 | if (resolv.where != .undef) break :blk; | 2417 | if (resolv.where != .undef) break :blk; |
| ... | @@ -2985,8 +2960,8 @@ fn setEntryPoint(self: *MachO) !void { | ... | @@ -2985,8 +2960,8 @@ fn setEntryPoint(self: *MachO) !void { |
| 2985 | // TODO we should respect the -entry flag passed in by the user to set a custom | 2960 | // TODO we should respect the -entry flag passed in by the user to set a custom |
| 2986 | // entrypoint. For now, assume default of `_main`. | 2961 | // entrypoint. For now, assume default of `_main`. |
| 2987 | const seg = self.load_commands.items[self.text_segment_cmd_index.?].Segment; | 2962 | const seg = self.load_commands.items[self.text_segment_cmd_index.?].Segment; |
| 2988 | const n_strx = self.strtab_dir.getKeyAdapted(@as([]const u8, "_main"), StringSliceAdapter{ | 2963 | const n_strx = self.strtab_dir.getKeyAdapted(@as([]const u8, "_main"), StringIndexAdapter{ |
| 2989 | .strtab = &self.strtab, | 2964 | .bytes = &self.strtab, |
| 2990 | }) orelse { | 2965 | }) orelse { |
| 2991 | log.err("'_main' export not found", .{}); | 2966 | log.err("'_main' export not found", .{}); |
| 2992 | return error.MissingMainEntrypoint; | 2967 | return error.MissingMainEntrypoint; |
| ... | @@ -4475,8 +4450,8 @@ pub fn populateMissingMetadata(self: *MachO) !void { | ... | @@ -4475,8 +4450,8 @@ pub fn populateMissingMetadata(self: *MachO) !void { |
| 4475 | }); | 4450 | }); |
| 4476 | self.load_commands_dirty = true; | 4451 | self.load_commands_dirty = true; |
| 4477 | } | 4452 | } |
| 4478 | if (!self.strtab_dir.containsAdapted(@as([]const u8, "dyld_stub_binder"), StringSliceAdapter{ | 4453 | if (!self.strtab_dir.containsAdapted(@as([]const u8, "dyld_stub_binder"), StringIndexAdapter{ |
| 4479 | .strtab = &self.strtab, | 4454 | .bytes = &self.strtab, |
| 4480 | })) { | 4455 | })) { |
| 4481 | const import_sym_index = @intCast(u32, self.undefs.items.len); | 4456 | const import_sym_index = @intCast(u32, self.undefs.items.len); |
| 4482 | const n_strx = try self.makeString("dyld_stub_binder"); | 4457 | const n_strx = try self.makeString("dyld_stub_binder"); |
| ... | @@ -4616,8 +4591,8 @@ pub fn addExternFn(self: *MachO, name: []const u8) !u32 { | ... | @@ -4616,8 +4591,8 @@ pub fn addExternFn(self: *MachO, name: []const u8) !u32 { |
| 4616 | const sym_name = try std.fmt.allocPrint(self.base.allocator, "_{s}", .{name}); | 4591 | const sym_name = try std.fmt.allocPrint(self.base.allocator, "_{s}", .{name}); |
| 4617 | defer self.base.allocator.free(sym_name); | 4592 | defer self.base.allocator.free(sym_name); |
| 4618 | | 4593 | |
| 4619 | if (self.strtab_dir.getKeyAdapted(@as([]const u8, sym_name), StringSliceAdapter{ | 4594 | if (self.strtab_dir.getKeyAdapted(@as([]const u8, sym_name), StringIndexAdapter{ |
| 4620 | .strtab = &self.strtab, | 4595 | .bytes = &self.strtab, |
| 4621 | })) |n_strx| { | 4596 | })) |n_strx| { |
| 4622 | const resolv = self.symbol_resolver.get(n_strx) orelse unreachable; | 4597 | const resolv = self.symbol_resolver.get(n_strx) orelse unreachable; |
| 4623 | return resolv.where_index; | 4598 | return resolv.where_index; |
| ... | @@ -5858,10 +5833,10 @@ pub fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) { | ... | @@ -5858,10 +5833,10 @@ pub fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) { |
| 5858 | } | 5833 | } |
| 5859 | | 5834 | |
| 5860 | pub fn makeString(self: *MachO, string: []const u8) !u32 { | 5835 | pub fn makeString(self: *MachO, string: []const u8) !u32 { |
| 5861 | const gop = try self.strtab_dir.getOrPutContextAdapted(self.base.allocator, @as([]const u8, string), StringSliceAdapter{ | 5836 | const gop = try self.strtab_dir.getOrPutContextAdapted(self.base.allocator, @as([]const u8, string), StringIndexAdapter{ |
| 5862 | .strtab = &self.strtab, | 5837 | .bytes = &self.strtab, |
| 5863 | }, StringIndexContext{ | 5838 | }, StringIndexContext{ |
| 5864 | .strtab = &self.strtab, | 5839 | .bytes = &self.strtab, |
| 5865 | }); | 5840 | }); |
| 5866 | if (gop.found_existing) { | 5841 | if (gop.found_existing) { |
| 5867 | const off = gop.key_ptr.*; | 5842 | const off = gop.key_ptr.*; |