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

Created on 11 Jul 2019  路  8Comments  路  Source: golang/go

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
Documentation FrozenDueToAge NeedsFix help wanted

Most helpful comment

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.

All 8 comments

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

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

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.

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.

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

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

@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.

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).

Was this page helpful?
0 / 5 - 0 ratings