aboutsummaryrefslogtreecommitdiff
path: root/src/codegen.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/codegen.zig')
-rw-r--r--src/codegen.zig20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/codegen.zig b/src/codegen.zig
index 4db32df16f..c46e41c6e6 100644
--- a/src/codegen.zig
+++ b/src/codegen.zig
@@ -721,6 +721,7 @@ fn lowerAnonDeclRef(
const ptr_width_bytes = @divExact(target.ptrBitWidth(), 8);
const decl_val = anon_decl.val;
const decl_ty = mod.intern_pool.typeOf(decl_val).toType();
+ log.debug("lowerAnonDecl: ty = {}", .{decl_ty.fmt(mod)});
const is_fn_body = decl_ty.zigTypeTag(mod) == .Fn;
if (!is_fn_body and !decl_ty.hasRuntimeBits(mod)) {
try code.appendNTimes(0xaa, ptr_width_bytes);
@@ -911,6 +912,14 @@ fn genDeclRef(
_ = try sym.getOrCreateZigGotEntry(sym_index, elf_file);
return GenResult.mcv(.{ .load_symbol = sym.esym_index });
} else if (bin_file.cast(link.File.MachO)) |macho_file| {
+ if (is_extern) {
+ // TODO make this part of getGlobalSymbol
+ const name = mod.intern_pool.stringToSlice(decl.name);
+ const sym_name = try std.fmt.allocPrint(bin_file.allocator, "_{s}", .{name});
+ defer bin_file.allocator.free(sym_name);
+ const global_index = try macho_file.addUndefined(sym_name, .{ .add_got = true });
+ return GenResult.mcv(.{ .load_got = link.File.MachO.global_symbol_bit | global_index });
+ }
const atom_index = try macho_file.getOrCreateAtomForDecl(decl_index);
const sym_index = macho_file.getAtom(atom_index).getSymbolIndex().?;
if (is_threadlocal) {
@@ -918,6 +927,17 @@ fn genDeclRef(
}
return GenResult.mcv(.{ .load_got = sym_index });
} else if (bin_file.cast(link.File.Coff)) |coff_file| {
+ if (is_extern) {
+ const name = mod.intern_pool.stringToSlice(decl.name);
+ // TODO audit this
+ const lib_name = if (decl.getOwnedVariable(mod)) |ov|
+ mod.intern_pool.stringToSliceUnwrap(ov.lib_name)
+ else
+ null;
+ const global_index = try coff_file.getGlobalSymbol(name, lib_name);
+ try coff_file.need_got_table.put(bin_file.allocator, global_index, {}); // needs GOT
+ return GenResult.mcv(.{ .load_got = link.File.Coff.global_symbol_bit | global_index });
+ }
const atom_index = try coff_file.getOrCreateAtomForDecl(decl_index);
const sym_index = coff_file.getAtom(atom_index).getSymbolIndex().?;
return GenResult.mcv(.{ .load_got = sym_index });