zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit aa0fbbcb3917ac988757263e064540e1be3402eb (tree)
parent f34247c4bc3a400d23b99c8921c9b358bf5a3250
Author: Jakub Konka <kubkon@jakubkonka.com>
Date:   Sat, 11 Nov 2023 07:31:40 +0100

elf: check for empty relocs buffers in ZigObject before emitting section

Diffstat:
Msrc/link/Elf.zig | 7+++++--
Msrc/link/Elf/ZigObject.zig | 5++++-
2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/link/Elf.zig b/src/link/Elf.zig @@ -4783,7 +4783,6 @@ fn writeAtomsObject(self: *Elf) !void { const atom_ptr = self.atom(atom_index).?; assert(atom_ptr.flags.alive); - const object = atom_ptr.file(self).?.object; 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; @@ -4796,7 +4795,11 @@ fn writeAtomsObject(self: *Elf) !void { // TODO decompress directly into provided buffer const out_code = buffer[offset..][0..size]; - const in_code = try object.codeDecompressAlloc(self, atom_index); + const in_code = switch (atom_ptr.file(self).?) { + .object => |x| try x.codeDecompressAlloc(self, atom_index), + .zig_object => |x| try x.codeAlloc(self, atom_index), + else => unreachable, + }; defer gpa.free(in_code); @memcpy(out_code, in_code); } diff --git a/src/link/Elf/ZigObject.zig b/src/link/Elf/ZigObject.zig @@ -286,6 +286,7 @@ pub fn addAtom(self: *ZigObject, elf_file: *Elf) !Symbol.Index { self.local_esyms.items(.elf_sym)[esym_index].st_shndx = SHN_ATOM; symbol_ptr.esym_index = esym_index; + // TODO I'm thinking that maybe we shouldn' set this value unless it's actually needed? const relocs_index = @as(u32, @intCast(self.relocs.items.len)); const relocs = try self.relocs.addOne(gpa); relocs.* = .{}; @@ -490,7 +491,9 @@ pub fn addAtomsToRelaSections(self: ZigObject, elf_file: *Elf) !void { for (self.atoms.items) |atom_index| { const atom = elf_file.atom(atom_index) orelse continue; if (!atom.flags.alive) continue; - _ = atom.relocsShndx() orelse continue; + const rela_shndx = atom.relocsShndx() orelse continue; + // TODO this check will become obsolete when we rework our relocs mechanism at the ZigObject level + if (self.relocs.items[rela_shndx].items.len == 0) continue; const out_shndx = atom.outputShndx().?; const out_shdr = elf_file.shdrs.items[out_shndx]; if (out_shdr.sh_type == elf.SHT_NOBITS) continue;