authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-10-02 13:32:13+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-10-09 12:38:53-07:00
log0e5cd112ef6e50cbebd7a65a048f541023dcb49c
tree769a3cbd9294babbe0267bb645d9900e790574f6
parentcf2e462d91cf1e07f5aedbae404cce311de6b664

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.

1 files changed, 4 insertions(+), 4 deletions(-)

src/link/Elf.zig+4-4
...@@ -987,6 +987,9 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod...@@ -987,6 +987,9 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod
987 for (self.objects.items) |index| {987 for (self.objects.items) |index| {
988 self.file(index).?.object.dirty = false;988 self.file(index).?.object.dirty = false;
989 }989 }
990 // TODO: would state tracking be more appropriate here? perhaps even custom relocation type?
991 self.rela_dyn.clearRetainingCapacity();
992 self.rela_plt.clearRetainingCapacity();
990993
991 if (self.zigObjectPtr()) |zo| {994 if (self.zigObjectPtr()) |zo| {
992 var has_reloc_errors = false;995 var has_reloc_errors = false;
...@@ -1017,6 +1020,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod...@@ -1017,6 +1020,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod
1017 try self.writeShdrTable();1020 try self.writeShdrTable();
1018 try self.writeAtoms();1021 try self.writeAtoms();
1019 try self.writeMergeSections();1022 try self.writeMergeSections();
1023
1020 self.writeSyntheticSections() catch |err| switch (err) {1024 self.writeSyntheticSections() catch |err| switch (err) {
1021 error.RelocFailure => return error.FlushFailure,1025 error.RelocFailure => return error.FlushFailure,
1022 error.UnsupportedCpuArch => {1026 error.UnsupportedCpuArch => {
...@@ -4236,8 +4240,6 @@ fn writeSyntheticSections(self: *Elf) !void {...@@ -4236,8 +4240,6 @@ fn writeSyntheticSections(self: *Elf) !void {
4236 }4240 }
42374241
4238 if (self.rela_dyn_section_index) |shndx| {4242 if (self.rela_dyn_section_index) |shndx| {
4239 // TODO: would state tracking be more appropriate here? perhaps even custom relocation type?
4240 self.rela_dyn.clearRetainingCapacity();
4241 const shdr = slice.items(.shdr)[shndx];4243 const shdr = slice.items(.shdr)[shndx];
4242 try self.got.addRela(self);4244 try self.got.addRela(self);
4243 try self.copy_rel.addRela(self);4245 try self.copy_rel.addRela(self);
...@@ -4270,8 +4272,6 @@ fn writeSyntheticSections(self: *Elf) !void {...@@ -4270,8 +4272,6 @@ fn writeSyntheticSections(self: *Elf) !void {
4270 }4272 }
42714273
4272 if (self.rela_plt_section_index) |shndx| {4274 if (self.rela_plt_section_index) |shndx| {
4273 // TODO: would state tracking be more appropriate here? perhaps even custom relocation type?
4274 self.rela_plt.clearRetainingCapacity();
4275 const shdr = slice.items(.shdr)[shndx];4275 const shdr = slice.items(.shdr)[shndx];
4276 try self.plt.addRela(self);4276 try self.plt.addRela(self);
4277 try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.rela_plt.items), shdr.sh_offset);4277 try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.rela_plt.items), shdr.sh_offset);