A COFF object file keeps its code, data and everything else in numbered sections, counted from 1. The section table comes straight after the file header, with one 40-byte entry for each section. The bytes of each section are elsewhere in the file, and the entry says where.
The section header
IMAGE_SECTION_HEADER, 40 bytes:
| Offset | Size | Field | Meaning in an object file |
|---|---|---|---|
| 0 | 8 | Name | An 8-byte, null-padded UTF-8 string, with no terminator when it is exactly 8 bytes long. See Names. |
| 8 | 4 | VirtualSize | Valid only for executable images; should be 0 in an object file. (LLVM notes that some buggy COFF writers set it anyway, and its reader ignores it for objects.) |
| 12 | 4 | VirtualAddress | In an object file, the address of the first byte before relocation is applied; compilers should set it to 0. Otherwise it is an arbitrary value that is subtracted from offsets during relocation. |
| 16 | 4 | SizeOfRawData | The size of the section in an object file (of the initialised data on disk in an image). PE Format says that when a section holds only uninitialised data this field should be 0. LLVM’s reader, however, takes SizeOfRawData as the section size for every object file (getSectionSize), so a nonzero value on a section with the uninitialised-data flag is read as its memory size (inference from that code; not stated by Microsoft). |
| 20 | 4 | PointerToRawData | The file offset of the section’s first page. In an object file it should be aligned on a 4-byte boundary for best performance. 0 when the section holds only uninitialised data. |
| 24 | 4 | PointerToRelocations | The file offset of its relocations, 0 when there are none (also 0 in an executable image). |
| 28 | 4 | PointerToLinenumbers | The file offset of its COFF line numbers; 0 when there are none. |
| 32 | 2 | NumberOfRelocations | How many relocations it has. When the count exceeds the 16 bits, the section has IMAGE_SCN_LNK_NRELOC_OVFL set and this field holds 0xFFFF; the actual count is then in the 32-bit VirtualAddress field of the first relocation (which is not itself a relocation to apply). It is an error if the flag is set and the section has fewer than 0xFFFF relocations (PE Format). |
| 34 | 2 | NumberOfLinenumbers | How many line numbers it has. |
| 36 | 4 | Characteristics | Flags, and the alignment; see Flags. |
Names
A name of up to 8 bytes is stored in the header itself. A longer name is written as a
slash and a decimal number, such as /123: the number is an ASCII decimal offset into the
string table. Executable images do not use a string table and do not support section names longer than 8 characters; a long name in an object file is truncated if it is emitted to an executable (PE Format). (The slash and up to 7 decimal digits fill the 8 bytes; a larger offset cannot be written in decimal.) For larger string tables LLVM’s reader accepts // followed by up to six base-64 digits, with the alphabet A–Z a–z 0–9 + / for the values 0 to 63; this form is not in Microsoft’s specification (it is in LLVM’s COFFObjectFile.cpp, decodeBase64StringEntry), and the reader rejects a value that does not fit 32 bits.
Section numbers in symbols and in the relocation table count from 1; entries are in the order the linker chooses.
In an object file the $ in a section name has a special meaning. To decide
which image section holds an object section, the linker discards the $ and
every character after it, so .text$X contributes to .text.
The characters after the $ order the contributions: all contributions with
the same object-section name are placed contiguously, and those blocks are sorted in
lexical order by object-section name, so .text$X comes after
.text$W and before .text$Y. An image section name never
contains a $ (PE Format, Grouped Sections).
Flags
| Bits | Constant | Meaning |
|---|---|---|
0x00000000, 0x1, 0x2, 0x4, 0x10, 0x400 | (reserved) | Reserved for future use |
0x00000008 | IMAGE_SCN_TYPE_NO_PAD | The section should not be padded to the next boundary. Obsolete, replaced by IMAGE_SCN_ALIGN_1BYTES; valid only for object files |
0x00000020 | IMAGE_SCN_CNT_CODE | Executable code |
0x00000040 | IMAGE_SCN_CNT_INITIALIZED_DATA | Initialised data |
0x00000080 | IMAGE_SCN_CNT_UNINITIALIZED_DATA | Uninitialised data (BSS) |
0x00000100 | IMAGE_SCN_LNK_OTHER | Reserved for future use |
0x00000200 | IMAGE_SCN_LNK_INFO | Comments or other information; the .drectve section has this type. Valid for object files only |
0x00000800 | IMAGE_SCN_LNK_REMOVE | The section will not become part of the image. Valid only for object files |
0x00001000 | IMAGE_SCN_LNK_COMDAT | A COMDAT section. Valid only for object files |
0x00008000 | IMAGE_SCN_GPREL | Data referenced through the global pointer (GP) |
0x00020000 | IMAGE_SCN_MEM_PURGEABLE / IMAGE_SCN_MEM_16BIT | Reserved for future use (two names for the same bit) |
0x00040000 | IMAGE_SCN_MEM_LOCKED | Reserved for future use |
0x00080000 | IMAGE_SCN_MEM_PRELOAD | Reserved for future use |
0x00100000–0x00E00000 | IMAGE_SCN_ALIGN_1BYTES (0x100000) to IMAGE_SCN_ALIGN_8192BYTES (0xE00000) | The alignment, valid only for object files: a value n from 1 to 14 in bits 20–23 (mask 0x00F00000) means 2n−1 bytes, so 1 is 1 byte, 2 is 2, 3 is 4, 4 is 8, 5 is 16, 6 is 32, 7 is 64, 8 is 128, 9 is 256, 10 is 512, 11 is 1024, 12 is 2048, 13 is 4096 and 14 is 8,192. The specification defines no meaning for the values 0 and 15. |
0x01000000 | IMAGE_SCN_LNK_NRELOC_OVFL | The section contains extended relocations: its count is in the first relocation (see NumberOfRelocations above) |
0x02000000 | IMAGE_SCN_MEM_DISCARDABLE | Can be discarded as needed |
0x04000000 | IMAGE_SCN_MEM_NOT_CACHED | Cannot be cached |
0x08000000 | IMAGE_SCN_MEM_NOT_PAGED | Cannot be paged |
0x10000000 | IMAGE_SCN_MEM_SHARED | Can be shared in memory |
0x20000000 | IMAGE_SCN_MEM_EXECUTE | Can be executed |
0x40000000 | IMAGE_SCN_MEM_READ | Can be read |
0x80000000 | IMAGE_SCN_MEM_WRITE | Can be written |
Section data
PointerToRawDatathe section’s bytesSizeOfRawDataPointerToRelocationsrelocations10 bytes eachPointerToLinenumbersline numbers6 bytes each
Microsoft’s compiler writes each section’s data followed by its relocations, but the
header’s offsets are what count. Initialised data is a plain block
of bytes, and for a section that is all zeros the data need not be included. The data of
a section sits at PointerToRawData and is SizeOfRawData bytes long. A section that holds
only uninitialised data (CNT_UNINITIALIZED_DATA) has a zero
PointerToRawData, as PE Format requires.
COFF line numbers
These old records, deprecated in favour of CodeView, are 6 bytes each:
| Offset | Size | Field | Meaning |
|---|---|---|---|
| 0 | 4 | Type | A union of two 4-byte fields. When Linenumber is 0 it is SymbolTableIndex, the index of a function symbol. Otherwise it is VirtualAddress: the address of the code for that source line (in an object file, the address within the section) |
| 4 | 2 | Linenumber | When nonzero, a one-based line number, relative to the beginning of the function. When zero, the Type field is a symbol table index for a function |
A group of line-number records always begins with a record that has Linenumber 0 and the index of a function symbol (if it is the first record in a section with the COMDAT flag, that symbol is also the COMDAT symbol name). It is followed by any number of records with Linenumber greater than 0, one for every source line of the function except its first line. The function symbol’s auxiliary record points at that first record. Source: PE Format, COFF Line Numbers (Deprecated): they “are no longer produced and, in the future, will not be consumed”. Microsoft’s variant is extended so that one section can relate to line numbers in several source files (the specification does not say how).