-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
389 lines (326 loc) · 8.22 KB
/
main.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package main
import (
"bufio"
"bytes"
"debug/dwarf"
"debug/macho"
"encoding/binary"
"fmt"
"io"
"log"
"os"
"path/filepath"
"regexp"
"slices"
"strings"
"text/template"
)
var (
funcmap = template.FuncMap{
"EmitOpcodes": emitOpcodes,
}
functemplate = template.Must(template.New("functempl").Funcs(funcmap).Parse(funcTempl))
headertemplate = template.Must(template.New("headertempl").Parse(headerTempl))
)
const (
m9prefix = "m9:"
funcTempl = `TEXT {{.Symbol.Markup}}
{{EmitOpcodes .Symbol .Data}}
`
headerTempl = `/*
* Generated by mach9 {{.}}; DO NOT EDIT.
*/
#include "textflag.h"
`
)
type symbol struct {
// These fields come from the Mach-O symbol table
Name string
Offset int
Data []byte
Type uint8
// This comes from the DWARF debugging info
DeclLine int
// This comes from the source file
Markup string
}
type templateData struct {
Symbols []symbol
Source []string
LineEntries []dwarf.LineEntry
Invocation string
}
func main() {
log.SetFlags(0)
log.SetPrefix("mach9: ")
if len(os.Args) < 2 {
log.Fatal("No object file provided")
}
mf, err := macho.Open(os.Args[1])
if err != nil {
log.Fatal(err)
}
defer mf.Close()
// Find the __text section, this contains the assembly opcodes
text, textidx := findSectionAndIdx("__text", mf)
if text == nil {
log.Fatal("No __text section")
}
// We need the symbols table to help identify where each function begins
// and ends in the assembly
if mf.Symtab == nil {
log.Fatal("Missing symbol table, can't do anything")
}
// Extract the assembled opcodes
code := make([]byte, text.Size)
if n, err := text.ReadAt(code, 0); n < int(text.Size) || err != nil {
if err == nil {
log.Fatal("Failed to read all bytes in section")
}
log.Fatal(err)
}
var symbols []symbol
for _, sym := range mf.Symtab.Syms {
// See notes.md: symbols which start with l are private link labels and
// can be ignored. I'm not confident so documenting here but not
// implementing.
// Ignore symbols that aren't in the text section
if sym.Sect != uint8(textidx+1) {
continue
}
symbols = append(symbols, symbol{
Name: sym.Name, Offset: int(sym.Value), Type: sym.Type,
})
}
// Add a sentinel that represents the end of the code buffer to simplify the
// next loop.
symbols = append(symbols, symbol{Name: "", Offset: len(code)})
// Sort the symbols in order how they appear in the file
slices.SortFunc(symbols, func(a, b symbol) int {
return int(a.Offset - b.Offset)
})
for i := 0; i < len(symbols)-1; i++ {
start := &symbols[i]
end := &symbols[i+1]
start.Data = code[start.Offset:end.Offset]
}
symbols = slices.DeleteFunc[[]symbol](symbols, func(sym symbol) bool {
// The bottom bit of Type is set if the symbol is an external symbol,
// one that can be referenced by the linker and other programs. See
// page 44 https://github.com/aidansteele/osx-abi-macho-file-format-reference/blob/master/Mach-O_File_Format.pdf
return sym.Type&1 == 0
})
var src []string
cu, le, dwarfOK := parseDWARF(mf, symbols)
if dwarfOK {
if lines, err := readSourceFile(cu); err == nil {
extractDecl(lines, symbols)
src = prepareSource(lines)
}
}
// Every symbol requires m9 markup
invalid := false
for _, sym := range symbols {
if sym.Markup == "" {
fmt.Fprintf(os.Stderr, "Symbol %q missing m9 declaration\n", sym.Name)
invalid = true
}
}
if invalid {
os.Exit(1)
}
outData := &templateData{
Symbols: symbols,
Source: src,
LineEntries: le,
Invocation: strings.Join(os.Args[1:], " "),
}
generateOutput(os.Stdout, outData)
}
func extractDecl(lines []string, symbols []symbol) {
for i := range symbols {
sym := &symbols[i]
line := lines[sym.DeclLine-1]
idx := strings.Index(line, m9prefix)
if idx != -1 {
idx += len(m9prefix)
sym.Markup = strings.TrimLeft(line[idx:], " \t")
}
}
}
func generateOutput(w io.Writer, data *templateData) error {
if err := headertemplate.Execute(w, data.Invocation); err != nil {
return err
}
for _, sym := range data.Symbols {
td := struct {
Symbol *symbol
Data *templateData
}{&sym, data}
if err := functemplate.Execute(w, &td); err != nil {
return err
}
}
return nil
}
// Returns the path to the source file for the compile unit and whether DWARF
// info was available. It also updates the symbols in the symmap with the
// declaration line of the symbol.
func parseDWARF(mf *macho.File, symbols []symbol) (string, []dwarf.LineEntry, bool) {
dwarfdata, err := mf.DWARF()
if err != nil {
return "", nil, false
}
var cuPath string
var lentries []dwarf.LineEntry
reader := dwarfdata.Reader()
for {
entry, err := reader.Next()
if err != nil {
return "", nil, false
}
if entry == nil {
break
}
switch entry.Tag {
case dwarf.TagCompileUnit:
cuName, ok := entry.Val(dwarf.AttrName).(string)
if !ok {
continue
}
cuCompDir, ok := entry.Val(dwarf.AttrCompDir).(string)
if !ok {
continue
}
cuPath = filepath.Join(cuCompDir, cuName)
// Read out line entry table for this CU
lr, err := dwarfdata.LineReader(entry)
if err != nil {
continue
}
var lentry dwarf.LineEntry
for lr.Next(&lentry) != io.EOF {
if !lentry.EndSequence {
lentries = append(lentries, lentry)
}
}
case dwarf.TagLabel:
labelName, ok := entry.Val(dwarf.AttrName).(string)
if !ok {
continue
}
declLine, ok := entry.Val(dwarf.AttrDeclLine).(int64)
if !ok {
continue
}
for i := range symbols {
sym := &symbols[i]
if sym.Name == labelName {
sym.DeclLine = int(declLine)
break
}
}
}
}
return cuPath, lentries, true
}
// Builds a string that contains the opcodes as literal bytes in Plan9 assembler format
func emitOpcodes(sym *symbol, data *templateData) string {
builder := strings.Builder{}
address := sym.Offset
// TODO - Simplify this code, can the three steps be combined?
code := sym.Data
n := len(code)
off := 0
s := n / 4
if s > 0 {
for i := 0; i < s; i++ {
line := findSourceLine(address+off, data)
opcodes := code[off : off+4]
builder.WriteString(fmt.Sprintf("\tWORD $%#8x", binary.LittleEndian.Uint32(opcodes)))
if line != "" {
builder.WriteString(" // " + line)
}
builder.WriteString("\n")
off += 4
}
n -= s * 4
}
s = n / 2
if s > 0 {
for i := 0; i < s; i++ {
line := findSourceLine(address+off, data)
opcodes := code[off : off+2]
builder.WriteString(fmt.Sprintf("\tWORD $%#4x\n", binary.LittleEndian.Uint16(opcodes)))
if line != "" {
builder.WriteString(" // " + line)
}
builder.WriteString("\n")
off += 2
}
n -= s * 2
}
for i := 0; i < n; i++ {
line := findSourceLine(address+off, data)
opcode := code[off]
builder.WriteString(fmt.Sprintf("\tWORD $%#2x\n", opcode))
if line != "" {
builder.WriteString(" // " + line)
}
builder.WriteString("\n")
off++
}
return builder.String()
}
func findSourceLine(offset int, data *templateData) string {
for i := range data.LineEntries {
if uint64(offset) == data.LineEntries[i].Address {
return data.Source[data.LineEntries[i].Line]
}
}
return ""
}
func findSectionAndIdx(name string, mf *macho.File) (*macho.Section, int) {
for i, s := range mf.Sections {
if s.Name == name {
return s, i
}
}
return nil, -1
}
// Returns the source file as an array of strings, one for each line of the
// file. The first element of the array is empty to allow convenient 1-based
// indexing.
func readSourceFile(file string) ([]string, error) {
src, err := os.ReadFile(file)
if err != nil {
return nil, err
}
lines := make([]string, 1)
scanner := bufio.NewScanner(bytes.NewBuffer(src))
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, nil
}
var dupws = regexp.MustCompile(`\s+`)
// This removes comment lines and duplicate whitespace to compact the source
func prepareSource(lines []string) []string {
out := make([]string, len(lines))
for i, l := range lines {
if len(l) > 0 {
l = deleteAfterComment(l)
out[i] = strings.TrimPrefix(dupws.ReplaceAllString(l, " "), " ")
}
}
return out
}
func deleteAfterComment(text string) string {
for _, delim := range []string{"//", ";"} {
index := strings.Index(text, delim)
if index != -1 {
return strings.TrimRight(text[:index], " ")
}
}
return text
}