gcc -m64 -fPIC 编译问题

做FreeBSD64位移植时,cpuid指令与gcc -fPIC开关冲突:由于-fPIC需要独占ebx,而cpuid要使用ebx,故修改汇编语句,不明确使用"=b",使gcc通过。

原:
asm("cpuid": "=a" (out_eax), "=b" (out_ebx), "=c" (out_ecx), "=d" (out_edx) : "a" (function));

修改:
asm volatile("pushl %%ebx \n\t" /* save %ebx */

"cpuid \n\t"

"movl %%ebx, %1 \n\t" /* save what cpuid just put in %ebx */

"popl %%ebx \n\t" /* restore the old %ebx */

: "=a"(out_eax), "=r"(out_ebx), "=c"(out_ecx), "=d"(out_edx)

: "a"(function)

: "cc");

后记:
#ifdef X86AMD64

uint64_t local_rax, local_rbx, local_rcx, local_rdx;

asm volatile("push %%rbx \n\t"

"cpuid \n\t"

"mov %%rbx, %1 \n\t"

"pop %%rbx \n\t"

: "=a"(local_rax), "=r"(local_rbx), "=c"(local_rcx), "=d"(local_rdx)

: "a"(function)

: "cc");

out_eax = local_rax;

out_ebx = local_rbx;

out_ecx = local_rcx;

out_edx = local_rdx;

#else

asm volatile("pushl %%ebx \n\t"

"cpuid \n\t"

"movl %%ebx, %1 \n\t"

"popl %%ebx \n\t"

: "=a"(out_eax), "=r"(out_ebx), "=c"(out_ecx), "=d"(out_edx)

: "a"(function)

: "cc");

#endif

参考 http://sam.zoy.org/blog/2007-04-13-shlib-with-non-pic-code-have-inline-assembly-and-pic-mix-well

你可能感兴趣的:(gcc -m64 -fPIC 编译问题)