在.NET Assembly中从RVA计算文件偏移 [英] Calculating file offset from RVA in .NET Assembly

查看:149
本文介绍了在.NET Assembly中从RVA计算文件偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用可选标头计算CLI标头文件的偏移量,我手动检查了一个示例.NET程序集,并注意到可选标头为我提供了CLI标头的RVA,它为 0x2008 ,CLI标头的文件偏移为 0x208 。如何从RVA计算文件偏移?
谢谢。

I'm trying to calculate the CLI Header file offset using the optional header, I manually checked a sample .NET Assembly and noticed that the optional header gives me the RVA for the CLI Header which is 0x2008 and the file offset of the CLI Header is 0x208. How can I calculate the file offset from the RVA? Thanks.

推荐答案

PE文件包含许多部分,这些部分使用该部分映射到页面对齐的虚拟地址表(紧接在可选标头之后)。

The PE file contains a bunch of sections that get mapped to page aligned virtual addresses using the section table (just after the optional header).

因此,要读取CLI标头,您可以:

So to read the CLI Header, you can either:


  • 使用类似 LoadLibrary LoadLibraryEx 映射到内存,然后将RVA添加到返回的模块基地址中,

  • ,或者您可以阅读节表并使用它来映射RVA

  • use something like LoadLibrary or LoadLibraryEx to map it into memory and then just add the RVA to the returned module base address,
  • or you can read the section table and use it to map the RVA to a file position.
/* pseudo code */
int GetFilePosition(int rva)
{
    foreach (var section in Sections)
    {
        var pos = rva - section.VirtualAddress;

        if (pos >= 0 && pos < section.VirtualSize)
        {
            return pos + section.PointerToRawData;
        }
    }

    Explode();
}

节表在 ECMA-335 分区II第25.3节

The Section table is described in ECMA-335 Partition II Section 25.3

这篇关于在.NET Assembly中从RVA计算文件偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆