Karthik Bhat
2012-Nov-29 07:49 UTC
[LLVMdev] clang modifying clobbered register in case of inline assembly resulting in data corruption
Hi All,
I'm looking into this simple inline assembly code.
Were we copy contents on data to eax,ebx,ecx and edx and later copy
them back from the registers to data.
Test Case -
#include <stdio.h>
int data[] = {
0x14131211,
0x24232221,
0x34333231,
0x44434241,
};
int
main (int argc, char **argv)
{
asm ("mov 0(%0), %%eax\n\t"
"mov 4(%0), %%ebx\n\t"
"mov 8(%0), %%ecx\n\t"
"mov 12(%0), %%edx\n\t"
: /* no output operands */
: "r" (data)
: "eax", "ebx", "ecx", "edx");
asm ("nop");
asm ("mov %%eax, 0(%0)\n\t"
"mov %%ebx, 4(%0)\n\t"
"mov %%ecx, 8(%0)\n\t"
"mov %%edx, 12(%0)\n\t"
: /* no output operands */
: "r" (data)
: "eax", "ebx", "ecx", "edx");
printf("data[0] = %x \n",data[0]);
printf("data[1] = %x \n",data[1]);
printf("data[2] = %x \n",data[2]);
printf("data[3] = %x \n",data[3]);
return 0;
}
In this case the value of data[0] has got corrupted as clang is not
honoring clobbered register and stores a different value in $eax.
Similarly when compiled with -fPIE option, GCC emits an error as -
i386-pseudo.c: In function ‘main’:
i386-pseudo.c:13:3: error: PIC register clobbered by ‘ebx’ in ‘asm’
i386-pseudo.c:22:3: error: PIC register clobbered by ‘ebx’ in ‘asm’
Clang compiles successfully without reporting this error.
Could someone guide me which part of code to look into to fix this issue.
Thanks
Karthik
Eli Friedman
2012-Nov-29 09:07 UTC
[LLVMdev] clang modifying clobbered register in case of inline assembly resulting in data corruption
On Wed, Nov 28, 2012 at 11:49 PM, Karthik Bhat <karthikthecool at gmail.com> wrote:> In this case the value of data[0] has got corrupted as clang is not > honoring clobbered register and stores a different value in $eax.Not a bug; that isn't how clobbers work. We don't make any guarantees about the values of registers between inline asm statements. If you need an operand in a particular register, see http://gcc.gnu.org/onlinedocs/gcc/Local-Reg-Vars.html etc. -Eli
Seemingly Similar Threads
- [LLVMdev] Value of structure passed byval to a recurse function not initialized when accessed through GDB
- [LLVMdev] Value of structure passed byval to a recurse function not initialized when accessed through GDB
- [LLVMdev] Value of structure passed byval to a recurse function not initialized when accessed through GDB
- [LLVMdev] Value of structure passed byval to a recurse function not initialized when accessed through GDB
- [LLVMdev] Value of structure passed byval to a recurse function not initialized when accessed through GDB