gpt4 book ai didi

c++ - 我们如何将一个位置的 RVA(相对虚拟地址)映射到 PE 文件偏移量?

转载 作者:太空狗 更新时间:2023-10-29 20:07:08 26 4
gpt4 key购买 nike

从磁盘位置读取 PE 文件时,我需要将 RVA(从 pdb 文件获取的相对虚拟地址)映射到 PE 文件 (EXE) 偏移量。为此,我需要将 RVA 转换为文件偏移量,以便我可以从该位置读出 GUIDS(CLSID,IID's)。

问候乌斯曼

最佳答案

template <class T> LPVOID GetPtrFromRVA(
DWORD rva,
T* pNTHeader,
PBYTE imageBase ) // 'T' = PIMAGE_NT_HEADERS
{
PIMAGE_SECTION_HEADER pSectionHdr;
INT delta;

pSectionHdr = GetEnclosingSectionHeader( rva, pNTHeader);
if ( !pSectionHdr )
return 0;

delta = (INT)(pSectionHdr->VirtualAddress-pSectionHdr->PointerToRawData);
return (PVOID) ( imageBase + rva - delta );
}

template <class T> PIMAGE_SECTION_HEADER GetEnclosingSectionHeader(
DWORD rva,
T* pNTHeader) // 'T' == PIMAGE_NT_HEADERS
{
PIMAGE_SECTION_HEADER section = IMAGE_FIRST_SECTION(pNTHeader);
unsigned i;

for ( i=0; i < pNTHeader->FileHeader.NumberOfSections; i++, section++ )
{
// This 3 line idiocy is because Watcom's linker actually sets the
// Misc.VirtualSize field to 0. (!!! - Retards....!!!)
DWORD size = section->Misc.VirtualSize;
if ( 0 == size )
size = section->SizeOfRawData;

// Is the RVA within this section?
if ( (rva >= section->VirtualAddress) &&
(rva < (section->VirtualAddress + size)))
return section;
}

return 0;
}

应该做的伎俩...

关于c++ - 我们如何将一个位置的 RVA(相对虚拟地址)映射到 PE 文件偏移量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4524837/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com