macho: ensure local syms buffer is nlist_64 aligned when re-reading from file

This commit is contained in:
Jakub Konka 2023-02-07 02:54:18 +01:00
parent f63eda3f6a
commit 4b1a883d35
2 changed files with 5 additions and 3 deletions

View File

@ -166,6 +166,8 @@ pub fn parseFromBinary(
const symtab_cmd = cmd.cast(macho.symtab_command).?;
const symtab = @ptrCast(
[*]const macho.nlist_64,
// Alignment is guaranteed as a dylib is a final linked image and has to have sections
// properly aligned in order to be correctly loaded by the loader.
@alignCast(@alignOf(macho.nlist_64), &data[symtab_cmd.symoff]),
)[0..symtab_cmd.nsyms];
const strtab = data[symtab_cmd.stroff..][0..symtab_cmd.strsize];

View File

@ -2697,12 +2697,12 @@ pub const Zld = struct {
// Exclude region comprising all symbol stabs.
const nlocals = self.dysymtab_cmd.nlocalsym;
const locals_buf = try self.gpa.alloc(u8, nlocals * @sizeOf(macho.nlist_64));
defer self.gpa.free(locals_buf);
const locals = try self.gpa.alloc(macho.nlist_64, nlocals);
defer self.gpa.free(locals);
const locals_buf = @ptrCast([*]u8, locals.ptr)[0 .. @sizeOf(macho.nlist_64) * nlocals];
const amt = try self.file.preadAll(locals_buf, self.symtab_cmd.symoff);
if (amt != locals_buf.len) return error.InputOutput;
const locals = @ptrCast([*]macho.nlist_64, @alignCast(@alignOf(macho.nlist_64), locals_buf))[0..nlocals];
const istab: usize = for (locals) |local, i| {
if (local.stab()) break i;