-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelf2bin
executable file
·61 lines (37 loc) · 1.32 KB
/
elf2bin
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
#!/usr/bin/python3
"""
Convert an ELF file into a binary file.
Copyright (C) 2018, Guillaume Gonnet
License MIT
"""
import argparse
import subprocess
import os.path as path
# GNU objcopy
GCC_PREFIX = "/opt/mips-toolchain/bin/mips-sde-elf-"
OBJCOPY = "%sobjcopy" % GCC_PREFIX
# Sections to use
SECTIONS = ("text", "data", "sdata", "bss", "rodata", "irq_vec")
def elf_to_bin(inname, outname, gap_fill):
"Convert an ELF file into binary."
args = ["-O", "binary"]
args += ["--only-section=.%s" % s for s in SECTIONS]
if gap_fill:
args += ["--gap-fill", "0xFF"]
subprocess.call([OBJCOPY, *args, inname, outname])
assert path.isfile(outname), "The binary file has not been generated."
def main():
"Entry point of the application."
parser = argparse.ArgumentParser(prog="elf2bin",
description="Convert an ELF file into a binary file.")
parser.add_argument("elf", help="input ELF filename (.elf)")
parser.add_argument("-o", help="output binary filename (.bin)",
default=None, metavar="outname")
parser.add_argument("-FF", help="replace empty sections by 0xFF",
action="store_true")
args = parser.parse_args()
if not args.o:
args.o = args.elf.replace(".elf", ".bin")
elf_to_bin(args.elf, args.o, args.FF)
if __name__ == "__main__":
main()