I'm looking for a little bit of direction in writing a compiler. I've written in Common Intermediate Language, C#, and various other .NET languages; I've written my own Metadata Parser and now I'm trying to understand the various aspects of the Portable Executable (PE) layout. One thing that somewhat befuddles me is the .reloc section.

I have the Relocs parsing (or at least I think I do? :) and I wanted to know, within .NET libraries they usually have a single Reloc block with a single HighLow (IMAGE_REL_BASED_HIGHLOW, or just 3) reloc at a given offset that changes. When I go to writing my own PE Header and COFF sections, how do I calculate the Relative Virtual Address of the block, and the Offset (lower 12 bits) on the TypeOffset entry that follows that block header?

I'm currently using the pecoff_v83.docx (Microsoft Portable Executable and Common Object File Format Specification Revision 8.3) from Microsoft's website, but I think there's a step I'm missing out on.

I'll link a relevant post: How are PE Base Relocations build up?

^ That helped me parse the Reloc entries, but parsing them and generating the data that goes into them are different questions.

  • 1
    For now, since it had been three-four years since I last worked with the PE Header work, I will go back and reevaluate their structure (with the spec) in hopes it will shed some light on the Reloc section, perhaps there's a commonality between them I'm not seeing. – Allen Clark Copeland Jr Nov 29 '15 at 19:52
  • A compiler targeting the .NET Framework (Common Language Infrastructure, ECMA-335.) – Allen Clark Copeland Jr Dec 1 '15 at 21:23
  • 1
    I think it's a fascinating idea. There are now THREE open source SETS of C# compilers. There's classic mono. There's the DNX stuff, and then there's ROSLYN. I would think you will find more "concrete documentation" on COFF-output-generation in these concrete implementations than anywhere in any documentation. Best of luck. – Warren P Dec 2 '15 at 14:49
  • @Warren P, May I ask what the name of the DNX C# compiler is? Thank you. – Frank Dec 3 '15 at 14:30

... how do I calculate the Relative Virtual Address of the block, and the Offset (lower 12 bits) on the Type Offset entry that follows that block header?

Microsoft - PE Format (05/30/2018):

"Base Relocation Block

Each base relocation block starts with the following structure:

Offset      Size      Field      Description

   0              4          RVA      The image base plus the page RVA is added to each offset to create the VA where the base relocation must be applied.

   4              4           BS         The total number of bytes in the base relocation
block, including the Page RVA and Block Size fields and the Type/Offset fields that follow.


The Block Size field is then followed by any number of Type or Offset field entries. Each entry is a WORD (2 bytes) and has the following structure:

Offset      Size      Field      Description

   0             4b        Type      Stored in the high 4 bits of the WORD, a value that indicates the type of base relocation to be applied. For more information, see Base Relocation Types.

   0            12b       Type      Stored in the remaining 12 bits of the WORD, an offset from the starting address that was specified in the Page RVA field for the block. This offset specifies where the base relocation is to be applied.



Example code is in BinUtils.

You can find testing code in binutils/ld/testsuite/ld-cygwin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.