[LLVMdev] clang modifying clobbered register in case of inline assembly resulting in data corruption

Karthik Bhat karthikthecool at gmail.com
Wed Nov 28 23:49:40 PST 2012

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[] = {

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.


More information about the llvm-dev mailing list