authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-11-11 07:31:40+01:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-11-12 18:12:40-05:00
logaa0fbbcb3917ac988757263e064540e1be3402eb
tree6757624b997cbdfcb7e6a143bcdd71895c70c3bc
parentf34247c4bc3a400d23b99c8921c9b358bf5a3250

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


2 files changed, 9 insertions(+), 3 deletions(-)

src/link/Elf.zig+5-2
...@@ -4783,7 +4783,6 @@ fn writeAtomsObject(self: *Elf) !void {...@@ -4783,7 +4783,6 @@ fn writeAtomsObject(self: *Elf) !void {
4783 const atom_ptr = self.atom(atom_index).?;4783 const atom_ptr = self.atom(atom_index).?;
4784 assert(atom_ptr.flags.alive);4784 assert(atom_ptr.flags.alive);
47854785
4786 const object = atom_ptr.file(self).?.object;
4787 const offset = math.cast(usize, atom_ptr.value - shdr.sh_addr - base_offset) orelse4786 const offset = math.cast(usize, atom_ptr.value - shdr.sh_addr - base_offset) orelse
4788 return error.Overflow;4787 return error.Overflow;
4789 const size = math.cast(usize, atom_ptr.size) orelse return error.Overflow;4788 const size = math.cast(usize, atom_ptr.size) orelse return error.Overflow;
...@@ -4796,7 +4795,11 @@ fn writeAtomsObject(self: *Elf) !void {...@@ -4796,7 +4795,11 @@ fn writeAtomsObject(self: *Elf) !void {
47964795
4797 // TODO decompress directly into provided buffer4796 // TODO decompress directly into provided buffer
4798 const out_code = buffer[offset..][0..size];4797 const out_code = buffer[offset..][0..size];
4799 const in_code = try object.codeDecompressAlloc(self, atom_index);4798 const in_code = switch (atom_ptr.file(self).?) {
4799 .object => |x| try x.codeDecompressAlloc(self, atom_index),
4800 .zig_object => |x| try x.codeAlloc(self, atom_index),
4801 else => unreachable,
4802 };
4800 defer gpa.free(in_code);4803 defer gpa.free(in_code);
4801 @memcpy(out_code, in_code);4804 @memcpy(out_code, in_code);
4802 }4805 }
src/link/Elf/ZigObject.zig+4-1
...@@ -286,6 +286,7 @@ pub fn addAtom(self: *ZigObject, elf_file: *Elf) !Symbol.Index {...@@ -286,6 +286,7 @@ pub fn addAtom(self: *ZigObject, elf_file: *Elf) !Symbol.Index {
286 self.local_esyms.items(.elf_sym)[esym_index].st_shndx = SHN_ATOM;286 self.local_esyms.items(.elf_sym)[esym_index].st_shndx = SHN_ATOM;
287 symbol_ptr.esym_index = esym_index;287 symbol_ptr.esym_index = esym_index;
288288
289 // TODO I'm thinking that maybe we shouldn' set this value unless it's actually needed?
289 const relocs_index = @as(u32, @intCast(self.relocs.items.len));290 const relocs_index = @as(u32, @intCast(self.relocs.items.len));
290 const relocs = try self.relocs.addOne(gpa);291 const relocs = try self.relocs.addOne(gpa);
291 relocs.* = .{};292 relocs.* = .{};
...@@ -490,7 +491,9 @@ pub fn addAtomsToRelaSections(self: ZigObject, elf_file: *Elf) !void {...@@ -490,7 +491,9 @@ pub fn addAtomsToRelaSections(self: ZigObject, elf_file: *Elf) !void {
490 for (self.atoms.items) |atom_index| {491 for (self.atoms.items) |atom_index| {
491 const atom = elf_file.atom(atom_index) orelse continue;492 const atom = elf_file.atom(atom_index) orelse continue;
492 if (!atom.flags.alive) continue;493 if (!atom.flags.alive) continue;
493 _ = atom.relocsShndx() orelse continue;494 const rela_shndx = atom.relocsShndx() orelse continue;
495 // TODO this check will become obsolete when we rework our relocs mechanism at the ZigObject level
496 if (self.relocs.items[rela_shndx].items.len == 0) continue;
494 const out_shndx = atom.outputShndx().?;497 const out_shndx = atom.outputShndx().?;
495 const out_shdr = elf_file.shdrs.items[out_shndx];498 const out_shdr = elf_file.shdrs.items[out_shndx];
496 if (out_shdr.sh_type == elf.SHT_NOBITS) continue;499 if (out_shdr.sh_type == elf.SHT_NOBITS) continue;