authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-09-13 22:11:20+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-09-13 22:11:20+02:00
log46a10401f035b50122af9a91348edeb3f57e864e
tree13a6a57deb0227a98bfe5798240f4f47aa9b49f9
parent4c36da1047a83019ce7af653a32938c9d1ea616d

macho: fix logic for updating exports in incremental codepath


1 files changed, 44 insertions(+), 29 deletions(-)

src/link/MachO.zig+44-29
......@@ -848,6 +848,22 @@ pub fn flush(self: *MachO, comp: *Compilation) !void {
848848 try self.allocateGlobalSymbols();
849849 try self.writeAtoms();
850850
851 // log.warn("Locals:", .{});
852 // for (self.locals.items) |sym, i| {
853 // log.warn(" => {d}: {s}, {}", .{ i, self.getString(sym.n_strx), sym });
854 // }
855 // log.warn("Globals:", .{});
856 // for (self.globals.items) |sym, i| {
857 // log.warn(" => {d}: {s} {}", .{ i, self.getString(sym.n_strx), sym });
858 // }
859 // {
860 // log.warn("Resolver:", .{});
861 // var it = self.symbol_resolver.iterator();
862 // while (it.next()) |entry| {
863 // log.warn(" => {s}: {}", .{ self.getString(entry.key_ptr.*), entry.value_ptr.* });
864 // }
865 // }
866
851867 if (self.bss_section_index) |idx| {
852868 const seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;
853869 const sect = &seg.sections.items[idx];
......@@ -1751,6 +1767,7 @@ fn allocateGlobalSymbols(self: *MachO) !void {
17511767 const sym = &self.globals.items[resolv.where_index];
17521768 sym.n_value = local_sym.n_value;
17531769 sym.n_sect = local_sym.n_sect;
1770 log.debug("allocating global symbol {s} at 0x{x}", .{ self.getString(sym.n_strx), local_sym.n_value });
17541771 }
17551772}
17561773
......@@ -2941,6 +2958,8 @@ fn freeAtom(self: *MachO, atom: *Atom, match: MatchingSection) void {
29412958 if (atom.prev) |prev| {
29422959 // TODO shrink the section size here
29432960 last_atom.* = prev;
2961 } else {
2962 _ = self.atoms.fetchRemove(match);
29442963 }
29452964 }
29462965 }
......@@ -3360,44 +3379,40 @@ pub fn updateDeclExports(
33603379 },
33613380 }
33623381
3363 if (exp.link.macho.sym_index) |i| {
3364 const sym = &self.globals.items[i];
3365 sym.* = .{
3366 .n_strx = sym.n_strx,
3367 .n_type = n_type,
3368 .n_sect = @intCast(u8, self.text_section_index.?) + 1,
3369 .n_desc = n_desc,
3370 .n_value = decl_sym.n_value,
3371 };
3372 } else {
3373 const name_str_index = try self.makeString(exp_name);
3374 const i = if (self.globals_free_list.popOrNull()) |i| i else blk: {
3382 const global_sym_index = if (exp.link.macho.sym_index) |i| i else blk: {
3383 const i = if (self.globals_free_list.popOrNull()) |i| i else inner: {
33753384 _ = self.globals.addOneAssumeCapacity();
3376 break :blk @intCast(u32, self.globals.items.len - 1);
3377 };
3378 self.globals.items[i] = .{
3379 .n_strx = name_str_index,
3380 .n_type = n_type,
3381 .n_sect = @intCast(u8, self.text_section_index.?) + 1,
3382 .n_desc = n_desc,
3383 .n_value = decl_sym.n_value,
3384 };
3385 const resolv = try self.symbol_resolver.getOrPut(self.base.allocator, name_str_index);
3386 resolv.value_ptr.* = .{
3387 .where = .global,
3388 .where_index = i,
3389 .local_sym_index = decl.link.macho.local_sym_index,
3385 break :inner @intCast(u32, self.globals.items.len - 1);
33903386 };
3387 break :blk i;
3388 };
33913389
3392 exp.link.macho.sym_index = @intCast(u32, i);
3393 }
3390 const n_strx = try self.makeString(exp_name);
3391 const sym = &self.globals.items[global_sym_index];
3392 sym.* = .{
3393 .n_strx = try self.makeString(exp_name),
3394 .n_type = n_type,
3395 .n_sect = @intCast(u8, self.text_section_index.?) + 1,
3396 .n_desc = n_desc,
3397 .n_value = decl_sym.n_value,
3398 };
3399 exp.link.macho.sym_index = global_sym_index;
3400
3401 const resolv = try self.symbol_resolver.getOrPut(self.base.allocator, n_strx);
3402 resolv.value_ptr.* = .{
3403 .where = .global,
3404 .where_index = global_sym_index,
3405 .local_sym_index = decl.link.macho.local_sym_index,
3406 };
33943407 }
33953408}
33963409
33973410pub fn deleteExport(self: *MachO, exp: Export) void {
33983411 const sym_index = exp.sym_index orelse return;
33993412 self.globals_free_list.append(self.base.allocator, sym_index) catch {};
3400 self.globals.items[sym_index].n_type = 0;
3413 const global = &self.globals.items[sym_index];
3414 global.n_type = 0;
3415 assert(self.symbol_resolver.remove(global.n_strx));
34013416}
34023417
34033418pub fn freeDecl(self: *MachO, decl: *Module.Decl) void {