Undefined reference to C++ variables in ASM code [

2019-06-05 03:35发布

问题:

This question already has an answer here:

  • How to use a global variable in gcc inline assembly 3 answers

I'm trying to compile following asm code inside a C++ file, but it like GCC can't find the variables used in the asm part.

DWORD d_eaxSave, d_ebxSave, d_ecxSave, d_edxSave, d_esiSave, d_ediSave, d_espSave, d_ebpSave;

#define ASM_UNPREFIXED_INTEL_START ".intel_syntax noprefix\n\t"
#define SAVE_REGISTERS() __asm__ (ASM_UNPREFIXED_INTEL_START    \
                              "mov d_eaxSave, eax\n\t"      \
                              "mov d_ebxSave, ebx\n\t"      \
                              "mov d_ecxSave, ecx\n\t"      \
                              "mov d_edxSave, edx\n\t"      \
                              "mov d_esiSave, esi\n\t"      \
                              "mov d_ediSave, edi\n\t"      \
                              "mov d_espSave, esp\n\t"      \
                              "mov d_ebpSave, ebp");

But when I try to compile, GCC gives me the following errors :

undefined reference to `d_eaxSave'
undefined reference to `d_ebxSave'
undefined reference to `d_ecxSave'
undefined reference to `d_edxSave'
undefined reference to `d_esiSave'
undefined reference to `d_ediSave'
undefined reference to `d_espSave'
undefined reference to `d_ebpSave'

What can I do to get rid of those erros, please ?

回答1:

You want to generally use the AT&T syntax, because as far as I know, GCC cannot emit proper intel syntax in inline assembler without -masm=intel command line switch for everything - I'd presume then you cannot include any header files that would use AT&T inline assembly.

Thus:

DWORD d_eaxSave, d_ebxSave, d_ecxSave, d_edxSave, d_esiSave, d_ediSave, d_espSave, d_ebpSave;

#define SAVE_REGISTERS() __asm__ __volatile__(         \
     "movl %%eax, %0\n\t"      \
     "movl %%ebx, %1\n\t"      \
     "movl %%ecx, %2\n\t"      \
     "movl %%edx, %3\n\t"      \
     "movl %%esi, %4\n\t"      \
     "movl %%edi, %5\n\t"      \
     "movl %%esp, %6\n\t"      \
     "movl %%ebp, %7":         \
     "=m"(d_eaxSave), "=m"(d_ebxSave), "=m"(d_ecxSave), "=m"(d_edxSave),  \
     "=m"(d_esiSave), "=m"(d_ediSave), "=m"(d_espSave), "=m"(d_ebpSave)   \
)

The symbols might not exist for the assembler, so you need to use them via output parameters! Note also, that generally you would want to use __volatile__ to make sure that the assembler block is not optimized away.