ELF 解析.dynamic 节

解析 .dynamic 节,并不对所以平台支持

/*
 * ParseAllDyna.c
 *
 *  Created on: 2014年7月19日
 *      Author: angel-toms
 */

#include "ElfParser.h"

void print_elf_section_of_dynamic(MemMapping* mem,Elf32_Ehdr* pHeader,Elf32_Shdr* pSheader){
	Elf32_Dyn* pDyncMem  			    = NULL;
	u1* shStringTableMem				= NULL;
	u4 i 								= 0;
	u4 size								= 0;
	u1 buf[75];
	LookupSection* pLookupSection 		= NULL;
	Elf32_Shdr* pDynamic				= 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_DYNAMIC && strcmp((const char*)(shStringTableMem + pSheader[i].sh_name),".dynamic") == 0){
			size = (pSheader[i].sh_size / pSheader[i].sh_entsize);
			pDyncMem = (Elf32_Dyn*) (mem->base + pSheader[i].sh_offset);
			pDynamic = (Elf32_Shdr*)&pSheader[i];
			break;
		}
		continue;
	}
	if(NULL != pDyncMem){
		//@1 by default ,dynamic link .dynstr ,so it sh_link is the index
		pLookupSection = get_section_by_index(mem,pHeader,pSheader,pDynamic->sh_link);
		if(NULL == pLookupSection){
			printf("Error,get section by index failed");
			goto Exit;
		}

		printf("Dynamic section :\n");
		printf("Tag        Type                                                             Name/Value\n");
		i = 0;
		for( ; i < size ; i++ ){
			printf("0x%.8x ",pDyncMem[i].d_tag);
			switch(pDyncMem[i].d_tag){
			case DT_NULL:
				printf("%-65s","Terminating entry");
				printf("0x%d ",pDyncMem[i].d_un.d_val);
				break;
			case DT_NEEDED:
				printf("%-65s","String table offset of a needed shared library");
				printf("[ %s ]",(pLookupSection->base + pDyncMem[i].d_un.d_val));//because @1 ,so in here not check the section
				break;
			case DT_PLTRELSZ:
				printf("%-65s","Total size in bytes of PLT relocations");
				printf("%d (bytes) ",pDyncMem[i].d_un.d_val);
				break;
			case DT_PLTGOT:
				printf("%-65s","Processor-dependent address");
				printf("0x%.8x ",pDyncMem[i].d_un.d_ptr);
				break;
			case DT_HASH:
				printf("%-65s","Address of symbol hash table");
				printf("0x%.8x ",pDyncMem[i].d_un.d_ptr);
				break;
			case DT_STRTAB:
				printf("%-65s","Address of string table");
				printf("0x%.8x ",pDyncMem[i].d_un.d_ptr);
				break;
			case DT_SYMTAB:
				printf("%-65s","Address of symbol table");
				printf("0x%.8x ",pDyncMem[i].d_un.d_ptr);
				break;
			case DT_RELA:
				printf("%-65s","Address of ElfNN_Rela relocations");
				printf("0x%.8x ",pDyncMem[i].d_un.d_ptr);
				break;
			case DT_RELASZ:
				printf("%-65s","Total size of ElfNN_Rela relocations");
				printf("%d (bytes) ",pDyncMem[i].d_un.d_val);
				break;
			case DT_RELAENT:
				printf("%-65s","Size of each ElfNN_Rela relocation entry");
				printf("%d (bytes) ",pDyncMem[i].d_un.d_val);
				break;
			case DT_STRSZ:
				printf("%-65s","Size of string table");
				printf("%d (bytes) ",pDyncMem[i].d_un.d_val);
				break;
			case DT_SYMENT:
				printf("%-65s","Size of each symbol table entry");
				printf("%d (bytes) ",pDyncMem[i].d_un.d_val);
				break;
			case DT_INIT:
				printf("%-65s","Address of initialization function");
				printf("0x%.8x ",pDyncMem[i].d_un.d_ptr);
				break;
			case DT_FINI:
				printf("%-65s","Address of finalization function");
				printf("0x%.8x ",pDyncMem[i].d_un.d_ptr);
				break;
			case DT_SONAME:
				printf("%-65s","String table offset of shared object name");
				printf("[ %s ]",(pLookupSection->base + pDyncMem[i].d_un.d_val));
				break;
			case DT_RPATH:
				printf("%-65s","String table offset of library path. [sup]");
				printf("[ %s ]",(pLookupSection->base + pDyncMem[i].d_un.d_val));
				break;
			case DT_SYMBOLIC:
				printf("%-65s","Indicates \"symbolic\" linking. [sup]");
				printf("%d ",pDyncMem[i].d_un.d_val);
				// TODO
				break;
			case DT_REL:
				printf("%-65s","Address of ElfNN_Rel relocations");
				printf("0x%.8x ",pDyncMem[i].d_un.d_ptr);
				break;
			case DT_RELSZ:
				printf("%-65s","Total size of ElfNN_Rel relocations");
				printf("%d ",pDyncMem[i].d_un.d_val);
				break;
			case DT_RELENT:
				printf("%-65s","Size of each ElfNN_Rel relocation");
				printf("%d (bytes) ",pDyncMem[i].d_un.d_val);
				break;
			case DT_PLTREL:
				printf("%-65s","Type of relocation used for PLT");
				printf("REL 0x%.2x ",pDyncMem[i].d_un.d_ptr);
				break;
			case DT_DEBUG:
				printf("%-65s","Reserved debug");
				printf("%.8x ",pDyncMem[i].d_un.d_ptr);
				break;
			case DT_TEXTREL:
				printf("%-65s","Indicates there may be relocations in non-writable segments. [sup]");
				break;
			case DT_JMPREL:
				printf("%-65s","Address of PLT relocations");
				printf("0x%.8x ",pDyncMem[i].d_un.d_ptr);
				break;
			case DT_BIND_NOW:
				printf("%-65s","BIND_NOW");
				printf("0x%.8x",pDyncMem[i].d_un.d_ptr);
				//TODO
				break;
			case DT_INIT_ARRAY:
				printf("%-65s","Address of the array of pointers to initialization functions");
				printf("0x%.8x ",pDyncMem[i].d_un.d_ptr);
				break;
			case DT_FINI_ARRAY:
				printf("%-65s","Address of the array of pointers to termination functions ");
				printf("0x%.8x ",pDyncMem[i].d_un.d_ptr);
				break;
			case DT_INIT_ARRAYSZ:
				printf("%-65s","Size in bytes of the array of initialization functions");
				printf("%d (bytes) ",pDyncMem[i].d_un.d_val);
				break;
			case DT_FINI_ARRAYSZ:
				printf("%-65s","Size in bytes of the array of termination functions");
				printf("%d (bytes) ",pDyncMem[i].d_un.d_val);
				break;
			case DT_RUNPATH:
				printf("%-65s","String table offset of a null-terminated library search path string");
				printf("0x%.8x ",pDyncMem[i].d_un.d_ptr);
				printf("[ %s ]",(pLookupSection->base + pDyncMem[i].d_un.d_val));
				break;
			case DT_FLAGS:
				printf("%-65s","Object specific flag values");
				memset(buf,0,75);
				if(pDyncMem[i].d_un.d_val & DF_ORIGIN){
					strcat(buf,"ORIGIN ");
				}
				if(pDyncMem[i].d_un.d_val & DF_SYMBOLIC){
					strcat(buf,"SYMBOLIC ");
				}
				if(pDyncMem[i].d_un.d_val & DF_TEXTREL){
					strcat(buf,"TEXTREL ");
				}
				if(pDyncMem[i].d_un.d_val & DF_BIND_NOW){
					strcat(buf,"BIND_NOW ");
				}
				if(pDyncMem[i].d_un.d_val & DF_STATIC_TLS){
					strcat(buf,"STATIC_TLS ");
				}
				printf("%s",buf);
				break;
			case DT_PREINIT_ARRAY:
				printf("%-65s","Address of the array of pointers to pre-initialization functions");
				printf("0x%.8x ",pDyncMem[i].d_un.d_ptr);
				break;
			case DT_PREINIT_ARRAYSZ:
				printf("%-65s","Size in bytes of the array of pre-initialization functions");
				printf("%d (bytes) ",pDyncMem[i].d_un.d_val);
				break;
			case DT_MAXPOSTAGS:
				printf("%-65s","number of positive tags");
				printf("%d (bytes) ",pDyncMem[i].d_un.d_val);
				break;
			case DT_LOOS:
				printf("%-65s","First OS-specific");
				break;
			case DT_SUNW_RTLDINF:
				printf("%-65s","ld.so.1 info (private)");
				break;
			case DT_SUNW_FILTER:
				printf("%-65s","symbol filter name");
				printf("[ %s ]",(pLookupSection->base + pDyncMem[i].d_un.d_val));
				// TODO
				break;
			case DT_SUNW_CAP:
				printf("%-65s","hardware/software");
				break;
			case DT_HIOS:
				printf("%-65s","Last OS-specific");
				break;
			case DT_VALRNGLO:
				printf("%-65s","VALRNGLO");
				break;
			case DT_CHECKSUM:
				printf("%-65s","elf checksum");
				printf("0x%.8x ",pDyncMem[i].d_un.d_val);
				break;
			case DT_PLTPADSZ:
				printf("%-65s","pltpadding size");
				printf("%d (bytes) ",pDyncMem[i].d_un.d_val);
				break;
			case DT_MOVEENT:
				printf("%-65s","move table entry size ");
				printf("%d (bytes) ",pDyncMem[i].d_un.d_val);
				break;
			case DT_MOVESZ:
				printf("%-65s","move table size");
				printf("%d (bytes) ",pDyncMem[i].d_un.d_val);
				break;
			case DT_FEATURE_1:
				printf("%-65s","feature holder");
				memset(buf,0,75);
				if(pDyncMem[i].d_un.d_val & DTF_1_PARINIT){
					strcat(buf,"PARINIT ");
				}
				if(pDyncMem[i].d_un.d_val & DTF_1_CONFEXP){
					strcat(buf,"CONFEXP ");
				}
				printf("%s",buf);
				break;
			case DT_POSFLAG_1:
				printf("%-65s","flags for DT_* entries, effecting 	the following DT_* entry See DF_P1_* definitions");
				memset(buf,0,75);
				if(pDyncMem[i].d_un.d_val & DF_P1_LAZYLOAD){
					strcat(buf,"LAZYLOAD ");
				}
				if(pDyncMem[i].d_un.d_val & DF_P1_GROUPPERM){
					strcat(buf,"GROUPPERM ");
				}
				printf("%s",buf);
				break;
			case DT_SYMINSZ:
				printf("%-65s","syminfo table size (in bytes)");
				printf("%d (bytes) ",pDyncMem[i].d_un.d_val);
				break;
			case DT_SYMINENT:
				printf("%-65s","syminfo entry size (in bytes)");
				printf("%d (bytes) ",pDyncMem[i].d_un.d_val);
				break;
			case DT_ADDRRNGLO:
				printf("%-65s","");
				break;
			case DT_GNU_HASH:
				printf("%-65s","GNU-style hash table");
				printf("0x%.8x ",pDyncMem[i].d_un.d_ptr);
				break;
			case DT_CONFIG:
				printf("%-65s","configuration information");
				break;
			case DT_DEPAUDIT:
				printf("%-65s","dependency auditing");
				break;
			case DT_AUDIT:
				printf("%-65s","object auditing");
				break;
			case DT_PLTPAD:
				printf("%-65s","pltpadding (sparcv9)");
				break;
			case DT_MOVETAB:
				printf("%-65s","move table");
				break;
			case DT_SYMINFO:
				printf("%-65s","yminfo table");
				break;
			case DT_VERSYM:
				printf("%-65s","Address of versym section");
				printf("0x%.8x ",pDyncMem[i].d_un.d_ptr);
				break;
			case DT_RELACOUNT:
				printf("%-65s","number of RELATIVE relocations");
				printf("%d ",pDyncMem[i].d_un.d_val);
				break;
			case DT_RELCOUNT:
				printf("%-65s","number of RELATIVE relocations");
				printf("%d (bytes) ",pDyncMem[i].d_un.d_val);
				break;
			case DT_FLAGS_1:
				printf("%-65s","state flags - see DF_1_* defs");
				memset(buf,0,75);
				strcat(buf,"Flags: ");
				if(pDyncMem[i].d_un.d_val & DF_1_BIND_NOW){
					strcat(buf,"NOW ");
				}
				if(pDyncMem[i].d_un.d_val & DF_1_GLOBAL){
					strcat(buf,"GLOBAL ");
				}
				if(pDyncMem[i].d_un.d_val & DF_1_NODELETE){
					strcat(buf,"NODELETE ");
				}
				if(pDyncMem[i].d_un.d_val & DF_1_LOADFLTR){
					strcat(buf,"LOADFLTR ");
				}
				if(pDyncMem[i].d_un.d_val & DF_1_NOOPEN){
					strcat(buf,"NOOPEN ");
				}
				if(pDyncMem[i].d_un.d_val & DF_1_ORIGIN){
					strcat(buf,"ORIGIN ");
				}
				if(pDyncMem[i].d_un.d_val & DF_1_NODEFLIB){
					strcat(buf,"NODEFLIB ");
				}
				printf("%s",buf);
				break;
			case DT_VERDEF:
				printf("%-65s","Address of verdef section");
				printf("0x%.8x ",pDyncMem[i].d_un.d_ptr);
				break;
			case DT_VERDEFNUM:
				printf("%-65s","Number of elems in verdef section ");
				printf("%d (bytes) ",pDyncMem[i].d_un.d_val);
				break;
			case DT_VERNEED:
				printf("%-65s","Address of verneed section");
				printf("0x%.8x ",pDyncMem[i].d_un.d_ptr);
				break;
			case DT_VERNEEDNUM:
				printf("%-65s","Number of elems in verneed section");
				printf("%d ",pDyncMem[i].d_un.d_val);
				break;
			case DT_LOPROC:
				printf("%-65s","First processor-specific type");
				break;
			case DT_DEPRECATED_SPARC_REGISTER:
				printf("%-65s","DEPRECATED_SPARC_REGISTER");
				break;
			case DT_AUXILIARY:
				printf("%-65s","shared library auxiliary name");
				// TODO
				printf("[ %s ]",(pLookupSection->base + pDyncMem[i].d_un.d_val));
				break;
			case DT_USED:
				printf("%-65s","ignored - same as needed");
				break;
			case DT_FILTER:
				printf("%-65s","shared library filter name");
				printf("[ %s ]",(pLookupSection->base + pDyncMem[i].d_un.d_val));
				//TODO
				break;
			case DT_GNU_PRELINKED:
				printf("%-65s","GNU_PRELINKED");
				u1 time[STR_TIME_LEN];
				memset(time,0,STR_TIME_LEN);
				if(get_pre_link_time(pDyncMem[i].d_un.d_val,time) != 0){
					goto Exit;
				}
				printf("%-65s ",time);
				break;
			case DT_GNU_LIBLIST:
				printf("%-65s","GNU_LIBLIST");
				printf("0x%.8x",pDyncMem[i].d_un.d_ptr);
				break;
			case DT_GNU_LIBLISTSZ:
				printf("%-65s","GNU_LIBLISTSZ");
				printf("%d (bytes)",pDyncMem[i].d_un.d_val);
				break;
			case DT_GNU_CONFLICT:
				printf("%-65s","GNU_CONFLICT");
				printf("0x%.8x",pDyncMem[i].d_un.d_ptr);
				break;
			case DT_GNU_CONFLICTSZ:
				printf("%-65s","GNU_CONFLICTSZ");
				printf("%d (bytes)",pDyncMem[i].d_un.d_val);
				break;
			default:
				printf("%-65s","");
				printf("0x%.8x",pDyncMem[i].d_un.d_ptr);
				break;
			}
			printf("\n");
			if(pDyncMem[i].d_tag == DT_NULL)
				break;
		}
	}

	Exit:
	if(shStringTableMem)
		free(shStringTableMem);
	if(pLookupSection)
		free(pLookupSection);
}


猜你喜欢

转载自blog.csdn.net/ylcangel/article/details/37997853