platform/android/dlsym-weak: fix code style

This commit is contained in:
mittorn 2023-12-16 20:17:51 +03:00 committed by Alibek Omarov
parent ffe1ff326a
commit 96c367973a
1 changed files with 53 additions and 41 deletions

View File

@ -32,24 +32,26 @@
#include <dlfcn.h> #include <dlfcn.h>
#include "linker.h" #include "linker.h"
static Elf_Sym* soinfo_elf_lookup(soinfo* si, unsigned hash, const char* name) { static Elf_Sym* soinfo_elf_lookup( soinfo* si, unsigned hash, const char* name )
{
Elf_Sym* symtab = si->symtab; Elf_Sym* symtab = si->symtab;
const char* strtab = si->strtab; const char* strtab = si->strtab;
if( si->nbucket == 0 ) if( si->nbucket == 0 )
return NULL; return NULL;
for (unsigned n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]) { for( unsigned n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n] )
{
Elf_Sym* s = symtab + n; Elf_Sym* s = symtab + n;
if (strcmp(strtab + s->st_name, name)) continue; if( strcmp( strtab + s->st_name, name )) continue;
/* only concern ourselves with global and weak symbol definitions */ /* only concern ourselves with global and weak symbol definitions */
switch (ELF_ST_BIND(s->st_info)) { switch( ELF_ST_BIND( s->st_info ))
{
case STB_GLOBAL: case STB_GLOBAL:
case STB_WEAK: case STB_WEAK:
if (s->st_shndx == SHN_UNDEF) { if( s->st_shndx == SHN_UNDEF )
continue; continue;
}
return s; return s;
} }
} }
@ -57,26 +59,30 @@ static Elf_Sym* soinfo_elf_lookup(soinfo* si, unsigned hash, const char* name) {
return NULL; return NULL;
} }
static Elf_Sym* soinfo_elf_lookup_reverse(soinfo* si, size_t addr) { static Elf_Sym* soinfo_elf_lookup_reverse(soinfo* si, size_t addr)
{
Elf_Sym* symtab = si->symtab; Elf_Sym* symtab = si->symtab;
if( si->nbucket == 0 ) if( si->nbucket == 0 )
return NULL; return NULL;
for(int j = 0; j < si->nbucket; j++) for( int j = 0; j < si->nbucket; j++ )
for (unsigned n = si->bucket[j]; n != 0; n = si->chain[n]) { {
Elf_Sym* s = symtab + n; for( unsigned n = si->bucket[j]; n != 0; n = si->chain[n] )
if (s->st_value != addr) continue; {
Elf_Sym* s = symtab + n;
if( s->st_value != addr )continue;
/* only concern ourselves with global and weak symbol definitions */ /* only concern ourselves with global and weak symbol definitions */
switch (ELF_ST_BIND(s->st_info)) { switch( ELF_ST_BIND( s->st_info ))
case STB_GLOBAL: {
case STB_LOCAL: case STB_GLOBAL:
case STB_WEAK: case STB_LOCAL:
if (s->st_shndx == SHN_UNDEF) { case STB_WEAK:
continue; if (s->st_shndx == SHN_UNDEF)
continue;
return s;
} }
return s;
} }
} }
@ -84,11 +90,13 @@ static Elf_Sym* soinfo_elf_lookup_reverse(soinfo* si, size_t addr) {
} }
static unsigned elfhash(const char* _name) { static unsigned elfhash( const char* _name )
const unsigned char* name = (const unsigned char*) _name; {
const unsigned char* name = ( const unsigned char* ) _name;
unsigned h = 0, g; unsigned h = 0, g;
while(*name) { while(*name)
{
h = (h << 4) + *name++; h = (h << 4) + *name++;
g = h & 0xf0000000; g = h & 0xf0000000;
h ^= g; h ^= g;
@ -106,8 +114,9 @@ static unsigned elfhash(const char* _name) {
Binary Interface) where in Chapter 5 it discuss resolving "Shared Binary Interface) where in Chapter 5 it discuss resolving "Shared
Object Dependencies" in breadth first search order. Object Dependencies" in breadth first search order.
*/ */
static Elf_Sym* dlsym_handle_lookup(soinfo* si, const char* name) { static Elf_Sym* dlsym_handle_lookup( soinfo* si, const char* name )
return soinfo_elf_lookup(si, elfhash(name), name); {
return soinfo_elf_lookup( si, elfhash( name ), name );
} }
extern "C" void *ANDROID_LoadLibrary( const char *dllname ); extern "C" void *ANDROID_LoadLibrary( const char *dllname );
@ -117,7 +126,7 @@ static int dladdr_fallback( const void *addr, Dl_info *info )
Elf_Sym *sym; Elf_Sym *sym;
if( !server_info ) if( !server_info )
server_info = (soinfo*)ANDROID_LoadLibrary( "server" ); server_info = ( soinfo* )ANDROID_LoadLibrary( "server" );
if( !server_info ) if( !server_info )
return 0; return 0;
//__android_log_print( ANDROID_LOG_ERROR, "dladdr_fb", "%p %p\n", addr, server_info ); //__android_log_print( ANDROID_LOG_ERROR, "dladdr_fb", "%p %p\n", addr, server_info );
@ -141,35 +150,38 @@ static int dladdr_fallback( const void *addr, Dl_info *info )
extern "C" int __attribute__((visibility("hidden"))) dladdr( const void *addr, Dl_info *info ) extern "C" int __attribute__((visibility("hidden"))) dladdr( const void *addr, Dl_info *info )
{ {
static int (*pDladdr)( const void *addr, Dl_info *info ); typedef int (*PFNDLADDR)( const void *addr, Dl_info *info );
// __android_log_print( ANDROID_LOG_ERROR, "dladdr", "dladdr %p %p %p\n", addr, pDladdr, &dladdr ); PFNDLADDR pfn_dladdr;
if(!pDladdr) if( !pfn_dladdr )
{ {
/* android does not have libdl, but have soinfo record for it from linker
* use dlopen to get this record directly */
void *lib = dlopen( "libdl.so", RTLD_NOW ); void *lib = dlopen( "libdl.so", RTLD_NOW );
if( lib ) if( lib )
*(void**)&pDladdr = dlsym( lib, "dladdr" ); pfn_dladdr = (PFNDLADDR)dlsym( lib, "dladdr" );
if( (void*)pDladdr == (void*)&dladdr ) if( pfn_dladdr == (PFNDLADDR)dladdr )
pDladdr = 0; pfn_dladdr = 0;
if( !pDladdr ) if( !pfn_dladdr )
pDladdr = dladdr_fallback; pfn_dladdr = (PFNDLADDR)dladdr_fallback;
} }
// __android_log_print( ANDROID_LOG_ERROR, "dladdr", "dladdr %p\n", addr );
return pDladdr( addr, info ); return pfn_dladdr( addr, info );
} }
extern "C" void* dlsym_weak(void* handle, const char* symbol) { extern "C" void* dlsym_weak( void* handle, const char* symbol ) {
soinfo* found = NULL; soinfo* found = NULL;
Elf_Sym* sym = NULL; Elf_Sym* sym = NULL;
found = reinterpret_cast<soinfo*>(handle); found = reinterpret_cast<soinfo*>( handle );
sym = dlsym_handle_lookup(found, symbol); sym = dlsym_handle_lookup( found, symbol );
if (sym != NULL) { if ( sym != NULL ) {
return reinterpret_cast<void*>(sym->st_value + found->base/*load_bias*/); return reinterpret_cast<void*>( sym->st_value + found->base /*load_bias*/ );
} }
__android_log_print(ANDROID_LOG_ERROR, "dlsym-weak", "Failed when looking up %s\n", symbol); __android_log_print( ANDROID_LOG_ERROR, "dlsym-weak", "Failed when looking up %s\n", symbol );
return NULL; return NULL;
} }
#endif #endif