Correct dl_iterate_phdr address

The base should be zero so that p_vaddr + dlpi_addr = p_vaddr
This commit is contained in:
LemonBoy 2020-01-29 00:08:01 +01:00 committed by Andrew Kelley
parent 3ec37c979e
commit 1ba4554855
3 changed files with 3 additions and 59 deletions

View File

@ -2697,7 +2697,7 @@ pub fn dl_iterate_phdr(
// the whole ELF image
if (it.end()) {
var info = dl_phdr_info{
.dlpi_addr = elf_base,
.dlpi_addr = 0,
.dlpi_name = "/proc/self/exe",
.dlpi_phdr = phdrs.ptr,
.dlpi_phnum = ehdr.e_phnum,

View File

@ -1041,63 +1041,6 @@ pub fn uname(uts: *utsname) usize {
return syscall1(SYS_uname, @ptrToInt(uts));
}
// XXX: This should be weak
extern const __ehdr_start: elf.Ehdr;
pub fn dl_iterate_phdr(comptime T: type, callback: extern fn (info: *dl_phdr_info, size: usize, data: ?*T) i32, data: ?*T) isize {
if (builtin.link_libc) {
return std.c.dl_iterate_phdr(@ptrCast(std.c.dl_iterate_phdr_callback, callback), @ptrCast(?*c_void, data));
}
const elf_base = @ptrToInt(&__ehdr_start);
const n_phdr = __ehdr_start.e_phnum;
const phdrs = (@intToPtr([*]elf.Phdr, elf_base + __ehdr_start.e_phoff))[0..n_phdr];
var it = dl.linkmap_iterator(phdrs) catch return 0;
// The executable has no dynamic link segment, create a single entry for
// the whole ELF image
if (it.end()) {
var info = dl_phdr_info{
.dlpi_addr = elf_base,
.dlpi_name = "/proc/self/exe",
.dlpi_phdr = @intToPtr([*]elf.Phdr, elf_base + __ehdr_start.e_phoff),
.dlpi_phnum = __ehdr_start.e_phnum,
};
return callback(&info, @sizeOf(dl_phdr_info), data);
}
// Last return value from the callback function
var last_r: isize = 0;
while (it.next()) |entry| {
var dlpi_phdr: usize = undefined;
var dlpi_phnum: u16 = undefined;
if (entry.l_addr != 0) {
const elf_header = @intToPtr(*elf.Ehdr, entry.l_addr);
dlpi_phdr = entry.l_addr + elf_header.e_phoff;
dlpi_phnum = elf_header.e_phnum;
} else {
// This is the running ELF image
dlpi_phdr = elf_base + __ehdr_start.e_phoff;
dlpi_phnum = __ehdr_start.e_phnum;
}
var info = dl_phdr_info{
.dlpi_addr = entry.l_addr,
.dlpi_name = entry.l_name,
.dlpi_phdr = @intToPtr([*]elf.Phdr, dlpi_phdr),
.dlpi_phnum = dlpi_phnum,
};
last_r = callback(&info, @sizeOf(dl_phdr_info), data);
if (last_r != 0) break;
}
return last_r;
}
pub fn io_uring_setup(entries: u32, p: *io_uring_params) usize {
return syscall2(SYS_io_uring_setup, entries, @ptrToInt(p));
}

View File

@ -186,8 +186,9 @@ fn iter_fn(info: *dl_phdr_info, size: usize, data: ?*usize) callconv(.C) i32 {
if (phdr.p_type != elf.PT_LOAD) continue;
const reloc_addr = info.dlpi_addr + phdr.p_vaddr;
// Find the ELF header
const elf_header = @intToPtr(*elf.Ehdr, phdr.p_vaddr - phdr.p_offset);
const elf_header = @intToPtr(*elf.Ehdr, reloc_addr - phdr.p_offset);
// Validate the magic
if (!mem.eql(u8, elf_header.e_ident[0..4], "\x7fELF")) return -1;
// Consistency check