| 32 | namespace libtas { |
| 33 | |
| 34 | bool link_function(void** function, const char* source, const char* library, const char *version /*= nullptr*/) |
| 35 | { |
| 36 | /* Test if function is already linked */ |
| 37 | if (*function != nullptr) |
| 38 | return true; |
| 39 | |
| 40 | /* First try to link it from the global namespace */ |
| 41 | #ifdef __linux__ |
| 42 | if (version) |
| 43 | *function = dlvsym(RTLD_NEXT, source, version); |
| 44 | if (*function == nullptr) |
| 45 | #endif |
| 46 | NATIVECALL(*function = dlsym(RTLD_NEXT, source)); |
| 47 | |
| 48 | if (*function != nullptr) { |
| 49 | LOG(LL_DEBUG, LCF_HOOK, "Imported symbol %s function : %p", source, *function); |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | if (library != nullptr) { |
| 54 | |
| 55 | /* If it did not succeed, try to link using a matching library |
| 56 | * loaded by the game. |
| 57 | */ |
| 58 | std::string libpath = find_lib(library); |
| 59 | |
| 60 | void* handle; |
| 61 | if (! libpath.empty()) { |
| 62 | |
| 63 | /* Try to link again using a matching library */ |
| 64 | NATIVECALL(handle = dlopen(libpath.c_str(), RTLD_LAZY)); |
| 65 | |
| 66 | if (handle != NULL) { |
| 67 | NATIVECALL(*function = dlsym(handle, source)); |
| 68 | |
| 69 | if (*function != nullptr) { |
| 70 | LOG(LL_DEBUG, LCF_HOOK, "Imported from lib %s symbol %s function : %p", libpath.c_str(), source, *function); |
| 71 | return true; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /* If it did not succeed, try to link using the given library */ |
| 77 | NATIVECALL(handle = dlopen(library, RTLD_LAZY)); |
| 78 | |
| 79 | if (handle != NULL) { |
| 80 | NATIVECALL(*function = dlsym(handle, source)); |
| 81 | |
| 82 | if (*function != nullptr) { |
| 83 | LOG(LL_DEBUG, LCF_HOOK, "Imported from lib %s symbol %s function : %p", library, source, *function); |
| 84 | |
| 85 | /* Add the library to our set of libraries */ |
| 86 | add_lib(library); |
| 87 | return true; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 |
no test coverage detected