authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-10-19 12:19:20+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-10-22 07:59:23+02:00
loga32bc0e39c9d2b33547241fcb9868d72e52ee1df
tree69bb56e02e6af8263215a85ad3096b9a397a2cab
parent04a590a171c68570be1b2371617d747efbda7f1f

macho: fix 32bit build


3 files changed, 35 insertions(+), 21 deletions(-)

src/link/MachO/DwarfInfo.zig+19-13
...@@ -5,6 +5,7 @@ const assert = std.debug.assert;...@@ -5,6 +5,7 @@ const assert = std.debug.assert;
5const dwarf = std.dwarf;5const dwarf = std.dwarf;
6const leb = std.leb;6const leb = std.leb;
7const log = std.log.scoped(.macho);7const log = std.log.scoped(.macho);
8const math = std.math;
8const mem = std.mem;9const mem = std.mem;
910
10const Allocator = mem.Allocator;11const Allocator = mem.Allocator;
...@@ -32,13 +33,14 @@ const CompileUnitIterator = struct {...@@ -32,13 +33,14 @@ const CompileUnitIterator = struct {
3233
33 const cuh = try CompileUnit.Header.read(reader);34 const cuh = try CompileUnit.Header.read(reader);
34 const total_length = cuh.length + @as(u64, if (cuh.is_64bit) @sizeOf(u64) else @sizeOf(u32));35 const total_length = cuh.length + @as(u64, if (cuh.is_64bit) @sizeOf(u64) else @sizeOf(u32));
36 const offset = math.cast(usize, creader.bytes_read) orelse return error.Overflow;
3537
36 const cu = CompileUnit{38 const cu = CompileUnit{
37 .cuh = cuh,39 .cuh = cuh,
38 .debug_info_off = creader.bytes_read,40 .debug_info_off = offset,
39 };41 };
4042
41 self.pos += total_length;43 self.pos += (math.cast(usize, total_length) orelse return error.Overflow);
4244
43 return cu;45 return cu;
44 }46 }
...@@ -102,7 +104,7 @@ pub fn genAbbrevLookupByKind(self: DwarfInfo, off: usize, lookup: *AbbrevLookupT...@@ -102,7 +104,7 @@ pub fn genAbbrevLookupByKind(self: DwarfInfo, off: usize, lookup: *AbbrevLookupT
102104
103 if (kind == 0) break;105 if (kind == 0) break;
104106
105 const pos = creader.bytes_read;107 const pos = math.cast(usize, creader.bytes_read) orelse return error.Overflow;
106 _ = try leb.readULEB128(u64, reader); // TAG108 _ = try leb.readULEB128(u64, reader); // TAG
107 _ = try reader.readByte(); // CHILDREN109 _ = try reader.readByte(); // CHILDREN
108110
...@@ -113,9 +115,11 @@ pub fn genAbbrevLookupByKind(self: DwarfInfo, off: usize, lookup: *AbbrevLookupT...@@ -113,9 +115,11 @@ pub fn genAbbrevLookupByKind(self: DwarfInfo, off: usize, lookup: *AbbrevLookupT
113 if (name == 0 and form == 0) break;115 if (name == 0 and form == 0) break;
114 }116 }
115117
118 const next_pos = math.cast(usize, creader.bytes_read) orelse return error.Overflow;
119
116 try lookup.putNoClobber(kind, .{120 try lookup.putNoClobber(kind, .{
117 .pos = pos,121 .pos = pos,
118 .len = creader.bytes_read - pos - 2,122 .len = next_pos - pos - 2,
119 });123 });
120 }124 }
121}125}
...@@ -179,7 +183,7 @@ const AbbrevEntryIterator = struct {...@@ -179,7 +183,7 @@ const AbbrevEntryIterator = struct {
179 const reader = creader.reader();183 const reader = creader.reader();
180184
181 const kind = try leb.readULEB128(u64, reader);185 const kind = try leb.readULEB128(u64, reader);
182 self.pos += creader.bytes_read;186 self.pos += (math.cast(usize, creader.bytes_read) orelse return error.Overflow);
183187
184 if (kind == 0) {188 if (kind == 0) {
185 return AbbrevEntry.@"null"();189 return AbbrevEntry.@"null"();
...@@ -325,7 +329,7 @@ const AttributeIterator = struct {...@@ -325,7 +329,7 @@ const AttributeIterator = struct {
325 const name = try leb.readULEB128(u64, reader);329 const name = try leb.readULEB128(u64, reader);
326 const form = try leb.readULEB128(u64, reader);330 const form = try leb.readULEB128(u64, reader);
327331
328 self.debug_abbrev_pos += creader.bytes_read;332 self.debug_abbrev_pos += (math.cast(usize, creader.bytes_read) orelse return error.Overflow);
329333
330 const len = try findFormSize(334 const len = try findFormSize(
331 self.ctx,335 self.ctx,
...@@ -366,11 +370,13 @@ fn getAbbrevEntry(self: DwarfInfo, da_off: usize, da_len: usize, di_off: usize,...@@ -366,11 +370,13 @@ fn getAbbrevEntry(self: DwarfInfo, da_off: usize, da_len: usize, di_off: usize,
366 else => try reader.readByte(),370 else => try reader.readByte(),
367 };371 };
368372
373 const pos = math.cast(usize, creader.bytes_read) orelse return error.Overflow;
374
369 return AbbrevEntry{375 return AbbrevEntry{
370 .tag = tag,376 .tag = tag,
371 .children = children,377 .children = children,
372 .debug_abbrev_off = creader.bytes_read + da_off,378 .debug_abbrev_off = pos + da_off,
373 .debug_abbrev_len = da_len - creader.bytes_read,379 .debug_abbrev_len = da_len - pos,
374 .debug_info_off = di_off,380 .debug_info_off = di_off,
375 .debug_info_len = di_len,381 .debug_info_len = di_len,
376 };382 };
...@@ -392,7 +398,7 @@ fn findFormSize(self: DwarfInfo, form: u64, di_off: usize, cuh: CompileUnit.Head...@@ -392,7 +398,7 @@ fn findFormSize(self: DwarfInfo, form: u64, di_off: usize, cuh: CompileUnit.Head
392 while (i < expr_len) : (i += 1) {398 while (i < expr_len) : (i += 1) {
393 _ = try reader.readByte();399 _ = try reader.readByte();
394 }400 }
395 return creader.bytes_read;401 return math.cast(usize, creader.bytes_read) orelse error.Overflow;
396 },402 },
397 dwarf.FORM.flag_present => return 0,403 dwarf.FORM.flag_present => return 0,
398404
...@@ -402,11 +408,11 @@ fn findFormSize(self: DwarfInfo, form: u64, di_off: usize, cuh: CompileUnit.Head...@@ -402,11 +408,11 @@ fn findFormSize(self: DwarfInfo, form: u64, di_off: usize, cuh: CompileUnit.Head
402 dwarf.FORM.data8 => return @sizeOf(u64),408 dwarf.FORM.data8 => return @sizeOf(u64),
403 dwarf.FORM.udata => {409 dwarf.FORM.udata => {
404 _ = try leb.readULEB128(u64, reader);410 _ = try leb.readULEB128(u64, reader);
405 return creader.bytes_read;411 return math.cast(usize, creader.bytes_read) orelse error.Overflow;
406 },412 },
407 dwarf.FORM.sdata => {413 dwarf.FORM.sdata => {
408 _ = try leb.readILEB128(i64, reader);414 _ = try leb.readILEB128(i64, reader);
409 return creader.bytes_read;415 return math.cast(usize, creader.bytes_read) orelse error.Overflow;
410 },416 },
411417
412 dwarf.FORM.ref1 => return @sizeOf(u8),418 dwarf.FORM.ref1 => return @sizeOf(u8),
...@@ -415,7 +421,7 @@ fn findFormSize(self: DwarfInfo, form: u64, di_off: usize, cuh: CompileUnit.Head...@@ -415,7 +421,7 @@ fn findFormSize(self: DwarfInfo, form: u64, di_off: usize, cuh: CompileUnit.Head
415 dwarf.FORM.ref8 => return @sizeOf(u64),421 dwarf.FORM.ref8 => return @sizeOf(u64),
416 dwarf.FORM.ref_udata => {422 dwarf.FORM.ref_udata => {
417 _ = try leb.readULEB128(u64, reader);423 _ = try leb.readULEB128(u64, reader);
418 return creader.bytes_read;424 return math.cast(usize, creader.bytes_read) orelse error.Overflow;
419 },425 },
420426
421 else => return error.ToDo,427 else => return error.ToDo,
...@@ -457,5 +463,5 @@ fn findAbbrevEntrySize(self: DwarfInfo, da_off: usize, da_len: usize, di_off: us...@@ -457,5 +463,5 @@ fn findAbbrevEntrySize(self: DwarfInfo, da_off: usize, da_len: usize, di_off: us
457463
458fn getString(self: DwarfInfo, off: u64) []const u8 {464fn getString(self: DwarfInfo, off: u64) []const u8 {
459 assert(off < self.debug_str.len);465 assert(off < self.debug_str.len);
460 return mem.sliceTo(@ptrCast([*:0]const u8, self.debug_str.ptr + off), 0);466 return mem.sliceTo(@ptrCast([*:0]const u8, self.debug_str.ptr + @intCast(usize, off)), 0);
461}467}
src/link/MachO/ZldAtom.zig+5-3
...@@ -925,13 +925,15 @@ pub fn getAtomCode(zld: *Zld, atom_index: AtomIndex) []const u8 {...@@ -925,13 +925,15 @@ pub fn getAtomCode(zld: *Zld, atom_index: AtomIndex) []const u8 {
925925
926 assert(!source_sect.isZerofill());926 assert(!source_sect.isZerofill());
927 const code = object.getSectionContents(source_sect);927 const code = object.getSectionContents(source_sect);
928 return code[0..atom.size];928 const code_len = @intCast(usize, atom.size);
929 return code[0..code_len];
929 };930 };
930 const source_sect = object.getSourceSection(source_sym.n_sect - 1);931 const source_sect = object.getSourceSection(source_sym.n_sect - 1);
931 assert(!source_sect.isZerofill());932 assert(!source_sect.isZerofill());
932 const offset = source_sym.n_value - source_sect.addr;
933 const code = object.getSectionContents(source_sect);933 const code = object.getSectionContents(source_sect);
934 return code[offset..][0..atom.size];934 const offset = @intCast(usize, source_sym.n_value - source_sect.addr);
935 const code_len = @intCast(usize, atom.size);
936 return code[offset..][0..code_len];
935}937}
936938
937pub fn getAtomRelocs(zld: *Zld, atom_index: AtomIndex) []align(1) const macho.relocation_info {939pub fn getAtomRelocs(zld: *Zld, atom_index: AtomIndex) []align(1) const macho.relocation_info {
src/link/MachO/zld.zig+11-5
...@@ -1725,11 +1725,12 @@ pub const Zld = struct {...@@ -1725,11 +1725,12 @@ pub const Zld = struct {
1725 } else {1725 } else {
1726 const code = Atom.getAtomCode(self, atom_index);1726 const code = Atom.getAtomCode(self, atom_index);
1727 const relocs = Atom.getAtomRelocs(self, atom_index);1727 const relocs = Atom.getAtomRelocs(self, atom_index);
1728 const size = math.cast(usize, atom.size) orelse return error.Overflow;
1728 buffer.appendSliceAssumeCapacity(code);1729 buffer.appendSliceAssumeCapacity(code);
1729 try Atom.resolveRelocs(1730 try Atom.resolveRelocs(
1730 self,1731 self,
1731 atom_index,1732 atom_index,
1732 buffer.items[offset..][0..atom.size],1733 buffer.items[offset..][0..size],
1733 relocs,1734 relocs,
1734 reverse_lookups[atom.getFile().?],1735 reverse_lookups[atom.getFile().?],
1735 );1736 );
...@@ -2457,7 +2458,7 @@ pub const Zld = struct {...@@ -2457,7 +2458,7 @@ pub const Zld = struct {
2457 const export_size = trie.size;2458 const export_size = trie.size;
2458 log.debug("writing export trie from 0x{x} to 0x{x}", .{ export_off, export_off + export_size });2459 log.debug("writing export trie from 0x{x} to 0x{x}", .{ export_off, export_off + export_size });
24592460
2460 const needed_size = export_off + export_size - rebase_off;2461 const needed_size = math.cast(usize, export_off + export_size - rebase_off) orelse return error.Overflow;
2461 link_seg.filesize = needed_size;2462 link_seg.filesize = needed_size;
24622463
2463 var buffer = try gpa.alloc(u8, needed_size);2464 var buffer = try gpa.alloc(u8, needed_size);
...@@ -2484,7 +2485,10 @@ pub const Zld = struct {...@@ -2484,7 +2485,10 @@ pub const Zld = struct {
2484 });2485 });
24852486
2486 try self.file.pwriteAll(buffer, rebase_off);2487 try self.file.pwriteAll(buffer, rebase_off);
2487 try self.populateLazyBindOffsetsInStubHelper(buffer[lazy_bind_off - rebase_off ..][0..lazy_bind_size]);2488
2489 const offset = math.cast(usize, lazy_bind_off - rebase_off) orelse return error.Overflow;
2490 const size = math.cast(usize, lazy_bind_size) orelse return error.Overflow;
2491 try self.populateLazyBindOffsetsInStubHelper(buffer[offset..][0..size]);
24882492
2489 try lc_writer.writeStruct(macho.dyld_info_command{2493 try lc_writer.writeStruct(macho.dyld_info_command{
2490 .cmd = .DYLD_INFO_ONLY,2494 .cmd = .DYLD_INFO_ONLY,
...@@ -3210,7 +3214,8 @@ pub const Zld = struct {...@@ -3210,7 +3214,8 @@ pub const Zld = struct {
3210 // We assume there is only one CU.3214 // We assume there is only one CU.
3211 var cu_it = debug_info.getCompileUnitIterator();3215 var cu_it = debug_info.getCompileUnitIterator();
3212 const compile_unit = while (try cu_it.next()) |cu| {3216 const compile_unit = while (try cu_it.next()) |cu| {
3213 try debug_info.genAbbrevLookupByKind(cu.cuh.debug_abbrev_offset, &lookup);3217 const offset = math.cast(usize, cu.cuh.debug_abbrev_offset) orelse return error.Overflow;
3218 try debug_info.genAbbrevLookupByKind(offset, &lookup);
3214 break cu;3219 break cu;
3215 } else {3220 } else {
3216 log.debug("no compile unit found in debug info in {s}; skipping", .{object.name});3221 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...@@ -4273,7 +4278,8 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr
4273 physical_zerofill_start = header.offset + header.size;4278 physical_zerofill_start = header.offset + header.size;
4274 } else break :blk;4279 } else break :blk;
4275 const linkedit = zld.getLinkeditSegmentPtr();4280 const linkedit = zld.getLinkeditSegmentPtr();
4276 const physical_zerofill_size = linkedit.fileoff - physical_zerofill_start;4281 const physical_zerofill_size = math.cast(usize, linkedit.fileoff - physical_zerofill_start) orelse
4282 return error.Overflow;
4277 if (physical_zerofill_size > 0) {4283 if (physical_zerofill_size > 0) {
4278 var padding = try zld.gpa.alloc(u8, physical_zerofill_size);4284 var padding = try zld.gpa.alloc(u8, physical_zerofill_size);
4279 defer zld.gpa.free(padding);4285 defer zld.gpa.free(padding);