diff --git a/go/tools/builders/asm.go b/go/tools/builders/asm.go index 133e950fd8..3d64c9ba37 100644 --- a/go/tools/builders/asm.go +++ b/go/tools/builders/asm.go @@ -15,6 +15,7 @@ package main import ( + "go/build" "io/ioutil" "os" "path/filepath" @@ -24,6 +25,12 @@ import ( "strings" ) +var ASM_DEFINES = []string{ + "-D", "GOOS_" + build.Default.GOOS, + "-D", "GOARCH_" + build.Default.GOARCH, + "-D", "GOOS_GOARCH_" + build.Default.GOOS + "_" + build.Default.GOARCH, +} + // buildSymabisFile generates a file from assembly files that is consumed // by the compiler. This is only needed in go1.12+ when there is at least one // .s file. If the symabis file is not needed, no file will be generated, @@ -87,8 +94,7 @@ func buildSymabisFile(goenv *env, sFiles, hFiles []fileInfo, asmhdr string) (str seenHdrDirs[hdrDir] = true } } - // TODO(#1894): define GOOS_goos, GOARCH_goarch, both here and in the - // GoAsm action. + asmargs = append(asmargs, ASM_DEFINES...) asmargs = append(asmargs, "-gensymabis", "-o", symabisName, "--") for _, sFile := range sFiles { asmargs = append(asmargs, sFile.filename) @@ -107,6 +113,7 @@ func asmFile(goenv *env, srcPath, packagePath string, asmFlags []string, outPath if packagePath != "" && isGo119OrHigher() { args = append(args, "-p", packagePath) } + args = append(args, ASM_DEFINES...) args = append(args, "-trimpath", ".") args = append(args, "-o", outPath) args = append(args, "--", srcPath) diff --git a/tests/core/cross/BUILD.bazel b/tests/core/cross/BUILD.bazel index 04a7ba1338..ec1799847c 100644 --- a/tests/core/cross/BUILD.bazel +++ b/tests/core/cross/BUILD.bazel @@ -33,6 +33,14 @@ go_binary( deps = [":platform_lib"], ) +go_binary( + name = "asm_cross", + srcs = ["asm.s", "main.go"], + goarch = "386", + goos = "linux", + deps = [":platform_lib"], +) + go_binary( name = "native_bin", srcs = ["main.go"], diff --git a/tests/core/cross/asm.s b/tests/core/cross/asm.s new file mode 100644 index 0000000000..7a5d521933 --- /dev/null +++ b/tests/core/cross/asm.s @@ -0,0 +1,15 @@ +// Example assembly copied from https://github.com/rpccloud/goid + +#include "go_asm.h" +#include "textflag.h" + +#ifdef GOARCH_386 +#define get_tls(r) MOVL TLS, r +#define g(r) 0(r)(TLS*1) +#endif + +TEXT ·getg(SB), NOSPLIT, $0-4 + get_tls(CX) + MOVL g(CX), AX + MOVL AX, ret+0(FP) + RET