Relocation Section

Relocation Section

The .reloc section of the image file contains the Fixup table, which holds entries for all fixups in the image file. The RVA and size of the .reloc section are defined by the Base Relocation table directory of the PE header. The Fixup table consists of blocks of fixups, each block representing the fixups for a 4-KB page. Blocks are 4-byte-aligned.

Each fixup describes the location of a specific address within the image file as well as how the OS loader should modify the address at this location when loading the image file into memory.

Each fixup block starts with two 4-byte unsigned integers: the RVA of the page containing the address to be fixed up and the size of the block. The fixup entries for this page immediately follow. Each entry is a 2-byte unsigned integer, of which 4 senior bits contain the type of relocation required. The remaining 12 bits contain the relocated address’s offset within the page.

To relocate an address, the OS loader calculates the difference (delta) between the preferred base address (the ImageBase field of the PE header) and the actual base address where the image file is loaded. This delta is then applied to the address according to the type of relocation. If the image file is loaded at its preferred address, no fixups need be applied.

The following relocation types are defined in Winnt.h:

  • IMAGE_REL_BASED_ABSOLUTE (0)  This type has no meaning in an image file, and the fixup is skipped.

  • IMAGE_REL_BASED_HIGH (1)  The high 16 bits of the delta are added to the 16-bit field at the offset. The 16-bit field in this case is the high half of the 32-bit address being relocated.

  • IMAGE_REL_BASED_LOW (2)  The low 16 bits of the delta are added to the 16-bit field at the offset. The 16-bit field in this case is the low half of the 32-bit address being relocated.

  • IMAGE_REL_BASED_HIGHLOW (3)  The delta is added to the 32-bit address at the offset.

  • IMAGE_REL_BASED_HIGHADJ (4)  The high 16 bits of the delta are added to the 16-bit field at the offset. The 16-bit field in this case is the high part of the 32-bit address being relocated. The low 16 bits of the address are stored in the 16-bit word that follows this relocation. A fixup of this type occupies two slots.

  • IMAGE_REL_BASED_MIPS_JMPADDR (5)  The fixup applies to a MIPS jump instruction.

  • IMAGE_REL_BASED_SECTION (6)  Reserved.

  • IMAGE_REL_BASED_REL32 (7)  Reserved.

  • IMAGE_REL_BASED_MIPS_JMPADDR16 (9)  The fixup applies to a MIPS16 jump function.

  • IMAGE_REL_BASED_IA64_IMM64 (9) This is the same type as IMAGE_REL_BASED_MIPS_JMPADDR16.

  • IMAGE_REL_BASED_DIR64 (10)  The delta is added to the 64-bit field at the offset.

  • IMAGE_REL_BASED_HIGH3ADJ (11)  The fixup adds the high 16 bits of the delta to the 16-bit field at the offset. The 16-bit field is the high one-third of a 48-bit address. The low 32 bits of the address are stored in the 32-bit double word that follows this relocation. A fixup of this type occupies three slots.

The only fixup type emitted by the existing managed compilers is IMAGE_REL_BASED_HIGHLOW.

A pure-IL PE file, as a rule, contains only one fixup in the .reloc section. This is for the benefit of the common language runtime startup stub, the only segment of native code in a pure-IL image file. This fixup is for the image file’s IAT, containing a single entry: the runtime DLL.

Windows XP, as a common language runtime aware operating system, needs neither the runtime startup stub nor the IAT to engage the runtime. Thus, if the common language runtime header flags indicate that the image file is IL-only (COMIMAGE_FLAGS_ILONLY), the operating system ignores the .reloc section altogether.

This optimization plays a bad joke with some image files generated by the ILAsm compiler. This compiler produces pure-IL image files but needs relocations executed if any data is located in thread local storage or if data-on-data is defined. To have these relocations executed when the image file is loaded under Windows XP, the ILAsm compiler is forced to cheat and set the common language runtime header flags as if the image file contained embedded native code (COMIMAGE_FLAGS_32BITREQUIRED).

Other compilers don’t have these problems. Compilers generating pure-IL image files (such as Microsoft Visual C# .NET and Microsoft Visual Basic .NET) don’t define TLS-based data or data-on-data.

Because the MC++ compiler and linker produce mixed-code image files, the .reloc sections of these image files can contain any number of relocations. But because mixed-code image files never carry IL-only common language runtime header flags, their relocations are always executed.



Inside Microsoft. NET IL Assembler
Inside Microsoft .NET IL Assembler
ISBN: 0735615470
EAN: 2147483647
Year: 2005
Pages: 147
Authors: SERGE LIDIN

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net