elf: fix 32bit build

This commit is contained in:
Jakub Konka
2023-10-16 19:56:47 +02:00
parent e89155b565
commit d2727b808c
5 changed files with 28 additions and 21 deletions

View File

@@ -4813,7 +4813,7 @@ fn writeAtoms(self: *Elf) !void {
unreachable;
} else 0;
const sh_offset = shdr.sh_offset + base_offset;
const sh_size = shdr.sh_size - base_offset;
const sh_size = math.cast(usize, shdr.sh_size - base_offset) orelse return error.Overflow;
const buffer = try gpa.alloc(u8, sh_size);
defer gpa.free(buffer);
@@ -4829,12 +4829,14 @@ fn writeAtoms(self: *Elf) !void {
assert(atom_ptr.flags.alive);
const object = atom_ptr.file(self).?.object;
const offset = atom_ptr.value - shdr.sh_addr - base_offset;
const offset = math.cast(usize, atom_ptr.value - shdr.sh_addr - base_offset) orelse
return error.Overflow;
const size = math.cast(usize, atom_ptr.size) orelse return error.Overflow;
log.debug("writing atom({d}) at 0x{x}", .{ atom_index, sh_offset + offset });
// TODO decompress directly into provided buffer
const out_code = buffer[offset..][0..atom_ptr.size];
const out_code = buffer[offset..][0..size];
const in_code = try object.codeDecompressAlloc(self, atom_index);
defer gpa.free(in_code);
@memcpy(out_code, in_code);
@@ -4924,7 +4926,8 @@ fn writeSyntheticSections(self: *Elf) !void {
if (self.interp_section_index) |shndx| {
const shdr = self.shdrs.items[shndx];
var buffer = try gpa.alloc(u8, shdr.sh_size);
const sh_size = math.cast(usize, shdr.sh_size) orelse return error.Overflow;
var buffer = try gpa.alloc(u8, sh_size);
defer gpa.free(buffer);
const dylinker = self.base.options.dynamic_linker.?;
@memcpy(buffer[0..dylinker.len], dylinker);
@@ -4981,7 +4984,8 @@ fn writeSyntheticSections(self: *Elf) !void {
if (self.eh_frame_section_index) |shndx| {
const shdr = self.shdrs.items[shndx];
var buffer = try std.ArrayList(u8).initCapacity(gpa, shdr.sh_size);
const sh_size = math.cast(usize, shdr.sh_size) orelse return error.Overflow;
var buffer = try std.ArrayList(u8).initCapacity(gpa, sh_size);
defer buffer.deinit();
try eh_frame.writeEhFrame(self, buffer.writer());
try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset);
@@ -4989,7 +4993,8 @@ fn writeSyntheticSections(self: *Elf) !void {
if (self.eh_frame_hdr_section_index) |shndx| {
const shdr = self.shdrs.items[shndx];
var buffer = try std.ArrayList(u8).initCapacity(gpa, shdr.sh_size);
const sh_size = math.cast(usize, shdr.sh_size) orelse return error.Overflow;
var buffer = try std.ArrayList(u8).initCapacity(gpa, sh_size);
defer buffer.deinit();
try eh_frame.writeEhFrameHdr(self, buffer.writer());
try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset);

View File

@@ -859,7 +859,7 @@ pub fn resolveRelocsAlloc(self: Atom, elf_file: *Elf, code: []u8) !void {
const S_ = @as(i64, @intCast(target.tlsDescAddress(elf_file)));
try cwriter.writeIntLittle(i32, @as(i32, @intCast(S_ + A - P)));
} else {
try x86_64.relaxGotPcTlsDesc(code[rel.r_offset - 3 ..]);
try x86_64.relaxGotPcTlsDesc(code[r_offset - 3 ..]);
try cwriter.writeIntLittle(i32, @as(i32, @intCast(S - TP)));
}
},

View File

@@ -592,13 +592,14 @@ pub fn convertCommonSymbols(self: *Object, elf_file: *Elf) !void {
if (is_tls) sh_flags |= elf.SHF_TLS;
const shndx = @as(u16, @intCast(self.shdrs.items.len));
const shdr = try self.shdrs.addOne(gpa);
const sh_size = math.cast(usize, this_sym.st_size) orelse return error.Overflow;
shdr.* = .{
.sh_name = try self.strings.insert(gpa, name),
.sh_type = elf.SHT_NOBITS,
.sh_flags = sh_flags,
.sh_addr = 0,
.sh_offset = 0,
.sh_size = this_sym.st_size,
.sh_size = sh_size,
.sh_link = 0,
.sh_info = 0,
.sh_addralign = alignment,

View File

@@ -47,11 +47,12 @@ pub fn parse(self: *SharedObject, elf_file: *Elf) !void {
const reader = stream.reader();
self.header = try reader.readStruct(elf.Elf64_Ehdr);
const shoff = std.math.cast(usize, self.header.?.e_shoff) orelse return error.Overflow;
var dynsym_index: ?u16 = null;
const shdrs = @as(
[*]align(1) const elf.Elf64_Shdr,
@ptrCast(self.data.ptr + self.header.?.e_shoff),
@ptrCast(self.data.ptr + shoff),
)[0..self.header.?.e_shnum];
try self.shdrs.ensureTotalCapacityPrecise(gpa, shdrs.len);

View File

@@ -1,7 +1,7 @@
pub const Fde = struct {
/// Includes 4byte size cell.
offset: u64,
size: u64,
offset: usize,
size: usize,
cie_index: u32,
rel_index: u32 = 0,
rel_num: u32 = 0,
@@ -36,7 +36,7 @@ pub const Fde = struct {
return std.mem.readIntLittle(u32, fde_data[4..8]);
}
pub fn calcSize(fde: Fde) u64 {
pub fn calcSize(fde: Fde) usize {
return fde.size + 4;
}
@@ -102,8 +102,8 @@ pub const Fde = struct {
pub const Cie = struct {
/// Includes 4byte size cell.
offset: u64,
size: u64,
offset: usize,
size: usize,
rel_index: u32 = 0,
rel_num: u32 = 0,
rel_section_index: u32 = 0,
@@ -127,7 +127,7 @@ pub const Cie = struct {
return contents[cie.offset..][0..cie.calcSize()];
}
pub fn calcSize(cie: Cie) u64 {
pub fn calcSize(cie: Cie) usize {
return cie.size + 4;
}
@@ -203,12 +203,12 @@ pub const Cie = struct {
pub const Iterator = struct {
data: []const u8,
pos: u64 = 0,
pos: usize = 0,
pub const Record = struct {
tag: enum { fde, cie },
offset: u64,
size: u64,
offset: usize,
size: usize,
};
pub fn next(it: *Iterator) !?Record {
@@ -233,7 +233,7 @@ pub const Iterator = struct {
};
pub fn calcEhFrameSize(elf_file: *Elf) !usize {
var offset: u64 = 0;
var offset: usize = 0;
var cies = std.ArrayList(Cie).init(elf_file.base.allocator);
defer cies.deinit();
@@ -283,7 +283,7 @@ pub fn calcEhFrameHdrSize(elf_file: *Elf) usize {
}
fn resolveReloc(rec: anytype, sym: *const Symbol, rel: elf.Elf64_Rela, elf_file: *Elf, contents: []u8) !void {
const offset = rel.r_offset - rec.offset;
const offset = std.math.cast(usize, rel.r_offset - rec.offset) orelse return error.Overflow;
const P = @as(i64, @intCast(rec.address(elf_file) + offset));
const S = @as(i64, @intCast(sym.address(.{}, elf_file)));
const A = rel.r_addend;
@@ -414,7 +414,7 @@ pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void {
try writer.writeAll(std.mem.sliceAsBytes(entries.items));
}
const eh_frame_hdr_header_size: u64 = 12;
const eh_frame_hdr_header_size: usize = 12;
const EH_PE = struct {
pub const absptr = 0x00;