| author | |
| committer | |
| log | 6910a50ae59d978b47897e6cc04601f4e7e395c8 |
| tree | 6bb5e757677812d994b711ac78237b26691614c1 |
| parent | 1a6d12ea92eb9033aed686c88b53ff406ec2fbe7 |
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 | ... | @@ -1088,7 +1088,8 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1088 | if (!atom_ptr.alive) continue; | 1088 | if (!atom_ptr.alive) continue; |
| 1089 | const shdr = &self.shdrs.items[atom_ptr.output_section_index]; | 1089 | const shdr = &self.shdrs.items[atom_ptr.output_section_index]; |
| 1090 | const file_offset = shdr.sh_offset + atom_ptr.value - shdr.sh_addr; | 1090 | 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); | ||
| 1092 | defer gpa.free(code); | 1093 | defer gpa.free(code); |
| 1093 | const amt = try self.base.file.?.preadAll(code, file_offset); | 1094 | const amt = try self.base.file.?.preadAll(code, file_offset); |
| 1094 | if (amt != code.len) return error.InputOutput; | 1095 | if (amt != code.len) return error.InputOutput; |
| ... | @@ -3145,7 +3146,7 @@ fn writeSymtab(self: *Elf) !void { | ... | @@ -3145,7 +3146,7 @@ fn writeSymtab(self: *Elf) !void { |
| 3145 | .p32 => @sizeOf(elf.Elf32_Sym), | 3146 | .p32 => @sizeOf(elf.Elf32_Sym), |
| 3146 | .p64 => @sizeOf(elf.Elf64_Sym), | 3147 | .p64 => @sizeOf(elf.Elf64_Sym), |
| 3147 | }; | 3148 | }; |
| 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; |
| 3149 | 3150 | ||
| 3150 | log.debug("writing {d} symbols at 0x{x}", .{ nsyms, shdr.sh_offset }); | 3151 | log.debug("writing {d} symbols at 0x{x}", .{ nsyms, shdr.sh_offset }); |
| 3151 | 3152 |
src/link/Elf/Atom.zig+7-6| ... | @@ -51,7 +51,7 @@ pub fn inputShdr(self: Atom, elf_file: *Elf) elf.Elf64_Shdr { | ... | @@ -51,7 +51,7 @@ pub fn inputShdr(self: Atom, elf_file: *Elf) elf.Elf64_Shdr { |
| 51 | return object.shdrs.items[self.input_section_index]; | 51 | return object.shdrs.items[self.input_section_index]; |
| 52 | } | 52 | } |
| 53 | 53 | ||
| 54 | pub fn codeInObject(self: Atom, elf_file: *Elf) []const u8 { | 54 | pub fn codeInObject(self: Atom, elf_file: *Elf) error{Overflow}![]const u8 { |
| 55 | const object = elf_file.file(self.file_index).?.object; | 55 | const object = elf_file.file(self.file_index).?.object; |
| 56 | return object.shdrContents(self.input_section_index); | 56 | return object.shdrContents(self.input_section_index); |
| 57 | } | 57 | } |
| ... | @@ -60,7 +60,7 @@ pub fn codeInObject(self: Atom, elf_file: *Elf) []const u8 { | ... | @@ -60,7 +60,7 @@ pub fn codeInObject(self: Atom, elf_file: *Elf) []const u8 { |
| 60 | /// Caller owns the memory. | 60 | /// Caller owns the memory. |
| 61 | pub fn codeInObjectUncompressAlloc(self: Atom, elf_file: *Elf) ![]u8 { | 61 | pub fn codeInObjectUncompressAlloc(self: Atom, elf_file: *Elf) ![]u8 { |
| 62 | const gpa = elf_file.base.allocator; | 62 | const gpa = elf_file.base.allocator; |
| 63 | const data = self.codeInObject(elf_file); | 63 | const data = try self.codeInObject(elf_file); |
| 64 | const shdr = self.inputShdr(elf_file); | 64 | const shdr = self.inputShdr(elf_file); |
| 65 | if (shdr.sh_flags & elf.SHF_COMPRESSED != 0) { | 65 | if (shdr.sh_flags & elf.SHF_COMPRESSED != 0) { |
| 66 | const chdr = @as(*align(1) const elf.Elf64_Chdr, @ptrCast(data.ptr)).*; | 66 | 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 { | ... | @@ -70,7 +70,8 @@ pub fn codeInObjectUncompressAlloc(self: Atom, elf_file: *Elf) ![]u8 { |
| 70 | var zlib_stream = std.compress.zlib.decompressStream(gpa, stream.reader()) catch | 70 | var zlib_stream = std.compress.zlib.decompressStream(gpa, stream.reader()) catch |
| 71 | return error.InputOutput; | 71 | return error.InputOutput; |
| 72 | defer zlib_stream.deinit(); | 72 | 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); | ||
| 74 | const nread = zlib_stream.reader().readAll(decomp) catch return error.InputOutput; | 75 | const nread = zlib_stream.reader().readAll(decomp) catch return error.InputOutput; |
| 75 | if (nread != decomp.len) { | 76 | if (nread != decomp.len) { |
| 76 | return error.InputOutput; | 77 | return error.InputOutput; |
| ... | @@ -288,7 +289,7 @@ pub fn free(self: *Atom, elf_file: *Elf) void { | ... | @@ -288,7 +289,7 @@ pub fn free(self: *Atom, elf_file: *Elf) void { |
| 288 | self.* = .{}; | 289 | self.* = .{}; |
| 289 | } | 290 | } |
| 290 | 291 | ||
| 291 | pub fn relocs(self: Atom, elf_file: *Elf) []align(1) const elf.Elf64_Rela { | 292 | pub fn relocs(self: Atom, elf_file: *Elf) error{Overflow}![]align(1) const elf.Elf64_Rela { |
| 292 | return switch (elf_file.file(self.file_index).?) { | 293 | return switch (elf_file.file(self.file_index).?) { |
| 293 | .zig_module => |x| x.relocs.items[self.relocs_section_index].items, | 294 | .zig_module => |x| x.relocs.items[self.relocs_section_index].items, |
| 294 | .object => |x| x.getRelocs(self.relocs_section_index), | 295 | .object => |x| x.getRelocs(self.relocs_section_index), |
| ... | @@ -314,7 +315,7 @@ pub fn freeRelocs(self: Atom, elf_file: *Elf) void { | ... | @@ -314,7 +315,7 @@ pub fn freeRelocs(self: Atom, elf_file: *Elf) void { |
| 314 | 315 | ||
| 315 | pub fn scanRelocs(self: Atom, elf_file: *Elf, undefs: anytype) !void { | 316 | pub fn scanRelocs(self: Atom, elf_file: *Elf, undefs: anytype) !void { |
| 316 | const file_ptr = elf_file.file(self.file_index).?; | 317 | const file_ptr = elf_file.file(self.file_index).?; |
| 317 | const rels = self.relocs(elf_file); | 318 | const rels = try self.relocs(elf_file); |
| 318 | var i: usize = 0; | 319 | var i: usize = 0; |
| 319 | while (i < rels.len) : (i += 1) { | 320 | while (i < rels.len) : (i += 1) { |
| 320 | const rel = rels[i]; | 321 | const rel = rels[i]; |
| ... | @@ -407,7 +408,7 @@ pub fn resolveRelocs(self: Atom, elf_file: *Elf, code: []u8) !void { | ... | @@ -407,7 +408,7 @@ pub fn resolveRelocs(self: Atom, elf_file: *Elf, code: []u8) !void { |
| 407 | var stream = std.io.fixedBufferStream(code); | 408 | var stream = std.io.fixedBufferStream(code); |
| 408 | const cwriter = stream.writer(); | 409 | const cwriter = stream.writer(); |
| 409 | 410 | ||
| 410 | for (self.relocs(elf_file)) |rel| { | 411 | for (try self.relocs(elf_file)) |rel| { |
| 411 | const r_type = rel.r_type(); | 412 | const r_type = rel.r_type(); |
| 412 | if (r_type == elf.R_X86_64_NONE) continue; | 413 | if (r_type == elf.R_X86_64_NONE) continue; |
| 413 | 414 |
src/link/Elf/Object.zig+20-16| ... | @@ -54,12 +54,13 @@ pub fn parse(self: *Object, elf_file: *Elf) !void { | ... | @@ -54,12 +54,13 @@ pub fn parse(self: *Object, elf_file: *Elf) !void { |
| 54 | 54 | ||
| 55 | const gpa = elf_file.base.allocator; | 55 | const gpa = elf_file.base.allocator; |
| 56 | 56 | ||
| 57 | const shoff = math.cast(usize, self.header.?.e_shoff) orelse return error.Overflow; | ||
| 57 | const shdrs = @as( | 58 | const shdrs = @as( |
| 58 | [*]align(1) const elf.Elf64_Shdr, | 59 | [*]align(1) const elf.Elf64_Shdr, |
| 59 | @ptrCast(self.data.ptr + self.header.?.e_shoff), | 60 | @ptrCast(self.data.ptr + shoff), |
| 60 | )[0..self.header.?.e_shnum]; | 61 | )[0..self.header.?.e_shnum]; |
| 61 | try self.shdrs.appendUnalignedSlice(gpa, shdrs); | 62 | 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)); |
| 63 | 64 | ||
| 64 | const symtab_index = for (self.shdrs.items, 0..) |shdr, i| switch (shdr.sh_type) { | 65 | const symtab_index = for (self.shdrs.items, 0..) |shdr, i| switch (shdr.sh_type) { |
| 65 | elf.SHT_SYMTAB => break @as(u16, @intCast(i)), | 66 | elf.SHT_SYMTAB => break @as(u16, @intCast(i)), |
| ... | @@ -70,10 +71,10 @@ pub fn parse(self: *Object, elf_file: *Elf) !void { | ... | @@ -70,10 +71,10 @@ pub fn parse(self: *Object, elf_file: *Elf) !void { |
| 70 | const shdr = shdrs[index]; | 71 | const shdr = shdrs[index]; |
| 71 | self.first_global = shdr.sh_info; | 72 | self.first_global = shdr.sh_info; |
| 72 | 73 | ||
| 73 | const symtab = self.shdrContents(index); | 74 | const symtab = try self.shdrContents(index); |
| 74 | const nsyms = @divExact(symtab.len, @sizeOf(elf.Elf64_Sym)); | 75 | const nsyms = @divExact(symtab.len, @sizeOf(elf.Elf64_Sym)); |
| 75 | self.symtab = @as([*]align(1) const elf.Elf64_Sym, @ptrCast(symtab.ptr))[0..nsyms]; | 76 | 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))); |
| 77 | } | 78 | } |
| 78 | 79 | ||
| 79 | try self.initAtoms(elf_file); | 80 | try self.initAtoms(elf_file); |
| ... | @@ -114,7 +115,7 @@ fn initAtoms(self: *Object, elf_file: *Elf) !void { | ... | @@ -114,7 +115,7 @@ fn initAtoms(self: *Object, elf_file: *Elf) !void { |
| 114 | }; | 115 | }; |
| 115 | 116 | ||
| 116 | const shndx = @as(u16, @intCast(i)); | 117 | const shndx = @as(u16, @intCast(i)); |
| 117 | const group_raw_data = self.shdrContents(shndx); | 118 | const group_raw_data = try self.shdrContents(shndx); |
| 118 | const group_nmembers = @divExact(group_raw_data.len, @sizeOf(u32)); | 119 | const group_nmembers = @divExact(group_raw_data.len, @sizeOf(u32)); |
| 119 | const group_members = @as([*]align(1) const u32, @ptrCast(group_raw_data.ptr))[0..group_nmembers]; | 120 | const group_members = @as([*]align(1) const u32, @ptrCast(group_raw_data.ptr))[0..group_nmembers]; |
| 120 | 121 | ||
| ... | @@ -177,7 +178,7 @@ fn addAtom(self: *Object, shdr: elf.Elf64_Shdr, shndx: u16, name: [:0]const u8, | ... | @@ -177,7 +178,7 @@ fn addAtom(self: *Object, shdr: elf.Elf64_Shdr, shndx: u16, name: [:0]const u8, |
| 177 | self.atoms.items[shndx] = atom_index; | 178 | self.atoms.items[shndx] = atom_index; |
| 178 | 179 | ||
| 179 | if (shdr.sh_flags & elf.SHF_COMPRESSED != 0) { | 180 | if (shdr.sh_flags & elf.SHF_COMPRESSED != 0) { |
| 180 | const data = self.shdrContents(shndx); | 181 | const data = try self.shdrContents(shndx); |
| 181 | const chdr = @as(*align(1) const elf.Elf64_Chdr, @ptrCast(data.ptr)).*; | 182 | const chdr = @as(*align(1) const elf.Elf64_Chdr, @ptrCast(data.ptr)).*; |
| 182 | atom.size = chdr.ch_size; | 183 | atom.size = chdr.ch_size; |
| 183 | atom.alignment = math.log2_int(u64, chdr.ch_addralign); | 184 | atom.alignment = math.log2_int(u64, chdr.ch_addralign); |
| ... | @@ -294,8 +295,8 @@ fn parseEhFrame(self: *Object, shndx: u16, elf_file: *Elf) !void { | ... | @@ -294,8 +295,8 @@ fn parseEhFrame(self: *Object, shndx: u16, elf_file: *Elf) !void { |
| 294 | }; | 295 | }; |
| 295 | 296 | ||
| 296 | const gpa = elf_file.base.allocator; | 297 | const gpa = elf_file.base.allocator; |
| 297 | const raw = self.shdrContents(shndx); | 298 | const raw = try self.shdrContents(shndx); |
| 298 | const relocs = self.getRelocs(relocs_shndx); | 299 | const relocs = try self.getRelocs(relocs_shndx); |
| 299 | const fdes_start = self.fdes.items.len; | 300 | const fdes_start = self.fdes.items.len; |
| 300 | const cies_start = self.cies.items.len; | 301 | const cies_start = self.cies.items.len; |
| 301 | 302 | ||
| ... | @@ -403,7 +404,7 @@ pub fn scanRelocs(self: *Object, elf_file: *Elf, undefs: anytype) !void { | ... | @@ -403,7 +404,7 @@ pub fn scanRelocs(self: *Object, elf_file: *Elf, undefs: anytype) !void { |
| 403 | } | 404 | } |
| 404 | 405 | ||
| 405 | for (self.cies.items) |cie| { | 406 | for (self.cies.items) |cie| { |
| 406 | for (cie.relocs(elf_file)) |rel| { | 407 | for (try cie.relocs(elf_file)) |rel| { |
| 407 | const sym = elf_file.symbol(self.symbols.items[rel.r_sym()]); | 408 | const sym = elf_file.symbol(self.symbols.items[rel.r_sym()]); |
| 408 | if (sym.flags.import) { | 409 | if (sym.flags.import) { |
| 409 | if (sym.type(elf_file) != elf.STT_FUNC) | 410 | if (sym.type(elf_file) != elf.STT_FUNC) |
| ... | @@ -656,10 +657,12 @@ pub fn globals(self: *Object) []const Symbol.Index { | ... | @@ -656,10 +657,12 @@ pub fn globals(self: *Object) []const Symbol.Index { |
| 656 | return self.symbols.items[start..]; | 657 | return self.symbols.items[start..]; |
| 657 | } | 658 | } |
| 658 | 659 | ||
| 659 | pub fn shdrContents(self: *Object, index: u32) []const u8 { | 660 | pub fn shdrContents(self: *Object, index: u32) error{Overflow}![]const u8 { |
| 660 | assert(index < self.shdrs.items.len); | 661 | assert(index < self.shdrs.items.len); |
| 661 | const shdr = self.shdrs.items[index]; | 662 | 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]; | ||
| 663 | } | 666 | } |
| 664 | 667 | ||
| 665 | fn getString(self: *Object, off: u32) [:0]const u8 { | 668 | fn getString(self: *Object, off: u32) [:0]const u8 { |
| ... | @@ -667,8 +670,8 @@ fn getString(self: *Object, off: u32) [:0]const u8 { | ... | @@ -667,8 +670,8 @@ fn getString(self: *Object, off: u32) [:0]const u8 { |
| 667 | return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.strtab.ptr + off)), 0); | 670 | return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.strtab.ptr + off)), 0); |
| 668 | } | 671 | } |
| 669 | 672 | ||
| 670 | pub fn comdatGroupMembers(self: *Object, index: u16) []align(1) const u32 { | 673 | pub fn comdatGroupMembers(self: *Object, index: u16) error{Overflow}![]align(1) const u32 { |
| 671 | const raw = self.shdrContents(index); | 674 | const raw = try self.shdrContents(index); |
| 672 | const nmembers = @divExact(raw.len, @sizeOf(u32)); | 675 | const nmembers = @divExact(raw.len, @sizeOf(u32)); |
| 673 | const members = @as([*]align(1) const u32, @ptrCast(raw.ptr))[1..nmembers]; | 676 | const members = @as([*]align(1) const u32, @ptrCast(raw.ptr))[1..nmembers]; |
| 674 | return members; | 677 | return members; |
| ... | @@ -678,8 +681,8 @@ pub fn asFile(self: *Object) File { | ... | @@ -678,8 +681,8 @@ pub fn asFile(self: *Object) File { |
| 678 | return .{ .object = self }; | 681 | return .{ .object = self }; |
| 679 | } | 682 | } |
| 680 | 683 | ||
| 681 | pub fn getRelocs(self: *Object, shndx: u32) []align(1) const elf.Elf64_Rela { | 684 | pub fn getRelocs(self: *Object, shndx: u32) error{Overflow}![]align(1) const elf.Elf64_Rela { |
| 682 | const raw = self.shdrContents(shndx); | 685 | const raw = try self.shdrContents(shndx); |
| 683 | const num = @divExact(raw.len, @sizeOf(elf.Elf64_Rela)); | 686 | const num = @divExact(raw.len, @sizeOf(elf.Elf64_Rela)); |
| 684 | return @as([*]align(1) const elf.Elf64_Rela, @ptrCast(raw.ptr))[0..num]; | 687 | return @as([*]align(1) const elf.Elf64_Rela, @ptrCast(raw.ptr))[0..num]; |
| 685 | } | 688 | } |
| ... | @@ -819,7 +822,8 @@ fn formatComdatGroups( | ... | @@ -819,7 +822,8 @@ fn formatComdatGroups( |
| 819 | const cg = elf_file.comdatGroup(cg_index); | 822 | const cg = elf_file.comdatGroup(cg_index); |
| 820 | const cg_owner = elf_file.comdatGroupOwner(cg.owner); | 823 | const cg_owner = elf_file.comdatGroupOwner(cg.owner); |
| 821 | if (cg_owner.file != object.index) continue; | 824 | 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| { | ||
| 823 | const atom_index = object.atoms.items[shndx]; | 827 | const atom_index = object.atoms.items[shndx]; |
| 824 | const atom = elf_file.atom(atom_index) orelse continue; | 828 | const atom = elf_file.atom(atom_index) orelse continue; |
| 825 | try writer.print(" atom({d}) : {s}\n", .{ atom_index, atom.name(elf_file) }); | 829 | 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 { | ... | @@ -20,9 +20,9 @@ pub const Fde = struct { |
| 20 | return base + fde.out_offset; | 20 | return base + fde.out_offset; |
| 21 | } | 21 | } |
| 22 | 22 | ||
| 23 | pub fn data(fde: Fde, elf_file: *Elf) []const u8 { | 23 | pub fn data(fde: Fde, elf_file: *Elf) error{Overflow}![]const u8 { |
| 24 | const object = elf_file.file(fde.file_index).?.object; | 24 | 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); |
| 26 | return contents[fde.offset..][0..fde.calcSize()]; | 26 | return contents[fde.offset..][0..fde.calcSize()]; |
| 27 | } | 27 | } |
| 28 | 28 | ||
| ... | @@ -39,17 +39,17 @@ pub const Fde = struct { | ... | @@ -39,17 +39,17 @@ pub const Fde = struct { |
| 39 | return fde.size + 4; | 39 | return fde.size + 4; |
| 40 | } | 40 | } |
| 41 | 41 | ||
| 42 | pub fn atom(fde: Fde, elf_file: *Elf) *Atom { | 42 | pub fn atom(fde: Fde, elf_file: *Elf) error{Overflow}!*Atom { |
| 43 | const object = elf_file.file(fde.file_index).?.object; | 43 | 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]; |
| 45 | const sym = object.symtab[rel.r_sym()]; | 45 | const sym = object.symtab[rel.r_sym()]; |
| 46 | const atom_index = object.atoms.items[sym.st_shndx]; | 46 | const atom_index = object.atoms.items[sym.st_shndx]; |
| 47 | return elf_file.atom(atom_index).?; | 47 | return elf_file.atom(atom_index).?; |
| 48 | } | 48 | } |
| 49 | 49 | ||
| 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 { |
| 51 | const object = elf_file.file(fde.file_index).?.object; | 51 | 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]; |
| 53 | } | 53 | } |
| 54 | 54 | ||
| 55 | pub fn format( | 55 | pub fn format( |
| ... | @@ -88,11 +88,15 @@ pub const Fde = struct { | ... | @@ -88,11 +88,15 @@ pub const Fde = struct { |
| 88 | const fde = ctx.fde; | 88 | const fde = ctx.fde; |
| 89 | const elf_file = ctx.elf_file; | 89 | const elf_file = ctx.elf_file; |
| 90 | const base_addr = fde.address(elf_file); | 90 | 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 | ""; | ||
| 91 | try writer.print("@{x} : size({x}) : cie({d}) : {s}", .{ | 95 | try writer.print("@{x} : size({x}) : cie({d}) : {s}", .{ |
| 92 | base_addr + fde.out_offset, | 96 | base_addr + fde.out_offset, |
| 93 | fde.calcSize(), | 97 | fde.calcSize(), |
| 94 | fde.cie_index, | 98 | fde.cie_index, |
| 95 | fde.atom(elf_file).name(elf_file), | 99 | atom_name, |
| 96 | }); | 100 | }); |
| 97 | if (!fde.alive) try writer.writeAll(" : [*]"); | 101 | if (!fde.alive) try writer.writeAll(" : [*]"); |
| 98 | } | 102 | } |
| ... | @@ -119,9 +123,9 @@ pub const Cie = struct { | ... | @@ -119,9 +123,9 @@ pub const Cie = struct { |
| 119 | return base + cie.out_offset; | 123 | return base + cie.out_offset; |
| 120 | } | 124 | } |
| 121 | 125 | ||
| 122 | pub fn data(cie: Cie, elf_file: *Elf) []const u8 { | 126 | pub fn data(cie: Cie, elf_file: *Elf) error{Overflow}![]const u8 { |
| 123 | const object = elf_file.file(cie.file_index).?.object; | 127 | 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); |
| 125 | return contents[cie.offset..][0..cie.calcSize()]; | 129 | return contents[cie.offset..][0..cie.calcSize()]; |
| 126 | } | 130 | } |
| 127 | 131 | ||
| ... | @@ -129,16 +133,16 @@ pub const Cie = struct { | ... | @@ -129,16 +133,16 @@ pub const Cie = struct { |
| 129 | return cie.size + 4; | 133 | return cie.size + 4; |
| 130 | } | 134 | } |
| 131 | 135 | ||
| 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 { |
| 133 | const object = elf_file.file(cie.file_index).?.object; | 137 | 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]; |
| 135 | } | 139 | } |
| 136 | 140 | ||
| 137 | pub fn eql(cie: Cie, other: Cie, elf_file: *Elf) bool { | 141 | pub fn eql(cie: Cie, other: Cie, elf_file: *Elf) error{Overflow}!bool { |
| 138 | if (!std.mem.eql(u8, cie.data(elf_file), other.data(elf_file))) return false; | 142 | if (!std.mem.eql(u8, try cie.data(elf_file), try other.data(elf_file))) return false; |
| 139 | 143 | ||
| 140 | const cie_relocs = cie.relocs(elf_file); | 144 | const cie_relocs = try cie.relocs(elf_file); |
| 141 | const other_relocs = other.relocs(elf_file); | 145 | const other_relocs = try other.relocs(elf_file); |
| 142 | if (cie_relocs.len != other_relocs.len) return false; | 146 | if (cie_relocs.len != other_relocs.len) return false; |
| 143 | 147 | ||
| 144 | for (cie_relocs, other_relocs) |cie_rel, other_rel| { | 148 | for (cie_relocs, other_relocs) |cie_rel, other_rel| { |
| ... | @@ -315,10 +319,10 @@ pub fn writeEhFrame(elf_file: *Elf, writer: anytype) !void { | ... | @@ -315,10 +319,10 @@ pub fn writeEhFrame(elf_file: *Elf, writer: anytype) !void { |
| 315 | for (object.cies.items) |cie| { | 319 | for (object.cies.items) |cie| { |
| 316 | if (!cie.alive) continue; | 320 | if (!cie.alive) continue; |
| 317 | 321 | ||
| 318 | const contents = try gpa.dupe(u8, cie.data(elf_file)); | 322 | const contents = try gpa.dupe(u8, try cie.data(elf_file)); |
| 319 | defer gpa.free(contents); | 323 | defer gpa.free(contents); |
| 320 | 324 | ||
| 321 | for (cie.relocs(elf_file)) |rel| { | 325 | for (try cie.relocs(elf_file)) |rel| { |
| 322 | const sym = object.symbol(rel.r_sym(), elf_file); | 326 | const sym = object.symbol(rel.r_sym(), elf_file); |
| 323 | try resolveReloc(cie, sym, rel, elf_file, contents); | 327 | try resolveReloc(cie, sym, rel, elf_file, contents); |
| 324 | } | 328 | } |
| ... | @@ -333,7 +337,7 @@ pub fn writeEhFrame(elf_file: *Elf, writer: anytype) !void { | ... | @@ -333,7 +337,7 @@ pub fn writeEhFrame(elf_file: *Elf, writer: anytype) !void { |
| 333 | for (object.fdes.items) |fde| { | 337 | for (object.fdes.items) |fde| { |
| 334 | if (!fde.alive) continue; | 338 | if (!fde.alive) continue; |
| 335 | 339 | ||
| 336 | const contents = try gpa.dupe(u8, fde.data(elf_file)); | 340 | const contents = try gpa.dupe(u8, try fde.data(elf_file)); |
| 337 | defer gpa.free(contents); | 341 | defer gpa.free(contents); |
| 338 | 342 | ||
| 339 | std.mem.writeIntLittle( | 343 | std.mem.writeIntLittle( |
| ... | @@ -342,7 +346,7 @@ pub fn writeEhFrame(elf_file: *Elf, writer: anytype) !void { | ... | @@ -342,7 +346,7 @@ pub fn writeEhFrame(elf_file: *Elf, writer: anytype) !void { |
| 342 | @as(i32, @truncate(@as(i64, @intCast(fde.out_offset + 4)) - @as(i64, @intCast(fde.cie(elf_file).out_offset)))), | 346 | @as(i32, @truncate(@as(i64, @intCast(fde.out_offset + 4)) - @as(i64, @intCast(fde.cie(elf_file).out_offset)))), |
| 343 | ); | 347 | ); |
| 344 | 348 | ||
| 345 | for (fde.relocs(elf_file)) |rel| { | 349 | for (try fde.relocs(elf_file)) |rel| { |
| 346 | const sym = object.symbol(rel.r_sym(), elf_file); | 350 | const sym = object.symbol(rel.r_sym(), elf_file); |
| 347 | try resolveReloc(fde, sym, rel, elf_file, contents); | 351 | try resolveReloc(fde, sym, rel, elf_file, contents); |
| 348 | } | 352 | } |
| ... | @@ -391,7 +395,7 @@ pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void { | ... | @@ -391,7 +395,7 @@ pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void { |
| 391 | for (object.fdes.items) |fde| { | 395 | for (object.fdes.items) |fde| { |
| 392 | if (!fde.alive) continue; | 396 | if (!fde.alive) continue; |
| 393 | 397 | ||
| 394 | const relocs = fde.relocs(elf_file); | 398 | const relocs = try fde.relocs(elf_file); |
| 395 | assert(relocs.len > 0); // Should this be an error? Things are completely broken anyhow if this trips... | 399 | assert(relocs.len > 0); // Should this be an error? Things are completely broken anyhow if this trips... |
| 396 | const rel = relocs[0]; | 400 | const rel = relocs[0]; |
| 397 | const sym = object.symbol(rel.r_sym(), elf_file); | 401 | const sym = object.symbol(rel.r_sym(), elf_file); |