From 1ad30844d9cc0d5792b055f44a6e98759587bbfb Mon Sep 17 00:00:00 2001 From: ZhouGuangyuan Date: Mon, 18 Aug 2025 19:27:46 +0800 Subject: [PATCH] cmd/asm: process forward jump to PCALIGN The forward jump target are not processed when the target is PCALIGN, so process it before emit nops for PCALIGN. Fixes #74648 Change-Id: I690fbfacf79e26d7a37628a2551729b2381616c5 Reviewed-on: https://go-review.googlesource.com/c/go/+/696915 LUCI-TryBot-Result: Go LUCI Reviewed-by: Carlos Amedee Reviewed-by: Cherry Mui --- src/cmd/internal/obj/x86/asm6.go | 25 ++++++++++++------------- test/fixedbugs/issue74648.dir/a.s | 11 +++++++++++ test/fixedbugs/issue74648.dir/x.go | 13 +++++++++++++ test/fixedbugs/issue74648.go | 9 +++++++++ 4 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 test/fixedbugs/issue74648.dir/a.s create mode 100644 test/fixedbugs/issue74648.dir/x.go create mode 100644 test/fixedbugs/issue74648.go diff --git a/src/cmd/internal/obj/x86/asm6.go b/src/cmd/internal/obj/x86/asm6.go index b071bd530d..03718fbb31 100644 --- a/src/cmd/internal/obj/x86/asm6.go +++ b/src/cmd/internal/obj/x86/asm6.go @@ -2120,19 +2120,6 @@ func span6(ctxt *obj.Link, s *obj.LSym, newprog obj.ProgAlloc) { c0 := c c = pjc.padJump(ctxt, s, p, c) - if p.As == obj.APCALIGN || p.As == obj.APCALIGNMAX { - v := obj.AlignmentPadding(c, p, ctxt, s) - if v > 0 { - s.Grow(int64(c) + int64(v)) - fillnop(s.P[c:], int(v)) - } - p.Pc = int64(c) - c += int32(v) - pPrev = p - continue - - } - if maxLoopPad > 0 && p.Back&branchLoopHead != 0 && c&(loopAlign-1) != 0 { // pad with NOPs v := -c & (loopAlign - 1) @@ -2165,6 +2152,18 @@ func span6(ctxt *obj.Link, s *obj.LSym, newprog obj.ProgAlloc) { } } + if p.As == obj.APCALIGN || p.As == obj.APCALIGNMAX { + v := obj.AlignmentPadding(c, p, ctxt, s) + if v > 0 { + s.Grow(int64(c) + int64(v)) + fillnop(s.P[c:], int(v)) + } + p.Pc = int64(c) + c += int32(v) + pPrev = p + continue + } + p.Rel = nil p.Pc = int64(c) diff --git a/test/fixedbugs/issue74648.dir/a.s b/test/fixedbugs/issue74648.dir/a.s new file mode 100644 index 0000000000..39a8d7684c --- /dev/null +++ b/test/fixedbugs/issue74648.dir/a.s @@ -0,0 +1,11 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +TEXT ·F(SB), $0 + JMP prealigned + INT $3 // should never be reached +prealigned: + PCALIGN $0x10 +aligned: + RET diff --git a/test/fixedbugs/issue74648.dir/x.go b/test/fixedbugs/issue74648.dir/x.go new file mode 100644 index 0000000000..afde832d4a --- /dev/null +++ b/test/fixedbugs/issue74648.dir/x.go @@ -0,0 +1,13 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Issue 74648: wrong jump target when using PCALIGN. + +package main + +func F() + +func main() { + F() +} diff --git a/test/fixedbugs/issue74648.go b/test/fixedbugs/issue74648.go new file mode 100644 index 0000000000..5db4b85174 --- /dev/null +++ b/test/fixedbugs/issue74648.go @@ -0,0 +1,9 @@ +// runindir + +//go:build amd64 + +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package ignored