authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-13 00:31:41+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-13 00:31:41+02:00
log6910a50ae59d978b47897e6cc04601f4e7e395c8
tree6bb5e757677812d994b711ac78237b26691614c1
parent1a6d12ea92eb9033aed686c88b53ff406ec2fbe7

elf: add u64 to usize casts where required


4 files changed, 54 insertions(+), 44 deletions(-)

src/link/Elf.zig+3-2
......@@ -1088,7 +1088,8 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
10881088 if (!atom_ptr.alive) continue;
10891089 const shdr = &self.shdrs.items[atom_ptr.output_section_index];
10901090 const file_offset = shdr.sh_offset + atom_ptr.value - shdr.sh_addr;
1091 const code = try gpa.alloc(u8, atom_ptr.size);
1091 const size = math.cast(usize, atom_ptr.size) orelse return error.Overflow;
1092 const code = try gpa.alloc(u8, size);
10921093 defer gpa.free(code);
10931094 const amt = try self.base.file.?.preadAll(code, file_offset);
10941095 if (amt != code.len) return error.InputOutput;
......@@ -3145,7 +3146,7 @@ fn writeSymtab(self: *Elf) !void {
31453146 .p32 => @sizeOf(elf.Elf32_Sym),
31463147 .p64 => @sizeOf(elf.Elf64_Sym),
31473148 };
3148 const nsyms = @divExact(shdr.sh_size, sym_size);
3149 const nsyms = math.cast(usize, @divExact(shdr.sh_size, sym_size)) orelse return error.Overflow;
31493150
31503151 log.debug("writing {d} symbols at 0x{x}", .{ nsyms, shdr.sh_offset });
31513152
src/link/Elf/Atom.zig+7-6
......@@ -51,7 +51,7 @@ pub fn inputShdr(self: Atom, elf_file: *Elf) elf.Elf64_Shdr {
5151 return object.shdrs.items[self.input_section_index];
5252}
5353
54pub fn codeInObject(self: Atom, elf_file: *Elf) []const u8 {
54pub fn codeInObject(self: Atom, elf_file: *Elf) error{Overflow}![]const u8 {
5555 const object = elf_file.file(self.file_index).?.object;
5656 return object.shdrContents(self.input_section_index);
5757}
......@@ -60,7 +60,7 @@ pub fn codeInObject(self: Atom, elf_file: *Elf) []const u8 {
6060/// Caller owns the memory.
6161pub fn codeInObjectUncompressAlloc(self: Atom, elf_file: *Elf) ![]u8 {
6262 const gpa = elf_file.base.allocator;
63 const data = self.codeInObject(elf_file);
63 const data = try self.codeInObject(elf_file);
6464 const shdr = self.inputShdr(elf_file);
6565 if (shdr.sh_flags & elf.SHF_COMPRESSED != 0) {
6666 const chdr = @as(*align(1) const elf.Elf64_Chdr, @ptrCast(data.ptr)).*;
......@@ -70,7 +70,8 @@ pub fn codeInObjectUncompressAlloc(self: Atom, elf_file: *Elf) ![]u8 {
7070 var zlib_stream = std.compress.zlib.decompressStream(gpa, stream.reader()) catch
7171 return error.InputOutput;
7272 defer zlib_stream.deinit();
73 const decomp = try gpa.alloc(u8, chdr.ch_size);
73 const size = std.math.cast(usize, chdr.ch_size) orelse return error.Overflow;
74 const decomp = try gpa.alloc(u8, size);
7475 const nread = zlib_stream.reader().readAll(decomp) catch return error.InputOutput;
7576 if (nread != decomp.len) {
7677 return error.InputOutput;
......@@ -288,7 +289,7 @@ pub fn free(self: *Atom, elf_file: *Elf) void {
288289 self.* = .{};
289290}
290291
291pub fn relocs(self: Atom, elf_file: *Elf) []align(1) const elf.Elf64_Rela {
292pub fn relocs(self: Atom, elf_file: *Elf) error{Overflow}![]align(1) const elf.Elf64_Rela {
292293 return switch (elf_file.file(self.file_index).?) {
293294 .zig_module => |x| x.relocs.items[self.relocs_section_index].items,
294295 .object => |x| x.getRelocs(self.relocs_section_index),
......@@ -314,7 +315,7 @@ pub fn freeRelocs(self: Atom, elf_file: *Elf) void {
314315
315316pub fn scanRelocs(self: Atom, elf_file: *Elf, undefs: anytype) !void {
316317 const file_ptr = elf_file.file(self.file_index).?;
317 const rels = self.relocs(elf_file);
318 const rels = try self.relocs(elf_file);
318319 var i: usize = 0;
319320 while (i < rels.len) : (i += 1) {
320321 const rel = rels[i];
......@@ -407,7 +408,7 @@ pub fn resolveRelocs(self: Atom, elf_file: *Elf, code: []u8) !void {
407408 var stream = std.io.fixedBufferStream(code);
408409 const cwriter = stream.writer();
409410
410 for (self.relocs(elf_file)) |rel| {
411 for (try self.relocs(elf_file)) |rel| {
411412 const r_type = rel.r_type();
412413 if (r_type == elf.R_X86_64_NONE) continue;
413414
src/link/Elf/Object.zig+20-16
......@@ -54,12 +54,13 @@ pub fn parse(self: *Object, elf_file: *Elf) !void {
5454
5555 const gpa = elf_file.base.allocator;
5656
57 const shoff = math.cast(usize, self.header.?.e_shoff) orelse return error.Overflow;
5758 const shdrs = @as(
5859 [*]align(1) const elf.Elf64_Shdr,
59 @ptrCast(self.data.ptr + self.header.?.e_shoff),
60 @ptrCast(self.data.ptr + shoff),
6061 )[0..self.header.?.e_shnum];
6162 try self.shdrs.appendUnalignedSlice(gpa, shdrs);
62 try self.strings.buffer.appendSlice(gpa, self.shdrContents(self.header.?.e_shstrndx));
63 try self.strings.buffer.appendSlice(gpa, try self.shdrContents(self.header.?.e_shstrndx));
6364
6465 const symtab_index = for (self.shdrs.items, 0..) |shdr, i| switch (shdr.sh_type) {
6566 elf.SHT_SYMTAB => break @as(u16, @intCast(i)),
......@@ -70,10 +71,10 @@ pub fn parse(self: *Object, elf_file: *Elf) !void {
7071 const shdr = shdrs[index];
7172 self.first_global = shdr.sh_info;
7273
73 const symtab = self.shdrContents(index);
74 const symtab = try self.shdrContents(index);
7475 const nsyms = @divExact(symtab.len, @sizeOf(elf.Elf64_Sym));
7576 self.symtab = @as([*]align(1) const elf.Elf64_Sym, @ptrCast(symtab.ptr))[0..nsyms];
76 self.strtab = self.shdrContents(@as(u16, @intCast(shdr.sh_link)));
77 self.strtab = try self.shdrContents(@as(u16, @intCast(shdr.sh_link)));
7778 }
7879
7980 try self.initAtoms(elf_file);
......@@ -114,7 +115,7 @@ fn initAtoms(self: *Object, elf_file: *Elf) !void {
114115 };
115116
116117 const shndx = @as(u16, @intCast(i));
117 const group_raw_data = self.shdrContents(shndx);
118 const group_raw_data = try self.shdrContents(shndx);
118119 const group_nmembers = @divExact(group_raw_data.len, @sizeOf(u32));
119120 const group_members = @as([*]align(1) const u32, @ptrCast(group_raw_data.ptr))[0..group_nmembers];
120121
......@@ -177,7 +178,7 @@ fn addAtom(self: *Object, shdr: elf.Elf64_Shdr, shndx: u16, name: [:0]const u8,
177178 self.atoms.items[shndx] = atom_index;
178179
179180 if (shdr.sh_flags & elf.SHF_COMPRESSED != 0) {
180 const data = self.shdrContents(shndx);
181 const data = try self.shdrContents(shndx);
181182 const chdr = @as(*align(1) const elf.Elf64_Chdr, @ptrCast(data.ptr)).*;
182183 atom.size = chdr.ch_size;
183184 atom.alignment = math.log2_int(u64, chdr.ch_addralign);
......@@ -294,8 +295,8 @@ fn parseEhFrame(self: *Object, shndx: u16, elf_file: *Elf) !void {
294295 };
295296
296297 const gpa = elf_file.base.allocator;
297 const raw = self.shdrContents(shndx);
298 const relocs = self.getRelocs(relocs_shndx);
298 const raw = try self.shdrContents(shndx);
299 const relocs = try self.getRelocs(relocs_shndx);
299300 const fdes_start = self.fdes.items.len;
300301 const cies_start = self.cies.items.len;
301302
......@@ -403,7 +404,7 @@ pub fn scanRelocs(self: *Object, elf_file: *Elf, undefs: anytype) !void {
403404 }
404405
405406 for (self.cies.items) |cie| {
406 for (cie.relocs(elf_file)) |rel| {
407 for (try cie.relocs(elf_file)) |rel| {
407408 const sym = elf_file.symbol(self.symbols.items[rel.r_sym()]);
408409 if (sym.flags.import) {
409410 if (sym.type(elf_file) != elf.STT_FUNC)
......@@ -656,10 +657,12 @@ pub fn globals(self: *Object) []const Symbol.Index {
656657 return self.symbols.items[start..];
657658}
658659
659pub fn shdrContents(self: *Object, index: u32) []const u8 {
660pub fn shdrContents(self: *Object, index: u32) error{Overflow}![]const u8 {
660661 assert(index < self.shdrs.items.len);
661662 const shdr = self.shdrs.items[index];
662 return self.data[shdr.sh_offset..][0..shdr.sh_size];
663 const offset = math.cast(usize, shdr.sh_offset) orelse return error.Overflow;
664 const size = math.cast(usize, shdr.sh_size) orelse return error.Overflow;
665 return self.data[offset..][0..size];
663666}
664667
665668fn getString(self: *Object, off: u32) [:0]const u8 {
......@@ -667,8 +670,8 @@ fn getString(self: *Object, off: u32) [:0]const u8 {
667670 return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.strtab.ptr + off)), 0);
668671}
669672
670pub fn comdatGroupMembers(self: *Object, index: u16) []align(1) const u32 {
671 const raw = self.shdrContents(index);
673pub fn comdatGroupMembers(self: *Object, index: u16) error{Overflow}![]align(1) const u32 {
674 const raw = try self.shdrContents(index);
672675 const nmembers = @divExact(raw.len, @sizeOf(u32));
673676 const members = @as([*]align(1) const u32, @ptrCast(raw.ptr))[1..nmembers];
674677 return members;
......@@ -678,8 +681,8 @@ pub fn asFile(self: *Object) File {
678681 return .{ .object = self };
679682}
680683
681pub fn getRelocs(self: *Object, shndx: u32) []align(1) const elf.Elf64_Rela {
682 const raw = self.shdrContents(shndx);
684pub fn getRelocs(self: *Object, shndx: u32) error{Overflow}![]align(1) const elf.Elf64_Rela {
685 const raw = try self.shdrContents(shndx);
683686 const num = @divExact(raw.len, @sizeOf(elf.Elf64_Rela));
684687 return @as([*]align(1) const elf.Elf64_Rela, @ptrCast(raw.ptr))[0..num];
685688}
......@@ -819,7 +822,8 @@ fn formatComdatGroups(
819822 const cg = elf_file.comdatGroup(cg_index);
820823 const cg_owner = elf_file.comdatGroupOwner(cg.owner);
821824 if (cg_owner.file != object.index) continue;
822 for (object.comdatGroupMembers(cg.shndx)) |shndx| {
825 const cg_members = object.comdatGroupMembers(cg.shndx) catch continue;
826 for (cg_members) |shndx| {
823827 const atom_index = object.atoms.items[shndx];
824828 const atom = elf_file.atom(atom_index) orelse continue;
825829 try writer.print(" atom({d}) : {s}\n", .{ atom_index, atom.name(elf_file) });
src/link/Elf/eh_frame.zig+24-20
......@@ -20,9 +20,9 @@ pub const Fde = struct {
2020 return base + fde.out_offset;
2121 }
2222
23 pub fn data(fde: Fde, elf_file: *Elf) []const u8 {
23 pub fn data(fde: Fde, elf_file: *Elf) error{Overflow}![]const u8 {
2424 const object = elf_file.file(fde.file_index).?.object;
25 const contents = object.shdrContents(fde.input_section_index);
25 const contents = try object.shdrContents(fde.input_section_index);
2626 return contents[fde.offset..][0..fde.calcSize()];
2727 }
2828
......@@ -39,17 +39,17 @@ pub const Fde = struct {
3939 return fde.size + 4;
4040 }
4141
42 pub fn atom(fde: Fde, elf_file: *Elf) *Atom {
42 pub fn atom(fde: Fde, elf_file: *Elf) error{Overflow}!*Atom {
4343 const object = elf_file.file(fde.file_index).?.object;
44 const rel = fde.relocs(elf_file)[0];
44 const rel = (try fde.relocs(elf_file))[0];
4545 const sym = object.symtab[rel.r_sym()];
4646 const atom_index = object.atoms.items[sym.st_shndx];
4747 return elf_file.atom(atom_index).?;
4848 }
4949
50 pub fn relocs(fde: Fde, elf_file: *Elf) []align(1) const elf.Elf64_Rela {
50 pub fn relocs(fde: Fde, elf_file: *Elf) error{Overflow}![]align(1) const elf.Elf64_Rela {
5151 const object = elf_file.file(fde.file_index).?.object;
52 return object.getRelocs(fde.rel_section_index)[fde.rel_index..][0..fde.rel_num];
52 return (try object.getRelocs(fde.rel_section_index))[fde.rel_index..][0..fde.rel_num];
5353 }
5454
5555 pub fn format(
......@@ -88,11 +88,15 @@ pub const Fde = struct {
8888 const fde = ctx.fde;
8989 const elf_file = ctx.elf_file;
9090 const base_addr = fde.address(elf_file);
91 const atom_name = if (fde.atom(elf_file)) |atom_ptr|
92 atom_ptr.name(elf_file)
93 else |_|
94 "";
9195 try writer.print("@{x} : size({x}) : cie({d}) : {s}", .{
9296 base_addr + fde.out_offset,
9397 fde.calcSize(),
9498 fde.cie_index,
95 fde.atom(elf_file).name(elf_file),
99 atom_name,
96100 });
97101 if (!fde.alive) try writer.writeAll(" : [*]");
98102 }
......@@ -119,9 +123,9 @@ pub const Cie = struct {
119123 return base + cie.out_offset;
120124 }
121125
122 pub fn data(cie: Cie, elf_file: *Elf) []const u8 {
126 pub fn data(cie: Cie, elf_file: *Elf) error{Overflow}![]const u8 {
123127 const object = elf_file.file(cie.file_index).?.object;
124 const contents = object.shdrContents(cie.input_section_index);
128 const contents = try object.shdrContents(cie.input_section_index);
125129 return contents[cie.offset..][0..cie.calcSize()];
126130 }
127131
......@@ -129,16 +133,16 @@ pub const Cie = struct {
129133 return cie.size + 4;
130134 }
131135
132 pub fn relocs(cie: Cie, elf_file: *Elf) []align(1) const elf.Elf64_Rela {
136 pub fn relocs(cie: Cie, elf_file: *Elf) error{Overflow}![]align(1) const elf.Elf64_Rela {
133137 const object = elf_file.file(cie.file_index).?.object;
134 return object.getRelocs(cie.rel_section_index)[cie.rel_index..][0..cie.rel_num];
138 return (try object.getRelocs(cie.rel_section_index))[cie.rel_index..][0..cie.rel_num];
135139 }
136140
137 pub fn eql(cie: Cie, other: Cie, elf_file: *Elf) bool {
138 if (!std.mem.eql(u8, cie.data(elf_file), other.data(elf_file))) return false;
141 pub fn eql(cie: Cie, other: Cie, elf_file: *Elf) error{Overflow}!bool {
142 if (!std.mem.eql(u8, try cie.data(elf_file), try other.data(elf_file))) return false;
139143
140 const cie_relocs = cie.relocs(elf_file);
141 const other_relocs = other.relocs(elf_file);
144 const cie_relocs = try cie.relocs(elf_file);
145 const other_relocs = try other.relocs(elf_file);
142146 if (cie_relocs.len != other_relocs.len) return false;
143147
144148 for (cie_relocs, other_relocs) |cie_rel, other_rel| {
......@@ -315,10 +319,10 @@ pub fn writeEhFrame(elf_file: *Elf, writer: anytype) !void {
315319 for (object.cies.items) |cie| {
316320 if (!cie.alive) continue;
317321
318 const contents = try gpa.dupe(u8, cie.data(elf_file));
322 const contents = try gpa.dupe(u8, try cie.data(elf_file));
319323 defer gpa.free(contents);
320324
321 for (cie.relocs(elf_file)) |rel| {
325 for (try cie.relocs(elf_file)) |rel| {
322326 const sym = object.symbol(rel.r_sym(), elf_file);
323327 try resolveReloc(cie, sym, rel, elf_file, contents);
324328 }
......@@ -333,7 +337,7 @@ pub fn writeEhFrame(elf_file: *Elf, writer: anytype) !void {
333337 for (object.fdes.items) |fde| {
334338 if (!fde.alive) continue;
335339
336 const contents = try gpa.dupe(u8, fde.data(elf_file));
340 const contents = try gpa.dupe(u8, try fde.data(elf_file));
337341 defer gpa.free(contents);
338342
339343 std.mem.writeIntLittle(
......@@ -342,7 +346,7 @@ pub fn writeEhFrame(elf_file: *Elf, writer: anytype) !void {
342346 @as(i32, @truncate(@as(i64, @intCast(fde.out_offset + 4)) - @as(i64, @intCast(fde.cie(elf_file).out_offset)))),
343347 );
344348
345 for (fde.relocs(elf_file)) |rel| {
349 for (try fde.relocs(elf_file)) |rel| {
346350 const sym = object.symbol(rel.r_sym(), elf_file);
347351 try resolveReloc(fde, sym, rel, elf_file, contents);
348352 }
......@@ -391,7 +395,7 @@ pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void {
391395 for (object.fdes.items) |fde| {
392396 if (!fde.alive) continue;
393397
394 const relocs = fde.relocs(elf_file);
398 const relocs = try fde.relocs(elf_file);
395399 assert(relocs.len > 0); // Should this be an error? Things are completely broken anyhow if this trips...
396400 const rel = relocs[0];
397401 const sym = object.symbol(rel.r_sym(), elf_file);