Loader introduction ---- 2

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.