diff -r fa7a3cc6effd -r 6d08f4a05d93 bintools/petools/pefile/pe_imp.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bintools/petools/pefile/pe_imp.cpp Fri Jun 25 18:11:34 2010 +0800 @@ -0,0 +1,235 @@ +// Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies). +// All rights reserved. +// This component and the accompanying materials are made available +// under the terms of "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: +// + +#include +#include +#include +#include +#include "h_utl.h" +#include "pe_file.h" + +TInt E32ImageFile_PE::CopyImportAddrTable(char *aPtr, PEFile &aPeFile) +// +// Copy the import address table entries +// + { + + TUint *ptr=(TUint *)aPtr; + char *importsection=aPeFile.iSectionData[KImportSection]; + TUint *src=(TUint *)importsection; + while (*src) + { + TUint vaoffset=src[4]; + if (!gLittleEndian) ByteSwap(vaoffset); + TUint offset=vaoffset-aPeFile.iSectionHeader[KImportSection]->VirtualAddress; // find the offset into the section of import addr table + vaoffset=src[3]; + if (!gLittleEndian) ByteSwap(vaoffset); + TUint exportername=vaoffset-aPeFile.iSectionHeader[KImportSection]->VirtualAddress; + TUint *p=(TUint *)(importsection+offset); + while (*p) + { + if ((*p&0x80000000)==0) + { + Print(EError, "%s exporting symbol by name\n", importsection+exportername); + return KErrGeneral; + } + *ptr++=(*p++)&0x7fffffff; // mask out the high bit (- indicates export by ordinal) + } + src+=5; + } + *ptr++=0; + return KErrNone; + } + +extern char* gX86imp; +extern int gX86num_imports; +extern int gX86num_imp_dlls; +extern int gX86imp_size; +int* gX86imp_relocs=NULL; + +TInt CrunchImportSection(TAny* aSection, TInt aNumImpDlls, TInt aNumImports) + { + // Remove lists of ordinals from import section + TInt* d = (TInt*)aSection; + TInt orig_size = *d; + TInt offset_correction = aNumImports*sizeof(TUint); + *d -= offset_correction; // reduce total section size + TInt *dd = d+1; + TInt *ss = d+1; + + TInt i; + for (i=0; i>2; + *s=rdata[rdata_int_offset]&0x7fffffffu; // rdata offset to ordinal + gX86imp_relocs[rdata_int_offset]=j<<2; + ++j; + ++s; + } + } + *(TInt*)section = bytecount; + aSize = CrunchImportSection(section, gX86num_imp_dlls, total_imports); + return section; + } + PIMAGE_SECTION_HEADER aHeader=aPeFile.iSectionHeader[KImportSection]; + TUint *aSrc=(TUint *)aPeFile.iSectionData[KImportSection]; + + TInt nimportdlls=aPeFile.NumberOfImportDlls(); + if (nimportdlls==0) + { + aSize=0; + return NULL; + } + E32ImportBlock *block=new E32ImportBlock [nimportdlls]; + char **name=new char* [nimportdlls]; + TUint **import=new TUint* [nimportdlls]; + + TInt bytecount=sizeof(E32ImportSection)+sizeof(E32ImportBlock)*nimportdlls; + TUint *src=aSrc; + TInt i; + for (i=0; iVirtualAddress; // find the offset into the section of import addr table + TUint *p=aSrc+offset/4; + block[i].iNumberOfImports=0; + while (*p++) + block[i].iNumberOfImports++; + total_imports += block[i].iNumberOfImports; + import[i]=new TUint [block[i].iNumberOfImports]; + TInt j; + p=aSrc+offset/4; + for (j=0; jVirtualAddress; + name[i]=((char *)aSrc)+offset; + bytecount+=strlen(name[i])+1; + src+=5; + } + + bytecount=ALIGN4(bytecount); + char *section=new char [bytecount]; + char *s=section+sizeof(E32ImportSection); + for (i=0; iiOffsetOfDllName=s-section; + strcpy(s, name[i]); + s+=strlen(name[i])+1; + t += ((E32ImportBlock *)t)->iNumberOfImports * sizeof(TUint) + sizeof(E32ImportBlock); + } + while ((s-section)VirtualAddress; + + while (iatVirtualAddress; + } + else + { + n++; + iat+=4; + } + } + + // Flag errors brought about by a corrupt input binary + if (iat>va) + { + Print(EError, "%s is corrupt - problem processing import address table.\n", this->iFileName); + return (TUint)KErrGeneral; + } + + return iHdr->iTextSize+n*sizeof(TUint); + } +