ORC JIT - Incorrect support for COFF files?

Hey LLVM-Mailing-List and Lang,

I’m still learning how to use the ORC JIT but I finally reached the point to JIT and execute some code. For this purpose I created a test file (TestModule.cpp) and compiled it with Clang, generating two different files, one in the LLVM IR format and one in the Microsoft COFF format.

The JIT resolves all undefined references, including “extern int planschiValue;” and “void externFunction();”. Using the IR Module I will get the correct address for “planschiValue” and the correct value – however this is not the case for the object file.

For the object file I get (via printf from the module) the address 0x000002295D6B003C while the actual address is 0x00007FF71D9959A4.

I’m really surprised about this, because the IR module works with no problem. I attach the source code, the IR file and the resulting object file (including its assembly file).

Any ideas what I’m doing wrong?

Thank you for the help in advance and kind greetings

Björn

TestModule.cpp (379 Bytes)

TestModule.asm (4.51 KB)

BitCodeFile.bc (6.77 KB)

ObjectFile.obj (2.59 KB)

I figured out that this problem is caused because “planschiValue” has a REL32 relocation and the addresses between the code and the variable overflows 32bit.

Is there any workaround for this kind of issue?

Hi Björn

I made a workaround for this specific issue a long time ago for the Projucer C++ JIT Engine. It basically forwards the call to another stub that provides enough space to encode a full 64-bit address. The patch is based on LLVM 3.9, so I guess it won’t work out-of-the-box on a recent release, but it may give you enough hints to figure it out on your own:
Alternatively, you could consider a memory manager that makes sure all calls are within a 32-bit range. For the code you JIT yourself, this should be no problem. In case you have to link against an old MSVCRT though, this is not possible, because it has a fixed load address that’s far beyond the 32-bit range from any heap allocation you can make for your JITed code. That’s been our issue back then and so this patch was the last resort. Please also note: this is for freestanding function only. I didn’t consider member function calls, etc. (addend is always 0) because it was not relevant for the specific issue. Hope it helps Stefan

Hi Stefan, Bjoern,

For calls across object boundaries __dllimport now supported in RuntimeDyld: https://github.com/llvm/llvm-project/commit/337e131ca7de48072def7729df69434c37a66eb7. You may just be able to mark your externs as __dllimport for this to work. I don’t recall whether __dllimport works for data symbols too, but I suspect so – I think it’s basically an explicit GOT entry.

If anyone is ever interested in writing a JITLink COFF implementation I will be happy to help out or review patches – I’d love to get better COFF support in tree.

– Lang.

Hey Lang and Stefan,

Using dllimport on my “planschiValue” actually worked! But I have no idea why, because the relocation is still a REL32 if I use dumpbin… So how is it possible for that to work?

However… when I load an COFF object file, am I able to change the relocations to dllimport somehow? I honestly can’t imagine how this would work since the machine code is probably already adjusted to use a REL32… or something… Just wanted to ask…

@Stefan Gränitz I remember your patch! How does it deal with extern variables though? As far as I understood from the code – not that I understood much – it fixes function calls by having a trampoline right?

Thank you guys!

Kind greetings

Björn

Hi Bjoern,

Using dllimport on my “planschiValue” actually worked! But I have no idea why, because the relocation is still a REL32 if I use dumpbin…

From memory dllimport is like a GOT access: You’ll have a REL32 either way, but instead of a REL32 directly to the variable you’ll end up with a REL32 to an entry in a pointer table containing the address of the variable, and the code sequence will change to access the variable indirectly via the pointer you load.

– Lang.

Hey Lang,

That is really cool :smiley: Is the creation of that table a Windows thingy or is this the way the LLVM handles it?

Also… since it is COFF related – the never ending story of “finding my global constructors” first of all: Yes! I tried using the “initialize” function of LLVMJIT – however this only worked when I was loading a Module. When I added the object file (from the same source) the constructors were not called at all. What also really bothers me, when I load the object from disk and iterate over the symbols I will find: “_GLOBAL__sub_I_TestModule.cpp” but when I do a lookup on it, the symbol will not be found…

It’s like being sooo close to the constructor but den someone takes it away from me :< Plus object files seem to be not passed to the TransformationFunction so no luck there either. I solved this issue with Modules by either using the mentioned function or by changing the visibility of the symbol – it seems like both is not possible with an object file .w.

Kind greetings

Björn

Hi Bjoern,

That is really cool :smiley: Is the creation of that table a Windows thingy or is this the way the LLVM handles it?

This is a Windows ABI feature, similar to GOTs on MachO and ELF. RuntimeDyld just needed to be taught to build the table correctly.

Also… since it is COFF related – the never ending story of “finding my global constructors” first of all: Yes! I tried using the “initialize” function of LLVMJIT – however this only worked when I was loading a Module. When I added the object file (from the same source) the constructors were not called at all. What also really bothers me, when I load the object from disk and iterate over the symbols I will find: “_GLOBAL__sub_I_TestModule.cpp” but when I do a lookup on it, the symbol will not be found…

This is a known limitation. To enable running static constructors from a COFF object file we would need a COFF Platform, and a COFF version of JITLink. The Platform would identify the static constructors when they are added to the JIT, and the COFF JITLink implementation would make them accessible despite them being static.

Regards,
Lang.

Hey Lang,

Thank you for the explanation!

This is a known limitation. To enable running static constructors from a COFF object file we would need a COFF Platform, and a COFF version of JITLink. The Platform would identify the static constructors when they are added to the JIT, and the COFF JITLink implementation would make them accessible despite them being static.

Oh I see… I had a quick look into the Platform thing… but it seems this is waaaay more then just the two pure virtual function I saw. Seems like, this is no easy thing to hijack right?

Thank you so far!

Kind greetings

Björn