| 714 | } |
| 715 | |
| 716 | void hook_patch(const char* name, const char* library, void** tramp_function, void* my_function) |
| 717 | { |
| 718 | const char* libpathstr = NULL; |
| 719 | void* handle; |
| 720 | |
| 721 | if (library) { |
| 722 | /* Find the path to the library */ |
| 723 | std::string libpath = find_lib(library); |
| 724 | |
| 725 | if (libpath.empty()) { |
| 726 | LOG(LL_ERROR, LCF_HOOK, "Could not find %s path", library); |
| 727 | return; |
| 728 | } |
| 729 | |
| 730 | libpathstr = libpath.c_str(); |
| 731 | } |
| 732 | |
| 733 | /* Open library */ |
| 734 | NATIVECALL(handle = dlopen(libpathstr, RTLD_LAZY)); |
| 735 | |
| 736 | if (!handle) { |
| 737 | LOG(LL_ERROR, LCF_HOOK, "Could not load %s", library); |
| 738 | return; |
| 739 | } |
| 740 | |
| 741 | /* Load function */ |
| 742 | void *orig_fun; |
| 743 | NATIVECALL(orig_fun = dlsym(handle, name)); |
| 744 | |
| 745 | if (!orig_fun && !library) { |
| 746 | /* If the symbol was not found, and if looking into the executable, |
| 747 | * ask libtas program to get the symbol address from the program elf |
| 748 | * header (much slower). We can't look into our own memory, because |
| 749 | * the .symtab and .strtab sections of the executable are not loaded |
| 750 | * in memory by the loader. |
| 751 | * I don't really want to do it here, because it relies on spawning |
| 752 | * another process to run `readelf`. |
| 753 | */ |
| 754 | sendMessage(MSGB_SYMBOL_ADDRESS); |
| 755 | std::string symstr(name); |
| 756 | sendString(symstr); |
| 757 | |
| 758 | uint64_t addr; |
| 759 | receiveData(&addr, sizeof(uint64_t)); |
| 760 | if (addr) |
| 761 | memcpy(&orig_fun, &addr, sizeof(void*)); |
| 762 | } |
| 763 | |
| 764 | if (!orig_fun) |
| 765 | return; |
| 766 | |
| 767 | LOG(LL_DEBUG, LCF_HOOK, "Patching function %s", name); |
| 768 | |
| 769 | hook_patch_addr(orig_fun, tramp_function, my_function); |
| 770 | } |
| 771 | |
| 772 | void hook_patch_addr(void *orig_fun, void** tramp_function, void* my_function) |
| 773 | { |
nothing calls this directly
no test coverage detected