From ec337051a92b6f65bb29ddf41a290ea8e46b37b1 Mon Sep 17 00:00:00 2001 From: Shane Peelar Date: Thu, 6 Jun 2024 20:04:16 -0400 Subject: [PATCH] Fix slight deviation from spec in handling Elf*_Rela relative relocations `Elf*_Rela` relocations store their argument in `r_addend`, including for `R_*_RELATIVE` relocations. Unlike `Elf*_Rel` relocations, they are not applied as a delta to the destination virtual address. Instead, they are computed from `base_address + r_addend` directly. --- lib/std/os/linux/start_pie.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/os/linux/start_pie.zig b/lib/std/os/linux/start_pie.zig index b576134042..df909a6230 100644 --- a/lib/std/os/linux/start_pie.zig +++ b/lib/std/os/linux/start_pie.zig @@ -113,7 +113,7 @@ pub fn relocate(phdrs: []elf.Phdr) void { const rela = std.mem.bytesAsSlice(elf.Rela, @as([*]u8, @ptrFromInt(rela_addr))[0..rela_size]); for (rela) |r| { if (r.r_type() != R_RELATIVE) continue; - @as(*usize, @ptrFromInt(base_addr + r.r_offset)).* += base_addr + @as(usize, @bitCast(r.r_addend)); + @as(*usize, @ptrFromInt(base_addr + r.r_offset)).* = base_addr + @as(usize, @bitCast(r.r_addend)); } } }