内联汇编 returns: 重定位 R_X86_64_32S 反对未定义的符号在创建共享对象时不能使用

Inline assembly returns: relocation R_X86_64_32S against undefined symbol can not be used when making a shared object

我正在研究 Xeno Kovah's example in slide 18 of Intermediate Assembly。他将 Visual Studios 与 Intel Assembly 内联使用。我已尝试按如下方式将其适应 GCC。我正在编译 -masm=intel -fPIC

#include <stdio.h>
int main(){
  unsigned int maxBasicCPUID;
  char vendorString[13];
  char * vendorStringPtr = (char *)vendorString; //Move the address into its own register
  //because it makes the asm syntax easier
  //First we will check whether we can even use CPUID
  //Such a check is actually more complicated than it seems (OMITED FROM SLIDES)

  __asm (
    "mov edi, vendorStringPtr;" //Get the base address of the char[] into a register
    "mov eax, 0;" //We're going to do CPUID with input of 0
    "cpuid;" //As stated, the instruction doesn't have any operands
    //Get back the results which are now stored in eax, ebx, ecx, edx
    //and will have values as specified by the manual
    "mov maxBasicCPUID, eax;"
    "mov [edi], ebx;" //We order which register we put into which address
    "mov [edi+4], edx;" //so that they all end up forming a human readable string
    "mov [edi+8], ecx;"
  );
  vendorString[12] = 0;
  printf("maxBasicCPUID = %#x, vendorString = %s\n", maxBasicCPUID, vendorString);
  return 0xb45eba11;
}

我不确定我做错了什么,但我收到以下错误

/usr/bin/ld: /tmp/ccSapgOG.o: relocation R_X86_64_32S against undefined symbol `vendorStringPtr' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status

在 gcc 中,您不能在汇编代码中直接通过名称引用局部变量。

此外,您需要告诉编译器您使用的所有寄存器(破坏)。

但是,从好的方面来说,您可以让编译器为您做更多的工作,正如您在下面的代码重写中看到的那样:

   uint32_t *str = (uint32_t *)vendorString;
   __asm("cpuid"
       : "=a"(maxBasicCPUID), "=b"(str[0]), "=d"(str[1]), "=c"(str[2])
       : "a"(0));

第一行参数告诉编译器将结果存储在哪里,第二行告诉编译器在执行内联汇编之前要加载哪些值。

有关所有详细信息,请参阅 https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html。 (感谢@MichaelPetch link。)