Commit 56296694d9

Jakub Konka <kubkon@jakubkonka.com>
2023-11-04 00:39:58
elf: fix 32bit build
1 parent ee66137
Changed files (2)
src/link/Elf/ZigObject.zig
@@ -542,7 +542,8 @@ pub fn updateArSize(self: *ZigObject, elf_file: *Elf) void {
 pub fn writeAr(self: ZigObject, elf_file: *Elf, writer: anytype) !void {
     const gpa = elf_file.base.allocator;
 
-    const contents = try gpa.alloc(u8, self.output_ar_state.size);
+    const size = std.math.cast(usize, self.output_ar_state.size) orelse return error.Overflow;
+    const contents = try gpa.alloc(u8, size);
     defer gpa.free(contents);
 
     const amt = try elf_file.base.file.?.preadAll(contents, 0);
@@ -553,7 +554,7 @@ pub fn writeAr(self: ZigObject, elf_file: *Elf, writer: anytype) !void {
 
     const hdr = Archive.setArHdr(.{
         .name = if (name.len <= 15) .{ .name = name } else .{ .name_off = self.output_ar_state.name_off },
-        .size = @intCast(self.output_ar_state.size),
+        .size = size,
     });
     try writer.writeAll(mem.asBytes(&hdr));
     try writer.writeAll(contents);
@@ -610,7 +611,7 @@ pub fn writeRelaSections(self: ZigObject, elf_file: *Elf) !void {
 
         var relocs = std.ArrayList(elf.Elf64_Rela).init(gpa);
         defer relocs.deinit();
-        try relocs.ensureTotalCapacityPrecise(@divExact(shdr.sh_size, shdr.sh_entsize));
+        try relocs.ensureTotalCapacityPrecise(@intCast(@divExact(shdr.sh_size, shdr.sh_entsize)));
 
         while (true) {
             for (atom.relocs(elf_file)) |rel| {
src/link/Elf.zig
@@ -1569,19 +1569,19 @@ pub fn flushStaticLib(self: *Elf, comp: *Compilation) link.File.FlushError!void
     }
 
     // Update file offsets of contributing objects.
-    const total_size: u64 = blk: {
-        var pos: u64 = Archive.SARMAG;
+    const total_size: usize = blk: {
+        var pos: usize = Archive.SARMAG;
         pos += @sizeOf(Archive.ar_hdr) + ar_symtab.size(.p64);
 
         if (ar_strtab.size() > 0) {
-            pos = mem.alignForward(u64, pos, 2);
+            pos = mem.alignForward(usize, pos, 2);
             pos += @sizeOf(Archive.ar_hdr) + ar_strtab.size();
         }
 
         if (self.zigObjectPtr()) |zig_object| {
-            pos = mem.alignForward(u64, pos, 2);
+            pos = mem.alignForward(usize, pos, 2);
             zig_object.output_ar_state.file_off = pos;
-            pos += @sizeOf(Archive.ar_hdr) + zig_object.output_ar_state.size;
+            pos += @sizeOf(Archive.ar_hdr) + (math.cast(usize, zig_object.output_ar_state.size) orelse return error.Overflow);
         }
 
         break :blk pos;
@@ -4672,7 +4672,8 @@ fn writeSymtab(self: *Elf) !void {
     log.debug("writing {d} symbols at 0x{x}", .{ nsyms, symtab_shdr.sh_offset });
 
     try self.symtab.resize(gpa, nsyms);
-    try self.strtab.ensureUnusedCapacity(gpa, strtab_shdr.sh_size - 1);
+    const needed_strtab_size = math.cast(usize, strtab_shdr.sh_size - 1) orelse return error.Overflow;
+    try self.strtab.ensureUnusedCapacity(gpa, needed_strtab_size);
 
     const Ctx = struct {
         ilocal: usize,