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 {
45654565 try self.base.file.?.pwriteAll(buffer, sh_offset);
45664566 }
45674567
4568 for (self.thunks.items) |th| {
4569 const shdr = self.shdrs.items[th.output_section_index];
4570 const offset = th.value + shdr.sh_offset;
4571 const buffer = try gpa.alloc(u8, th.size(self));
4572 defer gpa.free(buffer);
4573 var stream = std.io.fixedBufferStream(buffer);
4574 try th.write(self, stream.writer());
4575 try self.base.file.?.pwriteAll(buffer, offset);
4568 if (self.requiresThunks()) {
4569 var buffer = std.ArrayList(u8).init(gpa);
4570 defer buffer.deinit();
4571
4572 for (self.thunks.items) |th| {
4573 const thunk_size = th.size(self);
4574 try buffer.ensureUnusedCapacity(thunk_size);
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 }
45764582 }
45774583
45784584 try self.reportUndefinedSymbols(&undefs);
......@@ -4603,12 +4609,12 @@ pub fn updateSymtabSize(self: *Elf) !void {
46034609 nlocals += 1;
46044610 }
46054611
4606 for (self.thunks.items) |*th| {
4612 if (self.requiresThunks()) for (self.thunks.items) |*th| {
46074613 th.output_symtab_ctx.ilocal = nlocals + 1;
46084614 th.calcSymtabSize(self);
46094615 nlocals += th.output_symtab_ctx.nlocals;
46104616 strsize += th.output_symtab_ctx.strsize;
4611 }
4617 };
46124618
46134619 for (files.items) |index| {
46144620 const file_ptr = self.file(index).?;
......@@ -4840,9 +4846,9 @@ pub fn writeSymtab(self: *Elf) !void {
48404846
48414847 self.writeSectionSymbols();
48424848
4843 for (self.thunks.items) |th| {
4849 if (self.requiresThunks()) for (self.thunks.items) |th| {
48444850 th.writeSymtab(self);
4845 }
4851 };
48464852
48474853 if (self.zigObjectPtr()) |zig_object| {
48484854 zig_object.asFile().writeSymtab(self);
......@@ -6007,10 +6013,14 @@ fn fmtDumpState(
60076013 try writer.print("linker_defined({d}) : (linker defined)\n", .{index});
60086014 try writer.print("{}\n", .{linker_defined.fmtSymtab(self)});
60096015 }
6010 try writer.writeAll("thunks\n");
6011 for (self.thunks.items, 0..) |th, index| {
6012 try writer.print("thunk({d}) : {}\n", .{ index, th.fmt(self) });
6016
6017 if (self.requiresThunks()) {
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 }
60136022 }
6023
60146024 try writer.print("{}\n", .{self.zig_got.fmt(self)});
60156025 try writer.print("{}\n", .{self.got.fmt(self)});
60166026 try writer.print("{}\n", .{self.plt.fmt(self)});