Line data Source code
1 : /* This Source Code Form is subject to the terms of the Mozilla Public
2 : * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 : * You can obtain one at http://mozilla.org/MPL/2.0/. */
4 :
5 : #include "GLLibraryLoader.h"
6 :
7 : #include "nsDebug.h"
8 :
9 : #ifdef WIN32
10 : #include <windows.h>
11 : #endif
12 :
13 : namespace mozilla {
14 : namespace gl {
15 :
16 : bool
17 0 : GLLibraryLoader::OpenLibrary(const char* library)
18 : {
19 : PRLibSpec lspec;
20 0 : lspec.type = PR_LibSpec_Pathname;
21 0 : lspec.value.pathname = library;
22 :
23 0 : mLibrary = PR_LoadLibraryWithFlags(lspec, PR_LD_LAZY | PR_LD_LOCAL);
24 0 : if (!mLibrary)
25 0 : return false;
26 :
27 0 : return true;
28 : }
29 :
30 : bool
31 0 : GLLibraryLoader::LoadSymbols(const SymLoadStruct* firstStruct,
32 : bool tryplatform,
33 : const char* prefix,
34 : bool warnOnFailure)
35 : {
36 0 : return LoadSymbols(mLibrary,
37 : firstStruct,
38 : tryplatform ? mLookupFunc : nullptr,
39 : prefix,
40 0 : warnOnFailure);
41 : }
42 :
43 : PRFuncPtr
44 0 : GLLibraryLoader::LookupSymbol(const char* sym)
45 : {
46 0 : return LookupSymbol(mLibrary, sym, mLookupFunc);
47 : }
48 :
49 : PRFuncPtr
50 0 : GLLibraryLoader::LookupSymbol(PRLibrary* lib,
51 : const char* sym,
52 : PlatformLookupFunction lookupFunction)
53 : {
54 0 : PRFuncPtr res = 0;
55 :
56 : // try finding it in the library directly, if we have one
57 0 : if (lib) {
58 0 : res = PR_FindFunctionSymbol(lib, sym);
59 : }
60 :
61 : // then try looking it up via the lookup symbol
62 0 : if (!res && lookupFunction) {
63 0 : res = lookupFunction(sym);
64 : }
65 :
66 : // finally just try finding it in the process
67 0 : if (!res) {
68 : PRLibrary* leakedLibRef;
69 0 : res = PR_FindFunctionSymbolAndLibrary(sym, &leakedLibRef);
70 : }
71 :
72 0 : return res;
73 : }
74 :
75 : bool
76 0 : GLLibraryLoader::LoadSymbols(PRLibrary* lib,
77 : const SymLoadStruct* firstStruct,
78 : PlatformLookupFunction lookupFunction,
79 : const char* prefix,
80 : bool warnOnFailure)
81 : {
82 : char sbuf[MAX_SYMBOL_LENGTH * 2];
83 0 : int failCount = 0;
84 :
85 0 : const SymLoadStruct* ss = firstStruct;
86 0 : while (ss->symPointer) {
87 0 : *ss->symPointer = 0;
88 :
89 0 : for (int i = 0; i < MAX_SYMBOL_NAMES; i++) {
90 0 : if (ss->symNames[i] == nullptr)
91 0 : break;
92 :
93 0 : const char* s = ss->symNames[i];
94 0 : if (prefix && *prefix != 0) {
95 0 : strcpy(sbuf, prefix);
96 0 : strcat(sbuf, ss->symNames[i]);
97 0 : s = sbuf;
98 : }
99 :
100 0 : PRFuncPtr p = LookupSymbol(lib, s, lookupFunction);
101 0 : if (p) {
102 0 : *ss->symPointer = p;
103 0 : break;
104 : }
105 : }
106 :
107 0 : if (*ss->symPointer == 0) {
108 0 : if (warnOnFailure) {
109 0 : printf_stderr("Can't find symbol '%s'.\n", ss->symNames[0]);
110 : }
111 :
112 0 : failCount++;
113 : }
114 :
115 0 : ss++;
116 : }
117 :
118 0 : return failCount == 0 ? true : false;
119 : }
120 :
121 : /*static*/ void
122 0 : GLLibraryLoader::ClearSymbols(const SymLoadStruct* const firstStruct)
123 : {
124 0 : for (auto cur = firstStruct; cur->symPointer; ++cur) {
125 0 : *cur->symPointer = nullptr;
126 : }
127 0 : }
128 :
129 : } /* namespace gl */
130 : } /* namespace mozilla */
131 :
|