bintools/petools/pefile/pe_utl.cpp
author jjkang
Wed, 30 Jun 2010 11:35:58 +0800
changeset 607 378360dbbdba
parent 600 6d08f4a05d93
permissions -rw-r--r--
Merge missed changeset 11 (raptor v2.14 and helium 10.0)
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 <e32std.h>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    17
#include <e32std_private.h>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    18
#include "pe_file.h"
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    19
#include <string.h>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    20
#include "e32image.h"
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    21
#include "h_utl.h"
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    22
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    23
TInt PEFile::CmpSectionName(PIMAGE_SECTION_HEADER apSectionHeader, char *aName)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    24
//
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    25
// Returns true if the name of the pe section is the same as aName
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    26
//
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    27
	{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    28
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    29
	return (strnicmp((const char *)apSectionHeader->Name, aName, IMAGE_SIZEOF_SHORT_NAME)==0);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    30
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    31
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    32
TInt PEFile::VirtualAddressInSection(TUint aVA, PIMAGE_SECTION_HEADER aHeader)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    33
//
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    34
// Returns true if the virtual address is in the section
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    35
//
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    36
	{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    37
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    38
	TUint start = iLinkedBase + aHeader->VirtualAddress;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    39
	TUint finish = start + aHeader->Misc.VirtualSize;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    40
	return (aVA>=start) && (aVA<finish);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    41
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    42
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    43
TUint PEFile::DistanceFromSection(TUint aVA, PIMAGE_SECTION_HEADER aHeader)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    44
//
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    45
// Returns the minimum distance from aVA to any address in the specified section
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    46
//
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    47
	{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    48
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    49
	TUint start = iLinkedBase + aHeader->VirtualAddress;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    50
	TUint finish = start + aHeader->Misc.VirtualSize;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    51
	if (aVA>=start)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    52
		{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    53
		if (aVA<finish)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    54
			return 0;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    55
		return aVA - finish + 1;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    56
		}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    57
	return start - aVA;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    58
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    59
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    60
TInt PEFile::FindSectionByVa(TUint aVA, TUint aTryToBeClever)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    61
	{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    62
	TInt i;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    63
	TInt s = -1;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    64
	if (aTryToBeClever == 0)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    65
		{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    66
		for (i=0; i<KNumberOfSections; i++)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    67
			if (iSectionHeader[i])
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    68
				if (VirtualAddressInSection(aVA, iSectionHeader[i]))
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    69
					s=i;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    70
		}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    71
	if (aTryToBeClever == 1)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    72
		{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    73
		// Find the minimum distance from the specified address to any section
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    74
		TUint dist = KMaxTUint;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    75
		for (i=0; i<KNumberOfSections; i++)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    76
			{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    77
			if (!iSectionHeader[i])
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    78
				continue;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    79
			TUint d = DistanceFromSection(aVA, iSectionHeader[i]);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    80
			if (d < dist)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    81
				{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    82
				dist = d;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    83
				s = i;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    84
				}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    85
			else if (d == dist)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    86
				{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    87
				s = -1;	// Ambiguous :-(
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    88
				}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    89
			}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    90
		if (dist >= 0x1000u)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    91
			s = -1;		// too far for comfort
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    92
		}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    93
	return s;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    94
	}