hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

translate.ha (869B)


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