Skip to content

Commit

Permalink
elf/symbol: SKip extern symbol
Browse files Browse the repository at this point in the history
    $ ./src/tests/ulpatch_test -f Elf.for_each_symbol -v | grep sym:printf
    =========================================
    ===
    === ULPatch Testing
    ===
    ===  version: v0.5.7-133-g8b30-dirty
    === ---------------------------
    ===   19/77   Elf.for_each_symbol
    sym:printf@GLIBC_2.2.5 extern:0 nphdrs:0
    sym:printf extern:0 nphdrs:0
    5769us OK
    =========================================
    === Total 1 tested
    ===  Success 1
    ===  Failed 0
    ===  Spend 5ms 5.77ms/per
    =========================================

Signed-off-by: Rong Tao <[email protected]>
  • Loading branch information
Rtoax committed Aug 6, 2024
1 parent 8b309b5 commit a20fd6e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
7 changes: 7 additions & 0 deletions src/elf/elf_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,16 @@ struct symbol {
/**
* Store GELF_ST_TYPE(sym->st_info), such as STT_OBJECT/STT_FUNC
* for rbtree compare and search.
*
* FIXME: This field is not useful right now.
*/
int type;

/**
* Mark the symbol is extern or not, it's use to resolve symbol.
*/
bool is_extern;

GElf_Sym sym;

/**
Expand Down
33 changes: 23 additions & 10 deletions src/elf/symbol.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,19 +342,20 @@ const char *elf_mcount_name(struct elf_file *elf)
int cmp_symbol_name(struct rb_node *n1, unsigned long key)
{
struct symbol *s1 = rb_entry(n1, struct symbol, node);
struct symbol *s2 = (struct symbol*)key;
struct symbol *s2 = (struct symbol *)key;

/**
* FIXME: pthread_create()'s symbol type in ulp is STT_NOTYPE
*/
#if 0
if (s1->type < s2->type) {
/**
* FIXME: pthread_create()'s symbol type in ulp is STT_NOTYPE, and
* kernel does not distinguish symbol type.
*/

if (s1->is_extern == s2->is_extern)
return strcmp(s1->name, s2->name);
else if (s1->is_extern > s2->is_extern)
return -1;
} else if (s1->type > s2->type) {
else if (s1->is_extern < s2->is_extern)
return 1;
} else
#endif
return strcmp(s1->name, s2->name);
return 0;
}

struct symbol *alloc_symbol(const char *name, const GElf_Sym *sym)
Expand All @@ -365,6 +366,7 @@ struct symbol *alloc_symbol(const char *name, const GElf_Sym *sym)

s->name = strdup(name);
s->type = GELF_ST_TYPE(sym->st_info);
s->is_extern = is_extern_symbol(sym);

memcpy(&s->sym, sym, sizeof(GElf_Sym));

Expand All @@ -376,6 +378,7 @@ struct symbol *find_symbol(struct elf_file *elf, const char *name, int type)
struct symbol tmp = {
.name = (char *)name,
.type = type,
.is_extern = false,
};
struct rb_node *node = rb_search_node(&elf->elf_file_symbols,
cmp_symbol_name,
Expand Down Expand Up @@ -459,6 +462,16 @@ void rb_free_symbol(struct rb_node *node)

int fprint_symbol(FILE *fp, struct symbol *s, int firstline)
{
int i;

fprintf(fp, "sym:%s extern:%d nphdrs:%d\n", s->name, s->is_extern,
s->nphdrs);

if (s->nphdrs > 0) {
for (i = 0; i < s->nphdrs; i++)
print_phdr(stdout, &s->phdrs[i], i == 0);
}

return fprint_sym(fp, &s->sym, s->name, NULL, firstline);
}

9 changes: 1 addition & 8 deletions src/tests/elf/symbol.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,8 @@ static const struct symbol_t sym_funcs[] = {
static void print_elf_symbol(struct elf_file *elf, struct symbol *sym,
void *arg)
{
int i;
static bool firstline = true;

fprint_sym(stdout, &sym->sym, sym->name, NULL, firstline);
if (sym->nphdrs > 0) {
for (i = 0; i < sym->nphdrs; i++)
print_phdr(stdout, &sym->phdrs[i], i == 0);
}

fprint_symbol(stdout, sym, firstline);
firstline = false;
}

Expand Down
1 change: 1 addition & 0 deletions src/utils/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,7 @@ struct symbol *task_vma_find_symbol(struct task_struct *task, const char *name,
struct symbol tmp = {
.name = (char *)name,
.type = type,
.is_extern = false,
};

ldebug("try find symbol %s\n", name);
Expand Down

0 comments on commit a20fd6e

Please sign in to comment.