authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-16 15:38:35+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-18 09:13:09+02:00
loge9328e7da81300f1b3d3ebf0c91c6cc650e2747b
tree11d319db18a87a585744089df04f6214a048bd91
parent33388130775672df611091910e0cb1482a7eeb02

macho: fix 32bit compilation issues


6 files changed, 48 insertions(+), 29 deletions(-)

src/link/MachO.zig+3-2
......@@ -2343,7 +2343,8 @@ fn resizeSections(self: *MachO) !void {
23432343 if (header.isZerofill()) continue;
23442344 if (self.isZigSection(@intCast(n_sect))) continue; // TODO this is horrible
23452345 const cpu_arch = self.getTarget().cpu.arch;
2346 try out.resize(self.base.comp.gpa, header.size);
2346 const size = math.cast(usize, header.size) orelse return error.Overflow;
2347 try out.resize(self.base.comp.gpa, size);
23472348 const padding_byte: u8 = if (header.isCode() and cpu_arch == .x86_64) 0xcc else 0;
23482349 @memset(out.items, padding_byte);
23492350 }
......@@ -2368,7 +2369,7 @@ fn writeSectionsAndUpdateLinkeditSizes(self: *MachO) !void {
23682369 }
23692370 for (self.thunks.items) |thunk| {
23702371 const out = self.sections.items(.out)[thunk.out_n_sect].items;
2371 const off = thunk.value;
2372 const off = math.cast(usize, thunk.value) orelse return error.Overflow;
23722373 const size = thunk.size();
23732374 var stream = std.io.fixedBufferStream(out[off..][0..size]);
23742375 try thunk.write(self, stream.writer());
src/link/MachO/Atom.zig+1-1
......@@ -983,7 +983,7 @@ pub fn writeRelocs(self: Atom, macho_file: *MachO, code: []u8, buffer: []macho.r
983983 var i: usize = 0;
984984 for (relocs) |rel| {
985985 defer i += 1;
986 const rel_offset = rel.offset - self.off;
986 const rel_offset = math.cast(usize, rel.offset - self.off) orelse return error.Overflow;
987987 const r_address: i32 = math.cast(i32, self.value + rel_offset) orelse return error.Overflow;
988988 assert(r_address >= 0);
989989 const r_symbolnum = r_symbolnum: {
src/link/MachO/InternalObject.zig+6-4
......@@ -415,8 +415,9 @@ pub fn resolveLiterals(self: *InternalObject, lp: *MachO.LiteralPool, macho_file
415415 const rel = relocs[0];
416416 assert(rel.tag == .@"extern");
417417 const target = rel.getTargetSymbol(atom.*, macho_file).getAtom(macho_file).?;
418 try buffer.ensureUnusedCapacity(target.size);
419 buffer.resize(target.size) catch unreachable;
418 const target_size = std.math.cast(usize, target.size) orelse return error.Overflow;
419 try buffer.ensureUnusedCapacity(target_size);
420 buffer.resize(target_size) catch unreachable;
420421 @memcpy(buffer.items, try self.getSectionData(target.n_sect));
421422 const res = try lp.insert(gpa, header.type(), buffer.items);
422423 buffer.clearRetainingCapacity();
......@@ -572,8 +573,9 @@ pub fn writeAtoms(self: *InternalObject, macho_file: *MachO) !void {
572573 if (!atom.flags.alive) continue;
573574 const sect = atom.getInputSection(macho_file);
574575 if (sect.isZerofill()) continue;
575 const off = atom.value;
576 const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items[off..][0..atom.size];
576 const off = std.math.cast(usize, atom.value) orelse return error.Overflow;
577 const size = std.math.cast(usize, atom.size) orelse return error.Overflow;
578 const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items[off..][0..size];
577579 @memcpy(buffer, try self.getSectionData(atom.n_sect));
578580 try atom.resolveRelocs(macho_file, buffer);
579581 }
src/link/MachO/Object.zig+28-15
......@@ -1015,7 +1015,8 @@ fn initEhFrameRecords(self: *Object, allocator: Allocator, sect_id: u8, file: Fi
10151015 const sect = slice.items(.header)[sect_id];
10161016 const relocs = slice.items(.relocs)[sect_id];
10171017
1018 try self.eh_frame_data.resize(allocator, sect.size);
1018 const size = math.cast(usize, sect.size) orelse return error.Overflow;
1019 try self.eh_frame_data.resize(allocator, size);
10191020 const amt = try file.preadAll(self.eh_frame_data.items, sect.offset + self.offset);
10201021 if (amt != self.eh_frame_data.items.len) return error.InputOutput;
10211022
......@@ -1116,7 +1117,8 @@ fn initUnwindRecords(self: *Object, allocator: Allocator, sect_id: u8, file: Fil
11161117 };
11171118
11181119 const header = self.sections.items(.header)[sect_id];
1119 const data = try allocator.alloc(u8, header.size);
1120 const size = math.cast(usize, header.size) orelse return error.Overflow;
1121 const data = try allocator.alloc(u8, size);
11201122 defer allocator.free(data);
11211123 const amt = try file.preadAll(data, header.offset + self.offset);
11221124 if (amt != data.len) return error.InputOutput;
......@@ -1346,7 +1348,8 @@ fn parseDebugInfo(self: *Object, macho_file: *MachO) !void {
13461348 const file = macho_file.getFileHandle(self.file_handle);
13471349 const debug_info = blk: {
13481350 const sect = slice.items(.header)[debug_info_index.?];
1349 const data = try gpa.alloc(u8, sect.size);
1351 const size = math.cast(usize, sect.size) orelse return error.Overflow;
1352 const data = try gpa.alloc(u8, size);
13501353 const amt = try file.preadAll(data, sect.offset + self.offset);
13511354 if (amt != data.len) return error.InputOutput;
13521355 break :blk data;
......@@ -1354,7 +1357,8 @@ fn parseDebugInfo(self: *Object, macho_file: *MachO) !void {
13541357 defer gpa.free(debug_info);
13551358 const debug_abbrev = blk: {
13561359 const sect = slice.items(.header)[debug_abbrev_index.?];
1357 const data = try gpa.alloc(u8, sect.size);
1360 const size = math.cast(usize, sect.size) orelse return error.Overflow;
1361 const data = try gpa.alloc(u8, size);
13581362 const amt = try file.preadAll(data, sect.offset + self.offset);
13591363 if (amt != data.len) return error.InputOutput;
13601364 break :blk data;
......@@ -1362,7 +1366,8 @@ fn parseDebugInfo(self: *Object, macho_file: *MachO) !void {
13621366 defer gpa.free(debug_abbrev);
13631367 const debug_str = if (debug_str_index) |sid| blk: {
13641368 const sect = slice.items(.header)[sid];
1365 const data = try gpa.alloc(u8, sect.size);
1369 const size = math.cast(usize, sect.size) orelse return error.Overflow;
1370 const data = try gpa.alloc(u8, size);
13661371 const amt = try file.preadAll(data, sect.offset + self.offset);
13671372 if (amt != data.len) return error.InputOutput;
13681373 break :blk data;
......@@ -1864,7 +1869,8 @@ pub fn writeAtoms(self: *Object, macho_file: *MachO) !void {
18641869
18651870 for (headers, 0..) |header, n_sect| {
18661871 if (header.isZerofill()) continue;
1867 const data = try gpa.alloc(u8, header.size);
1872 const size = math.cast(usize, header.size) orelse return error.Overflow;
1873 const data = try gpa.alloc(u8, size);
18681874 const amt = try file.preadAll(data, header.offset + self.offset);
18691875 if (amt != data.len) return error.InputOutput;
18701876 sections_data[n_sect] = data;
......@@ -1874,11 +1880,13 @@ pub fn writeAtoms(self: *Object, macho_file: *MachO) !void {
18741880 if (!atom.flags.alive) continue;
18751881 const sect = atom.getInputSection(macho_file);
18761882 if (sect.isZerofill()) continue;
1877 const off = atom.value;
1883 const value = math.cast(usize, atom.value) orelse return error.Overflow;
1884 const off = math.cast(usize, atom.off) orelse return error.Overflow;
1885 const size = math.cast(usize, atom.size) orelse return error.Overflow;
18781886 const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items;
18791887 const data = sections_data[atom.n_sect];
1880 @memcpy(buffer[off..][0..atom.size], data[atom.off..][0..atom.size]);
1881 try atom.resolveRelocs(macho_file, buffer[off..][0..atom.size]);
1888 @memcpy(buffer[value..][0..size], data[off..][0..size]);
1889 try atom.resolveRelocs(macho_file, buffer[value..][0..size]);
18821890 }
18831891}
18841892
......@@ -1900,7 +1908,8 @@ pub fn writeAtomsRelocatable(self: *Object, macho_file: *MachO) !void {
19001908
19011909 for (headers, 0..) |header, n_sect| {
19021910 if (header.isZerofill()) continue;
1903 const data = try gpa.alloc(u8, header.size);
1911 const size = math.cast(usize, header.size) orelse return error.Overflow;
1912 const data = try gpa.alloc(u8, size);
19041913 const amt = try file.preadAll(data, header.offset + self.offset);
19051914 if (amt != data.len) return error.InputOutput;
19061915 sections_data[n_sect] = data;
......@@ -1910,13 +1919,15 @@ pub fn writeAtomsRelocatable(self: *Object, macho_file: *MachO) !void {
19101919 if (!atom.flags.alive) continue;
19111920 const sect = atom.getInputSection(macho_file);
19121921 if (sect.isZerofill()) continue;
1913 const off = atom.value;
1922 const value = math.cast(usize, atom.value) orelse return error.Overflow;
1923 const off = math.cast(usize, atom.off) orelse return error.Overflow;
1924 const size = math.cast(usize, atom.size) orelse return error.Overflow;
19141925 const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items;
19151926 const data = sections_data[atom.n_sect];
1916 @memcpy(buffer[off..][0..atom.size], data[atom.off..][0..atom.size]);
1927 @memcpy(buffer[value..][0..size], data[off..][0..size]);
19171928 const relocs = macho_file.sections.items(.relocs)[atom.out_n_sect].items;
19181929 const extra = atom.getExtra(macho_file);
1919 try atom.writeRelocs(macho_file, buffer[off..][0..atom.size], relocs[extra.rel_out_index..][0..extra.rel_out_count]);
1930 try atom.writeRelocs(macho_file, buffer[value..][0..size], relocs[extra.rel_out_index..][0..extra.rel_out_count]);
19201931 }
19211932}
19221933
......@@ -2812,7 +2823,8 @@ const x86_64 = struct {
28122823 }
28132824 const relocs = @as([*]align(1) const macho.relocation_info, @ptrCast(relocs_buffer.ptr))[0..sect.nreloc];
28142825
2815 const code = try gpa.alloc(u8, sect.size);
2826 const sect_size = math.cast(usize, sect.size) orelse return error.Overflow;
2827 const code = try gpa.alloc(u8, sect_size);
28162828 defer gpa.free(code);
28172829 {
28182830 const amt = try handle.preadAll(code, sect.offset + self.offset);
......@@ -2984,7 +2996,8 @@ const aarch64 = struct {
29842996 }
29852997 const relocs = @as([*]align(1) const macho.relocation_info, @ptrCast(relocs_buffer.ptr))[0..sect.nreloc];
29862998
2987 const code = try gpa.alloc(u8, sect.size);
2999 const sect_size = math.cast(usize, sect.size) orelse return error.Overflow;
3000 const code = try gpa.alloc(u8, sect_size);
29883001 defer gpa.free(code);
29893002 {
29903003 const amt = try handle.preadAll(code, sect.offset + self.offset);
src/link/MachO/ZigObject.zig+8-6
......@@ -529,12 +529,13 @@ pub fn writeAtomsRelocatable(self: *ZigObject, macho_file: *MachO) !void {
529529 if (sect.isZerofill()) continue;
530530 if (macho_file.isZigSection(atom.out_n_sect)) continue;
531531 if (atom.getRelocs(macho_file).len == 0) continue;
532 const off = atom.value;
532 const off = std.math.cast(usize, atom.value) orelse return error.Overflow;
533 const size = std.math.cast(usize, atom.size) orelse return error.Overflow;
533534 const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items;
534 try self.getAtomData(macho_file, atom.*, buffer[off..][0..atom.size]);
535 try self.getAtomData(macho_file, atom.*, buffer[off..][0..size]);
535536 const relocs = macho_file.sections.items(.relocs)[atom.out_n_sect].items;
536537 const extra = atom.getExtra(macho_file);
537 try atom.writeRelocs(macho_file, buffer[off..][0..atom.size], relocs[extra.rel_out_index..][0..extra.rel_out_count]);
538 try atom.writeRelocs(macho_file, buffer[off..][0..size], relocs[extra.rel_out_index..][0..extra.rel_out_count]);
538539 }
539540}
540541
......@@ -551,10 +552,11 @@ pub fn writeAtoms(self: *ZigObject, macho_file: *MachO) !void {
551552 const sect = atom.getInputSection(macho_file);
552553 if (sect.isZerofill()) continue;
553554 if (macho_file.isZigSection(atom.out_n_sect)) continue;
554 const off = atom.value;
555 const off = std.math.cast(usize, atom.value) orelse return error.Overflow;
556 const size = std.math.cast(usize, atom.size) orelse return error.Overflow;
555557 const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items;
556 try self.getAtomData(macho_file, atom.*, buffer[off..][0..atom.size]);
557 try atom.resolveRelocs(macho_file, buffer[off..][0..atom.size]);
558 try self.getAtomData(macho_file, atom.*, buffer[off..][0..size]);
559 try atom.resolveRelocs(macho_file, buffer[off..][0..size]);
558560 }
559561}
560562
src/link/MachO/relocatable.zig+2-1
......@@ -626,7 +626,8 @@ fn writeSections(macho_file: *MachO) !void {
626626 for (slice.items(.header), slice.items(.out), slice.items(.relocs), 0..) |header, *out, *relocs, n_sect| {
627627 if (header.isZerofill()) continue;
628628 if (!macho_file.isZigSection(@intCast(n_sect))) { // TODO this is wrong; what about debug sections?
629 try out.resize(gpa, header.size);
629 const size = math.cast(usize, header.size) orelse return error.Overflow;
630 try out.resize(gpa, size);
630631 const padding_byte: u8 = if (header.isCode() and cpu_arch == .x86_64) 0xcc else 0;
631632 @memset(out.items, padding_byte);
632633 }