diff options
| author | Jakub Konka <kubkon@jakubkonka.com> | 2021-07-20 20:33:07 +0200 |
|---|---|---|
| committer | Jakub Konka <kubkon@jakubkonka.com> | 2021-07-20 20:33:07 +0200 |
| commit | a442b165f1e219b429e497e1de26780612762871 (patch) | |
| tree | a29e87e96c8280185e7cffbaa7424e1eb2a78b1e /src/codegen.zig | |
| parent | 1843ecf51b240c43a4a9a9cadbcc1286b9b9f41a (diff) | |
| download | zig-a442b165f1e219b429e497e1de26780612762871.tar.gz zig-a442b165f1e219b429e497e1de26780612762871.zip | |
macho: add stub relocs when adding extern fn
in self-hosted.
Diffstat (limited to 'src/codegen.zig')
| -rw-r--r-- | src/codegen.zig | 51 |
1 files changed, 22 insertions, 29 deletions
diff --git a/src/codegen.zig b/src/codegen.zig index ec75cbadc6..7cb7119f0d 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -2523,36 +2523,29 @@ 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 already_defined = macho_file.symbol_resolver.contains(decl_name); - const resolv = macho_file.symbol_resolver.get(decl_name) orelse blk: { - break :blk try macho_file.addExternFn(decl_name); - }; - const start = self.code.items.len; - const len: usize = blk: { - switch (arch) { - .x86_64 => { - // callq - try self.code.ensureCapacity(self.code.items.len + 5); - self.code.appendSliceAssumeCapacity(&[5]u8{ 0xe8, 0x0, 0x0, 0x0, 0x0 }); - break :blk 5; - }, - .aarch64 => { - // bl - writeInt(u32, try self.code.addManyAsArray(4), 0); - break :blk 4; - }, - else => unreachable, // unsupported architecture on MachO - } - }; - try macho_file.stub_fixups.append(self.bin_file.allocator, .{ - .symbol = resolv.where_index, - .already_defined = already_defined, - .start = start, - .len = len, + const where_index = try macho_file.addExternFn(mem.spanZ(decl.name)); + const offset = @intCast(u32, self.code.items.len); + switch (arch) { + .x86_64 => { + // callq + try self.code.ensureCapacity(self.code.items.len + 5); + self.code.appendSliceAssumeCapacity(&[5]u8{ 0xe8, 0x0, 0x0, 0x0, 0x0 }); + }, + .aarch64 => { + // bl + writeInt(u32, try self.code.addManyAsArray(4), Instruction.bl(0).toU32()); + }, + else => unreachable, // unsupported architecture on MachO + } + // Add relocation to the decl. + try decl.link.macho.relocs.append(self.bin_file.allocator, .{ + .offset = offset, + .where = .import, + .where_index = where_index, + .payload = .{ .branch = .{ + .arch = arch, + } }, }); - // We mark the space and fix it up later. } else { return self.fail(inst.base.src, "TODO implement calling bitcasted functions", .{}); } |
