Skip to content

Commit e1c0d30

Browse files
committed
Fixed the bug of incorrect symbol name matching in xdl_dsym().
1 parent cf1284d commit e1c0d30

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

xdl/src/main/cpp/xdl.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ void *xdl_sym(void *handle, const char *symbol, size_t *symbol_size) {
668668
* symbol name in .symtab lookup is match
669669
* ---------------------- ---------------- --------
670670
* abcd abc N
671+
* abcd abcde N
671672
* abcd abcd Y
672673
* abcd.llvm.10190306339727611508 abc N
673674
* abcd.llvm.10190306339727611508 abcd Y
@@ -678,17 +679,19 @@ void *xdl_sym(void *handle, const char *symbol, size_t *symbol_size) {
678679
* abcd.__uniq.513291356003753 abcd.__uniq.513291356003753 Y
679680
*/
680681
// clang-format on
681-
static inline bool xdl_dsym_is_match(const char *str, const char *sym, size_t str_len) {
682-
if (__predict_false(0 == str_len)) return false;
682+
static inline bool xdl_dsym_is_match(const char *str, const char *sym, size_t sym_len) {
683+
size_t str_len = strlen(str);
684+
if (0 == str_len) return false;
683685

684-
do {
685-
if (*str != *sym) return __predict_false('.' == *str && '\0' == *sym);
686-
str++;
687-
sym++;
688-
if ('\0' == *str) break;
689-
} while (0 != --str_len);
690-
691-
return true;
686+
if (str_len < sym_len) {
687+
return false;
688+
} else {
689+
bool sym_len_match = (0 == memcmp(str, sym, sym_len));
690+
if (str_len == sym_len)
691+
return sym_len_match;
692+
else // str_len > sym_len
693+
return sym_len_match && (str[sym_len] == '.');
694+
}
692695
}
693696

694697
void *xdl_dsym(void *handle, const char *symbol, size_t *symbol_size) {
@@ -705,12 +708,13 @@ void *xdl_dsym(void *handle, const char *symbol, size_t *symbol_size) {
705708

706709
// find symbol
707710
if (NULL == self->symtab) return NULL;
711+
size_t symbol_len = strlen(symbol);
708712
for (size_t i = 0; i < self->symtab_cnt; i++) {
709713
ElfW(Sym) *sym = self->symtab + i;
710714

711715
if (!XDL_SYMTAB_IS_EXPORT_SYM(sym->st_shndx)) continue;
712716
// if (0 != strncmp(self->strtab + sym->st_name, symbol, self->strtab_sz - sym->st_name)) continue;
713-
if (!xdl_dsym_is_match(self->strtab + sym->st_name, symbol, self->strtab_sz - sym->st_name)) continue;
717+
if (!xdl_dsym_is_match(self->strtab + sym->st_name, symbol, symbol_len)) continue;
714718

715719
if (NULL != symbol_size) *symbol_size = sym->st_size;
716720
return (void *)(self->load_bias + sym->st_value);

0 commit comments

Comments
 (0)