12from optparse
import OptionParser
16 parser = OptionParser()
21 help=
"output assembly file name")
23 "--input", action=
"store", type=
"string", help=
"input binary blob file")
24 parser.add_option(
"--symbol_name", action=
"store", type=
"string")
25 parser.add_option(
"--executable", action=
"store_true", default=
False)
26 parser.add_option(
"--target_os", action=
"store", type=
"string")
27 parser.add_option(
"--size_symbol_name", action=
"store", type=
"string")
28 parser.add_option(
"--target_arch", action=
"store", type=
"string")
29 parser.add_option(
"--incbin", action=
"store_true", default=
False)
31 (options, args) = parser.parse_args()
32 if not options.output:
33 sys.stderr.write(
"--output not specified\n")
37 sys.stderr.write(
"--input not specified\n")
40 if not os.path.isfile(options.input):
41 sys.stderr.write(
"input file does not exist: %s\n" % options.input)
44 if not options.symbol_name:
45 sys.stderr.write(
"--symbol_name not specified\n")
48 if not options.target_os:
49 sys.stderr.write(
"--target_os not specified\n")
53 with open(options.output,
"w")
as output_file:
54 if options.target_os
in [
"mac",
"ios"]:
55 if options.executable:
56 output_file.write(
".text\n")
58 output_file.write(
".const\n")
59 output_file.write(
".global _%s\n" % options.symbol_name)
60 output_file.write(
".balign 32\n")
61 output_file.write(
"_%s:\n" % options.symbol_name)
62 elif options.target_os
in [
"win"]:
63 output_file.write(
"ifndef _ML64_X64\n")
64 output_file.write(
".model flat, C\n")
65 output_file.write(
"endif\n")
66 if options.executable:
67 output_file.write(
".code\n")
69 output_file.write(
".const\n")
70 output_file.write(
"public %s\n" % options.symbol_name)
71 output_file.write(
"%s label byte\n" % options.symbol_name)
72 elif options.target_os
in [
"win_gnu"]:
74 if options.executable:
75 output_file.write(
".text\n")
77 output_file.write(
".section .rodata\n")
78 output_file.write(
".global %s\n" % options.symbol_name)
79 output_file.write(
".balign 32\n")
80 output_file.write(
"%s:\n" % options.symbol_name)
82 if options.executable:
83 output_file.write(
".text\n")
84 output_file.write(
".type %s STT_FUNC\n" % options.symbol_name)
86 output_file.write(
".section .rodata\n")
87 output_file.write(
".type %s STT_OBJECT\n" % options.symbol_name)
88 output_file.write(
".global %s\n" % options.symbol_name)
89 output_file.write(
".balign 32\n")
90 output_file.write(
"%s:\n" % options.symbol_name)
93 with open(options.input,
"rb")
as input_file:
94 if options.target_os
in [
"win"]:
95 for byte
in input_file.read():
96 output_file.write(
"byte %d\n" % (byte
if isinstance(byte, int)
else ord(byte)))
99 incbin = options.incbin
100 for byte
in input_file.read():
105 (byte
if isinstance(byte, int)
else ord(byte)))
107 output_file.write(
".incbin \"%s\"\n" % options.input)
109 if options.target_os
not in [
"mac",
"ios",
"win",
"win_gnu"]:
110 output_file.write(
".size {0}, .-{0}\n".
format(options.symbol_name))
112 if options.size_symbol_name:
113 if not options.target_arch:
114 sys.stderr.write(
"--target_arch not specified\n")
119 if options.target_arch:
120 if options.target_arch
in [
"arm64",
"x64",
"riscv64"]:
123 if options.target_os
in [
"win"]:
124 output_file.write(
"public %s\n" % options.size_symbol_name)
125 output_file.write(
"%s label byte\n" % options.size_symbol_name)
127 output_file.write(
"qword %d\n" % size)
129 output_file.write(
"dword %d\n" % size)
131 if options.target_os
in [
"mac",
"ios"]:
133 ".global _%s\n" % options.size_symbol_name)
134 output_file.write(
"_%s:\n" % options.size_symbol_name)
136 output_file.write(
".global %s\n" % options.size_symbol_name)
137 output_file.write(
"%s:\n" % options.size_symbol_name)
139 output_file.write(
".quad %d\n" % size)
141 output_file.write(
".long %d\n" % size)
143 if options.target_os
in [
"win"]:
144 output_file.write(
"end\n")
149if __name__ ==
"__main__":
uint32_t uint32_t * format