linux 汇编 `.eabi_attribute',编译C/C++语言程序源码生成的汇编语言程序源码中的.fnstart,.fnend等伪操作...

本文详细探讨了C语言源码在使用arm-linux-androideabi-gcc编译时,两种不同编译选项(-fexceptions与不带该选项)下汇编源码的区别,重点在于.fnstart和.fnend等伪操作的出现与否。理解这些差异有助于开发者针对目标平台优化代码和处理异常处理。

C语言程序源码文件名称arm-c.c,程序源码:

int sum(int a, int b)

{

return a + b;

}

int sub(int a, int b)

{

return a - b;

}

使用的编译工具:

LiuWeitekiMacBook-Pro:sample03 LiuWei$ arm-linux-androideabi-gcc --version

arm-linux-androideabi-gcc (GCC) 4.8

1、编译生成不包含.fnstart,.fnend等伪操作的汇编语言代码:

arm-linux-androideabi-gcc -S -mcpu=cortex-a9 -mfloat-abi=softfp -mfpu=neon  -o arm-c-no-unwind.s arm-c.c

查看编译生成的汇编源码:

.cpu cortex-a9

.eabi_attribute 27, 3

.fpu neon

.eabi_attribute 20, 1

.eabi_attribute 21, 1

.eabi_attribute 23, 3

.eabi_attribute 24, 1

.eabi_attribute 25, 1

.eabi_attribute 26, 2

.eabi_attribute 30, 6

.eabi_attribute 34, 1

.eabi_attribute 18, 4

.file   "arm-c.c"

.text

.align  2

.global sum

.type   sum, %function

sum:

@ args = 0, pretend = 0, frame = 8

@ frame_needed = 1, uses_anonymous_args = 0

@ link register save eliminated.

str     fp, [sp, #-4]!

add     fp, sp, #0

sub     sp, sp, #12

str     r0, [fp, #-8]

str     r1, [fp, #-12]

ldr     r2, [fp, #-8]

ldr     r3, [fp, #-12]

add     r3, r2, r3

mov     r0, r3

sub     sp, fp, #0

@ sp needed

ldr     fp, [sp], #4

bx      lr

.size   sum, .-sum

.align  2

.global sub

.type   sub, %function

sub:

@ args = 0, pretend = 0, frame = 8

@ frame_needed = 1, uses_anonymous_args = 0

@ link register save eliminated.

str     fp, [sp, #-4]!

add     fp, sp, #0

sub     sp, sp, #12

str     r0, [fp, #-8]

str     r1, [fp, #-12]

ldr     r2, [fp, #-8]

ldr     r3, [fp, #-12]

rsb     r3, r3, r2

mov     r0, r3

sub     sp, fp, #0

@ sp needed

ldr     fp, [sp], #4

bx      lr

.size   sub, .-sub

.ident  "GCC: (GNU) 4.8"

.section        .note.GNU-stack,"",%progbits

查看生成的汇编语言源码,没有.fnstart,.fnend等伪指令;

2、编译生成包含.fnstart,.fnend等伪操作的汇编语言代码:

arm-linux-androideabi-gcc -S -mcpu=cortex-a9 -mfloat-abi=softfp -mfpu=neon -fexceptions -o arm-c-unwind.s arm-c.c

查看编译生成的汇编语言源码:

.cpu cortex-a9

.eabi_attribute 27, 3

.fpu neon

.eabi_attribute 20, 1

.eabi_attribute 21, 1

.eabi_attribute 23, 3

.eabi_attribute 24, 1

.eabi_attribute 25, 1

.eabi_attribute 26, 2

.eabi_attribute 30, 6

.eabi_attribute 34, 1

.eabi_attribute 18, 4

.file   "arm-c.c"

.text

.align  2

.global sum

.type   sum, %function

sum:

.fnstart

.LFB0:

@ args = 0, pretend = 0, frame = 8

@ frame_needed = 1, uses_anonymous_args = 0

@ link register save eliminated.

str     fp, [sp, #-4]!

add     fp, sp, #0

sub     sp, sp, #12

str     r0, [fp, #-8]

str     r1, [fp, #-12]

ldr     r2, [fp, #-8]

ldr     r3, [fp, #-12]

add     r3, r2, r3

mov     r0, r3

sub     sp, fp, #0

@ sp needed

ldr     fp, [sp], #4

bx      lr

.cantunwind

.fnend

.size   sum, .-sum

.align  2

.global sub

.type   sub, %function

sub:

.fnstart

.LFB1:

@ args = 0, pretend = 0, frame = 8

@ frame_needed = 1, uses_anonymous_args = 0

@ link register save eliminated.

str     fp, [sp, #-4]!

add     fp, sp, #0

sub     sp, sp, #12

str     r0, [fp, #-8]

str     r1, [fp, #-12]

ldr     r2, [fp, #-8]

ldr     r3, [fp, #-12]

rsb     r3, r3, r2

mov     r0, r3

sub     sp, fp, #0

@ sp needed

ldr     fp, [sp], #4

bx      lr

.cantunwind

.fnend

.size   sub, .-sub

.ident  "GCC: (GNU) 4.8"

.section        .note.GNU-stack,"",%progbits

查看代码,发现多了.fnstart,.fnend等伪操作;

产生的汇编语言程序源码不同,原因就是在编译时增加了一个flag:-fexceptions

查看gcc的文档,关于-fexceptions的解释:

-fexceptions

Enable exception handling. Generates extra code needed to propagate excep-tions. For some targets, this implies GCC will generate frame unwind informa-tion for all functions, which can produce significant data size overhead, althoughit does not affect execution. If you do not specify this option, GCC will enableit by default for languages like C++ which normally require exception handling,and disable it for languages like C that do not normally require it. However,you may need to enable this option when compiling C code that needs to inter-operate properly with exception handlers written in C++. You may also wishto disable this option if you are compiling older C++ programs that don’t useexception handling.

编译C++语言程序源码时,这个flag默认打开,编译C语言程序源码时,这个flag默认关闭,因此在

http://sourceware.org/binutils/docs/as/ARM-Unwinding-Tutorial.html

中看到的例子,默认是有.fnstart,.fnend等伪操作的,不是因为编译器不同,而是因为编程语言不同;

同时,还需要注意,在gcc文档中,还有一个flag,与-fexceptions相似:

-funwind-tables Similar to ‘-fexceptions’, except that it will just generate any needed staticdata, but will not affect the generated code in any other way. You will normallynot enable this option; instead, a language processor that needs this handlingwould enable it on your behalf.

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值