Benjoyo
Benjoyo

Reputation: 423

Why is the PE format position dependent?

Is there any good reason that an executable file needs a preferred loading address and therefore persist of position dependent code instead of simply using RVAs all over the file?
For me this looks like a major design flaw, I don't understand how one would even come up with this idea.

Upvotes: 4

Views: 521

Answers (1)

Neitsa
Neitsa

Reputation: 8166

I guess the reason is more historical than practical.

Citing Matt Pietrek, from his well known "Peering Inside the PE":

It's common knowledge that Windows NT has a VAX® VMS® and UNIX® heritage. Many of the Windows NT creators designed and coded for those platforms before coming to Microsoft. When it came time to design Windows NT, it was only natural that they tried to minimize their bootstrap time by using previously written and tested tools. The executable and object module format that these tools produced and worked with is called COFF (an acronym for Common Object File Format). [...] The COFF format by itself was a good starting point, but needed to be extended to meet all the needs of a modern operating system like Windows NT or Windows 95. The result of this updating is the Portable Executable format.

So the PE format is based on the COFF format, and the later has the concept of relocations: they allow the system (more precisely the system's Loader) to rebase a PE at runtime by patching position dependent addresses.

The official PE documentation explicitly names Relocations as "COFF Relocations" so I guess the PE relocations are inherited from COFF and are not a new addition brought by the PE format itself.

All in all, my guess is that a position independant PE was discarded (if even ever considered) as the COFF format has already the concept of relocations which achieve the same feature.

Upvotes: 5

Related Questions