forked from mkw-sp/mkw-sp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
elf2dol.py
executable file
·76 lines (60 loc) · 2.26 KB
/
elf2dol.py
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
#!/usr/bin/env python3
from argparse import ArgumentParser
from elftools.elf.constants import P_FLAGS
from elftools.elf.elffile import ELFFile
import io
def segment_is_text(segment):
return segment['p_flags'] & P_FLAGS.PF_X == P_FLAGS.PF_X
def segment_is_data(segment):
return not segment_is_text(segment) and not segment_is_bss(segment)
def segment_is_bss(segment):
return segment['p_filesz'] == 0
def write_to_dol_header(file, offset, val):
file.seek(offset)
file.write(val.to_bytes(4, byteorder = 'big'))
file.seek(0, io.SEEK_END)
def write_segment_to_dol(idx, segment, dol_file):
write_to_dol_header(dol_file, 0x00 + 0x04 * idx, dol_file.tell())
write_to_dol_header(dol_file, 0x48 + 0x04 * idx, segment['p_vaddr'])
# align filesz to 0x20
filesz = ((segment['p_filesz'] + 0x1F) >> 5) << 5
write_to_dol_header(dol_file, 0x90 + 0x04 * idx, filesz)
dol_file.write(segment.data())
# align current dol size to 0x20
size = 0x20 - dol_file.tell() & 0x1F
dol_file.write(bytes([0x00] * size))
parser = ArgumentParser()
parser.add_argument('in_path')
parser.add_argument('out_path')
args = parser.parse_args()
with open(args.in_path, 'rb') as elf_file, open(args.out_path, 'wb') as dol_file:
elf_file = ELFFile(elf_file)
num_segments = elf_file.num_segments()
dol_file.write(bytes([0x00] * 0x100))
idx = 0
for i in range(num_segments):
segment = elf_file.get_segment(i)
if not segment_is_text(segment):
continue
write_segment_to_dol(idx, segment, dol_file)
idx += 1
idx = 7
for i in range(num_segments):
segment = elf_file.get_segment(i)
if not segment_is_data(segment):
continue
write_segment_to_dol(idx, segment, dol_file)
idx += 1
bss_start = 0
bss_end = 0
for i in range(num_segments):
segment = elf_file.get_segment(i)
if not segment_is_bss(segment):
continue
if bss_start == 0:
bss_start = segment['p_vaddr']
bss_end = segment['p_vaddr'] + segment['p_memsz']
write_to_dol_header(dol_file, 0xD8, bss_start)
bss_size = bss_end - bss_start
write_to_dol_header(dol_file, 0xDC, bss_size)
write_to_dol_header(dol_file, 0xE0, elf_file['e_entry'])