ocaml / flexdll

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

Support for /alternatename: linker directive needed for x86 #113

Closed jonahbeckford closed 1 year ago

jonahbeckford commented 1 year ago

I can see the problem clearly in 32-bit code, although it doesn't look like it is limited to 32-bit code.

Here is the invocation of flexlink 0.42 (edited to split newlines):

flexlink.exe ^
-o src\ActorSystem\system.exe -U /out:src\ActorSystem\system.exe -implib ^
-L C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\lib\x86 ^
-L C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\lib\um\x86 ^
-L C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\ucrt\x86 ^
-L C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x86 ^
-L Z:\source\DkHelloWorldActor\build_community\DkSDKFiles\o\s\o\lib\ocaml ^
src\ActorSystem\DkHelloWorldActor_system.lib advapi32.lib ws2_32.lib version.lib -exe ^
-no-merge-manifest -custom-crt msvcrt.lib -chain msvc -stack 16777216 ^
-L Z:\source\DkHelloWorldActor\build_community\DkSDKFiles\o\s\o\lib\ocaml src\ActorSystem\DkHelloWorldActor_system.lib ^
advapi32.lib ws2_32.lib version.lib libasmrund.lib src\ActorSystem\CMakeFiles\system.dir\_main.c.obj ^
-- ^
/nologo /pdb:src\ActorSystem\system.pdb ^
/version:0.0 /ENTRY:wmainCRTStartup /machine:X86 /debug /INCREMENTAL /subsystem:console ^
/MANIFEST /MANIFESTFILE:src\ActorSystem\CMakeFiles\system.dir/intermediate.manifest ^
src\ActorSystem\CMakeFiles\system.dir/manifest.res

which gives:

failed (exit code 2) with the following output:
** Cannot resolve symbols for C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\lib\x86\msvcrt.lib(d:\agent\_work\2\s\Intermediate\vctools\msvcrt.nativeproj_110336922\objr\x86\chandler4gs.obj):
 __filter_x86_sse2_floating_point_exception

The root cause seems to be the Linker Directives:

dumpbin /all 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\lib\x86\msvcrt.lib' | grep -B100 -10 -E 'Archive member name|filter_x86_sse2'

...
   4950F4 __filter_x86_sse2_floating_point_exception_default
...
      4D6 __filter_x86_sse2_floating_point_exception_default
...
Archive member name at 65B67A: /124277         d:\agent\_work\2\s\Intermediate\vctools\msvcrt.nativeproj_110336922\objr\x86\chandler4gs.obj
5EA42E04 time/date Sat Apr 25 05:33:08 2020
         uid
         gid
  100666 mode
    54F8 size
correct header end

FILE HEADER VALUES
             14C machine (x86)
               7 number of sections
        5EA42E04 time date stamp Sat Apr 25 05:33:08 2020
            52C5 file pointer to symbol table
              16 number of symbols
               0 size of optional header
               0 characteristics

SECTION HEADER #1
.drectve name
       0 physical address
       0 virtual address
      70 size of raw data
     12C file pointer to raw data (0000012C to 0000019B)
       0 file pointer to relocation table
       0 file pointer to line numbers
       0 number of relocations
       0 number of line numbers
  100A00 flags
         Info
         Remove
         1 byte align

RAW DATA #1
  00000000: 20 20 20 2F 61 6C 74 65 72 6E 61 74 65 6E 61 6D     /alternatenam
  00000010: 65 3A 5F 5F 66 69 6C 74 65 72 5F 78 38 36 5F 73  e:__filter_x86_s
  00000020: 73 65 32 5F 66 6C 6F 61 74 69 6E 67 5F 70 6F 69  se2_floating_poi
  00000030: 6E 74 5F 65 78 63 65 70 74 69 6F 6E 3D 5F 5F 66  nt_exception=__f
  00000040: 69 6C 74 65 72 5F 78 38 36 5F 73 73 65 32 5F 66  ilter_x86_sse2_f
  00000050: 6C 6F 61 74 69 6E 67 5F 70 6F 69 6E 74 5F 65 78  loating_point_ex
  00000060: 63 65 70 74 69 6F 6E 5F 64 65 66 61 75 6C 74 20  ception_default

   Linker Directives
   -----------------
   /alternatename:__filter_x86_sse2_floating_point_exception=__filter_x86_sse2_floating_point_exception_default

...
COFF SYMBOL TABLE
...
00D 00000000 UNDEF  notype ()    External     | __filter_x86_sse2_floating_point_exception
jonahbeckford commented 1 year ago

I can pull out the data with the existing COFF reader:

#mod_use "Compat.ml";;
#mod_use "coff.ml";;

let msvcrt_path = {|C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\lib\x86\msvcrt.lib|} ;;

let msvcrt_objects, msvcrt_imports = match Coff.Lib.read msvcrt_path with
  `Lib lib -> lib | _ -> failwith "Not a `Lib" ;;

let chandler4gs_obj = snd @@ List.find (
  function (objpath, coff) ->
    String.equal objpath {|d:\agent\_work\2\s\Intermediate\vctools\msvcrt.nativeproj_110336922\objr\x86\chandler4gs.obj|})
  msvcrt_objects ;;

Coff.Coff.directives chandler4gs_obj ;;
(*
- : (string * string list) list =
[("alternatename",  ["__filter_x86_sse2_floating_point_exception=__filter_x86_sse2_floating_point_exception_default"])]   
*)

I can may be able to do a PR if I can figure out what to do with that alternatename directive. It sounds similar to an alias, so I'll start there.

dra27 commented 1 year ago

Fixed in #114