Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/cgo: cgo_import_dynamic doesn't work on PE dlls (Windows) #9095

Closed
steeve opened this issue Nov 12, 2014 · 3 comments
Closed

cmd/cgo: cgo_import_dynamic doesn't work on PE dlls (Windows) #9095

steeve opened this issue Nov 12, 2014 · 3 comments

Comments

@steeve
Copy link
Contributor

steeve commented Nov 12, 2014

go version go1.4beta1 linux/amd64

With some make-fu is it possible to have SWIG+CGO build a DLL out of a package. This
fixes the issue of external linking not available in Windows.

Basically, applying the following patches to SWIG does the following:
- Mark all structs as __packed__ [1]
- Bind crosscall2 and _cgo_* via DllMain (instead of extern) [2]
- Mark all wrap functions as SWIGEXPORT [3]
- Mark all the functions in the _gc.c file as dynimport name name "mylib.dll"

Once the file is generated/compiled, to get a .dll, I do the following:
- unpack the library
- repack the library minus the _wrap object file
- cc -shared the _wrap object file into a .dll (with linked needed libraries)

Everything does compile after that with no issues.

The issue is that cgo_import_dynamic doesn't properly resolve the symbol in the DLL, but
in the IAT section of the .exe file. This causes the executing program to crash.

I traced the program with IDA and confirmed that. See the thread of go-nuts with lots of
additional informations and IDA outputs [4].

I have no idea why C code using this pragma works fine, but Go code using the variable
generated to the symbol doesn't. Maybe a bug in the PE linker?

Thank you

[1] steeve/swig@e70fce1
[2] steeve/swig@27bb9bd
[3] steeve/swig@2c55397
[4] https://groups.google.com/forum/#!topic/golang-nuts/-M-8X-PLvOw
@ianlancetaylor
Copy link
Contributor

Comment 1:

Labels changed: added repo-main, release-none, os-windows.

@alexbrainman
Copy link
Member

Comment 2:

>> ... The issue is that cgo_import_dynamic doesn't properly resolve the symbol in the
DLL, but in the IAT section of the .exe file. This causes the executing program to crash.
I am not clear about what you are trying to do. Please, provide a small program to
demonstrate. Something I can try here. Thank you.
Alex

@minux
Copy link
Member

minux commented Nov 26, 2014

Comment 3:

I don't think that could work around the issue of not having external linking support.
If you are that familiar with internals of PE/COFF, I'd suggest you tackle the external
linking support, rather than make very complicated workaround that is not guaranteed
to work with future versions of Go.

@bradfitz bradfitz removed the new label Dec 18, 2014
@rsc rsc removed the os-windows label Apr 10, 2015
@rsc rsc added this to the Unplanned milestone Apr 10, 2015
@steeve steeve closed this as completed Jan 25, 2019
@golang golang locked and limited conversation to collaborators Jan 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants