authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-11-17 16:00:01+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-11-17 20:49:32+01:00
log91329ce9441e8711e954b91c8ae5cbc81de1c796
treedf4c6c1003f576fbce00e75074f3958dbb102075
parent5f864140194c91a60cb8e132a7596b555971e808

std.macho: fix LoadCommandIterator to work with underaligned data


2 files changed, 8 insertions(+), 14 deletions(-)

lib/std/macho.zig+7-13
......@@ -1870,18 +1870,15 @@ pub const LoadCommandIterator = struct {
18701870
18711871 pub fn cast(lc: LoadCommand, comptime Cmd: type) ?Cmd {
18721872 if (lc.data.len < @sizeOf(Cmd)) return null;
1873 return @as(*const Cmd, @ptrCast(@alignCast(&lc.data[0]))).*;
1873 return @as(*align(1) const Cmd, @ptrCast(lc.data.ptr)).*;
18741874 }
18751875
18761876 /// Asserts LoadCommand is of type segment_command_64.
1877 pub fn getSections(lc: LoadCommand) []const section_64 {
1877 pub fn getSections(lc: LoadCommand) []align(1) const section_64 {
18781878 const segment_lc = lc.cast(segment_command_64).?;
18791879 if (segment_lc.nsects == 0) return &[0]section_64{};
18801880 const data = lc.data[@sizeOf(segment_command_64)..];
1881 const sections = @as(
1882 [*]const section_64,
1883 @ptrCast(@alignCast(&data[0])),
1884 )[0..segment_lc.nsects];
1881 const sections = @as([*]align(1) const section_64, @ptrCast(data.ptr))[0..segment_lc.nsects];
18851882 return sections;
18861883 }
18871884
......@@ -1900,12 +1897,12 @@ pub const LoadCommandIterator = struct {
19001897 }
19011898
19021899 /// Asserts LoadCommand is of type build_version_command.
1903 pub fn getBuildVersionTools(lc: LoadCommand) []const build_tool_version {
1900 pub fn getBuildVersionTools(lc: LoadCommand) []align(1) const build_tool_version {
19041901 const build_lc = lc.cast(build_version_command).?;
19051902 const ntools = build_lc.ntools;
19061903 if (ntools == 0) return &[0]build_tool_version{};
19071904 const data = lc.data[@sizeOf(build_version_command)..];
1908 const tools = @as([*]const build_tool_version, @ptrCast(@alignCast(&data[0])))[0..ntools];
1905 const tools = @as([*]align(1) const build_tool_version, @ptrCast(data.ptr))[0..ntools];
19091906 return tools;
19101907 }
19111908 };
......@@ -1913,16 +1910,13 @@ pub const LoadCommandIterator = struct {
19131910 pub fn next(it: *LoadCommandIterator) ?LoadCommand {
19141911 if (it.index >= it.ncmds) return null;
19151912
1916 const hdr = @as(
1917 *const load_command,
1918 @ptrCast(@alignCast(&it.buffer[0])),
1919 ).*;
1913 const hdr = @as(*align(1) const load_command, @ptrCast(it.buffer.ptr)).*;
19201914 const cmd = LoadCommand{
19211915 .hdr = hdr,
19221916 .data = it.buffer[0..hdr.cmdsize],
19231917 };
19241918
1925 it.buffer = @alignCast(it.buffer[hdr.cmdsize..]);
1919 it.buffer = it.buffer[hdr.cmdsize..];
19261920 it.index += 1;
19271921
19281922 return cmd;
src/link/MachO/Object.zig+1-1
......@@ -877,7 +877,7 @@ pub fn getSourceSectionIndexByName(self: Object, segname: []const u8, sectname:
877877 } else return null;
878878}
879879
880pub fn getSourceSections(self: Object) []const macho.section_64 {
880pub fn getSourceSections(self: Object) []align(1) const macho.section_64 {
881881 var it = LoadCommandIterator{
882882 .ncmds = self.header.ncmds,
883883 .buffer = self.contents[@sizeOf(macho.mach_header_64)..][0..self.header.sizeofcmds],