#include "base/native_library.h"
#include <dlfcn.h>
#include <string_view>
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/scoped_blocking_call.h"
#include "build/build_config.h"
namespace base {
std::string NativeLibraryLoadError::ToString() const {
return message;
}
NativeLibrary LoadNativeLibrary(const FilePath& library_path,
NativeLibraryLoadError* error) {
ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
void* dl = dlopen(library_path.value().c_str(), RTLD_LAZY);
if (!dl && error) {
error->message = dlerror();
}
return dl;
}
void UnloadNativeLibrary(NativeLibrary library) {
int ret = dlclose(library);
if (ret < 0) {
DLOG(ERROR) << "dlclose failed: " << dlerror();
NOTREACHED();
}
}
void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
const char* name) {
return dlsym(library, name);
}
std::string GetNativeLibraryName(std::string_view name) {
DCHECK(IsStringASCII(name));
return StrCat({"lib", name, ".so"});
}
std::string GetLoadableModuleName(std::string_view name) {
return GetNativeLibraryName(name);
}
}