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/ld: Link to a shared library fails on windows #5273

Closed
simonhege opened this issue Apr 11, 2013 · 17 comments
Closed

cmd/ld: Link to a shared library fails on windows #5273

simonhege opened this issue Apr 11, 2013 · 17 comments
Milestone

Comments

@simonhege
Copy link

What steps will reproduce the problem?

1. Compile the attached c code as a dll
2. Compile the attached go code using the dll produced during previous step

(see build.bat in the attached zip file)

What is the expected output?
Executable file

What do you see instead?
C:\Users\Simon\AppData\Local\Temp\go-build373913279\_\C_\GoEcho.a(_all.o): malformed pe
file: unexpected flags for PE section .idata$7

Which compiler are you using (5g, 6g, 8g, gccgo)?
8g

Which operating system are you using?
Windows 8 / x64

Which version are you using?  (run 'go version')
go version devel +b3017cc3e17b Wed Apr 10 21:41:54 2013 -0700 windows/386

The same steps produces the expected result when using go1.0.3

Please provide any additional information below.

>> go env
set GOARCH=386
set GOBIN=
set GOCHAR=8
set GOEXE=.exe
set GOHOSTARCH=386
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\Simon\Go
set GORACE=
set GOROOT=C:\go
set GOTOOLDIR=C:\go\pkg\tool\windows_386
set CC=gcc
set GOGCCFLAGS=-g -O2 -m32 -mthreads
set CGO_ENABLED=1

>>gcc -v
Utilisation des specs internes.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.7.2/lto-wrapper.exe
Target: mingw32
Configuré avec: ../gcc-4.7.2/configure --enable-languages=c,c++,ada,fortran,objc
,obj-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgom
p --disable-win32-registry --enable-libstdcxx-debug --disable-build-poststage1-w
ith-cxx --enable-version-specific-runtime-libs --build=mingw32 --prefix=/mingw
Modèle de thread: win32
gcc version 4.7.2 (GCC)

>> go build -x
WORK=C:\Users\Simon\AppData\Local\Temp\go-build019044383
mkdir -p $WORK\_\C_\GoEcho\_obj\
cd C:\GoEcho
C:\go\pkg\tool\windows_386\cgo.exe -objdir $WORK\_\C_\GoEcho\_obj\ -- -I. -I $WO
RK\_\C_\GoEcho\_obj\ main.go
C:\go\pkg\tool\windows_386\8c.exe -F -V -w -I $WORK\_\C_\GoEcho\_obj\ -I C:\go\p
kg\windows_386 -o $WORK\_\C_\GoEcho\_obj\_cgo_defun.8 -D GOOS_windows -D GOARCH_
386 $WORK\_\C_\GoEcho\_obj\_cgo_defun.c
gcc -I . -g -O2 -m32 -mthreads -print-libgcc-file-name
gcc -I . -g -O2 -m32 -mthreads -I. -I $WORK\_\C_\GoEcho\_obj\ -o $WORK\_\C_\GoEc
ho\_obj\_cgo_main.o -c $WORK\_\C_\GoEcho\_obj\_cgo_main.c
gcc -I . -g -O2 -m32 -mthreads -I. -I $WORK\_\C_\GoEcho\_obj\ -o $WORK\_\C_\GoEc
ho\_obj\_cgo_export.o -c $WORK\_\C_\GoEcho\_obj\_cgo_export.c
gcc -I . -g -O2 -m32 -mthreads -I. -I $WORK\_\C_\GoEcho\_obj\ -o $WORK\_\C_\GoEc
ho\_obj\main.cgo2.o -c $WORK\_\C_\GoEcho\_obj\main.cgo2.c
gcc -I . -g -O2 -m32 -mthreads -o $WORK\_\C_\GoEcho\_obj\_cgo_.o $WORK\_\C_\GoEc
ho\_obj\_cgo_main.o $WORK\_\C_\GoEcho\_obj\_cgo_export.o $WORK\_\C_\GoEcho\_obj\
main.cgo2.o libecho_dll.a
C:\go\pkg\tool\windows_386\cgo.exe -objdir $WORK\_\C_\GoEcho\_obj\ -dynimport $W
ORK\_\C_\GoEcho\_obj\_cgo_.o -dynout $WORK\_\C_\GoEcho\_obj\_cgo_import.c
C:\go\pkg\tool\windows_386\8c.exe -F -V -w -I $WORK\_\C_\GoEcho\_obj\ -I C:\go\p
kg\windows_386 -o $WORK\_\C_\GoEcho\_obj\_cgo_import.8 -D GOOS_windows -D GOARCH
_386 $WORK\_\C_\GoEcho\_obj\_cgo_import.c
gcc -I . -g -O2 -m32 -mthreads -o $WORK\_\C_\GoEcho\_obj\_all.o $WORK\_\C_\GoEch
o\_obj\_cgo_export.o $WORK\_\C_\GoEcho\_obj\main.cgo2.o libecho_dll.a -Wl,-r -no
stdlib -lmingwex -lmingw32 c:/mingw/bin/../lib/gcc/mingw32/4.7.2/libgcc.a
C:\go\pkg\tool\windows_386\8g.exe -o $WORK\_\C_\GoEcho\_obj\_go_.8 -p _/C_/GoEch
o -D _/C_/GoEcho -I $WORK $WORK\_\C_\GoEcho\_obj\_cgo_gotypes.go $WORK\_\C_\GoEc
ho\_obj\main.cgo1.go
C:\go\pkg\tool\windows_386\pack.exe grcP $WORK $WORK\_\C_\GoEcho.a $WORK\_\C_\Go
Echo\_obj\_go_.8 $WORK\_\C_\GoEcho\_obj\_cgo_import.8 $WORK\_\C_\GoEcho\_obj\_cg
o_defun.8 $WORK\_\C_\GoEcho\_obj\_all.o
cd .
C:\go\pkg\tool\windows_386\8l.exe -o GoEcho.exe -L $WORK $WORK\_\C_\GoEcho.a
# _/C_/GoEcho
C:\Users\Simon\AppData\Local\Temp\go-build019044383\_\C_\GoEcho.a(_all.o): malfo
rmed pe file: unexpected flags for PE section .idata$7


Steps used to create the dll, the c and the go executables:
cd GccEcho
gcc -c Echo.c -DECHO_EXPORTS
gcc -shared -o echo_dll.dll Echo.o -Wl,--out-implib,libecho_dll.a
gcc -c main.c
gcc -o main_dll.exe main.o -L. -lecho_dll
cd ..\GoEcho
copy ..\GccEcho\Echo.h
copy ..\GccEcho\libecho_dll.a
copy ..\GccEcho\echo_dll.dll
go build

The C test program links against the .a file and executes without problem.

I tried to reproduce the issue on linux x64 with a .so library, and the build succeeded.
It seems to be windows related.

This issue may be related to issue #5106, but it seems to me that the build goes one step
ahead in my case.

Do not hesitate if you want me to perform some other tests or give more detailed
information.

Simon

Attachments:

  1. GoIssue_src.zip (1700 bytes)
@simonhege
Copy link
Author

Comment 1:

FYI, I was able to reproduce the problem on Windows 7 32bit with the tip version.

@ianlancetaylor
Copy link
Contributor

Comment 2:

This looks the same as 5106 to me.  Why do you think it looks different.
Marking for Go 1.1 though I can't promise it will be fixed.

Labels changed: added priority-soon, go1.1, removed priority-triage.

@ianlancetaylor
Copy link
Contributor

Comment 3:

Can you update to tip and run your test case again and report the new error message? 
Thanks.

@simonhege
Copy link
Author

Comment 4:

The new error message is:
# _/C_/GoIssue_dll/GoEcho
C:\Users\SIMONH~1\AppData\Local\Temp\go-build412296543\_\C_\GoIssue_dll\GoEcho.a
(_all.o): malformed pe file: unexpected flags -0x3fd00000 for PE section .idata$
7

@ianlancetaylor
Copy link
Contributor

Comment 5:

The flags set for the section are
MEM_READ | MEM_WRITE | ALIGN_8BYTES
The Go linker is unhappy because neither INITIALIZED_DATA nor UNINITIALIZED_DATA are set.

@ianlancetaylor
Copy link
Contributor

Comment 6:

Thanks for the info.  Could you please try applying the patch at
https://golang.org/cl/8715043 and see if your test case works?  (If you have
the hg codereview extension installed you can use "hg clpatch 8715043").  Thanks.

Owner changed to @ianlancetaylor.

@simonhege
Copy link
Author

Comment 7:

Thanks for working on the issue. I applied the patch and the error message changed to:
# _/C_/GoIssue_dll/GoEcho
C:\go\pkg\tool\windows_386\8l.exe: internal fatal error.
Full log (go build -x):
WORK=C:\Users\Simon\AppData\Local\Temp\go-build843336003
mkdir -p $WORK\_\C_\GoIssue_dll\GoEcho\_obj\
cd C:\GoIssue_dll\GoEcho
C:\go\pkg\tool\windows_386\cgo.exe -objdir $WORK\_\C_\GoIssue_dll\GoEcho\_obj\ -
- -I. -I $WORK\_\C_\GoIssue_dll\GoEcho\_obj\ main.go
C:\go\pkg\tool\windows_386\8c.exe -F -V -w -I $WORK\_\C_\GoIssue_dll\GoEcho\_obj
\ -I C:\go\pkg\windows_386 -o $WORK\_\C_\GoIssue_dll\GoEcho\_obj\_cgo_defun.8 -D
 GOOS_windows -D GOARCH_386 $WORK\_\C_\GoIssue_dll\GoEcho\_obj\_cgo_defun.c
gcc -I . -g -O2 -m32 -mthreads -print-libgcc-file-name
gcc -I . -g -O2 -m32 -mthreads -I. -I $WORK\_\C_\GoIssue_dll\GoEcho\_obj\ -o $WO
RK\_\C_\GoIssue_dll\GoEcho\_obj\_cgo_main.o -c $WORK\_\C_\GoIssue_dll\GoEcho\_ob
j\_cgo_main.c
gcc -I . -g -O2 -m32 -mthreads -I. -I $WORK\_\C_\GoIssue_dll\GoEcho\_obj\ -o $WO
RK\_\C_\GoIssue_dll\GoEcho\_obj\_cgo_export.o -c $WORK\_\C_\GoIssue_dll\GoEcho\_
obj\_cgo_export.c
gcc -I . -g -O2 -m32 -mthreads -I. -I $WORK\_\C_\GoIssue_dll\GoEcho\_obj\ -o $WO
RK\_\C_\GoIssue_dll\GoEcho\_obj\main.cgo2.o -c $WORK\_\C_\GoIssue_dll\GoEcho\_ob
j\main.cgo2.c
gcc -I . -g -O2 -m32 -mthreads -o $WORK\_\C_\GoIssue_dll\GoEcho\_obj\_cgo_.o $WO
RK\_\C_\GoIssue_dll\GoEcho\_obj\_cgo_main.o $WORK\_\C_\GoIssue_dll\GoEcho\_obj\_
cgo_export.o $WORK\_\C_\GoIssue_dll\GoEcho\_obj\main.cgo2.o libecho_dll.a
C:\go\pkg\tool\windows_386\cgo.exe -objdir $WORK\_\C_\GoIssue_dll\GoEcho\_obj\ -
dynimport $WORK\_\C_\GoIssue_dll\GoEcho\_obj\_cgo_.o -dynout $WORK\_\C_\GoIssue_
dll\GoEcho\_obj\_cgo_import.c
C:\go\pkg\tool\windows_386\8c.exe -F -V -w -I $WORK\_\C_\GoIssue_dll\GoEcho\_obj
\ -I C:\go\pkg\windows_386 -o $WORK\_\C_\GoIssue_dll\GoEcho\_obj\_cgo_import.8 -
D GOOS_windows -D GOARCH_386 $WORK\_\C_\GoIssue_dll\GoEcho\_obj\_cgo_import.c
gcc -I . -g -O2 -m32 -mthreads -o $WORK\_\C_\GoIssue_dll\GoEcho\_obj\_all.o $WOR
K\_\C_\GoIssue_dll\GoEcho\_obj\_cgo_export.o $WORK\_\C_\GoIssue_dll\GoEcho\_obj\
main.cgo2.o libecho_dll.a -Wl,-r -nostdlib -lmingwex -lmingw32 c:/mingw/bin/../l
ib/gcc/mingw32/4.7.2/libgcc.a
C:\go\pkg\tool\windows_386\8g.exe -o $WORK\_\C_\GoIssue_dll\GoEcho\_obj\_go_.8 -
p _/C_/GoIssue_dll/GoEcho -D _/C_/GoIssue_dll/GoEcho -I $WORK $WORK\_\C_\GoIssue
_dll\GoEcho\_obj\_cgo_gotypes.go $WORK\_\C_\GoIssue_dll\GoEcho\_obj\main.cgo1.go
C:\go\pkg\tool\windows_386\pack.exe grcP $WORK $WORK\_\C_\GoIssue_dll\GoEcho.a $
WORK\_\C_\GoIssue_dll\GoEcho\_obj\_go_.8 $WORK\_\C_\GoIssue_dll\GoEcho\_obj\_cgo
_import.8 $WORK\_\C_\GoIssue_dll\GoEcho\_obj\_cgo_defun.8 $WORK\_\C_\GoIssue_dll
\GoEcho\_obj\_all.o
cd .
C:\go\pkg\tool\windows_386\8l.exe -o GoEcho.exe -L $WORK $WORK\_\C_\GoIssue_dll\
GoEcho.a
# _/C_/GoIssue_dll/GoEcho
C:\go\pkg\tool\windows_386\8l.exe: internal fatal error.

@simonhege
Copy link
Author

Comment 8:

I also tested against an other dll and got the given error message (with tip+patch):
malformed pe file: unexpected flags 0xe0300020 for PE section .text

@ianlancetaylor
Copy link
Contributor

Comment 9:

Thanks for testing.  I don't know what is causing that.  This is going to have to go to
somebody who knows Windows.
I have no idea what changed between 1.0.3 and tip to cause this to regress.

Owner changed to ---.

@alexbrainman
Copy link
Member

Comment 10:

This start to break at:
changeset:   13896:5334356f42b3
user:        Shenghou Ma <minux.ma@gmail.com>
date:        Fri Aug 17 03:42:34 2012 +0800
summary:     cmd/go: new cgo build procedure
Alex

@minux
Copy link
Member

minux commented Apr 15, 2013

Comment 11:

thank you for bisecting. Will take a look.
We need to add a misc/cgo/testdll for windows.

@minux
Copy link
Member

minux commented Apr 15, 2013

Comment 12:

the solution is simple:
we don't need implib (*.a) created by gcc thanks to
the new cgo build procedure.
just change the LDFLAGS line to
#cgo LDFLAGS: echo_dll.dll
and main.go will build and run fine.
@simonhege, please test this solution.
i will add an example to misc/cgo/testdll make it run
in run.bat.

Status changed to WaitingForReply.

@minux
Copy link
Member

minux commented Apr 15, 2013

Comment 13:

note: we also need https://golang.org/cl/8715043/ for
windows/amd64 (not needed for windows/386).

@alexbrainman
Copy link
Member

Comment 14:

@minux, it still fails here:
C:\tmp\zzz\1.1\issue5273>build.bat
C:\tmp\zzz\1.1\issue5273>cd GccEcho
C:\tmp\zzz\1.1\issue5273\GccEcho>gcc -c Echo.c -DECHO_EXPORTS
C:\tmp\zzz\1.1\issue5273\GccEcho>gcc -shared -o echo_dll.dll Echo.o -Wl,--out-im
plib,libecho_dll.a
Creating library file: libecho_dll.a
C:\tmp\zzz\1.1\issue5273\GccEcho>gcc -c main.c
C:\tmp\zzz\1.1\issue5273\GccEcho>gcc -o main_dll.exe main.o -L. -lecho_dll
C:\tmp\zzz\1.1\issue5273\GccEcho>cd ..\GoEcho
C:\tmp\zzz\1.1\issue5273\GoEcho>copy ..\GccEcho\Echo.h
        1 file(s) copied.
C:\tmp\zzz\1.1\issue5273\GoEcho>rem copy ..\GccEcho\libecho_dll.a
C:\tmp\zzz\1.1\issue5273\GoEcho>copy ..\GccEcho\echo_dll.dll
        1 file(s) copied.
C:\tmp\zzz\1.1\issue5273\GoEcho>go build
# _/C_/tmp/zzz/1.1/issue5273/GoEcho
C:\DOCUME~1\brainman\LOCALS~1\Temp\go-build279830815\_\C_\tmp\zzz\1.1\issue5273\
GoEcho.a(_all.o): malformed pe file: unexpected flags -0x3fd00000 for PE section
 .idata$2
C:\tmp\zzz\1.1\issue5273\GoEcho>

@alexbrainman
Copy link
Member

Comment 15:

It works, if I apply Ian's patch.
Alex

@simonhege
Copy link
Author

Comment 16:

I confirm that minux's solution + Ian's patch solve the issue.
Thank you all.
Simon

@minux
Copy link
Member

minux commented Apr 22, 2013

Comment 17:

This issue was closed by revision 8c72b81.

Status changed to Fixed.

@rsc rsc added this to the Go1.1 milestone Apr 14, 2015
@rsc rsc removed the go1.1 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
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

6 participants