1
votes

When I create an asm file of my program in ARM the GCC sometimes does not generate the function epilogue in the function itself, but instead it creates some labels and then call the label for function epilogue. This is only happening in the assembly code and then later linker fix it. Is there anyway to force the assembler code to generate .s files that do not put function epilogue in the labels?

Example when I compile the c code to assembly with -S switch:

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}

Here is the disassembly of 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

As you can see the L7 is actually the one that holds the function epilogue for the FunctionX. Once I compile the code to binary, it will be in its correct form (function epilogue at the end of the function) as you can see at address 0x10e34. But is it possible to have the same form in the assembly code too (when I compile the source file to asm using -S switch) and force the gcc to not make the labels for function epilogue?

1
You appear to be mistaking the first basic block of the function for the whole function. - EOF
To prevent GCC emitting labels and branches, simply avoid using any conditional logic or loops in your functions :P - Notlikethat
The linker hasn't changed anything. The branch to the epilogue at .L7 is still there: 10d10: ea000040 b 10e18 < FunctionX +0x190>. Also note that the B instruction isn't a call instruction, it's a simple unconditional branch (jump). - Ross Ridge
If you compiled with some optimization, you might find that the b .L7 goes away which is probably what your main concern is (aside for the mis-understandings). - artless noise

1 Answers

3
votes

.L7 is a local label. It's a jump target for branches within your function. As @Ross points out, the branch to .L7 is just a b (jump) instruction, not a bl (call).

These labels aren't separate functions, they're just basic blocks.