Reputation: 85
I want to print symbol's name in symbol table. i'm mapping the the elf to the virtual memory (using mmap), I successfully an accessed to the symbol table, but when trying to print symbol names it fails (an odd string is show, comparing it to the elf file results).
my code :
void printSymboles() {
Elf32_Sym* symtab;
Elf32_Shdr * sh_strtab_p ;
char *sh_strtab;
int symbol_num=-1;
if(currentFd==-1){
printf("not legal file set\n");
} else {
sectionHeader=(Elf32_Shdr*)(map_start+header->e_shoff);
int section_num=header->e_shnum;
int numSectionsFound=0;
for(int i=0;i<section_num &&numSectionsFound<2;i++){
if(sectionHeader[i].sh_type==SHT_SYMTAB) {
symtab=(Elf32_Sym *) (map_start+sectionHeader[i].sh_offset);
symbol_num= sectionHeader[i].sh_size/sectionHeader[i].sh_entsize; // symobl tbl size/ entrysize
numSectionsFound++;
}
if(sectionHeader[i].sh_type==SHT_STRTAB) {
sh_strtab_p=§ionHeader[i];
sh_strtab=(char*) map_start+sh_strtab_p->sh_offset;
numSectionsFound++;
}
}
if(symbol_num==-1) {
printf("symbol table doesn't exist");
} else {
printf("symbol table : \n");
for(int i=0;i<symbol_num;i++) {
printf("name : %s\n",sh_strtab+symtab[i].st_name);
}
}
Upvotes: 1
Views: 2945
Reputation: 126518
The problem is almost certainly that you're looking in the wrong SHT_STRTAB section -- you scan through the header looking for SHT_STRTAB sections and whichever one you find last, you remember in sh_strtab_p. If your ELF file is like most elf files, that's probably the section header string table (contains section header names) and not the string table with your symbol names.
To find the string table with your symbol names, you need to look in the sh_link
field of the symbol table section header -- that tells you the section number (index in the section header) of the string table section containing the names of the symbols in that symbol section. There can be arbitrarily many SYMTAB sections in the file, each with its own STRTAB section.
Putting all that together, you want something more like:
Elf32_Shdr *section = (Elf32_Shdr*)(map_start+header->e_shoff);
char *section_names = (char *)(map_start + section[header->e_shstrndx].sh_offset);
for(int i=0; i<header->e_shnum; i++) {
if(section[i].sh_type==SHT_SYMTAB) {
printf("Symobl table %s:\n", section_names + section[i].sh_name);
Elf32_Sym *symtab = (Elf32_Sym *)(map_start+section[i].sh_offset);
int symbol_num = section[i].sh_size/section[i].sh_entsize;
char *symbol_names = (char *)(map_start + section[section[i].sh_link].sh_offset);
for (int j=0; j<symbol_num; j++) {
printf("name : %s\n", symbol_names + symtab[j].st_name);
}
}
}
Of course, it would also be good to do sanity checking to make sure that none of the indexes are out of range for the section they are indexing into, and that sh_entsize and and e_shentsize match the sizeof the structs you are using, just in case the ELF file has been corrupted.
Upvotes: 3
Reputation: 85
as @chris Dodd mentioned, the problem indeed was the wrong SHT_STRTAB. I've changed this section of code :
if(sectionHeader[i].sh_type==SHT_STRTAB) {
sh_strtab_p=§ionHeader[i];
sh_strtab=(char*) map_start+sh_strtab_p->sh_offset;
numSectionsFound++;
}
to this :
Elf32_Shde * sh_sectionStrTbl_p;
char * sh_sectionStrTbl;
sh_sectionStrTbl_p=§ionHeader[header->e_shoff);
sh_sectionStrTbl=map_start+sh_sectionStrTbl_p->sh_offset;
if(sectionHeader[i].sh_type==SHT_STRTAB) {
if(strcmp(sectionHeader[i].sh_name+sh_sectionStrTbl,".strtab")==0) {
sh_strtab_p=§ionHeader[i];
sh_strtab=(char*) map_start+sh_strtab_p->sh_offset;
numSectionsFound++;
}
}
and it works.
Upvotes: 0