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: unable to find value of constant for indirect macro on arm64 while setting -fsanitize=hwaddress #53285

Closed
niukuo opened this issue Jun 8, 2022 · 6 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@niukuo
Copy link

niukuo commented Jun 8, 2022

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

$ go version
go version go1.18.3 linux/arm64

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="arm64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_arm64"
GOVCS=""
GOVERSION="go1.18.3"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/opt/cgotest/go.mod"
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 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build4015687639=/tmp/go-build -gno-record-gcc-switches"

What did you do?

$docker run --rm -it --platform linux/arm64/v8 golang:stretch bash
$apt install clang-11 lld-11 -y

$cat << EOF > a.go
package main

/*
#define VARA 5
#define VARB VARA
*/
import "C"

import (
    "fmt"
)

func main() {
    fmt.Println(C.VARB)
}
EOF

$go mod init cgotest
$CC=clang-11 CGO_CFLAGS="-fsanitize=hwaddress" CGO_LDFLAGS="-fsanitize=hwaddress -fuse-ld=lld" go build

What did you expect to see?

What did you see instead?

./a.go:14:17: unable to find value of constant C.VARB
@niukuo niukuo changed the title cgo: unable to find value of constant for indirect macro on arm64 while setting -fsanitize=hwaddress cmd/cgo: unable to find value of constant for indirect macro on arm64 while setting -fsanitize=hwaddress Jun 8, 2022
@cherrymui cherrymui added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jun 8, 2022
@cherrymui cherrymui added this to the Backlog milestone Jun 8, 2022
@cherrymui
Copy link
Member

cc @ianlancetaylor

@ianlancetaylor
Copy link
Contributor

I can't recreate this problem using any of

  • GCC 11.2.0
  • clang 13.0.1
  • clang 11.1.0

In general it's hard to see why this would be affected by -fsanitize=hwaddress. Does the build succeed without that option?

What is the exact compiler version that you are using? Is it possible to try a newer version?

@ianlancetaylor ianlancetaylor added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jun 8, 2022
@niukuo
Copy link
Author

niukuo commented Jun 9, 2022

it works on x86
I'm trying

  • clang 11.0.1 on arm64
  • clang 11.1.0 on x86 with --target=aarch64-unknown-linux-gnu

and it doesn't work.

it works on

  • x86
  • arm64 with -fsanitize=address
  • arm64 without -fsanitize=hwaddress

@niukuo
Copy link
Author

niukuo commented Jun 9, 2022

i tried clang-13 and it's also not working.

it can reproduce with following params:

  • --target=aarch64-unknown-linux-gnu
  • -fsanitize=hwaddress

@ianlancetaylor
Copy link
Contributor

Thanks. It looks like -fsanitize=hwaddress introduces differences in the object file format that cgo does not understand. For example, cgo compiles a C file that contains, among other code,

long long __cgodebug_ints[] = {
        VARB,
        1
};

cgo then looks through the symbol table of the object file to find __cgodebug_ints and to look at the values that the compiler put into the object file. Without -fsanitize=hwaddress this works fine. With -fsanitize=hwaddress the symbol table entry as reported by the readelf program is

    28: 6a00000000000000    16 OBJECT  GLOBAL DEFAULT   11 __cgodebug_ints

This is saying that the symbol is defined in section 11 at offset 0x6a00000000000000. This is not the case, so cgo can't find the value it is looking for, and cgo fails.

It may be possible for cgo to detect this case and adjut.

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

Change https://go.dev/cl/411698 mentions this issue: cmd/cgo: remove -fsanitize=hwaddress hardware tags

@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. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

4 participants