Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macho: sort all relocs in descending order to improve tools compat #101

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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