ELF 解析符号包,包含symtab 和dynsym

来源:互联网 发布:江苏邦宁域名证书下载 编辑:程序博客网 时间:2024/04/29 10:10

ELF 解析符号包,包含symtab 和dynsym,并未对所以平台支持

/* * ParseDynSymTable.c * *  Created on: 2014年7月19日 *      Author: angel-toms */#include "ElfParser.h"Elf32_Sym* get_elf_section_of_sym_table(MemMapping* mem,Elf32_Ehdr* pHeader,Elf32_Shdr* pSheader){Elf32_Sym* pSymMem      = NULL;u1* shStringTableMem= NULL;u4 i = 0;shStringTableMem = get_elf_section_of_shstr_table(mem,pHeader,pSheader);if(NULL == shStringTableMem){printf("Error,get elf section header string table failed !\n");goto Exit;}for( ; i < pHeader->e_shnum ; i++){if(pSheader[i].sh_type == SHT_SYMTAB && strcmp((const char*)(shStringTableMem + pSheader[i].sh_name),".symtab") == 0){pSymMem = calloc(1,pSheader[i].sh_size);if(NULL == pSymMem){perror("Error,calloc elf sym table  failed");goto Exit;}memcpy(pSymMem,(mem->base + pSheader[i].sh_offset),pSheader[i].sh_size);break;}continue;}Exit:if(shStringTableMem)free(shStringTableMem);return pSymMem;}Elf32_Sym* get_elf_section_of_dynsym_table(MemMapping* mem,Elf32_Ehdr* pHeader,Elf32_Shdr* pSheader){Elf32_Sym* pDynmMem      = NULL;u1* shStringTableMem= NULL;u4 i = 0;shStringTableMem = get_elf_section_of_shstr_table(mem,pHeader,pSheader);if(NULL == shStringTableMem){printf("Error,get elf section header string table failed !\n");goto Exit;}for( ; i < pHeader->e_shnum ; i++){if(pSheader[i].sh_type == SHT_SYMTAB && strcmp((const char*)(shStringTableMem + pSheader[i].sh_name),".symtab") == 0){pDynmMem = calloc(1,pSheader[i].sh_size);if(NULL == pDynmMem){perror("Error,calloc elf dyn sym  section  failed");goto Exit;}memcpy(pDynmMem,(mem->base + pSheader[i].sh_offset),pSheader[i].sh_size);break;}continue;}Exit:if(shStringTableMem)free(shStringTableMem);return pDynmMem;}void print_elf_section_of_sym_table(MemMapping* mem,Elf32_Ehdr* pHeader,Elf32_Shdr* pSheader){Elf32_Sym* pSymMem      = NULL;u1* shStringTableMem= NULL;u4 i = 0;u4 size= 0;LookupSection* pLookupSection = NULL;Elf32_Shdr* pSymtab= NULL;shStringTableMem = get_elf_section_of_shstr_table(mem,pHeader,pSheader);if(NULL == shStringTableMem){printf("Error,get elf section header string table failed !\n");goto Exit;}for( ; i < pHeader->e_shnum ; i++){if(pSheader[i].sh_type == SHT_SYMTAB && strcmp((const char*)(shStringTableMem + pSheader[i].sh_name),".symtab") == 0){size = (pSheader[i].sh_size / pSheader[i].sh_entsize);pSymMem =  (Elf32_Sym*)(mem->base + pSheader[i].sh_offset);pSymtab = (Elf32_Shdr*) &pSheader[i];break;}continue;}if(NULL != pSymMem){//@1 by default ,symtab link strtab ,so it sh_link is the index, u can impl get_elf_section_of_string_tablepLookupSection = get_section_by_index(mem,pHeader,pSheader,pSymtab->sh_link);if(NULL == pLookupSection){printf("Error,get section by index failed");goto Exit;}printf("Symbol table :\n");printf("Symbol table '.symtab' contains %d entries:\n",size);printf("Num:  Value    Size   Type                          Bind                                 Vis       Ndx       Name\n");i = 0;for(; i < size ; i++){printf("%3d:  ",i);printf("%.8x ",pSymMem[i].st_value);printf("%-7d",pSymMem[i].st_size);switch(ELF32_ST_TYPE(pSymMem[i].st_info)){case STT_NOTYPE:printf("%-30s","Unspecified type");break;case STT_OBJECT:printf("%-30s","Data object");break;case STT_FUNC:printf("%-30s","Function");break;case STT_SECTION:printf("%-30s","Section");break;case STT_FILE:printf("%-30s","Source file");break;case STT_COMMON:printf("%-30s","Uninitialized common block");break;case STT_TLS:printf("%-30s","TLS object");break;case STT_NUM:printf("%-30s","NUM");break;case STT_LOOS:printf("%-30s","TLS object");break;case STT_HIOS:printf("%-30s","specific semantics");break;case STT_LOPROC:printf("%-30s","reserved range for processor");break;case STT_HIPROC:printf("%-30s","specific semantics");break;default:printf("%-30s","");break;}switch(ELF32_ST_BIND(pSymMem[i].st_info)){case STB_LOCAL:printf("%-37s","Local symbol");break;case STB_GLOBAL:printf("%-37s","Global symbol");break;case STB_WEAK:printf("%-37s","like global - lower precedence");break;case STB_LOOS:printf("%-37s","Reserved range for operating system");break;case STB_HIOS:printf("%-37s","specific semantics");break;case STB_LOPROC:printf("%-37s","reserved range for processor");break;case STB_HIPROC:printf("%-37s","specific semantics");break;default:printf("%-37s","");break;}switch(ELF32_ST_VISIBILITY(pSymMem[i].st_other)){case STV_DEFAULT:printf("%-10s","DEFAULT");break;case STV_INTERNAL:printf("%-10s","INTERNA");break;case STV_HIDDEN:printf("%-10s","HIDDEN");break;case STV_PROTECTED:printf("%-10s","PROTECTED");break;case STV_EXPORTED:printf("%-10s","EXPORTED");break;case STV_SINGLETON:printf("%-10s","SINGLETON");break;case STV_ELIMINATE:printf("%-10s","ELIMINATE");break;default:printf("%-10s","");break;}switch(pSymMem[i].st_shndx){case SHN_UNDEF:printf("%-10s","UNDEF");break;case SHN_LORESERVE:printf("%-10s","LORESERVE");break;case SHN_HIPROC:printf("%-10s","HIPROC");break;case SHN_LOOS:printf("%-10s","LOOS");break;case SHN_HIOS:printf("%-10s","HIOS");break;case SHN_ABS:printf("%-10s","ABS");break;case SHN_COMMON:printf("%-10s","COMMON");break;case SHN_XINDEX:printf("%-10s","XINDEX");break;default:printf("%-10d",pSymMem[i].st_shndx);break;}printf("%s",(pLookupSection->base + pSymMem[i].st_name));printf("\n");}}Exit:if(shStringTableMem)free(shStringTableMem);if(pLookupSection)free(pLookupSection);}void print_elf_section_of_dyn_sym_table(MemMapping* mem,Elf32_Ehdr* pHeader,Elf32_Shdr* pSheader){Elf32_Sym* pDynSymMem      = NULL;u1* shStringTableMem= NULL;u4 i = 0;u4 size= 0;LookupSection* pLookupSection = NULL;Elf32_Shdr* pynSymtab= NULL;shStringTableMem = get_elf_section_of_shstr_table(mem,pHeader,pSheader);if(NULL == shStringTableMem){printf("Error,get elf section header string table failed !\n");goto Exit;}for( ; i < pHeader->e_shnum ; i++){if(pSheader[i].sh_type == SHT_DYNSYM && strcmp((const char*)(shStringTableMem + pSheader[i].sh_name),".dynsym") == 0){size = (pSheader[i].sh_size / pSheader[i].sh_entsize);pDynSymMem = (Elf32_Sym*)(mem->base + pSheader[i].sh_offset);pynSymtab = (Elf32_Shdr*)&pSheader[i];break;}continue;}if(NULL != pDynSymMem){//@1 by default ,dynsym link strtab ,so it sh_link is the index, u can impl get_elf_section_of_string_tablepLookupSection = get_section_by_index(mem,pHeader,pSheader,pynSymtab->sh_link);if(NULL == pLookupSection){printf("Error,get section by index failed");goto Exit;}printf("Symbol dyn table :\n");printf("Symbol table '.dynsym' contains %d entries:\n",size);printf("Num:  Value    Size   Type                          Bind                                 Vis       Ndx       Name\n");i = 0;for(; i < size ; i++){printf("%3d:  ",i);printf("%.8x ",pDynSymMem[i].st_value);printf("%-7d",pDynSymMem[i].st_size);switch(ELF32_ST_TYPE(pDynSymMem[i].st_info)){case STT_NOTYPE:printf("%-30s","Unspecified type");break;case STT_OBJECT:printf("%-30s","Data object");break;case STT_FUNC:printf("%-30s","Function");break;case STT_SECTION:printf("%-30s","Section");break;case STT_FILE:printf("%-30s","Source file");break;case STT_COMMON:printf("%-30s","Uninitialized common block");break;case STT_TLS:printf("%-30s","TLS object");break;case STT_NUM:printf("%-30s","NUM");break;case STT_LOOS:printf("%-30s","TLS object");break;case STT_HIOS:printf("%-30s","specific semantics");break;case STT_LOPROC:printf("%-30s","reserved range for processor");break;case STT_HIPROC:printf("%-30s","specific semantics");break;default:printf("%-30s","");break;}switch(ELF32_ST_BIND(pDynSymMem[i].st_info)){case STB_LOCAL:printf("%-37s","Local symbol");break;case STB_GLOBAL:printf("%-37s","Global symbol");break;case STB_WEAK:printf("%-37s","like global - lower precedence");break;case STB_LOOS:printf("%-37s","Reserved range for operating system");break;case STB_HIOS:printf("%-37s","specific semantics");break;case STB_LOPROC:printf("%-37s","reserved range for processor");break;case STB_HIPROC:printf("%-37s","specific semantics");break;default:printf("%-37s","");break;}switch(ELF32_ST_VISIBILITY(pDynSymMem[i].st_other)){case STV_DEFAULT:printf("%-10s","DEFAULT");break;case STV_INTERNAL:printf("%-10s","INTERNA");break;case STV_HIDDEN:printf("%-10s","HIDDEN");break;case STV_PROTECTED:printf("%-10s","PROTECTED");break;case STV_EXPORTED:printf("%-10s","EXPORTED");break;case STV_SINGLETON:printf("%-10s","SINGLETON");break;case STV_ELIMINATE:printf("%-10s","ELIMINATE");break;default:printf("%-10s","");break;}switch(pDynSymMem[i].st_shndx){case SHN_UNDEF:printf("%-10s","UNDEF");break;case SHN_LORESERVE:printf("%-10s","LORESERVE");break;case SHN_HIPROC:printf("%-10s","HIPROC");break;case SHN_LOOS:printf("%-10s","LOOS");break;case SHN_HIOS:printf("%-10s","HIOS");break;case SHN_ABS:printf("%-10s","ABS");break;case SHN_COMMON:printf("%-10s","COMMON");break;case SHN_XINDEX:printf("%-10s","XINDEX");break;default:printf("%-10d",pDynSymMem[i].st_shndx);break;}printf("%s",(pLookupSection->base + pDynSymMem[i].st_name));printf("\n");}}Exit:if(shStringTableMem)free(shStringTableMem);if(pLookupSection)free(pLookupSection);}


0 0
原创粉丝点击