2017-12-21 07:45:38 +08:00
|
|
|
/*
|
2020-02-23 05:21:06 +08:00
|
|
|
* Copyright (C) 2017-2020 Intel Corporation
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
2018-09-18 15:11:08 +08:00
|
|
|
* SPDX-License-Identifier: MIT
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/os_interface/linux/os_library_linux.h"
|
2019-02-27 18:39:32 +08:00
|
|
|
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/helpers/debug_helpers.h"
|
2019-02-27 18:39:32 +08:00
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
#include <dlfcn.h>
|
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
namespace NEO {
|
2017-12-21 07:45:38 +08:00
|
|
|
OsLibrary *OsLibrary::load(const std::string &name) {
|
|
|
|
auto ptr = new (std::nothrow) Linux::OsLibrary(name);
|
|
|
|
if (ptr == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (!ptr->isLoaded()) {
|
|
|
|
delete ptr;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
namespace Linux {
|
|
|
|
|
|
|
|
OsLibrary::OsLibrary(const std::string &name) {
|
|
|
|
if (name.empty()) {
|
|
|
|
this->handle = dlopen(0, RTLD_LAZY);
|
|
|
|
} else {
|
2019-03-01 01:13:29 +08:00
|
|
|
#ifdef SANITIZER_BUILD
|
|
|
|
constexpr auto dlopenFlag = RTLD_LAZY;
|
|
|
|
#else
|
|
|
|
constexpr auto dlopenFlag = RTLD_LAZY | RTLD_DEEPBIND;
|
2020-08-06 23:58:01 +08:00
|
|
|
/* Background: https://github.com/intel/compute-runtime/issues/122 */
|
2019-03-01 01:13:29 +08:00
|
|
|
#endif
|
|
|
|
this->handle = dlopen(name.c_str(), dlopenFlag);
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OsLibrary::~OsLibrary() {
|
|
|
|
if (this->handle != nullptr) {
|
|
|
|
dlclose(this->handle);
|
|
|
|
this->handle = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OsLibrary::isLoaded() {
|
|
|
|
return this->handle != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *OsLibrary::getProcAddress(const std::string &procName) {
|
|
|
|
DEBUG_BREAK_IF(this->handle == nullptr);
|
|
|
|
|
|
|
|
return dlsym(this->handle, procName.c_str());
|
|
|
|
}
|
2018-06-13 03:54:39 +08:00
|
|
|
} // namespace Linux
|
2019-03-26 18:59:46 +08:00
|
|
|
} // namespace NEO
|