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

查看:126
本文介绍了测量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

在使用条带和优化选项之前,花了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图像上使用elfdump的一个例子test.elf可能是 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>

所以我可以看到我的代码占用了多少(.text部分)和我的只读数据。稍后我在文件中看到...

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.

希望这有帮助。

编辑:这也将有助于如何获取大小C程序中的C函数或内联汇编?

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

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