From 5ec1457941d1754a583a745259b9c308749c0bd1 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Sun, 17 Nov 2024 19:53:18 -0500 Subject: [PATCH] cmd/compile: avoid static init of strings in FIPS mode Strings have relocations, and data relocations are bad. Other literals are fine. Fixes build failure in pending CL 628776. Change-Id: I7a38bbff9776a365c5823d54c4a00e068dda5d9a Reviewed-on: https://go-review.googlesource.com/c/go/+/628915 Reviewed-by: Cuong Manh Le Reviewed-by: Robert Griesemer Reviewed-by: Cherry Mui LUCI-TryBot-Result: Go LUCI Auto-Submit: Russ Cox --- src/cmd/compile/internal/staticinit/sched.go | 3 +++ src/cmd/compile/internal/walk/complit.go | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/cmd/compile/internal/staticinit/sched.go b/src/cmd/compile/internal/staticinit/sched.go index fae4eb0d8b7..ce2e921771e 100644 --- a/src/cmd/compile/internal/staticinit/sched.go +++ b/src/cmd/compile/internal/staticinit/sched.go @@ -332,6 +332,9 @@ func (s *Schedule) StaticAssign(l *ir.Name, loff int64, r ir.Node, typ *types.Ty if ir.IsZero(r) { return true } + if disableGlobalAddrs && r.Type().IsString() { + return false + } staticdata.InitConst(l, loff, r, int(typ.Size())) return true diff --git a/src/cmd/compile/internal/walk/complit.go b/src/cmd/compile/internal/walk/complit.go index 70750ab0373..6452618f6ce 100644 --- a/src/cmd/compile/internal/walk/complit.go +++ b/src/cmd/compile/internal/walk/complit.go @@ -85,7 +85,9 @@ const ( func getdyn(n ir.Node, top bool) initGenType { switch n.Op() { default: - if ir.IsConstNode(n) { + // Handle constants in linker, except that linker cannot do + // the relocations necessary for string constants in FIPS packages. + if ir.IsConstNode(n) && (!n.Type().IsString() || !base.Ctxt.IsFIPS()) { return initConst } return initDynamic