测量移植到嵌入式平台的C ++的静态内存使用 [英] Measure static memory usage for C++ ported to embedded platform

查看:142
本文介绍了测量移植到嵌入式平台的C ++的静态内存使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个小程序作为要在嵌入式平台上实现的系统的概念验证。该程序用C ++ 11编写,使用std编译并运行在笔记本电脑上。以后应该实现的最终程序是嵌入式系统。我们没有访问嵌入式平台的编译器。

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

我想知道如果有一个方法来确定程序静态内存(编译二进制文件的大小)在一个明智的和可比的方式,当它应该被移植到嵌入式平台。
要求是二进制文件的大小小于10kb。
当编译和剥离时,我们的二进制文件的大小为700Kb,具有以下标志:

I would like to know if there is a way to determine a programs static memory (the size of the compiled binaries) in a sensible and comparable way when it should be ported to an embedded platform. The requirement is that the size of the binary is less than 10kb. Our binary has a size of 700Kb when compiled and stripped 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

在我们使用strip和优化选项之前,它占用了4MB。

It took up 4MB before we used strip and optimization options.

我仍然离开,它不是真的那么大的程序。我如何证明与嵌入式平台上的等效程序的任何方式的比较。

I am still way off and it is not really that big a program. How can I justify a comparison in any way with an equivalent program on an embedded platform.

推荐答案

请注意,二进制文件的大小可能有点欺骗性,未初始化的变量,.bss节不一定占用二进制文件中的物理空间,因为这些通常只是注意到的,而实际上没有给他们任何空间...这通常发生在OS加载程序运行你的程序时。

Note that the size of the binary can be a little deceptive in the sense that uninitialised variables, the .bss sections, will not necessarily take up physical space in the binary as these are generally just noted as present without actually have any space given to them... this normally happens by the OS loader when it runs your program.

objdump http:/ /www.gnu.org/software/binutils/ )或可能 elfdump 或elf工具链( http://sourceforge.net/apps/trac/elftoolchain/ )将帮助您确定各种细分,数据和文本的大小作为单个函数和全局变量的大小等。所有这些程序看到你编译的二进制文件,并提取了很多信息,如.text,.data节的大小,列出各种符号,它们的位置和大小,甚至可以解构.text节...

objdump (http://www.gnu.org/software/binutils/) or perhaps elfdump or the elf tool chain (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 and extract a lot of information such as the size of the .text, .data section, list the various symbols, their locations and sizes, and can even dissasemble the .text section...

在ELF映像test.elf上使用elfdump的示例可能是 elfdump -z test。 elf> output.txt 。这将转储一切,包括文本节拆分。例如,从我的系统上的 elfdump 我看到

An example of using elfdump on an ELF image test.elf might be elfdump -z test.elf > output.txt. This will dump everything including text section dissassembly. For example, from an 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 can see how much my code is taking up (the .text sections) and my read only data. Later in the file I then see...

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

所以我可以看到我的函数 IRemovedThisName 需要130个字节。快速脚本将允许列出按大小排序的函数和按大小排序的变量。

So I can see that my function IRemovedThisName takes 130 bytes. A quick script would allow you list functions sorted by size and variables sorted by size. This could point you at places to optimize...

有关 objdump 的一个很好的例子,请尝试 http://www.thegeekstuff.com/2012/09/objdump-examples/ ,具体第3节,它显示如何使用 -h 选项获取节头的内容。

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

至于程序将如何在两个不同的平台上进行比较我认为你只需要在两个平台上进行编译,并比较每个系统上从 obj / elfdump 获得的结果 - 结果将取决于系统指令集,每个编译器可以优化的程度,通用硬件架构差异等。

As to how the program will compare on two different platforms I think you will just have to compile on both platforms and compare the results you get from your obj/elfdump on each system - the results will depend on the system instruction set, how well each compiler can optimize, general hardware architecture differences etc.

如果您无法访问嵌入式系统,您可以尝试在笔记本电脑上使用为最终目标配置的交叉编译器。这将给你一个适合嵌入式平台和工具来分析文件(即跨平台版本 objdump )的二进制文件。这将给你一些球公园数字如何程序将看起来最终的嵌入式系统。

If you don't have access to the embedded system, you might try using a cross-compiler, configured for your eventual target, on your laptop. This would give you a binary suited to the embedded platform and the tools to analyze the file (i.e. the cross-platform version of objdump). This would give you some ball-park figures for how the program would look on the eventual embedded sys.

希望这有助于。

EDIT:这也将帮助如何获取尺寸的C函数?

This will also help How to get the size of a C function?

这篇关于测量移植到嵌入式平台的C ++的静态内存使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆