Repository navigation
Support relocation kind 0003 and extend IMAGE_REL_ types - #141
Conversation
Specifically adds support for: - IMAGE_REL_AMD64_ADDR32NB. (This is the "relocation kind 0003"). - IMAGE_REL_I386_DIR32NB. (This is the x86 version of the above). - IMAGE_REL_AMD64_REL32_5. (This is documented but weirdly not implemented).
| err->code = 3; | ||
| goto restore; | ||
| } | ||
| *((UINT32*) ptr->addr) = s; |
There was a problem hiding this comment.
Just for my own understanding:
- In LLVM, this relocation is implemented by
add32(off, s), so thatoffin LLVM corresponds toptr->addrin Flexlink andsin LLVM corresponds tosin Flexlink. - The relocation
IMAGE_REL_AMD64_ADDR64in LLVM (RELOC_ABSin Flexlink) is implemented byadd64(off, s + imageBase), so thats + imageBasein LLVM corresponds tosin Flexlink.
Do you understand why the s arguments do not seem to match between the two cases?
There was a problem hiding this comment.
Going one step further, those equations imply that imageBase = 0.
I spent a lot of time looking and I couldn't find out why IMAGE_REL_AMD64_ADDR64 seems to work. The math for all the other relocation kinds make sense.
However, two things ...
- The
/base:base addresslink.exeoption is fixed for OCaml executable to be0x10000atLine 47 in 80496b5
and also the same in
Line 77 in 80496b5
for OCaml plugins / stubs. - All of the relative relocations from this section of code are translated by:
Lines 470 to 475 in 80496b5
into absolute relocations byReloc.abs:Lines 437 to 444 in 80496b5
So my suspicion was that the Reloc.abs absolute relocations were being translated at CreateProcess time by ntdll.LdrInitializeThunk (or whatever is reading the PE .reloc section) to complete the imageBase adjustment.
@dra27, any chance you can explain why base_addr (or image_base for DLLs) is not used in the calculations?
(These calculations are undocumented in flexdll and fairly complicated)
There was a problem hiding this comment.
It's far too long since I stared at all this, but are we comparing like with like here? Isn't that code in LLVM the part of a linker which is computing actual relocations to be carried out by the loader? In flexdll's, case, I don't think we have to worry about the image base - the symbol table as a data structure has already been had all the relocations resolved when the executable was loaded (by the Windows loader) - so s in flexdll is an absolute address for the symbol, where in lld it's still base-relative?
There was a problem hiding this comment.
Yes, your loader comment is what I had meant with "translated at CreateProcess time by ntdll.LdrInitializeThunk". But I am still puzzled why that only applies to IMAGE_REL_AMD64_ADDR64.
|
I just noticed that I have not extended the DLL logic at Lines 235 to 253 in 80496b5 That DLL logic does use the I don't really have a way to test that. The stubs/plugins generated by OCaml do not have those relocations (which is also why I didn't notice them). |
387e4b8 to
19aadf0
Compare
Co-authored-by: Antonin Décimo <[email protected]>
dra27
left a comment
There was a problem hiding this comment.
This looks correct to me - at least, it sounds as though it's reasonably clear from your testing that the RELOC_32NB implementation is working, and the RELOC_REL32_5 implementation is clearly correct w.r.t. the others already there.
Do you mind adding the last missing RELOC_REL32_3 while we here, and I think this can go straight in.
|
FIXED. This is an independent problem.
etc. @MisterDA: Looks like casting of int64/uint64 to uint32 is not done correctly ... I think it was just luck before if it was working. Your Avoid conversion from 'INT_PTR' to 'UINT32' warnings review suggestion is very close to where I'm seeing the failures, so it sounds like the C compiler was giving us a good warning. If you know how to make those INT_PTR/INT32/UINT32 operations robust, reply back! I'll investigate more in the meantime. EDIT: After looking at old emails, it sounds like the checks are to make sure that the relocations are within 2GB, which presumes all the DLLs are within 2GB (here and here). And that begs the question what magic mechanism is enforcing that 2GB bin-packing! I wonder if there is a MSVC option to force all these problematic address references to be 64-bit on 64-bit machines. EDIT 2: Changing |
Also reorder match (OCaml) and case (C) for clarity and to ensure nothing is missed.
dra27
left a comment
There was a problem hiding this comment.
Tiny nit with the (very welcome) readme update, but otherwise good to go, thanks!
|
Indeed, the docs in flexdll are out of date w.r.t. Except that we lost Cygwin64 support in 2018 with the release of Cygwin's IIRC mingw-w64 had never clamped the base address because it hadn't been necessary, but that started to become necessary with binutils 2.36 - at which point (ocaml/ocaml#10351) co-opted the same mechanism for mingw-w64. Hwoever, msvc64 still uses the base address trick. |
|
Are those failures with msvc64 or mingw-w64, @jonahbeckford? IIRC opam-repository-mingw "fixed" the binutils thing by forcing the base address for mingw-w64 instead, which means that this will be becoming a gradually bigger problem, requiring more uses of The systhreads failure is surprising, as I thought that was only in 5.1+ (and is fixed in 5.2.1 and 5.3.0) - which version of OCaml is that one with? |
Co-authored-by: David Allsopp <[email protected]>
msvc64
4.14.2 with the nine (9) ocaml-common-4_14-a* patches at https://github.com/diskuv/dkml-compiler/tree/main/src/p and two (2) non-DkML patches (see below) using the ucrt branch of flexdll (ucrt link in this PR's main comment). ocaml-common-4_14-z02: This is relevant to systhreads. |
Thanks for the context! I feel at some point I might explore getting rid of flexdll entirely. There is a lot of complexity in flexdll for a a) use case centered around resolving circular references for plugins which I think was not a great design, and b) to remove the need for Line 65 in a003c18 Basically, I think today there is little benefit and a lot of drawbacks where 10-20 years ago the balance was a lot more positive. But OCaml is deeply tied to the circular reference model for stubs and also (maybe related) deeply tied to a flat symbol namespace ... the two assumptions from ELF linking where DLL and dylib linking have better designs ... so I'm under no illusion that untangling the assumptions would be easy. |
Fixes #29 .
Specifically adds support for:
IMAGE_REL_AMD64_ADDR32NB. (This is the "relocation kind 0003").
IMAGE_REL_I386_DIR32NB. (This is the x86 version of the above).
IMAGE_REL_AMD64_REL32_5. (This is documented but weirdly not implemented).
Tested with my ucrt branch https://github.com/jonahbeckford/flexdll/tree/0.43%2Bucrt where relocation kind 0003 occurs often. It especially occurs in "normal" C libraries (ucvrt;
/MD) that are linked into an OCaml executable. None of the tested code used/GS-.References: