mirror of
https://github.com/ziglang/zig.git
synced 2025-12-14 18:23:12 +00:00
macho: get the ball rolling!
This commit is contained in:
parent
2f94dc939e
commit
dd0addab1f
@ -4013,10 +4013,11 @@ fn store(self: *Self, ptr: MCValue, value: MCValue, ptr_ty: Type, value_ty: Type
|
||||
.import => unreachable,
|
||||
};
|
||||
const atom_index = switch (self.bin_file.tag) {
|
||||
.macho => blk: {
|
||||
const macho_file = self.bin_file.cast(link.File.MachO).?;
|
||||
const atom = try macho_file.getOrCreateAtomForDecl(self.owner_decl);
|
||||
break :blk macho_file.getAtom(atom).getSymbolIndex().?;
|
||||
.macho => {
|
||||
// const macho_file = self.bin_file.cast(link.File.MachO).?;
|
||||
// const atom = try macho_file.getOrCreateAtomForDecl(self.owner_decl);
|
||||
// break :blk macho_file.getAtom(atom).getSymbolIndex().?;
|
||||
@panic("TODO store");
|
||||
},
|
||||
.coff => blk: {
|
||||
const coff_file = self.bin_file.cast(link.File.Coff).?;
|
||||
@ -4321,14 +4322,16 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
|
||||
const got_addr = @as(u32, @intCast(sym.zigGotAddress(elf_file)));
|
||||
try self.genSetReg(Type.usize, .x30, .{ .memory = got_addr });
|
||||
} else if (self.bin_file.cast(link.File.MachO)) |macho_file| {
|
||||
const atom = try macho_file.getOrCreateAtomForDecl(func.owner_decl);
|
||||
const sym_index = macho_file.getAtom(atom).getSymbolIndex().?;
|
||||
try self.genSetReg(Type.u64, .x30, .{
|
||||
.linker_load = .{
|
||||
.type = .got,
|
||||
.sym_index = sym_index,
|
||||
},
|
||||
});
|
||||
_ = macho_file;
|
||||
@panic("TODO airCall");
|
||||
// const atom = try macho_file.getOrCreateAtomForDecl(func.owner_decl);
|
||||
// const sym_index = macho_file.getAtom(atom).getSymbolIndex().?;
|
||||
// try self.genSetReg(Type.u64, .x30, .{
|
||||
// .linker_load = .{
|
||||
// .type = .got,
|
||||
// .sym_index = sym_index,
|
||||
// },
|
||||
// });
|
||||
} else if (self.bin_file.cast(link.File.Coff)) |coff_file| {
|
||||
const atom = try coff_file.getOrCreateAtomForDecl(func.owner_decl);
|
||||
const sym_index = coff_file.getAtom(atom).getSymbolIndex().?;
|
||||
@ -4352,18 +4355,20 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
|
||||
const decl_name = mod.intern_pool.stringToSlice(mod.declPtr(extern_func.decl).name);
|
||||
const lib_name = mod.intern_pool.stringToSliceUnwrap(extern_func.lib_name);
|
||||
if (self.bin_file.cast(link.File.MachO)) |macho_file| {
|
||||
const sym_index = try macho_file.getGlobalSymbol(decl_name, lib_name);
|
||||
const atom = try macho_file.getOrCreateAtomForDecl(self.owner_decl);
|
||||
const atom_index = macho_file.getAtom(atom).getSymbolIndex().?;
|
||||
_ = try self.addInst(.{
|
||||
.tag = .call_extern,
|
||||
.data = .{
|
||||
.relocation = .{
|
||||
.atom_index = atom_index,
|
||||
.sym_index = sym_index,
|
||||
},
|
||||
},
|
||||
});
|
||||
_ = macho_file;
|
||||
@panic("TODO airCall");
|
||||
// const sym_index = try macho_file.getGlobalSymbol(decl_name, lib_name);
|
||||
// const atom = try macho_file.getOrCreateAtomForDecl(self.owner_decl);
|
||||
// const atom_index = macho_file.getAtom(atom).getSymbolIndex().?;
|
||||
// _ = try self.addInst(.{
|
||||
// .tag = .call_extern,
|
||||
// .data = .{
|
||||
// .relocation = .{
|
||||
// .atom_index = atom_index,
|
||||
// .sym_index = sym_index,
|
||||
// },
|
||||
// },
|
||||
// });
|
||||
} else if (self.bin_file.cast(link.File.Coff)) |coff_file| {
|
||||
const sym_index = try coff_file.getGlobalSymbol(decl_name, lib_name);
|
||||
try self.genSetReg(Type.u64, .x30, .{
|
||||
@ -5532,10 +5537,11 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro
|
||||
.import => unreachable,
|
||||
};
|
||||
const atom_index = switch (self.bin_file.tag) {
|
||||
.macho => blk: {
|
||||
const macho_file = self.bin_file.cast(link.File.MachO).?;
|
||||
const atom = try macho_file.getOrCreateAtomForDecl(self.owner_decl);
|
||||
break :blk macho_file.getAtom(atom).getSymbolIndex().?;
|
||||
.macho => {
|
||||
// const macho_file = self.bin_file.cast(link.File.MachO).?;
|
||||
// const atom = try macho_file.getOrCreateAtomForDecl(self.owner_decl);
|
||||
// break :blk macho_file.getAtom(atom).getSymbolIndex().?;
|
||||
@panic("TODO genSetStack");
|
||||
},
|
||||
.coff => blk: {
|
||||
const coff_file = self.bin_file.cast(link.File.Coff).?;
|
||||
@ -5653,10 +5659,11 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void
|
||||
.import => .load_memory_import,
|
||||
};
|
||||
const atom_index = switch (self.bin_file.tag) {
|
||||
.macho => blk: {
|
||||
const macho_file = self.bin_file.cast(link.File.MachO).?;
|
||||
const atom = try macho_file.getOrCreateAtomForDecl(self.owner_decl);
|
||||
break :blk macho_file.getAtom(atom).getSymbolIndex().?;
|
||||
.macho => {
|
||||
@panic("TODO genSetReg");
|
||||
// const macho_file = self.bin_file.cast(link.File.MachO).?;
|
||||
// const atom = try macho_file.getOrCreateAtomForDecl(self.owner_decl);
|
||||
// break :blk macho_file.getAtom(atom).getSymbolIndex().?;
|
||||
},
|
||||
.coff => blk: {
|
||||
const coff_file = self.bin_file.cast(link.File.Coff).?;
|
||||
@ -5850,10 +5857,11 @@ fn genSetStackArgument(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) I
|
||||
.import => unreachable,
|
||||
};
|
||||
const atom_index = switch (self.bin_file.tag) {
|
||||
.macho => blk: {
|
||||
const macho_file = self.bin_file.cast(link.File.MachO).?;
|
||||
const atom = try macho_file.getOrCreateAtomForDecl(self.owner_decl);
|
||||
break :blk macho_file.getAtom(atom).getSymbolIndex().?;
|
||||
.macho => {
|
||||
@panic("TODO genSetStackArgument");
|
||||
// const macho_file = self.bin_file.cast(link.File.MachO).?;
|
||||
// const atom = try macho_file.getOrCreateAtomForDecl(self.owner_decl);
|
||||
// break :blk macho_file.getAtom(atom).getSymbolIndex().?;
|
||||
},
|
||||
.coff => blk: {
|
||||
const coff_file = self.bin_file.cast(link.File.Coff).?;
|
||||
|
||||
@ -677,6 +677,7 @@ fn mirDebugEpilogueBegin(emit: *Emit) !void {
|
||||
fn mirCallExtern(emit: *Emit, inst: Mir.Inst.Index) !void {
|
||||
assert(emit.mir.instructions.items(.tag)[inst] == .call_extern);
|
||||
const relocation = emit.mir.instructions.items(.data)[inst].relocation;
|
||||
_ = relocation;
|
||||
|
||||
const offset = blk: {
|
||||
const offset = @as(u32, @intCast(emit.code.items.len));
|
||||
@ -684,19 +685,22 @@ fn mirCallExtern(emit: *Emit, inst: Mir.Inst.Index) !void {
|
||||
try emit.writeInstruction(Instruction.bl(0));
|
||||
break :blk offset;
|
||||
};
|
||||
_ = offset;
|
||||
|
||||
if (emit.bin_file.cast(link.File.MachO)) |macho_file| {
|
||||
// Add relocation to the decl.
|
||||
const atom_index = macho_file.getAtomIndexForSymbol(.{ .sym_index = relocation.atom_index }).?;
|
||||
const target = macho_file.getGlobalByIndex(relocation.sym_index);
|
||||
try link.File.MachO.Atom.addRelocation(macho_file, atom_index, .{
|
||||
.type = .branch,
|
||||
.target = target,
|
||||
.offset = offset,
|
||||
.addend = 0,
|
||||
.pcrel = true,
|
||||
.length = 2,
|
||||
});
|
||||
_ = macho_file;
|
||||
@panic("TODO mirCallExtern");
|
||||
// // Add relocation to the decl.
|
||||
// const atom_index = macho_file.getAtomIndexForSymbol(.{ .sym_index = relocation.atom_index }).?;
|
||||
// const target = macho_file.getGlobalByIndex(relocation.sym_index);
|
||||
// try link.File.MachO.Atom.addRelocation(macho_file, atom_index, .{
|
||||
// .type = .branch,
|
||||
// .target = target,
|
||||
// .offset = offset,
|
||||
// .addend = 0,
|
||||
// .pcrel = true,
|
||||
// .length = 2,
|
||||
// });
|
||||
} else if (emit.bin_file.cast(link.File.Coff)) |_| {
|
||||
unreachable; // Calling imports is handled via `.load_memory_import`
|
||||
} else {
|
||||
@ -900,32 +904,34 @@ fn mirLoadMemoryPie(emit: *Emit, inst: Mir.Inst.Index) !void {
|
||||
}
|
||||
|
||||
if (emit.bin_file.cast(link.File.MachO)) |macho_file| {
|
||||
const Atom = link.File.MachO.Atom;
|
||||
const Relocation = Atom.Relocation;
|
||||
const atom_index = macho_file.getAtomIndexForSymbol(.{ .sym_index = data.atom_index }).?;
|
||||
try Atom.addRelocations(macho_file, atom_index, &[_]Relocation{ .{
|
||||
.target = .{ .sym_index = data.sym_index },
|
||||
.offset = offset,
|
||||
.addend = 0,
|
||||
.pcrel = true,
|
||||
.length = 2,
|
||||
.type = switch (tag) {
|
||||
.load_memory_got, .load_memory_ptr_got => Relocation.Type.got_page,
|
||||
.load_memory_direct, .load_memory_ptr_direct => Relocation.Type.page,
|
||||
else => unreachable,
|
||||
},
|
||||
}, .{
|
||||
.target = .{ .sym_index = data.sym_index },
|
||||
.offset = offset + 4,
|
||||
.addend = 0,
|
||||
.pcrel = false,
|
||||
.length = 2,
|
||||
.type = switch (tag) {
|
||||
.load_memory_got, .load_memory_ptr_got => Relocation.Type.got_pageoff,
|
||||
.load_memory_direct, .load_memory_ptr_direct => Relocation.Type.pageoff,
|
||||
else => unreachable,
|
||||
},
|
||||
} });
|
||||
_ = macho_file;
|
||||
@panic("TODO mirLoadMemoryPie");
|
||||
// const Atom = link.File.MachO.Atom;
|
||||
// const Relocation = Atom.Relocation;
|
||||
// const atom_index = macho_file.getAtomIndexForSymbol(.{ .sym_index = data.atom_index }).?;
|
||||
// try Atom.addRelocations(macho_file, atom_index, &[_]Relocation{ .{
|
||||
// .target = .{ .sym_index = data.sym_index },
|
||||
// .offset = offset,
|
||||
// .addend = 0,
|
||||
// .pcrel = true,
|
||||
// .length = 2,
|
||||
// .type = switch (tag) {
|
||||
// .load_memory_got, .load_memory_ptr_got => Relocation.Type.got_page,
|
||||
// .load_memory_direct, .load_memory_ptr_direct => Relocation.Type.page,
|
||||
// else => unreachable,
|
||||
// },
|
||||
// }, .{
|
||||
// .target = .{ .sym_index = data.sym_index },
|
||||
// .offset = offset + 4,
|
||||
// .addend = 0,
|
||||
// .pcrel = false,
|
||||
// .length = 2,
|
||||
// .type = switch (tag) {
|
||||
// .load_memory_got, .load_memory_ptr_got => Relocation.Type.got_pageoff,
|
||||
// .load_memory_direct, .load_memory_ptr_direct => Relocation.Type.pageoff,
|
||||
// else => unreachable,
|
||||
// },
|
||||
// } });
|
||||
} else if (emit.bin_file.cast(link.File.Coff)) |coff_file| {
|
||||
const atom_index = coff_file.getAtomIndexForSymbol(.{ .sym_index = data.atom_index, .file = null }).?;
|
||||
const target = switch (tag) {
|
||||
|
||||
@ -139,8 +139,10 @@ const Owner = union(enum) {
|
||||
if (ctx.bin_file.cast(link.File.Elf)) |elf_file| {
|
||||
return elf_file.zigObjectPtr().?.getOrCreateMetadataForDecl(elf_file, decl_index);
|
||||
} else if (ctx.bin_file.cast(link.File.MachO)) |macho_file| {
|
||||
const atom = try macho_file.getOrCreateAtomForDecl(decl_index);
|
||||
return macho_file.getAtom(atom).getSymbolIndex().?;
|
||||
_ = macho_file;
|
||||
// const atom = try macho_file.getOrCreateAtomForDecl(decl_index);
|
||||
// return macho_file.getAtom(atom).getSymbolIndex().?;
|
||||
@panic("TODO getSymbolIndex");
|
||||
} else if (ctx.bin_file.cast(link.File.Coff)) |coff_file| {
|
||||
const atom = try coff_file.getOrCreateAtomForDecl(decl_index);
|
||||
return coff_file.getAtom(atom).getSymbolIndex().?;
|
||||
@ -153,9 +155,11 @@ const Owner = union(enum) {
|
||||
return elf_file.zigObjectPtr().?.getOrCreateMetadataForLazySymbol(elf_file, lazy_sym) catch |err|
|
||||
ctx.fail("{s} creating lazy symbol", .{@errorName(err)});
|
||||
} else if (ctx.bin_file.cast(link.File.MachO)) |macho_file| {
|
||||
const atom = macho_file.getOrCreateAtomForLazySymbol(lazy_sym) catch |err|
|
||||
return ctx.fail("{s} creating lazy symbol", .{@errorName(err)});
|
||||
return macho_file.getAtom(atom).getSymbolIndex().?;
|
||||
_ = macho_file;
|
||||
// const atom = macho_file.getOrCreateAtomForLazySymbol(lazy_sym) catch |err|
|
||||
// return ctx.fail("{s} creating lazy symbol", .{@errorName(err)});
|
||||
// return macho_file.getAtom(atom).getSymbolIndex().?;
|
||||
@panic("TODO getSymbolIndex");
|
||||
} else if (ctx.bin_file.cast(link.File.Coff)) |coff_file| {
|
||||
const atom = coff_file.getOrCreateAtomForLazySymbol(lazy_sym) catch |err|
|
||||
return ctx.fail("{s} creating lazy symbol", .{@errorName(err)});
|
||||
@ -10951,10 +10955,12 @@ fn genCall(self: *Self, info: union(enum) {
|
||||
try self.genSetReg(.rax, Type.usize, .{ .lea_got = sym_index });
|
||||
try self.asmRegister(.{ ._, .call }, .rax);
|
||||
} else if (self.bin_file.cast(link.File.MachO)) |macho_file| {
|
||||
const atom = try macho_file.getOrCreateAtomForDecl(func.owner_decl);
|
||||
const sym_index = macho_file.getAtom(atom).getSymbolIndex().?;
|
||||
try self.genSetReg(.rax, Type.usize, .{ .lea_got = sym_index });
|
||||
try self.asmRegister(.{ ._, .call }, .rax);
|
||||
_ = macho_file;
|
||||
@panic("TODO genCall");
|
||||
// const atom = try macho_file.getOrCreateAtomForDecl(func.owner_decl);
|
||||
// const sym_index = macho_file.getAtom(atom).getSymbolIndex().?;
|
||||
// try self.genSetReg(.rax, Type.usize, .{ .lea_got = sym_index });
|
||||
// try self.asmRegister(.{ ._, .call }, .rax);
|
||||
} else if (self.bin_file.cast(link.File.Plan9)) |p9| {
|
||||
const atom_index = try p9.seeDecl(func.owner_decl);
|
||||
const atom = p9.getAtom(atom_index);
|
||||
@ -13814,11 +13820,15 @@ fn genExternSymbolRef(
|
||||
_ = try self.addInst(.{
|
||||
.tag = .call,
|
||||
.ops = .extern_fn_reloc,
|
||||
.data = .{ .reloc = .{
|
||||
.atom_index = atom_index,
|
||||
.sym_index = link.File.MachO.global_symbol_bit | global_index,
|
||||
} },
|
||||
.data = .{
|
||||
.reloc = .{
|
||||
.atom_index = atom_index,
|
||||
// .sym_index = link.File.MachO.global_symbol_bit | global_index,
|
||||
.sym_index = global_index,
|
||||
},
|
||||
},
|
||||
});
|
||||
@panic("TODO genExternSymbolRef");
|
||||
} else return self.fail("TODO implement calling extern functions", .{});
|
||||
}
|
||||
|
||||
@ -13906,19 +13916,21 @@ fn genLazySymbolRef(
|
||||
else => unreachable,
|
||||
}
|
||||
} else if (self.bin_file.cast(link.File.MachO)) |macho_file| {
|
||||
const atom_index = macho_file.getOrCreateAtomForLazySymbol(lazy_sym) catch |err|
|
||||
return self.fail("{s} creating lazy symbol", .{@errorName(err)});
|
||||
const sym_index = macho_file.getAtom(atom_index).getSymbolIndex().?;
|
||||
switch (tag) {
|
||||
.lea, .call => try self.genSetReg(reg, Type.usize, .{ .lea_got = sym_index }),
|
||||
.mov => try self.genSetReg(reg, Type.usize, .{ .load_got = sym_index }),
|
||||
else => unreachable,
|
||||
}
|
||||
switch (tag) {
|
||||
.lea, .mov => {},
|
||||
.call => try self.asmRegister(.{ ._, .call }, reg),
|
||||
else => unreachable,
|
||||
}
|
||||
_ = macho_file;
|
||||
@panic("TODO genLazySymbolRef");
|
||||
// const atom_index = macho_file.getOrCreateAtomForLazySymbol(lazy_sym) catch |err|
|
||||
// return self.fail("{s} creating lazy symbol", .{@errorName(err)});
|
||||
// const sym_index = macho_file.getAtom(atom_index).getSymbolIndex().?;
|
||||
// switch (tag) {
|
||||
// .lea, .call => try self.genSetReg(reg, Type.usize, .{ .lea_got = sym_index }),
|
||||
// .mov => try self.genSetReg(reg, Type.usize, .{ .load_got = sym_index }),
|
||||
// else => unreachable,
|
||||
// }
|
||||
// switch (tag) {
|
||||
// .lea, .mov => {},
|
||||
// .call => try self.asmRegister(.{ ._, .call }, reg),
|
||||
// else => unreachable,
|
||||
// }
|
||||
} else {
|
||||
return self.fail("TODO implement genLazySymbol for x86_64 {s}", .{@tagName(self.bin_file.tag)});
|
||||
}
|
||||
|
||||
@ -49,21 +49,23 @@ pub fn emitMir(emit: *Emit) Error!void {
|
||||
.r_addend = -4,
|
||||
});
|
||||
} else if (emit.lower.bin_file.cast(link.File.MachO)) |macho_file| {
|
||||
// Add relocation to the decl.
|
||||
const atom_index =
|
||||
macho_file.getAtomIndexForSymbol(.{ .sym_index = symbol.atom_index }).?;
|
||||
const target = if (link.File.MachO.global_symbol_bit & symbol.sym_index != 0)
|
||||
macho_file.getGlobalByIndex(link.File.MachO.global_symbol_mask & symbol.sym_index)
|
||||
else
|
||||
link.File.MachO.SymbolWithLoc{ .sym_index = symbol.sym_index };
|
||||
try link.File.MachO.Atom.addRelocation(macho_file, atom_index, .{
|
||||
.type = .branch,
|
||||
.target = target,
|
||||
.offset = end_offset - 4,
|
||||
.addend = 0,
|
||||
.pcrel = true,
|
||||
.length = 2,
|
||||
});
|
||||
_ = macho_file;
|
||||
@panic("TODO emitMir");
|
||||
// // Add relocation to the decl.
|
||||
// const atom_index =
|
||||
// macho_file.getAtomIndexForSymbol(.{ .sym_index = symbol.atom_index }).?;
|
||||
// const target = if (link.File.MachO.global_symbol_bit & symbol.sym_index != 0)
|
||||
// macho_file.getGlobalByIndex(link.File.MachO.global_symbol_mask & symbol.sym_index)
|
||||
// else
|
||||
// link.File.MachO.SymbolWithLoc{ .sym_index = symbol.sym_index };
|
||||
// try link.File.MachO.Atom.addRelocation(macho_file, atom_index, .{
|
||||
// .type = .branch,
|
||||
// .target = target,
|
||||
// .offset = end_offset - 4,
|
||||
// .addend = 0,
|
||||
// .pcrel = true,
|
||||
// .length = 2,
|
||||
// });
|
||||
} else if (emit.lower.bin_file.cast(link.File.Coff)) |coff_file| {
|
||||
// Add relocation to the decl.
|
||||
const atom_index = coff_file.getAtomIndexForSymbol(
|
||||
@ -157,25 +159,27 @@ pub fn emitMir(emit: *Emit) Error!void {
|
||||
=> |symbol| if (emit.lower.bin_file.cast(link.File.Elf)) |_| {
|
||||
unreachable;
|
||||
} else if (emit.lower.bin_file.cast(link.File.MachO)) |macho_file| {
|
||||
const atom_index =
|
||||
macho_file.getAtomIndexForSymbol(.{ .sym_index = symbol.atom_index }).?;
|
||||
const target = if (link.File.MachO.global_symbol_bit & symbol.sym_index != 0)
|
||||
macho_file.getGlobalByIndex(link.File.MachO.global_symbol_mask & symbol.sym_index)
|
||||
else
|
||||
link.File.MachO.SymbolWithLoc{ .sym_index = symbol.sym_index };
|
||||
try link.File.MachO.Atom.addRelocation(macho_file, atom_index, .{
|
||||
.type = switch (lowered_relocs[0].target) {
|
||||
.linker_got => .got,
|
||||
.linker_direct => .signed,
|
||||
.linker_tlv => .tlv,
|
||||
else => unreachable,
|
||||
},
|
||||
.target = target,
|
||||
.offset = @intCast(end_offset - 4),
|
||||
.addend = 0,
|
||||
.pcrel = true,
|
||||
.length = 2,
|
||||
});
|
||||
_ = macho_file;
|
||||
@panic("TODO emitMir");
|
||||
// const atom_index =
|
||||
// macho_file.getAtomIndexForSymbol(.{ .sym_index = symbol.atom_index }).?;
|
||||
// const target = if (link.File.MachO.global_symbol_bit & symbol.sym_index != 0)
|
||||
// macho_file.getGlobalByIndex(link.File.MachO.global_symbol_mask & symbol.sym_index)
|
||||
// else
|
||||
// link.File.MachO.SymbolWithLoc{ .sym_index = symbol.sym_index };
|
||||
// try link.File.MachO.Atom.addRelocation(macho_file, atom_index, .{
|
||||
// .type = switch (lowered_relocs[0].target) {
|
||||
// .linker_got => .got,
|
||||
// .linker_direct => .signed,
|
||||
// .linker_tlv => .tlv,
|
||||
// else => unreachable,
|
||||
// },
|
||||
// .target = target,
|
||||
// .offset = @intCast(end_offset - 4),
|
||||
// .addend = 0,
|
||||
// .pcrel = true,
|
||||
// .length = 2,
|
||||
// });
|
||||
} else if (emit.lower.bin_file.cast(link.File.Coff)) |coff_file| {
|
||||
const atom_index = coff_file.getAtomIndexForSymbol(.{
|
||||
.sym_index = symbol.atom_index,
|
||||
|
||||
@ -984,20 +984,22 @@ fn genDeclRef(
|
||||
}
|
||||
return GenResult.mcv(.{ .load_symbol = sym.esym_index });
|
||||
} else if (lf.cast(link.File.MachO)) |macho_file| {
|
||||
_ = macho_file;
|
||||
if (is_extern) {
|
||||
// TODO make this part of getGlobalSymbol
|
||||
const name = zcu.intern_pool.stringToSlice(decl.name);
|
||||
const sym_name = try std.fmt.allocPrint(gpa, "_{s}", .{name});
|
||||
defer gpa.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 name = zcu.intern_pool.stringToSlice(decl.name);
|
||||
// const sym_name = try std.fmt.allocPrint(gpa, "_{s}", .{name});
|
||||
// defer gpa.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) {
|
||||
return GenResult.mcv(.{ .load_tlv = sym_index });
|
||||
}
|
||||
return GenResult.mcv(.{ .load_got = sym_index });
|
||||
// const atom_index = try macho_file.getOrCreateAtomForDecl(decl_index);
|
||||
// const sym_index = macho_file.getAtom(atom_index).getSymbolIndex().?;
|
||||
// if (is_threadlocal) {
|
||||
// return GenResult.mcv(.{ .load_tlv = sym_index });
|
||||
// }
|
||||
// return GenResult.mcv(.{ .load_got = sym_index });
|
||||
@panic("TODO genDeclRef");
|
||||
} else if (lf.cast(link.File.Coff)) |coff_file| {
|
||||
if (is_extern) {
|
||||
const name = zcu.intern_pool.stringToSlice(decl.name);
|
||||
|
||||
5272
src/link/MachO.zig
5272
src/link/MachO.zig
File diff suppressed because it is too large
Load Diff
@ -449,7 +449,7 @@ fn resolveRelocInner(
|
||||
if (rel.getTargetSymbol(macho_file).flags.got) {
|
||||
try writer.writeInt(i32, @intCast(G + A - P), .little);
|
||||
} else {
|
||||
try relaxGotLoad(code[rel_offset - 3 ..]);
|
||||
try x86_64.relaxGotLoad(code[rel_offset - 3 ..]);
|
||||
try writer.writeInt(i32, @intCast(S + A - P), .little);
|
||||
}
|
||||
},
|
||||
@ -463,7 +463,7 @@ fn resolveRelocInner(
|
||||
const S_: i64 = @intCast(sym.getTlvPtrAddress(macho_file));
|
||||
try writer.writeInt(i32, @intCast(S_ + A - P), .little);
|
||||
} else {
|
||||
try relaxTlv(code[rel_offset - 3 ..]);
|
||||
try x86_64.relaxTlv(code[rel_offset - 3 ..]);
|
||||
try writer.writeInt(i32, @intCast(S + A - P), .little);
|
||||
}
|
||||
},
|
||||
@ -631,43 +631,51 @@ fn resolveRelocInner(
|
||||
}
|
||||
}
|
||||
|
||||
fn relaxGotLoad(code: []u8) error{RelaxFail}!void {
|
||||
const old_inst = disassemble(code) orelse return error.RelaxFail;
|
||||
switch (old_inst.encoding.mnemonic) {
|
||||
.mov => {
|
||||
const inst = Instruction.new(old_inst.prefix, .lea, &old_inst.ops) catch return error.RelaxFail;
|
||||
relocs_log.debug(" relaxing {} => {}", .{ old_inst.encoding, inst.encoding });
|
||||
encode(&.{inst}, code) catch return error.RelaxFail;
|
||||
},
|
||||
else => return error.RelaxFail,
|
||||
const x86_64 = struct {
|
||||
fn relaxGotLoad(code: []u8) error{RelaxFail}!void {
|
||||
const old_inst = disassemble(code) orelse return error.RelaxFail;
|
||||
switch (old_inst.encoding.mnemonic) {
|
||||
.mov => {
|
||||
const inst = Instruction.new(old_inst.prefix, .lea, &old_inst.ops) catch return error.RelaxFail;
|
||||
relocs_log.debug(" relaxing {} => {}", .{ old_inst.encoding, inst.encoding });
|
||||
encode(&.{inst}, code) catch return error.RelaxFail;
|
||||
},
|
||||
else => return error.RelaxFail,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn relaxTlv(code: []u8) error{RelaxFail}!void {
|
||||
const old_inst = disassemble(code) orelse return error.RelaxFail;
|
||||
switch (old_inst.encoding.mnemonic) {
|
||||
.mov => {
|
||||
const inst = Instruction.new(old_inst.prefix, .lea, &old_inst.ops) catch return error.RelaxFail;
|
||||
relocs_log.debug(" relaxing {} => {}", .{ old_inst.encoding, inst.encoding });
|
||||
encode(&.{inst}, code) catch return error.RelaxFail;
|
||||
},
|
||||
else => return error.RelaxFail,
|
||||
fn relaxTlv(code: []u8) error{RelaxFail}!void {
|
||||
const old_inst = disassemble(code) orelse return error.RelaxFail;
|
||||
switch (old_inst.encoding.mnemonic) {
|
||||
.mov => {
|
||||
const inst = Instruction.new(old_inst.prefix, .lea, &old_inst.ops) catch return error.RelaxFail;
|
||||
relocs_log.debug(" relaxing {} => {}", .{ old_inst.encoding, inst.encoding });
|
||||
encode(&.{inst}, code) catch return error.RelaxFail;
|
||||
},
|
||||
else => return error.RelaxFail,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn disassemble(code: []const u8) ?Instruction {
|
||||
var disas = Disassembler.init(code);
|
||||
const inst = disas.next() catch return null;
|
||||
return inst;
|
||||
}
|
||||
|
||||
fn encode(insts: []const Instruction, code: []u8) !void {
|
||||
var stream = std.io.fixedBufferStream(code);
|
||||
const writer = stream.writer();
|
||||
for (insts) |inst| {
|
||||
try inst.encode(writer, .{});
|
||||
fn disassemble(code: []const u8) ?Instruction {
|
||||
var disas = Disassembler.init(code);
|
||||
const inst = disas.next() catch return null;
|
||||
return inst;
|
||||
}
|
||||
}
|
||||
|
||||
fn encode(insts: []const Instruction, code: []u8) !void {
|
||||
var stream = std.io.fixedBufferStream(code);
|
||||
const writer = stream.writer();
|
||||
for (insts) |inst| {
|
||||
try inst.encode(writer, .{});
|
||||
}
|
||||
}
|
||||
|
||||
const bits = @import("../../arch/x86_64/bits.zig");
|
||||
const encoder = @import("../../arch/x86_64/encoder.zig");
|
||||
const Disassembler = @import("../../arch/x86_64/Disassembler.zig");
|
||||
const Immediate = bits.Immediate;
|
||||
const Instruction = encoder.Instruction;
|
||||
};
|
||||
|
||||
pub fn calcNumRelocs(self: Atom, macho_file: *MachO) u32 {
|
||||
switch (macho_file.options.cpu_arch.?) {
|
||||
@ -879,24 +887,22 @@ pub const Loc = struct {
|
||||
len: usize = 0,
|
||||
};
|
||||
|
||||
const aarch64 = @import("../aarch64.zig");
|
||||
pub const Alignment = @import("../../InternPool.zig").Alignment;
|
||||
|
||||
const aarch64 = @import("../../arch/aarch64/bits.zig");
|
||||
const assert = std.debug.assert;
|
||||
const bind = @import("dyld_info/bind.zig");
|
||||
const dis_x86_64 = @import("dis_x86_64");
|
||||
const macho = std.macho;
|
||||
const math = std.math;
|
||||
const mem = std.mem;
|
||||
const log = std.log.scoped(.link);
|
||||
const relocs_log = std.log.scoped(.relocs);
|
||||
const std = @import("std");
|
||||
const trace = @import("../tracy.zig").trace;
|
||||
const trace = @import("../../tracy.zig").trace;
|
||||
|
||||
const Allocator = mem.Allocator;
|
||||
const Atom = @This();
|
||||
const Disassembler = dis_x86_64.Disassembler;
|
||||
const File = @import("file.zig").File;
|
||||
const Instruction = dis_x86_64.Instruction;
|
||||
const Immediate = dis_x86_64.Immediate;
|
||||
const MachO = @import("../MachO.zig");
|
||||
const Object = @import("Object.zig");
|
||||
const Relocation = @import("Relocation.zig");
|
||||
|
||||
@ -11,7 +11,6 @@ const Allocator = mem.Allocator;
|
||||
const Hasher = @import("hasher.zig").ParallelHasher;
|
||||
const MachO = @import("../MachO.zig");
|
||||
const Sha256 = std.crypto.hash.sha2.Sha256;
|
||||
const Zld = @import("../Zld.zig");
|
||||
|
||||
const hash_size = Sha256.digest_length;
|
||||
|
||||
|
||||
@ -461,7 +461,7 @@ const leb = std.leb;
|
||||
const log = std.log.scoped(.link);
|
||||
const mem = std.mem;
|
||||
const std = @import("std");
|
||||
const trace = @import("../tracy.zig").trace;
|
||||
const trace = @import("../../tracy.zig").trace;
|
||||
|
||||
const Allocator = mem.Allocator;
|
||||
const DwarfInfo = @This();
|
||||
|
||||
@ -12,7 +12,7 @@ symbols: std.ArrayListUnmanaged(Symbol.Index) = .{},
|
||||
dependents: std.ArrayListUnmanaged(Id) = .{},
|
||||
rpaths: std.StringArrayHashMapUnmanaged(void) = .{},
|
||||
umbrella: File.Index = 0,
|
||||
platform: ?MachO.Options.Platform = null,
|
||||
platform: ?MachO.Platform = null,
|
||||
|
||||
needed: bool,
|
||||
weak: bool,
|
||||
@ -815,7 +815,7 @@ const macho = std.macho;
|
||||
const math = std.math;
|
||||
const mem = std.mem;
|
||||
const tapi = @import("../tapi.zig");
|
||||
const trace = @import("../tracy.zig").trace;
|
||||
const trace = @import("../../tracy.zig").trace;
|
||||
const std = @import("std");
|
||||
|
||||
const Allocator = mem.Allocator;
|
||||
|
||||
@ -12,7 +12,7 @@ strtab: []const u8 = &[0]u8{},
|
||||
symbols: std.ArrayListUnmanaged(Symbol.Index) = .{},
|
||||
atoms: std.ArrayListUnmanaged(Atom.Index) = .{},
|
||||
|
||||
platform: ?MachO.Options.Platform = null,
|
||||
platform: ?MachO.Platform = null,
|
||||
dwarf_info: ?DwarfInfo = null,
|
||||
stab_files: std.ArrayListUnmanaged(StabFile) = .{},
|
||||
|
||||
@ -2075,7 +2075,7 @@ const log = std.log.scoped(.link);
|
||||
const macho = std.macho;
|
||||
const math = std.math;
|
||||
const mem = std.mem;
|
||||
const trace = @import("../tracy.zig").trace;
|
||||
const trace = @import("../../tracy.zig").trace;
|
||||
const std = @import("std");
|
||||
|
||||
const Allocator = mem.Allocator;
|
||||
@ -2088,6 +2088,5 @@ const LoadCommandIterator = macho.LoadCommandIterator;
|
||||
const MachO = @import("../MachO.zig");
|
||||
const Object = @This();
|
||||
const Relocation = @import("Relocation.zig");
|
||||
const StringTable = @import("../strtab.zig").StringTable;
|
||||
const Symbol = @import("Symbol.zig");
|
||||
const UnwindInfo = @import("UnwindInfo.zig");
|
||||
|
||||
@ -670,7 +670,7 @@ const log = std.log.scoped(.link);
|
||||
const macho = std.macho;
|
||||
const math = std.math;
|
||||
const mem = std.mem;
|
||||
const trace = @import("../tracy.zig").trace;
|
||||
const trace = @import("../../tracy.zig").trace;
|
||||
|
||||
const Allocator = mem.Allocator;
|
||||
const Atom = @import("Atom.zig");
|
||||
|
||||
@ -193,7 +193,7 @@ const log = std.log.scoped(.dead_strip);
|
||||
const macho = std.macho;
|
||||
const math = std.math;
|
||||
const mem = std.mem;
|
||||
const trace = @import("../tracy.zig").trace;
|
||||
const trace = @import("../../tracy.zig").trace;
|
||||
const track_live_log = std.log.scoped(.dead_strip_track_live);
|
||||
const std = @import("std");
|
||||
|
||||
|
||||
@ -559,7 +559,7 @@ const macho = std.macho;
|
||||
const math = std.math;
|
||||
const mem = std.mem;
|
||||
const std = @import("std");
|
||||
const trace = @import("../tracy.zig").trace;
|
||||
const trace = @import("../../tracy.zig").trace;
|
||||
|
||||
const Allocator = std.mem.Allocator;
|
||||
const Atom = @import("Atom.zig");
|
||||
|
||||
@ -67,7 +67,7 @@ const assert = std.debug.assert;
|
||||
const fs = std.fs;
|
||||
const mem = std.mem;
|
||||
const std = @import("std");
|
||||
const trace = @import("../tracy.zig").trace;
|
||||
const trace = @import("../../tracy.zig").trace;
|
||||
|
||||
const Allocator = mem.Allocator;
|
||||
const ThreadPool = std.Thread.Pool;
|
||||
|
||||
@ -160,14 +160,14 @@ const max_distance = (1 << (jump_bits - 1));
|
||||
/// and assume margin to be 5MiB.
|
||||
const max_allowed_distance = max_distance - 0x500_000;
|
||||
|
||||
const aarch64 = @import("../aarch64.zig");
|
||||
const aarch64 = @import("../../arch/aarch64/bits.zig");
|
||||
const assert = std.debug.assert;
|
||||
const log = std.log.scoped(.link);
|
||||
const macho = std.macho;
|
||||
const math = std.math;
|
||||
const mem = std.mem;
|
||||
const std = @import("std");
|
||||
const trace = @import("../tracy.zig").trace;
|
||||
const trace = @import("../../tracy.zig").trace;
|
||||
|
||||
const Allocator = mem.Allocator;
|
||||
const Atom = @import("Atom.zig");
|
||||
|
||||
@ -47,7 +47,7 @@ inline fn conform(out: *[Md5.digest_length]u8) void {
|
||||
const fs = std.fs;
|
||||
const mem = std.mem;
|
||||
const std = @import("std");
|
||||
const trace = @import("../tracy.zig").trace;
|
||||
const trace = @import("../../tracy.zig").trace;
|
||||
|
||||
const Allocator = mem.Allocator;
|
||||
const Md5 = std.crypto.hash.Md5;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user