2017-12-21 07:45:38 +08:00
|
|
|
/*
|
2021-05-17 02:51:16 +08:00
|
|
|
* Copyright (C) 2019-2021 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"
|
2021-02-23 21:03:54 +08:00
|
|
|
#include "shared/source/os_interface/linux/sys_calls.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) {
|
2021-03-12 09:01:51 +08:00
|
|
|
return load(name, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
OsLibrary *OsLibrary::load(const std::string &name, std::string *errorValue) {
|
|
|
|
auto ptr = new (std::nothrow) Linux::OsLibrary(name, errorValue);
|
2017-12-21 07:45:38 +08:00
|
|
|
if (ptr == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (!ptr->isLoaded()) {
|
|
|
|
delete ptr;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
2020-10-30 20:42:13 +08:00
|
|
|
|
|
|
|
const std::string OsLibrary::createFullSystemPath(const std::string &name) {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
namespace Linux {
|
|
|
|
|
2021-03-12 09:01:51 +08:00
|
|
|
OsLibrary::OsLibrary(const std::string &name, std::string *errorValue) {
|
2017-12-21 07:45:38 +08:00
|
|
|
if (name.empty()) {
|
2021-02-23 21:03:54 +08:00
|
|
|
this->handle = SysCalls::dlopen(0, RTLD_LAZY);
|
2017-12-21 07:45:38 +08:00
|
|
|
} else {
|
2019-03-01 01:13:29 +08:00
|
|
|
#ifdef SANITIZER_BUILD
|
2021-02-23 21:03:54 +08:00
|
|
|
auto dlopenFlag = RTLD_LAZY;
|
2019-03-01 01:13:29 +08:00
|
|
|
#else
|
2021-02-23 21:03:54 +08:00
|
|
|
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
|
2021-02-23 21:03:54 +08:00
|
|
|
adjustLibraryFlags(dlopenFlag);
|
|
|
|
this->handle = SysCalls::dlopen(name.c_str(), dlopenFlag);
|
2021-03-12 09:01:51 +08:00
|
|
|
if (!this->handle && (errorValue != nullptr)) {
|
|
|
|
errorValue->assign(dlerror());
|
|
|
|
}
|
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
|