-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: misgenerated offsets when passing C-side arguments to winapi functions on windows/386 #8092
Labels
Milestone
Comments
Oh I think I get it: the main.main() is a red herring: the problem is /something/ is taking text and using it to mean "the start of the text segment". package main // #include <stdio.h> // char *data = "hello, ", *text = "world\n"; // void c(void) { printf("0x%p 0x%p\n", text, data); } import "C" func main() { println(C.text, C.data); C.c() } pietro@pietro-laptop:/tmp/datatext$ GOOS=windows GOARCH=386 CGO_ENABLED=1 go run x.go fixme:process:SetProcessPriorityBoost (0xffffffff,1): stub 0x140d8b64 0x1 0x0044089B 0x004408A2 pietro@pietro-laptop:/tmp/datatext$ GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go run x.go 0xfffff0250c8b4864 0xffffffff00000001 0x0x42680f 0x0x426816 It looks like cgo is doing it. I don't know if it can be changed... or if it should be. (The fixme: line is from wine and is a separate issue.) |
Yes, I believe we have symbols named "data" and "text" that point to correspondent pe sections during linking. I don't know, if these interfere with your C variables. I don't know what cgo does with your variables - it must rename them to something. You could use -x and -work flags when you build executable, then you will be able to have the exact source that gets compiled. I would look at Go linker (cmd/ld/ldpe.c) to see what happens to all those symbols. Perhaps some can be renamed so we don't get clashes. Alex |
This is not windows specific problem. I can see the same issue on linux-386: # cat main.go package main /* #include <stdio.h> char *data = "data", *text = "text", *alex = "alex"; void c(void) { printf("text=%s data=%s alex=%s\n", text, data, alex); printf("text=%p data=%p alex=%p\n", text, data, alex); } */ import "C" import "fmt" func main() { C.c() fmt.Printf("text=%v data=%v alex=%v\n", C.text, C.data, C.alex) } # go run main.go text=text data=data alex=alex text=0x80c121f data=0x80c121a alex=0x80c1224 text=0xd8b65 data=0x29690001 alex=0x80c1224 Alex Labels changed: removed os-windows. |
Looks like a collision between the symbols provided by cmd/ld/data.c:^address for use by the "runtime" package and external C symbols we're trying to use. The quick fix for the immediate issue would be to rename those symbols to "runtime.text", "runtime.data", etc. and update the "runtime" package to use the new names. (Might be helpful in converting runtime C code to Go too?) Beyond that, there's only a handful of global symbols in package runtime that are unnamespaced (dropg, getprofile, and mheap_alloc_m), and it looks like they could all be easily marked "static". |
I've prepared https://golang.org/cl/126790043/ that moves all linker defined symbols (e.g., text, etext, data, ...) into package runtime (i.e., runtime.text, runtime.etext, runtime.data, ...) to avoid conflicts with cgo symbols. It also marks the couple of C runtime functions mentioned as static, and it fixes the test case from comment #5. However, I'm running into an issue that regenerating the cmd/link/testdata files on Linux causes the cmd/link tests to fail (even without my patch). I want to investigate what's going on there before mailing out the above CL. Lastly, although it's a pretty straight forward change, it's still rather large. So I wonder if it's worth breaking into steps. E.g., first just change ld to emit both "text" and "runtime.text", then switch package runtime to use the "runtime.*" symbols, then stop defining the old symbols. |
CL https://golang.org/cl/122250043 mentions this issue. |
Thanks, I'll bring it up on golang-dev then. And in case anyone's interested, I'm tracking the cmd/link/testdata issue in issue #8494. |
This issue was updated by revision 28cf62e. LGTM=dvyukov R=golang-codereviews, minux, dvyukov CC=golang-codereviews https://golang.org/cl/122250043 Committer: Dmitriy Vyukov |
CL https://golang.org/cl/126790043 mentions this issue. |
This issue was closed by revision 2c110a1. Status changed to Fixed. |
CL https://golang.org/cl/138770043 mentions this issue. |
This issue was updated by revision 43d4f93. LGTM=iant, alex.brainman R=rsc, iant, alex.brainman CC=golang-codereviews https://golang.org/cl/138770043 Committer: Ian Lance Taylor |
wheatman
pushed a commit
to wheatman/go-akaros
that referenced
this issue
Jun 25, 2018
…ols into runtime package Fixes golang#8092. LGTM=rsc R=iant, rsc CC=golang-codereviews https://golang.org/cl/126790043
wheatman
pushed a commit
to wheatman/go-akaros
that referenced
this issue
Jul 9, 2018
…ols into runtime package Fixes golang#8092. LGTM=rsc R=iant, rsc CC=golang-codereviews https://golang.org/cl/126790043
This issue was closed.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
The text was updated successfully, but these errors were encountered: