aboutsummaryrefslogtreecommitdiff
path: root/src/arch
diff options
context:
space:
mode:
authorJakub Konka <kubkon@jakubkonka.com>2023-04-05 08:23:46 +0200
committerJakub Konka <kubkon@jakubkonka.com>2023-04-13 11:47:51 +0200
commiteba280ce20d308ec0abb17719ab4cc43a27901eb (patch)
treeb9bbac734e4c82bb2487f0921e26fce627291578 /src/arch
parent1795b8eb4e8db95cb639349c6c1bc4b0b82741bb (diff)
downloadzig-eba280ce20d308ec0abb17719ab4cc43a27901eb.tar.gz
zig-eba280ce20d308ec0abb17719ab4cc43a27901eb.zip
macho: refactor relocation type in incremental linker
Diffstat (limited to 'src/arch')
-rw-r--r--src/arch/aarch64/Emit.zig29
-rw-r--r--src/arch/x86_64/Emit.zig13
2 files changed, 16 insertions, 26 deletions
diff --git a/src/arch/aarch64/Emit.zig b/src/arch/aarch64/Emit.zig
index b2e23c6278..3903229a21 100644
--- a/src/arch/aarch64/Emit.zig
+++ b/src/arch/aarch64/Emit.zig
@@ -673,7 +673,7 @@ fn mirCallExtern(emit: *Emit, inst: Mir.Inst.Index) !void {
const atom_index = macho_file.getAtomIndexForSymbol(.{ .sym_index = relocation.atom_index, .file = null }).?;
const target = macho_file.getGlobalByIndex(relocation.sym_index);
try link.File.MachO.Atom.addRelocation(macho_file, atom_index, .{
- .type = @enumToInt(std.macho.reloc_type_arm64.ARM64_RELOC_BRANCH26),
+ .type = .branch,
.target = target,
.offset = offset,
.addend = 0,
@@ -883,41 +883,32 @@ 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, .file = null }).?;
- // TODO this causes segfault in stage1
- // try atom.addRelocations(macho_file, 2, .{
- try link.File.MachO.Atom.addRelocation(macho_file, atom_index, .{
+ try Atom.addRelocations(macho_file, atom_index, &[_]Relocation{ .{
.target = .{ .sym_index = data.sym_index, .file = null },
.offset = offset,
.addend = 0,
.pcrel = true,
.length = 2,
.type = switch (tag) {
- .load_memory_got,
- .load_memory_ptr_got,
- => @enumToInt(std.macho.reloc_type_arm64.ARM64_RELOC_GOT_LOAD_PAGE21),
- .load_memory_direct,
- .load_memory_ptr_direct,
- => @enumToInt(std.macho.reloc_type_arm64.ARM64_RELOC_PAGE21),
+ .load_memory_got, .load_memory_ptr_got => Relocation.Type.got_page,
+ .load_memory_direct, .load_memory_ptr_direct => Relocation.Type.page,
else => unreachable,
},
- });
- try link.File.MachO.Atom.addRelocation(macho_file, atom_index, .{
+ }, .{
.target = .{ .sym_index = data.sym_index, .file = null },
.offset = offset + 4,
.addend = 0,
.pcrel = false,
.length = 2,
.type = switch (tag) {
- .load_memory_got,
- .load_memory_ptr_got,
- => @enumToInt(std.macho.reloc_type_arm64.ARM64_RELOC_GOT_LOAD_PAGEOFF12),
- .load_memory_direct,
- .load_memory_ptr_direct,
- => @enumToInt(std.macho.reloc_type_arm64.ARM64_RELOC_PAGEOFF12),
+ .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) {
diff --git a/src/arch/x86_64/Emit.zig b/src/arch/x86_64/Emit.zig
index 30d63b6adf..16fb8a0183 100644
--- a/src/arch/x86_64/Emit.zig
+++ b/src/arch/x86_64/Emit.zig
@@ -42,7 +42,7 @@ pub fn emitMir(emit: *Emit) Error!void {
).?;
const target = macho_file.getGlobalByIndex(inst.data.relocation.sym_index);
try link.File.MachO.Atom.addRelocation(macho_file, atom_index, .{
- .type = @enumToInt(std.macho.reloc_type_x86_64.X86_64_RELOC_BRANCH),
+ .type = .branch,
.target = target,
.offset = end_offset - 4,
.addend = 0,
@@ -68,17 +68,16 @@ pub fn emitMir(emit: *Emit) Error!void {
.mov_linker, .lea_linker => if (emit.bin_file.cast(link.File.MachO)) |macho_file| {
const metadata =
emit.lower.mir.extraData(Mir.LeaRegisterReloc, inst.data.payload).data;
- const reloc_type = switch (inst.ops) {
- .got_reloc => @enumToInt(std.macho.reloc_type_x86_64.X86_64_RELOC_GOT),
- .direct_reloc => @enumToInt(std.macho.reloc_type_x86_64.X86_64_RELOC_SIGNED),
- else => unreachable,
- };
const atom_index = macho_file.getAtomIndexForSymbol(.{
.sym_index = metadata.atom_index,
.file = null,
}).?;
try link.File.MachO.Atom.addRelocation(macho_file, atom_index, .{
- .type = reloc_type,
+ .type = switch (inst.ops) {
+ .got_reloc => .got,
+ .direct_reloc => .signed,
+ else => unreachable,
+ },
.target = .{ .sym_index = metadata.sym_index, .file = null },
.offset = @intCast(u32, end_offset - 4),
.addend = 0,