authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-03-12 13:25:29+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-03-12 13:25:29+01:00
log55c085b893892f89e7d9930f3ec6c22537ebb54f
tree11a7c25fa278531bc5c557f7eeda56901b670f1e
parentfaa4bdb0175ee142834be320326063ae99dac1e4

elf: re-use output buffer for emitting thunks


1 files changed, 25 insertions(+), 15 deletions(-)

src/link/Elf.zig+25-15
...@@ -4565,14 +4565,20 @@ fn writeAtoms(self: *Elf) !void {...@@ -4565,14 +4565,20 @@ fn writeAtoms(self: *Elf) !void {
4565 try self.base.file.?.pwriteAll(buffer, sh_offset);4565 try self.base.file.?.pwriteAll(buffer, sh_offset);
4566 }4566 }
45674567
4568 for (self.thunks.items) |th| {4568 if (self.requiresThunks()) {
4569 const shdr = self.shdrs.items[th.output_section_index];4569 var buffer = std.ArrayList(u8).init(gpa);
4570 const offset = th.value + shdr.sh_offset;4570 defer buffer.deinit();
4571 const buffer = try gpa.alloc(u8, th.size(self));4571
4572 defer gpa.free(buffer);4572 for (self.thunks.items) |th| {
4573 var stream = std.io.fixedBufferStream(buffer);4573 const thunk_size = th.size(self);
4574 try th.write(self, stream.writer());4574 try buffer.ensureUnusedCapacity(thunk_size);
4575 try self.base.file.?.pwriteAll(buffer, offset);4575 const shdr = self.shdrs.items[th.output_section_index];
4576 const offset = th.value + shdr.sh_offset;
4577 try th.write(self, buffer.writer());
4578 assert(buffer.items.len == thunk_size);
4579 try self.base.file.?.pwriteAll(buffer.items, offset);
4580 buffer.clearRetainingCapacity();
4581 }
4576 }4582 }
45774583
4578 try self.reportUndefinedSymbols(&undefs);4584 try self.reportUndefinedSymbols(&undefs);
...@@ -4603,12 +4609,12 @@ pub fn updateSymtabSize(self: *Elf) !void {...@@ -4603,12 +4609,12 @@ pub fn updateSymtabSize(self: *Elf) !void {
4603 nlocals += 1;4609 nlocals += 1;
4604 }4610 }
46054611
4606 for (self.thunks.items) |*th| {4612 if (self.requiresThunks()) for (self.thunks.items) |*th| {
4607 th.output_symtab_ctx.ilocal = nlocals + 1;4613 th.output_symtab_ctx.ilocal = nlocals + 1;
4608 th.calcSymtabSize(self);4614 th.calcSymtabSize(self);
4609 nlocals += th.output_symtab_ctx.nlocals;4615 nlocals += th.output_symtab_ctx.nlocals;
4610 strsize += th.output_symtab_ctx.strsize;4616 strsize += th.output_symtab_ctx.strsize;
4611 }4617 };
46124618
4613 for (files.items) |index| {4619 for (files.items) |index| {
4614 const file_ptr = self.file(index).?;4620 const file_ptr = self.file(index).?;
...@@ -4840,9 +4846,9 @@ pub fn writeSymtab(self: *Elf) !void {...@@ -4840,9 +4846,9 @@ pub fn writeSymtab(self: *Elf) !void {
48404846
4841 self.writeSectionSymbols();4847 self.writeSectionSymbols();
48424848
4843 for (self.thunks.items) |th| {4849 if (self.requiresThunks()) for (self.thunks.items) |th| {
4844 th.writeSymtab(self);4850 th.writeSymtab(self);
4845 }4851 };
48464852
4847 if (self.zigObjectPtr()) |zig_object| {4853 if (self.zigObjectPtr()) |zig_object| {
4848 zig_object.asFile().writeSymtab(self);4854 zig_object.asFile().writeSymtab(self);
...@@ -6007,10 +6013,14 @@ fn fmtDumpState(...@@ -6007,10 +6013,14 @@ fn fmtDumpState(
6007 try writer.print("linker_defined({d}) : (linker defined)\n", .{index});6013 try writer.print("linker_defined({d}) : (linker defined)\n", .{index});
6008 try writer.print("{}\n", .{linker_defined.fmtSymtab(self)});6014 try writer.print("{}\n", .{linker_defined.fmtSymtab(self)});
6009 }6015 }
6010 try writer.writeAll("thunks\n");6016
6011 for (self.thunks.items, 0..) |th, index| {6017 if (self.requiresThunks()) {
6012 try writer.print("thunk({d}) : {}\n", .{ index, th.fmt(self) });6018 try writer.writeAll("thunks\n");
6019 for (self.thunks.items, 0..) |th, index| {
6020 try writer.print("thunk({d}) : {}\n", .{ index, th.fmt(self) });
6021 }
6013 }6022 }
6023
6014 try writer.print("{}\n", .{self.zig_got.fmt(self)});6024 try writer.print("{}\n", .{self.zig_got.fmt(self)});
6015 try writer.print("{}\n", .{self.got.fmt(self)});6025 try writer.print("{}\n", .{self.got.fmt(self)});
6016 try writer.print("{}\n", .{self.plt.fmt(self)});6026 try writer.print("{}\n", .{self.plt.fmt(self)});