Chapter 15. win32: Microsoft Win32 Object Files

Table of Contents

15.1. win32 Extensions to the SECTION Directive
15.2. win32: Safe Structured Exception Handling

The win32 object format generates Microsoft Win32 object files for use on the 32-bit native Windows XP (and Vista) platforms. Object files produced using this object format may be linked with 32-bit Microsoft linkers such as Visual Studio in order to produce 32-bit PE executables.

The win32 object format provides a default output filename extension of .obj.

Note that although Microsoft say that Win32 object files follow the COFF (Common Object File Format) standard, the object files produced by Microsoft Win32 compilers are not compatible with COFF linkers such as DJGPP’s, and vice versa. This is due to a difference of opinion over the precise semantics of PC-relative relocations. To produce COFF files suitable for DJGPP, use the coff output format; conversely, the coff format does not produce object files that Win32 linkers can generate correct output from.

15.1. win32 Extensions to the SECTION Directive

The win32 object format allows you to specify additional information on the SECTION directive line, to control the type and properties of sections you declare. Section types and properties are generated automatically by Yasm for the standard section names .text, .data and .bss, but may still be overridden by these qualifiers.

The available qualifiers are:

code or text
Defines the section to be a code section. This marks the section as readable and executable, but not writable, and also indicates to the linker that the type of the section is code.
data or bss
Defines the section to be a data section, analogously to code. Data sections are marked as readable and writable, but not executable. data declares an initialized data section, whereas bss declares an uninitialized data section.
rdata
Declares an initialized data section that is readable but not writable. Microsoft compilers use this section to place constants in it.
info
Defines the section to be an informational section, which is not included in the executable file by the linker, but may (for example) pass information to the linker. For example, declaring an info-type section called .drectve causes the linker to interpret the contents of the section as command-line options.
align=n
Specifies the alignment requirements of the section. The maximum you may specify is 8192: the Win32 object file format contains no means to request a greater section alignment. If alignment is not explicitly specified, the defaults are 16-byte alignment for code sections, 8-byte alignment for rdata sections and 4-byte alignment for data (and BSS) sections. Informational sections get a default alignment of 1 byte (no alignment), though the value does not matter. The alignment must be a power of 2.

Other qualifiers are supported which control specific section flags: discard, cache, page, share, execute, read, write, and base. Each of these sets the similarly-named section flag, while prefixing them with no clears the corresponding section flag; e.g. nodiscard clears the discard flag.

The defaults assumed by Yasm if you do not specify the above qualifiers are:

section .text    code  align=16
section .data    data  align=4
section .rdata   rdata align=8
section .rodata  rdata align=8
section .rdata$  rdata align=8
section .bss     bss   align=4
section .drectve info
section .comment info

Any other section name is treated by default like .text.