加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

使用arm-linux-gnueabi-gcc编译时,为什么堆栈指针比堆栈帧大小下

发布时间:2020-12-13 22:55:43 所属栏目:Linux 来源:网络整理
导读:以下面的简单C程序为例. main()对4个整数传递的sum进行函数调用. sum()使用4个本地人. void sum(int a,int b,int c,int d);void main(void){ sum(11,12,13,14);}void sum(int a,int d){ int x; int y; int z; int z2; x = a; y = b; z = c; z2 = d;} 在我的U
以下面的简单C程序为例. main()对4个整数传递的sum进行函数调用. sum()使用4个本地人.

void sum(int a,int b,int c,int d);

void main(void)
{
    sum(11,12,13,14);
}

void sum(int a,int d)
{
    int x;
    int y;
    int z;
    int z2;

    x = a;
    y = b;
    z = c;
    z2 = d;
}

在我的Ubuntu服务器12.04.04 LTS上我使用编译该程序

arm-linux-gnueabi-gcc -S -mthumb func.c

sum:
@ args = 0,pretend = 0,frame = 32
@ frame_needed = 1,uses_anonymous_args = 0
@ link register save eliminated.
push    {r7}
sub sp,sp,#36    <===   why is this 36 and not 32 bytes?
add r7,#0

str r0,[r7,#12]
str r1,#8]
str r2,#4]
str r3,#0]   <- paramaters passed

ldr r3,#12]
str r3,#16]  <- locals
ldr r3,#8]
str r3,#20]
ldr r3,#24]
ldr r3,#0]
str r3,#28]

add r7,r7,#36
mov sp,r7
pop {r7}
bx  lr

似乎int是每个4个字节.该函数的4个locals和4个参数总共(4 * 4字节)(4 * 4bytes)= 32字节,这与汇编输出“frame = 32”匹配.

但是为什么堆栈指针会减少36而不是32?

解决方法

ARM的过程调用标准需要8字节对齐.

5.2.1.2 Stack constraints at a public interface

The stack must also conform to the following constraint at a public interface:

  • SP mod 8 = 0. The stack must be double-word aligned.

由于您正在生成程序集,因此默认情况下会导出所有内容,因此您将获得8字节对(我试过这个并且gcc在生成汇编时没有向静态函数添加.global< symbol>指令.我想这甚至说静态函数是公共接口或者gcc只是将每个函数对齐以具有8字节堆栈对齐. )

You can use -fomit-frame-pointer跳过按r7然后gcc应该将堆栈深度保持在32.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读