您的位置:首页 > 编程语言 > C语言/C++

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

2013-08-07 22:37 2421 查看
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: