在 .intel_syntax GNU C 内联汇编中引用内存操作数
Referencing memory operands in .intel_syntax GNU C inline assembly
我在使用内联汇编编译和 linking 源文件时遇到 link 错误。
测试文件如下:
via:$ cat test.cxx
extern int libtest();
int main(int argc, char* argv[])
{
return libtest();
}
$ cat lib.cxx
#include <stdint.h>
int libtest()
{
uint32_t rnds_00_15;
__asm__ __volatile__
(
".intel_syntax noprefix ;\n\t"
"mov DWORD PTR [rnds_00_15], 1 ;\n\t"
"cmp DWORD PTR [rnds_00_15], 1 ;\n\t"
"je done ;\n\t"
"done: ;\n\t"
".att_syntax noprefix ;\n\t"
:
: [rnds_00_15] "m" (rnds_00_15)
: "memory", "cc"
);
return 0;
}
编译和 linking 程序结果:
via:$ g++ -fPIC test.cxx lib.cxx -c
via:$ g++ -fPIC lib.o test.o -o test.exe
lib.o: In function `libtest()':
lib.cxx:(.text+0x1d): undefined reference to `rnds_00_15'
lib.cxx:(.text+0x27): undefined reference to `rnds_00_15'
collect2: error: ld returned 1 exit status
真正的程序更复杂。该例程超出了寄存器,因此标志 rnds_00_15
必须是内存操作数。 rnds_00_15
的使用仅限于 asm 块。它在 C 代码中声明,以确保内存分配在堆栈上,仅此而已。就 C 代码而言,我们不会读取或写入它。我们将其列为内存输入,以便 GCC 知道我们使用它并在扩展 ASM 中连接 "C variable name"。
为什么我会收到 link 错误,我该如何解决?
使用 gcc -masm=intel
编译并且不要尝试在 asm 模板字符串中切换模式。 AFAIK 在 clang14 之前没有等效项(注意:MacOS 默认将 clang 安装为 gcc
/ g++
。)
此外,您当然需要使用有效的 GNU C 内联汇编,使用操作数告诉编译器您要读取和写入哪些 C 对象。
- Can I use Intel syntax of x86 assembly with GCC? clang14 像 GCC
一样支持 -masm=intel
- How to set gcc to use intel syntax permanently? clang13 和更早版本没有。
I don't believe Intel syntax uses the percent sign. Perhaps I am missing something?
你在 %operand
替换到 Extended-Asm 模板(使用 single %
)之间混淆了,与汇编程序看到的最终汇编。
您需要 %%
才能在最终的 asm 中使用文字 %
。您不会在英特尔语法内联汇编中使用 "mov %%eax, 1"
,但您仍然使用 "mov %0, 1"
或 %[named_operand]
.
参见 https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html。在 Basic asm(无操作数)中,没有替换并且 % 在模板中不是特殊的,所以如果出于某种原因你会在 Basic asm 中写 mov , %eax
而在 Extended 中写 mov , %%eax
没有使用 mov , %[tmp]
或 mov , %0
.
这样的操作数
uint32_t rnds_00_15;
是一个带有自动存储的本地。当然没有那个名字的asm符号。
使用 %[rnds_00_15]
并使用 -masm=intel
进行编译(并在末尾删除 .att_syntax
;这会破坏编译器生成的 asm之后。)
您还需要删除 DWORD PTR
,因为操作数扩展已经包含了它,例如DWORD PTR [rsp - 4]
,DWORD PTR DWORD PTR [rsp - 4]
上出现 clang 错误。 (GAS 很好地接受了它,但是第二个优先,所以它毫无意义并且可能会产生误导。)
如果您希望编译器在堆栈上为您保留一些划痕 space,您将需要一个 "=m"
输出操作数。您不能修改仅输入操作数,即使它在 C 中未使用。也许编译器决定它可以与其他内容重叠,因为它没有被写入也没有被初始化(即 UB)。 (我不确定你的 "memory"
clobber 是否使它安全,但没有理由不在这里使用 early-clobber 输出操作数。)
并且您需要使用 %=
获得唯一编号以避免标签名称冲突。
工作示例(GCC 和 ICC,但不幸的是不是 clang)、on the Godbolt compiler explorer(根据下拉列表中的选项使用 -masm=intel
)。您可以使用“二进制模式”(11010 按钮)来证明它在编译为 asm 后确实在没有警告的情况下进行了汇编。
int libtest_intel()
{
uint32_t rnds_00_15;
// Intel syntax operand-size can only be overridden with operand modifiers
// because the expansion includes an explicit DWORD PTR
__asm__ __volatile__
( // ".intel_syntax noprefix \n\t"
"mov %[rnds_00_15], 1 \n\t"
"cmp %[rnds_00_15], 1 \n\t"
"je .Ldone%= \n\t"
".Ldone%=: \n\t"
: [rnds_00_15] "=&m" (rnds_00_15)
:
: // no clobbers
);
return 0;
}
编译(使用 gcc -O3 -masm=intel
)到这个 asm。当然也适用于 gcc -m32 -masm=intel
:
libtest_intel:
mov DWORD PTR [rsp-4], 1
cmp DWORD PTR [rsp-4], 1
je .Ldone8
.Ldone8:
xor eax, eax
ret
我无法让它与 clang 一起工作:当我明确地将它留在 .intel_syntax noprefix
时它窒息 .
操作数大小覆盖:
您必须使用 %b[tmp]
让编译器替换 BYTE PTR [rsp-4]
以仅访问双字输入操作数的低字节。如果您想做很多这样的事情,我建议您使用 AT&T 语法。
Using %[rnds_00_15]
results in Error: junk '(%ebp)' after expression.
那是因为你在没有告诉编译器的情况下切换到 Intel 语法。如果您希望它使用 Intel 寻址模式,使用 -masm=intel
进行编译,以便编译器可以使用正确的语法替换到模板中。
This is why I avoid that crappy GCC inline assembly at nearly all costs. Man I despise this crappy tool.
你只是用错了。它有点麻烦,但很有意义,而且如果您了解它的设计方式,大部分情况下都能很好地工作。
跟着我重复:编译器根本不解析 asm 字符串 ,除了对 %operand
[=128] 进行文本替换=].这就是为什么它没有注意到您的 .intel_syntax noprefex
并一直替换 AT&T 语法的原因。
不过,使用 AT&T 语法确实可以更好、更轻松地工作,例如用于覆盖内存操作数的操作数大小,或添加偏移量。 (例如 4 + %[mem]
适用于 AT&T 语法)。
备选方言:
如果你想编写不依赖于 -masm=intel
或不依赖的内联汇编,use Dialect alternatives(这会使你的代码超级难看;除了包装一两个之外不推荐用于任何其他用途说明):
还演示了操作数大小覆盖
#include <stdint.h>
int libtest_override_operand_size()
{
uint32_t rnds_00_15;
// Intel syntax operand-size can only be overriden with operand modifiers
// because the expansion includes an explicit DWORD PTR
__asm__ __volatile__
(
"{movl , %[rnds_00_15] | mov %[rnds_00_15], 1} \n\t"
"{cmpl , %[rnds_00_15] | cmp %k[rnds_00_15], 1} \n\t"
"{cmpw , %[rnds_00_15] | cmp %w[rnds_00_15], 1} \n\t"
"{cmpb , %[rnds_00_15] | cmp %b[rnds_00_15], 1} \n\t"
"je .Ldone%= \n\t"
".Ldone%=: \n\t"
: [rnds_00_15] "=&m" (rnds_00_15)
);
return 0;
}
使用 Intel 语法,gcc 将其编译为:
mov DWORD PTR [rsp-4], 1
cmp DWORD PTR [rsp-4], 1
cmp WORD PTR [rsp-4], 1
cmp BYTE PTR [rsp-4], 1
je .Ldone38
.Ldone38:
xor eax, eax
ret
使用 AT&T 语法,编译为:
movl , -4(%rsp)
cmpl , -4(%rsp)
cmpw , -4(%rsp)
cmpb , -4(%rsp)
je .Ldone38
.Ldone38:
xorl %eax, %eax
ret
我在使用内联汇编编译和 linking 源文件时遇到 link 错误。
测试文件如下:
via:$ cat test.cxx
extern int libtest();
int main(int argc, char* argv[])
{
return libtest();
}
$ cat lib.cxx
#include <stdint.h>
int libtest()
{
uint32_t rnds_00_15;
__asm__ __volatile__
(
".intel_syntax noprefix ;\n\t"
"mov DWORD PTR [rnds_00_15], 1 ;\n\t"
"cmp DWORD PTR [rnds_00_15], 1 ;\n\t"
"je done ;\n\t"
"done: ;\n\t"
".att_syntax noprefix ;\n\t"
:
: [rnds_00_15] "m" (rnds_00_15)
: "memory", "cc"
);
return 0;
}
编译和 linking 程序结果:
via:$ g++ -fPIC test.cxx lib.cxx -c
via:$ g++ -fPIC lib.o test.o -o test.exe
lib.o: In function `libtest()':
lib.cxx:(.text+0x1d): undefined reference to `rnds_00_15'
lib.cxx:(.text+0x27): undefined reference to `rnds_00_15'
collect2: error: ld returned 1 exit status
真正的程序更复杂。该例程超出了寄存器,因此标志 rnds_00_15
必须是内存操作数。 rnds_00_15
的使用仅限于 asm 块。它在 C 代码中声明,以确保内存分配在堆栈上,仅此而已。就 C 代码而言,我们不会读取或写入它。我们将其列为内存输入,以便 GCC 知道我们使用它并在扩展 ASM 中连接 "C variable name"。
为什么我会收到 link 错误,我该如何解决?
使用 gcc -masm=intel
编译并且不要尝试在 asm 模板字符串中切换模式。 AFAIK 在 clang14 之前没有等效项(注意:MacOS 默认将 clang 安装为 gcc
/ g++
。)
此外,您当然需要使用有效的 GNU C 内联汇编,使用操作数告诉编译器您要读取和写入哪些 C 对象。
- Can I use Intel syntax of x86 assembly with GCC? clang14 像 GCC 一样支持
- How to set gcc to use intel syntax permanently? clang13 和更早版本没有。
-masm=intel
I don't believe Intel syntax uses the percent sign. Perhaps I am missing something?
你在 %operand
替换到 Extended-Asm 模板(使用 single %
)之间混淆了,与汇编程序看到的最终汇编。
您需要 %%
才能在最终的 asm 中使用文字 %
。您不会在英特尔语法内联汇编中使用 "mov %%eax, 1"
,但您仍然使用 "mov %0, 1"
或 %[named_operand]
.
参见 https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html。在 Basic asm(无操作数)中,没有替换并且 % 在模板中不是特殊的,所以如果出于某种原因你会在 Basic asm 中写 mov , %eax
而在 Extended 中写 mov , %%eax
没有使用 mov , %[tmp]
或 mov , %0
.
uint32_t rnds_00_15;
是一个带有自动存储的本地。当然没有那个名字的asm符号。
使用 %[rnds_00_15]
并使用 -masm=intel
进行编译(并在末尾删除 .att_syntax
;这会破坏编译器生成的 asm之后。)
您还需要删除 DWORD PTR
,因为操作数扩展已经包含了它,例如DWORD PTR [rsp - 4]
,DWORD PTR DWORD PTR [rsp - 4]
上出现 clang 错误。 (GAS 很好地接受了它,但是第二个优先,所以它毫无意义并且可能会产生误导。)
如果您希望编译器在堆栈上为您保留一些划痕 space,您将需要一个 "=m"
输出操作数。您不能修改仅输入操作数,即使它在 C 中未使用。也许编译器决定它可以与其他内容重叠,因为它没有被写入也没有被初始化(即 UB)。 (我不确定你的 "memory"
clobber 是否使它安全,但没有理由不在这里使用 early-clobber 输出操作数。)
并且您需要使用 %=
获得唯一编号以避免标签名称冲突。
工作示例(GCC 和 ICC,但不幸的是不是 clang)、on the Godbolt compiler explorer(根据下拉列表中的选项使用 -masm=intel
)。您可以使用“二进制模式”(11010 按钮)来证明它在编译为 asm 后确实在没有警告的情况下进行了汇编。
int libtest_intel()
{
uint32_t rnds_00_15;
// Intel syntax operand-size can only be overridden with operand modifiers
// because the expansion includes an explicit DWORD PTR
__asm__ __volatile__
( // ".intel_syntax noprefix \n\t"
"mov %[rnds_00_15], 1 \n\t"
"cmp %[rnds_00_15], 1 \n\t"
"je .Ldone%= \n\t"
".Ldone%=: \n\t"
: [rnds_00_15] "=&m" (rnds_00_15)
:
: // no clobbers
);
return 0;
}
编译(使用 gcc -O3 -masm=intel
)到这个 asm。当然也适用于 gcc -m32 -masm=intel
:
libtest_intel:
mov DWORD PTR [rsp-4], 1
cmp DWORD PTR [rsp-4], 1
je .Ldone8
.Ldone8:
xor eax, eax
ret
我无法让它与 clang 一起工作:当我明确地将它留在 .intel_syntax noprefix
时它窒息 .
操作数大小覆盖:
您必须使用 %b[tmp]
让编译器替换 BYTE PTR [rsp-4]
以仅访问双字输入操作数的低字节。如果您想做很多这样的事情,我建议您使用 AT&T 语法。
Using
%[rnds_00_15]
results inError: junk '(%ebp)' after expression.
那是因为你在没有告诉编译器的情况下切换到 Intel 语法。如果您希望它使用 Intel 寻址模式,使用 -masm=intel
进行编译,以便编译器可以使用正确的语法替换到模板中。
This is why I avoid that crappy GCC inline assembly at nearly all costs. Man I despise this crappy tool.
你只是用错了。它有点麻烦,但很有意义,而且如果您了解它的设计方式,大部分情况下都能很好地工作。
跟着我重复:编译器根本不解析 asm 字符串 ,除了对 %operand
[=128] 进行文本替换=].这就是为什么它没有注意到您的 .intel_syntax noprefex
并一直替换 AT&T 语法的原因。
不过,使用 AT&T 语法确实可以更好、更轻松地工作,例如用于覆盖内存操作数的操作数大小,或添加偏移量。 (例如 4 + %[mem]
适用于 AT&T 语法)。
备选方言:
如果你想编写不依赖于 -masm=intel
或不依赖的内联汇编,use Dialect alternatives(这会使你的代码超级难看;除了包装一两个之外不推荐用于任何其他用途说明):
还演示了操作数大小覆盖
#include <stdint.h>
int libtest_override_operand_size()
{
uint32_t rnds_00_15;
// Intel syntax operand-size can only be overriden with operand modifiers
// because the expansion includes an explicit DWORD PTR
__asm__ __volatile__
(
"{movl , %[rnds_00_15] | mov %[rnds_00_15], 1} \n\t"
"{cmpl , %[rnds_00_15] | cmp %k[rnds_00_15], 1} \n\t"
"{cmpw , %[rnds_00_15] | cmp %w[rnds_00_15], 1} \n\t"
"{cmpb , %[rnds_00_15] | cmp %b[rnds_00_15], 1} \n\t"
"je .Ldone%= \n\t"
".Ldone%=: \n\t"
: [rnds_00_15] "=&m" (rnds_00_15)
);
return 0;
}
使用 Intel 语法,gcc 将其编译为:
mov DWORD PTR [rsp-4], 1
cmp DWORD PTR [rsp-4], 1
cmp WORD PTR [rsp-4], 1
cmp BYTE PTR [rsp-4], 1
je .Ldone38
.Ldone38:
xor eax, eax
ret
使用 AT&T 语法,编译为:
movl , -4(%rsp)
cmpl , -4(%rsp)
cmpw , -4(%rsp)
cmpb , -4(%rsp)
je .Ldone38
.Ldone38:
xorl %eax, %eax
ret