2005-09-08 00:09:32 +00:00
|
|
|
/* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS system libraries
|
|
|
|
* FILE: lib/rtl/image.c
|
2004-06-20 23:27:21 +00:00
|
|
|
* PURPOSE: Image handling functions
|
2006-09-10 08:00:57 +00:00
|
|
|
* Relocate functions were previously located in
|
2006-09-14 16:37:05 +00:00
|
|
|
* ntoskrnl/ldr/loader.c and
|
|
|
|
* dll/ntdll/ldr/utils.c files
|
|
|
|
* PROGRAMMER: Eric Kohl + original authors from loader.c and utils.c file
|
|
|
|
* Aleksey Bragin
|
2004-06-20 23:27:21 +00:00
|
|
|
*/
|
|
|
|
|
2005-09-08 00:09:32 +00:00
|
|
|
/* INCLUDES *****************************************************************/
|
|
|
|
|
2005-07-26 08:39:07 +00:00
|
|
|
#include <rtl.h>
|
2004-06-20 23:27:21 +00:00
|
|
|
|
|
|
|
#define NDEBUG
|
2005-05-08 05:14:46 +00:00
|
|
|
#include <debug.h>
|
2004-06-20 23:27:21 +00:00
|
|
|
|
2009-01-25 16:13:16 +00:00
|
|
|
#define RVA(m, b) ((PVOID)((ULONG_PTR)(b) + (ULONG_PTR)(m)))
|
|
|
|
|
2005-09-08 00:09:32 +00:00
|
|
|
/* FUNCTIONS *****************************************************************/
|
2004-06-20 23:27:21 +00:00
|
|
|
|
2011-03-16 11:58:12 +00:00
|
|
|
FORCEINLINE
|
2014-04-27 12:45:51 +00:00
|
|
|
USHORT
|
2011-03-16 11:58:12 +00:00
|
|
|
ChkSum(ULONG Sum, PUSHORT Src, ULONG Len)
|
|
|
|
{
|
|
|
|
ULONG i;
|
|
|
|
|
|
|
|
for (i=0; i<Len; i++)
|
|
|
|
{
|
|
|
|
/* Sum up the current word */
|
|
|
|
Sum += Src[i];
|
|
|
|
|
|
|
|
/* Sum up everything above the low word as a carry */
|
|
|
|
Sum = (Sum & 0xFFFF) + (Sum >> 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Apply carry one more time and clamp to the USHORT */
|
|
|
|
return (Sum + (Sum >> 16)) & 0xFFFF;
|
|
|
|
}
|
|
|
|
|
2006-10-08 02:10:34 +00:00
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
2010-12-22 13:33:09 +00:00
|
|
|
LdrVerifyMappedImageMatchesChecksum(
|
|
|
|
IN PVOID BaseAddress,
|
2011-09-19 11:09:09 +00:00
|
|
|
IN SIZE_T ImageSize,
|
2010-12-22 13:33:09 +00:00
|
|
|
IN ULONG FileLength)
|
2006-10-08 02:10:34 +00:00
|
|
|
{
|
2011-03-29 11:00:34 +00:00
|
|
|
#if 0
|
2011-03-16 09:49:07 +00:00
|
|
|
PIMAGE_NT_HEADERS Header;
|
|
|
|
PUSHORT Ptr;
|
|
|
|
ULONG Sum;
|
|
|
|
ULONG CalcSum;
|
|
|
|
ULONG HeaderSum;
|
|
|
|
ULONG i;
|
|
|
|
|
2011-03-16 11:58:12 +00:00
|
|
|
// HACK: Ignore calls with ImageSize=0. Should be fixed by new MM.
|
|
|
|
if (ImageSize == 0) return TRUE;
|
|
|
|
|
2011-03-16 09:49:07 +00:00
|
|
|
/* Get NT header to check if it's an image at all */
|
2011-03-16 11:58:12 +00:00
|
|
|
Header = RtlImageNtHeader(BaseAddress);
|
2011-03-16 09:49:07 +00:00
|
|
|
if (!Header) return FALSE;
|
|
|
|
|
|
|
|
/* Get checksum to match */
|
|
|
|
HeaderSum = Header->OptionalHeader.CheckSum;
|
|
|
|
|
|
|
|
/* Zero checksum seems to be accepted */
|
|
|
|
if (HeaderSum == 0) return TRUE;
|
|
|
|
|
|
|
|
/* Calculate the checksum */
|
|
|
|
Sum = 0;
|
|
|
|
Ptr = (PUSHORT) BaseAddress;
|
|
|
|
for (i = 0; i < ImageSize / sizeof (USHORT); i++)
|
|
|
|
{
|
|
|
|
Sum += (ULONG)*Ptr;
|
|
|
|
if (HIWORD(Sum) != 0)
|
|
|
|
{
|
|
|
|
Sum = LOWORD(Sum) + HIWORD(Sum);
|
|
|
|
}
|
|
|
|
Ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImageSize & 1)
|
|
|
|
{
|
|
|
|
Sum += (ULONG)*((PUCHAR)Ptr);
|
|
|
|
if (HIWORD(Sum) != 0)
|
|
|
|
{
|
|
|
|
Sum = LOWORD(Sum) + HIWORD(Sum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CalcSum = (USHORT)(LOWORD(Sum) + HIWORD(Sum));
|
|
|
|
|
|
|
|
/* Subtract image checksum from calculated checksum. */
|
|
|
|
/* fix low word of checksum */
|
|
|
|
if (LOWORD(CalcSum) >= LOWORD(HeaderSum))
|
|
|
|
{
|
|
|
|
CalcSum -= LOWORD(HeaderSum);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CalcSum = ((LOWORD(CalcSum) - LOWORD(HeaderSum)) & 0xFFFF) - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fix high word of checksum */
|
|
|
|
if (LOWORD(CalcSum) >= HIWORD(HeaderSum))
|
|
|
|
{
|
|
|
|
CalcSum -= HIWORD(HeaderSum);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CalcSum = ((LOWORD(CalcSum) - HIWORD(HeaderSum)) & 0xFFFF) - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add file length */
|
|
|
|
CalcSum += ImageSize;
|
|
|
|
|
|
|
|
if (CalcSum != HeaderSum)
|
2011-03-16 11:58:12 +00:00
|
|
|
DPRINT1("Image %p checksum mismatches! 0x%x != 0x%x, ImageSize %x, FileLen %x\n", BaseAddress, CalcSum, HeaderSum, ImageSize, FileLength);
|
2011-03-16 09:49:07 +00:00
|
|
|
|
|
|
|
return (BOOLEAN)(CalcSum == HeaderSum);
|
2011-03-29 11:00:34 +00:00
|
|
|
#else
|
2011-07-10 21:55:55 +00:00
|
|
|
/*
|
|
|
|
* FIXME: Warning, this violates the PE standard and makes ReactOS drivers
|
|
|
|
* and other system code when normally on Windows they would not, since
|
|
|
|
* we do not write the checksum in them.
|
|
|
|
* Our compilers should be made to write out the checksum and this function
|
|
|
|
* should be enabled as to reject badly checksummed code.
|
|
|
|
*/
|
2011-03-29 11:00:34 +00:00
|
|
|
return TRUE;
|
|
|
|
#endif
|
2006-10-08 02:10:34 +00:00
|
|
|
}
|
|
|
|
|
2004-06-20 23:27:21 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2011-07-10 21:55:55 +00:00
|
|
|
NTSTATUS
|
2010-12-22 13:33:09 +00:00
|
|
|
NTAPI
|
2011-07-10 21:55:55 +00:00
|
|
|
RtlImageNtHeaderEx(IN ULONG Flags,
|
|
|
|
IN PVOID Base,
|
|
|
|
IN ULONG64 Size,
|
|
|
|
OUT PIMAGE_NT_HEADERS *OutHeaders)
|
2004-06-20 23:27:21 +00:00
|
|
|
{
|
2011-07-10 21:55:55 +00:00
|
|
|
PIMAGE_NT_HEADERS NtHeaders;
|
|
|
|
PIMAGE_DOS_HEADER DosHeader;
|
2011-07-11 00:10:27 +00:00
|
|
|
BOOLEAN WantsRangeCheck;
|
2011-07-10 21:55:55 +00:00
|
|
|
|
|
|
|
/* You must want NT Headers, no? */
|
|
|
|
if (!OutHeaders) return STATUS_INVALID_PARAMETER;
|
2004-06-20 23:27:21 +00:00
|
|
|
|
2011-07-10 21:55:55 +00:00
|
|
|
/* Assume failure */
|
|
|
|
*OutHeaders = NULL;
|
|
|
|
|
|
|
|
/* Validate Flags */
|
|
|
|
if (Flags &~ RTL_IMAGE_NT_HEADER_EX_FLAG_NO_RANGE_CHECK)
|
2004-06-20 23:27:21 +00:00
|
|
|
{
|
2011-07-10 21:55:55 +00:00
|
|
|
DPRINT1("Invalid flag combination... check for new API flags?\n");
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
2004-06-20 23:27:21 +00:00
|
|
|
}
|
2011-07-10 21:55:55 +00:00
|
|
|
|
|
|
|
/* Validate base */
|
|
|
|
if (!(Base) || (Base == (PVOID)-1)) return STATUS_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
/* Check if the caller wants validation */
|
2011-07-11 00:10:27 +00:00
|
|
|
WantsRangeCheck = !(Flags & RTL_IMAGE_NT_HEADER_EX_FLAG_NO_RANGE_CHECK);
|
|
|
|
if (WantsRangeCheck)
|
2011-07-10 21:55:55 +00:00
|
|
|
{
|
|
|
|
/* Make sure the image size is at least big enough for the DOS header */
|
|
|
|
if (Size < sizeof(IMAGE_DOS_HEADER))
|
|
|
|
{
|
|
|
|
DPRINT1("Size too small\n");
|
|
|
|
return STATUS_INVALID_IMAGE_FORMAT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the DOS Signature matches */
|
|
|
|
DosHeader = Base;
|
|
|
|
if (DosHeader->e_magic != IMAGE_DOS_SIGNATURE)
|
2004-06-20 23:27:21 +00:00
|
|
|
{
|
2011-07-10 21:55:55 +00:00
|
|
|
/* Not a valid COFF */
|
|
|
|
DPRINT1("Not an MZ file\n");
|
|
|
|
return STATUS_INVALID_IMAGE_FORMAT;
|
2004-06-20 23:27:21 +00:00
|
|
|
}
|
|
|
|
|
2011-07-10 21:55:55 +00:00
|
|
|
/* Check if the caller wants validation */
|
2011-07-11 00:10:27 +00:00
|
|
|
if (WantsRangeCheck)
|
2011-07-10 21:55:55 +00:00
|
|
|
{
|
|
|
|
/* The offset should fit in the passsed-in size */
|
|
|
|
if (DosHeader->e_lfanew >= Size)
|
|
|
|
{
|
|
|
|
/* Fail */
|
|
|
|
DPRINT1("e_lfanew is larger than PE file\n");
|
|
|
|
return STATUS_INVALID_IMAGE_FORMAT;
|
|
|
|
}
|
2011-09-19 11:09:09 +00:00
|
|
|
|
2011-07-10 21:55:55 +00:00
|
|
|
/* It shouldn't be past 4GB either */
|
|
|
|
if (DosHeader->e_lfanew >=
|
|
|
|
(MAXULONG - sizeof(IMAGE_DOS_SIGNATURE) - sizeof(IMAGE_FILE_HEADER)))
|
|
|
|
{
|
|
|
|
/* Fail */
|
|
|
|
DPRINT1("e_lfanew is larger than 4GB\n");
|
|
|
|
return STATUS_INVALID_IMAGE_FORMAT;
|
|
|
|
}
|
2011-09-19 11:09:09 +00:00
|
|
|
|
2011-07-10 21:55:55 +00:00
|
|
|
/* And the whole file shouldn't overflow past 4GB */
|
|
|
|
if ((DosHeader->e_lfanew +
|
|
|
|
sizeof(IMAGE_DOS_SIGNATURE) - sizeof(IMAGE_FILE_HEADER)) >= Size)
|
|
|
|
{
|
|
|
|
/* Fail */
|
|
|
|
DPRINT1("PE is larger than 4GB\n");
|
|
|
|
return STATUS_INVALID_IMAGE_FORMAT;
|
|
|
|
}
|
|
|
|
}
|
2011-09-19 11:09:09 +00:00
|
|
|
|
2011-07-10 21:55:55 +00:00
|
|
|
/* The offset also can't be larger than 256MB, as a hard-coded check */
|
|
|
|
if (DosHeader->e_lfanew >= (256 * 1024 * 1024))
|
|
|
|
{
|
|
|
|
/* Fail */
|
|
|
|
DPRINT1("PE offset is larger than 256MB\n");
|
|
|
|
return STATUS_INVALID_IMAGE_FORMAT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now it's safe to get the NT Headers */
|
|
|
|
NtHeaders = (PIMAGE_NT_HEADERS)((ULONG_PTR)Base + DosHeader->e_lfanew);
|
|
|
|
|
|
|
|
/* Verify the PE Signature */
|
|
|
|
if (NtHeaders->Signature != IMAGE_NT_SIGNATURE)
|
|
|
|
{
|
|
|
|
/* Fail */
|
|
|
|
DPRINT1("PE signature missing\n");
|
|
|
|
return STATUS_INVALID_IMAGE_FORMAT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now return success and the NT header */
|
|
|
|
*OutHeaders = NtHeaders;
|
|
|
|
return STATUS_SUCCESS;
|
2004-06-20 23:27:21 +00:00
|
|
|
}
|
2011-09-19 11:09:09 +00:00
|
|
|
|
2011-07-10 21:55:55 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
PIMAGE_NT_HEADERS
|
|
|
|
NTAPI
|
|
|
|
RtlImageNtHeader(IN PVOID Base)
|
|
|
|
{
|
|
|
|
PIMAGE_NT_HEADERS NtHeader;
|
2004-06-20 23:27:21 +00:00
|
|
|
|
2011-07-10 21:55:55 +00:00
|
|
|
/* Call the new API */
|
|
|
|
RtlImageNtHeaderEx(RTL_IMAGE_NT_HEADER_EX_FLAG_NO_RANGE_CHECK,
|
|
|
|
Base,
|
|
|
|
0,
|
|
|
|
&NtHeader);
|
|
|
|
return NtHeader;
|
|
|
|
}
|
2004-06-20 23:27:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
PVOID
|
2005-10-19 17:03:38 +00:00
|
|
|
NTAPI
|
2010-12-22 13:33:09 +00:00
|
|
|
RtlImageDirectoryEntryToData(
|
|
|
|
PVOID BaseAddress,
|
|
|
|
BOOLEAN MappedAsImage,
|
|
|
|
USHORT Directory,
|
|
|
|
PULONG Size)
|
2004-06-20 23:27:21 +00:00
|
|
|
{
|
2010-12-22 13:33:09 +00:00
|
|
|
PIMAGE_NT_HEADERS NtHeader;
|
|
|
|
ULONG Va;
|
|
|
|
|
|
|
|
/* Magic flag for non-mapped images. */
|
|
|
|
if ((ULONG_PTR)BaseAddress & 1)
|
|
|
|
{
|
|
|
|
BaseAddress = (PVOID)((ULONG_PTR)BaseAddress & ~1);
|
|
|
|
MappedAsImage = FALSE;
|
|
|
|
}
|
2005-07-31 10:22:29 +00:00
|
|
|
|
2013-02-16 17:21:34 +00:00
|
|
|
NtHeader = RtlImageNtHeader(BaseAddress);
|
2010-12-22 13:33:09 +00:00
|
|
|
if (NtHeader == NULL)
|
|
|
|
return NULL;
|
2004-06-20 23:27:21 +00:00
|
|
|
|
2010-12-22 13:33:09 +00:00
|
|
|
if (Directory >= SWAPD(NtHeader->OptionalHeader.NumberOfRvaAndSizes))
|
|
|
|
return NULL;
|
2004-06-20 23:27:21 +00:00
|
|
|
|
2010-12-22 13:33:09 +00:00
|
|
|
Va = SWAPD(NtHeader->OptionalHeader.DataDirectory[Directory].VirtualAddress);
|
|
|
|
if (Va == 0)
|
|
|
|
return NULL;
|
2004-06-20 23:27:21 +00:00
|
|
|
|
2010-12-22 13:33:09 +00:00
|
|
|
*Size = SWAPD(NtHeader->OptionalHeader.DataDirectory[Directory].Size);
|
2004-06-20 23:27:21 +00:00
|
|
|
|
2010-12-22 13:33:09 +00:00
|
|
|
if (MappedAsImage || Va < SWAPD(NtHeader->OptionalHeader.SizeOfHeaders))
|
|
|
|
return (PVOID)((ULONG_PTR)BaseAddress + Va);
|
2004-06-20 23:27:21 +00:00
|
|
|
|
2010-12-22 13:33:09 +00:00
|
|
|
/* image mapped as ordinary file, we must find raw pointer */
|
2013-02-16 17:21:34 +00:00
|
|
|
return RtlImageRvaToVa(NtHeader, BaseAddress, Va, NULL);
|
2004-06-20 23:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
PIMAGE_SECTION_HEADER
|
2005-10-19 17:03:38 +00:00
|
|
|
NTAPI
|
2010-12-22 13:33:09 +00:00
|
|
|
RtlImageRvaToSection(
|
|
|
|
PIMAGE_NT_HEADERS NtHeader,
|
|
|
|
PVOID BaseAddress,
|
|
|
|
ULONG Rva)
|
2004-06-20 23:27:21 +00:00
|
|
|
{
|
2010-12-22 13:33:09 +00:00
|
|
|
PIMAGE_SECTION_HEADER Section;
|
|
|
|
ULONG Va;
|
|
|
|
ULONG Count;
|
|
|
|
|
|
|
|
Count = SWAPW(NtHeader->FileHeader.NumberOfSections);
|
|
|
|
Section = IMAGE_FIRST_SECTION(NtHeader);
|
|
|
|
|
|
|
|
while (Count--)
|
|
|
|
{
|
|
|
|
Va = SWAPD(Section->VirtualAddress);
|
|
|
|
if ((Va <= Rva) &&
|
|
|
|
(Rva < Va + SWAPD(Section->Misc.VirtualSize)))
|
|
|
|
return Section;
|
|
|
|
Section++;
|
|
|
|
}
|
|
|
|
return NULL;
|
2004-06-20 23:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2005-12-08 23:55:34 +00:00
|
|
|
PVOID
|
2005-10-19 17:03:38 +00:00
|
|
|
NTAPI
|
2010-12-22 13:33:09 +00:00
|
|
|
RtlImageRvaToVa(
|
|
|
|
PIMAGE_NT_HEADERS NtHeader,
|
|
|
|
PVOID BaseAddress,
|
|
|
|
ULONG Rva,
|
|
|
|
PIMAGE_SECTION_HEADER *SectionHeader)
|
2004-06-20 23:27:21 +00:00
|
|
|
{
|
2010-12-22 13:33:09 +00:00
|
|
|
PIMAGE_SECTION_HEADER Section = NULL;
|
|
|
|
|
|
|
|
if (SectionHeader)
|
|
|
|
Section = *SectionHeader;
|
|
|
|
|
2013-02-16 17:21:34 +00:00
|
|
|
if ((Section == NULL) ||
|
|
|
|
(Rva < SWAPD(Section->VirtualAddress)) ||
|
|
|
|
(Rva >= SWAPD(Section->VirtualAddress) + SWAPD(Section->Misc.VirtualSize)))
|
2010-12-22 13:33:09 +00:00
|
|
|
{
|
|
|
|
Section = RtlImageRvaToSection (NtHeader, BaseAddress, Rva);
|
|
|
|
if (Section == NULL)
|
2013-02-16 17:21:34 +00:00
|
|
|
return NULL;
|
2010-12-22 13:33:09 +00:00
|
|
|
|
|
|
|
if (SectionHeader)
|
|
|
|
*SectionHeader = Section;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (PVOID)((ULONG_PTR)BaseAddress +
|
|
|
|
Rva +
|
|
|
|
SWAPD(Section->PointerToRawData) -
|
|
|
|
(ULONG_PTR)SWAPD(Section->VirtualAddress));
|
2004-06-20 23:27:21 +00:00
|
|
|
}
|
|
|
|
|
2006-09-14 16:37:05 +00:00
|
|
|
PIMAGE_BASE_RELOCATION
|
2006-09-15 16:01:50 +00:00
|
|
|
NTAPI
|
2006-09-14 16:37:05 +00:00
|
|
|
LdrProcessRelocationBlockLongLong(
|
|
|
|
IN ULONG_PTR Address,
|
|
|
|
IN ULONG Count,
|
|
|
|
IN PUSHORT TypeOffset,
|
2010-12-22 13:33:09 +00:00
|
|
|
IN LONGLONG Delta)
|
2006-09-14 16:37:05 +00:00
|
|
|
{
|
|
|
|
SHORT Offset;
|
|
|
|
USHORT Type;
|
2011-11-20 19:45:06 +00:00
|
|
|
ULONG i;
|
2006-09-14 16:37:05 +00:00
|
|
|
PUSHORT ShortPtr;
|
|
|
|
PULONG LongPtr;
|
Merge freeldr from amd64 branch:
34725, 34726, 34727, 34728, 34733, 34735, 34747, 34748, 34888, 34937, 34968, 35021, 35351, 35360, 35445, 35493, 35495, 35505, 35547, 35696, 35704, 36343, 36355, 36865, 37306, 37896, 38332, 38942, 38950, 38951, 38959, 39073, 39278
svn path=/trunk/; revision=39638
2009-02-17 04:05:26 +00:00
|
|
|
PULONGLONG LongLongPtr;
|
2006-09-14 16:37:05 +00:00
|
|
|
|
|
|
|
for (i = 0; i < Count; i++)
|
|
|
|
{
|
2007-10-14 23:09:12 +00:00
|
|
|
Offset = SWAPW(*TypeOffset) & 0xFFF;
|
|
|
|
Type = SWAPW(*TypeOffset) >> 12;
|
2006-09-14 16:37:05 +00:00
|
|
|
ShortPtr = (PUSHORT)(RVA(Address, Offset));
|
|
|
|
/*
|
|
|
|
* Don't relocate within the relocation section itself.
|
|
|
|
* GCC/LD generates sometimes relocation records for the relocation section.
|
|
|
|
* This is a bug in GCC/LD.
|
|
|
|
* Fix for it disabled, since it was only in ntoskrnl and not in ntdll
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
if ((ULONG_PTR)ShortPtr < (ULONG_PTR)RelocationDir ||
|
|
|
|
(ULONG_PTR)ShortPtr >= (ULONG_PTR)RelocationEnd)
|
|
|
|
{*/
|
|
|
|
switch (Type)
|
|
|
|
{
|
2010-12-22 13:33:09 +00:00
|
|
|
/* case IMAGE_REL_BASED_SECTION : */
|
|
|
|
/* case IMAGE_REL_BASED_REL32 : */
|
2006-09-14 16:37:05 +00:00
|
|
|
case IMAGE_REL_BASED_ABSOLUTE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IMAGE_REL_BASED_HIGH:
|
2011-11-20 19:45:06 +00:00
|
|
|
*ShortPtr = HIWORD(MAKELONG(0, *ShortPtr) + (Delta & 0xFFFFFFFF));
|
2006-09-14 16:37:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IMAGE_REL_BASED_LOW:
|
2011-11-20 19:45:06 +00:00
|
|
|
*ShortPtr = SWAPW(*ShortPtr) + LOWORD(Delta & 0xFFFF);
|
2006-09-14 16:37:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IMAGE_REL_BASED_HIGHLOW:
|
|
|
|
LongPtr = (PULONG)RVA(Address, Offset);
|
2011-11-20 19:45:06 +00:00
|
|
|
*LongPtr = SWAPD(*LongPtr) + (Delta & 0xFFFFFFFF);
|
2006-09-14 16:37:05 +00:00
|
|
|
break;
|
|
|
|
|
Merge freeldr from amd64 branch:
34725, 34726, 34727, 34728, 34733, 34735, 34747, 34748, 34888, 34937, 34968, 35021, 35351, 35360, 35445, 35493, 35495, 35505, 35547, 35696, 35704, 36343, 36355, 36865, 37306, 37896, 38332, 38942, 38950, 38951, 38959, 39073, 39278
svn path=/trunk/; revision=39638
2009-02-17 04:05:26 +00:00
|
|
|
case IMAGE_REL_BASED_DIR64:
|
|
|
|
LongLongPtr = (PUINT64)RVA(Address, Offset);
|
|
|
|
*LongLongPtr = SWAPQ(*LongLongPtr) + Delta;
|
|
|
|
break;
|
|
|
|
|
2006-09-14 16:37:05 +00:00
|
|
|
case IMAGE_REL_BASED_HIGHADJ:
|
|
|
|
case IMAGE_REL_BASED_MIPS_JMPADDR:
|
|
|
|
default:
|
|
|
|
DPRINT1("Unknown/unsupported fixup type %hu.\n", Type);
|
2013-06-02 18:01:00 +00:00
|
|
|
DPRINT1("Address %p, Current %u, Count %u, *TypeOffset %x\n",
|
|
|
|
(PVOID)Address, i, Count, SWAPW(*TypeOffset));
|
2006-09-14 16:37:05 +00:00
|
|
|
return (PIMAGE_BASE_RELOCATION)NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeOffset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (PIMAGE_BASE_RELOCATION)TypeOffset;
|
|
|
|
}
|
|
|
|
|
2006-09-10 08:00:57 +00:00
|
|
|
ULONG
|
|
|
|
NTAPI
|
|
|
|
LdrRelocateImageWithBias(
|
|
|
|
IN PVOID BaseAddress,
|
|
|
|
IN LONGLONG AdditionalBias,
|
2006-09-12 05:21:27 +00:00
|
|
|
IN PCCH LoaderName,
|
2006-09-10 08:00:57 +00:00
|
|
|
IN ULONG Success,
|
|
|
|
IN ULONG Conflict,
|
2010-12-22 13:33:09 +00:00
|
|
|
IN ULONG Invalid)
|
2006-09-10 08:00:57 +00:00
|
|
|
{
|
|
|
|
PIMAGE_NT_HEADERS NtHeaders;
|
|
|
|
PIMAGE_DATA_DIRECTORY RelocationDDir;
|
|
|
|
PIMAGE_BASE_RELOCATION RelocationDir, RelocationEnd;
|
2006-09-14 16:37:05 +00:00
|
|
|
ULONG Count;
|
|
|
|
ULONG_PTR Address;
|
2006-09-10 08:00:57 +00:00
|
|
|
PUSHORT TypeOffset;
|
2006-09-14 16:37:05 +00:00
|
|
|
LONGLONG Delta;
|
2006-09-10 08:00:57 +00:00
|
|
|
|
|
|
|
NtHeaders = RtlImageNtHeader(BaseAddress);
|
|
|
|
|
|
|
|
if (NtHeaders == NULL)
|
|
|
|
return Invalid;
|
|
|
|
|
2007-10-14 23:09:12 +00:00
|
|
|
if (SWAPW(NtHeaders->FileHeader.Characteristics) & IMAGE_FILE_RELOCS_STRIPPED)
|
2006-09-10 08:00:57 +00:00
|
|
|
{
|
|
|
|
return Conflict;
|
2006-12-05 18:33:03 +00:00
|
|
|
}
|
2006-09-10 08:00:57 +00:00
|
|
|
|
|
|
|
RelocationDDir = &NtHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];
|
|
|
|
|
2007-10-14 23:09:12 +00:00
|
|
|
if (SWAPD(RelocationDDir->VirtualAddress) == 0 || SWAPD(RelocationDDir->Size) == 0)
|
2006-09-10 08:00:57 +00:00
|
|
|
{
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
2007-10-14 23:09:12 +00:00
|
|
|
Delta = (ULONG_PTR)BaseAddress - SWAPD(NtHeaders->OptionalHeader.ImageBase) + AdditionalBias;
|
|
|
|
RelocationDir = (PIMAGE_BASE_RELOCATION)((ULONG_PTR)BaseAddress + SWAPD(RelocationDDir->VirtualAddress));
|
|
|
|
RelocationEnd = (PIMAGE_BASE_RELOCATION)((ULONG_PTR)RelocationDir + SWAPD(RelocationDDir->Size));
|
2006-09-10 08:00:57 +00:00
|
|
|
|
|
|
|
while (RelocationDir < RelocationEnd &&
|
2010-12-22 13:33:09 +00:00
|
|
|
SWAPW(RelocationDir->SizeOfBlock) > 0)
|
2006-09-10 08:00:57 +00:00
|
|
|
{
|
2007-10-14 23:09:12 +00:00
|
|
|
Count = (SWAPW(RelocationDir->SizeOfBlock) - sizeof(IMAGE_BASE_RELOCATION)) / sizeof(USHORT);
|
|
|
|
Address = (ULONG_PTR)RVA(BaseAddress, SWAPD(RelocationDir->VirtualAddress));
|
2006-09-10 08:00:57 +00:00
|
|
|
TypeOffset = (PUSHORT)(RelocationDir + 1);
|
|
|
|
|
2006-09-14 16:37:05 +00:00
|
|
|
RelocationDir = LdrProcessRelocationBlockLongLong(Address,
|
2010-12-22 13:33:09 +00:00
|
|
|
Count,
|
|
|
|
TypeOffset,
|
|
|
|
Delta);
|
2006-09-14 16:37:05 +00:00
|
|
|
|
|
|
|
if (RelocationDir == NULL)
|
2006-09-10 08:00:57 +00:00
|
|
|
{
|
2006-09-14 16:37:05 +00:00
|
|
|
DPRINT1("Error during call to LdrProcessRelocationBlockLongLong()!\n");
|
|
|
|
return Invalid;
|
2006-09-10 08:00:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Success;
|
|
|
|
}
|
2010-12-22 13:33:09 +00:00
|
|
|
|
2004-06-20 23:27:21 +00:00
|
|
|
/* EOF */
|