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