Code segfaults with clang -O1, works with gcc49 and clang -O2

Tijl Coosemans tijl at coosemans.org
Wed Oct 30 13:33:12 UTC 2013


On Wed, 30 Oct 2013 10:04:12 +0000 Bruce Cran wrote:
> The following code works (on FreeBSD 11-CURRENT) with gcc49 or clang -O2 
> or higher, but segfaults in do_cpuid() with clang -O1 or lower. Is there 
> a bug in the asm statement, or is clang doing something wrong?
> 
> #include <stdio.h>
> 
> static inline void do_cpuid(unsigned int *eax, unsigned int *ebx,
>                              unsigned int *ecx, unsigned int *edx)
> {
>          asm volatile("cpuid"
>                  : "=a" (*eax), "=b" (*ebx), "=r" (*ecx), "=d" (*edx)

Should be: "=c" (*ecx)

But you can also use the '+' modifier and remove the input operands:

: "+a" (*eax), "=b" (*ebx), "+c" (*ecx), "=d" (*edx)
:
: "memory"


>                  : "0" (*eax), "2" (*ecx)
>                  : "memory");
> }
> 
> int main(int argc, char **argv)
> {
>      unsigned int a = 0, b = 0, c = 0, d = 0;
>      do_cpuid(&a, &b, &c, &d);
>      printf("%d %d %d %d\n", a, b, c, d);
>      return 0;
> }
> 
> 



More information about the freebsd-questions mailing list