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/cgo: missing references to symbols in .syso #52863

Closed
sbinet opened this issue May 12, 2022 · 16 comments
Closed

cmd/cgo: missing references to symbols in .syso #52863

sbinet opened this issue May 12, 2022 · 16 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. release-blocker
Milestone

Comments

@sbinet
Copy link
Member

sbinet commented May 12, 2022

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

$ go version
go version devel go1.19-2bea43b0e7 Thu May 12 04:47:29 2022 +0000 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
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/binet/.cache/go-build"
GOENV="/home/binet/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/binet/dev/go/gocode/pkg/mod"
GONOPROXY="gitlab.cern.ch/tile-in-one/tio-go"
GONOSUMDB="gitlab.cern.ch/tile-in-one/tio-go"
GOOS="linux"
GOPATH="/home/binet/dev/go/gocode"
GOPRIVATE="gitlab.cern.ch/tile-in-one/tio-go"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/binet/sdk/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/binet/sdk/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="devel go1.19-2bea43b0e7 Thu May 12 04:47:29 2022 +0000"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
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-build3696923155=/tmp/go-build -gno-record-gcc-switches"

What did you do?

$> cd somewhere
$> git clone git@github.com:gioui/gio gio && cd gio
$> go build ./...
[...]
gioui.org/widget/material
gioui.org/gpu/internal/vulkan
gioui.org/gpu
gioui.org/internal/egl
# gioui.org/internal/egl
/usr/bin/ld: /home/binet/work/gio/pkg/mod/gioui.org/shader@v1.0.6/piet/backdrop_linux_amd64.syso: in function `backdrop_coroutine_begin':
:(.text+0x2b): undefined reference to `coroutine_alloc_frame'
/usr/bin/ld: /home/binet/work/gio/pkg/mod/gioui.org/shader@v1.0.6/piet/backdrop_linux_amd64.syso: in function `backdrop_coroutine_begin.destroy':
:(.text+0x2a42): undefined reference to `coroutine_free_frame'
/usr/bin/ld: /home/binet/work/gio/pkg/mod/gioui.org/shader@v1.0.6/piet/backdrop_linux_amd64.syso: in function `backdrop_coroutine_begin.cleanup':
:(.text+0x2a54): undefined reference to `coroutine_free_frame'
/usr/bin/ld: /home/binet/work/gio/pkg/mod/gioui.org/shader@v1.0.6/piet/binning_linux_amd64.syso: in function `binning_coroutine_begin':
:(.text+0x24): undefined reference to `coroutine_alloc_frame'
/usr/bin/ld: /home/binet/work/gio/pkg/mod/gioui.org/shader@v1.0.6/piet/binning_linux_amd64.syso: in function `binning_coroutine_begin.resume':
:(.text+0x13fe): undefined reference to `floorf'
/usr/bin/ld: :(.text+0x140f): undefined reference to `floorf'
/usr/bin/ld: :(.text+0x1421): undefined reference to `floorf'
/usr/bin/ld: :(.text+0x1434): undefined reference to `floorf'
/usr/bin/ld: :(.text+0x1464): undefined reference to `floorf'

everything works fine w/ Go-1.18.x

What did you expect to see?

a valid build.

@heschi
Copy link
Contributor

heschi commented May 12, 2022

cc @golang/runtime

Hard to say without investigating if this is a new bug in the compiler or a latent bug in gioui.

@heschi heschi added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 12, 2022
@heschi heschi added this to the Go1.19 milestone May 12, 2022
@cherrymui
Copy link
Member

What is in the syso and how is it built? It looks like those symbols are from C libraries. floorf is a C function that needs to be linked with -lm. coroutine_alloc_frame is from LLVM. gio probably needs to pass the right flags to the C linker to link with those libraries.

@seankhliao
Copy link
Member

cc @eliasnaur

@seankhliao seankhliao changed the title compile: missing references to symbols in .syso cmd/compile: missing references to symbols in .syso May 12, 2022
@sbinet
Copy link
Member Author

sbinet commented May 16, 2022

it seems the build is taken care of by:

https://git.sr.ht/~eliasnaur/gio-shader/tree/main/item/piet/gencpu.sh

the program that actually creates the .syso seems to be:
https://git.sr.ht/~eliasnaur/gio-cpu/tree/main/item/cmd/compile/main.go#L120
(I have highlighted the part that does symbol renaming, which may have changed behaviour from 1.18 to 1.19)

@eliasnaur
Copy link
Contributor

Does -x reveal the external linker invocation? FWIW, -lm is specified in https://git.sr.ht/~eliasnaur/gio-shader/tree/main/item/piet/backdrop_abi.go#L13, and coroutine_ functions are defined in support.c.

@cherrymui
Copy link
Member

go build -x doesn't print external linker invocation, but -ldflags=-v does. Yeah, it would be good to show the external linker invocation to see what's missing.

@cherrymui cherrymui added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label May 18, 2022
@gopherbot
Copy link
Contributor

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@dominikh
Copy link
Member

Reopening because I'm seeing this, too.

@dominikh dominikh reopened this Jun 21, 2022
@dominikh
Copy link
Member

I'm seeing slightly different behavior, where go build only complains about floorf:

$ go build -x -ldflags=-v
WORK=/tmp/nix-shell.NrT9cB/go-build4005297001
mkdir -p $WORK/b001/
cd /home/dominikh/tmp/gio/internal/egl
pkg-config --cflags -- egl
pkg-config --libs -- egl
TERM='dumb' CGO_LDFLAGS='"-g" "-O2" "-L/nix/store/7vymg77rj9q42iriksanwzafln601ing-libglvnd-1.4.0/lib" "-lEGL"' /home/dominikh/prj/go/pkg/tool/linux_amd64/cgo -objdir $WORK/b001/ -importpath gioui.org/internal/egl -- -I/nix/store/mzskcvicgwsjfyynqsqzbv0w3lh6rpxg-libglvnd-1.4.0-dev/include -I $WORK/b001/ -g -O2 -DEGL_NO_X11 ./egl_unix.go
cd $WORK
gcc -fno-caret-diagnostics -c -x c - -o /dev/null || true
gcc -Qunused-arguments -c -x c - -o /dev/null || true
gcc -Wl,--no-gc-sections -x c - -o /dev/null || true
gcc -fdebug-prefix-map=a=b -c -x c - -o /dev/null || true
gcc -gno-record-gcc-switches -c -x c - -o /dev/null || true
cd $WORK/b001
TERM='dumb' gcc -I /home/dominikh/tmp/gio/internal/egl -fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -I/nix/store/mzskcvicgwsjfyynqsqzbv0w3lh6rpxg-libglvnd-1.4.0-dev/include -I ./ -g -O2 -DEGL_NO_X11 -o ./_x001.o -c _cgo_export.c
TERM='dumb' gcc -I /home/dominikh/tmp/gio/internal/egl -fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -I/nix/store/mzskcvicgwsjfyynqsqzbv0w3lh6rpxg-libglvnd-1.4.0-dev/include -I ./ -g -O2 -DEGL_NO_X11 -o ./_x002.o -c egl_unix.cgo2.c
TERM='dumb' gcc -I /home/dominikh/tmp/gio/internal/egl -fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -I/nix/store/mzskcvicgwsjfyynqsqzbv0w3lh6rpxg-libglvnd-1.4.0-dev/include -I ./ -g -O2 -DEGL_NO_X11 -o ./_cgo_main.o -c _cgo_main.c
cd /home/dominikh/tmp/gio/internal/egl
TERM='dumb' gcc -I . -fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -o $WORK/b001/_cgo_.o $WORK/b001/_cgo_main.o $WORK/b001/_x001.o $WORK/b001/_x002.o /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/backdrop_linux_amd64.syso /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/binning_linux_amd64.syso /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/coarse_linux_amd64.syso /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/elements_linux_amd64.syso /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/kernel4_linux_amd64.syso /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/path_coarse_linux_amd64.syso /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/tile_alloc_linux_amd64.syso -g -O2 -L/nix/store/7vymg77rj9q42iriksanwzafln601ing-libglvnd-1.4.0/lib -lEGL
# gioui.org/internal/egl
/nix/store/cz52w8xf3i1d3xvzpzd9abf7rvpl9017-binutils-2.38/bin/ld: /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/binning_linux_amd64.syso: undefined reference to symbol 'floorf@@GLIBC_2.2.5'
/nix/store/cz52w8xf3i1d3xvzpzd9abf7rvpl9017-binutils-2.38/bin/ld: /nix/store/fz33c1mfi2krpg1lwzizfw28kj705yg0-glibc-2.34-210/lib/libm.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

Adding -lm to the failing gcc invocation leads to this:

$ gcc -I . -fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -o $WORK/b001/_cgo_.o $WORK/b001/_cgo_main.o $WORK/b001/_x001.o $WORK/b001/_x002.o /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/backdrop_linux_amd64.syso /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/binning_linux_amd64.syso /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/coarse_linux_amd64.syso /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/elements_linux_amd64.syso /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/kernel4_linux_amd64.syso /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/path_coarse_linux_amd64.syso /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/tile_alloc_linux_amd64.syso -g -O2 -L/nix/store/7vymg77rj9q42iriksanwzafln601ing-libglvnd-1.4.0/lib -lEGL -lm
/nix/store/cz52w8xf3i1d3xvzpzd9abf7rvpl9017-binutils-2.38/bin/ld: /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/backdrop_linux_amd64.syso: in function `backdrop_coroutine_begin':
:(.text+0x2b): undefined reference to `coroutine_alloc_frame'
/nix/store/cz52w8xf3i1d3xvzpzd9abf7rvpl9017-binutils-2.38/bin/ld: /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/backdrop_linux_amd64.syso: in function `backdrop_coroutine_begin.destroy':
:(.text+0x2a42): undefined reference to `coroutine_free_frame'
/nix/store/cz52w8xf3i1d3xvzpzd9abf7rvpl9017-binutils-2.38/bin/ld: /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/backdrop_linux_amd64.syso: in function `backdrop_coroutine_begin.cleanup':
:(.text+0x2a54): undefined reference to `coroutine_free_frame'
/nix/store/cz52w8xf3i1d3xvzpzd9abf7rvpl9017-binutils-2.38/bin/ld: /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/binning_linux_amd64.syso: in function `binning_coroutine_begin':
:(.text+0x24): undefined reference to `coroutine_alloc_frame'
/nix/store/cz52w8xf3i1d3xvzpzd9abf7rvpl9017-binutils-2.38/bin/ld: /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/binning_linux_amd64.syso: in function `binning_coroutine_begin.destroy':
:(.text+0x3622): undefined reference to `coroutine_free_frame'
/nix/store/cz52w8xf3i1d3xvzpzd9abf7rvpl9017-binutils-2.38/bin/ld: /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/binning_linux_amd64.syso: in function `binning_coroutine_begin.cleanup':
:(.text+0x3634): undefined reference to `coroutine_free_frame'
/nix/store/cz52w8xf3i1d3xvzpzd9abf7rvpl9017-binutils-2.38/bin/ld: /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/coarse_linux_amd64.syso: in function `coarse_coroutine_begin':
:(.text+0x26): undefined reference to `coroutine_alloc_frame'
/nix/store/cz52w8xf3i1d3xvzpzd9abf7rvpl9017-binutils-2.38/bin/ld: /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/coarse_linux_amd64.syso: in function `coarse_coroutine_begin.destroy':
:(.text+0xca42): undefined reference to `coroutine_free_frame'
/nix/store/cz52w8xf3i1d3xvzpzd9abf7rvpl9017-binutils-2.38/bin/ld: /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/coarse_linux_amd64.syso: in function `coarse_coroutine_begin.cleanup':
:(.text+0xca54): undefined reference to `coroutine_free_frame'
/nix/store/cz52w8xf3i1d3xvzpzd9abf7rvpl9017-binutils-2.38/bin/ld: /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/elements_linux_amd64.syso: in function `elements_coroutine_begin':
:(.text+0x24): undefined reference to `coroutine_alloc_frame'
/nix/store/cz52w8xf3i1d3xvzpzd9abf7rvpl9017-binutils-2.38/bin/ld: /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/elements_linux_amd64.syso: in function `elements_coroutine_begin.destroy':
:(.text+0x20d12): undefined reference to `coroutine_free_frame'
/nix/store/cz52w8xf3i1d3xvzpzd9abf7rvpl9017-binutils-2.38/bin/ld: /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/elements_linux_amd64.syso: in function `elements_coroutine_begin.cleanup':
:(.text+0x20d24): undefined reference to `coroutine_free_frame'
/nix/store/cz52w8xf3i1d3xvzpzd9abf7rvpl9017-binutils-2.38/bin/ld: /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/tile_alloc_linux_amd64.syso: in function `tile_alloc_coroutine_begin':
:(.text+0x2b): undefined reference to `coroutine_alloc_frame'
/nix/store/cz52w8xf3i1d3xvzpzd9abf7rvpl9017-binutils-2.38/bin/ld: /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/tile_alloc_linux_amd64.syso: in function `tile_alloc_coroutine_begin.destroy':
:(.text+0x35f2): undefined reference to `coroutine_free_frame'
/nix/store/cz52w8xf3i1d3xvzpzd9abf7rvpl9017-binutils-2.38/bin/ld: /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/tile_alloc_linux_amd64.syso: in function `tile_alloc_coroutine_begin.cleanup':
:(.text+0x3604): undefined reference to `coroutine_free_frame'
collect2: error: ld returned 1 exit status

@cherrymui
Copy link
Member

Thanks @dominikh for the information. It seems the error comes from this command

TERM='dumb' gcc -I . -fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -o $WORK/b001/_cgo_.o $WORK/b001/_cgo_main.o $WORK/b001/_x001.o $WORK/b001/_x002.o /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/backdrop_linux_amd64.syso /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/binning_linux_amd64.syso /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/coarse_linux_amd64.syso /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/elements_linux_amd64.syso /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/kernel4_linux_amd64.syso /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/path_coarse_linux_amd64.syso /home/dominikh/prj/pkg/mod/gioui.org/shader@v1.0.6/piet/tile_alloc_linux_amd64.syso -g -O2 -L/nix/store/7vymg77rj9q42iriksanwzafln601ing-libglvnd-1.4.0/lib -lEGL

which is the cgo tool building _cgo_main.c, instead of the final linking of the binary.

CL https://go-review.googlesource.com/c/go/+/402596 may be related, which makes the cgo tool pass syso files. It appears that when compiling the egl package the cgo tool passes the syso from the piet package (per CL description, it includes transitive dependencies). The Go and C files from the piet package (https://git.sr.ht/~eliasnaur/gio-shader/tree/main/item/piet/backdrop_abi.go#L13 , https://git.sr.ht/~eliasnaur/gio-shader/tree/main/item/piet/support.c#L20) provides the -lm flag and the missing symbols, but they are not included by the cgo tool.

cc @ianlancetaylor @rsc

@cherrymui cherrymui changed the title cmd/compile: missing references to symbols in .syso cmd/cgo: missing references to symbols in .syso Jun 21, 2022
@cherrymui cherrymui removed the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jun 21, 2022
@ianlancetaylor
Copy link
Member

We did not expect to see packages use .syso files that depend on code defined by the package. We expected .syso files to stand alone, or at least to only depend on other .syso files.

The problematic link is only required when internal linking mode. I wonder how much would break if we required some new go build option for cgo-using packages outside the standard library to support internal linking.

@cherrymui
Copy link
Member

cherrymui commented Jun 21, 2022

I wonder if we can do that link step at (Go) link time. At that point, we know what link mode we use. If we use external linking, we don't need to run it. If we use internal linking, we only need that information at link time. And presumably we can run it just once for a binary, instead of once for each cgo-using packages. At that point we could probably pull in all the C code together. (Of course that would need a lot of work for the go command, and perhaps the cgo tool.)

@ianlancetaylor
Copy link
Member

I think I have a relatively simple solution: if cgo fails when generating _cgo_import.go, let's silently hide the failure, and mark the object (somehow) as requiring external linking. Then we can fail at link time if we are internally linking and the object says we can't do that.

@cherrymui
Copy link
Member

SGTM. Thanks.

@ianlancetaylor
Copy link
Member

Working on it.

@ianlancetaylor ianlancetaylor self-assigned this Jun 22, 2022
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/413460 mentions this issue: cmd/go, cmd/link: support failure to create _cgo_import.go

jproberts pushed a commit to jproberts/go that referenced this issue Aug 10, 2022
For a package that uses cgo, the file _cgo_import.go is created to
record information required for internal linking: the non-Go dynamic
symbols and libraries that the package depends on. Generating this
information sometimes fails, because it can require recreating all the
dependencies of all transitively imported packages. And the
information is rarely needed, since by default we use external linking
when there are packages outside of the standard library that use cgo.

With this CL, if generating _cgo_import.go fails, we don't report an
error. Instead, we mark the package as requiring external linking, by
adding an empty file named "dynimportfail" into the generated archive.
If the linker sees a file with that name, it rejects an attempt to use
internal linking.

Fixes golang#52863

Change-Id: Ie586e6753a5b67e49bb14533cd7603d9defcf0ea
Reviewed-on: https://go-review.googlesource.com/c/go/+/413460
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
@golang golang locked and limited conversation to collaborators Jun 22, 2023
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. release-blocker
Projects
Status: Done
Development

No branches or pull requests

8 participants