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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
|
#!/usr/bin/env python
# Kamek - build tool for custom C++ code in New Super Mario Bros. Wii
# All rights reserved (c) Treeki 2010 - 2012
# Header files compiled by Treeki, Tempus and megazig
# Requires PyYAML and pyelftools
version_str = 'Kamek 0.2 by Treeki'
import binascii
import os
import os.path
import shutil
import struct
import subprocess
import sys
import tempfile
import yaml
import elftools.elf.elffile
import hooks
u32 = struct.Struct('>I')
verbose = True
use_rels = True
use_mw = False
use_wine = False
mw_path = ''
gcc_path = ''
gcc_type = 'powerpc-eabi'
show_cmd = False
delete_temp = True
override_config_file = None
only_build = None
fast_hack = False
def parse_cmd_options():
global use_rels, use_mw, use_wine, show_cmd, delete_temp, only_build, fast_hack
global override_config_file, gcc_type, gcc_path, mw_path
if '--no-rels' in sys.argv:
use_rels = False
if '--use-mw' in sys.argv:
use_mw = True
if '--use-wine' in sys.argv:
use_wine = True
if '--show-cmd' in sys.argv:
show_cmd = True
if '--keep-temp' in sys.argv:
delete_temp = False
if '--fast-hack' in sys.argv:
fast_hack = True
only_build = []
for arg in sys.argv:
if arg.startswith('--configs='):
override_config_file = arg[10:]
if arg.startswith('--build='):
only_build.append(arg[8:])
if arg.startswith('--gcc-type='):
gcc_type = arg[11:]
if arg.startswith('--gcc-path='):
gcc_path = arg[11:] + '/'
if arg.startswith('--mw-path='):
mw_path = arg[10:] + '/'
if len(only_build) == 0:
only_build = None
def print_debug(s):
if verbose: print '* '+str(s)
def read_configs(filename):
with open(filename, 'r') as f:
data = f.read()
return yaml.safe_load(data)
current_unique_id = 0
def generate_unique_id():
# this is used for temporary filenames, to ensure that .o files
# do not overwrite each other
global current_unique_id
current_unique_id += 1
return current_unique_id
def align_addr_up(addr, align):
align -= 1
return (addr + align) & ~align
def generate_riiv_mempatch(offset, data):
return '<memory offset="0x%08X" value="%s" />' % (offset, binascii.hexlify(data))
def generate_ocarina_patch(destOffset, data):
out = []
count = len(data)
sourceOffset = 0
destOffset -= 0x80000000
for i in xrange(count >> 2):
out.append('%08X %s' % (destOffset | 0x4000000, binascii.hexlify(data[sourceOffset:sourceOffset+4])))
sourceOffset += 4
destOffset += 4
# take care
remainder = count % 4
if remainder == 3:
out.append('%08X 0000%s' % (destOffset | 0x2000000, binascii.hexlify(data[sourceOffset:sourceOffset+2])))
out.append('%08X 000000%s' % (destOffset, binascii.hexlify(data[sourceOffset+2])))
elif remainder == 2:
out.append('%08X 0000%s' % (destOffset | 0x2000000, binascii.hexlify(data[sourceOffset:sourceOffset+2])))
elif remainder == 1:
out.append('%08X 000000%s' % (destOffset, binascii.hexlify(data[sourceOffset])))
return '\n'.join(out)
def generate_kamek_patches(patchlist):
kamekpatch = ''
for patch in patchlist:
if len(patch[1]) > 4:
# block patch
kamekpatch += u32.pack(align_addr_up(len(patch[1]), 4) / 4)
kamekpatch += u32.pack(patch[0])
kamekpatch += patch[1]
# align it
if len(patch[1]) % 4 != 0:
kamekpatch += '\0' * (4 - (len(patch[1]) % 4))
else:
# single patch
kamekpatch += u32.pack(patch[0])
kamekpatch += patch[1]
kamekpatch += u32.pack(0xFFFFFFFF)
return kamekpatch
class DyLinkCreator(object):
R_PPC_ADDR32 = 1
R_PPC_ADDR16_LO = 4
R_PPC_ADDR16_HI = 5
R_PPC_ADDR16_HA = 6
R_PPC_REL24 = 10
VALID_RELOCS = set([1, 4, 5, 6, 10])
def __init__(self, other=None):
if other:
self._relocs = other._relocs[:]
self._targets = other._targets[:]
self._target_lookups = other._target_lookups.copy()
else:
self._relocs = []
self._targets = []
self._target_lookups = {}
self.elf = None
def set_elf(self, stream):
if self.elf != None:
raise ValueError('ELF already set')
self.elf = elftools.elf.elffile.ELFFile(stream)
self.code = self.elf.get_section_by_name('.text').data()
self._add_relocs(self.elf.get_section_by_name('.rela.text'))
def _add_relocs(self, section):
sym_values = {}
sym_section = self.elf.get_section_by_name('.symtab')
for reloc in section.iter_relocations():
entry = reloc.entry
#print entry
sym_id = entry['r_info_sym']
try:
sym_value, sym_name = sym_values[sym_id]
except KeyError:
sym = sym_section.get_symbol(sym_id)
sym_value = sym.entry['st_value']
sym_name = sym.name
sym_values[sym_id] = (sym_value, sym_name)
#print hex(sym_value)
self.add_reloc(entry['r_info_type'], entry['r_offset'], sym_value+entry['r_addend'], sym_name)
def add_reloc(self, reltype, addr, target, name="UNKNOWN NAME"):
if reltype not in self.VALID_RELOCS:
raise ValueError('Unknown/unsupported rel type: %d (%x => %x)' % (reltype, addr, target))
try:
target_id = self._target_lookups[target]
except KeyError:
target_id = len(self._targets)
self._target_lookups[target] = target_id
self._targets.append(target)
if target <= 0:
print("Warning: The following reloc (%x) points to %d: Is this right? %s" % (addr, target, name))
self._relocs.append((reltype, addr, target_id))
def build_reloc_data(self):
header_struct = struct.Struct('>8sI')
rel_struct_pack = struct.Struct('>II').pack
target_struct_pack = struct.Struct('>I').pack
rel_data = map(lambda x: rel_struct_pack((x[0] << 24) | x[2], x[1]), self._relocs)
target_data = map(target_struct_pack, self._targets)
header = header_struct.pack('NewerREL', 12 + (len(self._relocs) * 8))
return header + ''.join(rel_data) + ''.join(target_data)
class KamekModule(object):
_requiredFields = ['source_files']
def __init__(self, filename):
# load the module data
self.modulePath = os.path.normpath(filename)
self.moduleName = os.path.basename(self.modulePath)
#self.moduleDir = os.path.dirname(self.modulePath)
self.moduleDir = 'processed'
with open(self.modulePath, 'r') as f:
self.rawData = f.read()
self.data = yaml.safe_load(self.rawData)
if not isinstance(self.data, dict):
raise ValueError, 'the module file %s is an invalid format (it should be a YAML mapping)' % self.moduleName
# verify it
for field in self._requiredFields:
if field not in self.data:
raise ValueError, 'Missing field in the module file %s: %s' % (self.moduleName, field)
class KamekBuilder(object):
def __init__(self, project, configs):
self.project = project
self.configs = configs
def build(self):
print_debug('Starting build')
self._prepare_dirs()
for config in self.configs:
if only_build != None and config['short_name'] not in only_build:
continue
self._set_config(config)
#self._configTempDir = tempfile.mkdtemp()
self._configTempDir = 'tmp'
os.mkdir('tmp')
print_debug('Temp files for this configuration are in: '+self._configTempDir)
if 'dynamic_link' in self._config and self._config['dynamic_link']:
self.dynamic_link_base = DyLinkCreator()
else:
self.dynamic_link_base = None
self._builtCodeAddr = 0x80001800
if 'code_address' in self.project.data:
self._builtCodeAddr = self.project.data['code_address']
# hook setup
self._hook_contexts = {}
for name, hookType in hooks.HookTypes.iteritems():
if hookType.has_context:
self._hook_contexts[hookType] = hookType.context_type()
self._compile_modules()
# figure out how many copies we need to build
# this is a mess!
if 'multi_build' in self._config:
self._multi_build = self._config['multi_build']
else:
self._multi_build = {self._config['short_name']: self._config['linker_script']}
for s_name, s_script in self._multi_build.iteritems():
self.current_build_name = s_name
self._patches = []
self._rel_patches = []
self._hooks = []
self._create_hooks()
self._link(s_name, s_script)
self._read_symbol_map()
if self.dynamic_link_base:
self.dynamic_link = DyLinkCreator(self.dynamic_link_base)
self.dynamic_link.set_elf(open(self._currentOutFile, 'rb'))
for hook in self._hooks:
hook.create_patches()
self._create_patch(s_name)
if delete_temp:
shutil.rmtree(self._configTempDir)
def _prepare_dirs(self):
self._outDir = self.project.makeRelativePath(self.project.data['output_dir'])
print_debug('Project will be built in: '+self._outDir)
if not os.path.isdir(self._outDir):
os.makedirs(self._outDir)
print_debug('Created that directory')
def _set_config(self, config):
self._config = config
print_debug('---')
print_debug('Building for configuration: '+config['friendly_name'])
self.config_short_name = config['short_name']
if 'rel_area_start' in config:
self._rel_area = (config['rel_area_start'], config['rel_area_end'])
else:
self._rel_area = (-50, -50)
def _create_hooks(self):
print_debug('---')
print_debug('Creating hooks')
for m in self.project.modules:
if 'hooks' in m.data:
for hookData in m.data['hooks']:
assert 'name' in hookData and 'type' in hookData
#print_debug('Hook: %s : %s' % (m.moduleName, hookData['name']))
if hookData['type'] in hooks.HookTypes:
hookType = hooks.HookTypes[hookData['type']]
hook = hookType(self, m, hookData)
self._hooks.append(hook)
else:
raise ValueError, 'Unknown hook type: %s' % hookData['type']
def _compile_modules(self):
print_debug('---')
print_debug('Compiling modules')
if use_mw:
# metrowerks setup
cc_command = ['%smwcceppc.exe' % mw_path, '-I.', '-I-', '-I.', '-nostdinc', '-Cpp_exceptions', 'off', '-Os', '-proc', 'gekko', '-fp', 'hard', '-enum', 'int', '-sdata', '0', '-sdata2', '0', '-g', '-RTTI', 'off', '-use_lmw_stmw', 'on']
as_command = ['%smwasmeppc.exe' % mw_path, '-I.', '-I-', '-I.', '-nostdinc', '-proc', 'gekko', '-d', '__MWERKS__']
if 'defines' in self._config:
for d in self._config['defines']:
cc_command.append('-d')
cc_command.append(d)
as_command.append('-d')
as_command.append(d)
for i in self._config['include_dirs']:
cc_command.append('-I%s' % i)
#cc_command.append(i)
as_command.append('-I%s' % i)
#as_command.append(i)
if use_wine:
cc_command.insert(0, 'wine')
as_command.insert(0, 'wine')
else:
# gcc setup
cc_command = ['%s%s-g++' % (gcc_path, gcc_type), '-nodefaultlibs', '-I.', '-fno-builtin', '-Os', '-fno-exceptions', '-fno-rtti', '-mno-sdata']
as_command = cc_command
if 'defines' in self._config:
for d in self._config['defines']:
cc_command.append('-D%s' % d)
for i in self._config['include_dirs']:
cc_command.append('-I%s' % i)
self._moduleFiles = []
if fast_hack:
fast_cpp_path = os.path.join(self._configTempDir, 'fasthack.cpp')
fast_cpp = open(fast_cpp_path, 'w')
for m in self.project.modules:
for normal_sourcefile in m.data['source_files']:
print_debug('Compiling %s : %s' % (m.moduleName, normal_sourcefile))
objfile = os.path.join(self._configTempDir, '%d.o' % generate_unique_id())
sourcefile = os.path.join(m.moduleDir, normal_sourcefile)
if sourcefile.endswith('.o'):
new_command = ['cp', sourcefile, objfile]
else:
# todo: better extension detection
if sourcefile.endswith('.s') or sourcefile.endswith('.S'):
command = as_command
elif sourcefile.endswith('.cpp') and fast_hack:
fast_cpp.write('//\n// %s\n//\n\n' % sourcefile)
fast_cpp.write(open(sourcefile, 'r').read())
fast_cpp.write('\n')
continue
else:
command = cc_command
new_command = command + ['-c', '-o', objfile, sourcefile]
if 'cc_args' in m.data:
new_command += m.data['cc_args']
if show_cmd:
print_debug(new_command)
errorVal = subprocess.call(new_command)
if errorVal != 0:
print 'BUILD FAILED!'
print 'compiler returned %d - an error occurred while compiling %s' % (errorVal, sourcefile)
sys.exit(1)
self._moduleFiles.append(objfile)
if fast_hack:
fast_cpp.close()
print_debug('Fast compilation!!')
objfile = os.path.join(self._configTempDir, 'fasthack.o')
new_command = cc_command + ['-c', '-o', objfile, fast_cpp_path]
if show_cmd:
print_debug(new_command)
errorVal = subprocess.call(new_command)
if errorVal != 0:
print 'BUILD FAILED!'
print 'compiler returned %d - an error occurred while compiling the fast hack' % errorVal
sys.exit(1)
self._moduleFiles.append(objfile)
print_debug('Compilation complete')
def _link(self, short_name, script_file):
print_debug('---')
print_debug('Linking %s (%s)...' % (short_name, script_file))
nice_name = '%s_%s' % (self._config['short_name'], short_name)
print_debug('---')
self._currentMapFile = '%s/%s_linkmap.map' % (self._outDir, nice_name)
outname = 'object.plf' if self.dynamic_link_base else 'object.bin'
self._currentOutFile = '%s/%s_%s' % (self._outDir, nice_name, outname)
ld_command = ['%s%s-ld.exe' % (gcc_path, gcc_type), '-L.']
ld_command.append('-o')
ld_command.append(self._currentOutFile)
if self.dynamic_link_base:
ld_command.append('-r')
ld_command.append('--oformat=elf32-powerpc')
else:
ld_command.append('--oformat=binary')
ld_command.append('-Ttext')
ld_command.append('0x%08X' % self._builtCodeAddr)
ld_command.append('-T')
ld_command.append(script_file)
ld_command.append('-Map')
ld_command.append(self._currentMapFile)
ld_command.append('--no-demangle') # for debugging
#ld_command.append('--verbose')
ld_command += self._moduleFiles
if show_cmd:
print_debug(ld_command)
errorVal = subprocess.call(ld_command)
if errorVal != 0:
print 'BUILD FAILED!'
print 'ld returned %d' % errorVal
sys.exit(1)
print_debug('Successfully linked %s' % short_name)
def _read_symbol_map(self):
print_debug('---')
print_debug('Reading symbol map')
self._symbols = []
file = open(self._currentMapFile, 'r')
for line in file:
if '__text_start' in line:
self._textSegStart = int(line.split()[0],0)
break
# now read the individual symbols
# this is probably a bad method to parse it, but whatever
for line in file:
if '__text_end' in line:
self._textSegEnd = int(line.split()[0],0)
break
if not line.startswith(' '): continue
sym = line.split()
sym[0] = int(sym[0],0)
self._symbols.append(sym)
# we've found __text_end, so now we should be at the output section
currentEndAddress = self._textSegEnd
for line in file:
if line[0] == '.':
# probably a segment
data = line.split()
if len(data) < 3: continue
segAddr = int(data[1],0)
segSize = int(data[2],0)
if segAddr+segSize > currentEndAddress:
currentEndAddress = segAddr+segSize
self._codeStart = self._textSegStart
self._codeEnd = currentEndAddress
file.close()
print_debug('Read, %d symbol(s) parsed' % len(self._symbols))
# next up, run it through c++filt
print_debug('Running c++filt')
p = subprocess.Popen(gcc_type + '-c++filt.exe', stdin=subprocess.PIPE, stdout=subprocess.PIPE)
symbolNameList = [sym[1] for sym in self._symbols]
filtResult = p.communicate('\n'.join(symbolNameList))
filteredSymbols = filtResult[0].split('\n')
for sym, filt in zip(self._symbols, filteredSymbols):
sym.append(filt.strip())
print_debug('Done. All symbols complete.')
print_debug('Generated code is at 0x%08X .. 0x%08X' % (self._codeStart, self._codeEnd - 4))
def find_func_by_symbol(self, find_symbol):
for sym in self._symbols:
#if show_cmd:
# out = "0x%08x - %s - %s" % (sym[0], sym[1], sym[2])
# print_debug(out)
if sym[2] == find_symbol:
return sym[0]
raise ValueError, 'Cannot find function: %s' % find_symbol
def add_patch(self, offset, data):
if offset >= self._rel_area[0] and offset <= self._rel_area[1] and use_rels:
self._rel_patches.append((offset, data))
else:
self._patches.append((offset, data))
def _create_patch(self, short_name):
print_debug('---')
print_debug('Creating patch')
nice_name = '%s_%s' % (self._config['short_name'], short_name)
# convert the .rel patches to KamekPatcher format
if len(self._rel_patches) > 0:
kamekpatch = generate_kamek_patches(self._rel_patches)
#self._patches.append((0x817F4800, kamekpatch))
self._patches.append((0x80002F60, kamekpatch))
if self.dynamic_link:
# put together the dynamic link files
dlcode = open('%s/%s_dlcode.bin' % (self._outDir, nice_name), 'wb')
dlcode.write(self.dynamic_link.code)
dlcode.close()
dlrelocs = open('%s/%s_dlrelocs.bin' % (self._outDir, nice_name), 'wb')
dlrelocs.write(self.dynamic_link.build_reloc_data())
dlrelocs.close()
else:
# add the outfile as a patch if not using dynamic linking
file = open(self._currentOutFile, 'rb')
patch = (self._codeStart, file.read())
file.close()
self._patches.append(patch)
# generate a Riivolution patch
riiv = open('%s/%s_riiv.xml' % (self._outDir, nice_name), 'w')
for patch in self._patches:
riiv.write(generate_riiv_mempatch(*patch) + '\n')
riiv.close()
# generate an Ocarina patch
ocarina = open('%s/%s_ocarina.txt' % (self._outDir, nice_name), 'w')
for patch in self._patches:
ocarina.write(generate_ocarina_patch(*patch) + '\n')
ocarina.close()
# generate a KamekPatcher patch
kpatch = open('%s/%s_loader.bin' % (self._outDir, nice_name), 'wb')
kpatch.write(generate_kamek_patches(self._patches))
kpatch.close()
print_debug('Patches generated')
class KamekProject(object):
_requiredFields = ['output_dir', 'modules']
def __init__(self, filename):
# load the project data
self.projectPath = os.path.abspath(filename)
self.projectName = os.path.basename(self.projectPath)
self.projectDir = '' #os.path.dirname(self.projectPath)
with open(self.projectPath, 'r') as f:
self.rawData = f.read()
self.data = yaml.safe_load(self.rawData)
if not isinstance(self.data, dict):
raise ValueError, 'the project file is an invalid format (it should be a YAML mapping)'
# verify it
for field in self._requiredFields:
if field not in self.data:
raise ValueError, 'Missing field in the project file: %s' % field
# load each module
self.modules = []
for moduleName in self.data['modules']:
modulePath = self.makeRelativePath(moduleName)
self.modules.append(KamekModule(modulePath))
def makeRelativePath(self, path):
return os.path.normpath(os.path.join(self.projectDir, path))
def build(self):
# compile everything in the project
builder = KamekBuilder(self, self.configs)
builder.build()
def main():
print version_str
print
if len(sys.argv) < 2:
print 'No input file specified'
sys.exit()
parse_cmd_options()
project = KamekProject(os.path.normpath(sys.argv[1]))
if override_config_file:
project.configs = read_configs(override_config_file)
else:
project.configs = read_configs('kamek_configs.yaml')
project.build()
if __name__ == '__main__':
main()
|