authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-09-10 00:58:16+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-09-10 00:58:16+02:00
log08248084441497489065d8f66e7f79d5e5115b7c
tree90cd59df45c620b4ab219c5a619076b36f8e5013
parent485d8819b33e3fca1eb63b0d109019d0c3fc15fc

macho: refactor direct use of locals container in favour of helpers


1 files changed, 39 insertions(+), 75 deletions(-)

src/link/MachO.zig+39-75
...@@ -2043,16 +2043,11 @@ fn writeAtomsIncremental(self: *MachO) !void {...@@ -2043,16 +2043,11 @@ fn writeAtomsIncremental(self: *MachO) !void {
20432043
2044pub fn createGotAtom(self: *MachO, target: SymbolWithLoc) !*Atom {2044pub fn createGotAtom(self: *MachO, target: SymbolWithLoc) !*Atom {
2045 const gpa = self.base.allocator;2045 const gpa = self.base.allocator;
2046 const sym_index = @intCast(u32, self.locals.items.len);2046 const sym_index = try self.allocateSymbol();
2047 try self.locals.append(gpa, .{
2048 .n_strx = 0,
2049 .n_type = macho.N_SECT,
2050 .n_sect = 0,
2051 .n_desc = 0,
2052 .n_value = 0,
2053 });
2054
2055 const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3);2047 const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3);
2048 const sym = atom.getSymbolPtr(self);
2049 sym.n_type = macho.N_SECT;
2050
2056 try atom.relocs.append(gpa, .{2051 try atom.relocs.append(gpa, .{
2057 .offset = 0,2052 .offset = 0,
2058 .target = target,2053 .target = target,
...@@ -2088,16 +2083,11 @@ pub fn createGotAtom(self: *MachO, target: SymbolWithLoc) !*Atom {...@@ -2088,16 +2083,11 @@ pub fn createGotAtom(self: *MachO, target: SymbolWithLoc) !*Atom {
20882083
2089pub fn createTlvPtrAtom(self: *MachO, target: SymbolWithLoc) !*Atom {2084pub fn createTlvPtrAtom(self: *MachO, target: SymbolWithLoc) !*Atom {
2090 const gpa = self.base.allocator;2085 const gpa = self.base.allocator;
2091 const sym_index = @intCast(u32, self.locals.items.len);2086 const sym_index = try self.allocateSymbol();
2092 try self.locals.append(gpa, .{
2093 .n_strx = 0,
2094 .n_type = macho.N_SECT,
2095 .n_sect = 0,
2096 .n_desc = 0,
2097 .n_value = 0,
2098 });
2099
2100 const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3);2087 const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3);
2088 const sym = atom.getSymbolPtr(self);
2089 sym.n_type = macho.N_SECT;
2090
2101 const target_sym = self.getSymbol(target);2091 const target_sym = self.getSymbol(target);
2102 assert(target_sym.undf());2092 assert(target_sym.undf());
21032093
...@@ -2125,15 +2115,10 @@ fn createDyldPrivateAtom(self: *MachO) !void {...@@ -2125,15 +2115,10 @@ fn createDyldPrivateAtom(self: *MachO) !void {
2125 if (self.dyld_private_atom != null) return;2115 if (self.dyld_private_atom != null) return;
21262116
2127 const gpa = self.base.allocator;2117 const gpa = self.base.allocator;
2128 const sym_index = @intCast(u32, self.locals.items.len);2118 const sym_index = try self.allocateSymbol();
2129 try self.locals.append(gpa, .{
2130 .n_strx = 0,
2131 .n_type = macho.N_SECT,
2132 .n_sect = 0,
2133 .n_desc = 0,
2134 .n_value = 0,
2135 });
2136 const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3);2119 const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3);
2120 const sym = atom.getSymbolPtr(self);
2121 sym.n_type = macho.N_SECT;
2137 self.dyld_private_atom = atom;2122 self.dyld_private_atom = atom;
21382123
2139 try self.allocateAtomCommon(atom, self.data_section_index.?);2124 try self.allocateAtomCommon(atom, self.data_section_index.?);
...@@ -2158,15 +2143,11 @@ fn createStubHelperPreambleAtom(self: *MachO) !void {...@@ -2158,15 +2143,11 @@ fn createStubHelperPreambleAtom(self: *MachO) !void {
2158 .aarch64 => 2,2143 .aarch64 => 2,
2159 else => unreachable,2144 else => unreachable,
2160 };2145 };
2161 const sym_index = @intCast(u32, self.locals.items.len);2146 const sym_index = try self.allocateSymbol();
2162 try self.locals.append(gpa, .{
2163 .n_strx = 0,
2164 .n_type = macho.N_SECT,
2165 .n_sect = 0,
2166 .n_desc = 0,
2167 .n_value = 0,
2168 });
2169 const atom = try MachO.createEmptyAtom(gpa, sym_index, size, alignment);2147 const atom = try MachO.createEmptyAtom(gpa, sym_index, size, alignment);
2148 const sym = atom.getSymbolPtr(self);
2149 sym.n_type = macho.N_SECT;
2150
2170 const dyld_private_sym_index = self.dyld_private_atom.?.sym_index;2151 const dyld_private_sym_index = self.dyld_private_atom.?.sym_index;
2171 switch (arch) {2152 switch (arch) {
2172 .x86_64 => {2153 .x86_64 => {
...@@ -2283,15 +2264,11 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {...@@ -2283,15 +2264,11 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {
2283 .aarch64 => 2,2264 .aarch64 => 2,
2284 else => unreachable,2265 else => unreachable,
2285 };2266 };
2286 const sym_index = @intCast(u32, self.locals.items.len);2267 const sym_index = try self.allocateSymbol();
2287 try self.locals.append(gpa, .{
2288 .n_strx = 0,
2289 .n_type = macho.N_SECT,
2290 .n_sect = 0,
2291 .n_desc = 0,
2292 .n_value = 0,
2293 });
2294 const atom = try MachO.createEmptyAtom(gpa, sym_index, stub_size, alignment);2268 const atom = try MachO.createEmptyAtom(gpa, sym_index, stub_size, alignment);
2269 const sym = atom.getSymbolPtr(self);
2270 sym.n_type = macho.N_SECT;
2271
2295 try atom.relocs.ensureTotalCapacity(gpa, 1);2272 try atom.relocs.ensureTotalCapacity(gpa, 1);
22962273
2297 switch (arch) {2274 switch (arch) {
...@@ -2347,15 +2324,11 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {...@@ -2347,15 +2324,11 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {
23472324
2348pub fn createLazyPointerAtom(self: *MachO, stub_sym_index: u32, target: SymbolWithLoc) !*Atom {2325pub fn createLazyPointerAtom(self: *MachO, stub_sym_index: u32, target: SymbolWithLoc) !*Atom {
2349 const gpa = self.base.allocator;2326 const gpa = self.base.allocator;
2350 const sym_index = @intCast(u32, self.locals.items.len);2327 const sym_index = try self.allocateSymbol();
2351 try self.locals.append(gpa, .{
2352 .n_strx = 0,
2353 .n_type = macho.N_SECT,
2354 .n_sect = 0,
2355 .n_desc = 0,
2356 .n_value = 0,
2357 });
2358 const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3);2328 const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3);
2329 const sym = atom.getSymbolPtr(self);
2330 sym.n_type = macho.N_SECT;
2331
2359 try atom.relocs.append(gpa, .{2332 try atom.relocs.append(gpa, .{
2360 .offset = 0,2333 .offset = 0,
2361 .target = .{ .sym_index = stub_sym_index, .file = null },2334 .target = .{ .sym_index = stub_sym_index, .file = null },
...@@ -2398,15 +2371,11 @@ pub fn createStubAtom(self: *MachO, laptr_sym_index: u32) !*Atom {...@@ -2398,15 +2371,11 @@ pub fn createStubAtom(self: *MachO, laptr_sym_index: u32) !*Atom {
2398 .aarch64 => 3 * @sizeOf(u32),2371 .aarch64 => 3 * @sizeOf(u32),
2399 else => unreachable, // unhandled architecture type2372 else => unreachable, // unhandled architecture type
2400 };2373 };
2401 const sym_index = @intCast(u32, self.locals.items.len);2374 const sym_index = try self.allocateSymbol();
2402 try self.locals.append(gpa, .{
2403 .n_strx = 0,
2404 .n_type = macho.N_SECT,
2405 .n_sect = 0,
2406 .n_desc = 0,
2407 .n_value = 0,
2408 });
2409 const atom = try MachO.createEmptyAtom(gpa, sym_index, stub_size, alignment);2375 const atom = try MachO.createEmptyAtom(gpa, sym_index, stub_size, alignment);
2376 const sym = atom.getSymbolPtr(self);
2377 sym.n_type = macho.N_SECT;
2378
2410 switch (arch) {2379 switch (arch) {
2411 .x86_64 => {2380 .x86_64 => {
2412 // jmp2381 // jmp
...@@ -2518,7 +2487,9 @@ fn createMhExecuteHeaderSymbol(self: *MachO) !void {...@@ -2518,7 +2487,9 @@ fn createMhExecuteHeaderSymbol(self: *MachO) !void {
25182487
2519 const gpa = self.base.allocator;2488 const gpa = self.base.allocator;
2520 const sym_index = try self.allocateSymbol();2489 const sym_index = try self.allocateSymbol();
2521 self.locals.items[sym_index] = .{2490 const sym_loc = SymbolWithLoc{ .sym_index = sym_index, .file = null };
2491 const sym = self.getSymbolPtr(sym_loc);
2492 sym.* = .{
2522 .n_strx = try self.strtab.insert(gpa, "__mh_execute_header"),2493 .n_strx = try self.strtab.insert(gpa, "__mh_execute_header"),
2523 .n_type = macho.N_SECT | macho.N_EXT,2494 .n_type = macho.N_SECT | macho.N_EXT,
2524 .n_sect = 0,2495 .n_sect = 0,
...@@ -2527,30 +2498,25 @@ fn createMhExecuteHeaderSymbol(self: *MachO) !void {...@@ -2527,30 +2498,25 @@ fn createMhExecuteHeaderSymbol(self: *MachO) !void {
2527 };2498 };
25282499
2529 const gop = try self.getOrPutGlobalPtr("__mh_execute_header");2500 const gop = try self.getOrPutGlobalPtr("__mh_execute_header");
2530 gop.value_ptr.* = .{2501 gop.value_ptr.* = sym_loc;
2531 .sym_index = sym_index,
2532 .file = null,
2533 };
2534}2502}
25352503
2536fn createDsoHandleSymbol(self: *MachO) !void {2504fn createDsoHandleSymbol(self: *MachO) !void {
2537 const global = self.getGlobalPtr("___dso_handle") orelse return;2505 const global = self.getGlobalPtr("___dso_handle") orelse return;
2538 const sym = self.getSymbolPtr(global.*);2506 if (!self.getSymbol(global.*).undf()) return;
2539 if (!sym.undf()) return;
25402507
2541 const gpa = self.base.allocator;2508 const gpa = self.base.allocator;
2542 const sym_index = try self.allocateSymbol();2509 const sym_index = try self.allocateSymbol();
2543 self.locals.items[sym_index] = .{2510 const sym_loc = SymbolWithLoc{ .sym_index = sym_index, .file = null };
2511 const sym = self.getSymbolPtr(sym_loc);
2512 sym.* = .{
2544 .n_strx = try self.strtab.insert(gpa, "___dso_handle"),2513 .n_strx = try self.strtab.insert(gpa, "___dso_handle"),
2545 .n_type = macho.N_SECT | macho.N_EXT,2514 .n_type = macho.N_SECT | macho.N_EXT,
2546 .n_sect = 0,2515 .n_sect = 0,
2547 .n_desc = macho.N_WEAK_DEF,2516 .n_desc = macho.N_WEAK_DEF,
2548 .n_value = 0,2517 .n_value = 0,
2549 };2518 };
2550 global.* = .{2519 global.* = sym_loc;
2551 .sym_index = sym_index,
2552 .file = null,
2553 };
2554 _ = self.unresolved.swapRemove(self.getGlobalIndex("___dso_handle").?);2520 _ = self.unresolved.swapRemove(self.getGlobalIndex("___dso_handle").?);
2555}2521}
25562522
...@@ -2789,7 +2755,8 @@ fn resolveDyldStubBinder(self: *MachO) !void {...@@ -2789,7 +2755,8 @@ fn resolveDyldStubBinder(self: *MachO) !void {
27892755
2790 const gpa = self.base.allocator;2756 const gpa = self.base.allocator;
2791 const sym_index = try self.allocateSymbol();2757 const sym_index = try self.allocateSymbol();
2792 const sym = &self.locals.items[sym_index];2758 const sym_loc = SymbolWithLoc{ .sym_index = sym_index, .file = null };
2759 const sym = self.getSymbolPtr(sym_loc);
2793 const sym_name = "dyld_stub_binder";2760 const sym_name = "dyld_stub_binder";
2794 sym.* = .{2761 sym.* = .{
2795 .n_strx = try self.strtab.insert(gpa, sym_name),2762 .n_strx = try self.strtab.insert(gpa, sym_name),
...@@ -2799,10 +2766,7 @@ fn resolveDyldStubBinder(self: *MachO) !void {...@@ -2799,10 +2766,7 @@ fn resolveDyldStubBinder(self: *MachO) !void {
2799 .n_value = 0,2766 .n_value = 0,
2800 };2767 };
2801 const gop = try self.getOrPutGlobalPtr(sym_name);2768 const gop = try self.getOrPutGlobalPtr(sym_name);
2802 gop.value_ptr.* = .{2769 gop.value_ptr.* = sym_loc;
2803 .sym_index = sym_index,
2804 .file = null,
2805 };
2806 const global = gop.value_ptr.*;2770 const global = gop.value_ptr.*;
28072771
2808 for (self.dylibs.items) |dylib, id| {2772 for (self.dylibs.items) |dylib, id| {