On 11/21/24 21:42, Sami Tolvanen wrote:
> The compiler may choose not to emit type information in DWARF for all
> aliases, but it's possible for each alias to be exported separately.
> To ensure we find type information for the aliases as well, read
> {section, address} tuples from the symbol table and match symbols also
> by address.
> 
> Signed-off-by: Sami Tolvanen <[email protected]>
> ---
[...]
> @@ -82,6 +118,130 @@ struct symbol *symbol_get(const char *name)
>       return sym;
>  }
>  
> +typedef void (*elf_symbol_callback_t)(const char *name, GElf_Sym *sym,
> +                                   Elf32_Word xndx, void *arg);
> +
> +static void elf_for_each_global(int fd, elf_symbol_callback_t func, void 
> *arg)
> +{
> +     size_t sym_size;
> +     GElf_Shdr shdr_mem;
> +     GElf_Shdr *shdr;
> +     Elf_Data *xndx_data = NULL;
> +     Elf_Scn *scn;
> +     Elf *elf;
> +
> +     if (elf_version(EV_CURRENT) != EV_CURRENT)
> +             error("elf_version failed: %s", elf_errmsg(-1));
> +
> +     elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
> +     if (!elf)
> +             error("elf_begin failed: %s", elf_errmsg(-1));
> +
> +     scn = elf_nextscn(elf, NULL);
> +
> +     while (scn) {
> +             shdr = gelf_getshdr(scn, &shdr_mem);
> +             if (!shdr)
> +                     error("gelf_getshdr failed: %s", elf_errmsg(-1));
> +
> +             if (shdr->sh_type == SHT_SYMTAB_SHNDX) {
> +                     xndx_data = elf_getdata(scn, NULL);
> +                     if (!xndx_data)
> +                             error("elf_getdata failed: %s", elf_errmsg(-1));
> +                     break;
> +             }
> +
> +             scn = elf_nextscn(elf, scn);
> +     }
> +
> +     sym_size = gelf_fsize(elf, ELF_T_SYM, 1, EV_CURRENT);
> +     scn = elf_nextscn(elf, NULL);
> +
> +     while (scn) {
> +             shdr = gelf_getshdr(scn, &shdr_mem);
> +             if (!shdr)
> +                     error("gelf_getshdr failed: %s", elf_errmsg(-1));
> +
> +             if (shdr->sh_type == SHT_SYMTAB) {
> +                     unsigned int nsyms;
> +                     unsigned int n;
> +                     Elf_Data *data = elf_getdata(scn, NULL);
> +
> +                     if (!data)
> +                             error("elf_getdata failed: %s", elf_errmsg(-1));
> +
> +                     if (shdr->sh_entsize != sym_size)
> +                             error("expected sh_entsize (%lu) to be %zu",
> +                                   shdr->sh_entsize, sym_size);
> +
> +                     nsyms = shdr->sh_size / shdr->sh_entsize;
> +
> +                     for (n = 1; n < nsyms; ++n) {
> +                             const char *name = NULL;
> +                             Elf32_Word xndx = 0;
> +                             GElf_Sym sym_mem;
> +                             GElf_Sym *sym;
> +
> +                             sym = gelf_getsymshndx(data, xndx_data, n,
> +                                                    &sym_mem, &xndx);
> +
> +                             if (!sym ||
> +                                 GELF_ST_BIND(sym->st_info) == STB_LOCAL)
> +                                     continue;

Doesn't NULL returned by gelf_getsymshndx() indicate an error? If yes
then I think it should be reported instead of silently skipping the
symbol.

With this addressed, feel free to add:
Reviewed-by: Petr Pavlu <[email protected]>

-- 
Thanks,
Petr

Reply via email to