macho: allow unaligned offsets in object files

This commit is contained in:
Jakub Konka
2022-08-11 00:06:31 +02:00
committed by Andrew Kelley
parent e218b7ea0c
commit 45c444ff18

View File

@@ -99,10 +99,11 @@ pub fn parse(self: *Object, allocator: Allocator, cpu_arch: std.Target.Cpu.Arch)
},
.SYMTAB => {
const symtab = cmd.cast(macho.symtab_command).?;
self.in_symtab = @ptrCast(
[*]const macho.nlist_64,
@alignCast(@alignOf(macho.nlist_64), &self.contents[symtab.symoff]),
)[0..symtab.nsyms];
// Sadly, SYMTAB may be at an unaligned offset within the object file.
self.in_symtab = @alignCast(@alignOf(macho.nlist_64), @ptrCast(
[*]align(1) const macho.nlist_64,
self.contents.ptr + symtab.symoff,
))[0..symtab.nsyms];
self.in_strtab = self.contents[symtab.stroff..][0..symtab.strsize];
try self.symtab.appendSlice(allocator, self.in_symtab);
},
@@ -302,10 +303,10 @@ pub fn splitIntoAtomsOneShot(self: *Object, macho_file: *MachO, object_id: u32)
const code: ?[]const u8 = if (!sect.isZerofill()) try self.getSectionContents(sect) else null;
// Read section's list of relocations
const relocs = @ptrCast(
[*]const macho.relocation_info,
@alignCast(@alignOf(macho.relocation_info), &self.contents[sect.reloff]),
)[0..sect.nreloc];
const relocs = @alignCast(@alignOf(macho.relocation_info), @ptrCast(
[*]align(1) const macho.relocation_info,
self.contents.ptr + sect.reloff,
))[0..sect.nreloc];
// Symbols within this section only.
const filtered_syms = filterSymbolsByAddress(
@@ -548,10 +549,10 @@ pub fn parseDataInCode(self: Object) ?[]const macho.data_in_code_entry {
.DATA_IN_CODE => {
const dice = cmd.cast(macho.linkedit_data_command).?;
const ndice = @divExact(dice.datasize, @sizeOf(macho.data_in_code_entry));
return @ptrCast(
[*]const macho.data_in_code_entry,
@alignCast(@alignOf(macho.data_in_code_entry), &self.contents[dice.dataoff]),
)[0..ndice];
return @alignCast(@alignOf(macho.data_in_code_entry), @ptrCast(
[*]align(1) const macho.data_in_code_entry,
self.contents.ptr + dice.dataoff,
))[0..ndice];
},
else => {},
}