compiler-constructionlanguage-agnosticcoff

Writing a COFF object file... how to reference external symbols


I'm going to write my first compiler (ok... most of the work was done by using ANTLR, but it still required effort)... I have already done the following things:

Now my question is - if I want to create the COFF object files myself, how do I reference external symbols in libraries / other object files?

I mean, for example (if I want to call printf) it works like this, when I do it in ml64:

lea rcx, "address of string"
call printf

But what exactly do I write into the COFF file instead of "printf"?

Everything else is not THAT hard to implement, but I have absolutely no idea how to go about referencing libraries / other object files... hm...

Edit: fixed formatting (sorry) + clarifications


Solution

  • Instead of the printf, you write four times 0x00, i. e. the call itself is assembled as 0xe8 0x00 0x00 0x00 0x00. Further, you need to write a relocation record with type DISP32, with the offset pointing to the first 0x00 and a value pointing to a symbol entry with the name printf. Wikipedia has some links to documents describing the COFF format in detail.