| 1 |
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
| 2 |
file Copyright.txt or https://cmake.org/licensing#kwsys for details. */ |
| 3 |
#if defined(_WIN32) |
| 4 |
# define NOMINMAX // hide min,max to not conflict with <limits> |
| 5 |
#endif |
| 6 |
|
| 7 |
#include "kwsysPrivate.h" |
| 8 |
#include KWSYS_HEADER(DynamicLoader.hxx) |
| 9 |
|
| 10 |
#include KWSYS_HEADER(Configure.hxx) |
| 11 |
#include KWSYS_HEADER(Encoding.hxx) |
| 12 |
|
| 13 |
// Work-around CMake dependency scanning limitation. This must |
| 14 |
// duplicate the above list of headers. |
| 15 |
#if 0 |
| 16 |
# include "Configure.hxx.in" |
| 17 |
# include "DynamicLoader.hxx.in" |
| 18 |
#endif |
| 19 |
|
| 20 |
// This file actually contains several different implementations: |
| 21 |
// * NOOP for environments without dynamic libs |
| 22 |
// * HP machines which uses shl_load |
| 23 |
// * Mac OS X 10.2.x and earlier which uses NSLinkModule |
| 24 |
// * Windows which uses LoadLibrary |
| 25 |
// * BeOS / Haiku |
| 26 |
// * FreeMiNT for Atari |
| 27 |
// * Default implementation for *NIX systems (including Mac OS X 10.3 and |
| 28 |
// later) which use dlopen |
| 29 |
// |
| 30 |
// Each part of the ifdef contains a complete implementation for |
| 31 |
// the static methods of DynamicLoader. |
| 32 |
|
| 33 |
#define CHECK_OPEN_FLAGS(var, supported, ret) \ |
| 34 |
do { \ |
| 35 |
/* Check for unknown flags. */ \ |
| 36 |
if ((var & AllOpenFlags) != var) { \ |
| 37 |
return ret; \ |
| 38 |
} \ |
| 39 |
\ |
| 40 |
/* Check for unsupported flags. */ \ |
| 41 |
if ((var & (supported)) != var) { \ |
| 42 |
return ret; \ |
| 43 |
} \ |
| 44 |
} while (0) |
| 45 |
|
| 46 |
namespace KWSYS_NAMESPACE { |
| 47 |
|
| 48 |
DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary( |
| 49 |
const std::string& libname) |
| 50 |
{ |
| 51 |
return DynamicLoader::OpenLibrary(libname, 0); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
#if !KWSYS_SUPPORTS_SHARED_LIBS |
| 56 |
// Implementation for environments without dynamic libs |
| 57 |
# include <string.h> // for strerror() |
| 58 |
|
| 59 |
namespace KWSYS_NAMESPACE { |
| 60 |
|
| 61 |
DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary( |
| 62 |
const std::string& libname, int flags) |
| 63 |
{ |
| 64 |
return 0; |
| 65 |
} |
| 66 |
|
| 67 |
int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib) |
| 68 |
{ |
| 69 |
if (!lib) { |
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
return 1; |
| 74 |
} |
| 75 |
|
| 76 |
DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress( |
| 77 |
DynamicLoader::LibraryHandle lib, const std::string& sym) |
| 78 |
{ |
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
const char* DynamicLoader::LastError() |
| 83 |
{ |
| 84 |
return "General error"; |
| 85 |
} |
| 86 |
|
| 87 |
} // namespace KWSYS_NAMESPACE |
| 88 |
|
| 89 |
#elif defined(__hpux) |
| 90 |
// Implementation for HPUX machines |
| 91 |
# include <dl.h> |
| 92 |
# include <errno.h> |
| 93 |
|
| 94 |
namespace KWSYS_NAMESPACE { |
| 95 |
|
| 96 |
DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary( |
| 97 |
const std::string& libname, int flags) |
| 98 |
{ |
| 99 |
CHECK_OPEN_FLAGS(flags, 0, 0); |
| 100 |
|
| 101 |
return shl_load(libname.c_str(), BIND_DEFERRED | DYNAMIC_PATH, 0L); |
| 102 |
} |
| 103 |
|
| 104 |
int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib) |
| 105 |
{ |
| 106 |
if (!lib) { |
| 107 |
return 0; |
| 108 |
} |
| 109 |
return !shl_unload(lib); |
| 110 |
} |
| 111 |
|
| 112 |
DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress( |
| 113 |
DynamicLoader::LibraryHandle lib, const std::string& sym) |
| 114 |
{ |
| 115 |
void* addr; |
| 116 |
int status; |
| 117 |
|
| 118 |
/* TYPE_PROCEDURE Look for a function or procedure. (This used to be default) |
| 119 |
* TYPE_DATA Look for a symbol in the data segment (for example, |
| 120 |
* variables). |
| 121 |
* TYPE_UNDEFINED Look for any symbol. |
| 122 |
*/ |
| 123 |
status = shl_findsym(&lib, sym.c_str(), TYPE_UNDEFINED, &addr); |
| 124 |
void* result = (status < 0) ? (void*)0 : addr; |
| 125 |
|
| 126 |
// Hack to cast pointer-to-data to pointer-to-function. |
| 127 |
return *reinterpret_cast<DynamicLoader::SymbolPointer*>(&result); |
| 128 |
} |
| 129 |
|
| 130 |
const char* DynamicLoader::LastError() |
| 131 |
{ |
| 132 |
// TODO: Need implementation with errno/strerror |
| 133 |
/* If successful, shl_findsym returns an integer (int) value zero. If |
| 134 |
* shl_findsym cannot find sym, it returns -1 and sets errno to zero. |
| 135 |
* If any other errors occur, shl_findsym returns -1 and sets errno to one |
| 136 |
* of these values (defined in <errno.h>): |
| 137 |
* ENOEXEC |
| 138 |
* A format error was detected in the specified library. |
| 139 |
* ENOSYM |
| 140 |
* A symbol on which sym depends could not be found. |
| 141 |
* EINVAL |
| 142 |
* The specified handle is invalid. |
| 143 |
*/ |
| 144 |
|
| 145 |
if (errno == ENOEXEC || errno == ENOSYM || errno == EINVAL) { |
| 146 |
return strerror(errno); |
| 147 |
} |
| 148 |
// else |
| 149 |
return 0; |
| 150 |
} |
| 151 |
|
| 152 |
} // namespace KWSYS_NAMESPACE |
| 153 |
|
| 154 |
#elif defined(__APPLE__) && (MAC_OS_X_VERSION_MAX_ALLOWED < 1030) |
| 155 |
// Implementation for Mac OS X 10.2.x and earlier |
| 156 |
# include <mach-o/dyld.h> |
| 157 |
# include <string.h> // for strlen |
| 158 |
|
| 159 |
namespace KWSYS_NAMESPACE { |
| 160 |
|
| 161 |
DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary( |
| 162 |
const std::string& libname, int flags) |
| 163 |
{ |
| 164 |
CHECK_OPEN_FLAGS(flags, 0, 0); |
| 165 |
|
| 166 |
NSObjectFileImageReturnCode rc; |
| 167 |
NSObjectFileImage image = 0; |
| 168 |
|
| 169 |
rc = NSCreateObjectFileImageFromFile(libname.c_str(), &image); |
| 170 |
// rc == NSObjectFileImageInappropriateFile when trying to load a dylib file |
| 171 |
if (rc != NSObjectFileImageSuccess) { |
| 172 |
return 0; |
| 173 |
} |
| 174 |
NSModule handle = NSLinkModule(image, libname.c_str(), |
| 175 |
NSLINKMODULE_OPTION_BINDNOW | |
| 176 |
NSLINKMODULE_OPTION_RETURN_ON_ERROR); |
| 177 |
NSDestroyObjectFileImage(image); |
| 178 |
return handle; |
| 179 |
} |
| 180 |
|
| 181 |
int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib) |
| 182 |
{ |
| 183 |
// NSUNLINKMODULE_OPTION_KEEP_MEMORY_MAPPED |
| 184 |
// With this option the memory for the module is not deallocated |
| 185 |
// allowing pointers into the module to still be valid. |
| 186 |
// You should use this option instead if your code experience some problems |
| 187 |
// reported against Panther 10.3.9 (fixed in Tiger 10.4.2 and up) |
| 188 |
bool success = NSUnLinkModule(lib, NSUNLINKMODULE_OPTION_NONE); |
| 189 |
return success; |
| 190 |
} |
| 191 |
|
| 192 |
DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress( |
| 193 |
DynamicLoader::LibraryHandle lib, const std::string& sym) |
| 194 |
{ |
| 195 |
void* result = 0; |
| 196 |
// Need to prepend symbols with '_' on Apple-gcc compilers |
| 197 |
std::string rsym = '_' + sym; |
| 198 |
|
| 199 |
NSSymbol symbol = NSLookupSymbolInModule(lib, rsym.c_str()); |
| 200 |
if (symbol) { |
| 201 |
result = NSAddressOfSymbol(symbol); |
| 202 |
} |
| 203 |
|
| 204 |
// Hack to cast pointer-to-data to pointer-to-function. |
| 205 |
return *reinterpret_cast<DynamicLoader::SymbolPointer*>(&result); |
| 206 |
} |
| 207 |
|
| 208 |
const char* DynamicLoader::LastError() |
| 209 |
{ |
| 210 |
return 0; |
| 211 |
} |
| 212 |
|
| 213 |
} // namespace KWSYS_NAMESPACE |
| 214 |
|
| 215 |
#elif defined(_WIN32) && !defined(__CYGWIN__) |
| 216 |
// Implementation for Windows win32 code but not cygwin |
| 217 |
# include <windows.h> |
| 218 |
|
| 219 |
# include <stdio.h> |
| 220 |
|
| 221 |
namespace KWSYS_NAMESPACE { |
| 222 |
|
| 223 |
DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary( |
| 224 |
const std::string& libname, int flags) |
| 225 |
{ |
| 226 |
CHECK_OPEN_FLAGS(flags, SearchBesideLibrary, nullptr); |
| 227 |
|
| 228 |
DWORD llFlags = 0; |
| 229 |
if (flags & SearchBesideLibrary) { |
| 230 |
llFlags |= LOAD_WITH_ALTERED_SEARCH_PATH; |
| 231 |
} |
| 232 |
|
| 233 |
return LoadLibraryExW(Encoding::ToWindowsExtendedPath(libname).c_str(), |
| 234 |
nullptr, llFlags); |
| 235 |
} |
| 236 |
|
| 237 |
int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib) |
| 238 |
{ |
| 239 |
return (int)FreeLibrary(lib); |
| 240 |
} |
| 241 |
|
| 242 |
DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress( |
| 243 |
DynamicLoader::LibraryHandle lib, const std::string& sym) |
| 244 |
{ |
| 245 |
// TODO: The calling convention affects the name of the symbol. We |
| 246 |
// should have a tool to help get the symbol with the desired |
| 247 |
// calling convention. Currently we assume cdecl. |
| 248 |
// |
| 249 |
// MSVC: |
| 250 |
// __cdecl = "func" (default) |
| 251 |
// __fastcall = "@_func@X" |
| 252 |
// __stdcall = "_func@X" |
| 253 |
// |
| 254 |
// Note that the "@X" part of the name above is the total size (in |
| 255 |
// bytes) of the arguments on the stack. |
| 256 |
void* result; |
| 257 |
const char* rsym = sym.c_str(); |
| 258 |
result = (void*)GetProcAddress(lib, rsym); |
| 259 |
return *reinterpret_cast<DynamicLoader::SymbolPointer*>(&result); |
| 260 |
} |
| 261 |
|
| 262 |
# define DYNLOAD_ERROR_BUFFER_SIZE 1024 |
| 263 |
|
| 264 |
const char* DynamicLoader::LastError() |
| 265 |
{ |
| 266 |
wchar_t lpMsgBuf[DYNLOAD_ERROR_BUFFER_SIZE + 1]; |
| 267 |
|
| 268 |
DWORD error = GetLastError(); |
| 269 |
DWORD length = FormatMessageW( |
| 270 |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, error, |
| 271 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language |
| 272 |
lpMsgBuf, DYNLOAD_ERROR_BUFFER_SIZE, nullptr); |
| 273 |
|
| 274 |
static char str[DYNLOAD_ERROR_BUFFER_SIZE + 1]; |
| 275 |
|
| 276 |
if (length < 1) { |
| 277 |
/* FormatMessage failed. Use a default message. */ |
| 278 |
snprintf(str, DYNLOAD_ERROR_BUFFER_SIZE, |
| 279 |
"DynamicLoader encountered error 0x%lX. " |
| 280 |
"FormatMessage failed with error 0x%lX", |
| 281 |
error, GetLastError()); |
| 282 |
return str; |
| 283 |
} |
| 284 |
|
| 285 |
if (!WideCharToMultiByte(CP_UTF8, 0, lpMsgBuf, -1, str, |
| 286 |
DYNLOAD_ERROR_BUFFER_SIZE, nullptr, nullptr)) { |
| 287 |
/* WideCharToMultiByte failed. Use a default message. */ |
| 288 |
snprintf(str, DYNLOAD_ERROR_BUFFER_SIZE, |
| 289 |
"DynamicLoader encountered error 0x%lX. " |
| 290 |
"WideCharToMultiByte failed with error 0x%lX", |
| 291 |
error, GetLastError()); |
| 292 |
} |
| 293 |
|
| 294 |
return str; |
| 295 |
} |
| 296 |
|
| 297 |
} // namespace KWSYS_NAMESPACE |
| 298 |
|
| 299 |
#elif defined(__BEOS__) |
| 300 |
// Implementation for BeOS / Haiku |
| 301 |
# include <string.h> // for strerror() |
| 302 |
|
| 303 |
# include <be/kernel/image.h> |
| 304 |
# include <be/support/Errors.h> |
| 305 |
|
| 306 |
namespace KWSYS_NAMESPACE { |
| 307 |
|
| 308 |
static image_id last_dynamic_err = B_OK; |
| 309 |
|
| 310 |
DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary( |
| 311 |
const std::string& libname, int flags) |
| 312 |
{ |
| 313 |
CHECK_OPEN_FLAGS(flags, 0, 0); |
| 314 |
|
| 315 |
// image_id's are integers, errors are negative. Add one just in case we |
| 316 |
// get a valid image_id of zero (is that even possible?). |
| 317 |
image_id rc = load_add_on(libname.c_str()); |
| 318 |
if (rc < 0) { |
| 319 |
last_dynamic_err = rc; |
| 320 |
return 0; |
| 321 |
} |
| 322 |
|
| 323 |
return rc + 1; |
| 324 |
} |
| 325 |
|
| 326 |
int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib) |
| 327 |
{ |
| 328 |
if (!lib) { |
| 329 |
last_dynamic_err = B_BAD_VALUE; |
| 330 |
return 0; |
| 331 |
} else { |
| 332 |
// The function dlclose() returns 0 on success, and non-zero on error. |
| 333 |
status_t rc = unload_add_on(lib - 1); |
| 334 |
if (rc != B_OK) { |
| 335 |
last_dynamic_err = rc; |
| 336 |
return 0; |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
return 1; |
| 341 |
} |
| 342 |
|
| 343 |
DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress( |
| 344 |
DynamicLoader::LibraryHandle lib, const std::string& sym) |
| 345 |
{ |
| 346 |
// Hack to cast pointer-to-data to pointer-to-function. |
| 347 |
union |
| 348 |
{ |
| 349 |
void* pvoid; |
| 350 |
DynamicLoader::SymbolPointer psym; |
| 351 |
} result; |
| 352 |
|
| 353 |
result.psym = nullptr; |
| 354 |
|
| 355 |
if (!lib) { |
| 356 |
last_dynamic_err = B_BAD_VALUE; |
| 357 |
} else { |
| 358 |
// !!! FIXME: BeOS can do function-only lookups...does this ever |
| 359 |
// !!! FIXME: actually _want_ a data symbol lookup, or was this union |
| 360 |
// !!! FIXME: a leftover of dlsym()? (s/ANY/TEXT for functions only). |
| 361 |
status_t rc = |
| 362 |
get_image_symbol(lib - 1, sym.c_str(), B_SYMBOL_TYPE_ANY, &result.pvoid); |
| 363 |
if (rc != B_OK) { |
| 364 |
last_dynamic_err = rc; |
| 365 |
result.psym = nullptr; |
| 366 |
} |
| 367 |
} |
| 368 |
return result.psym; |
| 369 |
} |
| 370 |
|
| 371 |
const char* DynamicLoader::LastError() |
| 372 |
{ |
| 373 |
const char* retval = strerror(last_dynamic_err); |
| 374 |
last_dynamic_err = B_OK; |
| 375 |
return retval; |
| 376 |
} |
| 377 |
|
| 378 |
} // namespace KWSYS_NAMESPACE |
| 379 |
|
| 380 |
#elif defined(__MINT__) |
| 381 |
// Implementation for FreeMiNT on Atari |
| 382 |
# define _GNU_SOURCE /* for program_invocation_name */ |
| 383 |
# include <dld.h> |
| 384 |
# include <errno.h> |
| 385 |
# include <malloc.h> |
| 386 |
# include <string.h> |
| 387 |
|
| 388 |
namespace KWSYS_NAMESPACE { |
| 389 |
|
| 390 |
DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary( |
| 391 |
const std::string& libname, int flags) |
| 392 |
{ |
| 393 |
CHECK_OPEN_FLAGS(flags, 0, nullptr); |
| 394 |
|
| 395 |
char* name = (char*)calloc(1, libname.size() + 1); |
| 396 |
dld_init(program_invocation_name); |
| 397 |
strncpy(name, libname.c_str(), libname.size()); |
| 398 |
dld_link(libname.c_str()); |
| 399 |
return (void*)name; |
| 400 |
} |
| 401 |
|
| 402 |
int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib) |
| 403 |
{ |
| 404 |
dld_unlink_by_file((char*)lib, 0); |
| 405 |
free(lib); |
| 406 |
return 0; |
| 407 |
} |
| 408 |
|
| 409 |
DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress( |
| 410 |
DynamicLoader::LibraryHandle lib, const std::string& sym) |
| 411 |
{ |
| 412 |
// Hack to cast pointer-to-data to pointer-to-function. |
| 413 |
union |
| 414 |
{ |
| 415 |
void* pvoid; |
| 416 |
DynamicLoader::SymbolPointer psym; |
| 417 |
} result; |
| 418 |
result.pvoid = dld_get_symbol(sym.c_str()); |
| 419 |
return result.psym; |
| 420 |
} |
| 421 |
|
| 422 |
const char* DynamicLoader::LastError() |
| 423 |
{ |
| 424 |
return dld_strerror(dld_errno); |
| 425 |
} |
| 426 |
|
| 427 |
} // namespace KWSYS_NAMESPACE |
| 428 |
|
| 429 |
#else |
| 430 |
// Default implementation for *NIX systems (including Mac OS X 10.3 and |
| 431 |
// later) which use dlopen |
| 432 |
# include <dlfcn.h> |
| 433 |
|
| 434 |
namespace KWSYS_NAMESPACE { |
| 435 |
|
| 436 |
DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary( |
| 437 |
const std::string& libname, int flags) |
| 438 |
{ |
| 439 |
CHECK_OPEN_FLAGS(flags, RTLDGlobal, nullptr); |
| 440 |
|
| 441 |
int llFlags = RTLD_LAZY; |
| 442 |
if (flags & RTLDGlobal) { |
| 443 |
llFlags |= RTLD_GLOBAL; |
| 444 |
} |
| 445 |
|
| 446 |
return dlopen(libname.c_str(), llFlags); |
| 447 |
} |
| 448 |
|
| 449 |
int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib) |
| 450 |
{ |
| 451 |
if (lib) { |
| 452 |
// The function dlclose() returns 0 on success, and non-zero on error. |
| 453 |
return !dlclose(lib); |
| 454 |
} |
| 455 |
// else |
| 456 |
return 0; |
| 457 |
} |
| 458 |
|
| 459 |
DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress( |
| 460 |
DynamicLoader::LibraryHandle lib, const std::string& sym) |
| 461 |
{ |
| 462 |
// Hack to cast pointer-to-data to pointer-to-function. |
| 463 |
union |
| 464 |
{ |
| 465 |
void* pvoid; |
| 466 |
DynamicLoader::SymbolPointer psym; |
| 467 |
} result; |
| 468 |
result.pvoid = dlsym(lib, sym.c_str()); |
| 469 |
return result.psym; |
| 470 |
} |
| 471 |
|
| 472 |
const char* DynamicLoader::LastError() |
| 473 |
{ |
| 474 |
return dlerror(); |
| 475 |
} |
| 476 |
|
| 477 |
} // namespace KWSYS_NAMESPACE |
| 478 |
#endif |