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)
|
|
|
|
|
|
|
|
with open(sys.argv[1], 'rb') as f:
|
|
|
|
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
|
|
|
|
|
|
|
with open(sys.argv[2], 'wb') as f:
|
2021-03-04 06:15:54 +09:00
|
|
|
elf.stream.seek(0)
|
|
|
|
elf.stream.read(text.header.sh_offset)
|
|
|
|
f.write(elf.stream.read())
|
2021-03-04 02:15:40 +09:00
|
|
|
|
2021-03-04 06:15:54 +09:00
|
|
|
print(f'Successfully extracted the necessary sections to "{sys.argv[2]}"')
|
2021-03-04 02:15:40 +09:00
|
|
|
|
|
|
|
|
|
|
|
main()
|