Purpose of GCC Stack Padding?

Andrew Tomazos andrew@tomazos.com
Wed Dec 17 23:04:00 GMT 2008


> you are generating alignment compatible with the use of SSE parallel
> instructions. The stack, then, must be 16-byte aligned before entry and
> at exit, and also a buffer of 16 bytes or more must be 16-byte aligned.

Your assertion is that the padding is for 16-byte alignment, however
it appears to be more than that.  Let's take a specific example:

When the below function is compiled we see that the 19 bytes between
-53(%ebp) and (%esp == -72(%ebp)) is unused.

Why is 19 bytes of unused space stack space needed to pad something to
16-byte alignment?  One should never need more than 15 bytes, correct?

$ cat test.c

void function()
{
        char buffer[49];

        buffer[0]++; // to see where buffer is allocated
}

$ gcc -S test.c

$ cat test.s

        .file   "test.c"
        .text
.globl function
        .type   function, @function

function:

        // setup stack
        pushl   %ebp
        movl    %esp, %ebp
        subl    $72, %esp

        // setup stack check at -4(%ebp) .. (%ebp)
        movl    %gs:20, %eax
        movl    %eax, -4(%ebp)
        xorl    %eax, %eax

        // buffer is allocated at -53(%ebp) .. -4(%ebp)
        movzbl  -53(%ebp), %eax
        addl    $1, %eax
        movb    %al, -53(%ebp)

        // 19 BYTES BETWEEN -72(%esp) .. -53(%ebp) UNUSED ?

        // execute stack check
        movl    -4(%ebp), %eax
        xorl    %gs:20, %eax
        je      .L3
        call    __stack_chk_fail
.L3:

        // return
        leave
        ret

        .size   function, .-function
        .ident  "GCC: (GNU) 4.2.4 (Ubuntu 4.2.4-1ubuntu3)"
        .section        .note.GNU-stack,"",@progbits

$

Regards,
Andrew.

-- 
Andrew Tomazos <andrew@tomazos.com> <http://www.tomazos.com>



More information about the Gcc-help mailing list