Skip to content

Commit c4ed177

Browse files
committed
Merge pull request #68 from 0xc0170/dev_gcc_sizes
gcc - print section sizes
2 parents 373fb55 + c357216 commit c4ed177

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

tools/hooks.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
_running_hooks = {}
1212

1313
# Available hook types
14-
_hook_types = ["binary", "compile", "link", "assemble"]
14+
_hook_types = ["binary", "compile", "link", "assemble", "static_sizes"]
1515

1616
# Available hook steps
1717
_hook_steps = ["pre", "replace", "post"]
@@ -103,6 +103,9 @@ def hook_cmdline_assembler(self, function):
103103
def hook_cmdline_binary(self, function):
104104
return self._hook_cmdline("binary", function)
105105

106+
def hook_cmdline_static_sizes(self, function):
107+
return self._hook_cmdline("static_sizes", function)
108+
106109
# Return the command line after applying the hook
107110
def _get_cmdline(self, hook_type, cmdline):
108111
if self._cmdline_hooks.has_key(hook_type):

tools/toolchains/__init__.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
from os.path import join, splitext, exists, relpath, dirname, basename, split
2626
from inspect import getmro
2727

28+
from elftools.elf.elffile import ELFFile
29+
2830
from multiprocessing import Pool, cpu_count
2931
from tools.utils import run_cmd, mkdir, rel_path, ToolException, NotSupportedException, split_path
3032
from tools.settings import BUILD_OPTIONS, MBED_ORG_USER
@@ -701,6 +703,11 @@ def link_program(self, r, tmp_path, name):
701703

702704
self.binary(r, elf, bin)
703705

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+
704711
self.var("compile_succeded", True)
705712
self.var("binary", filename)
706713

@@ -757,6 +764,41 @@ def tool_error(self, message):
757764
def var(self, key, value):
758765
self.notify({'type': 'var', 'key': key, 'val': value})
759766

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+
760802
from tools.settings import ARM_BIN
761803
from tools.settings import GCC_ARM_PATH, GCC_CR_PATH
762804
from tools.settings import IAR_PATH

0 commit comments

Comments
 (0)