Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
bin_to_assembly.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2#
3# Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
4# for details. All rights reserved. Use of this source code is governed by a
5# BSD-style license that can be found in the LICENSE file.
6
7# Generates an assembly source file the defines a symbol with the bytes from
8# a given file.
9
10import os
11import sys
12from optparse import OptionParser
13
14
15def Main():
16 parser = OptionParser()
17 parser.add_option(
18 "--output",
19 action="store",
20 type="string",
21 help="output assembly file name")
22 parser.add_option(
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)
30
31 (options, args) = parser.parse_args()
32 if not options.output:
33 sys.stderr.write("--output not specified\n")
34 parser.print_help()
35 return -1
36 if not options.input:
37 sys.stderr.write("--input not specified\n")
38 parser.print_help()
39 return -1
40 if not os.path.isfile(options.input):
41 sys.stderr.write("input file does not exist: %s\n" % options.input)
42 parser.print_help()
43 return -1
44 if not options.symbol_name:
45 sys.stderr.write("--symbol_name not specified\n")
46 parser.print_help()
47 return -1
48 if not options.target_os:
49 sys.stderr.write("--target_os not specified\n")
50 parser.print_help()
51 return -1
52
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")
57 else:
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")
68 else:
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"]:
73 # Cross compilation from Linux to Windows.
74 if options.executable:
75 output_file.write(".text\n")
76 else:
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)
81 else:
82 if options.executable:
83 output_file.write(".text\n")
84 output_file.write(".type %s STT_FUNC\n" % options.symbol_name)
85 else:
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)
91
92 size = 0
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)))
97 size += 1
98 else:
99 incbin = options.incbin
100 for byte in input_file.read():
101 size += 1
102 if not incbin:
103 output_file.write(
104 ".byte %d\n" %
105 (byte if isinstance(byte, int) else ord(byte)))
106 if incbin:
107 output_file.write(".incbin \"%s\"\n" % options.input)
108
109 if options.target_os not in ["mac", "ios", "win", "win_gnu"]:
110 output_file.write(".size {0}, .-{0}\n".format(options.symbol_name))
111
112 if options.size_symbol_name:
113 if not options.target_arch:
114 sys.stderr.write("--target_arch not specified\n")
115 parser.print_help()
116 return -1
117
118 is64bit = 0
119 if options.target_arch:
120 if options.target_arch in ["arm64", "x64", "riscv64"]:
121 is64bit = 1
122
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)
126 if (is64bit == 1):
127 output_file.write("qword %d\n" % size)
128 else:
129 output_file.write("dword %d\n" % size)
130 else:
131 if options.target_os in ["mac", "ios"]:
132 output_file.write(
133 ".global _%s\n" % options.size_symbol_name)
134 output_file.write("_%s:\n" % options.size_symbol_name)
135 else:
136 output_file.write(".global %s\n" % options.size_symbol_name)
137 output_file.write("%s:\n" % options.size_symbol_name)
138 if (is64bit == 1):
139 output_file.write(".quad %d\n" % size)
140 else:
141 output_file.write(".long %d\n" % size)
142
143 if options.target_os in ["win"]:
144 output_file.write("end\n")
145
146 return 0
147
148
149if __name__ == "__main__":
150 sys.exit(Main())
uint32_t uint32_t * format