authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-03-27 18:23:25+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-03-28 12:28:48+02:00
log0dc210f950ff926fb8e57288a8ff257abc942b6d
tree6bcef8b17ff3c4210ece46c8bce99b17ae307b70
parentd2040b2763ad2684dcacce9acd8f8511bf9db397

link: pass expected lib name as hint in getGlobalSymbol()


7 files changed, 22 insertions(+), 29 deletions(-)

src/arch/aarch64/CodeGen.zig+4-10
......@@ -4318,16 +4318,10 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
43184318 });
43194319 } else if (func_value.castTag(.extern_fn)) |func_payload| {
43204320 const extern_fn = func_payload.data;
4321 const decl_name = mod.declPtr(extern_fn.owner_decl).name;
4322 if (extern_fn.lib_name) |lib_name| {
4323 log.debug("TODO enforce that '{s}' is expected in '{s}' library", .{
4324 decl_name,
4325 lib_name,
4326 });
4327 }
4328
4321 const decl_name = mem.sliceTo(mod.declPtr(extern_fn.owner_decl).name, 0);
4322 const lib_name = mem.sliceTo(extern_fn.lib_name, 0);
43294323 if (self.bin_file.cast(link.File.MachO)) |macho_file| {
4330 const sym_index = try macho_file.getGlobalSymbol(mem.sliceTo(decl_name, 0));
4324 const sym_index = try macho_file.getGlobalSymbol(decl_name, lib_name);
43314325 const atom = try macho_file.getOrCreateAtomForDecl(self.mod_fn.owner_decl);
43324326 const atom_index = macho_file.getAtom(atom).getSymbolIndex().?;
43334327 _ = try self.addInst(.{
......@@ -4340,7 +4334,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
43404334 },
43414335 });
43424336 } else if (self.bin_file.cast(link.File.Coff)) |coff_file| {
4343 const sym_index = try coff_file.getGlobalSymbol(mem.sliceTo(decl_name, 0));
4337 const sym_index = try coff_file.getGlobalSymbol(decl_name, lib_name);
43444338 try self.genSetReg(Type.initTag(.u64), .x30, .{
43454339 .linker_load = .{
43464340 .type = .import,
src/arch/wasm/CodeGen.zig+1-1
......@@ -6121,7 +6121,7 @@ fn callIntrinsic(
61216121 args: []const WValue,
61226122) InnerError!WValue {
61236123 assert(param_types.len == args.len);
6124 const symbol_index = func.bin_file.base.getGlobalSymbol(name) catch |err| {
6124 const symbol_index = func.bin_file.base.getGlobalSymbol(name, null) catch |err| {
61256125 return func.fail("Could not find or create global symbol '{s}'", .{@errorName(err)});
61266126 };
61276127
src/arch/x86_64/CodeGen.zig+4-10
......@@ -5317,16 +5317,10 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
53175317 } else unreachable;
53185318 } else if (func_value.castTag(.extern_fn)) |func_payload| {
53195319 const extern_fn = func_payload.data;
5320 const decl_name = mod.declPtr(extern_fn.owner_decl).name;
5321 if (extern_fn.lib_name) |lib_name| {
5322 log.debug("TODO enforce that '{s}' is expected in '{s}' library", .{
5323 decl_name,
5324 lib_name,
5325 });
5326 }
5327
5320 const decl_name = mem.sliceTo(mod.declPtr(extern_fn.owner_decl).name, 0);
5321 const lib_name = mem.sliceTo(extern_fn.lib_name, 0);
53285322 if (self.bin_file.cast(link.File.Coff)) |coff_file| {
5329 const sym_index = try coff_file.getGlobalSymbol(mem.sliceTo(decl_name, 0));
5323 const sym_index = try coff_file.getGlobalSymbol(decl_name, lib_name);
53305324 try self.genSetReg(Type.initTag(.usize), .rax, .{
53315325 .linker_load = .{
53325326 .type = .import,
......@@ -5335,7 +5329,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
53355329 });
53365330 try self.asmRegister(.call, .rax);
53375331 } else if (self.bin_file.cast(link.File.MachO)) |macho_file| {
5338 const sym_index = try macho_file.getGlobalSymbol(mem.sliceTo(decl_name, 0));
5332 const sym_index = try macho_file.getGlobalSymbol(decl_name, lib_name);
53395333 const atom = try macho_file.getOrCreateAtomForDecl(self.mod_fn.owner_decl);
53405334 const atom_index = macho_file.getAtom(atom).getSymbolIndex().?;
53415335 _ = try self.addInst(.{
src/link.zig+7-5
......@@ -504,18 +504,20 @@ pub const File = struct {
504504 /// Called from within CodeGen to retrieve the symbol index of a global symbol.
505505 /// If no symbol exists yet with this name, a new undefined global symbol will
506506 /// be created. This symbol may get resolved once all relocatables are (re-)linked.
507 pub fn getGlobalSymbol(base: *File, name: []const u8) UpdateDeclError!u32 {
507 /// Optionally, it is possible to specify where to expect the symbol defined if it
508 /// is an import.
509 pub fn getGlobalSymbol(base: *File, name: []const u8, lib_name: ?[]const u8) UpdateDeclError!u32 {
508510 if (build_options.only_c) @compileError("unreachable");
509 log.debug("getGlobalSymbol '{s}'", .{name});
511 log.debug("getGlobalSymbol '{s}' (expected in '{?s}')", .{ name, lib_name });
510512 switch (base.tag) {
511513 // zig fmt: off
512 .coff => return @fieldParentPtr(Coff, "base", base).getGlobalSymbol(name),
514 .coff => return @fieldParentPtr(Coff, "base", base).getGlobalSymbol(name, lib_name),
513515 .elf => unreachable,
514 .macho => return @fieldParentPtr(MachO, "base", base).getGlobalSymbol(name),
516 .macho => return @fieldParentPtr(MachO, "base", base).getGlobalSymbol(name, lib_name),
515517 .plan9 => unreachable,
516518 .spirv => unreachable,
517519 .c => unreachable,
518 .wasm => return @fieldParentPtr(Wasm, "base", base).getGlobalSymbol(name),
520 .wasm => return @fieldParentPtr(Wasm, "base", base).getGlobalSymbol(name, lib_name),
519521 .nvptx => unreachable,
520522 // zig fmt: on
521523 }
src/link/Coff.zig+2-1
......@@ -1526,7 +1526,8 @@ pub fn getDeclVAddr(self: *Coff, decl_index: Module.Decl.Index, reloc_info: link
15261526 return 0;
15271527}
15281528
1529pub fn getGlobalSymbol(self: *Coff, name: []const u8) !u32 {
1529pub fn getGlobalSymbol(self: *Coff, name: []const u8, lib_name: ?[]const u8) !u32 {
1530 _ = lib_name;
15301531 const gop = try self.getOrPutGlobalPtr(name);
15311532 const global_index = self.getGlobalIndex(name).?;
15321533
src/link/MachO.zig+2-1
......@@ -3202,7 +3202,8 @@ fn insertSection(self: *MachO, segment_index: u8, header: macho.section_64) !u8
32023202 return insertion_index;
32033203}
32043204
3205pub fn getGlobalSymbol(self: *MachO, name: []const u8) !u32 {
3205pub fn getGlobalSymbol(self: *MachO, name: []const u8, lib_name: ?[]const u8) !u32 {
3206 _ = lib_name;
32063207 const gpa = self.base.allocator;
32073208
32083209 const sym_name = try std.fmt.allocPrint(gpa, "_{s}", .{name});
src/link/Wasm.zig+2-1
......@@ -1573,7 +1573,8 @@ pub fn lowerUnnamedConst(wasm: *Wasm, tv: TypedValue, decl_index: Module.Decl.In
15731573/// such as an exported or imported symbol.
15741574/// If the symbol does not yet exist, creates a new one symbol instead
15751575/// and then returns the index to it.
1576pub fn getGlobalSymbol(wasm: *Wasm, name: []const u8) !u32 {
1576pub fn getGlobalSymbol(wasm: *Wasm, name: []const u8, lib_name: ?[]const u8) !u32 {
1577 _ = lib_name;
15771578 const name_index = try wasm.string_table.put(wasm.base.allocator, name);
15781579 const gop = try wasm.globals.getOrPut(wasm.base.allocator, name_index);
15791580 if (gop.found_existing) {