authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-01-18 17:24:07+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-01-24 12:34:41+01:00
logee463efdf2e662fb4970aa6e9ba43d165e0cdfc7
tree3e25e3fef3fd40a4637c386bec191496f74245d0
parentc98d229844e4b12ba2c61291fdf7fa0376f4e087

macho: fill in more blanks in ZigObject


2 files changed, 37 insertions(+), 11 deletions(-)

src/link/MachO.zig+1-1
......@@ -21,7 +21,7 @@ sections: std.MultiArrayList(Section) = .{},
2121symbols: std.ArrayListUnmanaged(Symbol) = .{},
2222symbols_extra: std.ArrayListUnmanaged(u32) = .{},
2323symbols_free_list: std.ArrayListUnmanaged(Symbol.Index) = .{},
24globals: std.AutoHashMapUnmanaged(u32, Symbol.Index) = .{},
24globals: std.AutoArrayHashMapUnmanaged(u32, Symbol.Index) = .{},
2525/// This table will be populated after `scanRelocs` has run.
2626/// Key is symbol index.
2727undefs: std.AutoHashMapUnmanaged(Symbol.Index, std.ArrayListUnmanaged(Atom.Index)) = .{},
src/link/MachO/ZigObject.zig+36-10
......@@ -6,6 +6,7 @@ symtab: std.MultiArrayList(Nlist) = .{},
66
77symbols: std.ArrayListUnmanaged(Symbol.Index) = .{},
88atoms: std.ArrayListUnmanaged(Atom.Index) = .{},
9globals_lookup: std.AutoHashMapUnmanaged(u32, Symbol.Index) = .{},
910
1011/// Table of tracked LazySymbols.
1112lazy_syms: LazySymbolTable = .{},
......@@ -53,6 +54,7 @@ pub fn deinit(self: *ZigObject, allocator: Allocator) void {
5354 self.symtab.deinit(allocator);
5455 self.symbols.deinit(allocator);
5556 self.atoms.deinit(allocator);
57 self.globals_lookup.deinit(allocator);
5658
5759 {
5860 var it = self.decls.iterator();
......@@ -918,20 +920,44 @@ pub fn deleteDeclExport(
918920 macho_file: *MachO,
919921 decl_index: InternPool.DeclIndex,
920922 name: InternPool.NullTerminatedString,
921) void {
922 _ = self;
923 _ = macho_file;
924 _ = decl_index;
925 _ = name;
926 @panic("TODO deleteDeclExport");
923) Allocator.Error!void {
924 const metadata = self.decls.getPtr(decl_index) orelse return;
925
926 const gpa = macho_file.base.comp.gpa;
927 const mod = macho_file.base.comp.module.?;
928 const exp_name = try std.fmt.allocPrint(gpa, "_{s}", .{mod.intern_pool.stringToSlice(name)});
929 defer gpa.free(exp_name);
930 const nlist_index = metadata.@"export"(self, macho_file, exp_name) orelse return;
931
932 log.debug("deleting export '{s}'", .{exp_name});
933
934 const nlist = &self.symtab.items(.nlist)[nlist_index.*];
935 self.symtab.items(.size)[nlist_index.*] = 0;
936 _ = self.globals_lookup.remove(nlist.n_strx);
937 const sym_index = macho_file.globals.get(nlist.n_strx).?;
938 const sym = macho_file.getSymbol(sym_index);
939 if (sym.file == self.index) {
940 _ = macho_file.globals.swapRemove(nlist.n_strx);
941 sym.* = .{};
942 }
943 nlist.* = MachO.null_sym;
927944}
928945
929946pub fn getGlobalSymbol(self: *ZigObject, macho_file: *MachO, name: []const u8, lib_name: ?[]const u8) !u32 {
930 _ = self;
931 _ = macho_file;
932 _ = name;
933947 _ = lib_name;
934 @panic("TODO getGlobalSymbol");
948 const gpa = macho_file.base.comp.gpa;
949 const off = try macho_file.strings.insert(gpa, name);
950 const lookup_gop = try self.globals_lookup.getOrPut(gpa, off);
951 if (!lookup_gop.found_existing) {
952 const nlist_index = try self.addNlist(gpa);
953 const nlist = &self.symtab.items(.nlist)[nlist_index];
954 nlist.n_strx = off;
955 nlist.n_type = macho.N_EXT;
956 lookup_gop.value_ptr.* = nlist_index;
957 const gop = try macho_file.getOrCreateGlobal(off);
958 try self.symbols.append(gpa, gop.index);
959 }
960 return lookup_gop.value_ptr.*;
935961}
936962
937963pub fn getOrCreateMetadataForDecl(