Skip to content

Commit 45c444f

Browse files
kubkonandrewrk
authored andcommitted
macho: allow unaligned offsets in object files
1 parent e218b7e commit 45c444f

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/link/MachO/Object.zig

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,11 @@ pub fn parse(self: *Object, allocator: Allocator, cpu_arch: std.Target.Cpu.Arch)
9999
},
100100
.SYMTAB => {
101101
const symtab = cmd.cast(macho.symtab_command).?;
102-
self.in_symtab = @ptrCast(
103-
[*]const macho.nlist_64,
104-
@alignCast(@alignOf(macho.nlist_64), &self.contents[symtab.symoff]),
105-
)[0..symtab.nsyms];
102+
// Sadly, SYMTAB may be at an unaligned offset within the object file.
103+
self.in_symtab = @alignCast(@alignOf(macho.nlist_64), @ptrCast(
104+
[*]align(1) const macho.nlist_64,
105+
self.contents.ptr + symtab.symoff,
106+
))[0..symtab.nsyms];
106107
self.in_strtab = self.contents[symtab.stroff..][0..symtab.strsize];
107108
try self.symtab.appendSlice(allocator, self.in_symtab);
108109
},
@@ -302,10 +303,10 @@ pub fn splitIntoAtomsOneShot(self: *Object, macho_file: *MachO, object_id: u32)
302303
const code: ?[]const u8 = if (!sect.isZerofill()) try self.getSectionContents(sect) else null;
303304

304305
// Read section's list of relocations
305-
const relocs = @ptrCast(
306-
[*]const macho.relocation_info,
307-
@alignCast(@alignOf(macho.relocation_info), &self.contents[sect.reloff]),
308-
)[0..sect.nreloc];
306+
const relocs = @alignCast(@alignOf(macho.relocation_info), @ptrCast(
307+
[*]align(1) const macho.relocation_info,
308+
self.contents.ptr + sect.reloff,
309+
))[0..sect.nreloc];
309310

310311
// Symbols within this section only.
311312
const filtered_syms = filterSymbolsByAddress(
@@ -548,10 +549,10 @@ pub fn parseDataInCode(self: Object) ?[]const macho.data_in_code_entry {
548549
.DATA_IN_CODE => {
549550
const dice = cmd.cast(macho.linkedit_data_command).?;
550551
const ndice = @divExact(dice.datasize, @sizeOf(macho.data_in_code_entry));
551-
return @ptrCast(
552-
[*]const macho.data_in_code_entry,
553-
@alignCast(@alignOf(macho.data_in_code_entry), &self.contents[dice.dataoff]),
554-
)[0..ndice];
552+
return @alignCast(@alignOf(macho.data_in_code_entry), @ptrCast(
553+
[*]align(1) const macho.data_in_code_entry,
554+
self.contents.ptr + dice.dataoff,
555+
))[0..ndice];
555556
},
556557
else => {},
557558
}

0 commit comments

Comments
 (0)