dimanche 10 août 2008

Le padding de GCC qui énerve ..

Je suis assez enerver de voir que gcc n' en fait qu' a sa tête,a partir de la version 3.x il rajoute un padding a chaque fois, un exemple :

napoleon@s4t4n:~$cat exemple.c
int adi()
{
int a = 5;
}
int main()
{
return 0;
}

napoleon@s4t4n:~$ gcc -c -g exemple.c
napoleon@s4t4n:~$ gcc -o exemple exemple.o
napoleon@s4t4n:~$ gcc -S exemple.c
napoleon@s4t4n:~$ gdb ./exemple
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...
(gdb) disass adi
Dump of assembler code for function adi:
0x08048344 : push %ebp
0x08048345 : mov %esp,%ebp
0x08048347 : sub $0x10,%esp
0x0804834a : movl $0x5,-0x4(%ebp)
0x08048351 : leave
0x08048352 : ret
End of assembler dump.
(gdb)

Comme on le voit a l' adresse 0x08048347 il soustrait 0x10 (16 en decimal) octets a esp, alors qu' il ne devrait en soustraire que 0x4 (4 en decimal) puisque chez moi un int prend 4 octets en mémoire.

Donc après une petite recherche sur google j' ai trouver comment éviter cela, la solution est de compiler avec -mpreferred-stack-boundary=2



napoleon@s4t4n:~$ gcc -c -g -mpreferred-stack-boundary=2 exemple.c
napoleon@s4t4n:~$ gcc -o exemple exemple.o
napoleon@s4t4n:~$ gcc -S exemple.c
napoleon@s4t4n:~$ gdb ./exemple
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...
(gdb) disass adi
Dump of assembler code for function adi:
0x08048344 : push %ebp
0x08048345 : mov %esp,%ebp
0x08048347 : sub $0x4,%esp
0x0804834a : movl $0x5,-0x4(%ebp)
0x08048351 : leave
0x08048352 : ret
End of assembler dump.

Et la miracle il nous a bien reservé les 4 octets normalement ! Que demande le peuple ? hien ??

Aucun commentaire: