1 /* ELF functions-only symbol table extractor by David van Moolenbroek.
2 * Replaces generic a.out version by Philip Homburg.
3 * Temporary solution until libc's nlist(3) becomes usable.
14 static int get_syms(Elf
*elf
, Elf_Scn
*scn
, Elf32_Shdr
*shdr
,
15 struct nlist
**nlistp
)
25 if ((data
= elf_getdata(scn
, NULL
)) == NULL
) {
30 nsyms
= data
->d_size
/ sizeof(Elf32_Sym
);
32 size
= sizeof(struct nlist
) * nsyms
;
33 if ((*nlistp
= nlp
= malloc(size
)) == NULL
)
37 sym
= (Elf32_Sym
*) data
->d_buf
;
38 for (i
= 0; i
< nsyms
; i
++, sym
++, nlp
++) {
39 nlp
->n_value
= sym
->st_value
;
41 /* The caller only cares about N_TEXT. Properly setting other
42 * types would involve crosschecking with section types.
44 switch (ELF32_ST_TYPE(sym
->st_info
)) {
45 case STT_FUNC
: nlp
->n_type
= N_TEXT
; break;
46 default: nlp
->n_type
= N_UNDF
; break;
49 name
= elf_strptr(elf
, shdr
->sh_link
, (size_t) sym
->st_name
);
50 if ((nlp
->n_name
= strdup(name
? name
: "")) == NULL
) {
59 int read_nlist(const char *filename
, struct nlist
**nlist_table
)
61 /* Read in the symbol table of 'filename'. Return the resulting symbols
62 * as an array, with 'nlist_table' set to point to it, and the number
63 * of elements as the return value. The caller has no way to free the
64 * results. Return 0 if the executable contains no symbols. Upon error,
65 * return -1 and set errno to an appropriate value.
72 if (elf_version(EV_CURRENT
) == EV_NONE
) {
77 if ((fd
= open(filename
, O_RDONLY
)) < 0)
80 elf
= elf_begin(fd
, ELF_C_READ
, NULL
);
89 while ((scn
= elf_nextscn(elf
, scn
)) != NULL
) {
90 if ((shdr
= elf32_getshdr(scn
)) == NULL
)
93 if (shdr
->sh_type
!= SHT_SYMTAB
)
96 res
= get_syms(elf
, scn
, shdr
, nlist_table
);