From 5092b5ac7ff8466baad7da747ed4dc6b144e2e7a Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 11 Jan 2025 06:53:21 +1100 Subject: [PATCH] Show better error messages for when required symbols aren't found --- misc/windows/min-kernel32.h | 8 ++++---- misc/windows/min-wincrypt.h | 2 +- misc/windows/min-winsock2.h | 18 +++++++++--------- misc/windows/min-winuser.h | 6 +++--- src/AudioBackend.c | 28 ++++++++++++++-------------- src/Graphics_D3D11.c | 2 +- src/Graphics_D3D9.c | 2 +- src/Graphics_GL1.c | 12 ++++++------ src/Http_Worker.c | 23 ++++++++++++----------- src/Platform.h | 17 +++++++++++------ src/Platform_Windows.c | 2 +- src/SSL.c | 2 +- src/_PlatformBase.h | 13 ++++++++++--- 13 files changed, 74 insertions(+), 61 deletions(-) diff --git a/misc/windows/min-kernel32.h b/misc/windows/min-kernel32.h index a5559cb45..2f0c342e2 100644 --- a/misc/windows/min-kernel32.h +++ b/misc/windows/min-kernel32.h @@ -17,10 +17,10 @@ CC_KERN32_FUNC void (NTAPI *_RtlCaptureContext)(CONTEXT* ContextRecord); static void Kernel32_LoadDynamicFuncs(void) { static const struct DynamicLibSym funcs[] = { - DynamicLib_Sym(AttachConsole), - DynamicLib_Sym(IsDebuggerPresent), - DynamicLib_Sym(GetSystemTimeAsFileTime), - DynamicLib_Sym(RtlCaptureContext) + DynamicLib_OptSym(AttachConsole), + DynamicLib_OptSym(IsDebuggerPresent), + DynamicLib_OptSym(GetSystemTimeAsFileTime), + DynamicLib_OptSym(RtlCaptureContext) }; static const cc_string kernel32 = String_FromConst("KERNEL32.DLL"); diff --git a/misc/windows/min-wincrypt.h b/misc/windows/min-wincrypt.h index 50ae350b0..715ee0f7a 100644 --- a/misc/windows/min-wincrypt.h +++ b/misc/windows/min-wincrypt.h @@ -12,7 +12,7 @@ CC_CRYPT32_FUNC BOOL (WINAPI *_CryptUnprotectData)(DATA_BLOB* dataIn, PWSTR* dat static void Crypt32_LoadDynamicFuncs(void) { static const struct DynamicLibSym funcs[] = { - DynamicLib_Sym(CryptProtectData), DynamicLib_Sym(CryptUnprotectData) + DynamicLib_OptSym(CryptProtectData), DynamicLib_OptSym(CryptUnprotectData) }; static const cc_string crypt32 = String_FromConst("CRYPT32.DLL"); diff --git a/misc/windows/min-winsock2.h b/misc/windows/min-winsock2.h index cb3d2427e..1b9768f61 100644 --- a/misc/windows/min-winsock2.h +++ b/misc/windows/min-winsock2.h @@ -147,14 +147,14 @@ CC_WINSOCK_FUNC void (WINAPI* _freeaddrinfo)(ADDRINFOA* addrInfo); static void Winsock_LoadDynamicFuncs(void) { static const struct DynamicLibSym funcs[] = { - DynamicLib_Sym(WSAStartup), DynamicLib_Sym(WSACleanup), - DynamicLib_Sym(WSAGetLastError), DynamicLib_Sym(WSAStringToAddressW), - DynamicLib_Sym(socket), DynamicLib_Sym(closesocket), - DynamicLib_Sym(connect), DynamicLib_Sym(shutdown), - DynamicLib_Sym(ioctlsocket), DynamicLib_Sym(getsockopt), - DynamicLib_Sym(gethostbyname), DynamicLib_Sym(htons), - DynamicLib_Sym(getaddrinfo), DynamicLib_Sym(freeaddrinfo), - DynamicLib_Sym(recv), DynamicLib_Sym(send), DynamicLib_Sym(select) + DynamicLib_ReqSym(WSAStartup), DynamicLib_ReqSym(WSACleanup), + DynamicLib_ReqSym(WSAGetLastError), DynamicLib_OptSym(WSAStringToAddressW), + DynamicLib_ReqSym(socket), DynamicLib_ReqSym(closesocket), + DynamicLib_ReqSym(connect), DynamicLib_ReqSym(shutdown), + DynamicLib_ReqSym(ioctlsocket), DynamicLib_ReqSym(getsockopt), + DynamicLib_ReqSym(gethostbyname), DynamicLib_ReqSym(htons), + DynamicLib_OptSym(getaddrinfo), DynamicLib_OptSym(freeaddrinfo), + DynamicLib_ReqSym(recv), DynamicLib_ReqSym(send), DynamicLib_ReqSym(select) }; static const cc_string winsock1 = String_FromConst("wsock32.DLL"); static const cc_string winsock2 = String_FromConst("WS2_32.DLL"); @@ -162,5 +162,5 @@ static void Winsock_LoadDynamicFuncs(void) { DynamicLib_LoadAll(&winsock2, funcs, Array_Elems(funcs), &lib); /* Windows 95 is missing WS2_32 dll */ - if (!_WSAStartup) DynamicLib_LoadAll(&winsock1, funcs, Array_Elems(funcs), &lib); + if (!lib) DynamicLib_LoadAll(&winsock1, funcs, Array_Elems(funcs), &lib); } diff --git a/misc/windows/min-winuser.h b/misc/windows/min-winuser.h index 016600c66..ac8d6a946 100644 --- a/misc/windows/min-winuser.h +++ b/misc/windows/min-winuser.h @@ -47,9 +47,9 @@ CC_USER32_FUNC BOOL (WINAPI *_SetProcessDPIAware)(void); static void User32_LoadDynamicFuncs(void) { static const struct DynamicLibSym funcs[] = { - DynamicLib_Sym(RegisterRawInputDevices), - DynamicLib_Sym(GetRawInputData), - DynamicLib_Sym(SetProcessDPIAware) + DynamicLib_OptSym(RegisterRawInputDevices), + DynamicLib_OptSym(GetRawInputData), + DynamicLib_OptSym(SetProcessDPIAware) }; static const cc_string user32 = String_FromConst("USER32.DLL"); void* lib; diff --git a/src/AudioBackend.c b/src/AudioBackend.c index ee13b7926..d91375acd 100644 --- a/src/AudioBackend.c +++ b/src/AudioBackend.c @@ -105,17 +105,17 @@ static const cc_string alLib = String_FromConst("libopenal.so.1"); static cc_bool LoadALFuncs(void) { static const struct DynamicLibSym funcs[] = { - DynamicLib_Sym(alcCreateContext), DynamicLib_Sym(alcMakeContextCurrent), - DynamicLib_Sym(alcDestroyContext), DynamicLib_Sym(alcOpenDevice), - DynamicLib_Sym(alcCloseDevice), DynamicLib_Sym(alcGetError), + DynamicLib_ReqSym(alcCreateContext), DynamicLib_ReqSym(alcMakeContextCurrent), + DynamicLib_ReqSym(alcDestroyContext), DynamicLib_ReqSym(alcOpenDevice), + DynamicLib_ReqSym(alcCloseDevice), DynamicLib_ReqSym(alcGetError), - DynamicLib_Sym(alGetError), - DynamicLib_Sym(alGenSources), DynamicLib_Sym(alDeleteSources), - DynamicLib_Sym(alGetSourcei), DynamicLib_Sym(alSourcef), - DynamicLib_Sym(alSourcePlay), DynamicLib_Sym(alSourceStop), - DynamicLib_Sym(alSourceQueueBuffers), DynamicLib_Sym(alSourceUnqueueBuffers), - DynamicLib_Sym(alGenBuffers), DynamicLib_Sym(alDeleteBuffers), - DynamicLib_Sym(alBufferData), DynamicLib_Sym(alDistanceModel) + DynamicLib_ReqSym(alGetError), + DynamicLib_ReqSym(alGenSources), DynamicLib_ReqSym(alDeleteSources), + DynamicLib_ReqSym(alGetSourcei), DynamicLib_ReqSym(alSourcef), + DynamicLib_ReqSym(alSourcePlay), DynamicLib_ReqSym(alSourceStop), + DynamicLib_ReqSym(alSourceQueueBuffers), DynamicLib_ReqSym(alSourceUnqueueBuffers), + DynamicLib_ReqSym(alGenBuffers), DynamicLib_ReqSym(alDeleteBuffers), + DynamicLib_ReqSym(alBufferData), DynamicLib_ReqSym(alDistanceModel) }; void* lib; @@ -499,10 +499,10 @@ static const cc_string slLib = String_FromConst("libOpenSLES.so"); static cc_bool LoadSLFuncs(void) { static const struct DynamicLibSym funcs[] = { - DynamicLib_Sym(slCreateEngine), DynamicLib_Sym(SL_IID_NULL), - DynamicLib_Sym(SL_IID_PLAY), DynamicLib_Sym(SL_IID_ENGINE), - DynamicLib_Sym(SL_IID_BUFFERQUEUE), DynamicLib_Sym(SL_IID_PLAYBACKRATE), - DynamicLib_Sym(SL_IID_VOLUME) + DynamicLib_ReqSym(slCreateEngine), DynamicLib_ReqSym(SL_IID_NULL), + DynamicLib_ReqSym(SL_IID_PLAY), DynamicLib_ReqSym(SL_IID_ENGINE), + DynamicLib_ReqSym(SL_IID_BUFFERQUEUE), DynamicLib_ReqSym(SL_IID_PLAYBACKRATE), + DynamicLib_ReqSym(SL_IID_VOLUME) }; void* lib; diff --git a/src/Graphics_D3D11.c b/src/Graphics_D3D11.c index a263dbd05..4eca4fc95 100644 --- a/src/Graphics_D3D11.c +++ b/src/Graphics_D3D11.c @@ -107,7 +107,7 @@ static PFN_D3D11_CREATE_DEVICE _D3D11CreateDevice; static void LoadD3D11Library(void) { static const struct DynamicLibSym funcs[] = { - DynamicLib_Sym(D3D11CreateDevice) + DynamicLib_ReqSym(D3D11CreateDevice) }; static const cc_string path = String_FromConst("d3d11.dll"); void* lib; diff --git a/src/Graphics_D3D9.c b/src/Graphics_D3D9.c index cb65e2e9d..df361065f 100644 --- a/src/Graphics_D3D9.c +++ b/src/Graphics_D3D9.c @@ -53,7 +53,7 @@ static IDirect3D9* (WINAPI *_Direct3DCreate9)(UINT SDKVersion); static void LoadD3D9Library(void) { static const struct DynamicLibSym funcs[] = { - DynamicLib_Sym(Direct3DCreate9) + DynamicLib_ReqSym(Direct3DCreate9) }; static const cc_string path = String_FromConst("d3d9.dll"); void* lib; diff --git a/src/Graphics_GL1.c b/src/Graphics_GL1.c index dd7826537..b0faf9b46 100644 --- a/src/Graphics_GL1.c +++ b/src/Graphics_GL1.c @@ -711,14 +711,14 @@ static void FallbackOpenGL(void) { static void GLBackend_Init(void) { static const struct DynamicLibSym coreVboFuncs[] = { - DynamicLib_Sym2("glBindBuffer", glBindBuffer), DynamicLib_Sym2("glDeleteBuffers", glDeleteBuffers), - DynamicLib_Sym2("glGenBuffers", glGenBuffers), DynamicLib_Sym2("glBufferData", glBufferData), - DynamicLib_Sym2("glBufferSubData", glBufferSubData) + DynamicLib_ReqSym2("glBindBuffer", glBindBuffer), DynamicLib_ReqSym2("glDeleteBuffers", glDeleteBuffers), + DynamicLib_ReqSym2("glGenBuffers", glGenBuffers), DynamicLib_ReqSym2("glBufferData", glBufferData), + DynamicLib_ReqSym2("glBufferSubData", glBufferSubData) }; static const struct DynamicLibSym arbVboFuncs[] = { - DynamicLib_Sym2("glBindBufferARB", glBindBuffer), DynamicLib_Sym2("glDeleteBuffersARB", glDeleteBuffers), - DynamicLib_Sym2("glGenBuffersARB", glGenBuffers), DynamicLib_Sym2("glBufferDataARB", glBufferData), - DynamicLib_Sym2("glBufferSubDataARB", glBufferSubData) + DynamicLib_ReqSym2("glBindBufferARB", glBindBuffer), DynamicLib_ReqSym2("glDeleteBuffersARB", glDeleteBuffers), + DynamicLib_ReqSym2("glGenBuffersARB", glGenBuffers), DynamicLib_ReqSym2("glBufferDataARB", glBufferData), + DynamicLib_ReqSym2("glBufferSubDataARB", glBufferSubData) }; static const cc_string vboExt = String_FromConst("GL_ARB_vertex_buffer_object"); cc_string extensions = String_FromReadonly((const char*)glGetString(GL_EXTENSIONS)); diff --git a/src/Http_Worker.c b/src/Http_Worker.c index 2b91f71d2..c6de77cf5 100644 --- a/src/Http_Worker.c +++ b/src/Http_Worker.c @@ -204,15 +204,19 @@ static const cc_string curlAlt = String_FromConst("libcurl.so.3"); static cc_bool LoadCurlFuncs(void) { static const struct DynamicLibSym funcs[] = { #if !defined CC_BUILD_OS2 - DynamicLib_Sym(curl_global_init), DynamicLib_Sym(curl_global_cleanup), - DynamicLib_Sym(curl_easy_init), DynamicLib_Sym(curl_easy_perform), - DynamicLib_Sym(curl_easy_setopt), DynamicLib_Sym(curl_easy_cleanup), - DynamicLib_Sym(curl_slist_free_all), DynamicLib_Sym(curl_slist_append) + DynamicLib_ReqSym(curl_global_init), DynamicLib_ReqSym(curl_global_cleanup), + DynamicLib_ReqSym(curl_easy_init), DynamicLib_ReqSym(curl_easy_perform), + DynamicLib_ReqSym(curl_easy_setopt), DynamicLib_ReqSym(curl_easy_cleanup), + DynamicLib_ReqSym(curl_slist_free_all), DynamicLib_ReqSym(curl_slist_append), + /* Non-essential function missing in older curl versions */ + DynamicLib_OptSym(curl_easy_strerror) #else - DynamicLib_SymC(curl_global_init), DynamicLib_SymC(curl_global_cleanup), - DynamicLib_SymC(curl_easy_init), DynamicLib_SymC(curl_easy_perform), - DynamicLib_SymC(curl_easy_setopt), DynamicLib_SymC(curl_easy_cleanup), - DynamicLib_SymC(curl_slist_free_all), DynamicLib_SymC(curl_slist_append) + DynamicLib_ReqSymC(curl_global_init), DynamicLib_ReqSymC(curl_global_cleanup), + DynamicLib_ReqSymC(curl_easy_init), DynamicLib_ReqSymC(curl_easy_perform), + DynamicLib_ReqSymC(curl_easy_setopt), DynamicLib_ReqSymC(curl_easy_cleanup), + DynamicLib_ReqSymC(curl_slist_free_all), DynamicLib_ReqSymC(curl_slist_append), + /* Non-essential function missing in older curl versions */ + DynamicLib_OptSymC(curl_easy_strerror) #endif }; cc_bool success; @@ -222,9 +226,6 @@ static cc_bool LoadCurlFuncs(void) { if (!lib) { success = DynamicLib_LoadAll(&curlAlt, funcs, Array_Elems(funcs), &lib); } - - /* Non-essential function missing in older curl versions */ - _curl_easy_strerror = DynamicLib_Get2(lib, "curl_easy_strerror"); return success; } diff --git a/src/Platform.h b/src/Platform.h index a884a2379..ae5470539 100644 --- a/src/Platform.h +++ b/src/Platform.h @@ -128,18 +128,23 @@ CC_API cc_result DynamicLib_Load(const cc_string* path, void** lib); /* OBSOLETE CC_API cc_result DynamicLib_Get(void* lib, const char* name, void** symbol); /* OBSOLETE */ #define DYNAMICLIB_QUOTE(x) #x -#define DynamicLib_Sym(sym) { DYNAMICLIB_QUOTE(sym), (void**)&_ ## sym } -#define DynamicLib_Sym2(name, sym) { name, (void**)&_ ## sym } +#define DynamicLib_ReqSym(sym) { DYNAMICLIB_QUOTE(sym), (void**)&_ ## sym, true } +#define DynamicLib_OptSym(sym) { DYNAMICLIB_QUOTE(sym), (void**)&_ ## sym, false } + +#define DynamicLib_ReqSym2(name, sym) { name, (void**)&_ ## sym, true } +#define DynamicLib_OptSym2(name, sym) { name, (void**)&_ ## sym, false } + #if defined CC_BUILD_OS2 -#define DynamicLib_SymC(sym) { DYNAMICLIB_QUOTE(_ ## sym), (void**)&_ ## sym } +#define DynamicLib_ReqSymC(sym) { DYNAMICLIB_QUOTE(_ ## sym), (void**)&_ ## sym, true } +#define DynamicLib_OptSymC(sym) { DYNAMICLIB_QUOTE(_ ## sym), (void**)&_ ## sym, false } #endif /* Contains a name and a pointer to variable that will hold the loaded symbol */ /* static int (APIENTRY *_myGetError)(void); --- (for example) */ -/* static struct DynamicLibSym sym = { "myGetError", (void**)&_myGetError }; */ -struct DynamicLibSym { const char* name; void** symAddr; }; +/* static struct DynamicLibSym sym = { "myGetError", (void**)&_myGetError, true }; */ +struct DynamicLibSym { const char* name; void** symAddr; cc_bool required; }; /* Loads all symbols using DynamicLib_Get2 in the given list */ -/* Returns true if all symbols were successfully retrieved */ +/* Returns true if all required symbols were successfully retrieved */ cc_bool DynamicLib_LoadAll(const cc_string* path, const struct DynamicLibSym* syms, int count, void** lib); diff --git a/src/Platform_Windows.c b/src/Platform_Windows.c index 4e995a581..c5ff6afe6 100644 --- a/src/Platform_Windows.c +++ b/src/Platform_Windows.c @@ -1085,7 +1085,7 @@ static BOOL (WINAPI *_RtlGenRandom)(PVOID data, ULONG len); cc_result Platform_GetEntropy(void* data, int len) { static const struct DynamicLibSym funcs[] = { - DynamicLib_Sym2("SystemFunction036", RtlGenRandom) + DynamicLib_ReqSym2("SystemFunction036", RtlGenRandom) }; if (!_RtlGenRandom) { diff --git a/src/SSL.c b/src/SSL.c index 77cf9b3ac..37bd5dc0f 100644 --- a/src/SSL.c +++ b/src/SSL.c @@ -48,7 +48,7 @@ void SSLBackend_Init(cc_bool verifyCerts) { /* then it DOES work. (and on later Windows versions, those functions */ /* exported from schannel.dll are just DLL forwards to secur32.dll */ static const struct DynamicLibSym funcs[] = { - DynamicLib_Sym(InitSecurityInterfaceA) + DynamicLib_ReqSym(InitSecurityInterfaceA) }; static const cc_string schannel = String_FromConst("schannel.dll"); _verifyCerts = verifyCerts; diff --git a/src/_PlatformBase.h b/src/_PlatformBase.h index b8376e892..697af0fe2 100644 --- a/src/_PlatformBase.h +++ b/src/_PlatformBase.h @@ -157,7 +157,9 @@ cc_result DynamicLib_Get(void* lib, const char* name, void** symbol) { cc_bool DynamicLib_LoadAll(const cc_string* path, const struct DynamicLibSym* syms, int count, void** _lib) { - int i, loaded = 0; + cc_bool foundAllRequired = true; + cc_string symName; + int i; void* addr; void* lib; @@ -167,8 +169,13 @@ cc_bool DynamicLib_LoadAll(const cc_string* path, const struct DynamicLibSym* sy for (i = 0; i < count; i++) { addr = DynamicLib_Get2(lib, syms[i].name); - if (addr) loaded++; *syms[i].symAddr = addr; + + if (addr || !syms[i].required) continue; + symName = String_FromReadonly(syms[i].name); + + Logger_DynamicLibWarn("loading symbol", &symName); + foundAllRequired = false; } - return loaded == count; + return foundAllRequired; }