1 // Copyright 2017 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package x86asm 6 7 import ( 8 "encoding/hex" 9 "testing" 10 ) 11 12 func testFormattingSymname(addr uint64) (string, uint64) { 13 switch addr { 14 case 0x424080: 15 return "runtime.printint", 0x424080 16 case 0x4c8068: 17 return "main.A", 0x4c8068 18 } 19 return "", 0 20 } 21 22 func TestFormatting(t *testing.T) { 23 testCases := []struct { 24 PC uint64 25 bytes string 26 27 goSyntax, intelSyntax, gnuSyntax string 28 }{ 29 {0x4816b2, "0f8677010000", 30 "JBE 0x48182f", 31 "jbe 0x48182f", 32 "jbe 0x48182f"}, 33 {0x45065b, "488b442408", 34 "MOVQ 0x8(SP), AX", 35 "mov rax, qword ptr [rsp+0x8]", 36 "mov 0x8(%rsp),%rax"}, 37 {0x450678, "488b05e9790700", 38 "MOVQ main.A(SB), AX", 39 "mov rax, qword ptr [main.A]", 40 "mov main.A,%rax"}, 41 {0x450664, "e8173afdff", 42 "CALL runtime.printint(SB)", 43 "call runtime.printint", 44 "callq runtime.printint"}, 45 {0x45069b, "488d0575d90100", 46 "LEAQ 0x1d975(IP), AX", 47 "lea rax, ptr [rip+0x1d975]", 48 "lea 0x1d975(%rip),%rax"}, 49 } 50 51 for _, testCase := range testCases { 52 t.Logf("%#x %s %s", testCase.PC, testCase.bytes, testCase.goSyntax) 53 bs, _ := hex.DecodeString(testCase.bytes) 54 inst, err := Decode(bs, 64) 55 if err != nil { 56 t.Errorf("decode error %v", err) 57 } 58 if out := GoSyntax(inst, testCase.PC, testFormattingSymname); out != testCase.goSyntax { 59 t.Errorf("GoSyntax: %q", out) 60 } 61 if out := IntelSyntax(inst, testCase.PC, testFormattingSymname); out != testCase.intelSyntax { 62 t.Errorf("IntelSyntax: %q expected: %q", out, testCase.intelSyntax) 63 } 64 if out := GNUSyntax(inst, testCase.PC, testFormattingSymname); out != testCase.gnuSyntax { 65 t.Errorf("GNUSyntax: %q expected: %q", out, testCase.gnuSyntax) 66 } 67 } 68 } 69