gcc优化对static函数调用的处理

本文通过实验揭示了C代码在不同优化级别下,编译器如何调整myfunc函数的调用方式,从嵌入到独立函数。展示了汇编级别的细节变化和优化策略。

最近追查一个崩溃问题,反汇编根据调用栈一层层查找,到了某一函数后竞找不到对应代码行,后进入其调用的某一个函数才将其调用顺序串起来。由此做了一个小实验,从汇编层面看看编译器如何处理。

C代码:

#include <stdio.h>

static void myfunc(void)
{
	printf("myfunc in\n");
	printf("myfunc out\n");
}

int main(int argc, char *argv[])
{
	printf("main in\n");
	myfunc();
	printf("main out\n");
	return 0;
}

 

用 arm-none-linux-gnueabi-gcc main.c 编译,再用arm-none-linux-gnueabi-objdump -d a.out 反汇编输出,从下面汇编片段,可以看出main函数对myfunc的调用: bl    842c <myfunc>

0000842c <myfunc>:
    842c:	e92d4800 	push	{fp, lr}
    8430:	e28db004 	add	fp, sp, #4
    8434:	e59f000c 	ldr	r0, [pc, #12]	; 8448 <myfunc+0x1c>
    8438:	ebffffcd 	bl	8374 <_init+0x44>
    843c:	e59f0008 	ldr	r0, [pc, #8]	; 844c <myfunc+0x20>
    8440:	ebffffcb 	bl	8374 <_init+0x44>
    8444:	e8bd8800 	pop	{fp, pc}
    8448:	
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值