Tutorial 1: Overview of PE file format
DOS MZ header
|
DOS stub
|
PE header
|
Section table
|
Section 1
|
Section 2
|
Section ...
|
Section n
|
If we view the PE file format as a logical disk, the PE header as the boot sector and the sections as files, we still don't have enough information to find out where the files reside on the disk, ie. we haven't discussed the directory equivalent of the PE file format. Immediately following the PE header is the section table which is an array of structures. Each structure contains the information about each section in the PE file such as its attribute, the file offset, virtual offset. If there are 5 sections in the PE file, there will be exactly 5 members in this structure array. We can then view the section table as the root directory of the logical disk. Each member of the array is equvalent to the each directory entry in the root directory.
The section table of this file looks like the following:
Section | Virtual Size | Virtual Offset | Raw Size | Raw Offset | Characteristics |
.text | 0000024C | 00001000 | 00000400 | 00000400 | 60000020 |
.rdata | 000001DC | 00002000 | 00000200 | 00000800 | 40000040 |
.data | 000000E0 | 00003000 | 00000200 | 00000A00 | C0000040 |
That's all about the physical layout of the PE file format. I'll summarize the major steps in loading a PE file into memory below:
- When the PE file is run, the PE loader examines the DOS MZ header for the offset of the PE header. If found, it skips to the PE header.
- The PE loader checks if the PE header is valid. If so, it goes to the end of the PE header.
- Immediately following the PE header is the section table. The PE header reads information about the sections and maps those sections into memory using file mapping. It also gives each section the attributes as specified in the section table.
- After the PE file is mapped into memory, the PE loader concerns itself with the logical parts of the PE file, such as the import table.