Fix logic for detecting _DYNAMIC symbol

Prevent spurious crashes for non-PIE executables.
This commit is contained in:
LemonBoy 2020-11-23 12:00:12 +01:00
parent c7170e4a54
commit 560043dadf
2 changed files with 5 additions and 15 deletions

View File

@ -59,24 +59,12 @@ const RDebug = extern struct {
r_ldbase: usize, 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 { 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 // 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 }; return LinkMap.Iterator{ .current = null };
} };
const link_map_ptr = init: { const link_map_ptr = init: {
var i: usize = 0; var i: usize = 0;

View File

@ -686,6 +686,8 @@ pub fn getBaseAddress() usize {
if (base != 0) { if (base != 0) {
return base; 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); const phdr = os.system.getauxval(std.elf.AT_PHDR);
return phdr - @sizeOf(std.elf.Ehdr); return phdr - @sizeOf(std.elf.Ehdr);
}, },