[ACCEPTED]-Print the symbol table of an ELF file-symbol-table

Accepted answer
Score: 10

You get the section table by looking at 8 the e_shoff field of the elf header:

sections = (Elf32_Shdr *)((char *)map_start + header->e_shoff);

You can now 7 search throught the section table for the 6 section with type SHT_SYMBTAB, which is the symbol 5 table.

for (i = 0; i < header->e_shnum; i++)
    if (sections[i].sh_type == SHT_SYMTAB) {
        symtab = (Elf32_Sym *)((char *)map_start + sections[i].sh_offset);
        break; }

Of course, you should also do lots 4 of sanity checking in case your file is 3 not an ELF file or has been corrupted in 2 some way.

The linux elf(5) manual page has lots of info about the 1 format.

Score: 1

Here is an example: https://docs.oracle.com/cd/E19683-01/817-0679/6mgfb878d/index.html

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <libelf.h>
#include <gelf.h>

main(int argc, char **argv)
    Elf         *elf;
    Elf_Scn     *scn = NULL;
    GElf_Shdr   shdr;
    Elf_Data    *data;
    int         fd, ii, count;


    fd = open(argv[1], O_RDONLY);
    elf = elf_begin(fd, ELF_C_READ, NULL);

    while ((scn = elf_nextscn(elf, scn)) != NULL) {
        gelf_getshdr(scn, &shdr);
        if (shdr.sh_type == SHT_SYMTAB) {
            /* found a symbol table, go print it. */

    data = elf_getdata(scn, NULL);
    count = shdr.sh_size / shdr.sh_entsize;

    /* print the symbol names */
    for (ii = 0; ii < count; ++ii) {
        GElf_Sym sym;
        gelf_getsym(data, ii, &sym);
        printf("%s\n", elf_strptr(elf, shdr.sh_link, sym.st_name));


More Related questions