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/gc: address &x.f is not resolved at link time #9355

Closed
randall77 opened this issue Dec 16, 2014 · 5 comments
Closed

cmd/gc: address &x.f is not resolved at link time #9355

randall77 opened this issue Dec 16, 2014 · 5 comments
Milestone

Comments

@randall77
Copy link
Contributor

If you do
var y=&x

Then global y points to global x, and that reference is computed at link time.

If you do (where f is a field of x)
var y = &x.f

This reference is not computed at link time. Instead, it is set up dynamically by init(). This means that other code during init can see y as nil.

See #9354 for why this matters.

@ianlancetaylor
Copy link
Member

I don't understand how other code can see y as nil. That sounds like a bug in the init dependency ordering.

@minux
Copy link
Member

minux commented Dec 16, 2014

The problem only happens in runtime where runtime.init is delayed.

@randall77 randall77 self-assigned this Dec 16, 2014
@randall77 randall77 added this to the Go1.5 milestone Dec 16, 2014
@randall77
Copy link
Contributor Author

In any case, it is worth fixing regardless. No need to compute this address at runtime when it is easily done at link time.

@minux
Copy link
Member

minux commented Dec 16, 2014

Sure. https://gist.github.com/minux/69994348b4f06b1c324b
fixes the underlying problem for 6g.

If you like the approach, I can finish target-specific code for
other ports, and mail it out.

We also need a test, which is harder (other than perhaps verifying
the output of gc)

@randall77
Copy link
Contributor Author

I have a competing CL to fix this bug. It's a bit more general, and it has a shiny test.
https://go-review.googlesource.com/1691

OlingCat referenced this issue in Go-zh/go.old Dec 19, 2014
When we do y = &x for global variables x and y, y gets initialized
at link time.  Do the same for y = &x.f if x is a struct and y=&x[5]
if x is an array.

fixes #9217
fixes #9355

Change-Id: Iea3c0ce2ce1b309e2b760e345608fd95460b5713
Reviewed-on: https://go-review.googlesource.com/1691
Reviewed-by: Minux Ma <[email protected]>
@golang golang locked and limited conversation to collaborators Jun 25, 2016
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

4 participants