engine: platform: android: make dlsym-weak C++ again (because of struct soinfo definition). Reformat linker.h file as well.

This commit is contained in:
Alibek Omarov 2024-10-24 17:54:09 +03:00
parent 128a1f59a9
commit 224e42d345
3 changed files with 98 additions and 109 deletions

View file

@ -35,8 +35,8 @@
static Elf_Sym *soinfo_elf_lookup( const soinfo *si, unsigned hash, const char *name ) static Elf_Sym *soinfo_elf_lookup( const soinfo *si, unsigned hash, const char *name )
{ {
const Elf_Sym *symtab = si->symtab; const Elf_Sym *symtab = si->symtab;
const char *strtab = si->strtab; const char *strtab = si->strtab;
unsigned n; unsigned n;
if( si->nbucket == 0 ) if( si->nbucket == 0 )
return NULL; return NULL;
@ -92,13 +92,13 @@ static Elf_Sym *dlsym_handle_lookup( const soinfo *si, const char *name )
return soinfo_elf_lookup( si, elfhash((const unsigned char *)name ), name ); return soinfo_elf_lookup( si, elfhash((const unsigned char *)name ), name );
} }
void *dlsym_weak( void *handle, const char *symbol ) extern "C" void *dlsym_weak( void *handle, const char *symbol )
{ {
soinfo *found = (soinfo *)handle; soinfo *found = (soinfo *)handle;
Elf_Sym *sym = dlsym_handle_lookup( found, symbol ); Elf_Sym *sym = dlsym_handle_lookup( found, symbol );
if( sym != NULL ) if( sym != NULL )
return (void*)( sym->st_value + found->base/*load_bias*/ ); return (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;

View file

@ -28,7 +28,6 @@
#ifndef _LINKER_H_ #ifndef _LINKER_H_
#define _LINKER_H_ #define _LINKER_H_
#ifdef __ANDROID__
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
@ -39,163 +38,153 @@
// a1ba: we don't really need custom linker on Android 64, because // a1ba: we don't really need custom linker on Android 64, because
// it's only intended to workaround bug which persist on Android < 4.4 // it's only intended to workaround bug which persist on Android < 4.4
#define Elf_Ehdr Elf32_Ehdr #define Elf_Ehdr Elf32_Ehdr
#define Elf_Phdr Elf32_Phdr #define Elf_Phdr Elf32_Phdr
#define Elf_Shdr Elf32_Shdr #define Elf_Shdr Elf32_Shdr
#define Elf_Sym Elf32_Sym #define Elf_Sym Elf32_Sym
#define Elf_Rel Elf32_Rel #define Elf_Rel Elf32_Rel
#define Elf_RelA Elf32_Rela #define Elf_RelA Elf32_Rela
#define Elf_Dyn Elf32_Dyn #define Elf_Dyn Elf32_Dyn
#define Elf_Half Elf32_Half #define Elf_Half Elf32_Half
#define Elf_Word Elf32_Word #define Elf_Word Elf32_Word
#define Elf_Sword Elf32_Sword #define Elf_Sword Elf32_Sword
#define Elf_Addr Elf32_Addr #define Elf_Addr Elf32_Addr
#define Elf_Off Elf32_Off #define Elf_Off Elf32_Off
#define Elf_Nhdr Elf32_Nhdr #define Elf_Nhdr Elf32_Nhdr
#define Elf_Note Elf32_Note #define Elf_Note Elf32_Note
// Returns the address of the page containing address 'x'. // Returns the address of the page containing address 'x'.
#define PAGE_START(x) ((x) & PAGE_MASK) #define PAGE_START( x ) (( x )&PAGE_MASK )
// Returns the offset of address 'x' in its page. // Returns the offset of address 'x' in its page.
#define PAGE_OFFSET(x) ((x) & ~PAGE_MASK) #define PAGE_OFFSET( x ) (( x ) & ~PAGE_MASK )
// Returns the address of the next page after address 'x', unless 'x' is // Returns the address of the next page after address 'x', unless 'x' is
// itself at the start of a page. // itself at the start of a page.
#define PAGE_END(x) PAGE_START((x) + (PAGE_SIZE-1)) #define PAGE_END( x ) PAGE_START(( x ) + ( PAGE_SIZE - 1 ))
// Magic shared structures that GDB knows about. // Magic shared structures that GDB knows about.
struct link_map_t { struct link_map_t {
uintptr_t l_addr; uintptr_t l_addr;
char* l_name; char *l_name;
uintptr_t l_ld; uintptr_t l_ld;
link_map_t* l_next; link_map_t *l_next;
link_map_t* l_prev; link_map_t *l_prev;
}; };
// Values for r_debug->state // Values for r_debug->state
enum { enum {
RT_CONSISTENT, RT_CONSISTENT,
RT_ADD, RT_ADD,
RT_DELETE RT_DELETE
}; };
#if 0 #define FLAG_LINKED 0x00000001
struct r_debug { #define FLAG_EXE 0x00000004 // The main executable
int32_t r_version; #define FLAG_LINKER 0x00000010 // The linker itself
link_map_t* r_map;
void (*r_brk)(void);
int32_t r_state;
uintptr_t r_ldbase;
};
#endif
#define FLAG_LINKED 0x00000001
#define FLAG_EXE 0x00000004 // The main executable
#define FLAG_LINKER 0x00000010 // The linker itself
#define SOINFO_NAME_LEN 128 #define SOINFO_NAME_LEN 128
typedef void (*linker_function_t)(); typedef void (*linker_function_t)();
// Android uses REL for 32-bit but only uses RELA for 64-bit. // Android uses REL for 32-bit but only uses RELA for 64-bit.
#if defined(__LP64__) #if defined( __LP64__ )
#define USE_RELA 1 #define USE_RELA 1
#endif #endif
struct soinfo { struct soinfo {
public: public:
char name[SOINFO_NAME_LEN]; char name[SOINFO_NAME_LEN];
const Elf_Phdr* phdr; const Elf_Phdr *phdr;
size_t phnum; size_t phnum;
Elf_Addr entry; Elf_Addr entry;
Elf_Addr base; Elf_Addr base;
unsigned size; unsigned size;
#ifndef __LP64__ #ifndef __LP64__
uint32_t unused1; // DO NOT USE, maintained for compatibility. uint32_t unused1; // DO NOT USE, maintained for compatibility.
#endif #endif
Elf_Dyn* dynamic; Elf_Dyn *dynamic;
#ifndef __LP64__ #ifndef __LP64__
uint32_t unused2; // DO NOT USE, maintained for compatibility uint32_t unused2; // DO NOT USE, maintained for compatibility
uint32_t unused3; // DO NOT USE, maintained for compatibility uint32_t unused3; // DO NOT USE, maintained for compatibility
#endif #endif
soinfo* next; soinfo *next;
unsigned flags; unsigned flags;
const char* strtab; const char *strtab;
Elf_Sym* symtab; Elf_Sym *symtab;
size_t nbucket; size_t nbucket;
size_t nchain; size_t nchain;
unsigned* bucket; unsigned *bucket;
unsigned* chain; unsigned *chain;
#if !defined(__LP64__) #if !defined( __LP64__ )
// This is only used by 32-bit MIPS, but needs to be here for // This is only used by 32-bit MIPS, but needs to be here for
// all 32-bit architectures to preserve binary compatibility. // all 32-bit architectures to preserve binary compatibility.
unsigned* plt_got; unsigned *plt_got;
#endif #endif
#if defined(USE_RELA) #if defined( USE_RELA )
Elf_RelA* plt_rela; Elf_RelA *plt_rela;
size_t plt_rela_count; size_t plt_rela_count;
Elf_RelA* rela; Elf_RelA *rela;
size_t rela_count; size_t rela_count;
#else #else
Elf_Rel* plt_rel; Elf_Rel *plt_rel;
size_t plt_rel_count; size_t plt_rel_count;
Elf_Rel* rel; Elf_Rel *rel;
size_t rel_count; size_t rel_count;
#endif #endif
linker_function_t* preinit_array; linker_function_t *preinit_array;
size_t preinit_array_count; size_t preinit_array_count;
linker_function_t* init_array; linker_function_t *init_array;
size_t init_array_count; size_t init_array_count;
linker_function_t* fini_array; linker_function_t *fini_array;
size_t fini_array_count; size_t fini_array_count;
linker_function_t init_func; linker_function_t init_func;
linker_function_t fini_func; linker_function_t fini_func;
#if defined(__arm__) #if defined( __arm__ )
// ARM EABI section used for stack unwinding. // ARM EABI section used for stack unwinding.
unsigned* ARM_exidx; unsigned *ARM_exidx;
size_t ARM_exidx_count; size_t ARM_exidx_count;
#elif defined(__mips__) #elif defined( __mips__ )
unsigned mips_symtabno; unsigned mips_symtabno;
unsigned mips_local_gotno; unsigned mips_local_gotno;
unsigned mips_gotsym; unsigned mips_gotsym;
#endif #endif
size_t ref_count; size_t ref_count;
link_map_t link_map; link_map_t link_map;
bool constructors_called; bool constructors_called;
// When you read a virtual address from the ELF file, add this // When you read a virtual address from the ELF file, add this
// value to get the corresponding address in the process' address space. // value to get the corresponding address in the process' address space.
Elf_Addr load_bias; Elf_Addr load_bias;
#if !defined(__LP64__) #if !defined( __LP64__ )
bool has_text_relocations; bool has_text_relocations;
#endif #endif
bool has_DT_SYMBOLIC; bool has_DT_SYMBOLIC;
void CallConstructors(); void CallConstructors();
void CallDestructors(); void CallDestructors();
void CallPreInitConstructors(); void CallPreInitConstructors();
private: private:
void CallArray(const char* array_name, linker_function_t* functions, size_t count, bool reverse); void CallArray( const char *array_name, linker_function_t *functions, size_t count, bool reverse );
void CallFunction(const char* function_name, linker_function_t function); void CallFunction( const char *function_name, linker_function_t function );
}; };
#endif #endif
#endif

View file

@ -191,7 +191,7 @@ def build(bld):
if bld.env.DEST_OS == 'android': if bld.env.DEST_OS == 'android':
libs += ['LOG'] libs += ['LOG']
source += bld.path.ant_glob(['platform/android/*.c', 'platform/linux/*.c']) source += bld.path.ant_glob(['platform/android/*.cpp', 'platform/android/*.c', 'platform/linux/*.c'])
if bld.env.DEST_OS == 'nswitch': if bld.env.DEST_OS == 'nswitch':
libs += [ 'SOLDER' ] libs += [ 'SOLDER' ]