engine: platform: android: reformat dlsym-weak and make it C only

This commit is contained in:
Alibek Omarov 2024-10-24 02:04:52 +03:00
parent 3d9eb869b0
commit a9fdae28b2

View file

@ -31,42 +31,50 @@
#include <android/log.h> #include <android/log.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( const soinfo *si, unsigned hash, const char *name )
Elf_Sym* symtab = si->symtab; {
const char* strtab = si->strtab; const Elf_Sym *symtab = si->symtab;
const char *strtab = si->strtab;
unsigned n;
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( n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n] )
Elf_Sym* s = symtab + n; {
if (strcmp(strtab + s->st_name, name)) continue; const Elf_Sym *s = symtab + n;
/* only concern ourselves with global and weak symbol definitions */ if( strcmp( strtab + s->st_name, name ))
switch (ELF_ST_BIND(s->st_info)) { continue;
case STB_GLOBAL:
case STB_WEAK:
if (s->st_shndx == SHN_UNDEF) {
continue;
}
return s;
}
}
return NULL; /* only concern ourselves with global and weak symbol definitions */
switch( ELF_ST_BIND( s->st_info ))
{
case STB_GLOBAL:
case STB_WEAK:
if( s->st_shndx == SHN_UNDEF )
continue;
return s;
}
}
return NULL;
} }
static unsigned elfhash(const char* _name) { static unsigned elfhash( const unsigned char *name )
const unsigned char* name = (const unsigned char*) _name; {
unsigned h = 0, g; unsigned h = 0;
while(*name) { while( *name )
h = (h << 4) + *name++; {
g = h & 0xf0000000; unsigned g;
h ^= g;
h ^= g >> 24; h = ( h << 4 ) + *name++;
} g = h & 0xf0000000;
return h; h ^= g;
h ^= g >> 24;
}
return h;
} }
/* This is used by dlsym(3). It performs symbol lookup only within the /* This is used by dlsym(3). It performs symbol lookup only within the
@ -78,21 +86,20 @@ 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( const soinfo *si, const char *name )
return soinfo_elf_lookup(si, elfhash(name), name); {
return soinfo_elf_lookup( si, elfhash((const unsigned char *)name ), name );
} }
extern "C" void* dlsym_weak(void* handle, const char* symbol) { void *dlsym_weak( void *handle, const char *symbol )
{
soinfo *found = (soinfo *)handle;
Elf_Sym *sym = dlsym_handle_lookup( found, symbol );
soinfo* found = NULL; if( sym != NULL )
Elf_Sym* sym = NULL; return (void*)( sym->st_value + found->base/*load_bias*/ );
found = reinterpret_cast<soinfo*>(handle);
sym = dlsym_handle_lookup(found, symbol);
if (sym != NULL) { __android_log_print( ANDROID_LOG_ERROR, "dlsym-weak", "Failed when looking up %s\n", symbol );
return reinterpret_cast<void*>(sym->st_value + found->base/*load_bias*/); return NULL;
}
__android_log_print(ANDROID_LOG_ERROR, "dlsym-weak", "Failed when looking up %s\n", symbol);
return NULL;
} }
#endif #endif