Flutter Engine
The Flutter Engine
bin_to_coff.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2#
3# Copyright (c) 2018, 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
7import argparse
8from ctypes import create_string_buffer
9from struct import *
10
11# FILE HEADER FLAGS
12FILE_HEADER_RELFLG = 0x1 # No relocation information
13FILE_HEADER_EXEC = 0x2 # Executable
14FILE_HEADER_LNNO = 0x4 # No line number information
15FILE_HEADER_LSYMS = 0x8 # Local symbols removed / not present
16FILE_HEADER_AR32WR = 0x100 # File is 32-bit little endian
17
18# SECTION HEADER FLAGS
19SECTION_HEADER_TEXT = 0x20 # Contains executable code
20SECTION_HEADER_DATA = 0x40 # Contains only initialized data
21SECTION_HEADER_BSS = 0x80 # Contains uninitialized data
22
23# FILE HEADER FORMAT
24# typedef struct {
25# unsigned short f_magic; /* magic number */
26# unsigned short f_nscns; /* number of sections */
27# unsigned long f_timdat; /* time & date stamp */
28# unsigned long f_symptr; /* file pointer to symtab */
29# unsigned long f_nsyms; /* number of symtab entries */
30# unsigned short f_opthdr; /* sizeof(optional hdr) */
31# unsigned short f_flags; /* flags */
32# } FILHDR;
33FILE_HEADER_FORMAT = 'HHIIIHH'
34FILE_HEADER_SIZE = calcsize(FILE_HEADER_FORMAT)
35FILE_HEADER_MAGICS = {
36 'x86': 0x014c,
37 'x64': 0x8664,
38 'arm': 0x1c0,
39 'arm64': 0xaa64,
40 'riscv32': 0x5032,
41 'riscv64': 0x5064,
42}
43FILE_HEADER_NUM_SECTIONS = 1
44FILE_HEADER_TIMESTAMP = 0
45FILE_HEADER_SIZE_OF_OPTIONAL = 0
46FILE_HEADER_FLAGS = FILE_HEADER_LNNO
47
48# SECTION HEADER FORMAT
49# typedef struct {
50# char s_name[8]; /* section name */
51# unsigned long s_paddr; /* physical address, aliased s_nlib */
52# unsigned long s_vaddr; /* virtual address */
53# unsigned long s_size; /* section size */
54# unsigned long s_scnptr; /* file ptr to raw data for section */
55# unsigned long s_relptr; /* file ptr to relocation */
56# unsigned long s_lnnoptr; /* file ptr to line numbers */
57# unsigned short s_nreloc; /* number of relocation entries */
58# unsigned short s_nlnno; /* number of line number entries */
59# unsigned long s_flags; /* flags */
60# } SCNHDR;
61SECTION_HEADER_FORMAT = '8sIIIIIIHHI'
62SECTION_HEADER_SIZE = calcsize(SECTION_HEADER_FORMAT)
63SECTION_NAME_RODATA = b'.rodata'
64SECTION_NAME_TEXT = b'.text'
65SECTION_PADDR = 0x0
66SECTION_VADDR = 0x0
67SECTION_RAW_DATA_PTR = (
68 FILE_HEADER_SIZE + FILE_HEADER_NUM_SECTIONS * SECTION_HEADER_SIZE)
69SECTION_RELOCATION_PTR = 0x0
70SECTION_LINE_NUMS_PTR = 0x0
71SECTION_NUM_RELOCATION = 0
72SECTION_NUM_LINE_NUMS = 0
73
74# SYMBOL TABLE FORMAT
75# typedef struct {
76# union {
77# char e_name[8];
78# struct {
79# unsigned long e_zeroes;
80# unsigned long e_offset;
81# } e;
82# } e;
83# unsigned long e_value;
84# short e_scnum;
85# unsigned short e_type;
86# unsigned char e_sclass;
87# unsigned char e_numaux;
88# } SYMENT;
89SYMBOL_TABLE_ENTRY_SHORT_LEN = 8
90SYMBOL_TABLE_ENTRY_FORMAT_SHORT = '8sIhHBB'
91SYMBOL_TABLE_ENTRY_FORMAT_LONG = 'IIIhHBB'
92SYMBOL_TABLE_ENTRY_SIZE = calcsize(SYMBOL_TABLE_ENTRY_FORMAT_SHORT)
93SYMBOL_TABLE_ENTRY_ZEROS = 0x0
94SYMBOL_TABLE_ENTRY_SECTION = 1
95SYMBOL_TABLE_ENTRY_TYPE = 0
96SYMBOL_TABLE_ENTRY_CLASS = 2 # External (public) symbol.
97SYMBOL_TABLE_ENTRY_NUM_AUX = 0 # Number of auxiliary entries.
98
99STRING_TABLE_OFFSET = 0x4 # Starting offset for the string table.
100SIZE_FORMAT = 'I'
101SIZE_LENGTH = calcsize(SIZE_FORMAT)
102
103SIZE_SYMBOL_FORMAT_X64 = 'Q'
104SIZE_SYMBOL_LENGTH_X64 = calcsize(SIZE_SYMBOL_FORMAT_X64)
105
106
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
260if __name__ == '__main__':
261 main()
Definition: main.py:1