|
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 namespace mozilla { |
|
10 namespace gl { |
|
11 |
|
12 bool |
|
13 GLLibraryLoader::OpenLibrary(const char *library) |
|
14 { |
|
15 PRLibSpec lspec; |
|
16 lspec.type = PR_LibSpec_Pathname; |
|
17 lspec.value.pathname = library; |
|
18 |
|
19 mLibrary = PR_LoadLibraryWithFlags(lspec, PR_LD_LAZY | PR_LD_LOCAL); |
|
20 if (!mLibrary) |
|
21 return false; |
|
22 |
|
23 return true; |
|
24 } |
|
25 |
|
26 bool |
|
27 GLLibraryLoader::LoadSymbols(SymLoadStruct *firstStruct, |
|
28 bool tryplatform, |
|
29 const char *prefix, |
|
30 bool warnOnFailure) |
|
31 { |
|
32 return LoadSymbols(mLibrary, |
|
33 firstStruct, |
|
34 tryplatform ? mLookupFunc : nullptr, |
|
35 prefix, |
|
36 warnOnFailure); |
|
37 } |
|
38 |
|
39 PRFuncPtr |
|
40 GLLibraryLoader::LookupSymbol(PRLibrary *lib, |
|
41 const char *sym, |
|
42 PlatformLookupFunction lookupFunction) |
|
43 { |
|
44 PRFuncPtr res = 0; |
|
45 |
|
46 // try finding it in the library directly, if we have one |
|
47 if (lib) { |
|
48 res = PR_FindFunctionSymbol(lib, sym); |
|
49 } |
|
50 |
|
51 // then try looking it up via the lookup symbol |
|
52 if (!res && lookupFunction) { |
|
53 res = lookupFunction(sym); |
|
54 } |
|
55 |
|
56 // finally just try finding it in the process |
|
57 if (!res) { |
|
58 PRLibrary *leakedLibRef; |
|
59 res = PR_FindFunctionSymbolAndLibrary(sym, &leakedLibRef); |
|
60 } |
|
61 |
|
62 return res; |
|
63 } |
|
64 |
|
65 bool |
|
66 GLLibraryLoader::LoadSymbols(PRLibrary *lib, |
|
67 SymLoadStruct *firstStruct, |
|
68 PlatformLookupFunction lookupFunction, |
|
69 const char *prefix, |
|
70 bool warnOnFailure) |
|
71 { |
|
72 char sbuf[MAX_SYMBOL_LENGTH * 2]; |
|
73 int failCount = 0; |
|
74 |
|
75 SymLoadStruct *ss = firstStruct; |
|
76 while (ss->symPointer) { |
|
77 *ss->symPointer = 0; |
|
78 |
|
79 for (int i = 0; i < MAX_SYMBOL_NAMES; i++) { |
|
80 if (ss->symNames[i] == nullptr) |
|
81 break; |
|
82 |
|
83 const char *s = ss->symNames[i]; |
|
84 if (prefix && *prefix != 0) { |
|
85 strcpy(sbuf, prefix); |
|
86 strcat(sbuf, ss->symNames[i]); |
|
87 s = sbuf; |
|
88 } |
|
89 |
|
90 PRFuncPtr p = LookupSymbol(lib, s, lookupFunction); |
|
91 if (p) { |
|
92 *ss->symPointer = p; |
|
93 break; |
|
94 } |
|
95 } |
|
96 |
|
97 if (*ss->symPointer == 0) { |
|
98 if (warnOnFailure) |
|
99 printf_stderr("Can't find symbol '%s'.\n", ss->symNames[0]); |
|
100 |
|
101 failCount++; |
|
102 } |
|
103 |
|
104 ss++; |
|
105 } |
|
106 |
|
107 return failCount == 0 ? true : false; |
|
108 } |
|
109 |
|
110 } /* namespace gl */ |
|
111 } /* namespace mozilla */ |
|
112 |