2012-03-17 02:24:12 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "GLLibraryLoader.h"
|
|
|
|
|
2012-12-19 11:16:02 +04:00
|
|
|
#include "nsDebug.h"
|
|
|
|
|
2016-07-14 21:54:48 +03:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2012-03-17 02:24:12 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace gl {
|
|
|
|
|
|
|
|
bool
|
2016-06-11 04:37:07 +03:00
|
|
|
GLLibraryLoader::OpenLibrary(const char* library)
|
2012-03-17 02:24:12 +04:00
|
|
|
{
|
|
|
|
PRLibSpec lspec;
|
|
|
|
lspec.type = PR_LibSpec_Pathname;
|
|
|
|
lspec.value.pathname = library;
|
|
|
|
|
|
|
|
mLibrary = PR_LoadLibraryWithFlags(lspec, PR_LD_LAZY | PR_LD_LOCAL);
|
|
|
|
if (!mLibrary)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-06-11 04:37:07 +03:00
|
|
|
GLLibraryLoader::LoadSymbols(const SymLoadStruct* firstStruct,
|
2012-12-19 11:16:02 +04:00
|
|
|
bool tryplatform,
|
2016-06-11 04:37:07 +03:00
|
|
|
const char* prefix,
|
2012-12-19 11:16:02 +04:00
|
|
|
bool warnOnFailure)
|
2012-03-17 02:24:12 +04:00
|
|
|
{
|
2012-12-19 11:16:02 +04:00
|
|
|
return LoadSymbols(mLibrary,
|
|
|
|
firstStruct,
|
|
|
|
tryplatform ? mLookupFunc : nullptr,
|
|
|
|
prefix,
|
|
|
|
warnOnFailure);
|
2012-03-17 02:24:12 +04:00
|
|
|
}
|
|
|
|
|
2016-12-21 07:22:04 +03:00
|
|
|
PRFuncPtr
|
|
|
|
GLLibraryLoader::LookupSymbol(const char* sym)
|
|
|
|
{
|
|
|
|
return LookupSymbol(mLibrary, sym, mLookupFunc);
|
|
|
|
}
|
|
|
|
|
2012-03-17 02:24:12 +04:00
|
|
|
PRFuncPtr
|
2016-06-11 04:37:07 +03:00
|
|
|
GLLibraryLoader::LookupSymbol(PRLibrary* lib,
|
|
|
|
const char* sym,
|
2012-12-19 11:16:02 +04:00
|
|
|
PlatformLookupFunction lookupFunction)
|
2012-03-17 02:24:12 +04:00
|
|
|
{
|
|
|
|
PRFuncPtr res = 0;
|
|
|
|
|
|
|
|
// try finding it in the library directly, if we have one
|
|
|
|
if (lib) {
|
|
|
|
res = PR_FindFunctionSymbol(lib, sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
// then try looking it up via the lookup symbol
|
|
|
|
if (!res && lookupFunction) {
|
|
|
|
res = lookupFunction(sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
// finally just try finding it in the process
|
|
|
|
if (!res) {
|
2016-06-11 04:37:07 +03:00
|
|
|
PRLibrary* leakedLibRef;
|
2012-03-17 02:24:12 +04:00
|
|
|
res = PR_FindFunctionSymbolAndLibrary(sym, &leakedLibRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-06-11 04:37:07 +03:00
|
|
|
GLLibraryLoader::LoadSymbols(PRLibrary* lib,
|
|
|
|
const SymLoadStruct* firstStruct,
|
2012-12-19 11:16:02 +04:00
|
|
|
PlatformLookupFunction lookupFunction,
|
2016-06-11 04:37:07 +03:00
|
|
|
const char* prefix,
|
2012-12-19 11:16:02 +04:00
|
|
|
bool warnOnFailure)
|
2012-03-17 02:24:12 +04:00
|
|
|
{
|
|
|
|
char sbuf[MAX_SYMBOL_LENGTH * 2];
|
|
|
|
int failCount = 0;
|
|
|
|
|
2016-06-11 04:37:07 +03:00
|
|
|
const SymLoadStruct* ss = firstStruct;
|
2012-03-17 02:24:12 +04:00
|
|
|
while (ss->symPointer) {
|
|
|
|
*ss->symPointer = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < MAX_SYMBOL_NAMES; i++) {
|
2012-07-30 18:20:58 +04:00
|
|
|
if (ss->symNames[i] == nullptr)
|
2012-03-17 02:24:12 +04:00
|
|
|
break;
|
|
|
|
|
2016-06-11 04:37:07 +03:00
|
|
|
const char* s = ss->symNames[i];
|
2012-03-17 02:24:12 +04:00
|
|
|
if (prefix && *prefix != 0) {
|
|
|
|
strcpy(sbuf, prefix);
|
|
|
|
strcat(sbuf, ss->symNames[i]);
|
|
|
|
s = sbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRFuncPtr p = LookupSymbol(lib, s, lookupFunction);
|
|
|
|
if (p) {
|
|
|
|
*ss->symPointer = p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*ss->symPointer == 0) {
|
2016-01-19 22:14:27 +03:00
|
|
|
if (warnOnFailure) {
|
2012-12-19 11:16:02 +04:00
|
|
|
printf_stderr("Can't find symbol '%s'.\n", ss->symNames[0]);
|
2016-01-19 22:14:27 +03:00
|
|
|
}
|
2012-12-19 11:16:02 +04:00
|
|
|
|
2012-03-17 02:24:12 +04:00
|
|
|
failCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
ss++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return failCount == 0 ? true : false;
|
|
|
|
}
|
|
|
|
|
2017-03-04 03:49:49 +03:00
|
|
|
/*static*/ void
|
|
|
|
GLLibraryLoader::ClearSymbols(const SymLoadStruct* const firstStruct)
|
|
|
|
{
|
|
|
|
for (auto cur = firstStruct; cur->symPointer; ++cur) {
|
|
|
|
*cur->symPointer = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 02:24:12 +04:00
|
|
|
} /* namespace gl */
|
|
|
|
} /* namespace mozilla */
|
|
|
|
|