e32tools/elf2e32/source/elf2e32.cpp
changeset 0 044383f39525
equal deleted inserted replaced
-1:000000000000 0:044383f39525
       
     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 Elf2E32 for the elf2e32 tool
       
    15 // @internalComponent
       
    16 // @released
       
    17 // 
       
    18 //
       
    19 
       
    20 #include "elf2e32.h"
       
    21 #include "errorhandler.h"
       
    22 #include "librarytarget.h"
       
    23 #include "dll_fb_target.h"
       
    24 #include "dll_rebuild_target.h"
       
    25 #include "exetarget.h"
       
    26 #include "polydll_fb_target.h"
       
    27 #include "exexp_fb_target.h"
       
    28 #include "exexp_rebuild_target.h"
       
    29 #include "polydll_rebuild_target.h"
       
    30 #include "stdexe_target.h"
       
    31 #include "filedump.h"
       
    32 
       
    33 #include <iostream>
       
    34 
       
    35 using std::cout;
       
    36 using std::endl;
       
    37 
       
    38 ParameterListInterface * Elf2E32::iInstance=0;
       
    39 
       
    40 /**
       
    41 Constructor for Elf2E32
       
    42 
       
    43 @internalComponent 
       
    44 @released
       
    45 
       
    46 @param aArgc
       
    47  The number of command line arguments passed into the program
       
    48 @param aArgv
       
    49  The listing of all the arguments
       
    50 */
       
    51 Elf2E32::Elf2E32(int aArgc, char **aArgv)
       
    52 {
       
    53 	iParameterListInterface = GetInstance(aArgc, aArgv);
       
    54 }
       
    55 
       
    56 
       
    57 Elf2E32::~Elf2E32()
       
    58 {
       
    59 	delete iInstance;
       
    60 }
       
    61 
       
    62 /**
       
    63 This function creates a single instance of the ParameterManager which is derived from 
       
    64 ParameterListInterface which is the abstract base class.
       
    65 
       
    66 @internalComponent
       
    67 @released
       
    68 
       
    69 @param aArgc
       
    70  The number of command line arguments passed into the program
       
    71 @param aArgv
       
    72  The listing of all the arguments
       
    73 
       
    74 @return A pointer to the newly created ParameterListInterface object which is the 
       
    75  abstract interface
       
    76 */
       
    77 ParameterListInterface * Elf2E32::GetInstance(int aArgc, char ** aArgv)
       
    78 {
       
    79 	if (iInstance == 0)
       
    80 		iInstance = new ParameterManager(aArgc, aArgv);
       
    81 
       
    82 	return iInstance;
       
    83 }
       
    84 
       
    85 /**
       
    86 This function is to select the appropriate use case based on the input values.
       
    87 1. If the input is only DEF file, then the usecase is the Create Library Target.
       
    88    For Library Creation, alongwith the DEF file input, the DSO file option and 
       
    89    the link as option SHOULD be passed. Otherwise, appropriate error message will
       
    90    be generated.
       
    91 @internalComponent
       
    92 @released
       
    93 
       
    94 @return A pointer to the newly created UseCaseBase object
       
    95 
       
    96 */
       
    97 UseCaseBase * Elf2E32::SelectUseCase()
       
    98 {
       
    99 	bool definputoption = iParameterListInterface->DefFileInOption();
       
   100 	bool elfinputoption = iParameterListInterface->ElfFileInOption();
       
   101 	char * deffilein = iParameterListInterface->DefInput();
       
   102 	char * elfin = iParameterListInterface->ElfInput();
       
   103 	bool filedumpoption = iParameterListInterface->FileDumpOption();
       
   104 	int dumpOptions = iParameterListInterface->DumpOptions();
       
   105 	char *filedumpsuboptions = iParameterListInterface->FileDumpSubOptions();
       
   106 
       
   107 	bool e32inputoption = iParameterListInterface->E32ImageInOption();
       
   108 	char * e32in = iParameterListInterface->E32Input();
       
   109 
       
   110 	bool dumpMessageFileOption = iParameterListInterface->DumpMessageFileOption();
       
   111 
       
   112 	if (definputoption && !deffilein)
       
   113 		throw ParameterParserError(NOARGUMENTERROR, "--definput");
       
   114 
       
   115 	if (elfinputoption && !elfin)
       
   116 		throw ParameterParserError(NOARGUMENTERROR, "--elfinput");
       
   117 
       
   118 	if(filedumpoption && !dumpOptions)
       
   119 	{
       
   120 		//throw for wrong options
       
   121 		throw InvalidArgumentError(INVALIDARGUMENTERROR,filedumpsuboptions,"--dump");
       
   122 	}
       
   123 
       
   124 	if(e32inputoption && !e32in)
       
   125 	{
       
   126         throw ParameterParserError(NOARGUMENTERROR, "--e32input");
       
   127 	}
       
   128 	
       
   129 	iTargetType = iParameterListInterface->TargetTypeName();
       
   130 
       
   131 //	if (iTargetType == ETargetTypeNotSet) // Will get this warning in build
       
   132 //		cout << "Warning: Target Type is not specified explicitly" << endl;
       
   133 
       
   134 	if (iTargetType == EInvalidTargetType || iTargetType == ETargetTypeNotSet)
       
   135 	{
       
   136 		if (elfin)
       
   137 		{
       
   138 			if (deffilein)
       
   139 				return iUseCase = new ExportTypeRebuildTarget(iParameterListInterface);
       
   140 			else
       
   141 				return iUseCase = new ElfFileSupplied(iParameterListInterface);
       
   142 		}
       
   143 		else if (filedumpoption || e32in)
       
   144 		{
       
   145 			return iUseCase = new FileDump(iParameterListInterface);
       
   146 		}
       
   147 		else if (deffilein)
       
   148 		{
       
   149 			iTargetType = ELib;
       
   150 		}
       
   151 		else if (dumpMessageFileOption)
       
   152 			return NULL;
       
   153 		else
       
   154 			throw InvalidInvocationError(INVALIDINVOCATIONERROR); //REVISIT
       
   155 	}
       
   156 
       
   157 	switch (iTargetType)
       
   158 	{
       
   159 	case EDll:
       
   160 		if (!deffilein && elfin)
       
   161 			iUseCase = new DLLFBTarget(iParameterListInterface);
       
   162 		else if (deffilein && elfin)
       
   163 			iUseCase = new DLLRebuildTarget(iParameterListInterface);
       
   164 		else if (!elfin)
       
   165 			throw ParameterParserError(NOREQUIREDOPTIONERROR,"--elfinput");
       
   166 
       
   167 		ValidateDSOGeneration(iParameterListInterface, iTargetType);
       
   168 		ValidateE32ImageGeneration(iParameterListInterface, iTargetType);
       
   169 		return iUseCase;
       
   170 	case ELib:
       
   171 		if (deffilein)
       
   172 		{
       
   173 			ValidateDSOGeneration(iParameterListInterface, iTargetType);
       
   174 			return iUseCase = new LibraryTarget(iParameterListInterface);
       
   175 		}
       
   176 		else
       
   177 		{
       
   178 			throw ParameterParserError(NOREQUIREDOPTIONERROR,"--definput");
       
   179 		}
       
   180 		break;
       
   181 	case EExe:
       
   182 		if (elfin)
       
   183 		{
       
   184 			iUseCase = new ExeTarget(iParameterListInterface);
       
   185 			ValidateE32ImageGeneration(iParameterListInterface, iTargetType);
       
   186 		}
       
   187 		else
       
   188 		{
       
   189 			throw ParameterParserError(NOREQUIREDOPTIONERROR,"--elfinput");
       
   190 		}
       
   191 		return iUseCase;
       
   192 	case EPolyDll:
       
   193 		if (!deffilein && elfin)
       
   194 			iUseCase = new POLYDLLFBTarget(iParameterListInterface);
       
   195 		else if (deffilein && elfin)
       
   196 			iUseCase = new POLYDLLRebuildTarget(iParameterListInterface);
       
   197 		else if (!elfin)
       
   198 			throw ParameterParserError(NOREQUIREDOPTIONERROR,"--elfinput");
       
   199 
       
   200 		ValidateE32ImageGeneration(iParameterListInterface, iTargetType);
       
   201 		return iUseCase;
       
   202 	case EExexp:
       
   203 	       	if (!deffilein && elfin)
       
   204                 iUseCase = new ExexpFBTarget(iParameterListInterface);
       
   205 			else if (deffilein && elfin)
       
   206 				iUseCase = new ExExpRebuildTarget(iParameterListInterface);
       
   207 			else if (!elfin)
       
   208 				throw ParameterParserError(NOREQUIREDOPTIONERROR,"--elfinput");
       
   209 
       
   210 		ValidateDSOGeneration(iParameterListInterface,iTargetType);
       
   211 		ValidateE32ImageGeneration(iParameterListInterface, iTargetType);
       
   212 		return iUseCase;
       
   213 	case EStdExe:
       
   214 		iUseCase = new StdExeTarget(iParameterListInterface);
       
   215 		return iUseCase;
       
   216 	default:
       
   217 		throw InvalidInvocationError(INVALIDINVOCATIONERROR);
       
   218 	}
       
   219 
       
   220 	return (iUseCase=0x0);
       
   221 }
       
   222 
       
   223 void Elf2E32::ValidateDSOGeneration(ParameterListInterface *aParameterListInterface, ETargetType aTargetType)
       
   224 {
       
   225 	bool dsofileoutoption = aParameterListInterface->DSOFileOutOption();
       
   226 	bool linkasoption = aParameterListInterface->LinkAsOption();
       
   227 	char * dsofileout = aParameterListInterface->DSOOutput();
       
   228 	char * linkas = aParameterListInterface->LinkAsDLLName();
       
   229 
       
   230 	if (aTargetType != ELib)
       
   231 	{
       
   232 		bool deffileoutoption = aParameterListInterface->DefFileOutOption();
       
   233 
       
   234 		if (!deffileoutoption)
       
   235 			throw ParameterParserError(NOREQUIREDOPTIONERROR,"--defoutput");
       
   236 
       
   237 		char * deffileout = aParameterListInterface->DefOutput();
       
   238 		//Incase if the DEF file name is not passed as an input
       
   239 		if (!deffileout)
       
   240 			throw ParameterParserError(NOARGUMENTERROR,"--defoutput");
       
   241 	}
       
   242 
       
   243 	if (dsofileoutoption && !dsofileout)
       
   244 		throw ParameterParserError(NOARGUMENTERROR, "--dso");
       
   245 	else if (linkasoption && !linkas)
       
   246 		throw ParameterParserError(NOFILENAMEERROR,"--linkas");
       
   247 	else if (!dsofileoutoption && !linkasoption)
       
   248 		throw ParameterParserError(NOREQUIREDOPTIONERROR,"--dso, --linkas");
       
   249 	else if (!dsofileoutoption)
       
   250 		throw ParameterParserError(NOREQUIREDOPTIONERROR,"--dso");
       
   251 	else if (!linkasoption)
       
   252 		throw ParameterParserError(NOREQUIREDOPTIONERROR,"--linkas");
       
   253 
       
   254 }
       
   255 
       
   256 void Elf2E32::ValidateE32ImageGeneration(ParameterListInterface *aParameterListInterface, ETargetType aTargetType)
       
   257 {
       
   258 	bool e32fileoutoption = aParameterListInterface->E32OutOption();
       
   259 	char * e32fileout = aParameterListInterface->E32ImageOutput();
       
   260 
       
   261 	if (!e32fileoutoption)
       
   262 		throw ParameterParserError(NOREQUIREDOPTIONERROR,"--output");
       
   263 	else if (!e32fileout)
       
   264 		throw ParameterParserError(NOARGUMENTERROR,"--output");
       
   265 
       
   266 	UINT uid1option = aParameterListInterface->Uid1Option();
       
   267 	UINT uid1val = aParameterListInterface->Uid1();
       
   268 
       
   269 	if (!uid1option) // REVISIT
       
   270 		throw ParameterParserError(NOREQUIREDOPTIONERROR,"--uid1");
       
   271 	else if (uid1option && !uid1val)
       
   272 		throw ParameterParserError(NOARGUMENTERROR,"--uid1");
       
   273 	else if (aTargetType == EDll && uid1val != 0x10000079)
       
   274 		cout << "UID1 should be set to 0x10000079 for DLL Generation" << endl;
       
   275 	else if (aTargetType == EExe && uid1val != 0x1000007A)
       
   276 		cout << "UID1 should be set to 0x1000007A for EXE Generation" << endl;
       
   277 
       
   278 }
       
   279 
       
   280 /**
       
   281 This function:
       
   282  1. Calls the ParameterAnalyser() which parses the command line options and extracts the inputs. 
       
   283  2. Calls the SelectUseCase() to select the appropriate use case based on the input values.
       
   284  3. Calls the Execute() of the selected use case.
       
   285 @internalComponent
       
   286 @released
       
   287 
       
   288 @return EXIT_SUCCESS if the generation of the target is successful, else EXIT_FAILURE
       
   289 
       
   290 */
       
   291 int Elf2E32::Execute()
       
   292 {
       
   293 	int result = EXIT_SUCCESS;
       
   294 	UseCaseBase * usecase=0;
       
   295 	bool dumpMessageFileOption;
       
   296 	char * dumpMessageFile;
       
   297 
       
   298 	try
       
   299 	{
       
   300 		iParameterListInterface->ParameterAnalyser();
       
   301 
       
   302 		dumpMessageFileOption = iParameterListInterface->DumpMessageFileOption();
       
   303 		dumpMessageFile = iParameterListInterface->DumpMessageFile();
       
   304 
       
   305 	 	if(dumpMessageFileOption)
       
   306 		{
       
   307 			if (dumpMessageFile)
       
   308 			{
       
   309 				//create message file
       
   310 				MessageHandler::GetInstance()->CreateMessageFile(dumpMessageFile);
       
   311 				//return result;
       
   312 			}
       
   313 			else
       
   314 			//dumpmessage file name is not provided as input
       
   315 			throw ParameterParserError(NOARGUMENTERROR, "--dumpmessagefile");
       
   316 		}
       
   317 	
       
   318 		usecase = SelectUseCase();
       
   319 		if (usecase)
       
   320 		{
       
   321 			result = usecase->Execute();
       
   322 		}
       
   323 		else if (dumpMessageFileOption)
       
   324 		{
       
   325 			return result;
       
   326 		}
       
   327 		else
       
   328 		{
       
   329 			result = EXIT_FAILURE;
       
   330 		}
       
   331 	}
       
   332 	catch(ErrorHandler& error) 
       
   333 	{ 
       
   334 		result = EXIT_FAILURE;
       
   335 		error.Report();
       
   336 	} 
       
   337 	catch(...) // If there are any other unhandled exception,they are handled here.
       
   338 	{
       
   339 		result = EXIT_FAILURE;
       
   340 		MessageHandler::GetInstance()->ReportMessage(ERROR, POSTLINKERERROR);
       
   341 	} 
       
   342 	delete usecase;
       
   343 	return result;
       
   344 }
       
   345 
       
   346 
       
   347