File: translate.ha

package info (click to toggle)
hare 0.26.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,352 kB
  • sloc: asm: 1,374; makefile: 123; sh: 117; lisp: 101
file content (23 lines) | stat: -rw-r--r-- 855 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

use rt;

// Tries to translate a pointer into an ELF address of the currently running
// binary.
export fn translate(ptr: uintptr) (uintptr | void) = {
	let dl_info = rt::dl_info { ... };

	// In order to avoid translating symbols in shared libaries and
	// producing invalid results, use the current function (which should be
	// in the main binary) to get the base image address.
	//
	// Theoretically, it should be possible to get symbol names from shared
	// libaries. You would have to load the ELF shared libary at the path
	// [[dl_info.fname]] as an ELF image and resolve the symbol name
	// from there. Then you could do "s/&translate/ptr" below.
	const ret = rt::dladdr(&translate: *opaque, &dl_info);
	if (ret != 0) {
		return ptr - dl_info.fbase: uintptr;
	};
};