e32tools/elf2e32/source/filedump.cpp
changeset 0 044383f39525
child 589 851206cea67b
child 590 360bd6b35136
equal deleted inserted replaced
-1:000000000000 0:044383f39525
       
     1 // Copyright (c) 2007-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 // FileDump Operations of elf2e32 tool to dump E32Image and generate ASM File.
       
    15 // @internalComponent
       
    16 // @released
       
    17 // 
       
    18 //
       
    19 
       
    20 #include "pl_common.h"
       
    21 #include "filedump.h"
       
    22 #include "e32imagefile.h"
       
    23 #include "h_utl.h"
       
    24 #include "deffile.h"
       
    25 #include "errorhandler.h"
       
    26 #include <stdio.h>
       
    27 /**
       
    28 Constructor for class FileDump
       
    29 @param aParameterListInterface - Instance of class ParameterListInterface
       
    30 @internalComponent
       
    31 @released
       
    32 */
       
    33 FileDump::FileDump(ParameterListInterface* aParameterListInterface) : UseCaseBase(aParameterListInterface)
       
    34 {
       
    35 }
       
    36 
       
    37 /**
       
    38 Destructor for class FileDump
       
    39 @internalComponent
       
    40 @released
       
    41 */
       
    42 FileDump::~FileDump()
       
    43 {
       
    44 }
       
    45 
       
    46 /**
       
    47 Execute Function for the File Dump. It dumps E32 image or generate ASM file based on the
       
    48 file dump options
       
    49 @return 0 on success, otherwise throw error 
       
    50 @internalComponent
       
    51 @released
       
    52 */
       
    53 int FileDump::Execute()
       
    54 {
       
    55 	if(iParameterListInterface->FileDumpOption() && iParameterListInterface->E32OutOption() && iParameterListInterface->DefFileInOption()) //DumpAsm
       
    56 	{
       
    57 		if(!(iParameterListInterface->DumpOptions() & EDumpAsm))
       
    58 			throw InvalidArgumentError(INVALIDARGUMENTERROR,(!iParameterListInterface->FileDumpSubOptions()?"":iParameterListInterface->FileDumpSubOptions()) ,"--dump");
       
    59 		if(iParameterListInterface->DumpOptions() & 31)
       
    60 			throw InvalidArgumentError(INVALIDARGUMENTERROR,(!iParameterListInterface->FileDumpSubOptions()?"":iParameterListInterface->FileDumpSubOptions()),"--dump");
       
    61 		if(!iParameterListInterface->E32ImageOutput())
       
    62 			throw ParameterParserError(NOREQUIREDOPTIONERROR,"--output");
       
    63 		if(!iParameterListInterface->DefInput())
       
    64 			throw ParameterParserError(NOREQUIREDOPTIONERROR,"--definput");
       
    65 
       
    66 		GenerateAsmFile(iParameterListInterface->E32ImageOutput());
       
    67 	}
       
    68 	else
       
    69 	{
       
    70 		if(!iParameterListInterface->E32Input())
       
    71 			throw ParameterParserError(NOREQUIREDOPTIONERROR,"--e32input");
       
    72 		if(iParameterListInterface->DumpOptions() & EDumpAsm )
       
    73 			throw InvalidArgumentError(INVALIDARGUMENTERROR,iParameterListInterface->FileDumpSubOptions() ,"--dump");
       
    74 		DumpE32Image(iParameterListInterface->E32Input());
       
    75 	}
       
    76 	return 0;
       
    77 }
       
    78 
       
    79 /**
       
    80 Function to generate ASM File.
       
    81 @param afileName - ASM File name
       
    82 @return 0 on success, otherwise throw error 
       
    83 @internalComponent
       
    84 @released
       
    85 */
       
    86 int FileDump::GenerateAsmFile(const char* afileName)//DumpAsm
       
    87 {
       
    88 	DefFile *iDefFile = new DefFile();
       
    89 	SymbolList *aSymList;
       
    90 	aSymList = iDefFile->ReadDefFile(iParameterListInterface->DefInput());
       
    91 
       
    92 	FILE *fptr;
       
    93 
       
    94 	if((fptr=fopen(afileName,"w"))==NULL)
       
    95 	{
       
    96 		throw FileError(FILEOPENERROR,(char*)afileName);
       
    97 	}
       
    98 	else
       
    99 	{
       
   100 		SymbolList::iterator aItr = aSymList->begin();
       
   101 		SymbolList::iterator last = aSymList->end();
       
   102 		Symbol *aSym;
       
   103 
       
   104 		while( aItr != last)
       
   105 		{
       
   106 			aSym = *aItr;
       
   107 
       
   108 			if(aSym->Absent())
       
   109 			{
       
   110 				aItr++;
       
   111 				continue;
       
   112 			}
       
   113 
       
   114 			fputs("\tIMPORT ",fptr);
       
   115 			fputs(aSym->SymbolName(),fptr);
       
   116 			//Set the visibility of the symbols as default."DYNAMIC" option is
       
   117 			//added to remove STV_HIDDEN visibility warnings generated by every 
       
   118 			//export during kernel build 
       
   119 			fputs(" [DYNAMIC]", fptr);
       
   120 			fputs("\n",fptr);
       
   121 			aItr++;
       
   122 		}
       
   123 
       
   124         // Create a directive section that instructs the linker to make all listed
       
   125         // symbols visible.
       
   126 
       
   127         fputs("\n AREA |.directive|, READONLY, NOALLOC\n\n",fptr);
       
   128 
       
   129         fputs("\tDCB \"#<SYMEDIT>#\\n\"\n", fptr);
       
   130 
       
   131 		aItr = aSymList->begin();
       
   132 		while (aItr != last)
       
   133 		{
       
   134 			aSym = *aItr;
       
   135 
       
   136 			if ( aSym->Absent() )
       
   137 			{
       
   138 				aItr++;
       
   139 				continue;
       
   140 			}
       
   141 
       
   142             // Example:
       
   143             //  DCB "EXPORT __ARM_ll_mlass\n"
       
   144 			fputs("\tDCB \"EXPORT ",fptr);
       
   145 			fputs(aSym->SymbolName(),fptr);
       
   146 			fputs("\\n\"\n", fptr);
       
   147 
       
   148 			aItr++;
       
   149 		}
       
   150 
       
   151 		fputs("\n END\n",fptr);
       
   152 		fclose(fptr);
       
   153 	}
       
   154 	return 0;
       
   155 }
       
   156 
       
   157 /**
       
   158 Function to Dump E32 Image.
       
   159 @param afileName - E32 Image File name
       
   160 @return 1 on success, otherwise throw error 
       
   161 @internalComponent
       
   162 @released
       
   163 */
       
   164 int FileDump::DumpE32Image(const char* afileName)
       
   165 {
       
   166 	E32ImageFile *aE32Imagefile=new E32ImageFile();
       
   167 	TInt result = aE32Imagefile->Open(afileName);
       
   168 	
       
   169 	if (result > 0)
       
   170 		return 1;
       
   171 	else if (result == KErrCorrupt || result == KErrNotSupported)
       
   172 	{
       
   173 		throw InvalidE32ImageError(INVALIDE32IMAGEERROR, (char *)afileName);
       
   174 	}
       
   175 	else if (result != 0)
       
   176 	{
       
   177 		throw FileError(FILEREADERROR, (char *)afileName);
       
   178 	}
       
   179 
       
   180 	int dumpOptions=iParameterListInterface->DumpOptions();
       
   181 	
       
   182 	aE32Imagefile->Dump((TText*)afileName, dumpOptions);
       
   183 	delete aE32Imagefile;
       
   184 	return KErrNone;
       
   185 }
       
   186