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