-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.py
More file actions
executable file
·40 lines (28 loc) · 916 Bytes
/
init.py
File metadata and controls
executable file
·40 lines (28 loc) · 916 Bytes
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
#!/usr/bin/python3
# the program generates an initialization assembler
# with a dump .data section of the elf file
# wykys 2022
def load_data_from_bin(path: str) -> tuple:
with open(path, 'rb') as fr:
return tuple(
int.from_bytes(bytes(data), 'little')
for data in zip(*(iter(fr.read()),) * 4)
)
def generate_init_asm(path: str, data: tuple) -> None:
asm = '''
.global _init
.section .text.init, "ax"
_init:'''
for i, value in enumerate(data):
address = i * 4
asm += f'''
li t0,0x{address:08x}
li t1,0x{value:08x}
sw t1,0(t0)'''
asm += '\n ret\n'
with open(path, 'w') as fw:
fw.writelines(asm)
if __name__ == '__main__':
rodata = load_data_from_bin('build/rodata.bin')
data = load_data_from_bin('build/data.bin')
generate_init_asm('build/init.S', rodata + data)