lab/x1/extract.py

35 lines
831 B
Python
Raw Normal View History

2021-03-04 02:15:40 +09:00
#!/usr/bin/env python3
import sys
from elftools.elf.elffile import ELFFile # pip install pyelftools
def main():
if len(sys.argv) < 3:
print(f'Usage: {sys.argv[0]} in.elf out.bin')
sys.exit(1)
2021-03-05 22:07:56 +09:00
with open(sys.argv[-2], 'rb') as f:
2021-03-04 02:15:40 +09:00
extract(ELFFile(f))
def extract(elf):
text = elf.get_section_by_name('.text')
if text is None:
print('Input ELF has no .text section', file=sys.stderr)
2021-03-04 14:49:51 +09:00
sys.exit(1)
2021-03-04 02:15:40 +09:00
2021-03-05 22:07:56 +09:00
with open(sys.argv[-1], 'wb') as f:
if '-p' in sys.argv:
print(f'Pure .text mode is enabled')
f.write(text.data())
else:
elf.stream.seek(0)
elf.stream.read(text.header.sh_offset)
f.write(elf.stream.read())
2021-03-04 02:15:40 +09:00
print(f'Successfully extracted the necessary sections to "{sys.argv[2]}"')
2021-03-04 02:15:40 +09:00
main()