diff options
| author | Joel Sing <joel@sing.id.au> | 2021-09-26 19:21:29 +1000 |
|---|---|---|
| committer | Joel Sing <joel@sing.id.au> | 2021-11-03 09:08:46 +0000 |
| commit | 5b213178e72b6031c9ec790bf9fe784c66b397e3 (patch) | |
| tree | 2796033f0ff66b84cad1c3cb26292f6d00d29c6f /src/cmd/asm | |
| parent | 519c0a2323700934cbec97b75df92917108548be (diff) | |
| download | go-5b213178e72b6031c9ec790bf9fe784c66b397e3.tar.xz | |
cmd/asm,cmd/compile,runtime: stop using X3 (aka GP) on riscv64
The X3 (aka GP) register will potentially be loaded with the __global_pointer$ symbol
during program start up (usually by the dynamic linker). As such, non-Go code may depend
on the contents of GP and calculate offsets based on it, including code called via cgo
and signal handlers installed by non-Go code. As such, stop using the X3 register so
that there are fewer issues interacting between Go and non-Go code.
While here remove the X4 (TP) name from the assembler such that any references must
use the 'TP' name. This should reduce the likelihood of accidental use (like we do
for the 'g' register). The same applies for X3 (GP) when the -shared flag is given.
Updates #47100
Change-Id: I72e82b5ca3f80c46a781781345ca0432a4111b74
Reviewed-on: https://go-review.googlesource.com/c/go/+/351859
Trust: Joel Sing <joel@sing.id.au>
Run-TryBot: Joel Sing <joel@sing.id.au>
Reviewed-by: Cherry Mui <cherryyz@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Diffstat (limited to 'src/cmd/asm')
| -rw-r--r-- | src/cmd/asm/internal/arch/arch.go | 14 | ||||
| -rw-r--r-- | src/cmd/asm/internal/asm/operand_test.go | 2 | ||||
| -rw-r--r-- | src/cmd/asm/main.go | 6 |
3 files changed, 14 insertions, 8 deletions
diff --git a/src/cmd/asm/internal/arch/arch.go b/src/cmd/asm/internal/arch/arch.go index 8c71b79965..4d374cb828 100644 --- a/src/cmd/asm/internal/arch/arch.go +++ b/src/cmd/asm/internal/arch/arch.go @@ -50,7 +50,7 @@ func nilRegisterNumber(name string, n int16) (int16, bool) { // Set configures the architecture specified by GOARCH and returns its representation. // It returns nil if GOARCH is not recognized. -func Set(GOARCH string) *Arch { +func Set(GOARCH string, shared bool) *Arch { switch GOARCH { case "386": return archX86(&x86.Link386) @@ -73,7 +73,7 @@ func Set(GOARCH string) *Arch { case "ppc64le": return archPPC64(&ppc64.Linkppc64le) case "riscv64": - return archRISCV64() + return archRISCV64(shared) case "s390x": return archS390x() case "wasm": @@ -541,12 +541,18 @@ func archMips64(linkArch *obj.LinkArch) *Arch { } } -func archRISCV64() *Arch { +func archRISCV64(shared bool) *Arch { register := make(map[string]int16) // Standard register names. for i := riscv.REG_X0; i <= riscv.REG_X31; i++ { - if i == riscv.REG_G { + // Disallow X3 in shared mode, as this will likely be used as the + // GP register, which could result in problems in non-Go code, + // including signal handlers. + if shared && i == riscv.REG_GP { + continue + } + if i == riscv.REG_TP || i == riscv.REG_G { continue } name := fmt.Sprintf("X%d", i-riscv.REG_X0) diff --git a/src/cmd/asm/internal/asm/operand_test.go b/src/cmd/asm/internal/asm/operand_test.go index 8ef02b1a0e..c1295a0c42 100644 --- a/src/cmd/asm/internal/asm/operand_test.go +++ b/src/cmd/asm/internal/asm/operand_test.go @@ -19,7 +19,7 @@ import ( func setArch(goarch string) (*arch.Arch, *obj.Link) { buildcfg.GOOS = "linux" // obj can handle this OS for all architectures. buildcfg.GOARCH = goarch - architecture := arch.Set(goarch) + architecture := arch.Set(goarch, false) if architecture == nil { panic("asm: unrecognized architecture " + goarch) } diff --git a/src/cmd/asm/main.go b/src/cmd/asm/main.go index 043bc696e5..3e32aa3d7d 100644 --- a/src/cmd/asm/main.go +++ b/src/cmd/asm/main.go @@ -29,13 +29,13 @@ func main() { buildcfg.Check() GOARCH := buildcfg.GOARCH - architecture := arch.Set(GOARCH) + flags.Parse() + + architecture := arch.Set(GOARCH, *flags.Shared || *flags.Dynlink) if architecture == nil { log.Fatalf("unrecognized architecture %s", GOARCH) } - flags.Parse() - ctxt := obj.Linknew(architecture.LinkArch) ctxt.Debugasm = flags.PrintOut ctxt.Debugvlog = flags.DebugV |
