bintools/petools/pefile/pe_reloc.cpp
author jascui
Mon, 29 Nov 2010 15:37:06 +0800
changeset 708 0757c2976f96
parent 600 6d08f4a05d93
permissions -rw-r--r--
Updating
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
600
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     1
// Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     2
// All rights reserved.
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     3
// This component and the accompanying materials are made available
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     4
// under the terms of "Eclipse Public License v1.0"
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     5
// which accompanies this distribution, and is available
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     7
//
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     8
// Initial Contributors:
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     9
// Nokia Corporation - initial contribution.
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    10
//
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    11
// Contributors:
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    12
//
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    13
// Description:
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    14
//
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    15
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    16
#include <stdlib.h>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    17
#include <string.h>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    18
#include <e32std.h>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    19
#include <e32std_private.h>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    20
#include "pe_file.h"
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    21
#include "h_utl.h"
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    22
#include <stdio.h>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    23
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    24
extern char* gX86imp;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    25
extern int gX86num_imp_dlls;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    26
extern int gX86imp_size;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    27
extern int gX86num_imports;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    28
extern int* gX86imp_relocs;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    29
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    30
TInt sizeOfCodeRelocs(TUint *relocs, TUint *relocsection, TInt nrelocs)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    31
	{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    32
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    33
	TInt bytecount=0;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    34
	TInt page=-1;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    35
	TInt i;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    36
	for (i=0; i<nrelocs; i++)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    37
		{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    38
		if ((TInt)relocsection[i]==KTextSection || (TInt)relocsection[i]==KConstSection || (TInt)relocsection[i]==KCrtSection)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    39
			{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    40
			TInt p=relocs[i]&0xfffff000;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    41
			if (page!=p)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    42
				{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    43
				if (bytecount%4!=0)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    44
					bytecount+=2;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    45
				bytecount+=8; // page, block size
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    46
				page=p;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    47
				}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    48
			bytecount+=2;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    49
			}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    50
		}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    51
	if (bytecount%4!=0)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    52
		bytecount+=2;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    53
	return bytecount;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    54
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    55
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    56
TInt sizeOfDataRelocs(TUint *relocs, TUint *relocsection, TInt nrelocs)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    57
	{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    58
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    59
	TInt bytecount=0;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    60
	TInt page=-1;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    61
	TInt i;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    62
	for (i=0; i<nrelocs; i++)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    63
		{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    64
		if ((TInt)relocsection[i]==KDataSection)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    65
			{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    66
			TInt p=relocs[i]&0xfffff000;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    67
			if (page!=p)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    68
				{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    69
				if (bytecount%4!=0)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    70
					bytecount+=2;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    71
				bytecount+=8; // page, block size
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    72
				page=p;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    73
				}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    74
			bytecount+=2;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    75
			}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    76
		}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    77
	if (bytecount%4!=0)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    78
		bytecount+=2;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    79
	return bytecount;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    80
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    81
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    82
void reorderRelocs(TUint aReloc[], TUint aRelocSection[], TInt aNumberOfRelocs)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    83
//
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    84
// sort the relocations in section order
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    85
//
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    86
	{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    87
	TUint *temp=new TUint [aNumberOfRelocs];
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    88
	TUint *tempsection=new TUint [aNumberOfRelocs];
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    89
	TInt idx=0;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    90
	TUint section=0;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    91
	while (idx<aNumberOfRelocs)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    92
		{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    93
		for (TInt i=0; i<aNumberOfRelocs; i++)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    94
			{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    95
			if (aRelocSection[i]==section)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    96
				{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    97
				temp[idx]=aReloc[i];
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    98
				tempsection[idx]=aRelocSection[i];
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    99
				idx++;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   100
				}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   101
			}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   102
		section++;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   103
		}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   104
	memcpy((char *)aReloc, (char *)temp, aNumberOfRelocs*sizeof(TUint));
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   105
	memcpy((char *)aRelocSection, (char *)tempsection, aNumberOfRelocs*sizeof(TUint));
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   106
	delete [] temp;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   107
	delete [] tempsection;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   108
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   109
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   110
char *E32ImageFile_PE::CreateCodeRelocs(TUint *relocs, TUint *relocsection, TInt nrelocs, TInt &aSize)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   111
	{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   112
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   113
	TInt bytecount=sizeOfCodeRelocs(relocs, relocsection, nrelocs);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   114
	aSize=0;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   115
	if (bytecount==0)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   116
		return NULL;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   117
	aSize=bytecount+sizeof(E32RelocSection);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   118
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   119
	char *section=new char [bytecount+sizeof(E32RelocSection)];
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   120
	char *data=section+sizeof(E32RelocSection);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   121
	char *startofblock=data;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   122
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   123
	TInt ncoderelocs=0;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   124
	TInt page=-1;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   125
	TInt pagesize=8;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   126
	TInt i;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   127
	for (i=0; i<nrelocs; i++)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   128
		{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   129
		if ((TInt)relocsection[i]==KTextSection || (TInt)relocsection[i]==KConstSection || (TInt)relocsection[i]==KCrtSection)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   130
			{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   131
			TInt p=relocs[i]&0xfffff000;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   132
			if (page!=p)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   133
				{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   134
				if (pagesize%4!=0)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   135
					{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   136
					*(TUint16 *)data=0;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   137
					data+=2;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   138
					pagesize+=2;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   139
					}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   140
				*(TUint *)startofblock=page;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   141
				*(TUint *)(startofblock+4)=pagesize;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   142
				pagesize=8;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   143
				page=p;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   144
				startofblock=data;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   145
				data+=8;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   146
				}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   147
			*(TUint16 *)data=(TUint16)((relocs[i]&0xfff)|0x3000);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   148
			data+=2;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   149
			pagesize+=2;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   150
			ncoderelocs++;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   151
			}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   152
		}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   153
	if (pagesize%4!=0)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   154
		{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   155
		*(TUint16 *)data=0;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   156
		data+=2;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   157
		pagesize+=2;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   158
		}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   159
	*(TUint *)startofblock=page;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   160
	*(TUint *)(startofblock+4)=pagesize;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   161
	((E32RelocSection *)section)->iNumberOfRelocs=ncoderelocs;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   162
	((E32RelocSection *)section)->iSize=bytecount;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   163
	return section;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   164
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   165
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   166
char *E32ImageFile_PE::CreateDataRelocs(TUint *relocs, TUint *relocsection, TInt nrelocs, TInt &aSize)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   167
	{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   168
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   169
	TInt bytecount=sizeOfDataRelocs(relocs, relocsection, nrelocs);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   170
	aSize=0;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   171
	if (bytecount==0)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   172
		return NULL;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   173
	aSize=bytecount+sizeof(E32RelocSection);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   174
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   175
	char *section=new char [bytecount+sizeof(E32RelocSection)];
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   176
	char *data=section+sizeof(E32RelocSection);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   177
	char *startofblock=data;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   178
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   179
	TInt ndatarelocs=0;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   180
	TInt page=-1;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   181
	TInt pagesize=8;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   182
	TInt i;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   183
	for (i=0; i<nrelocs; i++)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   184
		{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   185
		if ((TInt)relocsection[i]==KDataSection)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   186
			{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   187
			TInt p=relocs[i]&0xfffff000;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   188
			if (page!=p)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   189
				{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   190
				if (pagesize%4!=0)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   191
					{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   192
					*(TUint16 *)data=0;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   193
					data+=2;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   194
					pagesize+=2;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   195
					}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   196
				*(TUint *)startofblock=page;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   197
				*(TUint *)(startofblock+4)=pagesize;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   198
				pagesize=8;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   199
				page=p;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   200
				startofblock=data;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   201
				data+=8;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   202
				}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   203
			*(TUint16 *)data=(TUint16)((relocs[i]&0xfff)|0x3000);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   204
			data+=2;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   205
			pagesize+=2;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   206
			ndatarelocs++;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   207
			}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   208
		}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   209
	if (pagesize%4!=0)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   210
		{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   211
		*(TUint16 *)data=0;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   212
		data+=2;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   213
		pagesize+=2;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   214
		}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   215
	*(TUint *)startofblock=page;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   216
	*(TUint *)(startofblock+4)=pagesize;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   217
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   218
	((E32RelocSection *)section)->iNumberOfRelocs=ndatarelocs;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   219
	((E32RelocSection *)section)->iSize=bytecount;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   220
	return section;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   221
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   222
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   223
void checkreloc(PEFile &aPeFile, TUint va, TUint reloc)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   224
	{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   225
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   226
	// Allow the section find routine to use heuristics to resolve addresses
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   227
	// which have been offset by the compiler
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   228
	TInt s = aPeFile.FindSectionByVa(va, 1);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   229
	switch(s)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   230
		{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   231
		case KTextSection:
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   232
		case KConstSection:
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   233
		case KDataSection:
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   234
		case KCrtSection:
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   235
		case KBssSection:
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   236
		case KImportSection:
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   237
			return;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   238
		default:
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   239
			break;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   240
		}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   241
	Print(EAlways, "bad relocation:  [%08x] = %08x\n", reloc, va);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   242
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   243
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   244
void E32ImageFile_PE::FixRelocs(PEFile &aPeFile, TUint *relocation, TUint *relocsection, TInt aNumberOfRelocs)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   245
	{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   246
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   247
	TUint *data;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   248
	TInt i;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   249
#if 0
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   250
	Print(EAlways, "Linked base %08x\n", aPeFile.iLinkedBase);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   251
	for (i=0; i<KNumberOfSections; i++)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   252
		{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   253
		if (!aPeFile.iSectionHeader[i])
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   254
			continue;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   255
		TUint start = aPeFile.iSectionHeader[i]->VirtualAddress;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   256
		TUint finish = start + aPeFile.iSectionHeader[i]->Misc.VirtualSize;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   257
		Print(EAlways, "Section %d %08x-%08x\n", i, start, finish);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   258
		}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   259
#endif
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   260
	for (i=0; i<aNumberOfRelocs; i++)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   261
		{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   262
		switch (relocsection[i])
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   263
			{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   264
		case KTextSection:
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   265
			relocation[i]-=aPeFile.iSectionHeader[KTextSection]->VirtualAddress;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   266
			data=(TUint *)(aPeFile.iSectionData[KTextSection]+relocation[i]);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   267
			if (!gLittleEndian) ByteSwap(*data);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   268
			checkreloc(aPeFile, *data, relocation[i]+aPeFile.iSectionHeader[KTextSection]->VirtualAddress);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   269
			*data=FixAddress(aPeFile, *data);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   270
			if (!gLittleEndian) ByteSwap(*data);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   271
			break;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   272
		case KConstSection:
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   273
			relocation[i]-=aPeFile.iSectionHeader[KConstSection]->VirtualAddress;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   274
			data=(TUint *)(aPeFile.iSectionData[KConstSection]+relocation[i]);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   275
			if (!gLittleEndian) ByteSwap(*data);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   276
			checkreloc(aPeFile, *data, relocation[i]+aPeFile.iSectionHeader[KConstSection]->VirtualAddress);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   277
			relocation[i]+=ConstOffset();
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   278
			*data=FixAddress(aPeFile, *data);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   279
			if (!gLittleEndian) ByteSwap(*data);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   280
			break;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   281
		case KCrtSection:
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   282
			relocation[i]-=aPeFile.iSectionHeader[KCrtSection]->VirtualAddress;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   283
			data=(TUint *)(aPeFile.iSectionData[KCrtSection]+relocation[i]);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   284
			if (!gLittleEndian) ByteSwap(*data);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   285
			checkreloc(aPeFile, *data, relocation[i]+aPeFile.iSectionHeader[KCrtSection]->VirtualAddress);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   286
			relocation[i]+=iCrtOffset;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   287
			*data=FixAddress(aPeFile, *data);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   288
			if (!gLittleEndian) ByteSwap(*data);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   289
			break;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   290
		case KDataSection:
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   291
			relocation[i]-=aPeFile.iSectionHeader[KDataSection]->VirtualAddress;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   292
			data=(TUint *)(aPeFile.iSectionData[KDataSection]+relocation[i]);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   293
			if (!gLittleEndian) ByteSwap(*data);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   294
			checkreloc(aPeFile, *data, relocation[i]+aPeFile.iSectionHeader[KDataSection]->VirtualAddress);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   295
			*data=FixAddress(aPeFile, *data);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   296
			if (!gLittleEndian) ByteSwap(*data);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   297
			break;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   298
		default:
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   299
			Print(EWarning, "Relocation in invalid section.\n");
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   300
			break;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   301
			}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   302
		}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   303
	reorderRelocs(relocation, relocsection, aNumberOfRelocs);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   304
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   305
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   306
TUint E32ImageFile_PE::FixAddress(PEFile &aPeFile, TUint va)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   307
//
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   308
// Fix the given virtual address for the new headers
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   309
//
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   310
	{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   311
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   312
	// Allow the section find routine to use heuristics to resolve addresses
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   313
	// which have been offset by the compiler
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   314
	TInt section=aPeFile.FindSectionByVa(va, 1);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   315
	switch(section)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   316
		{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   317
		case KTextSection:
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   318
			va-=aPeFile.iLinkedBase;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   319
			va-=aPeFile.iSectionHeader[KTextSection]->VirtualAddress;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   320
			va+=iHdr->iCodeBase;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   321
			break;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   322
		case KConstSection:
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   323
			va-=aPeFile.iLinkedBase;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   324
			va-=aPeFile.iSectionHeader[KConstSection]->VirtualAddress;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   325
			if (gX86imp)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   326
				{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   327
				TUint old_iat_size=(gX86num_imports+gX86num_imp_dlls)<<2;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   328
				if (va<old_iat_size)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   329
					{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   330
					TInt offset=iHdr->iTextSize;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   331
//					fprintf(stderr,"IAT OFF %x ",va);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   332
					va=gX86imp_relocs[va>>2]+iHdr->iCodeBase+offset;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   333
//					fprintf(stderr,"-> %x\n",va);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   334
					break;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   335
					}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   336
				}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   337
			va+=iHdr->iCodeBase+ConstOffset();
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   338
//			fprintf(stderr,"const reloc -> %x\n",va);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   339
			break;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   340
		case KDataSection:
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   341
			va-=aPeFile.iLinkedBase;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   342
			va-=aPeFile.iSectionHeader[KDataSection]->VirtualAddress;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   343
			va+=iHdr->iDataBase; //DataOffset();
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   344
			break;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   345
		case KCrtSection:
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   346
			va-=aPeFile.iLinkedBase;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   347
			va-=aPeFile.iSectionHeader[KCrtSection]->VirtualAddress;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   348
			va+=iHdr->iCodeBase+iCrtOffset;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   349
			break;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   350
		case KBssSection:
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   351
			va-=aPeFile.iLinkedBase;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   352
			va-=aPeFile.iSectionHeader[KBssSection]->VirtualAddress;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   353
			va+=iHdr->iDataBase+iHdr->iDataSize;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   354
			break;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   355
		case KImportSection:
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   356
			va-=aPeFile.iLinkedBase;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   357
			va=FixImportThunk(aPeFile, va-aPeFile.iSectionHeader[KImportSection]->VirtualAddress);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   358
			va+=iHdr->iCodeBase;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   359
			break;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   360
		default:
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   361
			if (va < 0x10000u)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   362
				{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   363
				// assume it's a relocation relative to an omitted section
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   364
				break;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   365
				}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   366
			Print(EWarning, "Address to relocate cannot be resolved to .text, .rdata, .idata or data sections\n");
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   367
			Print(EWarning, "Problem address = %08x (section %d)\n", va, section);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   368
			break;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   369
		}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   370
	// va is now an offset from the start of the text
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   371
	return va;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   372
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   373
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   374