diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2018-08-25 03:07:37 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2018-08-25 03:07:37 -0400 |
| commit | ac36f98e72f7ef33346bc772e4f257b79d04fcff (patch) | |
| tree | 88fc4a2e89dcecbfbb4c42eee9ed6411512d45c9 /std/os | |
| parent | 32901926f088cd9617e7d98e23b0b056b5495193 (diff) | |
| download | zig-ac36f98e72f7ef33346bc772e4f257b79d04fcff.tar.gz zig-ac36f98e72f7ef33346bc772e4f257b79d04fcff.zip | |
fix stack traces on linux
Diffstat (limited to 'std/os')
| -rw-r--r-- | std/os/index.zig | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/std/os/index.zig b/std/os/index.zig index a0b6e6bf45..29d887e214 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -635,6 +635,35 @@ fn posixExecveErrnoToErr(err: usize) PosixExecveError { pub var linux_aux_raw = []usize{0} ** 38; pub var posix_environ_raw: [][*]u8 = undefined; +/// See std.elf for the constants. +pub fn linuxGetAuxVal(index: usize) usize { + if (builtin.link_libc) { + return usize(std.c.getauxval(index)); + } else { + return linux_aux_raw[index]; + } +} + +pub fn getBaseAddress() usize { + switch (builtin.os) { + builtin.Os.linux => { + const base = linuxGetAuxVal(std.elf.AT_BASE); + if (base != 0) { + return base; + } + const phdr = linuxGetAuxVal(std.elf.AT_PHDR); + const ElfHeader = switch (@sizeOf(usize)) { + 4 => std.elf.Elf32_Ehdr, + 8 => std.elf.Elf64_Ehdr, + else => @compileError("Unsupported architecture"), + }; + return phdr - @sizeOf(ElfHeader); + }, + builtin.Os.macosx => return @ptrToInt(&std.c._mh_execute_header), + else => @compileError("Unsupported OS"), + } +} + /// Caller must free result when done. /// TODO make this go through libc when we have it pub fn getEnvMap(allocator: *Allocator) !BufMap { |
