ponydos/add_syscalls.py

48 lines
1.0 KiB
Python

import sys
syscalls = {
'open_file': None,
'read_sectors': None,
'draw_rect': None,
}
if len(sys.argv) != 3:
print(f'Usage: {sys.argv[0]} outfile infile', file=sys.stderr)
sys.exit(1)
outfile = sys.argv[1]
infile = sys.argv[2]
with open(infile, 'r') as f:
header = f.read()
header = f'; This is from {infile}\n' + header
mapfile = []
while True:
try:
line = input()
except EOFError:
break
mapfile.append(line)
section = None
for line in mapfile:
line = line.split()
if len(line) == 4 and all(c == '-' for c in line[0]) and line[1] == 'Section' and all(c == '-' for c in line[3]):
section = line[2]
if section == '.text' and len(line) == 3:
address, _, name = line
if name in syscalls:
syscalls[name] = int(address, 16)
header += f'\n;This was automatically generated\n'
for syscall, address in syscalls.items():
if address is None:
print(f'{sys.argv[0]}: Error: syscall {syscall} not found', file=sys.stderr)
sys.exit(1)
header += f'SYS_{syscall.upper()} equ 0x{address:x}\n'
with open(outfile, 'w') as f:
f.write(header)