11#include "Relocs.h"
22#include "Symbol.h"
33
4+ #include <string.h> // strcmp
5+
46typedef struct {
57 CTRDLHandle * handle ;
68 CTRDLElf * elf ;
@@ -23,6 +25,7 @@ static u32 ctrdl_resolveSymbol(const RelContext* ctx, Elf32_Word index, bool* is
2325 return 0 ;
2426 }
2527
28+ u32 symBase = 0 ;
2629 const Elf32_Sym * symEntry = & ctx -> elf -> symEntries [index ];
2730 const char * name = & ctx -> elf -> stringTable [symEntry -> st_name ];
2831 * isWeak = ELF32_ST_BIND (symEntry -> st_info ) == STB_WEAK ;
@@ -47,20 +50,40 @@ static u32 ctrdl_resolveSymbol(const RelContext* ctx, Elf32_Word index, bool* is
4750 CTRDLHandle * h = ctrdl_unsafeGetHandleByIndex (i );
4851 if (h -> flags & RTLD_GLOBAL ) {
4952 sym = ctrdl_symNameLookupSingle (h , name );
50- if (sym )
53+ if (sym ) {
54+ symBase = h -> base ;
5155 break ;
56+ }
5257 }
5358 }
5459
5560 ctrdl_releaseHandleMtx ();
5661
62+ if (!sym ) {
63+ // Look into ourselves.
64+ if (ctx -> elf -> numSymChains ) {
65+ const Elf32_Word hash = ctrdl_getELFSymNameHash (name );
66+ size_t chainIndex = ctx -> elf -> symBuckets [hash % ctx -> elf -> numSymBuckets ];
67+
68+ while (chainIndex != STN_UNDEF ) {
69+ const Elf32_Sym * candidate = & ctx -> elf -> symEntries [chainIndex ];
70+ if (!strcmp (& ctx -> elf -> stringTable [candidate -> st_name ], name )) {
71+ sym = candidate ;
72+ symBase = ctx -> handle -> base ;
73+ break ;
74+ }
75+
76+ chainIndex = ctx -> elf -> symChains [chainIndex ];
77+ }
78+ }
79+ }
80+
5781 if (!sym ) {
5882 // Look into dependencies.
59- // TODO: sym info for current module is not setup; do we need to look into ourselves?
60- sym = ctrdl_symNameLookupLoadOrder (ctx -> handle , name );
83+ sym = ctrdl_symNameLookupLoadOrder (ctx -> handle , name , & symBase );
6184 }
6285
63- return sym ? (ctx -> handle -> base + sym -> st_value ) : 0 ;
86+ return sym ? (symBase + sym -> st_value ) : 0 ;
6487}
6588
6689static bool ctrdl_handleSingleReloc (RelContext * ctx , RelEntry * entry ) {
0 commit comments