|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Display the segment sizes used by an ELF |
| 4 | +# |
| 5 | +# Copyright (C) 2019 - Earle F. Philhower, III |
| 6 | +# |
| 7 | +# This program is free software: you can redistribute it and/or modify |
| 8 | +# it under the terms of the GNU General Public License as published by |
| 9 | +# the Free Software Foundation, either version 3 of the License, or |
| 10 | +# (at your option) any later version. |
| 11 | +# |
| 12 | +# This program is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +# GNU General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU General Public License |
| 18 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +from __future__ import print_function |
| 21 | +import argparse |
| 22 | +import os |
| 23 | +import subprocess |
| 24 | +import sys |
| 25 | + |
| 26 | +def get_segment_sizes(elf, path): |
| 27 | + sizes = {} |
| 28 | + sizes['IROM'] = 0 |
| 29 | + sizes['IRAM'] = 0 |
| 30 | + sizes['DATA'] = 0 |
| 31 | + sizes['RODATA'] = 0 |
| 32 | + sizes['BSS'] = 0 |
| 33 | + p = subprocess.Popen([path + "/xtensa-lx106-elf-size", '-A', elf], stdout=subprocess.PIPE, universal_newlines=True ) |
| 34 | + lines = p.stdout.readlines() |
| 35 | + for line in lines: |
| 36 | + words = line.split() |
| 37 | + if line.startswith('.irom0.text'): |
| 38 | + sizes['IROM'] = sizes['IROM'] + int(words[1]) |
| 39 | + elif line.startswith('.text'): # Gets .text and .text1 |
| 40 | + sizes['IRAM'] = sizes['IRAM'] + int(words[1]) |
| 41 | + elif line.startswith('.data'): # Gets .text and .text1 |
| 42 | + sizes['DATA'] = sizes['DATA'] + int(words[1]) |
| 43 | + elif line.startswith('.rodata'): # Gets .text and .text1 |
| 44 | + sizes['RODATA'] = sizes['RODATA'] + int(words[1]) |
| 45 | + elif line.startswith('.bss'): # Gets .text and .text1 |
| 46 | + sizes['BSS'] = sizes['BSS'] + int(words[1]) |
| 47 | + return sizes |
| 48 | + |
| 49 | +def main(): |
| 50 | + parser = argparse.ArgumentParser(description='Report the different segment sizes of a compiled ELF file') |
| 51 | + parser.add_argument('-e', '--elf', action='store', required=True, help='Path to the Arduino sketch ELF') |
| 52 | + parser.add_argument('-p', '--path', action='store', required=True, help='Path to Xtensa toolchain binaries') |
| 53 | + |
| 54 | + args = parser.parse_args() |
| 55 | + sizes = get_segment_sizes(args.elf, args.path) |
| 56 | + |
| 57 | + sys.stderr.write("Executable segment sizes:" + os.linesep) |
| 58 | + for k in sizes.keys(): |
| 59 | + sys.stderr.write("%-7s: %d%s" % (k, sizes[k], os.linesep)) |
| 60 | + |
| 61 | + return 0 |
| 62 | + |
| 63 | +if __name__ == '__main__': |
| 64 | + sys.exit(main()) |
0 commit comments