88# See https://swift.org/LICENSE.txt for license information
99
1010#
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.
1313#
1414# Usage:
1515# $ elf2hex.py <input> <output> [--symbol-map <output>]
1919#
2020
2121import argparse
22- import os
23- import pathlib
2422import json
23+ import pathlib
24+
2525import elftools .elf .elffile
2626
27+
2728def main ():
2829 parser = argparse .ArgumentParser ()
2930 parser .add_argument ('input' )
@@ -38,7 +39,7 @@ def emitrecord(record):
3839 checksum = 0
3940 pos = 0
4041 while pos < len (record ):
41- checksum = (checksum + int (record [pos :pos + 2 ], 16 )) % 256
42+ checksum = (checksum + int (record [pos :pos + 2 ], 16 )) % 256
4243 pos += 2
4344 checksum = (256 - checksum ) % 256
4445 outf .write ((":" + record + f"{ checksum :02X} " + "\n " ).encode ())
@@ -47,39 +48,43 @@ def emit(vmaddr, data):
4748 pos = 0
4849 while pos < len (data ):
4950 chunklen = min (16 , len (data ) - pos )
50- chunk = data [pos :pos + chunklen ]
51+ chunk = data [pos :pos + chunklen ]
5152 chunkhex = chunk .hex ().upper ()
5253
5354 assert vmaddr < 0x100000000 , f"vmaddr: { vmaddr :x} "
5455 vmaddr_high = (vmaddr >> 16 ) & 0xffff
55- recordtype = "04" # Extended Linear Address
56+ recordtype = "04" # Extended Linear Address
5657 emitrecord (f"{ 2 :02X} { 0 :04X} { recordtype } { vmaddr_high :04X} " )
5758
5859 vmaddr_low = vmaddr & 0xffff
59- recordtype = "00" # Data
60+ recordtype = "00" # Data
6061 emitrecord (f"{ chunklen :02X} { vmaddr_low :04X} { recordtype } { chunkhex } " )
6162
6263 pos += chunklen
6364 vmaddr += chunklen
6465
6566 elffile = elftools .elf .elffile .ELFFile (inf )
6667 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
6870 vmaddr = segment .header .p_paddr
6971 data = segment .data ()
7072 emit (segment .header .p_paddr , data )
7173
7274 chunklen = 0
7375 vmaddr = 0
74- recordtype = "01" # EOF
76+ recordtype = "01" # EOF
7577 emitrecord (f"{ chunklen :02X} { vmaddr :04X} { recordtype } " )
7678
7779 symbol_map = {}
7880 symtab_section = elffile .get_section_by_name (".symtab" )
7981 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
8388 symbol_map [s .name ] = s .entry .st_value
8489
8590 if args .symbol_map is not None :
@@ -88,5 +93,6 @@ def emit(vmaddr, data):
8893 inf .close ()
8994 outf .close ()
9095
96+
9197if __name__ == '__main__' :
9298 main ()
0 commit comments