什么是c/c ++数据段和堆栈大小? [英] What is c/c++ data segment and stack size?

查看:90
本文介绍了什么是c/c ++数据段和堆栈大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到,这取决于编译器和操作系统体系结构.如何在使用GCC作为编译器的Linux系统上找出数据段和最大堆栈大小?

解决方案

让我尝试一下:创建文件"test.c",如下所示:

int main (void) { return 0; }

现在编译它,指定最大堆栈大小(只是为了轻松在映射文件中查找此数字并确定符号名称,请参考它):

gcc test.c -o test.x -Wl,--stack=0x20000 -Wl,-Map=output.map

确定数据大小很简单:

size -A -d test.x

您将获得以下内容:

section           size         addr
.text             1880   4299165696
.data              104   4299169792
...

同样``objdump -h test.x''可以正常工作,但结果较少.

这里有更多部分(不仅是代码和数据),但这里没有堆栈信息.为什么?由于堆栈大小不是ELF节,因此只有在加载要执行的程序后才保留堆栈大小.您应该像这样从文件中的一些(取决于平台的)符号中读取它:

$ nm test.x  | grep __size_of_stack_reserve__
0000000000020000 A __size_of_stack_reserve__

不足为奇,它的大小为0x20000,如编译时所述.<​​/p>

我通过查看在编译期间生成的output.map文件来确定符号名称.我建议您也从研究它开始.

接下来,当您确实有一些未知文件a.out时,只需重复序列:

size -A -d a.out
nm a.out | grep __size_of_stack_reserve__

用您在实验中确定的平台相关符号代替上述符号.

I read that it depends on the compiler and operating system architecture. How do I find out the data segment and stack max size on a Linux system using GCC as compiler?

解决方案

Let me experiment with you: create file ``test.c'' like this:

int main (void) { return 0; }

Now compile it, specifying max stack size (just to easy lookup this number in map file and determine symbol name, refering to it):

gcc test.c -o test.x -Wl,--stack=0x20000 -Wl,-Map=output.map

Determining data size is simple:

size -A -d test.x

You will get something like this:

section           size         addr
.text             1880   4299165696
.data              104   4299169792
...

Also ``objdump -h test.x'' will work fine but with less verbose results.

There is more sections here (not just code and data) but there is no stack information here. Why? Because stack size is not ELF section, it is reserved only after your program is loaded to be executed. You should read it from some (platform dependent) symbol in your file like this:

$ nm test.x  | grep __size_of_stack_reserve__
0000000000020000 A __size_of_stack_reserve__

It is not surprising, that size is 0x20000, as it was stated when compiling.

I determined symbol name by looking into output.map file that was generated during compilation. I recommend you also to start from looking at it.

Next when you do have some unknown file a.out, just repeat sequence:

size -A -d a.out
nm a.out | grep __size_of_stack_reserve__

Substituting a platform dependent symbol to that, you determined in experiment, described above.

这篇关于什么是c/c ++数据段和堆栈大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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