golang/go

cmd/6l: CGO program will only link with MinGW 4.8.1 on Windows

slimsag opened this issue · 29 comments

I have tried the following Go versions:
* Tip
* Official Go 1.3.1 binaries.

What steps reproduce the problem?

1. Extract attached zip file (5 files, 47 lines in total) to $GOPATH/src/
2. cd $GOPATH/src/mingwbug
3. go build

What happened?

C:\Users\stephen\Desktop\godev\src\mingwbug>go build -a main.go
# command-line-arguments
mingwbug/glfw(.text): mingwbug/glfw(/76): not defined
mingwbug/glfw(.text): undefined: mingwbug/glfw(/76)

What should have happened instead?

The program should link.

Please provide any additional information below.

The program compiles, links, and runs fine when using MinGW 4.8.1.

If using MinGW 4.8.3 OR 4.9.1 (MinGW-W64 project or TDM-GCC) then the linker reports the
errors above.

This issue is a minimal example if the problem we ran into at
go-gl/glfw#91 if you want more information.

Comment 1:

(attachment)

Attachments:

  1. mingwbug.zip (1498 bytes)

Comment 2:

Please try running "go build -x" and report the results here so that we can see just
which command failed.  Thanks.

Labels changed: added repo-main, release-go1.4maybe, os-windows.

Comment 3:

The output is rather large? View it in full at http://pastebin.com/1niy3W2U
C:\Users\stephen\Desktop\godev\src\mingwbug>go build -x
...trimmed by me...
cd C:\Users\stephen\Desktop\godev\src\mingwbug
"C:\\tipgoamd64\\pkg\\tool\\windows_amd64\\6g.exe" -o
"C:\\Users\\stephen\\AppData\\Local\\Temp\\go-build954290891\\mingwbug.a" -t
rimpath "C:\\Users\\stephen\\AppData\\Local\\Temp\\go-build954290891" -p mingwbug
-complete -D _/C_/Users/stephen/Desktop/godev/sr
c/mingwbug -I "C:\\Users\\stephen\\AppData\\Local\\Temp\\go-build954290891" -I
"C:\\Users\\stephen\\Desktop\\godev\\pkg\\windows_a
md64" -pack "C:\\Users\\stephen\\Desktop\\godev\\src\\mingwbug\\main.go"
cd .
"C:\\tipgoamd64\\pkg\\tool\\windows_amd64\\6l.exe" -o
"C:\\Users\\stephen\\AppData\\Local\\Temp\\go-build954290891\\mingwbug\\_obj
\\exe\\a.out.exe" -L "C:\\Users\\stephen\\AppData\\Local\\Temp\\go-build954290891" -L
"C:\\Users\\stephen\\Desktop\\godev\\pkg\\wi
ndows_amd64" -extld=gcc
"C:\\Users\\stephen\\AppData\\Local\\Temp\\go-build954290891\\mingwbug.a"
# mingwbug
mingwbug/glfw(.text): mingwbug/glfw(/76): not defined
mingwbug/glfw(.text): undefined: mingwbug/glfw(/76)

Comment 4:

Sorry, I have no idea what is going on.  This is failing when running the Go linker on
Go code, so I can't see why changing the GCC version would make any difference.  I also
don't know what the "(/76)" is doing there.  This will have to be debugged by somebody
running Windows.
rsc commented

Comment 5:

Owner changed to @rsc.

Status changed to Accepted.

rsc commented

Comment 6:

The form mingwbug/glfw(name) refers to the section name inside an object in
mingwbug/glfw. The name /76 is a PE-encoded string table reference, since they only
reserved 8 letters for the section name. 
$GOROOT/src/cmd/ld/ldpe.c needs to look for the /%d form of section name and do the
appropriate translation to the correct section name.

Labels changed: added release-go1.4, removed release-go1.4maybe.

Owner changed to ---.

Comment 7:

There are 2 problems here as far as I can see (in ldpe.c):
1) section names are not read properly - that is why we see /76 in mingwbug/glfw(/76),
/76 is a reference into "string table" where real name lives;
2) ldpe.c code assumes (in a few places) that all symbols with name starting with "."
are section names - this is broken now that new gcc starts some symbols with ".".
I am not familiar with that code enough to decide how to change it safely yet. But I
will keep looking.
Alex

Comment 8:

I had trouble finding latest gcc. http://nuwen.net/mingw.html is what I end-up using.
Alex

Comment 9:

@Alex: http://mingw-w64.sourceforge.net/ - on Windows the easiest way is to use the
mingw-builds distribution.

Comment 10 by rich.amacker:

@Alex
Here's a 32-bit version:
http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/mingw-builds/4.9.1/
Here's a 64-bit version:
http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/4.9.1/
If you go up to the parent folder, you'll find 4.8.1, 4.8.2, etc. as well.

Comment 11:

I'll fix the issue.

Comment 12:

Sure, go ahead. I also have started on this issue. One of us will succeed. :-)
Alex

Comment 13:

Oh sorry Alex, I've thought no one is on it. You can fix it if you want?
Jens

Comment 14:

No, please continue with your fix. I am not certain my solution is correct. Two heads
are better then one. Whoever is ready first will discuss during review.
Alex

Comment 15:

ok

Comment 16:

Interesting, there are more .rdata symbols on the 4.9.1 object file by the equivalent c
code.
e:\tmp>diff -uprN _cgo_export.o_good_481.sym _cgo_export.o_wrong_491.sym
--- _cgo_export.o_good_481.sym      Wed Oct 08 16:48:13 2014
+++ _cgo_export.o_wrong_491.sym     Wed Oct 08 16:47:11 2014
@@ -6,7 +6,9 @@
 0000000000000000 N .debug_info
 0000000000000000 N .debug_line
 0000000000000000 p .pdata
+0000000000000000 r .rdata$.refptr._cgoexp_9f48158d77e0_something
 0000000000000000 r .rdata$zzz
+0000000000000000 R .refptr._cgoexp_9f48158d77e0_something
 0000000000000000 t .text
 0000000000000000 r .xdata
                  U _cgoexp_9f48158d77e0_something
Jens

Comment 17:

e:\tmp>diff -uprN two_good_481.sym two_wrong_491.sym
--- two_good_481.sym    Wed Oct 08 17:49:32 2014
+++ two_wrong_491.sym   Wed Oct 08 17:49:50 2014
@@ -7,7 +7,9 @@
 0000000000000000 N .debug_line
 0000000000000000 N .debug_str
 0000000000000000 p .pdata
+0000000000000000 r .rdata$.refptr._glfwInitialized
 0000000000000000 r .rdata$zzz
+0000000000000000 R .refptr._glfwInitialized
 0000000000000000 t .text
 0000000000000000 r .xdata
                  U _glfwInitialized
If I replace two.o and _cgo_export.o with the 4.8.1 versions and build all manually then
it will work. Now the question is: are the additional gcc .rdata sections wrong or has
the 6l.exe a bug and should handle it right?
Jens

Comment 18:

I think https://golang.org/issue/8856 is related to.

Comment 19:

Yes 8856 is the same issue. I think.
Alex

Comment 20:

I think https://golang.org/issue/8856 is related to this too.
Jens

Comment 21:

That's the minimal setup to reproduce the issue.
Jens

Attachments:

  1. mingwbug5.zip (1055 bytes)

Comment 22:

When there are at least two .refptr. sections in _all.o which depends on each other it
fails.
Jens

Comment 23:

CL https://golang.org/cl/154210044 mentions this issue.

Comment 24:

Please, review https://golang.org/cl/152410043 to see if that fixes your
problem. Thank you.
Alex

Status changed to Started.

Comment 25:

CL https://golang.org/cl/152410043 mentions this issue.

Comment 26:

Alex, you've been faster. The CL LGTM and passes my tests.
Jens

Comment 27:

Thanks for review. I will wait for others.
Alex

Comment 28:

This issue was updated by revision d0ee959.

LGTM=iant, jfrederich
R=golang-codereviews, iant, jfrederich
CC=golang-codereviews
https://golang.org/cl/154210044

Comment 29:

This issue was closed by revision d704bb0.

Status changed to Fixed.