authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-16 19:56:47+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-16 19:56:47+02:00
logd2727b808cf0876a91b78b3d40a776fc60d2bd58
tree5d4d6484072a5f4b02f5d211d1675b274309b64b
parente89155b565885d8fb0aca3d9843fba914f43e7d9

elf: fix 32bit build


5 files changed, 28 insertions(+), 21 deletions(-)

src/link/Elf.zig+11-6
...@@ -4813,7 +4813,7 @@ fn writeAtoms(self: *Elf) !void {...@@ -4813,7 +4813,7 @@ fn writeAtoms(self: *Elf) !void {
4813 unreachable;4813 unreachable;
4814 } else 0;4814 } else 0;
4815 const sh_offset = shdr.sh_offset + base_offset;4815 const sh_offset = shdr.sh_offset + base_offset;
4816 const sh_size = shdr.sh_size - base_offset;4816 const sh_size = math.cast(usize, shdr.sh_size - base_offset) orelse return error.Overflow;
48174817
4818 const buffer = try gpa.alloc(u8, sh_size);4818 const buffer = try gpa.alloc(u8, sh_size);
4819 defer gpa.free(buffer);4819 defer gpa.free(buffer);
...@@ -4829,12 +4829,14 @@ fn writeAtoms(self: *Elf) !void {...@@ -4829,12 +4829,14 @@ fn writeAtoms(self: *Elf) !void {
4829 assert(atom_ptr.flags.alive);4829 assert(atom_ptr.flags.alive);
48304830
4831 const object = atom_ptr.file(self).?.object;4831 const object = atom_ptr.file(self).?.object;
4832 const offset = atom_ptr.value - shdr.sh_addr - base_offset;4832 const offset = math.cast(usize, atom_ptr.value - shdr.sh_addr - base_offset) orelse
4833 return error.Overflow;
4834 const size = math.cast(usize, atom_ptr.size) orelse return error.Overflow;
48334835
4834 log.debug("writing atom({d}) at 0x{x}", .{ atom_index, sh_offset + offset });4836 log.debug("writing atom({d}) at 0x{x}", .{ atom_index, sh_offset + offset });
48354837
4836 // TODO decompress directly into provided buffer4838 // TODO decompress directly into provided buffer
4837 const out_code = buffer[offset..][0..atom_ptr.size];4839 const out_code = buffer[offset..][0..size];
4838 const in_code = try object.codeDecompressAlloc(self, atom_index);4840 const in_code = try object.codeDecompressAlloc(self, atom_index);
4839 defer gpa.free(in_code);4841 defer gpa.free(in_code);
4840 @memcpy(out_code, in_code);4842 @memcpy(out_code, in_code);
...@@ -4924,7 +4926,8 @@ fn writeSyntheticSections(self: *Elf) !void {...@@ -4924,7 +4926,8 @@ fn writeSyntheticSections(self: *Elf) !void {
49244926
4925 if (self.interp_section_index) |shndx| {4927 if (self.interp_section_index) |shndx| {
4926 const shdr = self.shdrs.items[shndx];4928 const shdr = self.shdrs.items[shndx];
4927 var buffer = try gpa.alloc(u8, shdr.sh_size);4929 const sh_size = math.cast(usize, shdr.sh_size) orelse return error.Overflow;
4930 var buffer = try gpa.alloc(u8, sh_size);
4928 defer gpa.free(buffer);4931 defer gpa.free(buffer);
4929 const dylinker = self.base.options.dynamic_linker.?;4932 const dylinker = self.base.options.dynamic_linker.?;
4930 @memcpy(buffer[0..dylinker.len], dylinker);4933 @memcpy(buffer[0..dylinker.len], dylinker);
...@@ -4981,7 +4984,8 @@ fn writeSyntheticSections(self: *Elf) !void {...@@ -4981,7 +4984,8 @@ fn writeSyntheticSections(self: *Elf) !void {
49814984
4982 if (self.eh_frame_section_index) |shndx| {4985 if (self.eh_frame_section_index) |shndx| {
4983 const shdr = self.shdrs.items[shndx];4986 const shdr = self.shdrs.items[shndx];
4984 var buffer = try std.ArrayList(u8).initCapacity(gpa, shdr.sh_size);4987 const sh_size = math.cast(usize, shdr.sh_size) orelse return error.Overflow;
4988 var buffer = try std.ArrayList(u8).initCapacity(gpa, sh_size);
4985 defer buffer.deinit();4989 defer buffer.deinit();
4986 try eh_frame.writeEhFrame(self, buffer.writer());4990 try eh_frame.writeEhFrame(self, buffer.writer());
4987 try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset);4991 try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset);
...@@ -4989,7 +4993,8 @@ fn writeSyntheticSections(self: *Elf) !void {...@@ -4989,7 +4993,8 @@ fn writeSyntheticSections(self: *Elf) !void {
49894993
4990 if (self.eh_frame_hdr_section_index) |shndx| {4994 if (self.eh_frame_hdr_section_index) |shndx| {
4991 const shdr = self.shdrs.items[shndx];4995 const shdr = self.shdrs.items[shndx];
4992 var buffer = try std.ArrayList(u8).initCapacity(gpa, shdr.sh_size);4996 const sh_size = math.cast(usize, shdr.sh_size) orelse return error.Overflow;
4997 var buffer = try std.ArrayList(u8).initCapacity(gpa, sh_size);
4993 defer buffer.deinit();4998 defer buffer.deinit();
4994 try eh_frame.writeEhFrameHdr(self, buffer.writer());4999 try eh_frame.writeEhFrameHdr(self, buffer.writer());
4995 try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset);5000 try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset);
src/link/Elf/Atom.zig+1-1
...@@ -859,7 +859,7 @@ pub fn resolveRelocsAlloc(self: Atom, elf_file: *Elf, code: []u8) !void {...@@ -859,7 +859,7 @@ pub fn resolveRelocsAlloc(self: Atom, elf_file: *Elf, code: []u8) !void {
859 const S_ = @as(i64, @intCast(target.tlsDescAddress(elf_file)));859 const S_ = @as(i64, @intCast(target.tlsDescAddress(elf_file)));
860 try cwriter.writeIntLittle(i32, @as(i32, @intCast(S_ + A - P)));860 try cwriter.writeIntLittle(i32, @as(i32, @intCast(S_ + A - P)));
861 } else {861 } else {
862 try x86_64.relaxGotPcTlsDesc(code[rel.r_offset - 3 ..]);862 try x86_64.relaxGotPcTlsDesc(code[r_offset - 3 ..]);
863 try cwriter.writeIntLittle(i32, @as(i32, @intCast(S - TP)));863 try cwriter.writeIntLittle(i32, @as(i32, @intCast(S - TP)));
864 }864 }
865 },865 },
src/link/Elf/Object.zig+2-1
...@@ -592,13 +592,14 @@ pub fn convertCommonSymbols(self: *Object, elf_file: *Elf) !void {...@@ -592,13 +592,14 @@ pub fn convertCommonSymbols(self: *Object, elf_file: *Elf) !void {
592 if (is_tls) sh_flags |= elf.SHF_TLS;592 if (is_tls) sh_flags |= elf.SHF_TLS;
593 const shndx = @as(u16, @intCast(self.shdrs.items.len));593 const shndx = @as(u16, @intCast(self.shdrs.items.len));
594 const shdr = try self.shdrs.addOne(gpa);594 const shdr = try self.shdrs.addOne(gpa);
595 const sh_size = math.cast(usize, this_sym.st_size) orelse return error.Overflow;
595 shdr.* = .{596 shdr.* = .{
596 .sh_name = try self.strings.insert(gpa, name),597 .sh_name = try self.strings.insert(gpa, name),
597 .sh_type = elf.SHT_NOBITS,598 .sh_type = elf.SHT_NOBITS,
598 .sh_flags = sh_flags,599 .sh_flags = sh_flags,
599 .sh_addr = 0,600 .sh_addr = 0,
600 .sh_offset = 0,601 .sh_offset = 0,
601 .sh_size = this_sym.st_size,602 .sh_size = sh_size,
602 .sh_link = 0,603 .sh_link = 0,
603 .sh_info = 0,604 .sh_info = 0,
604 .sh_addralign = alignment,605 .sh_addralign = alignment,
src/link/Elf/SharedObject.zig+2-1
...@@ -47,11 +47,12 @@ pub fn parse(self: *SharedObject, elf_file: *Elf) !void {...@@ -47,11 +47,12 @@ pub fn parse(self: *SharedObject, elf_file: *Elf) !void {
47 const reader = stream.reader();47 const reader = stream.reader();
4848
49 self.header = try reader.readStruct(elf.Elf64_Ehdr);49 self.header = try reader.readStruct(elf.Elf64_Ehdr);
50 const shoff = std.math.cast(usize, self.header.?.e_shoff) orelse return error.Overflow;
5051
51 var dynsym_index: ?u16 = null;52 var dynsym_index: ?u16 = null;
52 const shdrs = @as(53 const shdrs = @as(
53 [*]align(1) const elf.Elf64_Shdr,54 [*]align(1) const elf.Elf64_Shdr,
54 @ptrCast(self.data.ptr + self.header.?.e_shoff),55 @ptrCast(self.data.ptr + shoff),
55 )[0..self.header.?.e_shnum];56 )[0..self.header.?.e_shnum];
56 try self.shdrs.ensureTotalCapacityPrecise(gpa, shdrs.len);57 try self.shdrs.ensureTotalCapacityPrecise(gpa, shdrs.len);
5758
src/link/Elf/eh_frame.zig+12-12
...@@ -1,7 +1,7 @@...@@ -1,7 +1,7 @@
1pub const Fde = struct {1pub const Fde = struct {
2 /// Includes 4byte size cell.2 /// Includes 4byte size cell.
3 offset: u64,3 offset: usize,
4 size: u64,4 size: usize,
5 cie_index: u32,5 cie_index: u32,
6 rel_index: u32 = 0,6 rel_index: u32 = 0,
7 rel_num: u32 = 0,7 rel_num: u32 = 0,
...@@ -36,7 +36,7 @@ pub const Fde = struct {...@@ -36,7 +36,7 @@ pub const Fde = struct {
36 return std.mem.readIntLittle(u32, fde_data[4..8]);36 return std.mem.readIntLittle(u32, fde_data[4..8]);
37 }37 }
3838
39 pub fn calcSize(fde: Fde) u64 {39 pub fn calcSize(fde: Fde) usize {
40 return fde.size + 4;40 return fde.size + 4;
41 }41 }
4242
...@@ -102,8 +102,8 @@ pub const Fde = struct {...@@ -102,8 +102,8 @@ pub const Fde = struct {
102102
103pub const Cie = struct {103pub const Cie = struct {
104 /// Includes 4byte size cell.104 /// Includes 4byte size cell.
105 offset: u64,105 offset: usize,
106 size: u64,106 size: usize,
107 rel_index: u32 = 0,107 rel_index: u32 = 0,
108 rel_num: u32 = 0,108 rel_num: u32 = 0,
109 rel_section_index: u32 = 0,109 rel_section_index: u32 = 0,
...@@ -127,7 +127,7 @@ pub const Cie = struct {...@@ -127,7 +127,7 @@ pub const Cie = struct {
127 return contents[cie.offset..][0..cie.calcSize()];127 return contents[cie.offset..][0..cie.calcSize()];
128 }128 }
129129
130 pub fn calcSize(cie: Cie) u64 {130 pub fn calcSize(cie: Cie) usize {
131 return cie.size + 4;131 return cie.size + 4;
132 }132 }
133133
...@@ -203,12 +203,12 @@ pub const Cie = struct {...@@ -203,12 +203,12 @@ pub const Cie = struct {
203203
204pub const Iterator = struct {204pub const Iterator = struct {
205 data: []const u8,205 data: []const u8,
206 pos: u64 = 0,206 pos: usize = 0,
207207
208 pub const Record = struct {208 pub const Record = struct {
209 tag: enum { fde, cie },209 tag: enum { fde, cie },
210 offset: u64,210 offset: usize,
211 size: u64,211 size: usize,
212 };212 };
213213
214 pub fn next(it: *Iterator) !?Record {214 pub fn next(it: *Iterator) !?Record {
...@@ -233,7 +233,7 @@ pub const Iterator = struct {...@@ -233,7 +233,7 @@ pub const Iterator = struct {
233};233};
234234
235pub fn calcEhFrameSize(elf_file: *Elf) !usize {235pub fn calcEhFrameSize(elf_file: *Elf) !usize {
236 var offset: u64 = 0;236 var offset: usize = 0;
237237
238 var cies = std.ArrayList(Cie).init(elf_file.base.allocator);238 var cies = std.ArrayList(Cie).init(elf_file.base.allocator);
239 defer cies.deinit();239 defer cies.deinit();
...@@ -283,7 +283,7 @@ pub fn calcEhFrameHdrSize(elf_file: *Elf) usize {...@@ -283,7 +283,7 @@ pub fn calcEhFrameHdrSize(elf_file: *Elf) usize {
283}283}
284284
285fn resolveReloc(rec: anytype, sym: *const Symbol, rel: elf.Elf64_Rela, elf_file: *Elf, contents: []u8) !void {285fn resolveReloc(rec: anytype, sym: *const Symbol, rel: elf.Elf64_Rela, elf_file: *Elf, contents: []u8) !void {
286 const offset = rel.r_offset - rec.offset;286 const offset = std.math.cast(usize, rel.r_offset - rec.offset) orelse return error.Overflow;
287 const P = @as(i64, @intCast(rec.address(elf_file) + offset));287 const P = @as(i64, @intCast(rec.address(elf_file) + offset));
288 const S = @as(i64, @intCast(sym.address(.{}, elf_file)));288 const S = @as(i64, @intCast(sym.address(.{}, elf_file)));
289 const A = rel.r_addend;289 const A = rel.r_addend;
...@@ -414,7 +414,7 @@ pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void {...@@ -414,7 +414,7 @@ pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void {
414 try writer.writeAll(std.mem.sliceAsBytes(entries.items));414 try writer.writeAll(std.mem.sliceAsBytes(entries.items));
415}415}
416416
417const eh_frame_hdr_header_size: u64 = 12;417const eh_frame_hdr_header_size: usize = 12;
418418
419const EH_PE = struct {419const EH_PE = struct {
420 pub const absptr = 0x00;420 pub const absptr = 0x00;