Loader ---- 1 places the memory allocation in the function, which greatly reduces the reusability of the function. If we want to use it at the driver layer, make the following changes, the memory layout function is only used for functional operations. Put the memory allocation related to the system out of the function.
[Cpp]
/*
* Map the file content to the address space based on the virtual address information
*/
BOOL LdrLayoutPe (char * pImage, DWORD FileSize, char * pRealImageBase, DWORD SizeOfImage)
{
PIMAGE_FILE_HEADER pFileHeader;
PIMAGE_SECTION_HEADER pseheader header;
Char * pSrc;
Char * pDst;
DWORD CopySize;
DWORD I;
If (! LdrValidateImage (pImage, FileSize ))
Return FALSE;
PFileHeader = _ GetFileHeaderFromPe (pImage );
Pseheader header = _ GetSectionHeaderFromPe (pFileHeader );
// Copy the file header information
PDst = pRealImageBase;
PSrc = pImage;
CopySize = (char *) pseheader header-pImage + pFileHeader-> NumberOfSections * sizeof (PIMAGE_SECTION_HEADER );
If (pDst + CopySize> = pRealImageBase + SizeOfImage)
Return FALSE;
RtlCopyMemory (pDst, pSrc, CopySize );
// Copy the node information to the memory
For (I = 0; I <pFileHeader-> NumberOfSections; I ++ ){
If (! Pseheader header [I]. PointerToRawData)
Continue;
// Determine the validity of the target pointer
PDst = pRealImageBase + pseheader header [I]. VirtualAddress;
PSrc = pImage + ctionheader header [I]. PointerToRawData;
CopySize = pseheader header [I]. SizeOfRawData;
If (pDst + CopySize> = pRealImageBase + SizeOfImage)
Return FALSE;
RtlCopyMemory (pDst, pSrc, CopySize );
}
Return TRUE;
}
From the light of the cloud