Opened 7 years ago

Closed 17 months ago

#2283 closed bug (fixed)

WIndows: loading objects that refer to DLL symbols

Reported by: simonmar Owned by:
Priority: highest Milestone: 7.8.1
Component: GHCi Version: 7.8.1-rc2
Keywords: Cc: hvr
Operating System: Windows Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: #7097 #7568 Differential Revisions:

Description

This is a test case from #1288, distilled into a separate report so I can close #1288:

test3b.c:

#include <stdio.h>

__declspec(dllexport) void _stdcall test(int arg)
{
   printf("The argument passed was %i\n", arg );
}

test_proxy_5a.c:

__declspec(dllimport) void _stdcall test(int arg);

void test_proxy(int arg)
{
   test(arg);
}

To reproduce:

gcc -c test3b.c
ar -rv test3b.a test3b.o 
c:/mingw/bin/dllwrap --export-all-symbols --output-lib test3b.dll.a -o test3b.dll test3b.a
gcc -c test_proxy_5a.c
ghci -ltest3b test_proxy_5a.o

the error message we get:

GHCi, version 6.9.20080512: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
Loading object (static) test_proxy_5a.o ... done
Loading object (dynamic) test3b ... done
ghc.exe:
test_proxy_5a.o: unknown symbol `__imp__test'
final link ... ghc.exe: linking extra libraries/objects failed

I'm not sure to what extent we need to support this, it's possible to link to the DLL by omitting the dllimport declaration.

Attachments (5)

ghc-7.6.3-wfiximp-after-w64fix.patch (607 bytes) - added by awson 19 months ago.
ghc-7.6.3-wfiximp-after-w64fix2.patch (654 bytes) - added by awson 19 months ago.
ghc-imp_fix.patch (1.2 KB) - added by awson 19 months ago.
indirect_link_windows.patch (2.0 KB) - added by awson 17 months ago.
indirect_link_windows.2.patch (2.0 KB) - added by awson 17 months ago.

Download all attachments as: .zip

Change History (28)

comment:1 Changed 6 years ago by batterseapower

Surely ghci can't be expected to handle this? On Windows, the user must also link against the DLL import lib (test3b.dll.a) if they want test_proxy_5a to work properly, whether they are building an EXE or running GHCi.

comment:2 Changed 3 years ago by igloo

  • Blocked By 3658 added

comment:3 Changed 3 years ago by morabbin

  • Type of failure set to None/Unknown

Behavior still present in 7.6.1 on Windows

comment:4 Changed 3 years ago by simonmar

#7097 and #7568 are more instances of this.

comment:5 Changed 3 years ago by simonmar

  • Milestone changed from _|_ to 7.8.1
  • Priority changed from normal to high

comment:6 Changed 19 months ago by awson

  • Cc hvr added

This patch solves the problem. The patch is made against 7.6.3 *with* the patch https://ghc.haskell.org/trac/ghc/attachment/ticket/7134/ghc-7.6.3-w64fix.patch applied.

These two patches should apply to HEAD cleanly in order.

This patch is *especially* important for 64-bit build because it is distributed with mingw-w64 gcc compiler, having __declspec(dllimport) as part of windows api specs in header files. Hence even C/C++ sources built via GHC driver refer to win32 API functions, decorated with __imp_ prefix.

Last edited 19 months ago by awson (previous) (diff)

comment:7 Changed 19 months ago by awson

  • Status changed from new to patch

Changed 19 months ago by awson

Changed 19 months ago by awson

comment:9 Changed 19 months ago by awson

Previous patches were buggy and worked on 7.6.3 by accident. This new patch is more or less independent and works.

comment:10 Changed 19 months ago by simonmar

Please add a reference to the ticket in the comment, and if you could add a sentence or two describing the problem (or a pointer to somewhere else that describes it) that would be really helpful.

comment:11 Changed 19 months ago by awson

Not quite understood. a reference to the ticket in the comment... do you mean a source code comment? a sentence or two describing the problem... this should go to source code too? or to a comment to this ticket?

comment:12 Changed 19 months ago by simonmar

Yes, a source code comment in both cases please.

comment:13 Changed 19 months ago by awson

I've updated the patch (added a reference to the ticket and updated the comment).

See also my explanation here: https://ghc.haskell.org/trac/ghc/ticket/7097#comment:19. Btw, with this patch we don't need to fix mingw-w64 (or any other) headers anymore. And also this fixes #7097 too.

Last edited 19 months ago by awson (previous) (diff)

Changed 19 months ago by awson

comment:14 Changed 19 months ago by awson

I've updated the patch: __imp_ could be perfectly legal part of identifier and we can link potentially wrong function here. So we issue corresponding warning if this occurs.

comment:15 Changed 18 months ago by Austin Seipp <austin@…>

In 25821cc9793c15e9860b3dcddf50aebf0eb718f7/ghc:

Win64 linker: fix loading foreign imports (#2283)

Signed-off-by: Austin Seipp <[email protected]>

comment:16 Changed 18 months ago by thoughtpolice

  • Blocked By 3658 removed
  • Resolution set to fixed
  • Status changed from patch to closed

Changed 17 months ago by awson

comment:17 Changed 17 months ago by awson

  • Architecture changed from x86 to Unknown/Multiple
  • Priority changed from high to highest
  • Resolution fixed deleted
  • Status changed from closed to new

That fix above was insufficient. It resolved linking of decorated function, but any attempt of some foreign code to call it lead to segfault because such foreign code want to call it *indirectly*, while we give it a direct address. This patch fixes things finally.

comment:18 Changed 17 months ago by awson

  • Status changed from new to patch

comment:19 Changed 17 months ago by awson

This patch is not a replacement for old patch. It should be applied to the current HEAD and 7.8.

Changed 17 months ago by awson

comment:20 Changed 17 months ago by awson

I've slightly improved the patch.

comment:21 Changed 17 months ago by thoughtpolice

  • Version changed from 6.8.2 to 7.8.1-rc2

comment:22 Changed 17 months ago by Austin Seipp <austin@…>

In 7a1c85113dd082153cc07f4792216beaf34daeeb/ghc:

linker: Fix indirect calls for x86_64 windows (#2283)

Signed-off-by: Austin Seipp <[email protected]>

comment:23 Changed 17 months ago by thoughtpolice

  • Resolution set to fixed
  • Status changed from patch to closed

Merged in HEAD and 7.8

Note: See TracTickets for help on using tickets.