toolsandutils/e32tools/elfdump/elfdump.cpp
changeset 0 83f4b4db085c
child 1 d4b442d23379
equal deleted inserted replaced
-1:000000000000 0:83f4b4db085c
       
     1 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <stdio.h>
       
    17 #include <string.h>
       
    18 #include <stdlib.h>
       
    19 #include <tools/elfdefs.h>
       
    20 #include <sys/stat.h>
       
    21 
       
    22 #if defined(__MSVCDOTNET__) || defined(__TOOLS2__)
       
    23 #include <iostream>
       
    24 #include <iomanip>
       
    25 using namespace std;
       
    26 #else //(!__MSVCDOTNET__ && !__TOOLS2__) 
       
    27 #include <iostream.h>
       
    28 #include <iomanip.h>
       
    29 #endif //__MSVCDOTNET__
       
    30 
       
    31 #define ADDR(rtype, p, o) (rtype *)(((char *)p) + o)
       
    32 
       
    33 bool ignoreSomeSections;
       
    34 
       
    35 void hexdump(unsigned char* data, int aSize, int offset)
       
    36 	//  print hex dump of relevant sections
       
    37 	{
       
    38 	int i=0;
       
    39 	int p=0;
       
    40 	while (i<aSize)	
       
    41 		{
       
    42 		int count=0;
       
    43 		if(p==0){printf("\t%06x   ",offset);} // offset into section
       
    44 		while (i<aSize && count<4)
       
    45 			{ 
       
    46 			printf("%02X", *data);		// print 4 lots of %08x for the data expresed as 32-bit word 
       
    47 			data++;
       
    48 			i++;
       
    49 			count++;
       
    50 			offset++;
       
    51 			}
       
    52 
       
    53 		printf("  ");
       
    54 		p++;
       
    55 		if (p==4)
       
    56 			{
       
    57 			data=data-16;
       
    58 			for (int i=0;i<16;i++)			//print 16 bytes of memory interpreted 
       
    59 				{							//as ASCII characters with all non-printing 
       
    60 				if (*data>32 && *data <127)	//characters converted to dots
       
    61 					{
       
    62 					printf("%1c",*data);
       
    63 					}
       
    64 				else
       
    65 					{
       
    66 					printf(".");
       
    67 					}
       
    68 					data++;
       
    69 				}
       
    70 			p=0; 
       
    71 			printf(" \n "); 
       
    72 			}
       
    73 		}
       
    74 		printf(" \n\n "); 	   	
       
    75 	}
       
    76 
       
    77 void print_directive(unsigned char* data, int size)
       
    78 	// print formatted text of directive section
       
    79 	{
       
    80 	printf ("\t");
       
    81 
       
    82 	for (int i=0; i<size; i++)
       
    83 		{
       
    84 		if ((char)data[i]>31 && (char)data[i]<127)
       
    85 			{
       
    86 			printf ("%c", (char)data[i]);
       
    87 			}
       
    88 
       
    89 		if ((char)data[i]=='\n')
       
    90 			{
       
    91 			printf ("\n\t");
       
    92 			}
       
    93 		}
       
    94 
       
    95 	printf ("\n");
       
    96 	}
       
    97 
       
    98 void print_reloc(Elf32_Ehdr* eh, Elf32_Sym* symT, unsigned char* strtab)
       
    99 	// print relocation section
       
   100 	{
       
   101 	Elf32_Shdr* shdr = ADDR(Elf32_Shdr, eh, eh->e_shoff);
       
   102 	for (int j=0;j< eh->e_shnum;j++)
       
   103 		{
       
   104 		char* sname = ADDR(char, eh, shdr[eh->e_shstrndx].sh_offset);
       
   105 		if ( (shdr[j].sh_type==9) && 
       
   106 		     ( (!ignoreSomeSections) || 
       
   107 		       (strncmp(".rel.debug_", &sname[shdr[j].sh_name], 11))
       
   108 		     )
       
   109 		   )
       
   110 			{
       
   111 			unsigned char* data = ADDR(unsigned char, eh, shdr[j].sh_offset);
       
   112 			int noOfReloc=shdr[j].sh_size / shdr[j].sh_entsize;
       
   113 			printf("\n\n\n\t\t\t%s\n", &sname[shdr[j].sh_name]);
       
   114 			Elf32_Rel* rl=(Elf32_Rel*)data;				// pointer to relocation section	
       
   115 			for (int i=0;i<noOfReloc;i++)
       
   116 				{
       
   117 				unsigned char* symbolName = strtab;		// pointer to firest element of string											// table which holds symbol names
       
   118 				Elf32_Sym*  sym = symT;					// pointer to symbol table
       
   119 				int symTIndx= ELF32_R_SYM(rl->r_info);		// symbol Tableindex
       
   120 				sym=sym+symTIndx;							
       
   121 				symbolName=symbolName+sym->st_name;		// index into string table section 
       
   122 															// with symbol names
       
   123 				printf("\t0x%08x \t", rl->r_offset);		// prints offset into relocation section
       
   124 				printf("%d", symTIndx);					// symbol table index
       
   125 				printf("\t%s\n",symbolName);				// symbol name
       
   126 				rl++;			
       
   127 				}
       
   128 			}
       
   129 		}
       
   130 	}	
       
   131 
       
   132 void print_GlSymbols(Elf32_Ehdr* eh, Elf32_Sym* symT, unsigned char* strtab)
       
   133 	// print global symbols from Symbol Table
       
   134 	{	
       
   135 	Elf32_Shdr* shdr = ADDR(Elf32_Shdr, eh, eh->e_shoff);
       
   136 	char* sname = ADDR(char, eh, shdr[eh->e_shstrndx].sh_offset);
       
   137 	for (int i=0;i< eh->e_shnum;i++)
       
   138 		{
       
   139 		if (!strcmp(".symtab", &sname[shdr[i].sh_name]))
       
   140 			{
       
   141 		  	int noOfSym=shdr[i].sh_size / shdr[i].sh_entsize; 	// number of symbols
       
   142 		  	const char *symName =(const char *)strtab;
       
   143 		  	int count = 1;										
       
   144 		  	printf("Global symbols:\n");
       
   145 		  	printf("=================\n\n");
       
   146 		  	for (int l=0;l< noOfSym ;l++)
       
   147 				{
       
   148 				symT=symT+1;
       
   149 				if( ELF32_ST_BIND(symT->st_info) == 1)			// searching for global symbols
       
   150 			 		{
       
   151 			  		symName = symName + symT->st_name;			// index into string table section 
       
   152 			 		printf("%d	",count);
       
   153 			  		printf(symName);
       
   154 			  		printf("\n");
       
   155 			  		symName = symName - symT->st_name;			// back to pointing to first byte of string table
       
   156 			  		count++;
       
   157 					}
       
   158 			
       
   159 				}
       
   160 			}
       
   161 		}
       
   162 	}
       
   163 	
       
   164 void print_elf_header(Elf32_Ehdr* eh)
       
   165 	{
       
   166 	// print elf header
       
   167 	if (eh->e_version==1)
       
   168 		printf("\tHeader version: EV_CURRENT (Current version)\n");
       
   169 	else
       
   170 		printf("\tInvalid version: EV_NONE (Invalid version)\n");
       
   171 
       
   172 	if (eh->e_type==0)
       
   173 		printf("\tFile Type: ET_NONE (No file type) (0)\n");
       
   174 	else if (eh->e_type==1)
       
   175 			printf("\tFile Type: ET_REL (Relocatable object) (1)\n");
       
   176 	else if (eh->e_type==2)
       
   177 			printf("\tFile Type: ET_EXEC (Executable file) (2)\n"); 
       
   178 	else if (eh->e_type==3)
       
   179 			printf("\tFile Type: ET_DYN (Shared object file) (3)\n"); 
       
   180 	else if (eh->e_type==4)
       
   181 			printf("\tFile Type: ET_CORE (Core File) (4)\n"); 
       
   182 	else if (eh->e_type==65280)
       
   183 			printf("\tFile Type: ET_LOPROC (Precessor Specific) (ff00)\n");
       
   184 	else	
       
   185 			printf("\tFile Type: ET_HIPROC (Precessor Specific) (ffff)\n");
       
   186 	if (eh->e_machine==40)
       
   187 		printf("\tMachine: EM_ARM (ARM)\n");
       
   188 	else
       
   189 		printf("\tERROR:\tUnexpected machine\n");
       
   190 
       
   191 	printf("\tEntry offset (in SHF_ENTRYSECT section):0x%08x \n",eh->e_entry);
       
   192 	printf("\tProgram header entries : %d\n",eh->e_phnum); 
       
   193 	printf("\tSection header entries : %d\n",eh->e_shnum); 
       
   194   
       
   195 	printf("\tProgram header offset  : %d",eh->e_phoff); 
       
   196 	printf("  bytes (0x%08X",eh->e_phoff);
       
   197 	printf(")\n");
       
   198 	printf("\tSection header offset  : %d",eh->e_shoff); 
       
   199 	printf("  bytes (0x%08X",eh->e_shoff);
       
   200 	printf(")\n");
       
   201 
       
   202 	printf("\tProgram header entry size  : %d",eh->e_phentsize); 
       
   203 	printf("  bytes (0x%02X",eh->e_phentsize);
       
   204 	printf(")\n");
       
   205 	printf("\tSection header entry size  : %d",eh->e_shentsize); 
       
   206 	printf("  bytes (0x%02X",eh->e_shentsize);
       
   207 	printf(")\n");
       
   208 	printf("\tSection header string table index: %d \n", eh->e_shstrndx);
       
   209 	printf("\tHeader size: %d", eh->e_ehsize);
       
   210 	printf("  bytes (0x%02X",eh->e_ehsize);
       
   211 	printf(")\n");
       
   212 	}
       
   213 
       
   214 void print_sect_header(char* sname, Elf32_Shdr* shdr, int count)
       
   215 	// print section header names
       
   216 	{
       
   217 	static const char* KtypeName[]={"0","SHT_PROGBITS (1)","SHT_SYMTAB (2)","SHT_STRTAB (3)",
       
   218 								  "SHT_RELA (4)","5",	"SHT_DINAMIC (6)","7","8","SHT_REL (9)",
       
   219 								  "10","SHT_DINSYM (11)"};
       
   220 						
       
   221 	printf("\n\n\tName\t\t:%1s\n ",&sname[shdr[count].sh_name]);
       
   222 	printf("\tType\t\t: %s\n",  KtypeName[shdr[count].sh_type]);
       
   223 	printf("\tAddr\t\t: 0x%08X\n",shdr[count].sh_addr);
       
   224 	printf("\tSize\t\t: %1d", shdr[count].sh_size);
       
   225 	printf("  bytes (0x%X",shdr[count].sh_size);
       
   226 	printf(")\n");
       
   227 	printf("\tEntry Size\t: %1d\n",shdr[count].sh_entsize);
       
   228 	printf("\tAligment\t: %1d\n\n\n",shdr[count].sh_addralign);		 	
       
   229 	}
       
   230 
       
   231 unsigned char* findSymbolStringT(Elf32_Ehdr* eh)
       
   232 	//calculate and return pointer to the first byte of string table(the one with symbol names)
       
   233 	{
       
   234 	Elf32_Shdr* shdr = ADDR(Elf32_Shdr, eh, eh->e_shoff);
       
   235 	char* sname = ADDR(char, eh, shdr[eh->e_shstrndx].sh_offset);
       
   236 	for (int i=0;i < eh->e_shnum; i++)
       
   237 		{
       
   238 		if (!strcmp(".strtab", &sname[shdr[i].sh_name]))
       
   239 			{
       
   240 			unsigned char* data = ADDR(unsigned char, eh, shdr[i].sh_offset); 
       
   241 			return data;	//pointer to the first byte of string table section
       
   242 			}
       
   243 		}
       
   244 	return NULL;	//if not found  
       
   245 	}
       
   246 
       
   247 Elf32_Sym* findSymbolT(Elf32_Ehdr* eh)
       
   248 	//calculate and return pointer to the first element of symbol table	
       
   249 	{
       
   250 	Elf32_Shdr* shdr = ADDR(Elf32_Shdr, eh, eh->e_shoff);
       
   251 	for (int i=0;i < eh->e_shnum;i++)
       
   252 		{
       
   253 		if (shdr[i].sh_type==2)
       
   254 			{
       
   255 			unsigned char* data = ADDR(unsigned char, eh, shdr[i].sh_offset);
       
   256 			Elf32_Sym* sym=(Elf32_Sym*)data;
       
   257 			return sym;		//pointer to the first element of symbol table.
       
   258 			}
       
   259 		}
       
   260 	return NULL; // if not found
       
   261 	}
       
   262 
       
   263 void print_Summary(Elf32_Ehdr* eh)
       
   264 	{
       
   265 	//print section names
       
   266 	Elf32_Shdr* shdr = ADDR(Elf32_Shdr, eh, eh->e_shoff);
       
   267 	char* sname = ADDR(char, eh, shdr[eh->e_shstrndx].sh_offset);
       
   268 	printf("\nSummary: \n");
       
   269 	printf("==========\n");
       
   270 	for (int i=0;i< eh->e_shnum;i++)
       
   271 		{
       
   272 		printf(&sname[shdr[i].sh_name]);
       
   273 		printf("\n");
       
   274 		}
       
   275 	}
       
   276 
       
   277 bool printAll;
       
   278 
       
   279 int do_elf_file(char* buffer, char* name)
       
   280 	{
       
   281 	Elf32_Ehdr* eh=(Elf32_Ehdr *)buffer;	//elf header
       
   282 	if (eh->e_ident[EI_MAG0] !=0x7f || eh->e_ident[EI_MAG1] != 0x45 || eh->e_ident[EI_MAG2] !=0x4c || eh->e_ident[EI_MAG3] != 0x46)
       
   283 		{
       
   284 		// EI_MAG0 to EI_MAG3 - A files' first 4 bytes hold a 'magic number', identifying the file as an ELF object file. 
       
   285 		cout << "Error: " << name << " is not a valid ELF file";
       
   286 		return 1;
       
   287 		}
       
   288 	if (eh->e_ident[EI_DATA] == 2)							
       
   289 		{
       
   290 		// ELF Header size should be 52 bytes or converted into Big-Endian system 13312
       
   291 		if (eh->e_ehsize != 13312)
       
   292 			{
       
   293 			printf("\tERROR:\tELF Header contains invalid file type\n");
       
   294 			exit(1);
       
   295 			}
       
   296 		// e_ident[EI_DATA] specifies the data encoding of the processor-specific data in the object file.
       
   297 		printf("\tERROR:\tData encoding ELFDATA2MSB (Big-Endian) not supported\n");
       
   298 		exit(1);
       
   299 		}
       
   300 	if (eh->e_ehsize != 52)
       
   301 		{
       
   302 		// ELF Header size should be 52 bytes
       
   303         printf("\tERROR:\tELF Header contains invalid file type\n");
       
   304         exit(1);
       
   305         }
       
   306 	int shoff = eh->e_shoff;							    // offset of section header table
       
   307 	Elf32_Shdr* shdr = ADDR(Elf32_Shdr, eh, shoff);			// calculating pointer to Secton Header Table
       
   308 															// Elf32_Shdr * shdr = (Elf32_Shdr *)(buffer+shoff); 
       
   309 	int shnum = eh->e_shnum;							    // number of section headers
       
   310 	int shstrndx = eh->e_shstrndx;
       
   311 	int snameoffset = shdr[shstrndx].sh_offset;				// offset in file of sections' names
       
   312 	char* sname = ADDR(char, eh, snameoffset);				// pointer to String Table which holds section names
       
   313 															// char * sname = (char *)(buffer+snameoffset);
       
   314 	print_elf_header(eh);									// print Elf Header
       
   315 	
       
   316 	Elf32_Sym* symT= findSymbolT(eh);	// pointer to Symbol table
       
   317 	if (symT==NULL)
       
   318 		{
       
   319 		printf("\nSymbol table not found\n");
       
   320 		}
       
   321 	unsigned char* strtab=findSymbolStringT(eh);	// pointer to String table which holds symbol names
       
   322 	if (strtab==NULL)
       
   323 		{	
       
   324 		printf("\nString (the one which holds symbol names) table not found\n");
       
   325 		}
       
   326 		print_reloc(eh,symT, strtab);	// print relocation info showing symbol names and
       
   327 					        // and the name of section in which the relocaton occurs.														
       
   328 	for(int i = 0; i < shnum; i++)
       
   329 		{    
       
   330 		unsigned char* data = ADDR(unsigned char, eh, shdr[i].sh_offset);	//pointer to the first byte in the section
       
   331 															//unsigned char * data = (unsigned char * )(buffer+shdr[i].sh_offset);
       
   332 		int size = shdr[i].sh_size;	// section size in bytes
       
   333 	
       
   334 		//print directive section
       
   335 		if (!strcmp(".directive", &sname[shdr[i].sh_name])) 
       
   336 			{ 
       
   337 			print_sect_header(sname, shdr, i);
       
   338 			print_directive(data,size);
       
   339 			}
       
   340 
       
   341 		if (!strcmp(".symtab", &sname[shdr[i].sh_name])) 
       
   342 			{
       
   343 			 print_sect_header(sname, shdr, i);	
       
   344 			 // print global symbols
       
   345 			 print_GlSymbols(eh,symT, strtab);								
       
   346 			}
       
   347 
       
   348 		//print relevant section header names
       
   349  		//print hex dump of relevant sections
       
   350 		if (shdr[i].sh_type==1 || shdr[i].sh_type==4 || shdr[i].sh_type==6 ||
       
   351 		    shdr[i].sh_type==9 || shdr[i].sh_type==11)
       
   352 			{
       
   353 			if (strcmp(".comment", &sname[shdr[i].sh_name])&&
       
   354 				strcmp(".line", &sname[shdr[i].sh_name])   &&
       
   355 				strcmp(".hash", &sname[shdr[i].sh_name])   &&
       
   356 				strcmp(".note", &sname[shdr[i].sh_name])   &&
       
   357 				strcmp(".directive", &sname[shdr[i].sh_name]) &&
       
   358 				strncmp(".debug",&sname[shdr[i].sh_name] ,6))
       
   359 				{
       
   360 				if ( ! ( (ignoreSomeSections) &&
       
   361 					 (strncmp(".rel.debug_", &sname[shdr[i].sh_name], 11)==0)
       
   362 				       )
       
   363 				   )
       
   364 					{
       
   365 					print_sect_header(sname, shdr, i);			
       
   366 			 	    hexdump(data,size,i);
       
   367 					}
       
   368 				}
       
   369 			}
       
   370 		if (printAll)		// displays extra information
       
   371 			{ 	
       
   372 			if(i!=0)
       
   373 			 	{
       
   374 		 	 	print_sect_header(sname, shdr, i);					
       
   375 		 		hexdump(data,size,i);				
       
   376 		 		}
       
   377 		 	}		
       
   378 		}
       
   379 	print_Summary(eh);	// print section names
       
   380 	return 0;
       
   381 }
       
   382 
       
   383 int read_ar_element_header(char* ptr)
       
   384 	{
       
   385 	int length = strtol(ptr+48,0,10);
       
   386 
       
   387 	if (strncmp(ptr+58, "\x60\x0A", 2) != 0)
       
   388 		{
       
   389 		return -1;
       
   390 		}
       
   391 	return length;
       
   392 	}
       
   393 	
       
   394 int main(int argc, char* argv[])
       
   395 	{
       
   396 	char* arg;
       
   397 	int numberOfOptions=2;
       
   398 	printAll=0;
       
   399 	ignoreSomeSections=0;
       
   400 	if (argc<2)
       
   401 		{
       
   402 		cout << "File not specified";
       
   403 		exit(1);
       
   404 		}
       
   405 	else if (argc>numberOfOptions+2)
       
   406 		{
       
   407 		cout << "Too many arguments";
       
   408 		exit(1);
       
   409 		}
       
   410 	else
       
   411 		{
       
   412 		for (int i=1;i<=argc-2;i++)
       
   413 			{
       
   414 			if ( strcmp("-i", argv[i]) ==0 )
       
   415 				{
       
   416 				ignoreSomeSections=1;
       
   417 				}
       
   418 			else if ( strcmp("-a", argv[i]) ==0 )
       
   419 				{
       
   420 				printAll=1;
       
   421 				}
       
   422 			}
       
   423 		arg=argv[argc-1];
       
   424 		}
       
   425 
       
   426 	struct stat results;
       
   427 	stat(arg, &results); 
       
   428 	FILE *elffile;
       
   429 	if((elffile  = fopen(arg, "rb" )) == NULL)
       
   430    		{
       
   431    		cout << "Error opening file " << arg;
       
   432 		exit (1); 
       
   433    		}
       
   434 	char* buffer=new  char [results.st_size];//allocating enough memory 
       
   435 	fread( buffer, sizeof( char ), results.st_size, elffile);
       
   436 	fclose(elffile);						
       
   437 	
       
   438 	if (strncmp(buffer, "!<arch>\x0A", 8) != 0)
       
   439 		{
       
   440 		// plain ELF file
       
   441 		if (do_elf_file(buffer, arg) != 0)
       
   442 			{
       
   443 			return 1;
       
   444 			}
       
   445 		return 0;
       
   446 		}
       
   447 
       
   448 	// library file
       
   449 	char* nextfile = buffer;
       
   450 	int remainder = results.st_size;
       
   451 
       
   452 #define ADVANCE(n)	nextfile+=(n); remainder-=(n);
       
   453 
       
   454 	ADVANCE(8);
       
   455 		
       
   456 	while (remainder > 0)
       
   457 		{
       
   458 		int element_length = read_ar_element_header(nextfile);
       
   459 		ADVANCE(60);
       
   460 		
       
   461 		if (element_length < 0 || element_length > remainder)
       
   462 			{
       
   463 			cout << "Error: archive file corrupt";
       
   464 			return 1;
       
   465 			}
       
   466 		
       
   467 		if (strncmp(nextfile, "\x7F\x45\x4C\x46",4) == 0)
       
   468 			{
       
   469 			if (do_elf_file(nextfile, "archive_element") != 0)
       
   470 				{
       
   471 				return 1;
       
   472 				}
       
   473 			}
       
   474 		element_length += element_length&1;	// round up to a multiple of 2
       
   475 		ADVANCE(element_length);
       
   476 		}
       
   477 		
       
   478 	return 0;
       
   479 	}