toolsandutils/e32tools/elf2e32/source/pl_elfconsumer.cpp
changeset 0 83f4b4db085c
child 1 d4b442d23379
equal deleted inserted replaced
-1:000000000000 0:83f4b4db085c
       
     1 // Copyright (c) 2004-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 // Implementation of the Class ElfConsumer for the elf2e32 tool
       
    15 // @internalComponent
       
    16 // @released
       
    17 // 
       
    18 //
       
    19 
       
    20 #include "pl_elfconsumer.h"
       
    21 #include "parameterlistinterface.h"
       
    22 #include "errorhandler.h"
       
    23 #include <iostream>
       
    24 #include <string>
       
    25 
       
    26 using std::list;
       
    27 using std::cout;
       
    28 using std::endl;
       
    29 using std::min;
       
    30 
       
    31 /**
       
    32 Constructor for class ElfConsumer
       
    33 @param aParameterListInterface - instance of class ParameterListInterface
       
    34 @internalComponent
       
    35 @released
       
    36 */
       
    37 ElfConsumer::ElfConsumer(ParameterListInterface *aParameterListInterface) :\
       
    38  ElfExecutable(aParameterListInterface) ,\
       
    39  iMemBlock(NULL)
       
    40 {
       
    41 }
       
    42 
       
    43 
       
    44 /**
       
    45 Destructor for class ElfConsumer
       
    46 @internalComponent
       
    47 @released
       
    48 */
       
    49 ElfConsumer::~ElfConsumer(){
       
    50 
       
    51 	DELETE_PTR_ARRAY(iMemBlock);
       
    52 }
       
    53 
       
    54 
       
    55 /**
       
    56 This operation takes the member of the ElfFileWriter object that is to be populated with the
       
    57 export info in the iExports member (from the iInputElfFile member of ElfConsumer).
       
    58 @param aFile - Elf file name
       
    59 @internalComponent
       
    60 @released
       
    61 */
       
    62 PLUINT32 ElfConsumer::ReadElfFile(char* aFile){
       
    63 	FILE*	aFd;
       
    64 
       
    65 	if( (aFd = fopen(aFile,"rb")) == NULL) {
       
    66 		throw FileError(FILEOPENERROR, aFile);
       
    67 	}
       
    68 
       
    69 	fseek(aFd, 0, SEEK_END);
       
    70 
       
    71 	PLUINT32 aSz = ftell(aFd);
       
    72 	iMemBlock = new char[aSz];
       
    73 
       
    74 	fseek(aFd, 0, SEEK_SET);
       
    75 
       
    76 	// Certain Windows devices (e.g., network shares) limit the size of I/O operations to 64MB
       
    77 	// or less.  We read all the data in individual KMaxWindowsIOSize (32MB) chunks to be safe.
       
    78 	PLUINT32 chunkSize = 0;
       
    79 	for( PLUINT32 bytesRead = 0; bytesRead < aSz; bytesRead += chunkSize) {
       
    80 		
       
    81 		chunkSize = min(aSz - bytesRead, PLUINT32(KMaxWindowsIOSize));
       
    82 		
       
    83 		if( fread(iMemBlock + bytesRead, chunkSize, 1, aFd) != 1) {
       
    84 			throw FileError(FILEREADERROR, aFile);
       
    85 		}		
       
    86 	}
       
    87 	
       
    88 	return 0;
       
    89 }
       
    90 
       
    91 
       
    92 /**
       
    93 Funtion for getting elf symbol list
       
    94 @param aList - list of symbols found in elf files
       
    95 @return - 0 for no exports in elf files, otherwise number of symbols found
       
    96 @internalComponent
       
    97 @released
       
    98 */
       
    99 int ElfConsumer::GetElfSymbolList(list<Symbol*>& aList){
       
   100 
       
   101 	if( !iExports ) 
       
   102 		return 0;
       
   103 
       
   104 	//Get the exported symbols
       
   105 	vector<DllSymbol*> aTmpList = iExports->GetExports(true);
       
   106 
       
   107 	typedef vector<DllSymbol*> List;
       
   108 	List::iterator aItr = aTmpList.begin();
       
   109 	while( aItr != aTmpList.end() ){
       
   110 		aList.push_back((Symbol*) (*aItr));
       
   111 		aItr++;
       
   112 	}
       
   113 	aTmpList.clear();
       
   114 	return aList.size();
       
   115 }
       
   116 
       
   117 /**
       
   118 Funtion for getting image details
       
   119 @internalComponent
       
   120 @released
       
   121 */
       
   122 void ElfConsumer::GetImageDetails(/*E32ImageInterface aInterface*/){
       
   123 
       
   124 }
       
   125 
       
   126 
       
   127 /**
       
   128 Funtion for processing elf file
       
   129 @internalComponent
       
   130 @released
       
   131 */
       
   132 PLUINT32 ElfConsumer::ProcessElfFile(){
       
   133 
       
   134 	Elf32_Ehdr *aElfHdr = ELF_ENTRY_PTR(Elf32_Ehdr, iMemBlock, 0);
       
   135 
       
   136 	try
       
   137 	{
       
   138 		ElfExecutable::ProcessElfFile(aElfHdr);
       
   139 		
       
   140 		/* The following is a workaround for the ARM linker problem.
       
   141 		 * Linker Problem: ARM linker generates Long ARM to Thumb veneers for which
       
   142 		 * relocation entries are not generated.
       
   143 		 * The linker problem is resolved in ARM Linker version RVCT 2.2 Build 616.
       
   144 		 * Hence the workaround is applicable only for executables generated
       
   145 		 * by ARM linker 2.2 and if build number is below 616.
       
   146 		 */
       
   147 		char * aARMCompiler = "ARM Linker, RVCT";
       
   148 		int length = strlen(aARMCompiler);
       
   149 		char * aCommentSection = ElfExecutable::FindCommentSection();
       
   150    		/* The .comment section in an elf file contains the compiler version information and 
       
   151    		 * it is used to apply the fore mentioned workaround. 
       
   152    		 * Some build tool chains generating elf file output without the .comment section, 
       
   153    		 * just to save the disk space. In this case the variable aCommentSection gets the NULL value.
       
   154    		 * Solution: This workaround is only applicable for RVCT compiler. So if the .comment section 
       
   155    		 * is not available in the elf file, then this workaround is no need to be applied.
       
   156    		 */
       
   157    		if(aCommentSection != NULL)
       
   158    		{
       
   159 		if (!strncmp(aCommentSection, aARMCompiler, length))
       
   160 		{
       
   161 			int WorkAroundBuildNo = 616;
       
   162 			int BuildNo = 0;
       
   163 			char* RVCTVersion = aCommentSection+length;
       
   164 
       
   165 			/* RVCTVersion contains the following string
       
   166 			 * "<MajorVersion>.<MinorVersion> [Build <BuildNumber>]"
       
   167 			 * Example: "2.2 [Build 616]"
       
   168 			 */
       
   169 			String Version(RVCTVersion);
       
   170 			size_t pos = Version.find_last_of(' ');
       
   171 			size_t size = Version.size();
       
   172 			if (pos < size)
       
   173 			{
       
   174 				size_t index = pos + 1;
       
   175 				if (index < size)
       
   176 				{
       
   177 					BuildNo = atoi(strtok(RVCTVersion+index, "]"));
       
   178 				}
       
   179 			}
       
   180 
       
   181 			/* Workaround is applicable only when the version is 2.2 and if the
       
   182 			 * build number is below 616.
       
   183 			 */
       
   184 			size_t minorVersionPos = Version.find_first_of('.');
       
   185 			char RVCTMinorVersion='0';
       
   186 			if (minorVersionPos < size)
       
   187 			{
       
   188 				size_t index = minorVersionPos + 1;
       
   189 				if (index < size)
       
   190 				{
       
   191 					RVCTMinorVersion = *(RVCTVersion + index);
       
   192 				}
       
   193 			}
       
   194 
       
   195 			if ((*RVCTVersion == '2') && (RVCTMinorVersion == '2') &&
       
   196 				(BuildNo < WorkAroundBuildNo))
       
   197 			{ 
       
   198 				/* The static symbol table should be processed to identify the veneer symbols.
       
   199 				 * Relocation entries should be generated for these symbols if the linker 
       
   200 				 * is not generating the same.
       
   201 				 */
       
   202 				ElfExecutable::FindStaticSymbolTable();
       
   203 				ElfExecutable::ProcessVeneers();
       
   204 			}
       
   205 		}
       
   206 		}
       
   207 	}
       
   208 	catch(ErrorHandler&) 
       
   209 	{
       
   210 		throw;
       
   211 	} 
       
   212 	/*catch(...) // If there are any other unhandled exception,they are handled here.
       
   213 	{
       
   214 		//Warning to indicate that there had been an exception at this point.
       
   215 		MessageHandler::GetInstance()->ReportWarning(ELFFILEERROR,(char*)iParameterListInterface->ElfInput());
       
   216 		throw;
       
   217 	} */
       
   218 	return 0;
       
   219 }
       
   220 
       
   221 
       
   222