Skip to content

Commit

Permalink
[Libomptarget] Make dynamic loading libffi more verbose. (#86891)
Browse files Browse the repository at this point in the history
  • Loading branch information
ye-luo committed Mar 27, 2024
1 parent 385e3e2 commit 19185d5
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions openmp/libomptarget/plugins-nextgen/host/dynamic_ffi/ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
//===----------------------------------------------------------------------===//

#include "llvm/Support/DynamicLibrary.h"

#include "Shared/Debug.h"
#include <memory>

#include "DLWrap.h"
Expand All @@ -37,24 +39,32 @@ uint32_t ffi_init() {
std::string ErrMsg;
auto DynlibHandle = std::make_unique<llvm::sys::DynamicLibrary>(
llvm::sys::DynamicLibrary::getPermanentLibrary(FFI_PATH, &ErrMsg));
if (!DynlibHandle->isValid())

if (!DynlibHandle->isValid()) {
DP("Unable to load library '%s': %s!\n", FFI_PATH, ErrMsg.c_str());
return DYNAMIC_FFI_FAIL;
}

for (size_t I = 0; I < dlwrap::size(); I++) {
const char *Sym = dlwrap::symbol(I);

void *P = DynlibHandle->getAddressOfSymbol(Sym);
if (P == nullptr)
if (P == nullptr) {
DP("Unable to find '%s' in '%s'!\n", Sym, FFI_PATH);
return DYNAMIC_FFI_FAIL;
}
DP("Implementing %s with dlsym(%s) -> %p\n", Sym, Sym, P);

*dlwrap::pointer(I) = P;
}

#define DYNAMIC_INIT(SYMBOL) \
{ \
void *SymbolPtr = DynlibHandle->getAddressOfSymbol(#SYMBOL); \
if (!SymbolPtr) \
if (!SymbolPtr) { \
DP("Unable to find '%s' in '%s'!\n", #SYMBOL, FFI_PATH); \
return DYNAMIC_FFI_FAIL; \
} \
SYMBOL = *reinterpret_cast<decltype(SYMBOL) *>(SymbolPtr); \
}
DYNAMIC_INIT(ffi_type_void);
Expand Down

0 comments on commit 19185d5

Please sign in to comment.