Debian Codegraph @

debug/+openbsd/translate.ha

raw
1 // SPDX-License-Identifier: MPL-2.0
2 // (c) Hare authors <https://harelang.org>
3
4 use rt;
5
6 // Tries to translate a pointer into an ELF address of the currently running
7 // binary.
8 export fn translate(ptr: uintptr) (uintptr | void) = {
9 let dl_info = rt::dl_info { ... };
10
11 // In order to avoid translating symbols in shared libaries and
12 // producing invalid results, use the current function (which should be
13 // in the main binary) to get the base image address.
14 //
15 // Theoretically, it should be possible to get symbol names from shared
16 // libaries. You would have to load the ELF shared libary at the path
17 // [[dl_info.fname]] as an ELF image and resolve the symbol name
18 // from there. Then you could do "s/&translate/ptr" below.
19 const ret = rt::dladdr(&translate: *opaque, &dl_info);
20 if (ret != 0) {
21 return ptr - dl_info.fbase: uintptr;
22 };
23 };