| ... | @@ -848,6 +848,22 @@ pub fn flush(self: *MachO, comp: *Compilation) !void { | ... | @@ -848,6 +848,22 @@ pub fn flush(self: *MachO, comp: *Compilation) !void { |
| 848 | try self.allocateGlobalSymbols(); | 848 | try self.allocateGlobalSymbols(); |
| 849 | try self.writeAtoms(); | 849 | try self.writeAtoms(); |
| 850 | | 850 | |
| | 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 | |
| 851 | if (self.bss_section_index) |idx| { | 867 | if (self.bss_section_index) |idx| { |
| 852 | const seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment; | 868 | const seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment; |
| 853 | const sect = &seg.sections.items[idx]; | 869 | const sect = &seg.sections.items[idx]; |
| ... | @@ -1751,6 +1767,7 @@ fn allocateGlobalSymbols(self: *MachO) !void { | ... | @@ -1751,6 +1767,7 @@ fn allocateGlobalSymbols(self: *MachO) !void { |
| 1751 | const sym = &self.globals.items[resolv.where_index]; | 1767 | const sym = &self.globals.items[resolv.where_index]; |
| 1752 | sym.n_value = local_sym.n_value; | 1768 | sym.n_value = local_sym.n_value; |
| 1753 | sym.n_sect = local_sym.n_sect; | 1769 | 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 }); |
| 1754 | } | 1771 | } |
| 1755 | } | 1772 | } |
| 1756 | | 1773 | |
| ... | @@ -2941,6 +2958,8 @@ fn freeAtom(self: *MachO, atom: *Atom, match: MatchingSection) void { | ... | @@ -2941,6 +2958,8 @@ fn freeAtom(self: *MachO, atom: *Atom, match: MatchingSection) void { |
| 2941 | if (atom.prev) |prev| { | 2958 | if (atom.prev) |prev| { |
| 2942 | // TODO shrink the section size here | 2959 | // TODO shrink the section size here |
| 2943 | last_atom.* = prev; | 2960 | last_atom.* = prev; |
| | 2961 | } else { |
| | 2962 | _ = self.atoms.fetchRemove(match); |
| 2944 | } | 2963 | } |
| 2945 | } | 2964 | } |
| 2946 | } | 2965 | } |
| ... | @@ -3360,44 +3379,40 @@ pub fn updateDeclExports( | ... | @@ -3360,44 +3379,40 @@ pub fn updateDeclExports( |
| 3360 | }, | 3379 | }, |
| 3361 | } | 3380 | } |
| 3362 | | 3381 | |
| 3363 | if (exp.link.macho.sym_index) |i| { | 3382 | const global_sym_index = if (exp.link.macho.sym_index) |i| i else blk: { |
| 3364 | const sym = &self.globals.items[i]; | 3383 | const i = if (self.globals_free_list.popOrNull()) |i| i else inner: { |
| 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: { | | |
| 3375 | _ = self.globals.addOneAssumeCapacity(); | 3384 | _ = self.globals.addOneAssumeCapacity(); |
| 3376 | break :blk @intCast(u32, self.globals.items.len - 1); | 3385 | break :inner @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, | | |
| 3390 | }; | 3386 | }; |
| | 3387 | break :blk i; |
| | 3388 | }; |
| 3391 | | 3389 | |
| 3392 | exp.link.macho.sym_index = @intCast(u32, i); | 3390 | const n_strx = try self.makeString(exp_name); |
| 3393 | } | 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 | }; |
| 3394 | } | 3407 | } |
| 3395 | } | 3408 | } |
| 3396 | | 3409 | |
| 3397 | pub fn deleteExport(self: *MachO, exp: Export) void { | 3410 | pub fn deleteExport(self: *MachO, exp: Export) void { |
| 3398 | const sym_index = exp.sym_index orelse return; | 3411 | const sym_index = exp.sym_index orelse return; |
| 3399 | self.globals_free_list.append(self.base.allocator, sym_index) catch {}; | 3412 | 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)); |
| 3401 | } | 3416 | } |
| 3402 | | 3417 | |
| 3403 | pub fn freeDecl(self: *MachO, decl: *Module.Decl) void { | 3418 | pub fn freeDecl(self: *MachO, decl: *Module.Decl) void { |