Measuring static memory usage for C ++ ported to the embedded platform

I created a small program as a proof of concept for a system that should be implemented on an embedded platform. The program is written in C ++ 11 using std and compiled to work on a laptop. The final program, which should be implemented later, is an embedded system. We do not have access to the embedded platform compiler.

I would like to know if there is a way to determine the static memory of programs (the size of the compiled binaries) in a reasonable and comparable way, when it should be ported to the embedded platform. The requirement is that the binary file size is less than 10 KB. Our binary file has a size of 700 KB when compiling and markup with the following flags:

g++ options: -Os -s -ffunction-sections -fdata-sections linker options: -s -Wl,--gc-sections strip libmodel.a -s -R .comment -R .gnu.version --strip-unneeded -R .note 

It took 4 MB before we used the bandwidth and optimization options.

I am still far away, and this is not a very big program. How can I justify a comparison in any way with an equivalent program on an embedded platform.

+8
c ++ optimization embedded g ++
source share
2 answers

Please note that the size of the binary file can be a little misleading in the sense that uninitialized variables, .bss sections, will not necessarily occupy the physical space in binary format, since they are usually simply marked as present, in fact, the space provided to them ... this usually happens by the bootloader when you start your program.

objdump ( http://www.gnu.org/software/binutils/ ) or maybe elfdump or the elf elfdump ( http://sourceforge.net/apps/trac/elftoolchain/ ) will help you determine the size of your various segments, data and text, as well as the size of individual functions and globals, etc. All these programs β€œlook” into your compiled binary file and extract large information, such as the size of the .text, .data section, list the various characters, their locations and sizes, and you can even decompose the .text section ...

An example of using elfdump in the test.elf ELF image is elfdump -z test.elf > output.txt . This resets everything, including disassembling the text section. For example, from elfdump on my system, I saw

 Section #6: .text, type=NOBITS, addr=0x500, off=0x5f168 size=149404(0x2479c), link=0, info=0, align=16, entsize=1 flags=<WRITE,ALLOC,EXECINSTR> Section #7: .text, type=NOBITS, addr=0x24c9c, off=0x5f168 size=362822(0x58946), link=0, info=0, align=4, entsize=1 flags=<WRITE,ALLOC,EXECINSTR,INCLUDE> .... Section #9: .rodata, type=NOBITS, addr=0x7d5e4, off=0x5f168 size=7670(0x1df6), link=0, info=0, align=4, entsize=1 flags=<WRITE,ALLOC> 

So, I see how much my code (.text sections) and my data are read-only. Later in the file I see ...

 Symbol table ".symtab" Value Size Bind Type Section Name ----- ---- ---- ---- ------- ---- 218 0x7c090 130 LOC FUNC .text IRemovedThisName 

So, I see that my IRemovedThisName function takes 130 bytes. A quick script will allow you to list functions sorted by size and variables sorted by size. This may indicate locations for optimization ...

For a good example of objdump try http://www.thegeekstuff.com/2012/09/objdump-examples/ , specifically section 3, which shows how to get the contents of section headers using the -h option.

As for how the program will compare on two different platforms, I think you just need to compile on both platforms and compare the results obtained with your obj/elfdump on each system - the results will depend on the set of system commands, how well each the compiler can optimize general differences in hardware architecture, etc.

If you don’t have access to the embedded system, you can try using the cross-compiler, configured for your ultimate goal, on your laptop. This will give you binary code suitable for the embedded platform, and tools for analyzing the file (i.e. the cross-platform version of objdump ). This will give you some ball details for how the program will look on embedded systems.

Hope this helps.

EDIT: This will also help. How do I get the size of a C function from a C program or inline assembly?

+4
source share

It seems that the included libraries took up a huge space (as indicated in the comment), and by deleting them, it was possible to reduce the size to almost zero in combination with the following flags:

 set(CMAKE_CXX_FLAGS "-Os -s -ffunction-sections -fdata-sections -DNO_STD -fno-rtti -fno-exceptions") set(CMAKE_EXE_LINKER_FLAGS "-s -Wl,--gc-sections") 

And remove the unnecessary code using:

 strip libmodel.a -s -R .comment -R .gnu.version --strip-unneeded -R .note 

4 MB can be reduced to 9.4 KB, which is below our limit.

Thus, std occupies a huge space.

+1
source share

All Articles