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