首页
社区
课程
招聘
[求助]请教对PE文件中几个数据的理解
发表于: 2009-1-30 17:35 3948

[求助]请教对PE文件中几个数据的理解

2009-1-30 17:35
3948
最近在复习PE文件的结构,其中几个数据我总是感到不解。请大家帮我解解惑吧。

下面是我用dumpbin.exe分析VC6的link.exe输出的结果。

对设成红色的那几个数我一直很困惑,请大家帮我分析分析。

D:\Program Files\Microsoft Visual Studio\VC98\Bin>dumpbin /headers link.exe
Microsoft (R) COFF Binary File Dumper Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

Dump of file link.exe

PE signature found

File Type: EXECUTABLE IMAGE

FILE HEADER VALUES
             14C machine (i386)
               3 number of sections
        36F795EF time date stamp Tue Mar 23 21:23:59 1999
               0 file pointer to symbol table
               0 number of symbols
              E0 size of optional header
             10F characteristics
                   Relocations stripped
                   Executable
                   Line numbers stripped
                   Symbols stripped
                   32 bit word machine

OPTIONAL HEADER VALUES
             10B magic #
            6.00 linker version
           68000 size of code
           35000 size of initialized data
               0 size of uninitialized data

           67FAF RVA of entry point
            1000 base of code
           69000 base of data
          400000 image base
            1000 section alignment
            1000 file alignment
            4.00 operating system version
            0.00 image version
            4.00 subsystem version
               0 Win32 version
           9E000 size of image
            1000 size of headers
               0 checksum
               3 subsystem (Windows CUI)
               0 DLL characteristics
          100000 size of stack reserve
            1000 size of stack commit
          100000 size of heap reserve
            1000 size of heap commit
               0 loader flags
              10 number of directories
               0 [       0] RVA [size] of Export Directory
           68548 [      50] RVA [size] of Import Directory
           97000 [    6F78] RVA [size] of Resource Directory
               0 [       0] RVA [size] of Exception Directory
               0 [       0] RVA [size] of Certificates Directory
               0 [       0] RVA [size] of Base Relocation Directory
            1280 [      1C] RVA [size] of Debug Directory
               0 [       0] RVA [size] of Architecture Directory
               0 [       0] RVA [size] of Special Directory
               0 [       0] RVA [size] of Thread Storage Directory
               0 [       0] RVA [size] of Load Configuration Directory
               0 [       0] RVA [size] of Bound Import Directory
            1000 [     278] RVA [size] of Import Address Table Directory
           68328 [      60] RVA [size] of Delay Import Directory
               0 [       0] RVA [size] of Reserved Directory
               0 [       0] RVA [size] of Reserved Directory

SECTION HEADER #1
   .text name
   67FF2 virtual size
    1000 virtual address
   68000 size of raw data
    1000 file pointer to raw data
       0 file pointer to relocation table
       0 file pointer to line numbers
       0 number of relocations
       0 number of line numbers
60000020 flags
         Code
         Execute Read

  Debug Directories

    Type       Size      RVA  Pointer
    ------ -------- -------- --------
    cv           35 00000000    71000    Format: NB10, 36f795a3, 7, E:\8447\vc98
\linker\release\link.pdb

SECTION HEADER #2
   .data name
   2D958 virtual size
   69000 virtual address
    1000 size of raw data
   69000 file pointer to raw data
       0 file pointer to relocation table
       0 file pointer to line numbers
       0 number of relocations
       0 number of line numbers
C0000040 flags
         Initialized Data
         Read Write


SECTION HEADER #3
   .rsrc name
    6F78 virtual size
   97000 virtual address
    7000 size of raw data
   6A000 file pointer to raw data
       0 file pointer to relocation table
       0 file pointer to line numbers
       0 number of relocations
       0 number of line numbers
40000040 flags
         Initialized Data
         Read Only

  Summary

       2E000 .data
        7000 .rsrc
       68000 .text

--------------------------------------->8--------------------------------------

在可选头部中有:

           68000 size of code
           35000 size of initialized data
               0 size of uninitialized data

这里写着未初始化数据的大小是0。

而在.data节中:

SECTION HEADER #2
   .data name
   2D958 virtual size
   69000 virtual address
    1000 size of raw data
   69000 file pointer to raw data
       0 file pointer to relocation table
       0 file pointer to line numbers
       0 number of relocations
       0 number of line numbers
C0000040 flags
         Initialized Data
         Read Write


从这里可以看到:
virtual size       =  2D958,
size of raw data   = 1000;
virtual size > size of raw data。

virtual size > size of raw data,这不是说明.data这个节含有未初始化的数据吗?

这和可选头部中的“0 size of uninitialized data”不是有冲突了吗?

[培训]科锐逆向工程师培训第53期2025年7月8日开班!

收藏
免费 0
支持
分享
最新回复 (8)
雪    币: 2067
活跃值: (82)
能力值: ( LV9,RANK:180 )
在线值:
发帖
回帖
粉丝
2
听说那个值不用管他.
听说那个值是无意义的.
2009-1-30 17:40
0
雪    币: 27
活跃值: (127)
能力值: ( LV8,RANK:120 )
在线值:
发帖
回帖
粉丝
3
“听说”不太靠谱吧,我们学东西需要扎实啊。
2009-1-30 17:44
0
雪    币: 2067
活跃值: (82)
能力值: ( LV9,RANK:180 )
在线值:
发帖
回帖
粉丝
4
嗯. 有道理.

听说操作系统并不检查这值正不正确
2009-1-30 17:53
0
雪    币: 27
活跃值: (127)
能力值: ( LV8,RANK:120 )
在线值:
发帖
回帖
粉丝
5
顶起来,呼叫书呆彭。

在以上dumpbin的输出结果中:

到底是“文件不含未初始化的数据”正确?

还是“在.data节中含有未初始化的数据”正确?
2009-1-30 23:15
0
雪    币: 2110
活跃值: (21)
能力值: (RANK:260 )
在线值:
发帖
回帖
粉丝
6
“程序”包含未初始化的数据。

“文件”不包含。

.data节,它的“内存镜像”中必须包含“未初始化数据”和“用0初始化的数据”

但它的“文件镜像”中只要包含“非0初始值”的数据即可。

OptionalHeader描述的是PE文件,SizeOfUninitializedData是0,是说在磁盘文件中没有保存“未初始”和“0初始”的数据的映像。

而SectionHeader描述.data这个section,既要描述它的文件镜像,也要描述它的内存镜像。

加载器是按照Section来进行内存映射的,它从文件头中读取section table,然后按照表中的section header把需要映射的section依次映射到内存中,再处理好相关重定位就好了。

有的链接器会把“未初始化数据”和“0初始化数据”放在名为.bss的section,该section的文件大小为0,而内存大小为需要分配的空间,这种情况下,OptionalHeader中SizeOfUninitializedData可能不为零,取决于连接器对该域的填充。

总之,OptionalHeader中的SizeOf字段是链接器填充的,然而加载器按section来映射内存,不使用它。所以连接器通常就直接将它填为0了。

听说是这样的。
2009-1-31 08:37
0
雪    币: 27
活跃值: (127)
能力值: ( LV8,RANK:120 )
在线值:
发帖
回帖
粉丝
7
to 书呆彭:

我是这样理解IMAGE_SECTION_HEADER结构中的几个成员的。

VirtualSize:节的内存镜像的大小(没有对齐)

SizeOfRawData:节的文件镜像的大小(按照OptionalHeader中的FileAlignment对齐)

这样理解对吗?

如果以上理解正确的话,怎么才可以从PE文件中得知“节的文件镜像的大小(没有对齐)”呢?
2009-1-31 16:28
0
雪    币: 2110
活跃值: (21)
能力值: (RANK:260 )
在线值:
发帖
回帖
粉丝
8
根据文档中的说明,是这样的。

获得“节的文件镜像的大小(没有对齐)”它本来就没什么意义。举例来说,比如一个节是代码节,它的大小是500个字节,而文件是按512字节对齐的,那么连接器直接在500字节的有用数据后面填上12字节的‘0字节’,然后SizeOfRawData就是512,无法得知,也不用去知道实际有用的数据是多少字节,因为除非程序(故意设计或因设计中存在BUG)“跑飞”或“越界访问”,否则填充的部分将永远不可能被访问到,即它们是“无害填充”。

对数据节是一样的道理,反正“未初始化”的数据也是需要用“0字节”来填充的,所以在“已初始化的数据”的节的后面用0来填充是合理的。

所以,只要记录了一个节的对齐的size即可,而不对齐的size没有必要记录。
2009-1-31 21:28
0
雪    币: 424
活跃值: (10)
能力值: ( LV9,RANK:850 )
在线值:
发帖
回帖
粉丝
9
size无需对齐,要对齐的是rva/offset
节的镜像大小就是VirtualSize
2009-2-1 01:00
0
游客
登录 | 注册 方可回帖
返回