toolsandutils/e32tools/elf2e32/source/filedump.cpp
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Tue, 02 Feb 2010 01:39:43 +0200
changeset 0 83f4b4db085c
child 2 99082257a271
permissions -rw-r--r--
Revision: 201005 Kit: 201005

// Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
// All rights reserved.
// This component and the accompanying materials are made available
// under the terms of the License "Eclipse Public License v1.0"
// which accompanies this distribution, and is available
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
//
// Initial Contributors:
// Nokia Corporation - initial contribution.
//
// Contributors:
//
// Description:
// FileDump Operations of elf2e32 tool to dump E32Image and generate ASM File.
// @internalComponent
// @released
// 
//

#include "pl_common.h"
#include "filedump.h"
#include "e32imagefile.h"
#include "h_utl.h"
#include "deffile.h"
#include "errorhandler.h"
#include <stdio.h>
/**
Constructor for class FileDump
@param aParameterListInterface - Instance of class ParameterListInterface
@internalComponent
@released
*/
FileDump::FileDump(ParameterListInterface* aParameterListInterface) : UseCaseBase(aParameterListInterface)
{
}

/**
Destructor for class FileDump
@internalComponent
@released
*/
FileDump::~FileDump()
{
}

/**
Execute Function for the File Dump. It dumps E32 image or generate ASM file based on the
file dump options
@return 0 on success, otherwise throw error 
@internalComponent
@released
*/
int FileDump::Execute()
{
	if(iParameterListInterface->FileDumpOption() && iParameterListInterface->E32OutOption() && iParameterListInterface->DefFileInOption()) //DumpAsm
	{
		if(!(iParameterListInterface->DumpOptions() & EDumpAsm))
			throw InvalidArgumentError(INVALIDARGUMENTERROR,(!iParameterListInterface->FileDumpSubOptions()?"":iParameterListInterface->FileDumpSubOptions()) ,"--dump");
		if(iParameterListInterface->DumpOptions() & 31)
			throw InvalidArgumentError(INVALIDARGUMENTERROR,(!iParameterListInterface->FileDumpSubOptions()?"":iParameterListInterface->FileDumpSubOptions()),"--dump");
		if(!iParameterListInterface->E32ImageOutput())
			throw ParameterParserError(NOREQUIREDOPTIONERROR,"--output");
		if(!iParameterListInterface->DefInput())
			throw ParameterParserError(NOREQUIREDOPTIONERROR,"--definput");

		GenerateAsmFile(iParameterListInterface->E32ImageOutput());
	}
	else
	{
		if(!iParameterListInterface->E32Input())
			throw ParameterParserError(NOREQUIREDOPTIONERROR,"--e32input");
		if(iParameterListInterface->DumpOptions() & EDumpAsm )
			throw InvalidArgumentError(INVALIDARGUMENTERROR,iParameterListInterface->FileDumpSubOptions() ,"--dump");
		DumpE32Image(iParameterListInterface->E32Input());
	}
	return 0;
}

/**
Function to generate ASM File.
@param afileName - ASM File name
@return 0 on success, otherwise throw error 
@internalComponent
@released
*/
int FileDump::GenerateAsmFile(const char* afileName)//DumpAsm
{
	DefFile *iDefFile = new DefFile();
	SymbolList *aSymList;
	aSymList = iDefFile->ReadDefFile(iParameterListInterface->DefInput());

	FILE *fptr;

	if((fptr=fopen(afileName,"w"))==NULL)
	{
		throw FileError(FILEOPENERROR,(char*)afileName);
	}
	else
	{
		SymbolList::iterator aItr = aSymList->begin();
		SymbolList::iterator last = aSymList->end();
		Symbol *aSym;

		while( aItr != last)
		{
			aSym = *aItr;

			if(aSym->Absent())
			{
				aItr++;
				continue;
			}

			fputs("\tIMPORT ",fptr);
			fputs(aSym->SymbolName(),fptr);
			//Set the visibility of the symbols as default."DYNAMIC" option is
			//added to remove STV_HIDDEN visibility warnings generated by every 
			//export during kernel build 
			fputs(" [DYNAMIC]", fptr);
			fputs("\n",fptr);
			aItr++;
		}

        // Create a directive section that instructs the linker to make all listed
        // symbols visible.

        fputs("\n AREA |.directive|, READONLY, NOALLOC\n\n",fptr);

        fputs("\tDCB \"#<SYMEDIT>#\\n\"\n", fptr);

		aItr = aSymList->begin();
		while (aItr != last)
		{
			aSym = *aItr;

			if ( aSym->Absent() )
			{
				aItr++;
				continue;
			}

            // Example:
            //  DCB "EXPORT __ARM_ll_mlass\n"
			fputs("\tDCB \"EXPORT ",fptr);
			fputs(aSym->SymbolName(),fptr);
			fputs("\\n\"\n", fptr);

			aItr++;
		}

		fputs("\n END\n",fptr);
		fclose(fptr);
	}
	return 0;
}

/**
Function to Dump E32 Image.
@param afileName - E32 Image File name
@return 1 on success, otherwise throw error 
@internalComponent
@released
*/
int FileDump::DumpE32Image(const char* afileName)
{
	E32ImageFile *aE32Imagefile=new E32ImageFile();
	TInt result = aE32Imagefile->Open(afileName);
	
	if (result > 0)
		return 1;
	else if (result == KErrCorrupt || result == KErrNotSupported)
	{
		throw InvalidE32ImageError(INVALIDE32IMAGEERROR, (char *)afileName);
	}
	else if (result != 0)
	{
		throw FileError(FILEREADERROR, (char *)afileName);
	}

	int dumpOptions=iParameterListInterface->DumpOptions();
	
	aE32Imagefile->Dump((TText*)afileName, dumpOptions);
	delete aE32Imagefile;
	return KErrNone;
}