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 {
40984098 }
40994099};
41004100
4101fn addErrorWithNotes(self: *Elf, note_count: usize) error{OutOfMemory}!ErrorWithNotes {
4101pub fn addErrorWithNotes(self: *Elf, note_count: usize) error{OutOfMemory}!ErrorWithNotes {
41024102 try self.misc_errors.ensureUnusedCapacity(self.base.allocator, 1);
41034103 return self.addErrorWithNotesAssumeCapacity(note_count);
41044104}
src/link/Elf/Atom.zig+12-5
......@@ -388,7 +388,17 @@ pub fn scanRelocs(self: Atom, elf_file: *Elf, undefs: anytype) !void {
388388
389389 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 },
392402 }
393403 }
394404}
......@@ -512,10 +522,7 @@ pub fn resolveRelocs(self: Atom, elf_file: *Elf, code: []u8) !void {
512522 try cwriter.writeIntLittle(i32, @as(i32, @intCast(G + GOT + A - P)));
513523 },
514524
515 else => {
516 log.err("TODO: unhandled relocation type {}", .{fmtRelocType(rel.r_type())});
517 @panic("TODO unhandled relocation type");
518 },
525 else => {},
519526 }
520527 }
521528}
src/link/Elf/Object.zig+2-3
......@@ -233,8 +233,7 @@ fn getOutputSectionIndex(self: *Object, elf_file: *Elf, shdr: elf.Elf64_Shdr) er
233233 const is_alloc = flags & elf.SHF_ALLOC != 0;
234234 const is_write = flags & elf.SHF_WRITE != 0;
235235 const is_exec = flags & elf.SHF_EXECINSTR != 0;
236 const is_tls = flags & elf.SHF_TLS != 0;
237 if (!is_alloc or is_tls) {
236 if (!is_alloc) {
238237 log.err("{}: output section {s} not found", .{ self.fmtPath(), name });
239238 @panic("TODO: missing output section!");
240239 }
......@@ -243,7 +242,7 @@ fn getOutputSectionIndex(self: *Object, elf_file: *Elf, shdr: elf.Elf64_Shdr) er
243242 if (is_exec) phdr_flags |= elf.PF_X;
244243 const phdr_index = try elf_file.allocateSegment(.{
245244 .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,
247246 .flags = phdr_flags,
248247 });
249248 const shndx = try elf_file.allocateAllocSection(.{