diff --git a/src/link/MachO.zig b/src/link/MachO.zig index 7b695304bb..d86e2aca17 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -2343,7 +2343,8 @@ fn resizeSections(self: *MachO) !void { if (header.isZerofill()) continue; if (self.isZigSection(@intCast(n_sect))) continue; // TODO this is horrible const cpu_arch = self.getTarget().cpu.arch; - try out.resize(self.base.comp.gpa, header.size); + const size = math.cast(usize, header.size) orelse return error.Overflow; + try out.resize(self.base.comp.gpa, size); const padding_byte: u8 = if (header.isCode() and cpu_arch == .x86_64) 0xcc else 0; @memset(out.items, padding_byte); } @@ -2368,7 +2369,7 @@ fn writeSectionsAndUpdateLinkeditSizes(self: *MachO) !void { } for (self.thunks.items) |thunk| { const out = self.sections.items(.out)[thunk.out_n_sect].items; - const off = thunk.value; + const off = math.cast(usize, thunk.value) orelse return error.Overflow; const size = thunk.size(); var stream = std.io.fixedBufferStream(out[off..][0..size]); try thunk.write(self, stream.writer()); diff --git a/src/link/MachO/Atom.zig b/src/link/MachO/Atom.zig index 0a2ebb931c..64f5723371 100644 --- a/src/link/MachO/Atom.zig +++ b/src/link/MachO/Atom.zig @@ -983,7 +983,7 @@ pub fn writeRelocs(self: Atom, macho_file: *MachO, code: []u8, buffer: []macho.r var i: usize = 0; for (relocs) |rel| { defer i += 1; - const rel_offset = rel.offset - self.off; + const rel_offset = math.cast(usize, rel.offset - self.off) orelse return error.Overflow; const r_address: i32 = math.cast(i32, self.value + rel_offset) orelse return error.Overflow; assert(r_address >= 0); const r_symbolnum = r_symbolnum: { diff --git a/src/link/MachO/InternalObject.zig b/src/link/MachO/InternalObject.zig index 55d7be00fb..ca6a53e983 100644 --- a/src/link/MachO/InternalObject.zig +++ b/src/link/MachO/InternalObject.zig @@ -415,8 +415,9 @@ pub fn resolveLiterals(self: *InternalObject, lp: *MachO.LiteralPool, macho_file const rel = relocs[0]; assert(rel.tag == .@"extern"); const target = rel.getTargetSymbol(atom.*, macho_file).getAtom(macho_file).?; - try buffer.ensureUnusedCapacity(target.size); - buffer.resize(target.size) catch unreachable; + const target_size = std.math.cast(usize, target.size) orelse return error.Overflow; + try buffer.ensureUnusedCapacity(target_size); + buffer.resize(target_size) catch unreachable; @memcpy(buffer.items, try self.getSectionData(target.n_sect)); const res = try lp.insert(gpa, header.type(), buffer.items); buffer.clearRetainingCapacity(); @@ -572,8 +573,9 @@ pub fn writeAtoms(self: *InternalObject, macho_file: *MachO) !void { if (!atom.flags.alive) continue; const sect = atom.getInputSection(macho_file); if (sect.isZerofill()) continue; - const off = atom.value; - const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items[off..][0..atom.size]; + const off = std.math.cast(usize, atom.value) orelse return error.Overflow; + const size = std.math.cast(usize, atom.size) orelse return error.Overflow; + const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items[off..][0..size]; @memcpy(buffer, try self.getSectionData(atom.n_sect)); try atom.resolveRelocs(macho_file, buffer); } diff --git a/src/link/MachO/Object.zig b/src/link/MachO/Object.zig index 7ab128aaeb..eda7ac91f1 100644 --- a/src/link/MachO/Object.zig +++ b/src/link/MachO/Object.zig @@ -1015,7 +1015,8 @@ fn initEhFrameRecords(self: *Object, allocator: Allocator, sect_id: u8, file: Fi const sect = slice.items(.header)[sect_id]; const relocs = slice.items(.relocs)[sect_id]; - try self.eh_frame_data.resize(allocator, sect.size); + const size = math.cast(usize, sect.size) orelse return error.Overflow; + try self.eh_frame_data.resize(allocator, size); const amt = try file.preadAll(self.eh_frame_data.items, sect.offset + self.offset); if (amt != self.eh_frame_data.items.len) return error.InputOutput; @@ -1116,7 +1117,8 @@ fn initUnwindRecords(self: *Object, allocator: Allocator, sect_id: u8, file: Fil }; const header = self.sections.items(.header)[sect_id]; - const data = try allocator.alloc(u8, header.size); + const size = math.cast(usize, header.size) orelse return error.Overflow; + const data = try allocator.alloc(u8, size); defer allocator.free(data); const amt = try file.preadAll(data, header.offset + self.offset); if (amt != data.len) return error.InputOutput; @@ -1346,7 +1348,8 @@ fn parseDebugInfo(self: *Object, macho_file: *MachO) !void { const file = macho_file.getFileHandle(self.file_handle); const debug_info = blk: { const sect = slice.items(.header)[debug_info_index.?]; - const data = try gpa.alloc(u8, sect.size); + const size = math.cast(usize, sect.size) orelse return error.Overflow; + const data = try gpa.alloc(u8, size); const amt = try file.preadAll(data, sect.offset + self.offset); if (amt != data.len) return error.InputOutput; break :blk data; @@ -1354,7 +1357,8 @@ fn parseDebugInfo(self: *Object, macho_file: *MachO) !void { defer gpa.free(debug_info); const debug_abbrev = blk: { const sect = slice.items(.header)[debug_abbrev_index.?]; - const data = try gpa.alloc(u8, sect.size); + const size = math.cast(usize, sect.size) orelse return error.Overflow; + const data = try gpa.alloc(u8, size); const amt = try file.preadAll(data, sect.offset + self.offset); if (amt != data.len) return error.InputOutput; break :blk data; @@ -1362,7 +1366,8 @@ fn parseDebugInfo(self: *Object, macho_file: *MachO) !void { defer gpa.free(debug_abbrev); const debug_str = if (debug_str_index) |sid| blk: { const sect = slice.items(.header)[sid]; - const data = try gpa.alloc(u8, sect.size); + const size = math.cast(usize, sect.size) orelse return error.Overflow; + const data = try gpa.alloc(u8, size); const amt = try file.preadAll(data, sect.offset + self.offset); if (amt != data.len) return error.InputOutput; break :blk data; @@ -1864,7 +1869,8 @@ pub fn writeAtoms(self: *Object, macho_file: *MachO) !void { for (headers, 0..) |header, n_sect| { if (header.isZerofill()) continue; - const data = try gpa.alloc(u8, header.size); + const size = math.cast(usize, header.size) orelse return error.Overflow; + const data = try gpa.alloc(u8, size); const amt = try file.preadAll(data, header.offset + self.offset); if (amt != data.len) return error.InputOutput; sections_data[n_sect] = data; @@ -1874,11 +1880,13 @@ pub fn writeAtoms(self: *Object, macho_file: *MachO) !void { if (!atom.flags.alive) continue; const sect = atom.getInputSection(macho_file); if (sect.isZerofill()) continue; - const off = atom.value; + const value = math.cast(usize, atom.value) orelse return error.Overflow; + const off = math.cast(usize, atom.off) orelse return error.Overflow; + const size = math.cast(usize, atom.size) orelse return error.Overflow; const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items; const data = sections_data[atom.n_sect]; - @memcpy(buffer[off..][0..atom.size], data[atom.off..][0..atom.size]); - try atom.resolveRelocs(macho_file, buffer[off..][0..atom.size]); + @memcpy(buffer[value..][0..size], data[off..][0..size]); + try atom.resolveRelocs(macho_file, buffer[value..][0..size]); } } @@ -1900,7 +1908,8 @@ pub fn writeAtomsRelocatable(self: *Object, macho_file: *MachO) !void { for (headers, 0..) |header, n_sect| { if (header.isZerofill()) continue; - const data = try gpa.alloc(u8, header.size); + const size = math.cast(usize, header.size) orelse return error.Overflow; + const data = try gpa.alloc(u8, size); const amt = try file.preadAll(data, header.offset + self.offset); if (amt != data.len) return error.InputOutput; sections_data[n_sect] = data; @@ -1910,13 +1919,15 @@ pub fn writeAtomsRelocatable(self: *Object, macho_file: *MachO) !void { if (!atom.flags.alive) continue; const sect = atom.getInputSection(macho_file); if (sect.isZerofill()) continue; - const off = atom.value; + const value = math.cast(usize, atom.value) orelse return error.Overflow; + const off = math.cast(usize, atom.off) orelse return error.Overflow; + const size = math.cast(usize, atom.size) orelse return error.Overflow; const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items; const data = sections_data[atom.n_sect]; - @memcpy(buffer[off..][0..atom.size], data[atom.off..][0..atom.size]); + @memcpy(buffer[value..][0..size], data[off..][0..size]); const relocs = macho_file.sections.items(.relocs)[atom.out_n_sect].items; const extra = atom.getExtra(macho_file); - try atom.writeRelocs(macho_file, buffer[off..][0..atom.size], relocs[extra.rel_out_index..][0..extra.rel_out_count]); + try atom.writeRelocs(macho_file, buffer[value..][0..size], relocs[extra.rel_out_index..][0..extra.rel_out_count]); } } @@ -2812,7 +2823,8 @@ const x86_64 = struct { } const relocs = @as([*]align(1) const macho.relocation_info, @ptrCast(relocs_buffer.ptr))[0..sect.nreloc]; - const code = try gpa.alloc(u8, sect.size); + const sect_size = math.cast(usize, sect.size) orelse return error.Overflow; + const code = try gpa.alloc(u8, sect_size); defer gpa.free(code); { const amt = try handle.preadAll(code, sect.offset + self.offset); @@ -2984,7 +2996,8 @@ const aarch64 = struct { } const relocs = @as([*]align(1) const macho.relocation_info, @ptrCast(relocs_buffer.ptr))[0..sect.nreloc]; - const code = try gpa.alloc(u8, sect.size); + const sect_size = math.cast(usize, sect.size) orelse return error.Overflow; + const code = try gpa.alloc(u8, sect_size); defer gpa.free(code); { const amt = try handle.preadAll(code, sect.offset + self.offset); diff --git a/src/link/MachO/ZigObject.zig b/src/link/MachO/ZigObject.zig index 39eadc11c7..d65cef5c10 100644 --- a/src/link/MachO/ZigObject.zig +++ b/src/link/MachO/ZigObject.zig @@ -529,12 +529,13 @@ pub fn writeAtomsRelocatable(self: *ZigObject, macho_file: *MachO) !void { if (sect.isZerofill()) continue; if (macho_file.isZigSection(atom.out_n_sect)) continue; if (atom.getRelocs(macho_file).len == 0) continue; - const off = atom.value; + const off = std.math.cast(usize, atom.value) orelse return error.Overflow; + const size = std.math.cast(usize, atom.size) orelse return error.Overflow; const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items; - try self.getAtomData(macho_file, atom.*, buffer[off..][0..atom.size]); + try self.getAtomData(macho_file, atom.*, buffer[off..][0..size]); const relocs = macho_file.sections.items(.relocs)[atom.out_n_sect].items; const extra = atom.getExtra(macho_file); - try atom.writeRelocs(macho_file, buffer[off..][0..atom.size], relocs[extra.rel_out_index..][0..extra.rel_out_count]); + try atom.writeRelocs(macho_file, buffer[off..][0..size], relocs[extra.rel_out_index..][0..extra.rel_out_count]); } } @@ -551,10 +552,11 @@ pub fn writeAtoms(self: *ZigObject, macho_file: *MachO) !void { const sect = atom.getInputSection(macho_file); if (sect.isZerofill()) continue; if (macho_file.isZigSection(atom.out_n_sect)) continue; - const off = atom.value; + const off = std.math.cast(usize, atom.value) orelse return error.Overflow; + const size = std.math.cast(usize, atom.size) orelse return error.Overflow; const buffer = macho_file.sections.items(.out)[atom.out_n_sect].items; - try self.getAtomData(macho_file, atom.*, buffer[off..][0..atom.size]); - try atom.resolveRelocs(macho_file, buffer[off..][0..atom.size]); + try self.getAtomData(macho_file, atom.*, buffer[off..][0..size]); + try atom.resolveRelocs(macho_file, buffer[off..][0..size]); } } diff --git a/src/link/MachO/relocatable.zig b/src/link/MachO/relocatable.zig index 5ea6ae18f3..51689ea226 100644 --- a/src/link/MachO/relocatable.zig +++ b/src/link/MachO/relocatable.zig @@ -626,7 +626,8 @@ fn writeSections(macho_file: *MachO) !void { for (slice.items(.header), slice.items(.out), slice.items(.relocs), 0..) |header, *out, *relocs, n_sect| { if (header.isZerofill()) continue; if (!macho_file.isZigSection(@intCast(n_sect))) { // TODO this is wrong; what about debug sections? - try out.resize(gpa, header.size); + const size = math.cast(usize, header.size) orelse return error.Overflow; + try out.resize(gpa, size); const padding_byte: u8 = if (header.isCode() and cpu_arch == .x86_64) 0xcc else 0; @memset(out.items, padding_byte); }