Commit d1f60a63bd

Jakub Konka <kubkon@jakubkonka.com>
2021-06-02 21:18:22
zld: fix the linker for 32bit comp targets
1 parent a9dd8d7
Changed files (4)
src/link/MachO/Archive.zig
@@ -58,7 +58,7 @@ const ar_hdr = extern struct {
 
     const NameOrLength = union(enum) {
         Name: []const u8,
-        Length: u64,
+        Length: u32,
     };
     fn nameOrLength(self: ar_hdr) !NameOrLength {
         const value = getValue(&self.ar_name);
@@ -70,14 +70,14 @@ const ar_hdr = extern struct {
         } else {
             // Name follows the header directly and its length is encoded in
             // the name field.
-            const length = try std.fmt.parseInt(u64, value[slash_index + 1 ..], 10);
+            const length = try std.fmt.parseInt(u32, value[slash_index + 1 ..], 10);
             return NameOrLength{ .Length = length };
         }
     }
 
-    fn size(self: ar_hdr) !u64 {
+    fn size(self: ar_hdr) !u32 {
         const value = getValue(&self.ar_size);
-        return std.fmt.parseInt(u64, value, 10);
+        return std.fmt.parseInt(u32, value, 10);
     }
 
     fn getValue(raw: []const u8) []const u8 {
src/link/MachO/Object.zig
@@ -284,7 +284,7 @@ pub fn parseSections(self: *Object) !void {
     for (seg.sections.items) |sect| {
         log.debug("parsing section '{s},{s}'", .{ parseName(&sect.segname), parseName(&sect.sectname) });
         // Read sections' code
-        var code = try self.allocator.alloc(u8, sect.size);
+        var code = try self.allocator.alloc(u8, @intCast(usize, sect.size));
         _ = try self.file.?.preadAll(code, sect.offset);
 
         var section = Section{
@@ -461,7 +461,7 @@ pub fn parseDebugInfo(self: *Object) !void {
 fn readSection(self: Object, allocator: *Allocator, index: u16) ![]u8 {
     const seg = self.load_commands.items[self.segment_cmd_index.?].Segment;
     const sect = seg.sections.items[index];
-    var buffer = try allocator.alloc(u8, sect.size);
+    var buffer = try allocator.alloc(u8, @intCast(usize, sect.size));
     _ = try self.file.?.preadAll(buffer, sect.offset);
     return buffer;
 }
src/link/MachO/reloc.zig
@@ -179,12 +179,12 @@ pub fn parse(
 
 pub const RelocIterator = struct {
     buffer: []const macho.relocation_info,
-    index: i64 = -1,
+    index: i32 = -1,
 
     pub fn next(self: *RelocIterator) ?macho.relocation_info {
         self.index += 1;
         if (self.index < self.buffer.len) {
-            const reloc = self.buffer[@intCast(u64, self.index)];
+            const reloc = self.buffer[@intCast(u32, self.index)];
             log.debug("relocation", .{});
             log.debug("    | type = {}", .{reloc.r_type});
             log.debug("    | offset = {}", .{reloc.r_address});
@@ -199,6 +199,6 @@ pub const RelocIterator = struct {
 
     pub fn peek(self: RelocIterator) macho.relocation_info {
         assert(self.index + 1 < self.buffer.len);
-        return self.buffer[@intCast(u64, self.index + 1)];
+        return self.buffer[@intCast(u32, self.index + 1)];
     }
 };
src/link/MachO/Zld.zig
@@ -2214,7 +2214,7 @@ fn flush(self: *Zld) !void {
         const seg = self.load_commands.items[self.data_segment_cmd_index.?].Segment;
         const sect = &seg.sections.items[index];
 
-        var buffer = try self.allocator.alloc(u8, sect.size);
+        var buffer = try self.allocator.alloc(u8, @intCast(usize, sect.size));
         defer self.allocator.free(buffer);
         _ = try self.file.?.preadAll(buffer, sect.offset);