如何使用其标头确定准确的 PE 图像文件大小?

发布于 2024-12-16 17:13:16 字数 74 浏览 3 评论 0原文

我需要字节大小,IMAGE_OPTIONAL_HEADER.SizeOfImage 似乎四舍五入到(不确定)边界并且大于实际文件大小。

I need byte size, IMAGE_OPTIONAL_HEADER.SizeOfImage appears to be rounded up to (unsure) boundary and is greater than real file size.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

御弟哥哥 2024-12-23 17:13:16

IMAGE_OPTIONAL_HEADER.SizeOfImage 是虚拟内存中加载的可执行文件/dll 的大小。
它与磁盘上的大小不同。

您可以使用上一节的VirtualAddress + VirtualSize来计算它。

IMAGE_OPTIONAL_HEADER.SizeOfImage 是向上舍入到 IMAGE_OPTIONAL_HEADER.SectionAlignment 的值(通常与页面大小相同)的值。

IMAGE_OPTIONAL_HEADER.SizeOfImage is the size of the loaded executable/dll in virtual memory.
It is not the same as the size on disk.

You can calculate it with VirtualAddress + VirtualSize of the last section.

IMAGE_OPTIONAL_HEADER.SizeOfImage is that value rounded up to the value of IMAGE_OPTIONAL_HEADER.SectionAlignment (usually the same as the page size).

过气美图社 2024-12-23 17:13:16
if(LastSectionVirtualSize >= LastSectionSizeOfRawData)
{
    if( LastSectionVirtualSize % LastSectionSectionAlignment )
    {
        TempValue = LastSectionVirtualSize - (LastSectionVirtualSize % LastSectionSectionAlignment) + LastSectionSectionAlignment ;
    }
    else
    {
        TempValue = LastSectionVirtualSize ;    
    }
}
else
{
    if(LastSectionSizeOfRawData % LastSectionSectionAlignment)
    {
        TempValue = LastSectionSizeOfRawData - (LastSectionSizeOfRawData % LastSectionSectionAlignment) + LastSectionSectionAlignment ;
    }
    else
    {
        TempValue = LastSectionSizeOfRawData ;
    }
}

OH.SizeOfImage  = TempValue + dwLastSecRVA ;
if(LastSectionVirtualSize >= LastSectionSizeOfRawData)
{
    if( LastSectionVirtualSize % LastSectionSectionAlignment )
    {
        TempValue = LastSectionVirtualSize - (LastSectionVirtualSize % LastSectionSectionAlignment) + LastSectionSectionAlignment ;
    }
    else
    {
        TempValue = LastSectionVirtualSize ;    
    }
}
else
{
    if(LastSectionSizeOfRawData % LastSectionSectionAlignment)
    {
        TempValue = LastSectionSizeOfRawData - (LastSectionSizeOfRawData % LastSectionSectionAlignment) + LastSectionSectionAlignment ;
    }
    else
    {
        TempValue = LastSectionSizeOfRawData ;
    }
}

OH.SizeOfImage  = TempValue + dwLastSecRVA ;
歌枕肩 2024-12-23 17:13:16

事实上,接受的答案是不正确的。要获取可执行文件在磁盘上的大小,您应该计算最后一部分的 PointerToRawData + SizeOfRawData,而不是虚拟对应部分。有关示例,请参阅 http://www.strchr.com/creating_self-extracting_executables

另请注意,某些编译器喜欢在最后一节之后附加调试信息。请务必删除任何符号表或设置发布模式,具体取决于您使用的编译器,才能使其正常工作。

Actually, the accepted answer is incorrect. To get the executable's size on disk, you should calculate PointerToRawData + SizeOfRawData of the last section, not the virtual counterparts. For an example see http://www.strchr.com/creating_self-extracting_executables

Also note that some compilers like to append debug information after the last section. Be sure to strip any symbol tables or set release mode, depending on what compiler you use, for this to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文