From 3e22077d46a02d1c4e02dd603b560978dc184a87 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Mon, 23 Nov 2020 14:58:08 +0100 Subject: [PATCH] Fix the ELF base calculation Find the effective ELF load address in dl_iterate_phdr by computing the difference between the in-memory phdr and its p_vaddr specified in the ELF file. This makes the dl_iterate_phdr test pass and restores the stack traces. --- lib/std/os.zig | 19 +++++++++++++++---- lib/std/os/test.zig | 2 -- lib/std/process.zig | 2 -- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/std/os.zig b/lib/std/os.zig index 2a6c8b2a5..e7c618431 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -4340,7 +4340,7 @@ pub fn dl_iterate_phdr( const elf_base = std.process.getBaseAddress(); const ehdr = @intToPtr(*elf.Ehdr, elf_base); - // Make sure the base address points to an ELF image + // Make sure the base address points to an ELF image. assert(mem.eql(u8, ehdr.e_ident[0..4], "\x7fELF")); const n_phdr = ehdr.e_phnum; const phdrs = (@intToPtr([*]elf.Phdr, elf_base + ehdr.e_phoff))[0..n_phdr]; @@ -4348,10 +4348,21 @@ pub fn dl_iterate_phdr( var it = dl.linkmap_iterator(phdrs) catch unreachable; // The executable has no dynamic link segment, create a single entry for - // the whole ELF image + // the whole ELF image. if (it.end()) { + // Find the base address for the ELF image, if this is a PIE the value + // is non-zero. + const base_address = for (phdrs) |*phdr| { + if (phdr.p_type == elf.PT_PHDR) { + break @ptrToInt(phdrs.ptr) - phdr.p_vaddr; + // We could try computing the difference between _DYNAMIC and + // the p_vaddr of the PT_DYNAMIC section, but using the phdr is + // good enough (Is it?). + } + } else unreachable; + var info = dl_phdr_info{ - .dlpi_addr = 0, + .dlpi_addr = base_address, .dlpi_name = "/proc/self/exe", .dlpi_phdr = phdrs.ptr, .dlpi_phnum = ehdr.e_phnum, @@ -4360,7 +4371,7 @@ pub fn dl_iterate_phdr( return callback(&info, @sizeOf(dl_phdr_info), context); } - // Last return value from the callback function + // Last return value from the callback function. while (it.next()) |entry| { var dlpi_phdr: [*]elf.Phdr = undefined; var dlpi_phnum: u16 = undefined; diff --git a/lib/std/os/test.zig b/lib/std/os/test.zig index 5a6f3bddf..a7123f9d8 100644 --- a/lib/std/os/test.zig +++ b/lib/std/os/test.zig @@ -386,8 +386,6 @@ fn iter_fn(info: *dl_phdr_info, size: usize, counter: *usize) IterFnError!void { test "dl_iterate_phdr" { if (builtin.os.tag == .windows or builtin.os.tag == .wasi or builtin.os.tag == .macos) return error.SkipZigTest; - if (builtin.position_independent_executable) - return error.SkipZigTest; var counter: usize = 0; try os.dl_iterate_phdr(&counter, IterFnError, iter_fn); diff --git a/lib/std/process.zig b/lib/std/process.zig index 5fc262e8b..b083126b3 100644 --- a/lib/std/process.zig +++ b/lib/std/process.zig @@ -686,8 +686,6 @@ pub fn getBaseAddress() usize { if (base != 0) { return base; } - // XXX: Wrong for PIE executables, it should look at the difference - // between _DYNAMIC and the PT_DYNAMIC phdr instead. const phdr = os.system.getauxval(std.elf.AT_PHDR); return phdr - @sizeOf(std.elf.Ehdr); },