authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-11-23 12:00:12+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-11-23 12:00:12+01:00
log560043dadfa8bf3a6c15d7f09bfbc60e48e0084c
tree035eaee95973b7387dd56b4ef6226d5f6d983dbf
parentc7170e4a5480581db5f30913eebd9ad4f7cd121e

Fix logic for detecting _DYNAMIC symbol

Prevent spurious crashes for non-PIE executables.

2 files changed, 5 insertions(+), 15 deletions(-)

lib/std/dynamic_library.zig+3-15
......@@ -59,24 +59,12 @@ const RDebug = extern struct {
5959 r_ldbase: usize,
6060};
6161
62// TODO: This should be weak (#1917)
63extern var _DYNAMIC: [128]elf.Dyn;
64
65comptime {
66 if (std.Target.current.os.tag == .linux) {
67 asm (
68 \\ .weak _DYNAMIC
69 \\ .hidden _DYNAMIC
70 );
71 }
72}
73
7462pub fn linkmap_iterator(phdrs: []elf.Phdr) !LinkMap.Iterator {
75 if (@ptrToInt(&_DYNAMIC[0]) == 0) {
63 const _DYNAMIC = @extern([*]elf.Dyn, .{ .name = "_DYNAMIC", .linkage = .Weak }) orelse {
7664 // No PT_DYNAMIC means this is either a statically-linked program or a
77 // badly corrupted one
65 // badly corrupted dynamically-linked one.
7866 return LinkMap.Iterator{ .current = null };
79 }
67 };
8068
8169 const link_map_ptr = init: {
8270 var i: usize = 0;
lib/std/process.zig+2
......@@ -686,6 +686,8 @@ pub fn getBaseAddress() usize {
686686 if (base != 0) {
687687 return base;
688688 }
689 // XXX: Wrong for PIE executables, it should look at the difference
690 // between _DYNAMIC and the PT_DYNAMIC phdr instead.
689691 const phdr = os.system.getauxval(std.elf.AT_PHDR);
690692 return phdr - @sizeOf(std.elf.Ehdr);
691693 },