File: os_library_linux.cpp

package info (click to toggle)
intel-compute-runtime 25.44.36015.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 79,632 kB
  • sloc: cpp: 931,547; lisp: 2,074; sh: 719; makefile: 162; python: 21
file content (102 lines) | stat: -rw-r--r-- 2,728 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * Copyright (C) 2019-2025 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "shared/source/os_interface/linux/os_library_linux.h"

#include "shared/source/helpers/debug_helpers.h"
#include "shared/source/os_interface/linux/sys_calls.h"

#include <cstring>
#include <dlfcn.h>
#include <link.h>

namespace NEO {

OsLibrary *OsLibrary::load(const OsLibraryCreateProperties &properties) {
    auto ptr = new (std::nothrow) Linux::OsLibrary(properties);
    if (ptr == nullptr) {
        return nullptr;
    }

    if (!ptr->isLoaded()) {
        delete ptr;
        return nullptr;
    }
    return ptr;
}

const std::string OsLibrary::createFullSystemPath(const std::string &name) {
    return name;
}

bool getLoadedLibVersion(const std::string &libName, const std::string &regexVersionPattern, std::string &outVersion, std::string &errReason) {
    return false;
}

namespace Linux {

OsLibrary::OsLibrary(const OsLibraryCreateProperties &properties) {
    if (properties.libraryName.empty() || properties.performSelfLoad) {
        this->handle = SysCalls::dlopen(0, RTLD_LAZY);
    } else {
        auto dlopenFlag = RTLD_LAZY;
        dlopenFlag = properties.customLoadFlags ? *properties.customLoadFlags : dlopenFlag;
        adjustLibraryFlags(dlopenFlag);
        this->handle = SysCalls::dlopen(properties.libraryName.c_str(), dlopenFlag);
        if (!this->handle && (properties.errorValue != nullptr)) {
            properties.errorValue->assign(dlerror());
        }
    }
}

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());
}

std::string OsLibrary::getFullPath() {
    struct link_map *map = nullptr;
    int retVal = NEO::SysCalls::dlinfo(this->handle, RTLD_DI_LINKMAP, &map);
    if (retVal == 0 && map != nullptr) {
        return std::string(map->l_name);
    }
    return std::string();
}

bool isLibraryLoaded(const std::string &libraryName) {
    auto handle = SysCalls::dlopen(0, RTLD_LAZY);
    if (!handle) {
        return false;
    }
    struct link_map *map = nullptr;
    int retVal = NEO::SysCalls::dlinfo(handle, RTLD_DI_LINKMAP, &map);
    if (retVal == 0 && map != nullptr) {
        while (map) {
            if (strstr(map->l_name, libraryName.c_str())) {
                dlclose(handle);
                return true;
            }
            map = map->l_next;
        }
    }
    dlclose(handle);
    return false;
}
} // namespace Linux
} // namespace NEO