aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/internal/obj/arm64
diff options
context:
space:
mode:
authorfanzha02 <fannie.zhang@arm.com>2019-12-19 08:15:06 +0000
committerCherry Zhang <cherryyz@google.com>2020-04-02 15:05:15 +0000
commit1dbcbcfca4692f67db7de2c1ff6a5ee59511cfa4 (patch)
tree3a2b59309bbecfc6af7653bb29c563db2e254378 /src/cmd/internal/obj/arm64
parenta7a0f0305035948f4c86e08e6e64409ab11a6f67 (diff)
downloadgo-1dbcbcfca4692f67db7de2c1ff6a5ee59511cfa4.tar.xz
cmd/asm: align an instruction or a function's address
Recently, the gVisor project needs an instruction's address with 128 bytes alignment to fit the architecture requirement for interrupt table. This patch allows aligning an instruction's address to be aligned to a specific value (2^n and in the range [8, 2048]) The main changes include: 1. Adds a new element in the FuncInfo structure defined in cmd/internal/obj/link.go file to record the alignment information. 2. Adds a new element in the Func structure defined in cmd/internal/goobj/read.go file to read the alignment information. 3. Adds the assembler support to align an intruction's offset with a specific value (2^n and in the range [8, 2048]). e.g. "PCALIGN $256" indicates that the next instruction should be aligned to 256 bytes. 4. An instruction's alignment is relative to the start of the function where this instruction is located, so the function's address must be aligned to the same or coarser boundary. This CL also adds a test. Change-Id: I9b365c111b3a12f767728f1b45aa0c00f073c37d Reviewed-on: https://go-review.googlesource.com/c/go/+/226997 Reviewed-by: Bryan C. Mills <bcmills@google.com> Reviewed-by: Cherry Zhang <cherryyz@google.com> Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/cmd/internal/obj/arm64')
-rw-r--r--src/cmd/internal/obj/arm64/asm7.go33
-rw-r--r--src/cmd/internal/obj/arm64/asm_test.go27
2 files changed, 35 insertions, 25 deletions
diff --git a/src/cmd/internal/obj/arm64/asm7.go b/src/cmd/internal/obj/arm64/asm7.go
index e8b092a2a8..8e5b598084 100644
--- a/src/cmd/internal/obj/arm64/asm7.go
+++ b/src/cmd/internal/obj/arm64/asm7.go
@@ -886,25 +886,10 @@ const OP_NOOP = 0xd503201f
// align code to a certain length by padding bytes.
func pcAlignPadLength(pc int64, alignedValue int64, ctxt *obj.Link) int {
- switch alignedValue {
- case 8:
- if pc%8 == 4 {
- return 4
- }
- case 16:
- switch pc % 16 {
- case 4:
- return 12
- case 8:
- return 8
- case 12:
- return 4
- }
- default:
- ctxt.Diag("Unexpected alignment: %d for PCALIGN directive\n", alignedValue)
+ if !((alignedValue&(alignedValue-1) == 0) && 8 <= alignedValue && alignedValue <= 2048) {
+ ctxt.Diag("alignment value of an instruction must be a power of two and in the range [8, 2048], got %d\n", alignedValue)
}
-
- return 0
+ return int(-pc & (alignedValue - 1))
}
func span7(ctxt *obj.Link, cursym *obj.LSym, newprog obj.ProgAlloc) {
@@ -940,8 +925,12 @@ func span7(ctxt *obj.Link, cursym *obj.LSym, newprog obj.ProgAlloc) {
if m == 0 {
switch p.As {
case obj.APCALIGN:
- a := p.From.Offset
- m = pcAlignPadLength(pc, a, ctxt)
+ alignedValue := p.From.Offset
+ m = pcAlignPadLength(pc, alignedValue, ctxt)
+ // Update the current text symbol alignment value.
+ if int32(alignedValue) > cursym.Func.Align {
+ cursym.Func.Align = int32(alignedValue)
+ }
break
case obj.ANOP, obj.AFUNCDATA, obj.APCDATA:
continue
@@ -1017,8 +1006,8 @@ func span7(ctxt *obj.Link, cursym *obj.LSym, newprog obj.ProgAlloc) {
if m == 0 {
switch p.As {
case obj.APCALIGN:
- a := p.From.Offset
- m = pcAlignPadLength(pc, a, ctxt)
+ alignedValue := p.From.Offset
+ m = pcAlignPadLength(pc, alignedValue, ctxt)
break
case obj.ANOP, obj.AFUNCDATA, obj.APCDATA:
continue
diff --git a/src/cmd/internal/obj/arm64/asm_test.go b/src/cmd/internal/obj/arm64/asm_test.go
index 1691828739..9efdb0217f 100644
--- a/src/cmd/internal/obj/arm64/asm_test.go
+++ b/src/cmd/internal/obj/arm64/asm_test.go
@@ -18,7 +18,9 @@ import (
// TestLarge generates a very large file to verify that large
// program builds successfully, in particular, too-far
-// conditional branches are fixed.
+// conditional branches are fixed, and also verify that the
+// instruction's pc can be correctly aligned even when branches
+// need to be fixed.
func TestLarge(t *testing.T) {
if testing.Short() {
t.Skip("Skip in short mode")
@@ -41,11 +43,28 @@ func TestLarge(t *testing.T) {
t.Fatalf("can't write output: %v\n", err)
}
- // build generated file
- cmd := exec.Command(testenv.GoToolPath(t), "tool", "asm", "-o", filepath.Join(dir, "x.o"), tmpfile)
+ pattern := `0x0080\s00128\s\(.*\)\tMOVD\t\$3,\sR3`
+
+ // assemble generated file
+ cmd := exec.Command(testenv.GoToolPath(t), "tool", "asm", "-S", "-o", filepath.Join(dir, "test.o"), tmpfile)
cmd.Env = append(os.Environ(), "GOARCH=arm64", "GOOS=linux")
out, err := cmd.CombinedOutput()
if err != nil {
+ t.Errorf("Assemble failed: %v, output: %s", err, out)
+ }
+ matched, err := regexp.MatchString(pattern, string(out))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !matched {
+ t.Errorf("The alignment is not correct: %t, output:%s\n", matched, out)
+ }
+
+ // build generated file
+ cmd = exec.Command(testenv.GoToolPath(t), "tool", "asm", "-o", filepath.Join(dir, "x.o"), tmpfile)
+ cmd.Env = append(os.Environ(), "GOARCH=arm64", "GOOS=linux")
+ out, err = cmd.CombinedOutput()
+ if err != nil {
t.Errorf("Build failed: %v, output: %s", err, out)
}
}
@@ -56,6 +75,8 @@ func gen(buf *bytes.Buffer) {
fmt.Fprintln(buf, "TBZ $5, R0, label")
fmt.Fprintln(buf, "CBZ R0, label")
fmt.Fprintln(buf, "BEQ label")
+ fmt.Fprintln(buf, "PCALIGN $128")
+ fmt.Fprintln(buf, "MOVD $3, R3")
for i := 0; i < 1<<19; i++ {
fmt.Fprintln(buf, "MOVD R0, R1")
}