如何强制gcc不在asm代码

时间:2016-08-05 11:51:45

标签: gcc assembly compilation arm

当我在ARM中创建我的程序的asm文件时,GCC有时不会在函数本身中生成函数结尾,而是创建一些标签然后调用函数结尾的标签。这只发生在汇编代码中,然后链接器修复它。反正是否强制汇编程序代码生成.s文件,这些文件没有在标签中放置函数尾声?

使用-S开关将c代码编译为程序集时的示例:

FunctionX

    stmfd   sp!, {fp, lr}
    add     fp, sp, #4
    sub     sp, sp, #32
    str     r0, [fp, #-32]
    ldr     r3, .L19
    sub     ip, fp, #28
    ldmia   r3, {r0, r1, r2, r3}
    stmia   ip, {r0, r1, r2, r3}
    mov     r3, #0
    str     r3, [fp, #-8]
    b       .L8
  .L8:
    ldr     r3, [fp, #-8]
    cmp     r3, #3
    bls     .L17
    mov     r0, r0  @ nop
  .L17:
    ldr     r3, [fp, #-8]
    mov     r3, r3, asl #2
    sub     r2, fp, #4
    add     r3, r2, r3
    ldr     r3, [r3, #-24]
    ldr     r1, .L19+4
    mov     r0, r3
    bl      fopen
    str     r0, [fp, #-12]
    ldr     r3, [fp, #-12]
    cmp     r3, #0
    bne     .L9
    ldr     r3, [fp, #-8]
    mov     r3, r3, asl #2
    sub     r2, fp, #4
    add     r3, r2, r3
    ldr     r3, [r3, #-24]
    mov     r1, r3
    ldr     r0, .L19+8
    bl      printf
    b       .L7
 .L7:
    sub     sp, fp, #4
    @ sp needed
    ldmfd   sp!, {fp, pc}

以下是FunctionX

的反汇编
00010c88 <FunctionX>:
   10c88:   e92d4800    push    {fp, lr}
   10c8c:   e28db004    add fp, sp, #4
   10c90:   e24dd028    sub sp, sp, #40 ; 0x28
   10c94:   e50b0028    str r0, [fp, #-40]  ; 0xffffffd8
   10c98:   e59f3198    ldr r3, [pc, #408]  ; 10e38 <FunctionX+0x1b0>
   10c9c:   e5933000    ldr r3, [r3]
   10ca0:   e50b3008    str r3, [fp, #-8]
   10ca4:   e59f3190    ldr r3, [pc, #400]  ; 10e3c <FunctionX>
   10ca8:   e24bc018    sub ip, fp, #24
   10cac:   e893000f    ldm r3, {r0, r1, r2, r3}
   10cb0:   e88c000f    stm ip, {r0, r1, r2, r3}
   10cb4:   e3a03000    mov r3, #0
   10cb8:   e50b3020    str r3, [fp, #-32]  ; 0xffffffe0
   10cbc:   ea000051    b   10e08 <FunctionX+0x180>
   10cc0:   e51b3020    ldr r3, [fp, #-32]  ; 0xffffffe0
   10cc4:   e1a03103    lsl r3, r3, #2
   10cc8:   e24b2004    sub r2, fp, #4
   10ccc:   e0823003    add r3, r2, r3
   10cd0:   e5133014    ldr r3, [r3, #-20]  ; 0xffffffec
   10cd4:   e59f1164    ldr r1, [pc, #356]  ; 10e40 < FunctionX +0x1b8>
   10cd8:   e1a00003    mov r0, r3
   10cdc:   ebffff01    bl  108e8 <fopen@plt>
   10ce0:   e50b001c    str r0, [fp, #-28]  ; 0xffffffe4
   10ce4:   e51b301c    ldr r3, [fp, #-28]  ; 0xffffffe4
   10ce8:   e3530000    cmp r3, #0
   10cec:   1a000008    bne 10d14 < FunctionX +0x8c>
   10cf0:   e51b3020    ldr r3, [fp, #-32]  ; 0xffffffe0
   10cf4:   e1a03103    lsl r3, r3, #2
   10cf8:   e24b2004    sub r2, fp, #4
   10cfc:   e0823003    add r3, r2, r3
   10d00:   e5133014    ldr r3, [r3, #-20]  ; 0xffffffec
   10d04:   e1a01003    mov r1, r3
   10d08:   e59f0134    ldr r0, [pc, #308]  ; 10e44 < FunctionX +0x1bc>
   10d0c:   ebfffef2    bl  108dc <printf@plt>
   10d10:   ea000040    b   10e18 < FunctionX +0x190>
   10d14:   e51b001c    ldr r0, [fp, #-28]  ; 0xffffffe4
   10d18:   ebffff1c    bl  10990 <fileno@plt>
   10d1c:   e1a03000    mov r3, r0
   10d20:   e3a01002    mov r1, #2
   10d24:   e1a00003    mov r0, r3
   10d28:   ebffff0c    bl  10960 <flock@plt>
   10d2c:   e51b3020    ldr r3, [fp, #-32]  ; 0xffffffe0
   10d30:   e3530003    cmp r3, #3
   10d34:   979ff103    ldrls   pc, [pc, r3, lsl #2]
   10d38:   ea000027    b   10ddc <FunctionX+0x154>
   10d3c:   00010d4c    .word   0x00010d4c
   10d40:   00010d70    .word   0x00010d70
   10d44:   00010d94    .word   0x00010d94
   10d48:   00010db8    .word   0x00010db8
   10d4c:   e51b3028    ldr r3, [fp, #-40]  ; 0xffffffd8
   10d50:   e5930010    ldr r0, [r3, #16]
   10d54:   e51b3028    ldr r3, [fp, #-40]  ; 0xffffffd8
   10d58:   e5933000    ldr r3, [r3]
   10d5c:   e1a02003    mov r2, r3
   10d60:   e51b301c    ldr r3, [fp, #-28]  ; 0xffffffe4
   10d64:   e3a01001    mov r1, #1
   10d68:   ebfffeea    bl  10918 <fwrite@plt>
   10d6c:   ea00001a    b   10ddc < FunctionX+0x154>
   10d70:   e51b3028    ldr r3, [fp, #-40]  ; 0xffffffd8
   10d74:   e5930014    ldr r0, [r3, #20]
   10d78:   e51b3028    ldr r3, [fp, #-40]  ; 0xffffffd8
   10d7c:   e5933004    ldr r3, [r3, #4]
   10d80:   e1a02003    mov r2, r3
   10d84:   e51b301c    ldr r3, [fp, #-28]  ; 0xffffffe4
   10d88:   e3a01001    mov r1, #1
   10d8c:   ebfffee1    bl  10918 <fwrite@plt>
   10d90:   ea000011    b   10ddc < FunctionX+0x154>
   10d94:   e51b3028    ldr r3, [fp, #-40]  ; 0xffffffd8
   10d98:   e5930018    ldr r0, [r3, #24]
   10d9c:   e51b3028    ldr r3, [fp, #-40]  ; 0xffffffd8
   10da0:   e5933008    ldr r3, [r3, #8]
   10da4:   e1a02003    mov r2, r3
   10da8:   e51b301c    ldr r3, [fp, #-28]  ; 0xffffffe4
   10dac:   e3a01002    mov r1, #2
   10db0:   ebfffed8    bl  10918 <fwrite@plt>
   10db4:   ea000008    b   10ddc < FunctionX+0x154>
   10db8:   e51b3028    ldr r3, [fp, #-40]  ; 0xffffffd8
   10dbc:   e593001c    ldr r0, [r3, #28]
   10dc0:   e51b3028    ldr r3, [fp, #-40]  ; 0xffffffd8
   10dc4:   e593300c    ldr r3, [r3, #12]
   10dc8:   e1a02003    mov r2, r3
   10dcc:   e51b301c    ldr r3, [fp, #-28]  ; 0xffffffe4
   10dd0:   e3a01002    mov r1, #2
   10dd4:   ebfffecf    bl  10918 <fwrite@plt>
   10dd8:   e1a00000    nop         ; (mov r0, r0)
   10ddc:   e51b001c    ldr r0, [fp, #-28]  ; 0xffffffe4
   10de0:   ebfffeea    bl  10990 <fileno@plt>
   10de4:   e1a03000    mov r3, r0
   10de8:   e3a01008    mov r1, #8
   10dec:   e1a00003    mov r0, r3
   10df0:   ebfffeda    bl  10960 <flock@plt>
   10df4:   e51b001c    ldr r0, [fp, #-28]  ; 0xffffffe4
   10df8:   ebfffee7    bl  1099c <fclose@plt>
   10dfc:   e51b3020    ldr r3, [fp, #-32]  ; 0xffffffe0
   10e00:   e2833001    add r3, r3, #1
   10e04:   e50b3020    str r3, [fp, #-32]  ; 0xffffffe0
   10e08:   e51b3020    ldr r3, [fp, #-32]  ; 0xffffffe0
   10e0c:   e3530003    cmp r3, #3
   10e10:   9affffaa    bls 10cc0 <FunctionX+0x38>
   10e14:   e1a00000    nop         ; (mov r0, r0)
   10e18:   e59f3018    ldr r3, [pc, #24]   ; 10e38 <FunctionX+0x1b0>
   10e1c:   e51b2008    ldr r2, [fp, #-8]
   10e20:   e5933000    ldr r3, [r3]
   10e24:   e1520003    cmp r2, r3
   10e28:   0a000000    beq 10e30 <FunctionX+0x1a8>
   10e2c:   ebfffeb6    bl  1090c <__stack_chk_fail@plt>
   10e30:   e24bd004    sub sp, fp, #4
   10e34:   e8bd8800    pop {fp, pc}
   10e38:   00022008    .word   0x00022008
   10e3c:   000110f4    .word   0x000110f4
   10e40:   00011098    .word   0x00011098
   10e44:   0001109c    .word   0x0001109c

正如您所看到的,L7实际上是持有FunctionX函数结尾的那个。一旦我将代码编译为二进制文件,它将以正确的形式(函数末尾的函数结尾),如地址0x10e34所示。但是也可以在汇编代码中使用相同的表单(当我使用-S开关将源文件编译为asm时)并强制gcc不为函数结尾制作标签?

1 个答案:

答案 0 :(得分:2)

.L7是本地标签。它是函数中的分支跳转目标。正如@Ross指出的那样,.L7的分支只是b(跳转)指令,而不是bl(调用)。

这些标签不是单独的功能,它们只是基本功能块。