This repository has been archived by the owner on Oct 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
yasm.go
176 lines (150 loc) · 4.18 KB
/
yasm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"unicode"
)
///////////////////////////////////////////////////////////////////////////////
//
// Y A S M S U P P O R T
//
///////////////////////////////////////////////////////////////////////////////
//
// yasm-assemble-disassemble-roundtrip-sse.txt
//
// franks-mbp:sse frankw$ more assembly.asm
// [bits 64]
//
// VPXOR YMM4, YMM2, YMM3 ; X4: Result
// franks-mbp:sse frankw$ yasm assembly.asm
// franks-mbp:sse frankw$ hexdump -C assembly
// 00000000 c5 ed ef e3 |....|
// 00000004
// franks-mbp:sse frankw$ echo 'lbl: db 0xc5, 0xed, 0xef, 0xe3' | yasm -f elf - -o assembly.o
// franks-mbp:sse frankw$ gobjdump -d -M intel assembly.o
//
// assembly.o: file format elf32-i386
//
//
// Disassembly of section .text:
//
// 00000000 <.text>:
// 0: c5 ed ef e3 vpxor ymm4,ymm2,ymm3
func yasm(instructions []Instruction) error {
for i, ins := range instructions {
assembled, opcodes, err := yasmSingle(ins.instruction, ins.lineno, ins.commentPos, ins.inDefine)
if err != nil {
return err
}
instructions[i].assembled = assembled
instructions[i].opcodes = make([]byte, len(opcodes))
copy(instructions[i].opcodes[:], opcodes)
}
return nil
}
func yasmSingle(instr string, lineno, commentPos int, inDefine bool) (string, []byte, error) {
instrFields := strings.Split(instr, "/*")
content := []byte("[bits 64]\n" + instrFields[0])
tmpfile, err := ioutil.TempFile("", "asm2plan9s")
if err != nil {
return "", nil, err
}
if _, err := tmpfile.Write(content); err != nil {
return "", nil, err
}
if err := tmpfile.Close(); err != nil {
return "", nil, err
}
asmFile := tmpfile.Name() + ".asm"
objFile := tmpfile.Name() + ".obj"
os.Rename(tmpfile.Name(), asmFile)
defer os.Remove(asmFile) // clean up
defer os.Remove(objFile) // clean up
app := "yasm"
arg0 := "-o"
arg1 := objFile
arg2 := asmFile
cmd := exec.Command(app, arg0, arg1, arg2)
cmb, err := cmd.CombinedOutput()
if err != nil {
if len(string(cmb)) == 0 { // command invocation failed
return "", nil, errors.New("exec error: YASM not installed?")
}
yasmErrs := strings.Split(string(cmb)[len(asmFile)+1:], ":")
yasmErr := strings.Join(yasmErrs[1:], ":")
return "", nil, errors.New(fmt.Sprintf("YASM error (line %d for '%s'):", lineno+1, strings.TrimSpace(instr)) + yasmErr)
}
return toPlan9sYasm(objFile, instr, commentPos, inDefine)
}
func toPlan9sYasm(objFile, instr string, commentPos int, inDefine bool) (string, []byte, error) {
opcodes, err := ioutil.ReadFile(objFile)
if err != nil {
return "", nil, err
}
s, err := toPlan9s(opcodes, instr, commentPos, inDefine)
return s, opcodes, err
}
func toPlan9s(opcodes []byte, instr string, commentPos int, inDefine bool) (string, error) {
sline := " "
i := 0
// First do QUADs (as many as needed)
for ; len(opcodes) >= 8; i++ {
if i != 0 {
sline += "; "
}
sline += fmt.Sprintf("QUAD $0x%02x%02x%02x%02x%02x%02x%02x%02x", opcodes[7], opcodes[6], opcodes[5], opcodes[4], opcodes[3], opcodes[2], opcodes[1], opcodes[0])
opcodes = opcodes[8:]
}
// Then do LONGs (as many as needed)
for ; len(opcodes) >= 4; i++ {
if i != 0 {
sline += "; "
}
sline += fmt.Sprintf("LONG $0x%02x%02x%02x%02x", opcodes[3], opcodes[2], opcodes[1], opcodes[0])
opcodes = opcodes[4:]
}
// Then do a WORD (if needed)
if len(opcodes) >= 2 {
if i != 0 {
sline += "; "
}
sline += fmt.Sprintf("WORD $0x%02x%02x", opcodes[1], opcodes[0])
i++
opcodes = opcodes[2:]
}
// And close with a BYTE (if needed)
if len(opcodes) == 1 {
if i != 0 {
sline += "; "
}
sline += fmt.Sprintf("BYTE $0x%02x", opcodes[0])
i++
opcodes = opcodes[1:]
}
if inDefine {
if commentPos > commentPos-2-len(sline) {
if commentPos-2-len(sline) > 0 {
sline += strings.Repeat(" ", commentPos-2-len(sline))
}
} else {
sline += " "
}
sline += `\ `
} else {
if commentPos > len(sline) {
if commentPos-len(sline) > 0 {
sline += strings.Repeat(" ", commentPos-len(sline))
}
} else {
sline += " "
}
}
if instr != "" {
sline += "//" + instr
}
return strings.TrimRightFunc(sline, unicode.IsSpace), nil
}