diff options
| author | Jakub Konka <kubkon@jakubkonka.com> | 2023-10-30 17:29:05 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-30 17:29:05 +0100 |
| commit | 10d03acdb5ff81c112280854629c9f9032e14330 (patch) | |
| tree | ed9c38b4c3c6ff17a1eaecacce35114219005bdb /src/codegen.zig | |
| parent | 91e117697ad90430d9266203415712b6cc59f669 (diff) | |
| parent | 324a93e673afcf1bcaac1163379d385952e52a27 (diff) | |
| download | zig-10d03acdb5ff81c112280854629c9f9032e14330.tar.gz zig-10d03acdb5ff81c112280854629c9f9032e14330.zip | |
Merge pull request #17773 from ziglang/elf-exports
link: implement exporting anon decls
Diffstat (limited to 'src/codegen.zig')
| -rw-r--r-- | src/codegen.zig | 20 |
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 }); |
