authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-10-07 20:32:02+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-10-07 20:34:40+02:00
logb5b25d38a8fa4e66e54ff1279c1becee877793f6
tree38aeff3d774dc291e75dcdd869c1eb3b2653b804
parentbdab4f53c1fa614fcd89468f305184fa36520039

Fix improper reuse of global symbols in MachO

Signed-off-by: Jakub Konka <kubkon@jakubkonka.com>

4 files changed, 42 insertions(+), 9 deletions(-)

src/Module.zig+12-3
......@@ -93,7 +93,7 @@ pub const Export = struct {
9393 /// Byte offset into the file that contains the export directive.
9494 src: usize,
9595 /// Represents the position of the export, if any, in the output file.
96 link: link.File.Elf.Export,
96 link: link.File.Export,
9797 /// The Decl that performs the export. Note that this is *not* the Decl being exported.
9898 owner_decl: *Decl,
9999 /// The Decl being exported. Note this is *not* the Decl performing the export.
......@@ -1712,7 +1712,10 @@ fn deleteDeclExports(self: *Module, decl: *Decl) void {
17121712 }
17131713 }
17141714 if (self.comp.bin_file.cast(link.File.Elf)) |elf| {
1715 elf.deleteExport(exp.link);
1715 elf.deleteExport(exp.link.elf);
1716 }
1717 if (self.comp.bin_file.cast(link.File.MachO)) |macho| {
1718 macho.deleteExport(exp.link.macho);
17161719 }
17171720 if (self.failed_exports.remove(exp)) |entry| {
17181721 entry.value.destroy(self.gpa);
......@@ -1875,7 +1878,13 @@ pub fn analyzeExport(self: *Module, scope: *Scope, src: usize, borrowed_symbol_n
18751878 new_export.* = .{
18761879 .options = .{ .name = symbol_name },
18771880 .src = src,
1878 .link = .{},
1881 .link = switch (self.comp.bin_file.tag) {
1882 .coff => .{ .coff = {} },
1883 .elf => .{ .elf = link.File.Elf.Export{} },
1884 .macho => .{ .macho = link.File.MachO.Export{} },
1885 .c => .{ .c = {} },
1886 .wasm => .{ .wasm = {} },
1887 },
18791888 .owner_decl = owner_decl,
18801889 .exported_decl = exported_decl,
18811890 .status = .in_progress,
src/link.zig+8
......@@ -133,6 +133,14 @@ pub const File = struct {
133133 wasm: ?Wasm.FnData,
134134 };
135135
136 pub const Export = union {
137 elf: Elf.Export,
138 coff: void,
139 macho: MachO.Export,
140 c: void,
141 wasm: void,
142 };
143
136144 /// For DWARF .debug_info.
137145 pub const DbgInfoTypeRelocsTable = std.HashMapUnmanaged(Type, DbgInfoTypeReloc, Type.hash, Type.eql, std.hash_map.DefaultMaxLoadPercentage);
138146
src/link/Elf.zig+2-2
......@@ -2588,7 +2588,7 @@ pub fn updateDeclExports(
25882588 },
25892589 };
25902590 const stt_bits: u8 = @truncate(u4, decl_sym.st_info);
2591 if (exp.link.sym_index) |i| {
2591 if (exp.link.elf.sym_index) |i| {
25922592 const sym = &self.global_symbols.items[i];
25932593 sym.* = .{
25942594 .st_name = try self.updateString(sym.st_name, exp.options.name),
......@@ -2613,7 +2613,7 @@ pub fn updateDeclExports(
26132613 .st_size = decl_sym.st_size,
26142614 };
26152615
2616 exp.link.sym_index = @intCast(u32, i);
2616 exp.link.elf.sym_index = @intCast(u32, i);
26172617 }
26182618 }
26192619}
src/link/MachO.zig+20-4
......@@ -115,6 +115,9 @@ local_symbols: std.ArrayListUnmanaged(macho.nlist_64) = .{},
115115global_symbols: std.ArrayListUnmanaged(macho.nlist_64) = .{},
116116/// Table of all undefined symbols
117117undef_symbols: std.ArrayListUnmanaged(macho.nlist_64) = .{},
118
119global_symbol_free_list: std.ArrayListUnmanaged(u32) = .{},
120
118121dyld_stub_binder_index: ?u16 = null,
119122
120123/// Table of symbol names aka the string table.
......@@ -178,6 +181,10 @@ pub const TextBlock = struct {
178181 };
179182};
180183
184pub const Export = struct {
185 sym_index: ?u32 = null,
186};
187
181188pub const SrcFn = struct {
182189 pub const empty = SrcFn{};
183190};
......@@ -713,6 +720,7 @@ pub fn deinit(self: *MachO) void {
713720 self.string_table.deinit(self.base.allocator);
714721 self.undef_symbols.deinit(self.base.allocator);
715722 self.global_symbols.deinit(self.base.allocator);
723 self.global_symbol_free_list.deinit(self.base.allocator);
716724 self.local_symbols.deinit(self.base.allocator);
717725 self.sections.deinit(self.base.allocator);
718726 self.load_commands.deinit(self.base.allocator);
......@@ -837,7 +845,7 @@ pub fn updateDeclExports(
837845 },
838846 };
839847 const n_type = decl_sym.n_type | macho.N_EXT;
840 if (exp.link.sym_index) |i| {
848 if (exp.link.macho.sym_index) |i| {
841849 const sym = &self.global_symbols.items[i];
842850 sym.* = .{
843851 .n_strx = try self.updateString(sym.n_strx, exp.options.name),
......@@ -848,8 +856,10 @@ pub fn updateDeclExports(
848856 };
849857 } else {
850858 const name_str_index = try self.makeString(exp.options.name);
851 _ = self.global_symbols.addOneAssumeCapacity();
852 const i = self.global_symbols.items.len - 1;
859 const i = if (self.global_symbol_free_list.popOrNull()) |i| i else blk: {
860 _ = self.global_symbols.addOneAssumeCapacity();
861 break :blk self.global_symbols.items.len - 1;
862 };
853863 self.global_symbols.items[i] = .{
854864 .n_strx = name_str_index,
855865 .n_type = n_type,
......@@ -858,11 +868,17 @@ pub fn updateDeclExports(
858868 .n_value = decl_sym.n_value,
859869 };
860870
861 exp.link.sym_index = @intCast(u32, i);
871 exp.link.macho.sym_index = @intCast(u32, i);
862872 }
863873 }
864874}
865875
876pub fn deleteExport(self: *MachO, exp: Export) void {
877 const sym_index = exp.sym_index orelse return;
878 self.global_symbol_free_list.append(self.base.allocator, sym_index) catch {};
879 self.global_symbols.items[sym_index].n_type = 0;
880}
881
866882pub fn freeDecl(self: *MachO, decl: *Module.Decl) void {}
867883
868884pub fn getDeclVAddr(self: *MachO, decl: *const Module.Decl) u64 {