Parse ELF Section Sizes

Hard Python Build Asked at ARM
Solve this problem

Read an ELF section header table and report the flash and RAM footprint of a firmware build, the way a size tool does.

Constraints

  • Parse the 32-bit little-endian ELF header and section headers.
  • Report text, data and bss sizes.
  • Flash usage is text plus data; RAM usage is data plus bss.
  • A file whose magic is not 0x7F 'ELF' must raise ValueError.
  • Use only the struct module.
Starter codePython
1import struct2 3def elf_sizes(blob):4    # TODO5    return {}