Reputation: 124
I am trying to print the symbol table from an elf file. I got the offsets of sections .symtab and .strtab ( I crosschecked with readelf). but the program is giving segmentation fault. Here is the code in question:
printf("\n\nSymbol table:");
for(i=0;i<symtab.sh_size/symtab.sh_entsize ;i++)
{
fseek(ElfFile,symtab.sh_offset+i*symtab.sh_entsize,SEEK_SET);
fread(&elfSym,1,sizeof elfSym,ElfFile);
printf("\nSymbol:%s,size:%u",elfSym.st_name+strtab.sh_offset,elfSym.st_size);
}
Upvotes: 1
Views: 1826
Reputation: 213385
This is the bug:
printf("\nSymbol:%s,size:%u",elfSym.st_name+strtab.sh_offset,elfSym.st_size);
The sym.st_name
gives you offset into the .strtab
section where the symbol name begins, and strtab.sh_offset
gives you offset into the file to where that section begins.
But adding two offsets does not give you a memory location that you can print with %s
, it gives you offset into the file where that string is. You still need to read from file at that offset into memory, and then you will be able to print it.
Upvotes: 1