Reversing
21 February 2011 2 Comments

Portable Executable: Converting RVA to File Offset and Back

Introduction

Most fields in a Portable Executable (.exe) file that refer to a location in memory use a so-called Relative Virtual Address (RVA). This is useful because it allows the Windows loader to load the executable in any virtual memory location, without having to change every pointer in the executable. The RVA is relative to the Load Base Address, which is the location where the first byte of the executable is loaded into memory. A virtual address refers to a location in memory, whereas a file offset refers to a location in an executable file on physical storage.

The goal is to convert between these values:

  • (Relative) Virtual Address: a location in virtual memory
  • File offset: a location in a file on physical storage

How it Works

When an executable is loaded into memory it is not copied byte for byte from disk. What happens is a process called aligning: the sections in a PE file are spaced so they all start on the first byte of a memory page (usually 4096 bytes on x86 systems). This can introduce padding at the end of sections. Because of this effect, among other things, we cannot translate directly between RVA and file offset.

An overview of information contained in the IMAGE_SECTION_HEADER.