Building modules with gccgo

I’d like to be able to build modules with gccgo, but its not working. I’m using go 1.13.7 and gccgo-9.

Given a very simple module with two files:

// go.mod


go 1.12


// helloworld.go

package main
import "fmt"
import "runtime"
func main() {
    fmt.Println("hello world from " + runtime.Version() + " " + " " + runtime.GOOS + " " + runtime.GOARCH)

and compiled like this

export GCCGO=gccgo-9
go build -compiler=gccgo -gccgoflags -static

it fails with

/tmp/go-build070129611/b001/_gomod_.go:3:3: error: __debug_modinfo__ is not a function; //go:linkname is only supported for functions
    3 | //go:linkname __debug_modinfo__ runtime.modinfo
      |   ^

If I don’t use gccgo, it works. Or, if I use gccgo and delete go.mod, the above command works. So, I can either build modules or use gccgo, but not both. What is going on here?

Using go 1.14rc1 is also broken, though the error changes to:

/usr/bin/ld: $WORK/b001/_pkg_.a(_go_.o): in function `main.main..init0':
/tmp/go-build/b001/_gomod_.go:5: undefined reference to `runtime.setmodinfo'
collect2: error: ld returned 1 exit status

I realized my problem. I was using the standard go tool (as built by gc). I needed to be using gccgo-go (as built by gccgo).

Now I can do:

go build -gccgoflags -static
hello world from go1.12.2 gccgo (Ubuntu 9.2.1-9ubuntu2) 9.2.1 20191008 
linux amd64

Since I’m using gccgo-go, its automatically wired up to gccgo, so there’s no need to define GCCGO (unless we’re cross-compiling).