Files
xash3d-fwgs/engine/platform/posix/lib_posix.c
2022-06-12 03:08:47 +03:00

367 lines
7.7 KiB
C

/*
lib_posix.c - dynamic library code for POSIX systems
Copyright (C) 2018 Flying With Gauss
This program is free software: you can redistribute it and/sor modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#define _GNU_SOURCE
#include "platform/platform.h"
#if XASH_LIB == LIB_POSIX
#include <dlfcn.h>
#include "common.h"
#include "library.h"
#include "filesystem.h"
#include "server.h"
#include "platform/android/lib_android.h"
#include "platform/emscripten/lib_em.h"
#include "platform/apple/lib_ios.h"
#define ENABLE_REFCOUNT_CHECK
#ifdef ENABLE_REFCOUNT_CHECK
struct dll_t
{
void *hInstance;
char name[256];
char file[256];
int line;
int refcount;
struct dll_t *next;
};
struct dll_t *dlls = NULL;
void *DLL_FindInListAndIncrement( const char *name )
{
struct dll_t *dll;
for( dll = dlls; dll; dll = dll->next )
{
if( !Q_strcmp( name, dll->name ))
{
dll->refcount++;
return dll;
}
}
return NULL;
}
void *DLL_AddToList( const char *name, void *hInstance, const char *file, int line )
{
struct dll_t *newdll;
if( !hInstance ) return NULL;
newdll = DLL_FindInListAndIncrement( name );
if( newdll )
return newdll;
newdll = Mem_Malloc( host.mempool, sizeof( *newdll ));
newdll->hInstance = hInstance;
Q_strncpy( newdll->name, name, sizeof( newdll->name ));
Q_strncpy( newdll->file, file, sizeof( newdll->file ));
newdll->line = line;
newdll->refcount = 1;
newdll->next = dlls;
dlls = newdll;
return newdll;
}
void *DLL_Free( void *_dll )
{
struct dll_t *dll = _dll;
void *hinst = dll->hInstance;
dll->refcount--;
if( dll->refcount == 0 )
{
struct dll_t **back, *find;
back = &dlls;
while( 1 )
{
find = *back;
if( !find ) break;
if( find == dll )
{
*back = dll->next;
Mem_Free( dll );
break;
}
back = &find->next;
}
}
return hinst;
}
void *DLL_GetInstance( void *_dll )
{
struct dll_t *dll = _dll;
return dll->hInstance;
}
void DLL_PrintLeaks( void )
{
struct dll_t *dll;
for( dll = dlls; dll; dll = dll->next )
{
Con_Printf( S_ERROR "UNLOADED LIBRARY DETECTED: %s loaded at %s:%i!\n", dll->name, dll->file, dll->line );
}
}
#else
void *DLL_AddToList( const char *name, void *hInstance, const char *file, int line )
{
return hInstance;
}
void DLL_Free( void *_dll )
{
;
}
void *DLL_GetInstance( void *_dll )
{
return _dll;
}
#endif
#ifdef XASH_DLL_LOADER // wine-based dll loader
void * Loader_LoadLibrary (const char *name);
void * Loader_GetProcAddress (void *hndl, const char *name);
void Loader_FreeLibrary(void *hndl);
void *Loader_GetDllHandle( void *hndl );
const char * Loader_GetFuncName( void *hndl, void *func);
const char * Loader_GetFuncName_int( void *wm , void *func);
#endif
#ifdef XASH_NO_LIBDL
#ifndef XASH_DLL_LOADER
#error Enable at least one dll backend!!!
#endif // XASH_DLL_LOADER
void *dlsym( void *handle, const char *symbol )
{
Con_DPrintf( "dlsym( %p, \"%s\" ): stub\n", handle, symbol );
return NULL;
}
void *dlopen( const char *name, int flag )
{
Con_DPrintf( "dlopen( \"%s\", %d ): stub\n", name, flag );
return NULL;
}
int dlclose( void *handle)
{
Con_DPrintf( "dlsym( %p ): stub\n", handle );
return 0;
}
char *dlerror( void )
{
return "Loading ELF libraries not supported in this build!\n";
}
int dladdr( const void *addr, Dl_info *info )
{
return 0;
}
#endif // XASH_NO_LIBDL
qboolean COM_CheckLibraryDirectDependency( const char *name, const char *depname, qboolean directpath )
{
// TODO: implement
return true;
}
void *_COM_LoadLibrary( const char *dllname, int build_ordinals_table, qboolean directpath, const char *file, int line )
{
dll_user_t *hInst = NULL;
void *pHandle = NULL;
COM_ResetLibraryError();
// platforms where gameinfo mechanism is impossible
#ifdef Platform_POSIX_LoadLibrary
return Platform_POSIX_LoadLibrary( dllname );
#endif
// platforms where gameinfo mechanism is working goes here
// and use FS_FindLibrary
hInst = FS_FindLibrary( dllname, directpath );
if( !hInst )
{
// HACKHACK: direct load dll
#ifdef XASH_DLL_LOADER
if( host.enabledll && ( pHandle = Loader_LoadLibrary(dllname)) )
{
return DLL_AddToList( dllname, pHandle, file, line );
}
#endif
// try to find by linker(LD_LIBRARY_PATH, DYLD_LIBRARY_PATH, LD_32_LIBRARY_PATH and so on...)
if( !pHandle )
{
pHandle = dlopen( dllname, RTLD_LAZY );
if( pHandle )
return DLL_AddToList( dllname, pHandle, file, line );
COM_PushLibraryError( va( "Failed to find library %s", dllname ));
COM_PushLibraryError( dlerror() );
return NULL;
}
}
if( hInst->custom_loader )
{
COM_PushLibraryError( va( "Custom library loader is not available. Extract library %s and fix gameinfo.txt!", hInst->fullPath ));
Mem_Free( hInst );
return NULL;
}
#ifdef XASH_DLL_LOADER
if( host.enabledll && ( !Q_stricmp( COM_FileExtension( hInst->shortPath ), "dll" ) ) )
{
if( hInst->encrypted )
{
COM_PushLibraryError( va( "Library %s is encrypted. Cannot load", hInst->shortPath ) );
Mem_Free( hInst );
return NULL;
}
if( !( hInst->hInstance = Loader_LoadLibrary( hInst->fullPath ) ) )
{
COM_PushLibraryError( va( "Failed to load DLL with DLL loader: %s", hInst->shortPath ) );
Mem_Free( hInst );
return NULL;
}
}
else
#endif
{
if( !( hInst->hInstance = dlopen( hInst->fullPath, RTLD_LAZY ) ) )
{
COM_PushLibraryError( dlerror() );
Mem_Free( hInst );
return NULL;
}
}
pHandle = hInst->hInstance;
Mem_Free( hInst );
return DLL_AddToList( dllname, pHandle, file, line );
}
void COM_FreeLibrary( void *hInstance )
{
void *hinst = DLL_Free( hInstance );
#ifdef XASH_DLL_LOADER
void *wm;
if( host.enabledll && (wm = Loader_GetDllHandle( hinst )) )
return Loader_FreeLibrary( hinst );
else
#endif
{
#ifdef Platform_POSIX_FreeLibrary
Platform_POSIX_FreeLibrary( hinst );
#else
dlclose( hinst );
#endif
}
}
void *COM_GetProcAddress( void *hInstance, const char *name )
{
void *hinst = DLL_GetInstance( hInstance );
#ifdef XASH_DLL_LOADER
void *wm;
if( host.enabledll && ( wm = Loader_GetDllHandle( hinst )))
return Loader_GetProcAddress( hinst, name );
else
#endif
#if Platform_POSIX_GetProcAddress
return Platform_POSIX_GetProcAddress( hinst, name );
#else
return dlsym( hinst, name );
#endif
}
void *COM_FunctionFromName( void *hInstance, const char *pName )
{
void *function;
if( !( function = COM_GetProcAddress( hInstance, pName ) ) )
{
Con_Reportf( S_ERROR "FunctionFromName: Can't get symbol %s: %s\n", pName, dlerror());
}
return function;
}
#ifdef XASH_DYNAMIC_DLADDR
static int d_dladdr( void *sym, Dl_info *info )
{
static int (*dladdr_real) ( void *sym, Dl_info *info );
if( !dladdr_real )
dladdr_real = dlsym( (void*)(size_t)(-1), "dladdr" );
memset( info, 0, sizeof( *info ) );
if( !dladdr_real )
return -1;
return dladdr_real( sym, info );
}
#define dladdr d_dladdr
#endif
const char *COM_NameForFunction( void *hInstance, void *function )
{
void *hinst = DLL_GetInstance( hInstance );
#ifdef XASH_DLL_LOADER
void *wm;
if( host.enabledll && ( wm = Loader_GetDllHandle( hinst )))
#error ConvertMangledName
return Loader_GetFuncName_int( wm, function );
else
#endif
// NOTE: dladdr() is a glibc extension
{
Dl_info info = {0};
dladdr( (void*)function, &info );
if( info.dli_sname )
return COM_GetPlatformNeutralName( info.dli_sname );
}
#ifdef XASH_ALLOW_SAVERESTORE_OFFSETS
return COM_OffsetNameForFunction( function );
#else
return NULL;
#endif
}
#endif // _WIN32