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: wrong program header offset when cross-compile to riscv64 when setting -T text alignment #57983

Closed
CN-GuoZiyang opened this issue Jan 25, 2023 · 5 comments
Labels
arch-riscv Issues solely affecting the riscv64 architecture. compiler/runtime Issues related to the Go compiler and/or runtime. 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

@CN-GuoZiyang
Copy link

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

$ go version
go version go1.19.5 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/coder/.cache/go-build"
GOENV="/home/coder/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/coder/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/coder/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19.5"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/coder/goose/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 -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3096622638=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I try to cross-compile an empty main function to RISC-V binary with the following command, trying to put text segment to 0x80200000:

GOOS=linux GOARCH=riscv64 go build -o kernel.elf -ldflags '-T 0x80200000' ./kmain

kmain is a directory containing a main.go with only an empty main func and nothing imports.

After executing, I tried to readelf -a kernel.elf, it says 'readelf: Error: the PHDR segment is not covered by a LOAD segment'. And the output program headers like:

Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  PHDR           0x0000000000000040 0x00000000801ff040 0x00000000801ff040
                 0x0000000000000188 0x0000000000000188  R      0x10000
  NOTE           0x0000000000000f9c 0x00000000801fff9c 0x00000000801fff9c
                 0x0000000000000064 0x0000000000000064  R      0x4
  LOAD           0xffffffffffff1000 0x00000000801f0000 0x00000000801f0000
                 0x0000000000063300 0x0000000000063300  R E    0x10000
  LOAD           0x0000000000060000 0x0000000080260000 0x0000000080260000
                 0x000000000006adb8 0x000000000006adb8  R      0x10000
  ...

The offset of the third section is pretty big and cannot be loaded.

When I change GOARCH to 'amd64' or delete -T, nothing above happens: none error and offset is also fine.

What did you expect to see?

none error and offsets of program headers is fine.

What did you see instead?

error and strangely big offset

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jan 25, 2023
@cherrymui cherrymui changed the title cmd/link: wrong program header offset when cross-compile to riscv64 cmd/link: wrong program header offset when cross-compile to riscv64 when setting -T text alignment Jan 25, 2023
@cherrymui
Copy link
Member

Thanks for report. Setting -T manually isn't really supported. It wouldn't be surprising that it doesn't work on some platform.

@cherrymui cherrymui added the arch-riscv Issues solely affecting the riscv64 architecture. label Jan 25, 2023
@cherrymui cherrymui added this to the Unplanned milestone Jan 25, 2023
@CN-GuoZiyang
Copy link
Author

Thanks for report. Setting -T manually isn't really supported. It wouldn't be surprising that it doesn't work on some platform.

so any other way I can set the address of text segment?

@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 30, 2023
@TotallyGamerJet
Copy link

This is potentially similar to #62064. A work around is to just set the rounding with -R=0x1000 since the default (0x10000) doesn't work with -T 0x80200000

@cherrymui
Copy link
Member

cherrymui commented Mar 11, 2024

As -T sets the start address of text symbols, which does not include the header, you probably want to add the header size. Try -T 0x80201000. Decreasing the rounding quantum (the -R flag) may get it build but may not be what you want.

@cherrymui cherrymui added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Mar 11, 2024
@gopherbot
Copy link

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.)

@gopherbot gopherbot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arch-riscv Issues solely affecting the riscv64 architecture. compiler/runtime Issues related to the Go compiler and/or runtime. 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

5 participants