aboutsummaryrefslogtreecommitdiff
path: root/src/codegen.zig
diff options
context:
space:
mode:
authorJakub Konka <kubkon@jakubkonka.com>2021-01-08 22:26:18 +0100
committerJakub Konka <kubkon@jakubkonka.com>2021-01-13 23:54:46 +0100
commit44a052a65f5c59b02383178f36fcc231df28b49f (patch)
tree55bb58983ccd0908b4b8f1337af6ff0321b204ae /src/codegen.zig
parent2f7cd7119387b517b07e5feb02a13e69651fe9eb (diff)
downloadzig-44a052a65f5c59b02383178f36fcc231df28b49f.tar.gz
zig-44a052a65f5c59b02383178f36fcc231df28b49f.zip
macho: write out stubs for new externs only
Diffstat (limited to 'src/codegen.zig')
-rw-r--r--src/codegen.zig30
1 files changed, 20 insertions, 10 deletions
diff --git a/src/codegen.zig b/src/codegen.zig
index 7100227db0..84148fcdd4 100644
--- a/src/codegen.zig
+++ b/src/codegen.zig
@@ -1862,18 +1862,28 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
} else if (func_value.castTag(.extern_fn)) |func_payload| {
const decl = func_payload.data;
const decl_name = try std.fmt.allocPrint(self.bin_file.allocator, "_{s}", .{decl.name});
- defer self.bin_file.allocator.free(decl_name);
- const name = try macho_file.makeString(decl_name);
- const symbol = macho_file.undef_symbols.items.len;
- try macho_file.undef_symbols.append(self.bin_file.allocator, .{
- .n_strx = name,
- .n_type = std.macho.N_UNDF | std.macho.N_EXT,
- .n_sect = 0,
- .n_desc = std.macho.REFERENCE_FLAG_UNDEFINED_NON_LAZY | std.macho.N_SYMBOL_RESOLVER,
- .n_value = 0,
- });
+ const exists: bool = macho_file.externs.contains(decl_name);
+ const symbol: u32 = blk: {
+ if (macho_file.externs.get(decl_name)) |index| {
+ self.bin_file.allocator.free(decl_name);
+ break :blk index;
+ } else {
+ const extern_index = @intCast(u32, macho_file.undef_symbols.items.len - 1); // TODO
+ try macho_file.externs.putNoClobber(self.bin_file.allocator, decl_name, extern_index);
+ const name = try macho_file.makeString(decl_name);
+ try macho_file.undef_symbols.append(self.bin_file.allocator, .{
+ .n_strx = name,
+ .n_type = std.macho.N_UNDF | std.macho.N_EXT,
+ .n_sect = 0,
+ .n_desc = std.macho.REFERENCE_FLAG_UNDEFINED_NON_LAZY | std.macho.N_SYMBOL_RESOLVER,
+ .n_value = 0,
+ });
+ break :blk extern_index;
+ }
+ };
try macho_file.stub_fixups.append(self.bin_file.allocator, .{
.symbol = symbol,
+ .exists = exists,
.start = self.code.items.len,
.len = 4,
});