ocaml / flexdll

a dlopen-like API for Windows
Other
100 stars 30 forks source link

Would a pull request allowing "__nm_" prefixes be accepted? #77

Closed ttamttam closed 4 years ago

ttamttam commented 5 years ago

Working with https://github.com/ocaml-cross/opam-cross-windows which relies on flexdll, I hit a strange problem: for some C++ DLL I'm calling from OCaml, some symbols where prefixed with "__nm_". I do not know why?

I solved this by patching flexdll (see https://github.com/ocaml-cross/opam-cross-windows/pull/108). I submitted this patch to https://github.com/ocaml-cross/opam-cross-windows, but if the solution to my problem is sane, it would be better to include it into flexdll.

This patch modifies aliases function (https://github.com/alainfrisch/flexdll/blob/master/coff.ml#L757-L766) for it to store both the whole symbol and the symbol without the "__nm_" prefix in the alias hash-table:

let aliases x =
    let a = ref [] in
    List.iter
      (fun s ->
         (match s.extra_info with
           | `Alias s' -> a := (s.sym_name,s'.sym_name) :: !a
           | _ -> ());
         if starts_with ~what:"__nm_" s.sym_name then
           a := (String.sub s.sym_name 5 (String.length s.sym_name - 5),
                s.sym_name) :: !a
      )
      x.symbols;
    !a

It works, but re-reading it, I think it should have been something like the following instead:

  let aliases x =
    let a = ref [] in
    List.iter
      (fun s ->
        match s.extra_info with
        | `Alias s' ->
            a := (s.sym_name, s'.sym_name) :: !a;
            if starts_with ~what:"__nm_" s.sym_name then
              a :=
                ( String.sub s.sym_name 5 (String.length s.sym_name - 5),
                  s'.sym_name )
                :: !a
        | _ -> () )
      x.symbols;
    !a

Anyway: after I would correct and test this, would such a PR be considered to be accepted in flexdll, or does it solve my problem the wrong way?

dra27 commented 5 years ago

This is https://github.com/alainfrisch/flexdll/pull/3, I think?

This certainly should be fixed - the problem with all these linker things (I find) is finding concrete documentation (or at least a clear pointer into binutils, or whatever else is doing it this way!). Last time I had a go at this, I gave up trying to find any - do you have any pointers to docs?

(fundamentally, yes, the fix belongs here in flexdll!)

nojb commented 5 years ago

http://www.cygwin.com/ml/cygwin/2002-01/msg00236.html may be of (some) help.

dra27 commented 4 years ago

The full gory details are also documented in binutils - https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=ld/pe-dll.c;h=0addde231863a50e7111e2ce23cd00699dcfb98c;hb=HEAD

dra27 commented 4 years ago

3 is now merged, if you're able to retry your use-case with flexdll master? I'm hoping we'll release 0.38 in time for 4.10.1/4.11.0, but feel free to re-open this issue if it's still not working for you.