toolsandutils/e32tools/e32image/tr_main.cpp
changeset 0 83f4b4db085c
child 10 d4b442d23379
equal deleted inserted replaced
-1:000000000000 0:83f4b4db085c
       
     1 // Copyright (c) 1996-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 // e32tools/e32image/tr_main.cpp
       
    15 // Translate X->E32Image top level
       
    16 // 
       
    17 //
       
    18 
       
    19 #ifndef __LINUX__
       
    20   #include <io.h>
       
    21 #endif
       
    22 #include <string.h>
       
    23 
       
    24 #ifdef __VC32__
       
    25  #ifdef __MSVCDOTNET__
       
    26   #include <strstream>
       
    27   #include <iomanip>
       
    28  #else //!__MSVCDOTNET__
       
    29   #include <strstrea.h>
       
    30   #include <iomanip.h>
       
    31  #endif //__MSVCDOTNET__
       
    32 #else // !__VC32__*/
       
    33 #ifdef __TOOLS2__
       
    34   #include <sstream>
       
    35   #include <iomanip>
       
    36   #else
       
    37  #include <strstream.h>
       
    38  #include <iomanip.h>
       
    39 #endif // __VC32__
       
    40 #endif
       
    41 
       
    42 #include <e32std.h>
       
    43 #include <e32std_private.h>
       
    44 
       
    45 #ifdef __SUPPORT_PE_FILES__
       
    46 #include "pe_file.h"
       
    47 #endif
       
    48 #ifdef __SUPPORT_ELF_FILES__
       
    49 #include "elftran.h"
       
    50 #endif
       
    51 
       
    52 #include "h_utl.h"
       
    53 #include "h_ver.h"
       
    54 #include <stdio.h>
       
    55 
       
    56 extern int gAlignConstSection;
       
    57 extern TUint gConstSectionAddressMask;
       
    58 
       
    59 int gVerbose=0;
       
    60 char *gFile1=NULL;
       
    61 char *gFile2=NULL;
       
    62 unsigned int gStack=0;
       
    63 unsigned int gHeapMin=0;
       
    64 unsigned int gHeapMax=0;
       
    65 TUid gUid1=KNullUid;
       
    66 TUid gUid2=KNullUid;
       
    67 TUid gUid3=KNullUid;
       
    68 unsigned int gSecureId=0;
       
    69 unsigned int gVendorId=0;
       
    70 unsigned int gVersionWord=0x00010000u;
       
    71 int gCallEntryPoints=TRUE;
       
    72 int gFixedAddress=FALSE;
       
    73 int gPriority=EPriorityForeground;
       
    74 SCapabilitySet gCapability={0};
       
    75 int gAllowDllData=FALSE;
       
    76 // fix warning for Linux warning: 0 instead of NULL
       
    77 TUint gDataBase=0;
       
    78 int gCompress=TRUE;
       
    79 unsigned int gFPU=0;
       
    80 
       
    81 int gCodePaged=FALSE;
       
    82 int gCodeUnpaged=FALSE;
       
    83 int gCodeDefaultPaged=FALSE;
       
    84 
       
    85 int gDataPaged=FALSE;
       
    86 int gDataUnpaged=FALSE;
       
    87 int gDataDefaultPaged=FALSE;
       
    88 
       
    89 int gDebuggable=FALSE;
       
    90 int gSmpSafe=FALSE;
       
    91 
       
    92 int gSetStack=FALSE;
       
    93 int gSetHeap=FALSE;
       
    94 int gSetUid1=FALSE;
       
    95 int gSetUid2=FALSE;
       
    96 int gSetUid3=FALSE;
       
    97 int gSetCallEntryPoints=FALSE;
       
    98 int gSetFixedAddress=FALSE;
       
    99 int gSetPriority=FALSE;
       
   100 int gSetCapability=FALSE;
       
   101 int gSetCompress=FALSE;
       
   102 int gSetVersion=FALSE;
       
   103 int gSetSecureId=FALSE;
       
   104 int gSetVendorId=FALSE;
       
   105 int gSetFPU=FALSE;
       
   106 
       
   107 int gSetCodePaged=FALSE;
       
   108 int gSetDataPaged=FALSE;
       
   109 
       
   110 int gSetSymLkup=FALSE;
       
   111 int gSetDebuggable=FALSE;
       
   112 int gSetSmpSafe=FALSE;
       
   113 
       
   114 enum CompressionMethods
       
   115 {
       
   116 	ENoCompression = 0,
       
   117 	EDeflate = 1,
       
   118 	EBytePair = 2,
       
   119 };
       
   120 
       
   121 int gCompressionMethod = EDeflate;
       
   122 int gSuppressComprMethod = FALSE;
       
   123 
       
   124 #ifdef __SUPPORT_PE_FILES__
       
   125 char* gX86imp=NULL;
       
   126 int gX86num_imp_dlls=0;
       
   127 int gX86imp_size=0;
       
   128 int gX86num_imports=0;
       
   129 #endif
       
   130 
       
   131 TBool gLittleEndian=ETrue;
       
   132 
       
   133 class E32ImageFileRef
       
   134 	{
       
   135 public:
       
   136 	E32ImageFileRef() {iPtr = E32ImageFile::New();}
       
   137 	~E32ImageFileRef() {delete iPtr;}
       
   138 	class E32ImageFile& Ref() {return *iPtr;}
       
   139 private:
       
   140 	E32ImageFileRef(const E32ImageFileRef&);
       
   141 	E32ImageFileRef& operator=(const E32ImageFileRef&);
       
   142 private:
       
   143 	E32ImageFile* iPtr;
       
   144 	};
       
   145 
       
   146 int setPagedFlags(E32ImageFile& f)
       
   147 	{
       
   148 	unsigned check1 = gCodePaged + gCodeUnpaged + gCodeDefaultPaged;
       
   149 	unsigned check2 = gDataPaged + gDataUnpaged + gDataDefaultPaged;
       
   150 
       
   151 	if (check1 > 1 || check2 > 1)
       
   152 		{
       
   153 		Print(EError, "Conflicting paging options.\n");
       
   154 		return KErrArgument;
       
   155 		}
       
   156 
       
   157 	if (gCodePaged)
       
   158 		{
       
   159 		f.iHdr->iFlags |= KImageCodePaged;
       
   160 		f.iHdr->iFlags &= ~KImageCodeUnpaged;
       
   161 		}
       
   162 	else if (gCodeUnpaged)
       
   163 		{
       
   164 		f.iHdr->iFlags |= KImageCodeUnpaged;
       
   165 		f.iHdr->iFlags &= ~KImageCodePaged;
       
   166 		}
       
   167 	else if (gCodeDefaultPaged)
       
   168 		{
       
   169 		f.iHdr->iFlags &= ~KImageCodePaged;
       
   170 		f.iHdr->iFlags &= ~KImageCodeUnpaged;
       
   171 		}
       
   172 
       
   173 	if (gDataPaged)
       
   174 		{
       
   175 		f.iHdr->iFlags |=  KImageDataPaged;
       
   176 		f.iHdr->iFlags &= ~KImageDataUnpaged;
       
   177 		}
       
   178 	else if (gDataUnpaged)
       
   179 		{
       
   180 		f.iHdr->iFlags |=  KImageDataUnpaged;
       
   181 		f.iHdr->iFlags &= ~KImageDataPaged;
       
   182 		}
       
   183 	else if (gDataDefaultPaged)
       
   184 		{
       
   185 		f.iHdr->iFlags &= ~KImageDataPaged;
       
   186 		f.iHdr->iFlags &= ~KImageDataUnpaged;
       
   187 		}
       
   188 
       
   189 	return KErrNone;
       
   190 	}
       
   191 
       
   192 void setDebuggableFlags(E32ImageFile& f)
       
   193 	{
       
   194 	if (gDebuggable)
       
   195 		{
       
   196 		f.iHdr->iFlags |= KImageDebuggable;
       
   197 		}
       
   198 	else
       
   199 		{
       
   200 		f.iHdr->iFlags &= ~KImageDebuggable;
       
   201 		}
       
   202 	}
       
   203 
       
   204 void setSmpSafeFlags(E32ImageFile& f)
       
   205 	{
       
   206 	if (gSmpSafe)
       
   207 		{
       
   208 		f.iHdr->iFlags |= KImageSMPSafe;
       
   209 		}
       
   210 	else
       
   211 		{
       
   212 		f.iHdr->iFlags &= ~KImageSMPSafe;
       
   213 		}
       
   214 	}
       
   215 
       
   216 int dotran(const char* ifilename, const char* ofilename)
       
   217 	{
       
   218 	E32ImageFileRef fRef;
       
   219 	E32ImageFile& f = fRef.Ref();
       
   220 	int r=f.Translate(ifilename, gDataBase, gAllowDllData, gSetSymLkup);
       
   221 	if (r!=KErrNone)
       
   222 		return r;
       
   223 	if (gSetStack)
       
   224 		f.SetStackSize(gStack);
       
   225 	if (gSetHeap)
       
   226 		{
       
   227 		f.SetHeapSizeMin(gHeapMin);
       
   228 		f.SetHeapSizeMax(gHeapMax);
       
   229 		}
       
   230 	if (!gSetUid1)
       
   231 		gUid1=TUid::Uid(f.iHdr->iUid1);
       
   232 	if (!gSetUid2)
       
   233 		gUid2=TUid::Uid(f.iHdr->iUid2);
       
   234 	if (!gSetUid3)
       
   235 		gUid3=TUid::Uid(f.iHdr->iUid3);
       
   236 	if (!gSetSecureId)
       
   237 		gSecureId = f.iHdr->iUid3;
       
   238 	if (!gSetVendorId)
       
   239 		gVendorId = 0;
       
   240 	f.SetUids(gUid1, gUid2, gUid3);
       
   241 	f.SetSecureId(gSecureId);
       
   242 	f.SetVendorId(gVendorId);
       
   243 	if (gSetCallEntryPoints)
       
   244 		f.SetCallEntryPoints(gCallEntryPoints);
       
   245 	if (gSetCapability)
       
   246 		f.SetCapability(gCapability);
       
   247 	if (gSetPriority)
       
   248 		{
       
   249 		if (f.iHdr->iFlags&KImageDll)
       
   250 			Print(EWarning,"Cannot set priority of a DLL.\n");
       
   251 		else
       
   252 			f.SetPriority((TProcessPriority)gPriority);
       
   253 		}
       
   254 	if (gSetFixedAddress)
       
   255 		{
       
   256 		if (f.iHdr->iFlags&KImageDll)
       
   257 			Print(EWarning,"Cannot set fixed address for DLL.\n");
       
   258 		else
       
   259 			f.SetFixedAddress(gFixedAddress);
       
   260 		}
       
   261 	if (gSetVersion)
       
   262 		f.iHdr->iModuleVersion = gVersionWord;
       
   263 
       
   264 	if(gCompress)
       
   265 	{
       
   266 		switch(gCompressionMethod)
       
   267 		{
       
   268 			case ENoCompression:
       
   269 				f.iHdr->iCompressionType = KFormatNotCompressed;
       
   270 				break;
       
   271 				
       
   272 			case EDeflate:
       
   273 				f.iHdr->iCompressionType = KUidCompressionDeflate;
       
   274 			
       
   275 				break;
       
   276 			
       
   277 			case EBytePair:
       
   278 				f.iHdr->iCompressionType = KUidCompressionBytePair;
       
   279 			
       
   280 				break;
       
   281 			
       
   282 			default:
       
   283 				Print(EError, "Unknown compression method:%d", gCompressionMethod);
       
   284 				return 1;
       
   285 			
       
   286 		} // End of switch()
       
   287 		
       
   288 	}
       
   289 	else
       
   290 	{
       
   291 		f.iHdr->iCompressionType = KFormatNotCompressed;		
       
   292 	}
       
   293 		
       
   294 
       
   295 	if (gSetFPU)
       
   296 		f.SetFPU(gFPU);
       
   297 
       
   298 	r = setPagedFlags(f);
       
   299 	if (r != KErrNone)
       
   300 	{
       
   301 		return r;
       
   302 	}
       
   303 
       
   304 	setDebuggableFlags(f);
       
   305 
       
   306 	setSmpSafeFlags(f);
       
   307 
       
   308 	f.CreateExportBitMap();
       
   309 	f.AddExportDescription();
       
   310 	f.UpdateHeaderCrc();
       
   311 	r = f.Validate();
       
   312 	if (r!=KErrNone)
       
   313 		return r;
       
   314 
       
   315 	ofstream ofile(ofilename, ios::binary);
       
   316 	if (!ofile)
       
   317 		{
       
   318 		Print(EError,"Cannot open %s for output.\n",ofilename);
       
   319 		return 1;
       
   320 		}
       
   321 	ofile << f;
       
   322 	ofile.close();
       
   323 	if (gVerbose)
       
   324 		f.Dump((TText*)ofilename,gVerbose);
       
   325 	return KErrNone;
       
   326 	}
       
   327 
       
   328 
       
   329 int dodump(const char* ifilename)
       
   330 	{
       
   331 	E32ImageFile f;
       
   332 	TInt r = f.Open(ifilename);
       
   333 	if (r>0)
       
   334 		return 1;
       
   335 	else if (r==KErrCorrupt || r==KErrNotSupported)
       
   336 		{
       
   337 		Print(EError,"%s is not a valid E32Image file.\n",ifilename);
       
   338 		return 1;
       
   339 		}
       
   340 	else if (r!=0)
       
   341 		{
       
   342 		Print(EError,"Error %d reading %s.\n",r,ifilename);
       
   343 		return 1;
       
   344 		}
       
   345 	f.Dump((TText*)ifilename, gVerbose ? gVerbose : E32ImageFile::EDumpDefaults);
       
   346 	return KErrNone;
       
   347 	}
       
   348 
       
   349 int doalter(const char* ifilename)
       
   350 	{
       
   351 	E32ImageFile f;
       
   352 	TInt r = f.Open(ifilename);
       
   353 	if (r>0)
       
   354 		return 1;
       
   355 	else if (r==KErrCorrupt || r==KErrNotSupported)
       
   356 		{
       
   357 		Print(EError,"%s is not a valid E32Image file.\n",ifilename);
       
   358 		return 1;
       
   359 		}
       
   360 	else if (r!=0)
       
   361 		{
       
   362 		Print(EError,"Error %d reading %s.\n",r,ifilename);
       
   363 		return 1;
       
   364 		}
       
   365 
       
   366 	TUint hdrfmt = f.iHdr->HeaderFormat();
       
   367 	if (hdrfmt != KImageHdrFmt_V)
       
   368 		{
       
   369 		Print(EError,"Can't modify old format binaries\n");
       
   370 		return 1;
       
   371 		}
       
   372 
       
   373 	if (gDataBase)
       
   374 		{
       
   375 		Print(EWarning, "Ignoring -datalinkaddress Switch");
       
   376 		}
       
   377 	if (gSetStack)
       
   378 		f.SetStackSize(gStack);
       
   379 	if (gSetHeap)
       
   380 		{
       
   381 		f.SetHeapSizeMin(gHeapMin);
       
   382 		f.SetHeapSizeMax(gHeapMax);
       
   383 		}
       
   384 	if (!gSetUid1)
       
   385 		gUid1=TUid::Uid(f.iHdr->iUid1);
       
   386 	if (!gSetUid2)
       
   387 		gUid2=TUid::Uid(f.iHdr->iUid2);
       
   388 	if (!gSetUid3)
       
   389 		gUid3=TUid::Uid(f.iHdr->iUid3);
       
   390 	f.SetUids(gUid1, gUid2, gUid3);
       
   391 	if (gSetSecureId)
       
   392 		f.SetSecureId(gSecureId);
       
   393 	if (gSetVendorId)
       
   394 		f.SetVendorId(gVendorId);
       
   395 	if (gSetCallEntryPoints)
       
   396 		f.SetCallEntryPoints(gCallEntryPoints);
       
   397 	if (gSetCapability)
       
   398 		f.SetCapability(gCapability);
       
   399 	if (gSetPriority)
       
   400 		{
       
   401 		if (f.iHdr->iFlags&KImageDll)
       
   402 			Print(EWarning,"Cannot set priority of a DLL.\n");
       
   403 		else
       
   404 			f.SetPriority((TProcessPriority)gPriority);
       
   405 		}
       
   406 	if (gSetFixedAddress)
       
   407 		{
       
   408 		if (f.iHdr->iFlags&KImageDll)
       
   409 			Print(EWarning,"Cannot set fixed address for DLL.\n");
       
   410 		else
       
   411 			f.SetFixedAddress(gFixedAddress);
       
   412 		}
       
   413 	if (gSetVersion)
       
   414 		f.iHdr->iModuleVersion = gVersionWord;
       
   415 
       
   416 	if(gCompress)
       
   417 	{
       
   418 		switch(gCompressionMethod)
       
   419 		{
       
   420 			case ENoCompression:
       
   421 				f.iHdr->iCompressionType = KFormatNotCompressed;
       
   422 				break;
       
   423 				
       
   424 			case EDeflate:
       
   425 				f.iHdr->iCompressionType = KUidCompressionDeflate;
       
   426 			
       
   427 				break;
       
   428 			
       
   429 			case EBytePair:
       
   430 				f.iHdr->iCompressionType = KUidCompressionBytePair;
       
   431 			
       
   432 				break;
       
   433 			
       
   434 			default:
       
   435 				Print(EError, "Unknown compression method:%d", gCompressionMethod);
       
   436 				return 1;
       
   437 			
       
   438 		} // End of switch()
       
   439 	}
       
   440 	else
       
   441 		f.iHdr->iCompressionType = KFormatNotCompressed;
       
   442 
       
   443 	if (gSetFPU)
       
   444 		f.SetFPU(gFPU);
       
   445 
       
   446 	r = setPagedFlags(f);
       
   447 	if (r != KErrNone)
       
   448 	{
       
   449 		return r;
       
   450 	}
       
   451 	
       
   452 	setDebuggableFlags(f);
       
   453 
       
   454 	setSmpSafeFlags(f);
       
   455 
       
   456 	f.UpdateHeaderCrc();
       
   457 	r = f.Validate();
       
   458 	if (r!=KErrNone)
       
   459 		return r;
       
   460 
       
   461 	ofstream ofile(ifilename, ios::binary);
       
   462 	if (!ofile)
       
   463 		{
       
   464 		Print(EError,"Cannot open %s for output.\n",ifilename);
       
   465 		return 1;
       
   466 		}
       
   467 	ofile << f;
       
   468 	ofile.close();
       
   469 	if (gVerbose)
       
   470 		f.Dump((TText *)ifilename,gVerbose);
       
   471 	return KErrNone;
       
   472 	}
       
   473 
       
   474 int helpme(char *aStr)
       
   475 	{
       
   476 	Print(EAlways,"Syntax: %s [options] inputfile outputfile\n",aStr);
       
   477 	Print(EAlways,"        %s [options] e32imagefile\n",aStr);
       
   478 	Print(EAlways,"option: [-v] [[-no]call[entrypoint]] [-priority <priority>]\n");
       
   479 	Print(EAlways,"        [-stack <size>] [-heap <min> <max>] [-uid<n> <uid>]\n");
       
   480 	Print(EAlways,"        [-allowdlldata] [-datalinkaddress <base>] [-fixed] [-moving]\n");
       
   481 	Print(EAlways,"        [-align-const-section] [-const-section-address-mask <mask>]\n");
       
   482 	Print(EAlways,"        [-[no]compress] [-compressionmethod none|deflate|bytepair]\n");
       
   483 	Print(EAlways,"        [-capability \"<list>\"] [-version M.m] [-vid <id>]\n");
       
   484 	Print(EAlways,"        [-fpu <softvfp|vfpv2>]\n");
       
   485 	Print(EAlways,"        [-codepaging <paged|unpaged|default>]\n");
       
   486 	Print(EAlways,"        [-datapaging <paged|unpaged|default>]\n");
       
   487 	Print(EAlways,"        [-debuggable]\n");
       
   488 	Print(EAlways,"        [-smpsafe]\n");
       
   489 	Print(EAlways,"        [-sym_name_lkup]\n");
       
   490 	Print(EAlways,"        [-dump [h][s][c][d][e][i]]\n");
       
   491 	Print(EAlways,"flags for dump: h Header\n");
       
   492 	Print(EAlways,"                s Security info\n");
       
   493 	Print(EAlways,"                c Code section\n");
       
   494 	Print(EAlways,"                d Data section\n");
       
   495 	Print(EAlways,"                e Export info\n");
       
   496 	Print(EAlways,"                i Import table\n");
       
   497 	return KErrArgument;
       
   498 	}
       
   499 
       
   500 int isNumber(char *aStr)
       
   501 	{
       
   502 	return (aStr[0]>='0') && (aStr[0]<='9');
       
   503 	}
       
   504 
       
   505 int getUIntArg(unsigned int &aVal, int argc, char *argv[], int i)
       
   506 	{
       
   507 	if (i>=argc)
       
   508 		return KErrArgument;
       
   509 	if (!isNumber(argv[i]))
       
   510 		return KErrArgument;
       
   511 #ifdef __LINUX__
       
   512 	int n;
       
   513 	sscanf(argv[i], "%i", &n);
       
   514 	aVal = n;
       
   515 #else
       
   516 #ifdef __TOOLS2__
       
   517 istringstream s(argv[i]/*, strlen(argv[i])*/);
       
   518 #else
       
   519 istrstream s(argv[i], strlen(argv[i]));
       
   520 #endif
       
   521 
       
   522 #if defined(__MSVCDOTNET__) || defined(__TOOLS2__)
       
   523 	s >> setbase(0);
       
   524 #endif //__MSVCDOTNET__
       
   525 
       
   526 	s >> aVal;
       
   527 #endif // __LINUX__
       
   528 	return KErrNone;
       
   529 	}
       
   530 
       
   531 int getCapabilitiesArg(SCapabilitySet& aVal, int argc, char *argv[], int i)
       
   532 	{
       
   533 	memset(&aVal,0,sizeof(aVal));
       
   534 	if (i>=argc)
       
   535 		return KErrArgument;
       
   536 	if (isNumber(argv[i]))
       
   537 		return getUIntArg(*(TUint*)&aVal[0], argc, argv, i);
       
   538 	return ParseCapabilitiesArg(aVal,argv[i]);
       
   539 	}
       
   540 
       
   541 int getPriorityArg(int &aVal, int argc, char *argv[], int i)
       
   542 	{
       
   543 
       
   544 	if (i>=argc)
       
   545 		return KErrArgument;
       
   546 	if (isNumber(argv[i]))
       
   547 		{
       
   548 #ifdef __LINUX__
       
   549 		int n;
       
   550 		sscanf(argv[i], "%i", &n);
       
   551 		aVal = n;
       
   552 #else
       
   553 #ifdef __TOOLS2__
       
   554 istringstream s(argv[i]);
       
   555 #else
       
   556 istrstream s(argv[i], strlen(argv[i]));
       
   557 #endif
       
   558 
       
   559 #if defined(__MSVCDOTNET__) || defined(__TOOLS2__)
       
   560 		s >> setbase(0);
       
   561 #endif //__MSVCDOTNET__
       
   562 
       
   563 		s>>aVal;
       
   564 #endif // __LINUX__
       
   565 		}
       
   566 	else
       
   567 		{
       
   568 		if (stricmp(argv[i], "low")==0)
       
   569 			aVal=EPriorityLow;
       
   570 		else if (strnicmp(argv[i], "background",4)==0)
       
   571 			aVal=EPriorityBackground;
       
   572 		else if (strnicmp(argv[i], "foreground",4)==0)
       
   573 			aVal=EPriorityForeground;
       
   574 		else if (stricmp(argv[i], "high")==0)
       
   575 			aVal=EPriorityHigh;
       
   576 		else if (strnicmp(argv[i], "windowserver",3)==0)
       
   577 			aVal=EPriorityWindowServer;
       
   578 		else if (strnicmp(argv[i], "fileserver",4)==0)
       
   579 			aVal=EPriorityFileServer;
       
   580 		else if (strnicmp(argv[i], "realtime",4)==0)
       
   581 			aVal=EPriorityRealTimeServer;
       
   582 		else if (strnicmp(argv[i], "supervisor",3)==0)
       
   583 			aVal=EPrioritySupervisor;
       
   584 		else
       
   585 			{
       
   586 			Print(EError, "Unrecognised priority\n");
       
   587 			return KErrArgument;
       
   588 			}
       
   589 		}
       
   590 	if (aVal<EPriorityLow || aVal>EPrioritySupervisor)
       
   591 		{
       
   592 		Print(EError, "Priority out of range\n");
       
   593 		return KErrArgument;
       
   594 		}
       
   595 	return KErrNone;
       
   596 	}
       
   597 
       
   598 int getVersionArg(unsigned int& aVal, const char* aArg)
       
   599 	{
       
   600 	const char* s = aArg;
       
   601 	unsigned int major = 0;
       
   602 	unsigned int minor = 0;
       
   603 	for (; major<=6553 && *s>='0' && *s<='9'; ++s)
       
   604 		major = major*10 + (*s - '0');
       
   605 	if (*s == '.')
       
   606 		{
       
   607 		for (++s; minor<=6553 && *s>='0' && *s<='9'; ++s)
       
   608 			minor = minor*10 + (*s - '0');
       
   609 		if (*s==0 && major<32768 && minor<32768)
       
   610 			{
       
   611 			aVal = (major << 16) | minor;
       
   612 			return KErrNone;
       
   613 			}
       
   614 		}
       
   615 	Print(EError, "Bad argument to -version\n");
       
   616 	return KErrArgument;
       
   617 	}
       
   618 
       
   619 int getFPUArg(unsigned int &aVal, int argc, char *argv[], int i)
       
   620 	{
       
   621 	if (i>=argc)
       
   622 		return KErrArgument;
       
   623 	else if (strnicmp(argv[i], "softvfp", 7)==0)
       
   624 		aVal = 0;
       
   625 	else if (strnicmp(argv[i], "vfpv2", 5)==0)
       
   626 		aVal = 1;
       
   627 	else
       
   628 		{
       
   629 		Print(EError, "Bad argument to -fpu\n");
       
   630 		return KErrArgument;
       
   631 		}
       
   632 
       
   633 	return KErrNone;
       
   634 	}
       
   635 
       
   636 int getCompressionMethod(int &aVal, int argc, char *argv[], int i)
       
   637 {
       
   638 	if( i >= argc || argv[i] == NULL)
       
   639 	{
       
   640 		Print(EError, "Missing argument to -compressionmethod\n");
       
   641 		return KErrArgument;
       
   642 	}
       
   643 	else if (strnicmp(argv[i], "none", 2) == 0)
       
   644 	{
       
   645 		aVal = ENoCompression;
       
   646 	}
       
   647 	else if (strnicmp(argv[i], "deflate", 7) == 0)
       
   648 	{
       
   649 		aVal = EDeflate;
       
   650 	}
       
   651 	else if (strnicmp(argv[i], "bytepair", 8) == 0)
       
   652 	{
       
   653 		aVal = EBytePair;
       
   654 	}
       
   655 	else
       
   656 	{
       
   657 		Print(EError, "Bad argument '%s' to -compressionmethod\n", argv[i]);
       
   658 		return KErrArgument;
       
   659 	}
       
   660 
       
   661 	return KErrNone;
       
   662 	
       
   663 }
       
   664 
       
   665 
       
   666 int processCL(int argc, char *argv[])
       
   667 	{
       
   668 
       
   669 	int r=KErrNone;
       
   670 	int i=1;
       
   671 	while (i<argc)
       
   672 		{
       
   673 		if (stricmp("-v", argv[i])==0)
       
   674 			gVerbose |= E32ImageFile::EDumpDefaults;
       
   675 		else if (stricmp("-dump", argv[i])==0)
       
   676 			{
       
   677 			i++;
       
   678 			if (i>=argc)
       
   679 				return KErrArgument;
       
   680 			char* s=argv[i];
       
   681 			while(char c = *(s++))
       
   682 				{
       
   683 				if(c<'a')
       
   684 					c += 'a'-'A';
       
   685 				switch(c)
       
   686 					{
       
   687 				case 'h': gVerbose |= E32ImageFile::EDumpHeader; break;
       
   688 				case 's': gVerbose |= E32ImageFile::EDumpSecurityInfo; break;
       
   689 				case 'c': gVerbose |= E32ImageFile::EDumpCode; break;
       
   690 				case 'd': gVerbose |= E32ImageFile::EDumpData; break;
       
   691 				case 'e': gVerbose |= E32ImageFile::EDumpExports; break;
       
   692 				case 'i': gVerbose |= E32ImageFile::EDumpImports; break;
       
   693 				default: return KErrArgument;
       
   694 					}
       
   695 				}
       
   696 			}
       
   697 		else if (stricmp("-stack", argv[i])==0)
       
   698 			{
       
   699 			i++;
       
   700 			gSetStack=TRUE;
       
   701 			r=getUIntArg(gStack, argc, argv, i);
       
   702 			}
       
   703 		else if (stricmp("-uid1", argv[i])==0)
       
   704 			{
       
   705 			i++;
       
   706 			gSetUid1=TRUE;
       
   707 			unsigned int id;
       
   708 			r=getUIntArg(id, argc, argv, i);
       
   709 			gUid1=TUid::Uid(id);
       
   710 			}
       
   711 		else if (stricmp("-uid2", argv[i])==0)
       
   712 			{
       
   713 			i++;
       
   714 			gSetUid2=TRUE;
       
   715 			unsigned int id;
       
   716 			r=getUIntArg(id, argc, argv, i);
       
   717 			gUid2=TUid::Uid(id);
       
   718 			}
       
   719 		else if (stricmp("-uid3", argv[i])==0)
       
   720 			{
       
   721 			i++;
       
   722 			gSetUid3=TRUE;
       
   723 			unsigned int id;
       
   724 			r=getUIntArg(id, argc, argv, i);
       
   725 			gUid3=TUid::Uid(id);
       
   726 			}
       
   727 		else if (stricmp("-version", argv[i])==0)
       
   728 			{
       
   729 			i++;
       
   730 			r=getVersionArg(gVersionWord, argv[i]);
       
   731 			gSetVersion=TRUE;
       
   732 			}
       
   733 		else if (stricmp("-sid", argv[i])==0)
       
   734 			{
       
   735 			i++;
       
   736 			r=getUIntArg(gSecureId, argc, argv, i);
       
   737 			gSetSecureId=TRUE;
       
   738 			}
       
   739 		else if (stricmp("-vid", argv[i])==0)
       
   740 			{
       
   741 			i++;
       
   742 			r=getUIntArg(gVendorId, argc, argv, i);
       
   743 			gSetVendorId=TRUE;
       
   744 			}
       
   745 		else if (strnicmp("-nocall", argv[i], 7)==0)
       
   746 			{
       
   747 			gSetCallEntryPoints=TRUE;
       
   748 			gCallEntryPoints=FALSE;
       
   749 			}
       
   750 		else if (strnicmp("-call", argv[i], 5)==0)
       
   751 			{
       
   752 			gSetCallEntryPoints=TRUE;
       
   753 			gCallEntryPoints=TRUE;
       
   754 			}
       
   755 		else if (strnicmp("-fixed", argv[i], 3)==0)
       
   756 			{
       
   757 			gSetFixedAddress=TRUE;
       
   758 			gFixedAddress=TRUE;
       
   759 			}
       
   760 		else if (strnicmp("-moving", argv[i], 3)==0)
       
   761 			{
       
   762 			gSetFixedAddress=TRUE;
       
   763 			gFixedAddress=FALSE;
       
   764 			}
       
   765 		else if (strnicmp("-priority", argv[i], 4)==0)
       
   766 			{
       
   767 			i++;
       
   768 			gSetPriority=TRUE;
       
   769 			r=getPriorityArg(gPriority,argc,argv,i);
       
   770 			}
       
   771 		else if (strnicmp("-capability", argv[i], 10)==0)
       
   772 			{
       
   773 			i++;
       
   774 			gSetCapability=TRUE;
       
   775 			r=getCapabilitiesArg(gCapability, argc, argv, i);
       
   776 			}
       
   777 		else if (strnicmp("-heap", argv[i], 4)==0)
       
   778 			{
       
   779 			i++;
       
   780 			gSetHeap=TRUE;
       
   781 			r=getUIntArg(gHeapMin, argc, argv, i);
       
   782 			if (r==KErrNone)
       
   783 				r=getUIntArg(gHeapMax, argc, argv, ++i);
       
   784 			}
       
   785 		else if (strnicmp("-allow", argv[i], 6)==0) // Note, toolchain passes 'allow' for 'allowdlldata'
       
   786 			{
       
   787 			gAllowDllData=TRUE;
       
   788 			}
       
   789 		else if( strnicmp("-compressionmethod", argv[i], 18) == 0)
       
   790 		{
       
   791 			if(!gSuppressComprMethod)
       
   792 			{
       
   793 				gCompress = TRUE;
       
   794 				gSetCompress = TRUE;
       
   795 				r = getCompressionMethod(gCompressionMethod, argc, argv, ++i);  
       
   796 			}
       
   797 			else
       
   798 			{
       
   799 				++i; // Skip the compression method because compessionmethod suppressed.
       
   800 			}
       
   801 		}
       
   802 		else if (strnicmp("-compress", argv[i], 9)==0)
       
   803 			{
       
   804 			gCompress=TRUE;
       
   805 			gSetCompress=TRUE;
       
   806 			gSuppressComprMethod=FALSE;
       
   807 			}
       
   808 		else if (strnicmp("-nocompress", argv[i], 11)==0)
       
   809 			{
       
   810 			gCompress=FALSE;
       
   811 			gSetCompress=TRUE;
       
   812 			gSuppressComprMethod = TRUE;
       
   813 			gCompressionMethod = ENoCompression;
       
   814 			}
       
   815 		else if (strnicmp("-datalinkaddress", argv[i], 16)==0)
       
   816 			{
       
   817 			i++;
       
   818 			r=getUIntArg(gDataBase, argc, argv, i);
       
   819 			}
       
   820 		else if (strnicmp("-align-const-section", argv[i], 20)==0)
       
   821 			{
       
   822 			gAlignConstSection=TRUE;
       
   823 			}
       
   824 		else if (strnicmp("-const-section-address-mask", argv[i], 27)==0)
       
   825 			{
       
   826 			i++;
       
   827 			r=getUIntArg(gConstSectionAddressMask, argc, argv, i);
       
   828 			}
       
   829 		else if (strnicmp("-fpu", argv[i], 4)==0)
       
   830 			{
       
   831 			i++;
       
   832 			r=getFPUArg(gFPU, argc, argv, i);
       
   833 			gSetFPU=TRUE;
       
   834 			}
       
   835 		else if (strnicmp("-paged", argv[i], 6) == 0)
       
   836 			{
       
   837 			gCodePaged=TRUE;
       
   838 			gSetCodePaged=TRUE;
       
   839 			}
       
   840 		else if (strnicmp("-unpaged", argv[i], 8) == 0)
       
   841 			{
       
   842 			gCodeUnpaged=TRUE;
       
   843 			gSetCodePaged=TRUE;
       
   844 			}
       
   845 		else if (strnicmp("-defaultpaged", argv[i], 13) == 0)
       
   846 			{
       
   847 			gCodeDefaultPaged=TRUE;
       
   848 			gSetCodePaged=TRUE;
       
   849 			}
       
   850 		else if (strnicmp("-codepaging", argv[i], 11)==0)
       
   851 			{
       
   852 			i++;
       
   853 
       
   854 			if (i>=argc)
       
   855 				{
       
   856 				r = KErrArgument;
       
   857 				}
       
   858 			else if ( strnicmp(argv[i], "paged", 5) == 0 )
       
   859 				{
       
   860 				gCodePaged=TRUE;
       
   861 				}
       
   862 			else if ( strnicmp(argv[i], "unpaged", 7) == 0 )
       
   863 				{
       
   864 				gCodeUnpaged=TRUE;
       
   865 				}
       
   866 			else if ( strnicmp(argv[i], "default", 7) == 0 )
       
   867 				{
       
   868 				gCodeDefaultPaged=TRUE;
       
   869 				}
       
   870 			else
       
   871 				{
       
   872 				Print(EError, "Bad argument to -codepaging\n");
       
   873 				r = KErrArgument;
       
   874 				}
       
   875 
       
   876 			gSetCodePaged=TRUE;
       
   877 			}
       
   878 		else if (strnicmp("-datapaging", argv[i], 11)==0)
       
   879 			{
       
   880 			i++;
       
   881 
       
   882 			if (i>=argc)
       
   883 				{
       
   884 				r = KErrArgument;
       
   885 				}
       
   886 			else if ( strnicmp(argv[i], "paged", 5) == 0 )
       
   887 				{
       
   888 				gDataPaged=TRUE;
       
   889 				}
       
   890 			else if ( strnicmp(argv[i], "unpaged", 7) == 0 )
       
   891 				{
       
   892 				gDataUnpaged=TRUE;
       
   893 				}
       
   894 			else if ( strnicmp(argv[i], "default", 7) == 0 )
       
   895 				{
       
   896 				gDataDefaultPaged=TRUE;
       
   897 				}
       
   898 			else
       
   899 				{
       
   900 				Print(EError, "Bad argument to -datapaging\n");
       
   901 				r = KErrArgument;
       
   902 				}
       
   903 
       
   904 			gSetDataPaged=TRUE;
       
   905 			}
       
   906 		else if (strnicmp("-sym_name_lkup", argv[i], 14) == 0)
       
   907 			{
       
   908 			gSetSymLkup=TRUE;
       
   909 			}
       
   910 		else if (strnicmp("-debuggable", argv[i], 11) == 0)
       
   911 			{
       
   912 			gDebuggable=TRUE;
       
   913 			gSetDebuggable=TRUE;
       
   914 			}
       
   915 		else if (strnicmp("-smpsafe", argv[i], 8) == 0)
       
   916 			{
       
   917 			gSmpSafe=TRUE;
       
   918 			gSetSmpSafe=TRUE;
       
   919 			}
       
   920 #ifdef __SUPPORT_PE_FILES__
       
   921 		else if (strnicmp("-x86imp=", argv[i], 8)==0)
       
   922 			{
       
   923 			const char* x86impfile=argv[i]+8;
       
   924 //			printf("%s\n",x86impfile);
       
   925 			FILE* f=fopen(x86impfile,"rb");
       
   926 			if (!f)
       
   927 				r=KErrArgument;
       
   928 			else
       
   929 				{
       
   930 				fseek(f,0,SEEK_END);
       
   931 				long size=ftell(f);
       
   932 				fseek(f,0,SEEK_SET);
       
   933 				if (size>4)
       
   934 					{
       
   935 					gX86imp=new char[size];
       
   936 					fread(gX86imp,1,size,f);
       
   937 					}
       
   938 				fclose(f);
       
   939 				r=KErrNone;
       
   940 				if (gX86imp)
       
   941 					{
       
   942 					gX86imp_size=ALIGN4(size);
       
   943 	//				printf("size %d\n",size);
       
   944 					int i;
       
   945 					int* p=(int*)gX86imp;
       
   946 					gX86num_imp_dlls=*p++;
       
   947 					for (i=0; i<gX86num_imp_dlls; ++i)
       
   948 						{
       
   949 						++p;
       
   950 						int n=*p++;
       
   951 						gX86num_imports+=n;
       
   952 						p+=n;
       
   953 						}
       
   954 	//				fprintf(stderr,"#imports=%d\n",gX86num_imports);
       
   955 					}
       
   956 				}
       
   957 			}
       
   958 #endif
       
   959 		else if (gFile1==NULL)
       
   960 			{
       
   961 			gFile1 = NormaliseFileName(argv[i]);
       
   962 			}
       
   963 		else if (gFile2==NULL)
       
   964 			{
       
   965 			gFile2 = NormaliseFileName(argv[i]);
       
   966 			}
       
   967 		else
       
   968 			r=KErrArgument;
       
   969 		if (r!=KErrNone)
       
   970 			return r;
       
   971 		i++;
       
   972 		}
       
   973 	return KErrNone;
       
   974 	}
       
   975 
       
   976 int main(int argc, char *argv[])
       
   977 	{
       
   978 #ifdef __SUPPORT_PE_FILES__
       
   979 	Print(EAlways,"\nPETRAN - PE file preprocessor");
       
   980 #endif
       
   981 #ifdef __SUPPORT_ELF_FILES__
       
   982 	Print(EAlways,"\nELFTRAN - ELF file preprocessor");
       
   983 #endif
       
   984   	Print(EAlways," V%02d.%02d (Build %03d)\n",MajorVersion,MinorVersion,Build);
       
   985 	int r=processCL(argc, argv);
       
   986 	if (r!=KErrNone)
       
   987 		return helpme(argv[0]);
       
   988 	if (gFile2)
       
   989 		return dotran(gFile1, gFile2);
       
   990 	if ((gSetStack || gSetUid1 || gSetUid2 || gSetUid3 || gSetCallEntryPoints || gSetPriority
       
   991 		|| gSetCapability || gSetCompress || gSetHeap || gSetVersion || gSetSecureId
       
   992 		|| gSetVendorId || gSetFixedAddress || gSetFPU || gSetCodePaged || gSetDataPaged || gSetDebuggable || gSetSmpSafe) && gFile1)
       
   993 		return doalter(gFile1);
       
   994 	if (gFile1)
       
   995 		return dodump(gFile1);
       
   996 	helpme(argv[0]);
       
   997 	return KErrArgument;
       
   998 	}