how to dump the .dynsym section of an ELF file?

Trey :

As far as I know the .dynsym section is a bunch of ElfN_Sym structs arranged sequentially, so I'm trying to dump the st_name of all symbols but apparently there's something wrong with the way I'm parsing the file.

First thing I did was to get the index of the .dynsym section by iterating the section headers and comparing the sh_name field through the string table. Then I got the address of .dynsym by taking the sh_addr field:

Elf64_Sym *symbol = (Elf64_Sym *) shdr[i].sh_addr;
printf("%s\n", symbol->st_name);  

This however is SEGFAULTing for some reason, what I am doing wrong and what's the right way to do this? Also, is there a difference between using shdr[i].sh_addr and &shdr[i]?

Employed Russian :

This however is SEGFAULTing for some reason,

It SIGSEGVs because symbol->st_name is not a pointer to a string, it's an offset into .dynstr section where the actual string resides.

In order to print the name, you must read the contents of .dynstr into a buffer (or mmap the .dynstr section), and use st_name as offset into that buffer.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=16974&siteId=1