macho: fix 32bit build

This commit is contained in:
Jakub Konka 2022-10-19 12:19:20 +02:00
parent 04a590a171
commit a32bc0e39c
3 changed files with 35 additions and 21 deletions

View File

@ -5,6 +5,7 @@ const assert = std.debug.assert;
const dwarf = std.dwarf;
const leb = std.leb;
const log = std.log.scoped(.macho);
const math = std.math;
const mem = std.mem;
const Allocator = mem.Allocator;
@ -32,13 +33,14 @@ const CompileUnitIterator = struct {
const cuh = try CompileUnit.Header.read(reader);
const total_length = cuh.length + @as(u64, if (cuh.is_64bit) @sizeOf(u64) else @sizeOf(u32));
const offset = math.cast(usize, creader.bytes_read) orelse return error.Overflow;
const cu = CompileUnit{
.cuh = cuh,
.debug_info_off = creader.bytes_read,
.debug_info_off = offset,
};
self.pos += total_length;
self.pos += (math.cast(usize, total_length) orelse return error.Overflow);
return cu;
}
@ -102,7 +104,7 @@ pub fn genAbbrevLookupByKind(self: DwarfInfo, off: usize, lookup: *AbbrevLookupT
if (kind == 0) break;
const pos = creader.bytes_read;
const pos = math.cast(usize, creader.bytes_read) orelse return error.Overflow;
_ = try leb.readULEB128(u64, reader); // TAG
_ = try reader.readByte(); // CHILDREN
@ -113,9 +115,11 @@ pub fn genAbbrevLookupByKind(self: DwarfInfo, off: usize, lookup: *AbbrevLookupT
if (name == 0 and form == 0) break;
}
const next_pos = math.cast(usize, creader.bytes_read) orelse return error.Overflow;
try lookup.putNoClobber(kind, .{
.pos = pos,
.len = creader.bytes_read - pos - 2,
.len = next_pos - pos - 2,
});
}
}
@ -179,7 +183,7 @@ const AbbrevEntryIterator = struct {
const reader = creader.reader();
const kind = try leb.readULEB128(u64, reader);
self.pos += creader.bytes_read;
self.pos += (math.cast(usize, creader.bytes_read) orelse return error.Overflow);
if (kind == 0) {
return AbbrevEntry.@"null"();
@ -325,7 +329,7 @@ const AttributeIterator = struct {
const name = try leb.readULEB128(u64, reader);
const form = try leb.readULEB128(u64, reader);
self.debug_abbrev_pos += creader.bytes_read;
self.debug_abbrev_pos += (math.cast(usize, creader.bytes_read) orelse return error.Overflow);
const len = try findFormSize(
self.ctx,
@ -366,11 +370,13 @@ fn getAbbrevEntry(self: DwarfInfo, da_off: usize, da_len: usize, di_off: usize,
else => try reader.readByte(),
};
const pos = math.cast(usize, creader.bytes_read) orelse return error.Overflow;
return AbbrevEntry{
.tag = tag,
.children = children,
.debug_abbrev_off = creader.bytes_read + da_off,
.debug_abbrev_len = da_len - creader.bytes_read,
.debug_abbrev_off = pos + da_off,
.debug_abbrev_len = da_len - pos,
.debug_info_off = di_off,
.debug_info_len = di_len,
};
@ -392,7 +398,7 @@ fn findFormSize(self: DwarfInfo, form: u64, di_off: usize, cuh: CompileUnit.Head
while (i < expr_len) : (i += 1) {
_ = try reader.readByte();
}
return creader.bytes_read;
return math.cast(usize, creader.bytes_read) orelse error.Overflow;
},
dwarf.FORM.flag_present => return 0,
@ -402,11 +408,11 @@ fn findFormSize(self: DwarfInfo, form: u64, di_off: usize, cuh: CompileUnit.Head
dwarf.FORM.data8 => return @sizeOf(u64),
dwarf.FORM.udata => {
_ = try leb.readULEB128(u64, reader);
return creader.bytes_read;
return math.cast(usize, creader.bytes_read) orelse error.Overflow;
},
dwarf.FORM.sdata => {
_ = try leb.readILEB128(i64, reader);
return creader.bytes_read;
return math.cast(usize, creader.bytes_read) orelse error.Overflow;
},
dwarf.FORM.ref1 => return @sizeOf(u8),
@ -415,7 +421,7 @@ fn findFormSize(self: DwarfInfo, form: u64, di_off: usize, cuh: CompileUnit.Head
dwarf.FORM.ref8 => return @sizeOf(u64),
dwarf.FORM.ref_udata => {
_ = try leb.readULEB128(u64, reader);
return creader.bytes_read;
return math.cast(usize, creader.bytes_read) orelse error.Overflow;
},
else => return error.ToDo,
@ -457,5 +463,5 @@ fn findAbbrevEntrySize(self: DwarfInfo, da_off: usize, da_len: usize, di_off: us
fn getString(self: DwarfInfo, off: u64) []const u8 {
assert(off < self.debug_str.len);
return mem.sliceTo(@ptrCast([*:0]const u8, self.debug_str.ptr + off), 0);
return mem.sliceTo(@ptrCast([*:0]const u8, self.debug_str.ptr + @intCast(usize, off)), 0);
}

View File

@ -925,13 +925,15 @@ pub fn getAtomCode(zld: *Zld, atom_index: AtomIndex) []const u8 {
assert(!source_sect.isZerofill());
const code = object.getSectionContents(source_sect);
return code[0..atom.size];
const code_len = @intCast(usize, atom.size);
return code[0..code_len];
};
const source_sect = object.getSourceSection(source_sym.n_sect - 1);
assert(!source_sect.isZerofill());
const offset = source_sym.n_value - source_sect.addr;
const code = object.getSectionContents(source_sect);
return code[offset..][0..atom.size];
const offset = @intCast(usize, source_sym.n_value - source_sect.addr);
const code_len = @intCast(usize, atom.size);
return code[offset..][0..code_len];
}
pub fn getAtomRelocs(zld: *Zld, atom_index: AtomIndex) []align(1) const macho.relocation_info {

View File

@ -1725,11 +1725,12 @@ pub const Zld = struct {
} else {
const code = Atom.getAtomCode(self, atom_index);
const relocs = Atom.getAtomRelocs(self, atom_index);
const size = math.cast(usize, atom.size) orelse return error.Overflow;
buffer.appendSliceAssumeCapacity(code);
try Atom.resolveRelocs(
self,
atom_index,
buffer.items[offset..][0..atom.size],
buffer.items[offset..][0..size],
relocs,
reverse_lookups[atom.getFile().?],
);
@ -2457,7 +2458,7 @@ pub const Zld = struct {
const export_size = trie.size;
log.debug("writing export trie from 0x{x} to 0x{x}", .{ export_off, export_off + export_size });
const needed_size = export_off + export_size - rebase_off;
const needed_size = math.cast(usize, export_off + export_size - rebase_off) orelse return error.Overflow;
link_seg.filesize = needed_size;
var buffer = try gpa.alloc(u8, needed_size);
@ -2484,7 +2485,10 @@ pub const Zld = struct {
});
try self.file.pwriteAll(buffer, rebase_off);
try self.populateLazyBindOffsetsInStubHelper(buffer[lazy_bind_off - rebase_off ..][0..lazy_bind_size]);
const offset = math.cast(usize, lazy_bind_off - rebase_off) orelse return error.Overflow;
const size = math.cast(usize, lazy_bind_size) orelse return error.Overflow;
try self.populateLazyBindOffsetsInStubHelper(buffer[offset..][0..size]);
try lc_writer.writeStruct(macho.dyld_info_command{
.cmd = .DYLD_INFO_ONLY,
@ -3210,7 +3214,8 @@ pub const Zld = struct {
// We assume there is only one CU.
var cu_it = debug_info.getCompileUnitIterator();
const compile_unit = while (try cu_it.next()) |cu| {
try debug_info.genAbbrevLookupByKind(cu.cuh.debug_abbrev_offset, &lookup);
const offset = math.cast(usize, cu.cuh.debug_abbrev_offset) orelse return error.Overflow;
try debug_info.genAbbrevLookupByKind(offset, &lookup);
break cu;
} else {
log.debug("no compile unit found in debug info in {s}; skipping", .{object.name});
@ -4273,7 +4278,8 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr
physical_zerofill_start = header.offset + header.size;
} else break :blk;
const linkedit = zld.getLinkeditSegmentPtr();
const physical_zerofill_size = linkedit.fileoff - physical_zerofill_start;
const physical_zerofill_size = math.cast(usize, linkedit.fileoff - physical_zerofill_start) orelse
return error.Overflow;
if (physical_zerofill_size > 0) {
var padding = try zld.gpa.alloc(u8, physical_zerofill_size);
defer zld.gpa.free(padding);