在 .c 文件中使用内联 PPC 汇编代码时出现“错误:不支持针对<寄存器>的重定位”错误。

5
我有以下内联汇编代码。但是当我尝试编译它时,它会抛出下面代码片段后面提到的错误。
unsigned int func(void)
{
  __asm__ ("mfspr r3, svr;");
}

以下是错误信息。
{standard input}: Assembler messages:
{standard input}:3349: Error: unsupported relocation against r3
{standard input}:3349: Error: unsupported relocation against svr
{standard input}:3375: Error: unsupported relocation against r3
{standard input}:3375: Error: unsupported relocation against svr
{standard input}:3510: Error: unsupported relocation against r3
{standard input}:3510: Error: unsupported relocation against svr
{standard input}:3517: Error: unsupported relocation against r3
{standard input}:3517: Error: unsupported relocation against svr

有人能帮我修复这些吗?


4个回答

2

显然,gas没有对这些寄存器提供内置支持。为了使用它们,您应该自己定义它们或者直接使用它们的索引,例如:

mfspr 3, <some_index_here>

或者您可以包含:ppc_asm.tmpl

如果您的核心是e500,那么svr索引将为1023。


@vimalprathap 是的。请查看e500mc参考手册: http://cache.freescale.com/files/32bit/doc/ref_manual/E500MCRM.pdf - dragosht

2

您应该明确指定输入和输出。如果未明确指定,您的ASM块可能会被优化掉!

unsigned int func(void)
{
    unsigned x;
    __asm__("mfspr %0, svr" : "=b"(x));
    return x;
}

编译器足够聪明,可以确定寄存器应该是r3。(这是编译器的主要工作之一:寄存器分配以最小化额外移动。)
如果您省略输出规范,然后启用优化编译,您可能会发现您的函数为空,没有找到任何mfspr操作码。

@vimalprathap:可能svr未定义。请使用数字版本。 - Dietrich Epp
@DietrichEpp 我的构建中也出现了类似的错误,尽管是针对系统调用swapcontext,请您也好心看一下。http://stackoverflow.com/questions/33785554/error-in-assembler-unsupported-relocation-against-nr-swapcontext - RootPhoenix

1
如果您在汇编时使用-mregnames选项(-Wa,-mregnames),那么至少一些错误将会消失。gas 2.19支持PPC的以下符号寄存器名称(来自binutils-2.19/gas/config/tc-ppc.c):
/* List of registers that are pre-defined:

   Each general register has predefined names of the form:
   1. r<reg_num> which has the value <reg_num>.
   2. r.<reg_num> which has the value <reg_num>.

   Each floating point register has predefined names of the form:
   1. f<reg_num> which has the value <reg_num>.
   2. f.<reg_num> which has the value <reg_num>.

   Each vector unit register has predefined names of the form:
   1. v<reg_num> which has the value <reg_num>.
   2. v.<reg_num> which has the value <reg_num>.

   Each condition register has predefined names of the form:
   1. cr<reg_num> which has the value <reg_num>.
   2. cr.<reg_num> which has the value <reg_num>.

   There are individual registers as well:
   sp or r.sp     has the value 1
   rtoc or r.toc  has the value 2
   fpscr          has the value 0
   xer            has the value 1
   lr             has the value 8
   ctr            has the value 9
   pmr            has the value 0
   dar            has the value 19
   dsisr          has the value 18
   dec            has the value 22
   sdr1           has the value 25
   srr0           has the value 26
   srr1           has the value 27

   The table is sorted. Suitable for searching by a binary search.  */

0

这是一个有用的错误信息,当gas试图表达它不知道"r3"和"svr"是寄存器名称时,就会发出这个错误信息。对于寄存器操作数,gas期望数字而不是寄存器名称。如果您尝试使用寄存器名称,您将收到类似的错误消息。

__asm__ ("mfspr foo, fum;");

换句话说,gas将寄存器名称解释为任意符号。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接