【PE学习----VA、RVA、File Offset】

来源:互联网 发布:js prop可用 编辑:程序博客网 时间:2024/05/22 12:18

参考了《Windows_PE权威指南》这本书

VA   (Virual Address)  虚拟地址   程序被载入od中 如该图显示的就是虚拟地址VA;

RVA(Relative Virual Address)相对虚拟地址,表示此段代码在内存中相对于基地址的偏移。
File Offset 文件偏移地址 :当PE文件存储在磁盘上时,某个数据的位置相对于文件头的偏移量,称为文件地址。即C32里面能看到的地址
imagebase 基址  :文件执行时将被映像到指定内存地址中,这个初始内存地址称为基址;指的是IMAGE_OPTIONAL_HEADER中ImageBase这个值;
他们之间的计算公式 
                VA = imagebase + RVA;
               File Offset = VA - ImageBase – VRk 或者 File Offset= RVA – VRk
              VRk为RVA同File Offset的一个差值
             VRk=RVA-File Offset    (IMAGE_SECTION_HEADER .virtualAddress  -  IMAGE_SECTION_HEADER .PointerToRawData)



附上 转换代码:

//RVA 转 文件偏移地址
DWORD RvaToOffset(PIMAGE_NT_HEADERS pNt, DWORD dwRva)
{
    DWORD nCount = pNt->FileHeader.NumberOfSections;
    PIMAGE_SECTION_HEADER pSection = IMAGE_FIRST_SECTION(pNt);
    if (pSection == NULL)
    {
        return 0;
    }
    for (int i = 0; i < nCount; i++, pSection++)
    {
        if ((dwRva >= pSection->VirtualAddress) &&( dwRva <= (pSection->VirtualAddress + pSection->Misc.VirtualSize)))
        {
            return dwRva-(pSection->VirtualAddress - pSection->PointerToRawData);
        }
    }
    return 0;
}
//文件偏移地址 转 RVA
DWORD OffsetToRva(PIMAGE_NT_HEADERS pNt, DWORD dwOffset)
{
    DWORD nCount = pNt->FileHeader.NumberOfSections;
    PIMAGE_SECTION_HEADER pSection = IMAGE_FIRST_SECTION(pNt);
    if (pSection == NULL)
    {
        return 0;
    }
    for (int i = 0; i < nCount; i++, pSection++)
    {
        if ((dwOffset >= pSection->PointerToRawData) &&( dwOffset <= (pSection->PointerToRawData + pSection->SizeOfRawData)))
        {
            return dwOffset + (pSection->VirtualAddress - pSection->PointerToRawData);
        }
    }
    return 0;
}

有不足之处请多多指教;


原创粉丝点击