1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #ifndef SharedLibrary_hpp
16 #define SharedLibrary_hpp
26 void *getLibraryHandle(const char *path);
27 void *loadLibrary(const char *path);
28 void freeLibrary(void *library);
29 void *getProcAddress(void *library, const char *name);
32 void *loadLibrary(const std::string &libraryDirectory, const char *(&names)[n], const char *mustContainSymbol = nullptr)
34 for(const char *libraryName : names)
36 std::string libraryPath = libraryDirectory + libraryName;
37 void *library = getLibraryHandle(libraryPath.c_str());
41 if(!mustContainSymbol || getProcAddress(library, mustContainSymbol))
50 for(const char *libraryName : names)
52 std::string libraryPath = libraryDirectory + libraryName;
53 void *library = loadLibrary(libraryPath.c_str());
57 if(!mustContainSymbol || getProcAddress(library, mustContainSymbol))
70 inline void *loadLibrary(const char *path)
72 return (void*)LoadLibrary(path);
75 inline void *getLibraryHandle(const char *path)
77 HMODULE module = NULL;
78 GetModuleHandleEx(0, path, &module);
82 inline void freeLibrary(void *library)
84 FreeLibrary((HMODULE)library);
87 inline void *getProcAddress(void *library, const char *name)
89 return (void*)GetProcAddress((HMODULE)library, name);
92 inline std::string getLibraryDirectoryFromSymbol(void* symbol)
94 HMODULE module = NULL;
95 GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCTSTR)symbol, &module);
97 if(module && (GetModuleFileName(module, filename, sizeof(filename)) != 0))
99 std::string directory(filename);
100 return directory.substr(0, directory.find_last_of("\\/") + 1).c_str();
108 inline void *loadLibrary(const char *path)
110 return dlopen(path, RTLD_LAZY | RTLD_LOCAL);
113 inline void *getLibraryHandle(const char *path)
116 // bionic doesn't support RTLD_NOLOAD before L
117 return dlopen(path, RTLD_NOW | RTLD_LOCAL);
119 void *resident = dlopen(path, RTLD_LAZY | RTLD_NOLOAD | RTLD_LOCAL);
123 return dlopen(path, RTLD_LAZY | RTLD_LOCAL); // Increment reference count
130 inline void freeLibrary(void *library)
138 inline void *getProcAddress(void *library, const char *name)
140 void *symbol = dlsym(library, name);
144 const char *reason = dlerror(); // Silence the error
151 inline std::string getLibraryDirectoryFromSymbol(void* symbol)
154 if(dladdr(symbol, &dl_info) != 0)
156 std::string directory(dl_info.dli_fname);
157 return directory.substr(0, directory.find_last_of("\\/") + 1).c_str();
166 #endif // SharedLibrary_hpp