Skip to content

cmd/link: RISC-V mapping symbols aren't handled correctly #73591

@ziyao233

Description

@ziyao233

Go version

go version go1.24.2 linux/riscv64

Output of go env in your module/workspace:

$ go env
AR='ar'
CC='/usr/bin/cc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='riscv64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/ziyao/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/ziyao/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -pthread -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1924204880=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='riscv64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/dev/null'
GOMODCACHE='/home/ziyao/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/ziyao/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GORISCV64='rva20u64'
GOROOT='/usr/lib/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/ziyao/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/go/pkg/tool/linux_riscv64'
GOVCS=''
GOVERSION='go1.24.2'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

Build test.go,

package main                                                                                                                                                   
                                       
/*                                                                                                                                                             
#include <stdio.h>                     
#include <stdlib.h>                                                                                                                                            
*/                                     
import "C"                                                                                                                                                     
                                       
import "unsafe"                                                                                                                                                
                                       
func main() {                                                                                                                                                  
    cs := C.CString("Hello World")     
    C.puts(cs)                                                                                                                                                 
    C.free(unsafe.Pointer(cs))         
}                                                                                                                                                              

which makes use of CGO with Clang and Golang internal linker

CC=clang go build -ldflags '-linkmode internal' example.go

What did you see happen?

The file successfully compiles and the resulted binary runs correctly.

What did you expect to see?

# command-line-arguments                                                       
loadelf: $WORK/b001/_pkg_.a(_x001.o): 66084: sym#27 ("$d"): ignoring symbol in section 4 (".debug_loclists") (type 0)
loadelf: $WORK/b001/_pkg_.a(_x002.o): 66118: sym#33 ("$d"): ignoring symbol in section 4 (".debug_loclists") (type 0)
loadelf: $WORK/b003/_pkg_.a(_x001.o): 66120: sym#2 ("$d"): ignoring symbol in section 3 (".comment") (type 0)
loadelf: $WORK/b003/_pkg_.a(_x002.o): 66122: sym#2 ("$d"): ignoring symbol in section 3 (".comment") (type 0)
loadelf: $WORK/b003/_pkg_.a(_x003.o): 66145: sym#22 ("$d"): ignoring symbol in section 4 (".debug_loclists") (type 0)
loadelf: $WORK/b003/_pkg_.a(_x004.o): 66169: sym#22 ("$d"): ignoring symbol in section 5 (".debug_loclists") (type 0)
loadelf: $WORK/b003/_pkg_.a(_x005.o): 66399: sym#227 ("$d"): ignoring symbol in section 6 (".debug_loclists") (type 0)
loadelf: $WORK/b003/_pkg_.a(_x006.o): 66489: sym#87 ("$d"): ignoring symbol in section 6 (".debug_loclists") (type 0)
loadelf: $WORK/b003/_pkg_.a(_x007.o): 66508: sym#18 ("$d"): ignoring symbol in section 4 (".debug_loclists") (type 0)
loadelf: $WORK/b003/_pkg_.a(_x008.o): 66524: sym#15 ("$d"): ignoring symbol in section 4 (".debug_loclists") (type 0)
loadelf: $WORK/b003/_pkg_.a(_x009.o): 66542: sym#17 ("$d"): ignoring symbol in section 3 (".debug_loclists") (type 0)
loadelf: $WORK/b003/_pkg_.a(_x010.o): 66572: sym#27 ("$d"): ignoring symbol in section 6 (".debug_loclists") (type 0)
loadelf: $WORK/b003/_pkg_.a(_x011.o): 66728: sym#155 ("$d"): ignoring symbol in section 4 (".debug_loclists") (type 0)
loadelf: $WORK/b003/_pkg_.a(_x012.o): 66790: sym#63 ("$d"): ignoring symbol in section 4 (".riscv.attributes") (type 0)
main._cgo_de7e76e774d0_Cfunc_free: relocation target _cgo_de7e76e774d0_Cfunc_free not defined
main._cgo_de7e76e774d0_Cfunc_puts: relocation target _cgo_de7e76e774d0_Cfunc_puts not defined
main._cgo_de7e76e774d0_Cfunc__Cmalloc: relocation target _cgo_de7e76e774d0_Cfunc__Cmalloc not defined
runtime.cgo_yield: relocation target _cgo_yield not defined                    
_cgo_init: relocation target x_cgo_init not defined                            
_cgo_thread_start: relocation target x_cgo_thread_start not defined            
_cgo_pthread_key_created: relocation target x_cgo_pthread_key_created not defined
/usr/lib/go/pkg/tool/linux_riscv64/link: too many errors    

These symbols with type equal to zero are "mapping symbols" specified in RISC-V psABI. They aren't correctly handled (IOW, ignored), causing crazy errors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.arch-riscvIssues solely affecting the riscv64 architecture.compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions