From 0e5cd112ef6e50cbebd7a65a048f541023dcb49c Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Wed, 2 Oct 2024 13:32:13 +0200 Subject: [PATCH] elf: clear dynamic relocs before resolving relocs in atoms When resolving and writing atoms to file, we may add dynamic relocs to the output buffer so clear the buffers before that happens. --- src/link/Elf.zig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/link/Elf.zig b/src/link/Elf.zig index 51987eb9cc..14c532640f 100644 --- a/src/link/Elf.zig +++ b/src/link/Elf.zig @@ -987,6 +987,9 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod for (self.objects.items) |index| { self.file(index).?.object.dirty = false; } + // TODO: would state tracking be more appropriate here? perhaps even custom relocation type? + self.rela_dyn.clearRetainingCapacity(); + self.rela_plt.clearRetainingCapacity(); if (self.zigObjectPtr()) |zo| { var has_reloc_errors = false; @@ -1017,6 +1020,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod try self.writeShdrTable(); try self.writeAtoms(); try self.writeMergeSections(); + self.writeSyntheticSections() catch |err| switch (err) { error.RelocFailure => return error.FlushFailure, error.UnsupportedCpuArch => { @@ -4236,8 +4240,6 @@ fn writeSyntheticSections(self: *Elf) !void { } if (self.rela_dyn_section_index) |shndx| { - // TODO: would state tracking be more appropriate here? perhaps even custom relocation type? - self.rela_dyn.clearRetainingCapacity(); const shdr = slice.items(.shdr)[shndx]; try self.got.addRela(self); try self.copy_rel.addRela(self); @@ -4270,8 +4272,6 @@ fn writeSyntheticSections(self: *Elf) !void { } if (self.rela_plt_section_index) |shndx| { - // TODO: would state tracking be more appropriate here? perhaps even custom relocation type? - self.rela_plt.clearRetainingCapacity(); const shdr = slice.items(.shdr)[shndx]; try self.plt.addRela(self); try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.rela_plt.items), shdr.sh_offset);