Flutter Engine
The Flutter Engine
Functions | Variables
bin_to_coff Namespace Reference

Functions

def main ()
 

Variables

int FILE_HEADER_RELFLG = 0x1
 
int FILE_HEADER_EXEC = 0x2
 
int FILE_HEADER_LNNO = 0x4
 
int FILE_HEADER_LSYMS = 0x8
 
int FILE_HEADER_AR32WR = 0x100
 
int SECTION_HEADER_TEXT = 0x20
 
int SECTION_HEADER_DATA = 0x40
 
int SECTION_HEADER_BSS = 0x80
 
string FILE_HEADER_FORMAT = 'HHIIIHH'
 
 FILE_HEADER_SIZE = calcsize(FILE_HEADER_FORMAT)
 
dictionary FILE_HEADER_MAGICS
 
int FILE_HEADER_NUM_SECTIONS = 1
 
int FILE_HEADER_TIMESTAMP = 0
 
int FILE_HEADER_SIZE_OF_OPTIONAL = 0
 
int FILE_HEADER_FLAGS = FILE_HEADER_LNNO
 
string SECTION_HEADER_FORMAT = '8sIIIIIIHHI'
 
 SECTION_HEADER_SIZE = calcsize(SECTION_HEADER_FORMAT)
 
string SECTION_NAME_RODATA = b'.rodata'
 
string SECTION_NAME_TEXT = b'.text'
 
int SECTION_PADDR = 0x0
 
int SECTION_VADDR = 0x0
 
tuple SECTION_RAW_DATA_PTR
 
int SECTION_RELOCATION_PTR = 0x0
 
int SECTION_LINE_NUMS_PTR = 0x0
 
int SECTION_NUM_RELOCATION = 0
 
int SECTION_NUM_LINE_NUMS = 0
 
int SYMBOL_TABLE_ENTRY_SHORT_LEN = 8
 
string SYMBOL_TABLE_ENTRY_FORMAT_SHORT = '8sIhHBB'
 
string SYMBOL_TABLE_ENTRY_FORMAT_LONG = 'IIIhHBB'
 
 SYMBOL_TABLE_ENTRY_SIZE = calcsize(SYMBOL_TABLE_ENTRY_FORMAT_SHORT)
 
int SYMBOL_TABLE_ENTRY_ZEROS = 0x0
 
int SYMBOL_TABLE_ENTRY_SECTION = 1
 
int SYMBOL_TABLE_ENTRY_TYPE = 0
 
int SYMBOL_TABLE_ENTRY_CLASS = 2
 
int SYMBOL_TABLE_ENTRY_NUM_AUX = 0
 
int STRING_TABLE_OFFSET = 0x4
 
string SIZE_FORMAT = 'I'
 
 SIZE_LENGTH = calcsize(SIZE_FORMAT)
 
string SIZE_SYMBOL_FORMAT_X64 = 'Q'
 
 SIZE_SYMBOL_LENGTH_X64 = calcsize(SIZE_SYMBOL_FORMAT_X64)
 

Function Documentation

◆ main()

def bin_to_coff.main ( )

Definition at line 107 of file bin_to_coff.py.

107def main():
108 parser = argparse.ArgumentParser(
109 description='Generate a COFF file for binary data.')
110 parser.add_argument('--input', dest='input', help='Path of the input file.')
111 parser.add_argument(
112 '--output', dest='output', help='Name of the output file.')
113 parser.add_argument(
114 '--symbol_name',
115 dest='symbol_name',
116 help='Name of the symbol for the binary data')
117 parser.add_argument(
118 '--size_symbol_name',
119 dest='size_name',
120 help='Name of the symbol for the size of the binary data')
121 parser.add_argument('--arch', dest='arch')
122 parser.add_argument(
123 '--executable', dest='executable', action='store_true', default=False)
124
125 args = parser.parse_args()
126 use_64_bit = args.arch in ['x64', 'arm64', 'riscv64']
127
128 with open(args.input, 'rb') as f:
129 section_data = f.read()
130
131 # We need to calculate the following to determine the size of our buffer:
132 # 1) Size of the data
133 # 2) Total length of the symbol strings which are over 8 characters
134
135 section_size = len(section_data)
136 includes_size_name = (args.size_name != None)
137
138 # Symbols on x86 are prefixed with '_'
139 symbol_prefix = b'_' if args.arch == 'x86' else b''
140 num_symbols = 2 if includes_size_name else 1
141 symbol_name = symbol_prefix + args.symbol_name.encode()
142 size_symbol_name = None
143 if (includes_size_name):
144 size_symbol = args.size_name if args.size_name else args.symbol_name + "Size"
145 size_symbol_name = symbol_prefix + size_symbol.encode()
146
147 size_symbol_format = SIZE_SYMBOL_FORMAT_X64 if use_64_bit else SIZE_FORMAT
148 size_symbol_size = SIZE_SYMBOL_LENGTH_X64 if use_64_bit else SIZE_LENGTH
149
150 # The symbol table is directly after the data section
151 symbol_table_ptr = (FILE_HEADER_SIZE + SECTION_HEADER_SIZE + section_size +
152 size_symbol_size)
153 string_table_len = 0
154
155 # Symbols longer than 8 characters have their string representations stored
156 # in the string table.
157 long_symbol_name = False
158 long_size_symbol_name = False
159 if (len(symbol_name) > SYMBOL_TABLE_ENTRY_SHORT_LEN):
160 string_table_len += len(symbol_name) + 1
161 long_symbol_name = True
162
163 if (includes_size_name and
164 (len(size_symbol_name) > SYMBOL_TABLE_ENTRY_SHORT_LEN)):
165 string_table_len += len(size_symbol_name) + 1
166 long_size_symbol_name = True
167
168 # Create the buffer and start building.
169 offset = 0
170 buff = create_string_buffer(
171 FILE_HEADER_SIZE + SECTION_HEADER_SIZE + section_size +
172 num_symbols * SYMBOL_TABLE_ENTRY_SIZE + SIZE_LENGTH + size_symbol_size +
173 string_table_len)
174
175 FILE_HEADER_MAGIC = FILE_HEADER_MAGICS[args.arch]
176
177 # Populate the file header. Basically constant except for the pointer to the
178 # beginning of the symbol table.
179 pack_into(FILE_HEADER_FORMAT, buff, offset, FILE_HEADER_MAGIC,
180 FILE_HEADER_NUM_SECTIONS, FILE_HEADER_TIMESTAMP, symbol_table_ptr,
181 num_symbols, FILE_HEADER_SIZE_OF_OPTIONAL, FILE_HEADER_FLAGS)
182 offset += FILE_HEADER_SIZE
183
184 section_name = SECTION_NAME_RODATA
185 section_type = SECTION_HEADER_DATA
186 if args.executable:
187 section_name = SECTION_NAME_TEXT
188 section_type = SECTION_HEADER_TEXT
189
190 # Populate the section header for a single section.
191 pack_into(SECTION_HEADER_FORMAT, buff, offset, section_name, SECTION_PADDR,
192 SECTION_VADDR, section_size + size_symbol_size,
193 SECTION_RAW_DATA_PTR, SECTION_RELOCATION_PTR,
194 SECTION_LINE_NUMS_PTR, SECTION_NUM_RELOCATION,
195 SECTION_NUM_LINE_NUMS, section_type)
196 offset += SECTION_HEADER_SIZE
197
198 # Copy the binary data.
199 buff[offset:offset + section_size] = section_data
200 offset += section_size
201
202 # Append the size of the section.
203 pack_into(size_symbol_format, buff, offset, section_size)
204 offset += size_symbol_size
205
206 # Build the symbol table. If a symbol name is 8 characters or less, it's
207 # placed directly in the symbol table. If not, it's entered in the string
208 # table immediately after the symbol table.
209
210 string_table_offset = STRING_TABLE_OFFSET
211 if long_symbol_name:
212 pack_into(SYMBOL_TABLE_ENTRY_FORMAT_LONG, buff, offset,
213 SYMBOL_TABLE_ENTRY_ZEROS, string_table_offset, 0x0,
214 SYMBOL_TABLE_ENTRY_SECTION, SYMBOL_TABLE_ENTRY_TYPE,
215 SYMBOL_TABLE_ENTRY_CLASS, SYMBOL_TABLE_ENTRY_NUM_AUX)
216 string_table_offset += len(symbol_name) + 1
217 else:
218 pack_into(SYMBOL_TABLE_ENTRY_FORMAT_SHORT, buff, offset, symbol_name,
219 0x0, SYMBOL_TABLE_ENTRY_SECTION, SYMBOL_TABLE_ENTRY_TYPE,
220 SYMBOL_TABLE_ENTRY_CLASS, SYMBOL_TABLE_ENTRY_NUM_AUX)
221 offset += SYMBOL_TABLE_ENTRY_SIZE
222
223 if includes_size_name:
224 # The size symbol table entry actually contains the value for the size.
225 if long_size_symbol_name:
226 pack_into(SYMBOL_TABLE_ENTRY_FORMAT_LONG, buff, offset,
227 SYMBOL_TABLE_ENTRY_ZEROS, string_table_offset,
228 section_size, SYMBOL_TABLE_ENTRY_SECTION,
229 SYMBOL_TABLE_ENTRY_TYPE, SYMBOL_TABLE_ENTRY_CLASS,
230 SYMBOL_TABLE_ENTRY_NUM_AUX)
231 else:
232 pack_into(SYMBOL_TABLE_ENTRY_FORMAT_SHORT, buff, offset,
233 symbol_name, section_size, SYMBOL_TABLE_ENTRY_SECTION,
234 SYMBOL_TABLE_ENTRY_TYPE, SYMBOL_TABLE_ENTRY_CLASS,
235 SYMBOL_TABLE_ENTRY_NUM_AUX)
236 offset += SYMBOL_TABLE_ENTRY_SIZE
237
238 pack_into(SIZE_FORMAT, buff, offset, string_table_len + SIZE_LENGTH)
239 offset += SIZE_LENGTH
240
241 # Populate the string table for any symbols longer than 8 characters.
242 if long_symbol_name:
243 symbol_len = len(symbol_name)
244 buff[offset:offset + symbol_len] = symbol_name
245 offset += symbol_len
246 buff[offset] = b'\0'
247 offset += 1
248
249 if includes_size_name and long_size_symbol_name:
250 symbol_len = len(size_symbol_name)
251 buff[offset:offset + symbol_len] = size_symbol_name
252 offset += symbol_len
253 buff[offset] = b'\0'
254 offset += 1
255
256 with open(args.output, 'wb') as f:
257 f.write(buff.raw)
258
259

Variable Documentation

◆ FILE_HEADER_AR32WR

int bin_to_coff.FILE_HEADER_AR32WR = 0x100

Definition at line 16 of file bin_to_coff.py.

◆ FILE_HEADER_EXEC

int bin_to_coff.FILE_HEADER_EXEC = 0x2

Definition at line 13 of file bin_to_coff.py.

◆ FILE_HEADER_FLAGS

int bin_to_coff.FILE_HEADER_FLAGS = FILE_HEADER_LNNO

Definition at line 46 of file bin_to_coff.py.

◆ FILE_HEADER_FORMAT

string bin_to_coff.FILE_HEADER_FORMAT = 'HHIIIHH'

Definition at line 33 of file bin_to_coff.py.

◆ FILE_HEADER_LNNO

int bin_to_coff.FILE_HEADER_LNNO = 0x4

Definition at line 14 of file bin_to_coff.py.

◆ FILE_HEADER_LSYMS

int bin_to_coff.FILE_HEADER_LSYMS = 0x8

Definition at line 15 of file bin_to_coff.py.

◆ FILE_HEADER_MAGICS

dictionary bin_to_coff.FILE_HEADER_MAGICS
Initial value:
1= {
2 'x86': 0x014c,
3 'x64': 0x8664,
4 'arm': 0x1c0,
5 'arm64': 0xaa64,
6 'riscv32': 0x5032,
7 'riscv64': 0x5064,
8}

Definition at line 35 of file bin_to_coff.py.

◆ FILE_HEADER_NUM_SECTIONS

int bin_to_coff.FILE_HEADER_NUM_SECTIONS = 1

Definition at line 43 of file bin_to_coff.py.

◆ FILE_HEADER_RELFLG

int bin_to_coff.FILE_HEADER_RELFLG = 0x1

Definition at line 12 of file bin_to_coff.py.

◆ FILE_HEADER_SIZE

bin_to_coff.FILE_HEADER_SIZE = calcsize(FILE_HEADER_FORMAT)

Definition at line 34 of file bin_to_coff.py.

◆ FILE_HEADER_SIZE_OF_OPTIONAL

int bin_to_coff.FILE_HEADER_SIZE_OF_OPTIONAL = 0

Definition at line 45 of file bin_to_coff.py.

◆ FILE_HEADER_TIMESTAMP

int bin_to_coff.FILE_HEADER_TIMESTAMP = 0

Definition at line 44 of file bin_to_coff.py.

◆ SECTION_HEADER_BSS

int bin_to_coff.SECTION_HEADER_BSS = 0x80

Definition at line 21 of file bin_to_coff.py.

◆ SECTION_HEADER_DATA

int bin_to_coff.SECTION_HEADER_DATA = 0x40

Definition at line 20 of file bin_to_coff.py.

◆ SECTION_HEADER_FORMAT

string bin_to_coff.SECTION_HEADER_FORMAT = '8sIIIIIIHHI'

Definition at line 61 of file bin_to_coff.py.

◆ SECTION_HEADER_SIZE

bin_to_coff.SECTION_HEADER_SIZE = calcsize(SECTION_HEADER_FORMAT)

Definition at line 62 of file bin_to_coff.py.

◆ SECTION_HEADER_TEXT

int bin_to_coff.SECTION_HEADER_TEXT = 0x20

Definition at line 19 of file bin_to_coff.py.

◆ SECTION_LINE_NUMS_PTR

int bin_to_coff.SECTION_LINE_NUMS_PTR = 0x0

Definition at line 70 of file bin_to_coff.py.

◆ SECTION_NAME_RODATA

string bin_to_coff.SECTION_NAME_RODATA = b'.rodata'

Definition at line 63 of file bin_to_coff.py.

◆ SECTION_NAME_TEXT

string bin_to_coff.SECTION_NAME_TEXT = b'.text'

Definition at line 64 of file bin_to_coff.py.

◆ SECTION_NUM_LINE_NUMS

int bin_to_coff.SECTION_NUM_LINE_NUMS = 0

Definition at line 72 of file bin_to_coff.py.

◆ SECTION_NUM_RELOCATION

int bin_to_coff.SECTION_NUM_RELOCATION = 0

Definition at line 71 of file bin_to_coff.py.

◆ SECTION_PADDR

int bin_to_coff.SECTION_PADDR = 0x0

Definition at line 65 of file bin_to_coff.py.

◆ SECTION_RAW_DATA_PTR

tuple bin_to_coff.SECTION_RAW_DATA_PTR
Initial value:
1= (
2 FILE_HEADER_SIZE + FILE_HEADER_NUM_SECTIONS * SECTION_HEADER_SIZE)

Definition at line 67 of file bin_to_coff.py.

◆ SECTION_RELOCATION_PTR

int bin_to_coff.SECTION_RELOCATION_PTR = 0x0

Definition at line 69 of file bin_to_coff.py.

◆ SECTION_VADDR

int bin_to_coff.SECTION_VADDR = 0x0

Definition at line 66 of file bin_to_coff.py.

◆ SIZE_FORMAT

string bin_to_coff.SIZE_FORMAT = 'I'

Definition at line 100 of file bin_to_coff.py.

◆ SIZE_LENGTH

bin_to_coff.SIZE_LENGTH = calcsize(SIZE_FORMAT)

Definition at line 101 of file bin_to_coff.py.

◆ SIZE_SYMBOL_FORMAT_X64

string bin_to_coff.SIZE_SYMBOL_FORMAT_X64 = 'Q'

Definition at line 103 of file bin_to_coff.py.

◆ SIZE_SYMBOL_LENGTH_X64

bin_to_coff.SIZE_SYMBOL_LENGTH_X64 = calcsize(SIZE_SYMBOL_FORMAT_X64)

Definition at line 104 of file bin_to_coff.py.

◆ STRING_TABLE_OFFSET

int bin_to_coff.STRING_TABLE_OFFSET = 0x4

Definition at line 99 of file bin_to_coff.py.

◆ SYMBOL_TABLE_ENTRY_CLASS

int bin_to_coff.SYMBOL_TABLE_ENTRY_CLASS = 2

Definition at line 96 of file bin_to_coff.py.

◆ SYMBOL_TABLE_ENTRY_FORMAT_LONG

string bin_to_coff.SYMBOL_TABLE_ENTRY_FORMAT_LONG = 'IIIhHBB'

Definition at line 91 of file bin_to_coff.py.

◆ SYMBOL_TABLE_ENTRY_FORMAT_SHORT

string bin_to_coff.SYMBOL_TABLE_ENTRY_FORMAT_SHORT = '8sIhHBB'

Definition at line 90 of file bin_to_coff.py.

◆ SYMBOL_TABLE_ENTRY_NUM_AUX

int bin_to_coff.SYMBOL_TABLE_ENTRY_NUM_AUX = 0

Definition at line 97 of file bin_to_coff.py.

◆ SYMBOL_TABLE_ENTRY_SECTION

int bin_to_coff.SYMBOL_TABLE_ENTRY_SECTION = 1

Definition at line 94 of file bin_to_coff.py.

◆ SYMBOL_TABLE_ENTRY_SHORT_LEN

int bin_to_coff.SYMBOL_TABLE_ENTRY_SHORT_LEN = 8

Definition at line 89 of file bin_to_coff.py.

◆ SYMBOL_TABLE_ENTRY_SIZE

bin_to_coff.SYMBOL_TABLE_ENTRY_SIZE = calcsize(SYMBOL_TABLE_ENTRY_FORMAT_SHORT)

Definition at line 92 of file bin_to_coff.py.

◆ SYMBOL_TABLE_ENTRY_TYPE

int bin_to_coff.SYMBOL_TABLE_ENTRY_TYPE = 0

Definition at line 95 of file bin_to_coff.py.

◆ SYMBOL_TABLE_ENTRY_ZEROS

int bin_to_coff.SYMBOL_TABLE_ENTRY_ZEROS = 0x0

Definition at line 93 of file bin_to_coff.py.