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>
This commit is contained in:
fanzha02 2019-12-19 08:15:06 +00:00 committed by Cherry Zhang
parent a7a0f03050
commit 1dbcbcfca4
8 changed files with 114 additions and 25 deletions

View file

@ -447,3 +447,73 @@ func TestStrictDup(t *testing.T) {
t.Errorf("unexpected output:\n%s", out)
}
}
const testFuncAlignSrc = `
package main
import (
"fmt"
"reflect"
)
func alignPc()
func main() {
addr := reflect.ValueOf(alignPc).Pointer()
if (addr % 512) != 0 {
fmt.Printf("expected 512 bytes alignment, got %v\n", addr)
} else {
fmt.Printf("PASS")
}
}
`
const testFuncAlignAsmSrc = `
#include "textflag.h"
TEXT ·alignPc(SB),NOSPLIT, $0-0
MOVD $2, R0
PCALIGN $512
MOVD $3, R1
RET
`
// TestFuncAlign verifies that the address of a function can be aligned
// with a specfic value on arm64.
func TestFuncAlign(t *testing.T) {
if runtime.GOARCH != "arm64" || runtime.GOOS != "linux" {
t.Skip("skipping on non-linux/arm64 platform")
}
testenv.MustHaveGoBuild(t)
tmpdir, err := ioutil.TempDir("", "TestFuncAlign")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
src := filepath.Join(tmpdir, "falign.go")
err = ioutil.WriteFile(src, []byte(testFuncAlignSrc), 0666)
if err != nil {
t.Fatal(err)
}
src = filepath.Join(tmpdir, "falign.s")
err = ioutil.WriteFile(src, []byte(testFuncAlignAsmSrc), 0666)
if err != nil {
t.Fatal(err)
}
// Build and run with old object file format.
cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", "falign")
cmd.Dir = tmpdir
out, err := cmd.CombinedOutput()
if err != nil {
t.Errorf("build failed: %v", err)
}
cmd = exec.Command(tmpdir + "/falign")
out, err = cmd.CombinedOutput()
if err != nil {
t.Errorf("failed to run with err %v, output: %s", err, out)
}
if string(out) != "PASS" {
t.Errorf("unexpected output: %s\n", out)
}
}