Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
| 2 | // |
| 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 |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 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. |
| 14 | |
| 15 | #ifndef SharedLibrary_hpp |
| 16 | #define SharedLibrary_hpp |
| 17 | |
| 18 | #if defined(_WIN32) |
| 19 | #include <Windows.h> |
| 20 | #else |
| 21 | #include <dlfcn.h> |
| 22 | #endif |
| 23 | |
Alexis Hetu | 99be318 | 2018-04-16 15:38:40 -0400 | [diff] [blame] | 24 | #include <string> |
| 25 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 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); |
Alexis Hetu | 0ab9f3b | 2018-11-26 17:25:23 -0500 | [diff] [blame] | 30 | std::string getModuleDirectory(); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 31 | |
| 32 | template<int n> |
Alexis Hetu | 99be318 | 2018-04-16 15:38:40 -0400 | [diff] [blame] | 33 | void *loadLibrary(const std::string &libraryDirectory, const char *(&names)[n], const char *mustContainSymbol = nullptr) |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 34 | { |
Nicolas Capens | a9969b2 | 2018-06-21 01:42:30 -0400 | [diff] [blame] | 35 | for(const char *libraryName : names) |
Alexis Hetu | 99be318 | 2018-04-16 15:38:40 -0400 | [diff] [blame] | 36 | { |
Nicolas Capens | a9969b2 | 2018-06-21 01:42:30 -0400 | [diff] [blame] | 37 | std::string libraryPath = libraryDirectory + libraryName; |
| 38 | void *library = getLibraryHandle(libraryPath.c_str()); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 39 | |
| 40 | if(library) |
| 41 | { |
| 42 | if(!mustContainSymbol || getProcAddress(library, mustContainSymbol)) |
| 43 | { |
| 44 | return library; |
| 45 | } |
| 46 | |
| 47 | freeLibrary(library); |
| 48 | } |
| 49 | } |
| 50 | |
Nicolas Capens | a9969b2 | 2018-06-21 01:42:30 -0400 | [diff] [blame] | 51 | for(const char *libraryName : names) |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 52 | { |
Nicolas Capens | a9969b2 | 2018-06-21 01:42:30 -0400 | [diff] [blame] | 53 | std::string libraryPath = libraryDirectory + libraryName; |
| 54 | void *library = loadLibrary(libraryPath.c_str()); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 55 | |
| 56 | if(library) |
| 57 | { |
| 58 | if(!mustContainSymbol || getProcAddress(library, mustContainSymbol)) |
| 59 | { |
| 60 | return library; |
| 61 | } |
| 62 | |
| 63 | freeLibrary(library); |
| 64 | } |
| 65 | } |
| 66 | |
Nicolas Capens | 77de547 | 2017-06-21 13:12:42 -0400 | [diff] [blame] | 67 | return nullptr; |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | #if defined(_WIN32) |
| 71 | inline void *loadLibrary(const char *path) |
| 72 | { |
| 73 | return (void*)LoadLibrary(path); |
| 74 | } |
| 75 | |
| 76 | inline void *getLibraryHandle(const char *path) |
| 77 | { |
Nicolas Capens | 77de547 | 2017-06-21 13:12:42 -0400 | [diff] [blame] | 78 | HMODULE module = NULL; |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 79 | GetModuleHandleEx(0, path, &module); |
| 80 | return (void*)module; |
| 81 | } |
| 82 | |
| 83 | inline void freeLibrary(void *library) |
| 84 | { |
| 85 | FreeLibrary((HMODULE)library); |
| 86 | } |
| 87 | |
| 88 | inline void *getProcAddress(void *library, const char *name) |
| 89 | { |
| 90 | return (void*)GetProcAddress((HMODULE)library, name); |
| 91 | } |
| 92 | #else |
| 93 | inline void *loadLibrary(const char *path) |
| 94 | { |
| 95 | return dlopen(path, RTLD_LAZY | RTLD_LOCAL); |
| 96 | } |
| 97 | |
| 98 | inline void *getLibraryHandle(const char *path) |
| 99 | { |
| 100 | #ifdef __ANDROID__ |
| 101 | // bionic doesn't support RTLD_NOLOAD before L |
| 102 | return dlopen(path, RTLD_NOW | RTLD_LOCAL); |
| 103 | #else |
| 104 | void *resident = dlopen(path, RTLD_LAZY | RTLD_NOLOAD | RTLD_LOCAL); |
| 105 | |
| 106 | if(resident) |
| 107 | { |
| 108 | return dlopen(path, RTLD_LAZY | RTLD_LOCAL); // Increment reference count |
| 109 | } |
| 110 | |
Nicolas Capens | 77de547 | 2017-06-21 13:12:42 -0400 | [diff] [blame] | 111 | return nullptr; |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 112 | #endif |
| 113 | } |
| 114 | |
| 115 | inline void freeLibrary(void *library) |
| 116 | { |
| 117 | if(library) |
| 118 | { |
| 119 | dlclose(library); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | inline void *getProcAddress(void *library, const char *name) |
| 124 | { |
| 125 | void *symbol = dlsym(library, name); |
| 126 | |
| 127 | if(!symbol) |
| 128 | { |
| 129 | const char *reason = dlerror(); // Silence the error |
| 130 | (void)reason; |
| 131 | } |
| 132 | |
| 133 | return symbol; |
| 134 | } |
| 135 | #endif |
| 136 | |
| 137 | #endif // SharedLibrary_hpp |