Skip to content

Commit

Permalink
Fix python lint issues in Tools/elf2hex.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kubamracek committed Jan 16, 2025
1 parent 08e9a6e commit 28a8a05
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions Tools/elf2hex.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
# See https://swift.org/LICENSE.txt for license information

#
# elf2hex -- Converts a statically-linked ELF executable into an "Intel HEX" file format suitable for flashing onto some
# embedded devices.
# elf2hex -- Converts a statically-linked ELF executable into an "Intel HEX"
# file format suitable for flashing onto some embedded devices.
#
# Usage:
# $ elf2hex.py <input> <output> [--symbol-map <output>]
Expand All @@ -19,11 +19,12 @@
#

import argparse
import os
import pathlib
import json
import pathlib

import elftools.elf.elffile


def main():
parser = argparse.ArgumentParser()
parser.add_argument('input')
Expand All @@ -38,7 +39,7 @@ def emitrecord(record):
checksum = 0
pos = 0
while pos < len(record):
checksum = (checksum + int(record[pos:pos+2], 16)) % 256
checksum = (checksum + int(record[pos:pos + 2], 16)) % 256
pos += 2
checksum = (256 - checksum) % 256
outf.write((":" + record + f"{checksum:02X}" + "\n").encode())
Expand All @@ -47,39 +48,43 @@ def emit(vmaddr, data):
pos = 0
while pos < len(data):
chunklen = min(16, len(data) - pos)
chunk = data[pos:pos+chunklen]
chunk = data[pos:pos + chunklen]
chunkhex = chunk.hex().upper()

assert vmaddr < 0x100000000, f"vmaddr: {vmaddr:x}"
vmaddr_high = (vmaddr >> 16) & 0xffff
recordtype = "04" # Extended Linear Address
recordtype = "04" # Extended Linear Address
emitrecord(f"{2:02X}{0:04X}{recordtype}{vmaddr_high:04X}")

vmaddr_low = vmaddr & 0xffff
recordtype = "00" # Data
recordtype = "00" # Data
emitrecord(f"{chunklen:02X}{vmaddr_low:04X}{recordtype}{chunkhex}")

pos += chunklen
vmaddr += chunklen

elffile = elftools.elf.elffile.ELFFile(inf)
for segment in elffile.iter_segments():
if segment.header.p_type != "PT_LOAD": continue
if segment.header.p_type != "PT_LOAD":
continue
vmaddr = segment.header.p_paddr
data = segment.data()
emit(segment.header.p_paddr, data)

chunklen = 0
vmaddr = 0
recordtype = "01" # EOF
recordtype = "01" # EOF
emitrecord(f"{chunklen:02X}{vmaddr:04X}{recordtype}")

symbol_map = {}
symtab_section = elffile.get_section_by_name(".symtab")
for s in symtab_section.iter_symbols():
if s.entry.st_info.type not in ["STT_FUNC", "STT_NOTYPE"]: continue
if s.entry.st_shndx == "SHN_ABS": continue
if s.name == "": continue
if s.entry.st_info.type not in ["STT_FUNC", "STT_NOTYPE"]:
continue
if s.entry.st_shndx == "SHN_ABS":
continue
if s.name == "":
continue
symbol_map[s.name] = s.entry.st_value

if args.symbol_map is not None:
Expand All @@ -88,5 +93,6 @@ def emit(vmaddr, data):
inf.close()
outf.close()


if __name__ == '__main__':
main()

0 comments on commit 28a8a05

Please sign in to comment.