8
8
# See https://swift.org/LICENSE.txt for license information
9
9
10
10
#
11
- # elf2hex -- Converts a statically-linked ELF executable into an "Intel HEX" file format suitable for flashing onto some
12
- # embedded devices.
11
+ # elf2hex -- Converts a statically-linked ELF executable into an "Intel HEX"
12
+ # file format suitable for flashing onto some embedded devices.
13
13
#
14
14
# Usage:
15
15
# $ elf2hex.py <input> <output> [--symbol-map <output>]
19
19
#
20
20
21
21
import argparse
22
- import os
23
- import pathlib
24
22
import json
23
+ import pathlib
24
+
25
25
import elftools .elf .elffile
26
26
27
+
27
28
def main ():
28
29
parser = argparse .ArgumentParser ()
29
30
parser .add_argument ('input' )
@@ -38,7 +39,7 @@ def emitrecord(record):
38
39
checksum = 0
39
40
pos = 0
40
41
while pos < len (record ):
41
- checksum = (checksum + int (record [pos :pos + 2 ], 16 )) % 256
42
+ checksum = (checksum + int (record [pos :pos + 2 ], 16 )) % 256
42
43
pos += 2
43
44
checksum = (256 - checksum ) % 256
44
45
outf .write ((":" + record + f"{ checksum :02X} " + "\n " ).encode ())
@@ -47,39 +48,43 @@ def emit(vmaddr, data):
47
48
pos = 0
48
49
while pos < len (data ):
49
50
chunklen = min (16 , len (data ) - pos )
50
- chunk = data [pos :pos + chunklen ]
51
+ chunk = data [pos :pos + chunklen ]
51
52
chunkhex = chunk .hex ().upper ()
52
53
53
54
assert vmaddr < 0x100000000 , f"vmaddr: { vmaddr :x} "
54
55
vmaddr_high = (vmaddr >> 16 ) & 0xffff
55
- recordtype = "04" # Extended Linear Address
56
+ recordtype = "04" # Extended Linear Address
56
57
emitrecord (f"{ 2 :02X} { 0 :04X} { recordtype } { vmaddr_high :04X} " )
57
58
58
59
vmaddr_low = vmaddr & 0xffff
59
- recordtype = "00" # Data
60
+ recordtype = "00" # Data
60
61
emitrecord (f"{ chunklen :02X} { vmaddr_low :04X} { recordtype } { chunkhex } " )
61
62
62
63
pos += chunklen
63
64
vmaddr += chunklen
64
65
65
66
elffile = elftools .elf .elffile .ELFFile (inf )
66
67
for segment in elffile .iter_segments ():
67
- if segment .header .p_type != "PT_LOAD" : continue
68
+ if segment .header .p_type != "PT_LOAD" :
69
+ continue
68
70
vmaddr = segment .header .p_paddr
69
71
data = segment .data ()
70
72
emit (segment .header .p_paddr , data )
71
73
72
74
chunklen = 0
73
75
vmaddr = 0
74
- recordtype = "01" # EOF
76
+ recordtype = "01" # EOF
75
77
emitrecord (f"{ chunklen :02X} { vmaddr :04X} { recordtype } " )
76
78
77
79
symbol_map = {}
78
80
symtab_section = elffile .get_section_by_name (".symtab" )
79
81
for s in symtab_section .iter_symbols ():
80
- if s .entry .st_info .type not in ["STT_FUNC" , "STT_NOTYPE" ]: continue
81
- if s .entry .st_shndx == "SHN_ABS" : continue
82
- if s .name == "" : continue
82
+ if s .entry .st_info .type not in ["STT_FUNC" , "STT_NOTYPE" ]:
83
+ continue
84
+ if s .entry .st_shndx == "SHN_ABS" :
85
+ continue
86
+ if s .name == "" :
87
+ continue
83
88
symbol_map [s .name ] = s .entry .st_value
84
89
85
90
if args .symbol_map is not None :
@@ -88,5 +93,6 @@ def emit(vmaddr, data):
88
93
inf .close ()
89
94
outf .close ()
90
95
96
+
91
97
if __name__ == '__main__' :
92
98
main ()
0 commit comments