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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值