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 {...@@ -2343,7 +2343,8 @@ fn resizeSections(self: *MachO) !void {
2343 if (header.isZerofill()) continue;2343 if (header.isZerofill()) continue;
2344 if (self.isZigSection(@intCast(n_sect))) continue; // TODO this is horrible2344 if (self.isZigSection(@intCast(n_sect))) continue; // TODO this is horrible
2345 const cpu_arch = self.getTarget().cpu.arch;2345 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);
2347 const padding_byte: u8 = if (header.isCode() and cpu_arch == .x86_64) 0xcc else 0;2348 const padding_byte: u8 = if (header.isCode() and cpu_arch == .x86_64) 0xcc else 0;
2348 @memset(out.items, padding_byte);2349 @memset(out.items, padding_byte);
2349 }2350 }
...@@ -2368,7 +2369,7 @@ fn writeSectionsAndUpdateLinkeditSizes(self: *MachO) !void {...@@ -2368,7 +2369,7 @@ fn writeSectionsAndUpdateLinkeditSizes(self: *MachO) !void {
2368 }2369 }
2369 for (self.thunks.items) |thunk| {2370 for (self.thunks.items) |thunk| {
2370 const out = self.sections.items(.out)[thunk.out_n_sect].items;2371 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;
2372 const size = thunk.size();2373 const size = thunk.size();
2373 var stream = std.io.fixedBufferStream(out[off..][0..size]);2374 var stream = std.io.fixedBufferStream(out[off..][0..size]);
2374 try thunk.write(self, stream.writer());2375 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...@@ -983,7 +983,7 @@ pub fn writeRelocs(self: Atom, macho_file: *MachO, code: []u8, buffer: []macho.r
983 var i: usize = 0;983 var i: usize = 0;
984 for (relocs) |rel| {984 for (relocs) |rel| {
985 defer i += 1;985 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;
987 const r_address: i32 = math.cast(i32, self.value + rel_offset) orelse return error.Overflow;987 const r_address: i32 = math.cast(i32, self.value + rel_offset) orelse return error.Overflow;
988 assert(r_address >= 0);988 assert(r_address >= 0);
989 const r_symbolnum = r_symbolnum: {989 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...@@ -415,8 +415,9 @@ pub fn resolveLiterals(self: *InternalObject, lp: *MachO.LiteralPool, macho_file
415 const rel = relocs[0];415 const rel = relocs[0];
416 assert(rel.tag == .@"extern");416 assert(rel.tag == .@"extern");
417 const target = rel.getTargetSymbol(atom.*, macho_file).getAtom(macho_file).?;417 const target = rel.getTargetSymbol(atom.*, macho_file).getAtom(macho_file).?;
418 try buffer.ensureUnusedCapacity(target.size);418 const target_size = std.math.cast(usize, target.size) orelse return error.Overflow;
419 buffer.resize(target.size) catch unreachable;419 try buffer.ensureUnusedCapacity(target_size);
420 buffer.resize(target_size) catch unreachable;
420 @memcpy(buffer.items, try self.getSectionData(target.n_sect));421 @memcpy(buffer.items, try self.getSectionData(target.n_sect));
421 const res = try lp.insert(gpa, header.type(), buffer.items);422 const res = try lp.insert(gpa, header.type(), buffer.items);
422 buffer.clearRetainingCapacity();423 buffer.clearRetainingCapacity();
...@@ -572,8 +573,9 @@ pub fn writeAtoms(self: *InternalObject, macho_file: *MachO) !void {...@@ -572,8 +573,9 @@ pub fn writeAtoms(self: *InternalObject, macho_file: *MachO) !void {
572 if (!atom.flags.alive) continue;573 if (!atom.flags.alive) continue;
573 const sect = atom.getInputSection(macho_file);574 const sect = atom.getInputSection(macho_file);
574 if (sect.isZerofill()) continue;575 if (sect.isZerofill()) continue;
575 const off = atom.value;576 const off = std.math.cast(usize, atom.value) orelse return error.Overflow;
576 const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items[off..][0..atom.size];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];
577 @memcpy(buffer, try self.getSectionData(atom.n_sect));579 @memcpy(buffer, try self.getSectionData(atom.n_sect));
578 try atom.resolveRelocs(macho_file, buffer);580 try atom.resolveRelocs(macho_file, buffer);
579 }581 }
src/link/MachO/Object.zig+28-15
...@@ -1015,7 +1015,8 @@ fn initEhFrameRecords(self: *Object, allocator: Allocator, sect_id: u8, file: Fi...@@ -1015,7 +1015,8 @@ fn initEhFrameRecords(self: *Object, allocator: Allocator, sect_id: u8, file: Fi
1015 const sect = slice.items(.header)[sect_id];1015 const sect = slice.items(.header)[sect_id];
1016 const relocs = slice.items(.relocs)[sect_id];1016 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);
1019 const amt = try file.preadAll(self.eh_frame_data.items, sect.offset + self.offset);1020 const amt = try file.preadAll(self.eh_frame_data.items, sect.offset + self.offset);
1020 if (amt != self.eh_frame_data.items.len) return error.InputOutput;1021 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...@@ -1116,7 +1117,8 @@ fn initUnwindRecords(self: *Object, allocator: Allocator, sect_id: u8, file: Fil
1116 };1117 };
11171118
1118 const header = self.sections.items(.header)[sect_id];1119 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);
1120 defer allocator.free(data);1122 defer allocator.free(data);
1121 const amt = try file.preadAll(data, header.offset + self.offset);1123 const amt = try file.preadAll(data, header.offset + self.offset);
1122 if (amt != data.len) return error.InputOutput;1124 if (amt != data.len) return error.InputOutput;
...@@ -1346,7 +1348,8 @@ fn parseDebugInfo(self: *Object, macho_file: *MachO) !void {...@@ -1346,7 +1348,8 @@ fn parseDebugInfo(self: *Object, macho_file: *MachO) !void {
1346 const file = macho_file.getFileHandle(self.file_handle);1348 const file = macho_file.getFileHandle(self.file_handle);
1347 const debug_info = blk: {1349 const debug_info = blk: {
1348 const sect = slice.items(.header)[debug_info_index.?];1350 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);
1350 const amt = try file.preadAll(data, sect.offset + self.offset);1353 const amt = try file.preadAll(data, sect.offset + self.offset);
1351 if (amt != data.len) return error.InputOutput;1354 if (amt != data.len) return error.InputOutput;
1352 break :blk data;1355 break :blk data;
...@@ -1354,7 +1357,8 @@ fn parseDebugInfo(self: *Object, macho_file: *MachO) !void {...@@ -1354,7 +1357,8 @@ fn parseDebugInfo(self: *Object, macho_file: *MachO) !void {
1354 defer gpa.free(debug_info);1357 defer gpa.free(debug_info);
1355 const debug_abbrev = blk: {1358 const debug_abbrev = blk: {
1356 const sect = slice.items(.header)[debug_abbrev_index.?];1359 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);
1358 const amt = try file.preadAll(data, sect.offset + self.offset);1362 const amt = try file.preadAll(data, sect.offset + self.offset);
1359 if (amt != data.len) return error.InputOutput;1363 if (amt != data.len) return error.InputOutput;
1360 break :blk data;1364 break :blk data;
...@@ -1362,7 +1366,8 @@ fn parseDebugInfo(self: *Object, macho_file: *MachO) !void {...@@ -1362,7 +1366,8 @@ fn parseDebugInfo(self: *Object, macho_file: *MachO) !void {
1362 defer gpa.free(debug_abbrev);1366 defer gpa.free(debug_abbrev);
1363 const debug_str = if (debug_str_index) |sid| blk: {1367 const debug_str = if (debug_str_index) |sid| blk: {
1364 const sect = slice.items(.header)[sid];1368 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);
1366 const amt = try file.preadAll(data, sect.offset + self.offset);1371 const amt = try file.preadAll(data, sect.offset + self.offset);
1367 if (amt != data.len) return error.InputOutput;1372 if (amt != data.len) return error.InputOutput;
1368 break :blk data;1373 break :blk data;
...@@ -1864,7 +1869,8 @@ pub fn writeAtoms(self: *Object, macho_file: *MachO) !void {...@@ -1864,7 +1869,8 @@ pub fn writeAtoms(self: *Object, macho_file: *MachO) !void {
18641869
1865 for (headers, 0..) |header, n_sect| {1870 for (headers, 0..) |header, n_sect| {
1866 if (header.isZerofill()) continue;1871 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);
1868 const amt = try file.preadAll(data, header.offset + self.offset);1874 const amt = try file.preadAll(data, header.offset + self.offset);
1869 if (amt != data.len) return error.InputOutput;1875 if (amt != data.len) return error.InputOutput;
1870 sections_data[n_sect] = data;1876 sections_data[n_sect] = data;
...@@ -1874,11 +1880,13 @@ pub fn writeAtoms(self: *Object, macho_file: *MachO) !void {...@@ -1874,11 +1880,13 @@ pub fn writeAtoms(self: *Object, macho_file: *MachO) !void {
1874 if (!atom.flags.alive) continue;1880 if (!atom.flags.alive) continue;
1875 const sect = atom.getInputSection(macho_file);1881 const sect = atom.getInputSection(macho_file);
1876 if (sect.isZerofill()) continue;1882 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;
1878 const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items;1886 const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items;
1879 const data = sections_data[atom.n_sect];1887 const data = sections_data[atom.n_sect];
1880 @memcpy(buffer[off..][0..atom.size], data[atom.off..][0..atom.size]);1888 @memcpy(buffer[value..][0..size], data[off..][0..size]);
1881 try atom.resolveRelocs(macho_file, buffer[off..][0..atom.size]);1889 try atom.resolveRelocs(macho_file, buffer[value..][0..size]);
1882 }1890 }
1883}1891}
18841892
...@@ -1900,7 +1908,8 @@ pub fn writeAtomsRelocatable(self: *Object, macho_file: *MachO) !void {...@@ -1900,7 +1908,8 @@ pub fn writeAtomsRelocatable(self: *Object, macho_file: *MachO) !void {
19001908
1901 for (headers, 0..) |header, n_sect| {1909 for (headers, 0..) |header, n_sect| {
1902 if (header.isZerofill()) continue;1910 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);
1904 const amt = try file.preadAll(data, header.offset + self.offset);1913 const amt = try file.preadAll(data, header.offset + self.offset);
1905 if (amt != data.len) return error.InputOutput;1914 if (amt != data.len) return error.InputOutput;
1906 sections_data[n_sect] = data;1915 sections_data[n_sect] = data;
...@@ -1910,13 +1919,15 @@ pub fn writeAtomsRelocatable(self: *Object, macho_file: *MachO) !void {...@@ -1910,13 +1919,15 @@ pub fn writeAtomsRelocatable(self: *Object, macho_file: *MachO) !void {
1910 if (!atom.flags.alive) continue;1919 if (!atom.flags.alive) continue;
1911 const sect = atom.getInputSection(macho_file);1920 const sect = atom.getInputSection(macho_file);
1912 if (sect.isZerofill()) continue;1921 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;
1914 const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items;1925 const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items;
1915 const data = sections_data[atom.n_sect];1926 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]);
1917 const relocs = macho_file.sections.items(.relocs)[atom.out_n_sect].items;1928 const relocs = macho_file.sections.items(.relocs)[atom.out_n_sect].items;
1918 const extra = atom.getExtra(macho_file);1929 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]);
1920 }1931 }
1921}1932}
19221933
...@@ -2812,7 +2823,8 @@ const x86_64 = struct {...@@ -2812,7 +2823,8 @@ const x86_64 = struct {
2812 }2823 }
2813 const relocs = @as([*]align(1) const macho.relocation_info, @ptrCast(relocs_buffer.ptr))[0..sect.nreloc];2824 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);
2816 defer gpa.free(code);2828 defer gpa.free(code);
2817 {2829 {
2818 const amt = try handle.preadAll(code, sect.offset + self.offset);2830 const amt = try handle.preadAll(code, sect.offset + self.offset);
...@@ -2984,7 +2996,8 @@ const aarch64 = struct {...@@ -2984,7 +2996,8 @@ const aarch64 = struct {
2984 }2996 }
2985 const relocs = @as([*]align(1) const macho.relocation_info, @ptrCast(relocs_buffer.ptr))[0..sect.nreloc];2997 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);
2988 defer gpa.free(code);3001 defer gpa.free(code);
2989 {3002 {
2990 const amt = try handle.preadAll(code, sect.offset + self.offset);3003 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 {...@@ -529,12 +529,13 @@ pub fn writeAtomsRelocatable(self: *ZigObject, macho_file: *MachO) !void {
529 if (sect.isZerofill()) continue;529 if (sect.isZerofill()) continue;
530 if (macho_file.isZigSection(atom.out_n_sect)) continue;530 if (macho_file.isZigSection(atom.out_n_sect)) continue;
531 if (atom.getRelocs(macho_file).len == 0) continue;531 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;
533 const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items;534 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]);
535 const relocs = macho_file.sections.items(.relocs)[atom.out_n_sect].items;536 const relocs = macho_file.sections.items(.relocs)[atom.out_n_sect].items;
536 const extra = atom.getExtra(macho_file);537 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]);
538 }539 }
539}540}
540541
...@@ -551,10 +552,11 @@ pub fn writeAtoms(self: *ZigObject, macho_file: *MachO) !void {...@@ -551,10 +552,11 @@ pub fn writeAtoms(self: *ZigObject, macho_file: *MachO) !void {
551 const sect = atom.getInputSection(macho_file);552 const sect = atom.getInputSection(macho_file);
552 if (sect.isZerofill()) continue;553 if (sect.isZerofill()) continue;
553 if (macho_file.isZigSection(atom.out_n_sect)) continue;554 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;
555 const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items;557 const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items;
556 try self.getAtomData(macho_file, atom.*, buffer[off..][0..atom.size]);558 try self.getAtomData(macho_file, atom.*, buffer[off..][0..size]);
557 try atom.resolveRelocs(macho_file, buffer[off..][0..atom.size]);559 try atom.resolveRelocs(macho_file, buffer[off..][0..size]);
558 }560 }
559}561}
560562
src/link/MachO/relocatable.zig+2-1
...@@ -626,7 +626,8 @@ fn writeSections(macho_file: *MachO) !void {...@@ -626,7 +626,8 @@ fn writeSections(macho_file: *MachO) !void {
626 for (slice.items(.header), slice.items(.out), slice.items(.relocs), 0..) |header, *out, *relocs, n_sect| {626 for (slice.items(.header), slice.items(.out), slice.items(.relocs), 0..) |header, *out, *relocs, n_sect| {
627 if (header.isZerofill()) continue;627 if (header.isZerofill()) continue;
628 if (!macho_file.isZigSection(@intCast(n_sect))) { // TODO this is wrong; what about debug sections?628 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);
630 const padding_byte: u8 = if (header.isCode() and cpu_arch == .x86_64) 0xcc else 0;631 const padding_byte: u8 = if (header.isCode() and cpu_arch == .x86_64) 0xcc else 0;
631 @memset(out.items, padding_byte);632 @memset(out.items, padding_byte);
632 }633 }