/usr/include/d/gtkd-3/gtkd/Loader.d is in libgtkd-3-dev 3.7.5-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 | /*
* This file is part of gtkD.
*
* gtkD is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version, with
* some exceptions, please read the COPYING file.
*
* gtkD is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with gtkD; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
*/
module gtkd.Loader;
import std.stdio;
import std.string;
import gtkd.paths;
public struct Linker
{
private static void*[string] loadedLibraries;
private static string[][string] loadFailures;
extern(C) static void unsupportedSymbol()
{
throw new Error("The function you are calling is not pressent in your version of GTK+.");
}
/*
* Links the provided symbol
* Params:
* funct = The function we are linking
* symbol = The name of the symbol to link
* libraries = One or more libraries to search for the symbol
*/
deprecated("Use the LIBRARY_* symbols defined for each package, instead of gtkd.paths.LIBRARY")
public static void link(T)(ref T funct, string symbol, LIBRARY[] libraries ...)
{
funct = cast(T)getSymbol(symbol, libraries);
}
/*
* Links the provided symbol
* Params:
* funct = The function we are linking
* symbol = The name of the symbol to link
* libraries = One or more libraries to search for the symbol
*/
public static void link(T)(ref T funct, string symbol, const string[] libraries ...)
{
funct = cast(T)getSymbol(symbol, libraries);
}
/*
* Gets a simbol from one of the provided libraries
* Params:
* symbol = The name of the symbol to link
* libraries = One or more libraries to search for the symbol
*/
deprecated("Use the LIBRARY_* symbols defined for each package, instead of gtkd.paths.LIBRARY")
public static void* getSymbol(string symbol, LIBRARY[] libraries ...)
{
string[] libStr = new string[libraries.length];
foreach (i, library; libraries )
{
libStr[i] = importLibs[library];
}
return getSymbol(symbol, libStr);
}
/*
* Gets a simbol from one of the provided libraries
* Params:
* symbol = The name of the symbol to link
* libraries = One or more libraries to search for the symbol
*/
public static void* getSymbol(string symbol, const string[] libraries ...)
{
void* handle;
foreach ( library; libraries )
{
if( !(library in loadedLibraries) )
loadLibrary(library);
handle = pGetSymbol(loadedLibraries[library], symbol);
if ( handle !is null )
break;
}
if ( handle is null )
{
foreach ( library; libraries )
loadFailures[library] ~= symbol;
handle = &unsupportedSymbol;
}
return handle;
}
/*
* Loads a library
*/
public static void loadLibrary(string library)
{
void* handle = pLoadLibrary(library);
//TODO: A more general way to try more than one version.
if ( handle is null && library == importLibs[LIBRARY.GSV] )
handle = pLoadLibrary(importLibs[LIBRARY.GSV1]);
if ( handle is null )
throw new Exception("Library load failed ("~ library ~"): "~ getErrorMessage());
loadedLibraries[library] = handle;
}
/*
* Unload a library
*/
deprecated("Use the LIBRARY_* symbols defined for each package, instead of gtkd.paths.LIBRARY")
public static void unloadLibrary(LIBRARY library)
{
unloadLibrary( importLibs[library] );
}
/*
* Unload a library
*/
public static void unloadLibrary(string library)
{
pUnloadLibrary(loadedLibraries[library]);
loadedLibraries.remove(library);
}
///Ditto
public static void unloadLibrary(const string[] libraries)
{
foreach ( lib; libraries )
{
unloadLibrary(lib);
}
}
/**
* Checks if any symbol failed to load
* Returns: true if ALL symbols are loaded
*/
public static bool isPerfectLoad()
{
return loadFailures.keys.length == 0;
}
/**
* Gets all libraries loaded.
* returns: An array with the loaded libraries
*/
public static string[] getLoadLibraries()
{
return loadedLibraries.keys;
}
/**
* Print all libraries loaded.
*/
public static void dumpLoadLibraries()
{
foreach ( lib; getLoadLibraries() )
{
writefln("Loaded lib = %s", lib);
}
}
/**
* Checks if a library is loaded.
* Returns: true is the library was loaded sucsessfully.
*/
deprecated("Use the LIBRARY_* symbols defined for each package, instead of gtkd.paths.LIBRARY")
public static bool isLoaded(LIBRARY library)
{
return isLoaded(importLibs[library]);
}
/**
* Checks if a library is loaded.
* Returns: true is the library was loaded sucsessfully.
*/
public static bool isLoaded(string library)
{
if ( library in loadedLibraries )
return true;
else
return false;
}
///Ditto
public static bool isLoaded(const string[] libraries)
{
return isLoaded(libraries[0]);
}
/**
* Gets all the failed loads for a specific library.
* returns: An array of the names hat failed to load for a specific library
* or null if none was found
*/
deprecated("Use the LIBRARY_* symbols defined for each package, instead of gtkd.paths.LIBRARY")
public static string[] getLoadFailures(LIBRARY library)
{
return getLoadFailures(importLibs[library]);
}
/**
* Gets all the failed loads for a specific library.
* returns: An array of the names hat failed to load for a specific library
* or null if none was found
*/
public static string[] getLoadFailures(string library)
{
if ( library in loadFailures )
return loadFailures[library];
else
return null;
}
///Ditto.
public static string[] getLoadFailures(const string[] libraries)
{
string[] failures;
foreach ( lib; libraries )
{
failures ~= getLoadFailures(lib);
}
return failures;
}
/**
* Print all symbols that failed to load
*/
public static void dumpFailedLoads()
{
foreach ( library; loadedLibraries.keys )
{
foreach ( symbol; getLoadFailures(library) )
{
writefln("failed (%s) %s", library, symbol);
}
}
}
static ~this()
{
foreach ( library; loadedLibraries.keys )
unloadLibrary(library);
}
}
// Platform specific implementation below.
version(Windows)
{
import core.sys.windows.winbase : LoadLibraryA, GetProcAddress, FreeLibrary, GetLastError, FormatMessageA,
FORMAT_MESSAGE_FROM_SYSTEM, FORMAT_MESSAGE_ARGUMENT_ARRAY;
import core.sys.windows.winnt : LANG_NEUTRAL, IMAGE_FILE_MACHINE_AMD64, IMAGE_FILE_MACHINE_I386;
extern(Windows)
{
int SetDllDirectoryA(const(char)* path);
}
private void* pLoadLibrary(string libraryName)
{
setDllPath();
return LoadLibraryA(cast(char*)toStringz(libraryName));
}
private void* pGetSymbol(void* handle, string symbol)
{
return GetProcAddress(handle, cast(char*)toStringz(symbol));
}
private alias FreeLibrary pUnloadLibrary;
private string getErrorMessage()
{
char[] buffer = new char[2048];
buffer[0] = '\0';
FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY,
null,
GetLastError(),
LANG_NEUTRAL,
buffer.ptr,
cast(uint)buffer.length,
cast(char**)["\0".ptr].ptr);
return buffer.ptr.fromStringz.idup;
}
private void setDllPath()
{
static bool isSet;
if ( isSet )
return;
string gtkPath = getGtkPath();
if ( gtkPath.length > 0 )
SetDllDirectoryA((gtkPath~'\0').ptr);
isSet = true;
}
private string getGtkPath()
{
import std.algorithm;
import std.path;
import std.process;
import std.file;
foreach (path; splitter(environment.get("PATH"), ';'))
{
string dllPath = buildNormalizedPath(path, "libgtk-3-0.dll");
if ( !exists(dllPath) )
continue;
if ( checkArchitecture(dllPath) )
return path;
}
return null;
}
private bool checkArchitecture(string dllPath)
{
import std.stdio;
File dll = File(dllPath);
dll.seek(0x3c);
int offset = dll.rawRead(new int[1])[0];
dll.seek(offset);
uint peHead = dll.rawRead(new uint[1])[0];
//Not a PE Header.
if( peHead != 0x00004550)
return false;
ushort type = dll.rawRead(new ushort[1])[0];
version(Win32)
{
if ( type == IMAGE_FILE_MACHINE_I386 )
return true;
}
else version(Win64)
{
if ( type == IMAGE_FILE_MACHINE_AMD64 )
return true;
}
return false;
}
}
else
{
import core.sys.posix.dlfcn : dlopen, dlerror, dlsym, dlclose, RTLD_NOW, RTLD_GLOBAL;
import std.path : buildPath;
private string lastError;
version(OSX)
{
string basePath()
{
import std.process;
static string path;
if (path !is null)
return path;
path = environment.get("GTK_BASEPATH");
if(!path){
path=environment.get("HOMEBREW_ROOT");
if(path){
path=path.buildPath("lib");
}
}
return path;
}
}
else
{
enum basePath = "";
}
private void* pLoadLibrary(string libraryName, int flag = RTLD_NOW)
{
void* handle = dlopen(cast(char*)toStringz(basePath.buildPath(libraryName)), flag | RTLD_GLOBAL);
if(!handle){
lastError = dlerror().fromStringz.idup;
}
// clear the error buffer
dlerror();
return handle;
}
private void* pGetSymbol(void* libraryHandle, string symbol)
{
void* symbolHandle = dlsym(libraryHandle, cast(char*)toStringz(symbol));
// clear the error buffer
dlerror();
return symbolHandle;
}
private int pUnloadLibrary(void* libraryHandle)
{
return dlclose(libraryHandle);
}
private string getErrorMessage()
{
scope(exit) lastError = null;
return lastError;
}
}
|