Opened 9 years ago

Closed 4 years 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 Rev(s):
Wiki Page:

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 4 years ago.
ghc-7.6.3-wfiximp-after-w64fix2.patch (654 bytes) - added by awson 4 years ago.
ghc-imp_fix.patch (1.2 KB) - added by awson 4 years ago.
indirect_link_windows.patch (2.0 KB) - added by awson 4 years ago.
indirect_link_windows.2.patch (2.0 KB) - added by awson 4 years ago.

Download all attachments as: .zip

Change History (28)

comment:1 Changed 8 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 5 years ago by igloo

Blocked By: 3658 added

comment:3 Changed 5 years ago by morabbin

Type of failure: None/Unknown

Behavior still present in 7.6.1 on Windows

comment:4 Changed 5 years ago by simonmar

#7097 and #7568 are more instances of this.

comment:5 Changed 5 years ago by simonmar

Milestone: _|_7.8.1
Priority: normalhigh

comment:6 Changed 4 years 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 4 years ago by awson (previous) (diff)

comment:7 Changed 4 years ago by awson

Status: newpatch

Changed 4 years ago by awson

Changed 4 years ago by awson

comment:9 Changed 4 years 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 4 years 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 4 years 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 4 years ago by simonmar

Yes, a source code comment in both cases please.

comment:13 Changed 4 years 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 4 years ago by awson (previous) (diff)

Changed 4 years ago by awson

Attachment: ghc-imp_fix.patch added

comment:14 Changed 4 years 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 4 years ago by Austin Seipp <austin@…>

In 25821cc9793c15e9860b3dcddf50aebf0eb718f7/ghc:

Win64 linker: fix loading foreign imports (#2283)

Signed-off-by: Austin Seipp <austin@well-typed.com>

comment:16 Changed 4 years ago by thoughtpolice

Blocked By: 3658 removed
Resolution: fixed
Status: patchclosed

Changed 4 years ago by awson

Attachment: indirect_link_windows.patch added

comment:17 Changed 4 years ago by awson

Architecture: x86Unknown/Multiple
Priority: highhighest
Resolution: fixed
Status: closednew

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 4 years ago by awson

Status: newpatch

comment:19 Changed 4 years ago by awson

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

Changed 4 years ago by awson

comment:20 Changed 4 years ago by awson

I've slightly improved the patch.

comment:21 Changed 4 years ago by thoughtpolice

Version: 6.8.27.8.1-rc2

comment:22 Changed 4 years ago by Austin Seipp <austin@…>

In 7a1c85113dd082153cc07f4792216beaf34daeeb/ghc:

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

Signed-off-by: Austin Seipp <austin@well-typed.com>

comment:23 Changed 4 years ago by thoughtpolice

Resolution: fixed
Status: patchclosed

Merged in HEAD and 7.8

Note: See TracTickets for help on using tickets.