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

cmd/link: building a package that uses cgo errors out with "cannot implicitly include runtime/cgo in a shared library" #47183

Closed
diamondburned opened this issue Jul 14, 2021 · 3 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@diamondburned
Copy link

diamondburned commented Jul 14, 2021

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

―❤―▶ go version
go version go1.16.5 linux/amd64

This issue was also reproduced with 1.15.10 and 1.17beta1.

Does this issue reproduce with the latest release?

No: the same code couldn't reproduce the same error on a Debian virtual machine, but a different error was returned. See the other section below.

Yes, but only before running go install -buildmode=shared -linkshared std.

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

go env Output
―❤―▶ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN="/home/diamond/.go/bin"
GOCACHE="/home/diamond/.cache/go-build"
GOENV="/home/diamond/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/diamond/.go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/diamond/.go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/nix/store/9asi3pbr7j0z3n1y80nv2pprgm0chvlv-go-1.16.5/share/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/nix/store/9asi3pbr7j0z3n1y80nv2pprgm0chvlv-go-1.16.5/share/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.5"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/tmp/shared-test/go.mod"
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 -fmessage-length=0 -fdebug-prefix-map=/run/user/1000/go-build672825401=/tmp/go-build -gno-record-gcc-switches"
/etc/os-release
NAME=NixOS
ID=nixos
VERSION="20.09.4302.17a455ce5a2 (Nightingale)"
VERSION_CODENAME=nightingale
VERSION_ID="20.09.4302.17a455ce5a2"
PRETTY_NAME="NixOS 20.09 (Nightingale)"
LOGO="nix-snowflake"
HOME_URL="https://nixos.org/"
DOCUMENTATION_URL="https://nixos.org/learn.html"
SUPPORT_URL="https://nixos.org/community.html"
BUG_REPORT_URL="https://github.com/NixOS/nixpkgs/issues"
lscpu Output
Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
Address sizes:                   39 bits physical, 48 bits virtual
CPU(s):                          8
On-line CPU(s) list:             0-7
Thread(s) per core:              2
Core(s) per socket:              4
Socket(s):                       1
NUMA node(s):                    1
Vendor ID:                       GenuineIntel
CPU family:                      6
Model:                           142
Model name:                      Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
Stepping:                        10
CPU MHz:                         2472.405
CPU max MHz:                     3400.0000
CPU min MHz:                     400.0000
BogoMIPS:                        3600.00
Virtualization:                  VT-x
L1d cache:                       128 KiB
L1i cache:                       128 KiB
L2 cache:                        1 MiB
L3 cache:                        6 MiB
NUMA node0 CPU(s):               0-7
Vulnerability Itlb multihit:     KVM: Vulnerable
Vulnerability L1tf:              Mitigation; PTE Inversion; VMX vulnerable
Vulnerability Mds:               Vulnerable; SMT vulnerable
Vulnerability Meltdown:          Vulnerable
Vulnerability Spec store bypass: Vulnerable
Vulnerability Spectre v1:        Vulnerable: __user pointer sanitization and usercopy barriers only; no swapgs barr
                                 iers
Vulnerability Spectre v2:        Vulnerable, IBPB: disabled, STIBP: disabled
Vulnerability Srbds:             Vulnerable
Vulnerability Tsx async abort:   Not affected
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dt
                                 s acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc ar
                                 t arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni p
                                 clmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4
                                 _1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm
                                  abm 3dnowprefetch cpuid_fault epb invpcid_single ssbd ibrs ibpb stibp tpr_shadow 
                                 vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms inv
                                 pcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm
                                  ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp md_clear flush_l1d

What did you do?

This issue was opened for two errors: a cannot implicitly include runtime/cgo in a shared library error and a cap out of range error in loader.(*Loader).LoadSyms.

When building this file at c/c.go:

package c

// int add(int i, int j) { return i + j; }
import "C"

func Add(i, j int) int {
	return int(C.add(C.int(i), C.int(j)))
}

using the command go build -buildmode=shared -linkshared ./c, the following error is returned:

# /run/user/1000/go-build3898680118/b018/libshared-test-c.so
/nix/store/9asi3pbr7j0z3n1y80nv2pprgm0chvlv-go-1.16.5/share/go/pkg/tool/linux_amd64/link: cannot implicitly include runtime/cgo in a shared library

The same error is returned when adding import _ "runtime/cgo".

With this code at ./main.go:

package main

import "fmt"

func main() {
	fmt.Println("bruh")
}

built using go build -linkshared ., a similar error is returned:

# shared-test
/nix/store/9asi3pbr7j0z3n1y80nv2pprgm0chvlv-go-1.16.5/share/go/pkg/tool/linux_amd64/link: cannot implicitly include runtime/cgo in a shared library

What did you expect to see?

The first library code built with go build -buildmode=shared -linkshared ./c should succeed, and so should building the main executable with go build -linkshared .. The main executable should be small and dynamically link libstd.so from somewhere else.

What did you see instead?

The two above errors:

# /run/user/1000/go-build3898680118/b018/libshared-test-c.so
/nix/store/9asi3pbr7j0z3n1y80nv2pprgm0chvlv-go-1.16.5/share/go/pkg/tool/linux_amd64/link: cannot implicitly include runtime/cgo in a shared library
# shared-test
/nix/store/9asi3pbr7j0z3n1y80nv2pprgm0chvlv-go-1.16.5/share/go/pkg/tool/linux_amd64/link: cannot implicitly include runtime/cgo in a shared library

This section describes an attempt at reproducing the above bug after running go install -buildmode=shared -linkshared std.

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

diamond@debianVM:/tmp/shared-test$ ~/go-1.16/bin/go version
go version go1.16.6 linux/amd64

Does this issue reproduce with the latest release?

Yes.

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

go env Output
O111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/diamond/.cache/go-build"
GOENV="/home/diamond/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/diamond/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/diamond/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/diamond/go-1.16"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/diamond/go-1.16/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.6"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/tmp/shared-test/go.mod"
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3297561287=/tmp/go-build -gno-record-gcc-switches"
/etc/os-release
PRETTY_NAME="Debian GNU/Linux bullseye/sid"
NAME="Debian GNU/Linux"
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

What did you do?

See the above section.

What did you expect to see?

See the above section.

What did you see instead?

Building package c yielded a makeslice: cap out of range panic:

diamond@debianVM:/tmp/shared-test$ ~/go-1.16/bin/go build -buildmode=shared -linkshared ./c
# /tmp/go-build2134261352/b018/libshared-test-c.so
panic: runtime error: makeslice: cap out of range

goroutine 1 [running]:
cmd/link/internal/loader.(*Loader).LoadSyms(0xc000786000, 0x87f340)
	/usr/local/go/src/cmd/link/internal/loader/loader.go:2188 +0x125
cmd/link/internal/ld.(*Link).loadlib(0xc00014e000)
	/usr/local/go/src/cmd/link/internal/ld/lib.go:558 +0x3de
cmd/link/internal/ld.Main(0x87f340, 0x20, 0x20, 0x1, 0x7, 0x10, 0x0, 0x0, 0x6e86a7, 0x1b, ...)
	/usr/local/go/src/cmd/link/internal/ld/main.go:244 +0xdbc
main.main()
	/usr/local/go/src/cmd/link/main.go:68 +0x258

The binary actually built successfully with the expected linking:

diamond@debianVM:/tmp/shared-test$ ~/go-1.16/bin/go build -linkshared .
diamond@debianVM:/tmp/shared-test$ ls
c  go.mod  main.go  shared-test
diamond@debianVM:/tmp/shared-test$ ./shared-test
bruh
diamond@debianVM:/tmp/shared-test$ ldd shared-test 
	linux-vdso.so.1 (0x00007fffcfb1e000)
	libstd.so => /home/diamond/go-1.16/pkg/linux_amd64_dynlink/libstd.so (0x00007fd819e56000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd819c7c000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fd819c76000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fd819c54000)
	/lib64/ld-linux-x86-64.so.2 (0x00007fd81c5eb000)
@mdempsky
Copy link
Member

/cc @ianlancetaylor @cherrymui

@mdempsky mdempsky added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 14, 2021
@diamondburned
Copy link
Author

diamondburned commented Jul 14, 2021

It seems like the second bug is reproducible only after a successful go install -buildmode=shared -linkshared std, so they seem to be two separate issues.

@seankhliao
Copy link
Member

Obsoleted by #47788

@golang golang locked and limited conversation to collaborators Oct 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants