Skip to content

Commit

Permalink
macho: sort all relocs in descending order to improve tools compat
Browse files Browse the repository at this point in the history
  • Loading branch information
kubkon committed Jan 8, 2024
1 parent 980a832 commit 80c0592
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/MachO/relocatable.zig
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@ fn allocateSections(macho_file: *MachO) !u32 {
return fileoff;
}

// We need to sort relocations in descending order to be compatible with Apple's linker.
fn sortReloc(ctx: void, lhs: macho.relocation_info, rhs: macho.relocation_info) bool {
_ = ctx;
return lhs.r_address > rhs.r_address;
}

fn writeAtoms(macho_file: *MachO) !void {
const tracy = trace(@src());
defer tracy.end();
Expand Down Expand Up @@ -238,6 +244,8 @@ fn writeAtoms(macho_file: *MachO) !void {

assert(relocs.items.len == header.nreloc);

mem.sort(macho.relocation_info, relocs.items, {}, sortReloc);

// TODO scattered writes?
try macho_file.base.file.pwriteAll(code, header.offset);
try macho_file.base.file.pwriteAll(mem.sliceAsBytes(relocs.items), header.reloff);
Expand Down Expand Up @@ -324,6 +332,8 @@ fn writeCompactUnwind(macho_file: *MachO) !void {
assert(entries.items.len == nrecs);
assert(relocs.items.len == header.nreloc);

mem.sort(macho.relocation_info, relocs.items, {}, sortReloc);

// TODO scattered writes?
try macho_file.base.file.pwriteAll(mem.sliceAsBytes(entries.items), header.offset);
try macho_file.base.file.pwriteAll(mem.sliceAsBytes(relocs.items), header.reloff);
Expand All @@ -343,6 +353,8 @@ fn writeEhFrame(macho_file: *MachO) !void {
try eh_frame.writeRelocs(macho_file, code, &relocs);
assert(relocs.items.len == header.nreloc);

mem.sort(macho.relocation_info, relocs.items, {}, sortReloc);

// TODO scattered writes?
try macho_file.base.file.pwriteAll(code, header.offset);
try macho_file.base.file.pwriteAll(mem.sliceAsBytes(relocs.items), header.reloff);
Expand Down

0 comments on commit 80c0592

Please sign in to comment.