NVIDIA / go-nvml

Go Bindings for the NVIDIA Management Library (NVML)
Apache License 2.0
316 stars 64 forks source link

Is there a way to build a working static linked binary using go 1.20? #62

Open jshen28 opened 1 year ago

jshen28 commented 1 year ago

Hello,

I would like to know if it possible to build a static linked binary using go 1.20? For example,

package main

import (
    "fmt"
    "github.com/NVIDIA/go-nvml/pkg/nvml"
)

func main() {
    ret := nvml.Init()
    fmt.Printf("Returns: %v \n", ret)

}

It is working if I build it with

go build main.go

But it is not working if I build it

go build -ldflags '-linkmode "external" -extldflags "-static"'

The stack is ,

fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x0]

runtime stack:
runtime.throw({0x55ef22?, 0xc000098000?})
        /usr/local/go/src/runtime/panic.go:1047 +0x5d fp=0x7ffcafe441d0 sp=0x7ffcafe441a0 pc=0x4317fd
runtime.sigpanic()
        /usr/local/go/src/runtime/signal_unix.go:821 +0x3e9 fp=0x7ffcafe44230 sp=0x7ffcafe441d0 pc=0x445ee9

goroutine 1 [syscall]:
runtime.cgocall(0x48ed90, 0xc000097ee8)
        /usr/local/go/src/runtime/cgocall.go:157 +0x5c fp=0xc000097ec0 sp=0xc000097e88 pc=0x4039dc
github.com/NVIDIA/go-nvml/pkg/nvml._Cfunc_nvmlInit_v2()
        _cgo_gotypes.go:4597 +0x48 fp=0xc000097ee8 sp=0xc000097ec0 pc=0x488788
github.com/NVIDIA/go-nvml/pkg/nvml.nvmlInit_v2()
        /home/sjt/go/src/pkg/mod/github.com/!n!v!i!d!i!a/go-nvml@v0.12.0-1/pkg/nvml/nvml.go:32 +0x19 fp=0xc000097f00 sp=0xc000097ee8 pc=0x489059
github.com/NVIDIA/go-nvml/pkg/nvml.Init()
        /home/sjt/go/src/pkg/mod/github.com/!n!v!i!d!i!a/go-nvml@v0.12.0-1/pkg/nvml/init.go:43 +0x91 fp=0xc000097f28 sp=0xc000097f00 pc=0x4889b1
main.main()
        /home/sjt/src/go-nvml-test/main.go:10 +0x19 fp=0xc000097f80 sp=0xc000097f28 pc=0x48b659
runtime.main()
        /usr/local/go/src/runtime/proc.go:250 +0x207 fp=0xc000097fe0 sp=0xc000097f80 pc=0x4340e7
runtime.goexit()
        /usr/local/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc000097fe8 sp=0xc000097fe0 pc=0x45e5c1

goroutine 2 [force gc (idle)]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
        /usr/local/go/src/runtime/proc.go:381 +0xd6 fp=0xc000084fb0 sp=0xc000084f90 pc=0x434516
runtime.goparkunlock(...)
        /usr/local/go/src/runtime/proc.go:387
runtime.forcegchelper()
        /usr/local/go/src/runtime/proc.go:305 +0xb0 fp=0xc000084fe0 sp=0xc000084fb0 pc=0x434350
runtime.goexit()
        /usr/local/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc000084fe8 sp=0xc000084fe0 pc=0x45e5c1
created by runtime.init.6
        /usr/local/go/src/runtime/proc.go:293 +0x25

goroutine 3 [GC sweep wait]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
        /usr/local/go/src/runtime/proc.go:381 +0xd6 fp=0xc000085780 sp=0xc000085760 pc=0x434516
runtime.goparkunlock(...)
        /usr/local/go/src/runtime/proc.go:387
runtime.bgsweep(0x0?)
        /usr/local/go/src/runtime/mgcsweep.go:278 +0x8e fp=0xc0000857c8 sp=0xc000085780 pc=0x42126e
runtime.gcenable.func1()
        /usr/local/go/src/runtime/mgc.go:178 +0x26 fp=0xc0000857e0 sp=0xc0000857c8 pc=0x416726
runtime.goexit()
        /usr/local/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc0000857e8 sp=0xc0000857e0 pc=0x45e5c1
created by runtime.gcenable
        /usr/local/go/src/runtime/mgc.go:178 +0x6b

goroutine 4 [GC scavenge wait]:
runtime.gopark(0xc0000ac000?, 0x575bf0?, 0x1?, 0x0?, 0x0?)
        /usr/local/go/src/runtime/proc.go:381 +0xd6 fp=0xc000085f70 sp=0xc000085f50 pc=0x434516
runtime.goparkunlock(...)
        /usr/local/go/src/runtime/proc.go:387
runtime.(*scavengerState).park(0x825ea0)
        /usr/local/go/src/runtime/mgcscavenge.go:400 +0x53 fp=0xc000085fa0 sp=0xc000085f70 pc=0x41f193
runtime.bgscavenge(0x0?)
        /usr/local/go/src/runtime/mgcscavenge.go:628 +0x45 fp=0xc000085fc8 sp=0xc000085fa0 pc=0x41f765
runtime.gcenable.func2()
        /usr/local/go/src/runtime/mgc.go:179 +0x26 fp=0xc000085fe0 sp=0xc000085fc8 pc=0x4166c6
runtime.goexit()
        /usr/local/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc000085fe8 sp=0xc000085fe0 pc=0x45e5c1
created by runtime.gcenable
        /usr/local/go/src/runtime/mgc.go:179 +0xaa

goroutine 5 [finalizer wait]:
runtime.gopark(0x1a0?, 0x8262e0?, 0x60?, 0x78?, 0xc000084770?)
        /usr/local/go/src/runtime/proc.go:381 +0xd6 fp=0xc000084628 sp=0xc000084608 pc=0x434516
runtime.runfinq()
        /usr/local/go/src/runtime/mfinal.go:193 +0x107 fp=0xc0000847e0 sp=0xc000084628 pc=0x415767
runtime.goexit()
        /usr/local/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc0000847e8 sp=0xc0000847e0 pc=0x45e5c1
created by runtime.createfing
        /usr/local/go/src/runtime/mfinal.go:163 +0x45

For references, go version is 1.20.4 and go-nvml version is v0.12.0-1

The hope to use a static linked binary is to make it easier for a running binary inside container. I am running a two staging image build, the image used for building go binary is different from image where binary is running.

Best,

klueska commented 1 year ago

~I see no reason why not. Did you run into issues when trying to do so?~

jshen28 commented 1 year ago

Thank you very much for reply, I've updated the description and hope this time it makes more sense.

halohsu commented 11 months ago

Keep the gcc = gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.2)