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 {
20432043
20442044pub fn createGotAtom(self: *MachO, target: SymbolWithLoc) !*Atom {
20452045 const gpa = self.base.allocator;
2046 const sym_index = @intCast(u32, self.locals.items.len);
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
2046 const sym_index = try self.allocateSymbol();
20552047 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
20562051 try atom.relocs.append(gpa, .{
20572052 .offset = 0,
20582053 .target = target,
......@@ -2088,16 +2083,11 @@ pub fn createGotAtom(self: *MachO, target: SymbolWithLoc) !*Atom {
20882083
20892084pub fn createTlvPtrAtom(self: *MachO, target: SymbolWithLoc) !*Atom {
20902085 const gpa = self.base.allocator;
2091 const sym_index = @intCast(u32, self.locals.items.len);
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
2086 const sym_index = try self.allocateSymbol();
21002087 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
21012091 const target_sym = self.getSymbol(target);
21022092 assert(target_sym.undf());
21032093
......@@ -2125,15 +2115,10 @@ fn createDyldPrivateAtom(self: *MachO) !void {
21252115 if (self.dyld_private_atom != null) return;
21262116
21272117 const gpa = self.base.allocator;
2128 const sym_index = @intCast(u32, self.locals.items.len);
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 });
2118 const sym_index = try self.allocateSymbol();
21362119 const atom = try MachO.createEmptyAtom(gpa, sym_index, @sizeOf(u64), 3);
2120 const sym = atom.getSymbolPtr(self);
2121 sym.n_type = macho.N_SECT;
21372122 self.dyld_private_atom = atom;
21382123
21392124 try self.allocateAtomCommon(atom, self.data_section_index.?);
......@@ -2158,15 +2143,11 @@ fn createStubHelperPreambleAtom(self: *MachO) !void {
21582143 .aarch64 => 2,
21592144 else => unreachable,
21602145 };
2161 const sym_index = @intCast(u32, self.locals.items.len);
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 });
2146 const sym_index = try self.allocateSymbol();
21692147 const atom = try MachO.createEmptyAtom(gpa, sym_index, size, alignment);
2148 const sym = atom.getSymbolPtr(self);
2149 sym.n_type = macho.N_SECT;
2150
21702151 const dyld_private_sym_index = self.dyld_private_atom.?.sym_index;
21712152 switch (arch) {
21722153 .x86_64 => {
......@@ -2283,15 +2264,11 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {
22832264 .aarch64 => 2,
22842265 else => unreachable,
22852266 };
2286 const sym_index = @intCast(u32, self.locals.items.len);
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 });
2267 const sym_index = try self.allocateSymbol();
22942268 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
22952272 try atom.relocs.ensureTotalCapacity(gpa, 1);
22962273
22972274 switch (arch) {
......@@ -2347,15 +2324,11 @@ pub fn createStubHelperAtom(self: *MachO) !*Atom {
23472324
23482325pub fn createLazyPointerAtom(self: *MachO, stub_sym_index: u32, target: SymbolWithLoc) !*Atom {
23492326 const gpa = self.base.allocator;
2350 const sym_index = @intCast(u32, self.locals.items.len);
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 });
2327 const sym_index = try self.allocateSymbol();
23582328 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
23592332 try atom.relocs.append(gpa, .{
23602333 .offset = 0,
23612334 .target = .{ .sym_index = stub_sym_index, .file = null },
......@@ -2398,15 +2371,11 @@ pub fn createStubAtom(self: *MachO, laptr_sym_index: u32) !*Atom {
23982371 .aarch64 => 3 * @sizeOf(u32),
23992372 else => unreachable, // unhandled architecture type
24002373 };
2401 const sym_index = @intCast(u32, self.locals.items.len);
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 });
2374 const sym_index = try self.allocateSymbol();
24092375 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
24102379 switch (arch) {
24112380 .x86_64 => {
24122381 // jmp
......@@ -2518,7 +2487,9 @@ fn createMhExecuteHeaderSymbol(self: *MachO) !void {
25182487
25192488 const gpa = self.base.allocator;
25202489 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.* = .{
25222493 .n_strx = try self.strtab.insert(gpa, "__mh_execute_header"),
25232494 .n_type = macho.N_SECT | macho.N_EXT,
25242495 .n_sect = 0,
......@@ -2527,30 +2498,25 @@ fn createMhExecuteHeaderSymbol(self: *MachO) !void {
25272498 };
25282499
25292500 const gop = try self.getOrPutGlobalPtr("__mh_execute_header");
2530 gop.value_ptr.* = .{
2531 .sym_index = sym_index,
2532 .file = null,
2533 };
2501 gop.value_ptr.* = sym_loc;
25342502}
25352503
25362504fn createDsoHandleSymbol(self: *MachO) !void {
25372505 const global = self.getGlobalPtr("___dso_handle") orelse return;
2538 const sym = self.getSymbolPtr(global.*);
2539 if (!sym.undf()) return;
2506 if (!self.getSymbol(global.*).undf()) return;
25402507
25412508 const gpa = self.base.allocator;
25422509 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.* = .{
25442513 .n_strx = try self.strtab.insert(gpa, "___dso_handle"),
25452514 .n_type = macho.N_SECT | macho.N_EXT,
25462515 .n_sect = 0,
25472516 .n_desc = macho.N_WEAK_DEF,
25482517 .n_value = 0,
25492518 };
2550 global.* = .{
2551 .sym_index = sym_index,
2552 .file = null,
2553 };
2519 global.* = sym_loc;
25542520 _ = self.unresolved.swapRemove(self.getGlobalIndex("___dso_handle").?);
25552521}
25562522
......@@ -2789,7 +2755,8 @@ fn resolveDyldStubBinder(self: *MachO) !void {
27892755
27902756 const gpa = self.base.allocator;
27912757 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);
27932760 const sym_name = "dyld_stub_binder";
27942761 sym.* = .{
27952762 .n_strx = try self.strtab.insert(gpa, sym_name),
......@@ -2799,10 +2766,7 @@ fn resolveDyldStubBinder(self: *MachO) !void {
27992766 .n_value = 0,
28002767 };
28012768 const gop = try self.getOrPutGlobalPtr(sym_name);
2802 gop.value_ptr.* = .{
2803 .sym_index = sym_index,
2804 .file = null,
2805 };
2769 gop.value_ptr.* = sym_loc;
28062770 const global = gop.value_ptr.*;
28072771
28082772 for (self.dylibs.items) |dylib, id| {