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

doc/asm: clarify that get_tls only works in runtime package #33054

Open
PureWhiteWu opened this issue Jul 11, 2019 · 8 comments

Comments

Projects
None yet
5 participants
@PureWhiteWu
Copy link

commented Jul 11, 2019

What version of Go are you using (go version)?

$ go version
go version go1.12.7 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/purewhite/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/purewhite/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.12.7/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.12.7/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/gr/t7x82cps0w563940rhk76_400000gn/T/go-build992278676=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I'm studying go asm, and want to write a demo to get the *g.

According to the doc, I should be able to get *g using code like this:

#include "textflag.h"
#include "go_asm.h"

TEXT ·getg(SB),NOSPLIT,$0-8
    get_tls(CX)
    MOVQ    g(CX), AX
    MOVQ    AX, ret+0(FP)
    RET

What did you expect to see?

Successfully get current g's address.

What did you see instead?

$ go build .
# local/study/getg
./getg.s:5: unrecognized instruction "get_tls"
./getg.s:6: expected pseudo-register; found CX
./getg.s:7: expected pseudo-register; found AX
asm: assembly of ./getg.s failed
@agnivade

This comment has been minimized.

Copy link
Member

commented Jul 11, 2019

It seems to me that macro moved to a runtime header file go_tls.h. You might have to do #include "../../src/runtime/go_tls.h".

But I will defer to some experts to confirm if my diagnosis is correct @ianlancetaylor @randall77

@agnivade agnivade changed the title compile error when trying to get g according to the asm doc doc/asm: get_tls not defined in go_asm.h Jul 11, 2019

@PureWhiteWu

This comment has been minimized.

Copy link
Author

commented Jul 11, 2019

It seems to me that macro moved to a runtime header file go_tls.h. You might have to do #include "../../src/runtime/go_tls.h".

But I will defer to some experts to confirm if my diagnosis is correct @ianlancetaylor @randall77

Hi, thanks for your reply.
After doing this, this program can be compiled, but if I want to get the m of g according to the doc, the problem still exists.

Code like this:

#include "textflag.h"
#include "go_asm.h"
#include "../../src/runtime/go_tls.h"

TEXT ·getm(SB),NOSPLIT,$0-8
    get_tls(CX)
    MOVQ    g(CX), AX
    MOVQ    g_m(AX), BX
    MOVQ    BX, ret+0(FP)
    RET

outputs:

$ go build .
# local/study/getm
./getm.s:8: expected pseudo-register; found AX
asm: assembly of ./getm.s failed
@cherrymui

This comment has been minimized.

Copy link
Contributor

commented Jul 11, 2019

Only the runtime package's go_asm.h defines the offsets of the runtime data structures, like g_m. When you run go build in local/study/getg, go_asm.h will contain the offsets of the local/study/getg package, not the runtime package.

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Jul 11, 2019

The asm documentation should be clarified to explain that this code only works within the runtime package itself.

We do not want to support this outside of the runtime package.

@ianlancetaylor ianlancetaylor changed the title doc/asm: get_tls not defined in go_asm.h doc/asm: clarify that get_tls only works in runtime package Jul 11, 2019

@ianlancetaylor ianlancetaylor added this to the Go1.14 milestone Jul 11, 2019

PureWhiteWu added a commit to PureWhiteWu/go that referenced this issue Jul 12, 2019

@PureWhiteWu

This comment has been minimized.

Copy link
Author

commented Jul 12, 2019

Hi, I've submitted a pr #33069 , please take a look.
Thanks!

@gopherbot

This comment has been minimized.

Copy link

commented Jul 12, 2019

Change https://golang.org/cl/185917 mentions this issue: doc/asm: document go_asm.h only works in the runtime package

@PureWhiteWu

This comment has been minimized.

Copy link
Author

commented Jul 12, 2019

@ianlancetaylor Hi, I think this is a fix for the doc, and should be released asap. Therefore, maybe the milestone should be go1.13 instead?
Just an inquiry.

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Jul 12, 2019

The milestone doesn't mean that it has to wait for 1.14. It only means that it should be resolved before 1.14 comes out. I'm fine with getting a fix into 1.13, but I would like @robpike to take a look at the CL (if he hasn't already).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.