authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-28 14:06:12+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-28 14:06:12+02:00
logaf00ac53b50ca9dc606560f106ef17c488e71249
tree9dd7a1745d4aacede1f97bea02b6fe573a1c079f
parenta63ce5a37c9655f4f453d848285b85fb91625390

elf: report fatal linker error for unhandled relocation types


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

src/link/Elf.zig+1-1
...@@ -4098,7 +4098,7 @@ const ErrorWithNotes = struct {...@@ -4098,7 +4098,7 @@ const ErrorWithNotes = struct {
4098 }4098 }
4099};4099};
41004100
4101fn addErrorWithNotes(self: *Elf, note_count: usize) error{OutOfMemory}!ErrorWithNotes {4101pub fn addErrorWithNotes(self: *Elf, note_count: usize) error{OutOfMemory}!ErrorWithNotes {
4102 try self.misc_errors.ensureUnusedCapacity(self.base.allocator, 1);4102 try self.misc_errors.ensureUnusedCapacity(self.base.allocator, 1);
4103 return self.addErrorWithNotesAssumeCapacity(note_count);4103 return self.addErrorWithNotesAssumeCapacity(note_count);
4104}4104}
src/link/Elf/Atom.zig+12-5
...@@ -388,7 +388,17 @@ pub fn scanRelocs(self: Atom, elf_file: *Elf, undefs: anytype) !void {...@@ -388,7 +388,17 @@ pub fn scanRelocs(self: Atom, elf_file: *Elf, undefs: anytype) !void {
388388
389 elf.R_X86_64_PC32 => {},389 elf.R_X86_64_PC32 => {},
390390
391 else => @panic("TODO"),391 else => {
392 var err = try elf_file.addErrorWithNotes(1);
393 try err.addMsg(elf_file, "fatal linker error: unhandled relocation type {}", .{
394 fmtRelocType(rel.r_type()),
395 });
396 try err.addNote(elf_file, "in {}:{s} at offset 0x{x}", .{
397 self.file(elf_file).?.fmtPath(),
398 self.name(elf_file),
399 rel.r_offset,
400 });
401 },
392 }402 }
393 }403 }
394}404}
...@@ -512,10 +522,7 @@ pub fn resolveRelocs(self: Atom, elf_file: *Elf, code: []u8) !void {...@@ -512,10 +522,7 @@ pub fn resolveRelocs(self: Atom, elf_file: *Elf, code: []u8) !void {
512 try cwriter.writeIntLittle(i32, @as(i32, @intCast(G + GOT + A - P)));522 try cwriter.writeIntLittle(i32, @as(i32, @intCast(G + GOT + A - P)));
513 },523 },
514524
515 else => {525 else => {},
516 log.err("TODO: unhandled relocation type {}", .{fmtRelocType(rel.r_type())});
517 @panic("TODO unhandled relocation type");
518 },
519 }526 }
520 }527 }
521}528}
src/link/Elf/Object.zig+2-3
...@@ -233,8 +233,7 @@ fn getOutputSectionIndex(self: *Object, elf_file: *Elf, shdr: elf.Elf64_Shdr) er...@@ -233,8 +233,7 @@ fn getOutputSectionIndex(self: *Object, elf_file: *Elf, shdr: elf.Elf64_Shdr) er
233 const is_alloc = flags & elf.SHF_ALLOC != 0;233 const is_alloc = flags & elf.SHF_ALLOC != 0;
234 const is_write = flags & elf.SHF_WRITE != 0;234 const is_write = flags & elf.SHF_WRITE != 0;
235 const is_exec = flags & elf.SHF_EXECINSTR != 0;235 const is_exec = flags & elf.SHF_EXECINSTR != 0;
236 const is_tls = flags & elf.SHF_TLS != 0;236 if (!is_alloc) {
237 if (!is_alloc or is_tls) {
238 log.err("{}: output section {s} not found", .{ self.fmtPath(), name });237 log.err("{}: output section {s} not found", .{ self.fmtPath(), name });
239 @panic("TODO: missing output section!");238 @panic("TODO: missing output section!");
240 }239 }
...@@ -243,7 +242,7 @@ fn getOutputSectionIndex(self: *Object, elf_file: *Elf, shdr: elf.Elf64_Shdr) er...@@ -243,7 +242,7 @@ fn getOutputSectionIndex(self: *Object, elf_file: *Elf, shdr: elf.Elf64_Shdr) er
243 if (is_exec) phdr_flags |= elf.PF_X;242 if (is_exec) phdr_flags |= elf.PF_X;
244 const phdr_index = try elf_file.allocateSegment(.{243 const phdr_index = try elf_file.allocateSegment(.{
245 .size = Elf.padToIdeal(shdr.sh_size),244 .size = Elf.padToIdeal(shdr.sh_size),
246 .alignment = if (is_tls) shdr.sh_addralign else elf_file.page_size,245 .alignment = elf_file.page_size,
247 .flags = phdr_flags,246 .flags = phdr_flags,
248 });247 });
249 const shndx = try elf_file.allocateAllocSection(.{248 const shndx = try elf_file.allocateAllocSection(.{