#include "base/native_library.h"
#include <dlfcn.h>
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "base/strings/strcat.h"
#include "base/strings/string_piece.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 LoadNativeLibraryWithOptions(const FilePath& library_path,
const NativeLibraryOptions& options,
NativeLibraryLoadError* error) {
ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
int flags = RTLD_LAZY;
#if BUILDFLAG(IS_ANDROID) || !defined(RTLD_DEEPBIND)
CHECK(!options.prefer_own_symbols);
#else
if (options.prefer_own_symbols)
flags |= RTLD_DEEPBIND;
#endif
void* dl = dlopen(library_path.value().c_str(), flags);
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,
StringPiece name) {
return dlsym(library, name.data());
}
std::string GetNativeLibraryName(StringPiece name) {
DCHECK(IsStringASCII(name));
return StrCat({"lib", name, ".so"});
}
std::string GetLoadableModuleName(StringPiece name) {
return GetNativeLibraryName(name);
}
}