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 {...@@ -59,24 +59,12 @@ const RDebug = extern struct {
59 r_ldbase: usize,59 r_ldbase: usize,
60};60};
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
74pub fn linkmap_iterator(phdrs: []elf.Phdr) !LinkMap.Iterator {62pub 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 {
76 // No PT_DYNAMIC means this is either a statically-linked program or a64 // No PT_DYNAMIC means this is either a statically-linked program or a
77 // badly corrupted one65 // badly corrupted dynamically-linked one.
78 return LinkMap.Iterator{ .current = null };66 return LinkMap.Iterator{ .current = null };
79 }67 };
8068
81 const link_map_ptr = init: {69 const link_map_ptr = init: {
82 var i: usize = 0;70 var i: usize = 0;
lib/std/process.zig+2
...@@ -686,6 +686,8 @@ pub fn getBaseAddress() usize {...@@ -686,6 +686,8 @@ pub fn getBaseAddress() usize {
686 if (base != 0) {686 if (base != 0) {
687 return base;687 return base;
688 }688 }
689 // XXX: Wrong for PIE executables, it should look at the difference
690 // between _DYNAMIC and the PT_DYNAMIC phdr instead.
689 const phdr = os.system.getauxval(std.elf.AT_PHDR);691 const phdr = os.system.getauxval(std.elf.AT_PHDR);
690 return phdr - @sizeOf(std.elf.Ehdr);692 return phdr - @sizeOf(std.elf.Ehdr);
691 },693 },