|
25 | 25 | from os.path import join, splitext, exists, relpath, dirname, basename, split
|
26 | 26 | from inspect import getmro
|
27 | 27 |
|
| 28 | +from elftools.elf.elffile import ELFFile |
| 29 | + |
28 | 30 | from multiprocessing import Pool, cpu_count
|
29 | 31 | from tools.utils import run_cmd, mkdir, rel_path, ToolException, NotSupportedException, split_path
|
30 | 32 | from tools.settings import BUILD_OPTIONS, MBED_ORG_USER
|
@@ -701,6 +703,11 @@ def link_program(self, r, tmp_path, name):
|
701 | 703 |
|
702 | 704 | self.binary(r, elf, bin)
|
703 | 705 |
|
| 706 | + self.info("Memory sections sizes:") |
| 707 | + size_dict = self.static_sizes(elf) |
| 708 | + for section, size in size_dict.iteritems(): |
| 709 | + print("{:20} {}".format(section, size)) |
| 710 | + |
704 | 711 | self.var("compile_succeded", True)
|
705 | 712 | self.var("binary", filename)
|
706 | 713 |
|
@@ -757,6 +764,41 @@ def tool_error(self, message):
|
757 | 764 | def var(self, key, value):
|
758 | 765 | self.notify({'type': 'var', 'key': key, 'val': value})
|
759 | 766 |
|
| 767 | + def static_sizes(self, elf): |
| 768 | + """Accepts elf, returns a dict sizes per section (text, data, bss)""" |
| 769 | + section_sizes = {} |
| 770 | + |
| 771 | + SHF_WRITE = 0x1 |
| 772 | + SHF_ALLOC = 0x2 |
| 773 | + SHF_EXECINSTR = 0x4 |
| 774 | + SHT_PROGBITS = "SHT_PROGBITS" |
| 775 | + SHT_NOBITS = "SHT_NOBITS" |
| 776 | + |
| 777 | + text = 0 |
| 778 | + data = 0 |
| 779 | + bss = 0 |
| 780 | + with open(elf, 'rb') as f: |
| 781 | + elffile = ELFFile(f) |
| 782 | + for section in elffile.iter_sections(): |
| 783 | + flags = section['sh_flags'] |
| 784 | + size = section['sh_size'] |
| 785 | + if (flags & SHF_ALLOC) == 0: |
| 786 | + # Section has no relevant data so ignore it |
| 787 | + continue |
| 788 | + if (flags & SHF_EXECINSTR) or not (flags & SHF_WRITE): |
| 789 | + # Executable code or read only data |
| 790 | + text += size |
| 791 | + elif section['sh_type'] != SHT_NOBITS: |
| 792 | + # Non-zero read/write data |
| 793 | + data += size |
| 794 | + else: |
| 795 | + # Zero init read/write data |
| 796 | + bss += size |
| 797 | + section_sizes["text"] = text |
| 798 | + section_sizes["data"] = data |
| 799 | + section_sizes["bss"] = bss |
| 800 | + return section_sizes |
| 801 | + |
760 | 802 | from tools.settings import ARM_BIN
|
761 | 803 | from tools.settings import GCC_ARM_PATH, GCC_CR_PATH
|
762 | 804 | from tools.settings import IAR_PATH
|
|
0 commit comments