-
Notifications
You must be signed in to change notification settings - Fork 8
/
compile.go
444 lines (368 loc) · 11.7 KB
/
compile.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"sync"
"unicode"
)
var useBatching bool
var useWarnings bool
var compileWaitGroup sync.WaitGroup
var toCompileCount uint32
var compileJobs []compileJob
var jobMtx sync.Mutex
type symbolInfo struct {
name string
linePos int
}
type labelInfo struct {
name string
num int
linePos int
}
type compileResponse struct {
code []byte
address string
}
type compileJob struct {
inputFile string
tempFile string
outFile string
addressExp string
response chan compileResponse
}
func execBatchCompile(jobs []compileJob) {
const asCmdLinux string = "powerpc-eabi-as"
const objcopyCmdLinux string = "powerpc-eabi-objcopy"
deleteFile := func(fp string) {
defer compileWaitGroup.Done()
os.Remove(fp)
}
outputFilePath := path.Join(argConfig.ProjectRoot, "compiled.elf")
compileWaitGroup.Add(1)
defer deleteFile(outputFilePath)
// Generate temp file names
for idx, job := range jobs {
file := job.inputFile
fileExt := filepath.Ext(file)
fileNoExt := file[0 : len(file)-len(fileExt)]
jobs[idx].tempFile = fmt.Sprintf("%s-file%d.asmtemp", fileNoExt, idx)
jobs[idx].outFile = fmt.Sprintf("%s-file%d.out", fileNoExt, idx)
}
// Set base args
args := []string{"-a32", "-mbig", "-mregnames", "-mgekko"}
if !useWarnings {
args = append(args, "--fatal-warnings")
}
// If defsym is defined, add it to the args
if argConfig.DefSym != "" {
args = append(args, "-defsym", argConfig.DefSym)
}
args = append(args, "-I", argConfig.ProjectRoot)
// Add local paths to look at when resolving includes
for _, job := range jobs {
file := job.inputFile
fileDir := filepath.Dir(file)
args = append(args, "-I", fileDir)
}
// Set output file
args = append(args, "-o", outputFilePath)
// Iterate through jobs, create temp files, and add them to the files to assemble
for idx, job := range jobs {
compileWaitGroup.Add(1)
defer deleteFile(job.tempFile)
buildTempAsmFile(job.inputFile, job.addressExp, job.tempFile, fmt.Sprintf(".file%d", idx))
args = append(args, job.tempFile)
}
cmd := exec.Command(asCmdLinux, args...)
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("Failed to compile files")
fmt.Printf("%s", output)
panic("as failure")
}
args = []string{outputFilePath}
for idx, job := range jobs {
compileWaitGroup.Add(1)
defer deleteFile(job.outFile)
args = append(args, "--dump-section", fmt.Sprintf(".file%d=%s", idx, job.outFile))
}
cmd = exec.Command(objcopyCmdLinux, args...)
output, err = cmd.CombinedOutput()
if err != nil {
fmt.Printf("Failed to pull extract code sections\n")
fmt.Printf("%s", output)
panic("objcopy failure")
}
for _, job := range jobs {
contents, err := ioutil.ReadFile(job.outFile)
if err != nil {
log.Panicf("Failed to read compiled file %s\n%s\n", job.outFile, err.Error())
}
code := contents[:len(contents)-4]
address := contents[len(contents)-4:]
if address[0] != 0x80 && address[0] != 0x81 {
log.Panicf(
"Injection address in file %s evaluated to a value that does not start with 0x80 or 0x81"+
", probably an invalid address\n",
job.inputFile,
)
}
job.response <- compileResponse{code: code, address: fmt.Sprintf("%x", address)}
}
}
func batchCompile(file, addressExp string) ([]byte, string) {
c := make(chan compileResponse)
jobMtx.Lock()
compileJobs = append(compileJobs, compileJob{
inputFile: file,
addressExp: addressExp,
response: c,
})
if len(compileJobs) >= int(toCompileCount) {
go execBatchCompile(compileJobs)
}
jobMtx.Unlock()
result := <-c
return result.code, result.address
}
func compile(file, addressExp string) ([]byte, string) {
if useBatching {
return batchCompile(file, addressExp)
}
fileExt := filepath.Ext(file)
outputFilePath := file[0:len(file)-len(fileExt)] + ".out"
compileFilePath := file[0:len(file)-len(fileExt)] + ".asmtemp"
// Clean up files
defer os.Remove(outputFilePath)
defer os.Remove(compileFilePath)
// First we are gonna load all the data from file and write it into temp file
// Technically this shouldn't be necessary but for some reason if the last line
// or the asm file has one of more spaces at the end and no new line, the last
// instruction is ignored and not compiled
buildTempAsmFile(file, addressExp, compileFilePath, "")
fileDir := filepath.Dir(file)
const asCmdLinux string = "powerpc-eabi-as"
const objcopyCmdLinux string = "powerpc-eabi-objcopy"
// Set base args
args := []string{"-a32", "-mbig", "-mregnames", "-mgekko"}
if !useWarnings {
args = append(args, "--fatal-warnings")
}
// If defsym is defined, add it to the args
if argConfig.DefSym != "" {
args = append(args, "-defsym", argConfig.DefSym)
}
// Add paths to look at when resolving includes
args = append(args, "-I", fileDir, "-I", argConfig.ProjectRoot)
// Set output file
args = append(args, "-o", outputFilePath, compileFilePath)
cmd := exec.Command(asCmdLinux, args...)
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("Failed to compile file: %s\n", file)
fmt.Printf("%s", output)
panic("as failure")
}
cmd = exec.Command(objcopyCmdLinux, "-O", "binary", outputFilePath, outputFilePath)
output, err = cmd.CombinedOutput()
if err != nil {
fmt.Printf("Failed to pull out .text section: %s\n", file)
fmt.Printf("%s", output)
panic("objcopy failure")
}
contents, err := ioutil.ReadFile(outputFilePath)
if err != nil {
log.Panicf("Failed to read compiled file %s\n%s\n", file, err.Error())
}
code := contents[:len(contents)-4]
address := contents[len(contents)-4:]
if address[0] != 0x80 && address[0] != 0x81 {
log.Panicf(
"Injection address in file %s evaluated to a value that does not start with 0x80 or 0x81"+
", probably an invalid address\n",
file,
)
}
return code, fmt.Sprintf("%x", address)
}
func isSymbolRune(r rune) bool {
return unicode.IsLetter(r) || unicode.IsNumber(r) || r == '_'
}
func splitAny(s string, seps string) []string {
splitter := func(r rune) bool {
return strings.ContainsRune(seps, r)
}
return strings.FieldsFunc(s, splitter)
}
func removeComments(asmContents []byte) []byte {
body := string(asmContents)
// Remove block comments
newBody := ""
idx := strings.Index(body, "/*")
for idx > -1 {
newBody += body[:idx]
body = body[idx:]
end := strings.Index(body, "*/")
if end > -1 {
body = body[end+2:]
}
idx = strings.Index(body, "/*")
}
newBody += body
// Remove line comments
lines := strings.Split(newBody, "\n")
newLines := []string{}
for _, line := range lines {
// Remove any comments
commentSplit := strings.Split(line, "#")
if len(commentSplit) == 0 {
newLines = append(newLines, line)
continue
}
newLines = append(newLines, strings.TrimSpace(commentSplit[0]))
}
return []byte(strings.Join(newLines, "\n"))
}
func isolateLabelNames(asmContents []byte) []byte {
// Start logic to isolate label names
// First we're going to extract all label positions as well as replace them with a number
// based label which functions as a local label. This will prevent errors from using
// the same label name in multiple files
lines := strings.Split(string(asmContents), "\n")
labels := map[string]labelInfo{}
newLines := []string{}
labelIdx := 100 // Start at 100 because hopefully no macros will use labels that high
for lineNum, line := range lines {
isLabel := len(line) > 0 && line[len(line)-1] == ':'
if !isLabel {
newLines = append(newLines, line)
continue
}
name := line[:len(line)-1]
labels[name] = labelInfo{name, labelIdx, lineNum}
newLines = append(newLines, fmt.Sprintf("%d:", labelIdx))
labelIdx += 1
}
// Now let's convert all the branch instructions we can find to use the local labels
// instead of the original label names
// TODO: It might be possible to throw errors here if referencing a label that doesn't exist
// TODO: I didn't do it yet because currently instructions like `branchl r12, ...` might
// TODO: trigger the easy form of detection. We'd probably have to detect all possible branch
// TODO: instructions in order to do this
finalLines := []string{}
for lineNum, line := range newLines {
parts := splitAny(line, " \t")
if len(parts) == 0 {
finalLines = append(finalLines, line)
continue
}
label := parts[len(parts)-1]
li, labelExists := labels[label]
isBranch := len(parts) >= 2 && line[0] == 'b' && labelExists
if !isBranch {
finalLines = append(finalLines, line)
continue
}
dir := "f"
if lineNum > li.linePos {
dir = "b"
}
parts[len(parts)-1] = fmt.Sprintf("%d%s", li.num, dir)
finalLines = append(finalLines, strings.Join(parts, " "))
}
return []byte(strings.Join(finalLines, "\n"))
}
func isolateSymbolNames(asmContents []byte, section string) []byte {
lines := strings.Split(string(asmContents), "\n")
symbolMap := map[string][]symbolInfo{}
newLines := []string{}
for idx, line := range lines {
parts := splitAny(line, " \t,")
if len(parts) == 0 {
newLines = append(newLines, line)
continue
}
isSet := parts[0] == ".set" && len(parts) >= 3
if !isSet {
newLines = append(newLines, line)
continue
}
newSymbol := fmt.Sprintf("__%s_symbol_%d", section, idx)
// Add this symbol to map
_, exists := symbolMap[parts[1]]
if !exists {
symbolMap[parts[1]] = []symbolInfo{}
}
symbolMap[parts[1]] = append(symbolMap[parts[1]], symbolInfo{newSymbol, idx})
newLines = append(newLines, strings.Replace(line, parts[1], newSymbol, 1))
}
finalLines := []string{}
for lineIdx, line := range newLines {
if len(line) == 0 {
finalLines = append(finalLines, line)
continue
}
symbolParts := strings.FieldsFunc(line, func(r rune) bool { return !isSymbolRune(r) })
connectingParts := strings.FieldsFunc(line, isSymbolRune)
for symbolIdx, symbol := range symbolParts {
instances, exists := symbolMap[symbol]
if !exists {
continue
}
remap := instances[0].name
for _, instance := range instances {
if instance.linePos > lineIdx {
break
}
remap = instance.name
}
symbolParts[symbolIdx] = remap
}
reconnected := []string{}
first, second := symbolParts, connectingParts
shouldSwap := !isSymbolRune(rune(line[0]))
if shouldSwap {
first, second = connectingParts, symbolParts
}
for partIdx, part1 := range first {
reconnected = append(reconnected, part1)
if partIdx < len(second) {
reconnected = append(reconnected, second[partIdx])
}
}
finalLines = append(finalLines, strings.Join(reconnected, ""))
}
return []byte(strings.Join(finalLines, "\n"))
}
func buildTempAsmFile(sourceFilePath, addressExp, targetFilePath, section string) {
asmContents, err := ioutil.ReadFile(sourceFilePath)
if err != nil {
log.Panicf("Failed to read asm file: %s\n%s\n", sourceFilePath, err.Error())
}
// If section provided, we need to take some precautions to isolate the code from others
if section != "" {
// Add the section label at the top so the code can be extracted individually
asmContents = append([]byte(fmt.Sprintf(".section %s\n", section)), asmContents...)
asmContents = removeComments(asmContents)
asmContents = isolateLabelNames(asmContents)
asmContents = isolateSymbolNames(asmContents, section)
}
// Add new line before .set for address
asmContents = append(asmContents, []byte("\n")...)
// Add .set to get file injection address
setLine := fmt.Sprintf(".long %s\n", addressExp)
asmContents = append(asmContents, []byte(setLine)...)
// Explicitly add a new line at the end of the file, which should prevent line skip
asmContents = append(asmContents, []byte("\n")...)
err = ioutil.WriteFile(targetFilePath, asmContents, 0644)
if err != nil {
log.Panicf("Failed to write temporary asm file: %s\n%s\n", targetFilePath, err.Error())
}
}