]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/asm: process forward jump to PCALIGN
authorZhouGuangyuan <zhouguangyuan@golangcn.org>
Mon, 18 Aug 2025 11:27:46 +0000 (19:27 +0800)
committerCherry Mui <cherryyz@google.com>
Fri, 22 Aug 2025 01:51:39 +0000 (18:51 -0700)
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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
src/cmd/internal/obj/x86/asm6.go
test/fixedbugs/issue74648.dir/a.s [new file with mode: 0644]
test/fixedbugs/issue74648.dir/x.go [new file with mode: 0644]
test/fixedbugs/issue74648.go [new file with mode: 0644]

index b071bd530d13527d1af5d049e913fd169f19ddb8..03718fbb31c8f554b643bec38279eaa90dbf361a 100644 (file)
@@ -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 (file)
index 0000000..39a8d76
--- /dev/null
@@ -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 (file)
index 0000000..afde832
--- /dev/null
@@ -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 (file)
index 0000000..5db4b85
--- /dev/null
@@ -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