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...@@ -4318,16 +4318,10 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
4318 });4318 });
4319 } else if (func_value.castTag(.extern_fn)) |func_payload| {4319 } else if (func_value.castTag(.extern_fn)) |func_payload| {
4320 const extern_fn = func_payload.data;4320 const extern_fn = func_payload.data;
4321 const decl_name = mod.declPtr(extern_fn.owner_decl).name;4321 const decl_name = mem.sliceTo(mod.declPtr(extern_fn.owner_decl).name, 0);
4322 if (extern_fn.lib_name) |lib_name| {4322 const lib_name = mem.sliceTo(extern_fn.lib_name, 0);
4323 log.debug("TODO enforce that '{s}' is expected in '{s}' library", .{
4324 decl_name,
4325 lib_name,
4326 });
4327 }
4328
4329 if (self.bin_file.cast(link.File.MachO)) |macho_file| {4323 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);
4331 const atom = try macho_file.getOrCreateAtomForDecl(self.mod_fn.owner_decl);4325 const atom = try macho_file.getOrCreateAtomForDecl(self.mod_fn.owner_decl);
4332 const atom_index = macho_file.getAtom(atom).getSymbolIndex().?;4326 const atom_index = macho_file.getAtom(atom).getSymbolIndex().?;
4333 _ = try self.addInst(.{4327 _ = try self.addInst(.{
...@@ -4340,7 +4334,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier...@@ -4340,7 +4334,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
4340 },4334 },
4341 });4335 });
4342 } else if (self.bin_file.cast(link.File.Coff)) |coff_file| {4336 } 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);
4344 try self.genSetReg(Type.initTag(.u64), .x30, .{4338 try self.genSetReg(Type.initTag(.u64), .x30, .{
4345 .linker_load = .{4339 .linker_load = .{
4346 .type = .import,4340 .type = .import,
src/arch/wasm/CodeGen.zig+1-1
...@@ -6121,7 +6121,7 @@ fn callIntrinsic(...@@ -6121,7 +6121,7 @@ fn callIntrinsic(
6121 args: []const WValue,6121 args: []const WValue,
6122) InnerError!WValue {6122) InnerError!WValue {
6123 assert(param_types.len == args.len);6123 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| {
6125 return func.fail("Could not find or create global symbol '{s}'", .{@errorName(err)});6125 return func.fail("Could not find or create global symbol '{s}'", .{@errorName(err)});
6126 };6126 };
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...@@ -5317,16 +5317,10 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
5317 } else unreachable;5317 } else unreachable;
5318 } else if (func_value.castTag(.extern_fn)) |func_payload| {5318 } else if (func_value.castTag(.extern_fn)) |func_payload| {
5319 const extern_fn = func_payload.data;5319 const extern_fn = func_payload.data;
5320 const decl_name = mod.declPtr(extern_fn.owner_decl).name;5320 const decl_name = mem.sliceTo(mod.declPtr(extern_fn.owner_decl).name, 0);
5321 if (extern_fn.lib_name) |lib_name| {5321 const lib_name = mem.sliceTo(extern_fn.lib_name, 0);
5322 log.debug("TODO enforce that '{s}' is expected in '{s}' library", .{
5323 decl_name,
5324 lib_name,
5325 });
5326 }
5327
5328 if (self.bin_file.cast(link.File.Coff)) |coff_file| {5322 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);
5330 try self.genSetReg(Type.initTag(.usize), .rax, .{5324 try self.genSetReg(Type.initTag(.usize), .rax, .{
5331 .linker_load = .{5325 .linker_load = .{
5332 .type = .import,5326 .type = .import,
...@@ -5335,7 +5329,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier...@@ -5335,7 +5329,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
5335 });5329 });
5336 try self.asmRegister(.call, .rax);5330 try self.asmRegister(.call, .rax);
5337 } else if (self.bin_file.cast(link.File.MachO)) |macho_file| {5331 } 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);
5339 const atom = try macho_file.getOrCreateAtomForDecl(self.mod_fn.owner_decl);5333 const atom = try macho_file.getOrCreateAtomForDecl(self.mod_fn.owner_decl);
5340 const atom_index = macho_file.getAtom(atom).getSymbolIndex().?;5334 const atom_index = macho_file.getAtom(atom).getSymbolIndex().?;
5341 _ = try self.addInst(.{5335 _ = try self.addInst(.{
src/link.zig+7-5
...@@ -504,18 +504,20 @@ pub const File = struct {...@@ -504,18 +504,20 @@ pub const File = struct {
504 /// Called from within CodeGen to retrieve the symbol index of a global symbol.504 /// Called from within CodeGen to retrieve the symbol index of a global symbol.
505 /// If no symbol exists yet with this name, a new undefined global symbol will505 /// If no symbol exists yet with this name, a new undefined global symbol will
506 /// be created. This symbol may get resolved once all relocatables are (re-)linked.506 /// 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 {
508 if (build_options.only_c) @compileError("unreachable");510 if (build_options.only_c) @compileError("unreachable");
509 log.debug("getGlobalSymbol '{s}'", .{name});511 log.debug("getGlobalSymbol '{s}' (expected in '{?s}')", .{ name, lib_name });
510 switch (base.tag) {512 switch (base.tag) {
511 // zig fmt: off513 // zig fmt: off
512 .coff => return @fieldParentPtr(Coff, "base", base).getGlobalSymbol(name),514 .coff => return @fieldParentPtr(Coff, "base", base).getGlobalSymbol(name, lib_name),
513 .elf => unreachable,515 .elf => unreachable,
514 .macho => return @fieldParentPtr(MachO, "base", base).getGlobalSymbol(name),516 .macho => return @fieldParentPtr(MachO, "base", base).getGlobalSymbol(name, lib_name),
515 .plan9 => unreachable,517 .plan9 => unreachable,
516 .spirv => unreachable,518 .spirv => unreachable,
517 .c => unreachable,519 .c => unreachable,
518 .wasm => return @fieldParentPtr(Wasm, "base", base).getGlobalSymbol(name),520 .wasm => return @fieldParentPtr(Wasm, "base", base).getGlobalSymbol(name, lib_name),
519 .nvptx => unreachable,521 .nvptx => unreachable,
520 // zig fmt: on522 // zig fmt: on
521 }523 }
src/link/Coff.zig+2-1
...@@ -1526,7 +1526,8 @@ pub fn getDeclVAddr(self: *Coff, decl_index: Module.Decl.Index, reloc_info: link...@@ -1526,7 +1526,8 @@ pub fn getDeclVAddr(self: *Coff, decl_index: Module.Decl.Index, reloc_info: link
1526 return 0;1526 return 0;
1527}1527}
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;
1530 const gop = try self.getOrPutGlobalPtr(name);1531 const gop = try self.getOrPutGlobalPtr(name);
1531 const global_index = self.getGlobalIndex(name).?;1532 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...@@ -3202,7 +3202,8 @@ fn insertSection(self: *MachO, segment_index: u8, header: macho.section_64) !u8
3202 return insertion_index;3202 return insertion_index;
3203}3203}
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;
3206 const gpa = self.base.allocator;3207 const gpa = self.base.allocator;
32073208
3208 const sym_name = try std.fmt.allocPrint(gpa, "_{s}", .{name});3209 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...@@ -1573,7 +1573,8 @@ pub fn lowerUnnamedConst(wasm: *Wasm, tv: TypedValue, decl_index: Module.Decl.In
1573/// such as an exported or imported symbol.1573/// such as an exported or imported symbol.
1574/// If the symbol does not yet exist, creates a new one symbol instead1574/// If the symbol does not yet exist, creates a new one symbol instead
1575/// and then returns the index to it.1575/// 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;
1577 const name_index = try wasm.string_table.put(wasm.base.allocator, name);1578 const name_index = try wasm.string_table.put(wasm.base.allocator, name);
1578 const gop = try wasm.globals.getOrPut(wasm.base.allocator, name_index);1579 const gop = try wasm.globals.getOrPut(wasm.base.allocator, name_index);
1579 if (gop.found_existing) {1580 if (gop.found_existing) {