Merge pull request #7204 from LemonBoy/piecrash
Fix logic for detecting _DYNAMIC symbol
This commit is contained in:
commit
9d2fe1682f
@ -59,24 +59,12 @@ const RDebug = extern struct {
|
||||
r_ldbase: usize,
|
||||
};
|
||||
|
||||
// TODO: This should be weak (#1917)
|
||||
extern var _DYNAMIC: [128]elf.Dyn;
|
||||
|
||||
comptime {
|
||||
if (std.Target.current.os.tag == .linux) {
|
||||
asm (
|
||||
\\ .weak _DYNAMIC
|
||||
\\ .hidden _DYNAMIC
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn linkmap_iterator(phdrs: []elf.Phdr) !LinkMap.Iterator {
|
||||
if (@ptrToInt(&_DYNAMIC[0]) == 0) {
|
||||
const _DYNAMIC = @extern([*]elf.Dyn, .{ .name = "_DYNAMIC", .linkage = .Weak }) orelse {
|
||||
// No PT_DYNAMIC means this is either a statically-linked program or a
|
||||
// badly corrupted one
|
||||
// badly corrupted dynamically-linked one.
|
||||
return LinkMap.Iterator{ .current = null };
|
||||
}
|
||||
};
|
||||
|
||||
const link_map_ptr = init: {
|
||||
var i: usize = 0;
|
||||
|
@ -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;
|
||||
|
@ -56,12 +56,13 @@ fn getDynamicSymbol() [*]elf.Dyn {
|
||||
: [ret] "=r" (-> usize)
|
||||
),
|
||||
.riscv64 => asm volatile (
|
||||
\\ .weak _DYNAMIC
|
||||
\\ .hidden _DYNAMIC
|
||||
\\ lla %[ret], _DYNAMIC
|
||||
: [ret] "=r" (-> usize)
|
||||
),
|
||||
else => @compileError("???"),
|
||||
};
|
||||
if (addr == 0) unreachable;
|
||||
return @intToPtr([*]elf.Dyn, addr);
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -282,7 +282,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
|
||||
\\source.zig:10:8: [address] in main (test)
|
||||
\\ foo();
|
||||
\\ ^
|
||||
\\start.zig:331:29: [address] in std.start.posixCallMainAndExit (test)
|
||||
\\start.zig:337:29: [address] in std.start.posixCallMainAndExit (test)
|
||||
\\ return root.main();
|
||||
\\ ^
|
||||
\\start.zig:162:5: [address] in std.start._start (test)
|
||||
|
Loading…
x
Reference in New Issue
Block a user