authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-12 19:17:57+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-12 19:17:57+02:00
logae74a36af0aa8c876b118973969aec1a10c25665
treeb9f478c3f66e11af43924f40033380cc0fa6fd0a
parent652ebf3b6a62007d37fb7fd4def393f11bb6159f

elf: resolve and write objects to file


4 files changed, 53 insertions(+), 7 deletions(-)

src/arch/x86_64/Emit.zig+1-1
......@@ -45,7 +45,7 @@ pub fn emitMir(emit: *Emit) Error!void {
4545 // Add relocation to the decl.
4646 const atom_ptr = elf_file.symbol(symbol.atom_index).atom(elf_file).?;
4747 try atom_ptr.addReloc(elf_file, .{
48 .r_offset = end_offset,
48 .r_offset = end_offset - 4,
4949 .r_info = (@as(u64, @intCast(symbol.sym_index)) << 32) | std.elf.R_X86_64_PLT32,
5050 .r_addend = -4,
5151 });
src/link/Elf.zig+28
......@@ -1072,6 +1072,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
10721072 try self.base.file.?.pwriteAll(code, file_offset);
10731073 }
10741074 }
1075 try self.writeObjects();
10751076
10761077 try self.updateSymtabSize();
10771078 try self.writeSymtab();
......@@ -1414,6 +1415,13 @@ fn allocateObjects(self: *Elf) !void {
14141415 try atom_ptr.allocate(self);
14151416 }
14161417
1418 for (object.locals()) |local_index| {
1419 const local = self.symbol(local_index);
1420 const atom_ptr = local.atom(self) orelse continue;
1421 if (!atom_ptr.alive) continue;
1422 local.value = atom_ptr.value;
1423 }
1424
14171425 for (object.globals()) |global_index| {
14181426 const global = self.symbol(global_index);
14191427 if (global.file_index == index) {
......@@ -1423,6 +1431,26 @@ fn allocateObjects(self: *Elf) !void {
14231431 }
14241432}
14251433
1434fn writeObjects(self: *Elf) !void {
1435 const gpa = self.base.allocator;
1436
1437 for (self.objects.items) |index| {
1438 const object = self.file(index).?.object;
1439 for (object.atoms.items) |atom_index| {
1440 const atom_ptr = self.atom(atom_index) orelse continue;
1441 if (!atom_ptr.alive) continue;
1442
1443 const shdr = &self.shdrs.items[atom_ptr.output_section_index];
1444 const file_offset = shdr.sh_offset + atom_ptr.value - shdr.sh_addr;
1445 const code = try atom_ptr.codeInObjectUncompressAlloc(self);
1446 defer gpa.free(code);
1447
1448 try atom_ptr.resolveRelocs(self, code);
1449 try self.base.file.?.pwriteAll(code, file_offset);
1450 }
1451 }
1452}
1453
14261454fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !void {
14271455 const tracy = trace(@src());
14281456 defer tracy.end();
src/link/Elf/Atom.zig+20-6
......@@ -67,12 +67,13 @@ pub fn codeInObjectUncompressAlloc(self: Atom, elf_file: *Elf) ![]u8 {
6767 switch (chdr.ch_type) {
6868 .ZLIB => {
6969 var stream = std.io.fixedBufferStream(data[@sizeOf(elf.Elf64_Chdr)..]);
70 var zlib_stream = try std.compress.zlib.decompressStream(gpa, stream.reader());
70 var zlib_stream = std.compress.zlib.decompressStream(gpa, stream.reader()) catch
71 return error.InputOutput;
7172 defer zlib_stream.deinit();
7273 const decomp = try gpa.alloc(u8, chdr.ch_size);
73 const nread = try zlib_stream.reader().readAll(decomp);
74 const nread = zlib_stream.reader().readAll(decomp) catch return error.InputOutput;
7475 if (nread != decomp.len) {
75 return error.Io;
76 return error.InputOutput;
7677 }
7778 return decomp;
7879 },
......@@ -366,6 +367,7 @@ pub fn scanRelocs(self: Atom, elf_file: *Elf) !void {
366367pub fn resolveRelocs(self: Atom, elf_file: *Elf, code: []u8) !void {
367368 relocs_log.debug("0x{x}: {s}", .{ self.value, self.name(elf_file) });
368369
370 const file_ptr = elf_file.file(self.file_index).?;
369371 var stream = std.io.fixedBufferStream(code);
370372 const cwriter = stream.writer();
371373
......@@ -373,7 +375,11 @@ pub fn resolveRelocs(self: Atom, elf_file: *Elf, code: []u8) !void {
373375 const r_type = rel.r_type();
374376 if (r_type == elf.R_X86_64_NONE) continue;
375377
376 const target = elf_file.symbol(rel.r_sym());
378 const target = switch (file_ptr) {
379 .zig_module => elf_file.symbol(rel.r_sym()),
380 .object => |x| elf_file.symbol(x.symbols.items[rel.r_sym()]),
381 else => unreachable,
382 };
377383
378384 // We will use equation format to resolve relocations:
379385 // https://intezer.com/blog/malware-analysis/executable-and-linkable-format-101-part-3-relocations/
......@@ -414,9 +420,17 @@ pub fn resolveRelocs(self: Atom, elf_file: *Elf, code: []u8) !void {
414420
415421 switch (rel.r_type()) {
416422 elf.R_X86_64_NONE => unreachable,
423
417424 elf.R_X86_64_64 => try cwriter.writeIntLittle(i64, S + A),
418 elf.R_X86_64_PLT32 => try cwriter.writeIntLittle(i32, @as(i32, @intCast(S + A - P))),
419 else => @panic("TODO"),
425
426 elf.R_X86_64_PLT32,
427 elf.R_X86_64_PC32,
428 => try cwriter.writeIntLittle(i32, @as(i32, @intCast(S + A - P))),
429
430 else => {
431 log.err("TODO: unhandled relocation type {}", .{fmtRelocType(rel.r_type())});
432 @panic("TODO unhandled relocation type");
433 },
420434 }
421435 }
422436}
src/link/Elf/Object.zig+4
......@@ -270,6 +270,10 @@ fn initSymtab(self: *Object, elf_file: *Elf) !void {
270270 sym_ptr.esym_index = @as(u32, @intCast(i));
271271 sym_ptr.atom_index = if (sym.st_shndx == elf.SHN_ABS) 0 else self.atoms.items[sym.st_shndx];
272272 sym_ptr.file_index = self.index;
273 sym_ptr.output_section_index = if (sym_ptr.atom(elf_file)) |atom_ptr|
274 atom_ptr.output_section_index
275 else
276 0;
273277 }
274278
275279 for (self.symtab[first_global..]) |sym| {