C 函数名称更改?
Name of C function changes?
我有以下 C 代码:
void test_func()
{
__asm__ ("mov [=10=]x2,%rax");
__asm__ ("mov [=10=]x6000dd,%rdi");
__asm__ ("mov [=10=]x0,%rsi");
__asm__ ("syscall");
}
int main(int argc, char *argv[]) {
test_func();
return 0;
}
当我这样编译时 gcc mine.cxx -S -no-pie
我在汇编文件中得到以下内容:
.globl _Z9test_funcv
为什么我的函数名称会改变以及如何防止这种情况/预测它的新名称?
您正在将其编译为 C++ 代码,而不是 C。在 C++ 中 names are mangled to support things like function overloading and templates. Mangling rules depend on compiler but it usually begins with _Z
, especially in compilers following Itanium C++ ABI
见What is name mangling, and how does it work?
您需要将代码编译为C。编译器通常根据扩展名来确定语言,而在GCC中cxx
is one of the C++ extensions so just rename the file to *.c
. You can also force the compiler to compile code as C regardless of the extension. The option to do that depends on compiler, for example in MSVC use /TC
and in GCC use -x c
请注意,您应该 never put instructions in separate __asm__
statements 那样,因为允许编译器在它们之间放置任意指令
我有以下 C 代码:
void test_func()
{
__asm__ ("mov [=10=]x2,%rax");
__asm__ ("mov [=10=]x6000dd,%rdi");
__asm__ ("mov [=10=]x0,%rsi");
__asm__ ("syscall");
}
int main(int argc, char *argv[]) {
test_func();
return 0;
}
当我这样编译时 gcc mine.cxx -S -no-pie
我在汇编文件中得到以下内容:
.globl _Z9test_funcv
为什么我的函数名称会改变以及如何防止这种情况/预测它的新名称?
您正在将其编译为 C++ 代码,而不是 C。在 C++ 中 names are mangled to support things like function overloading and templates. Mangling rules depend on compiler but it usually begins with _Z
, especially in compilers following Itanium C++ ABI
见What is name mangling, and how does it work?
您需要将代码编译为C。编译器通常根据扩展名来确定语言,而在GCC中cxx
is one of the C++ extensions so just rename the file to *.c
. You can also force the compiler to compile code as C regardless of the extension. The option to do that depends on compiler, for example in MSVC use /TC
and in GCC use -x c
请注意,您应该 never put instructions in separate __asm__
statements 那样,因为允许编译器在它们之间放置任意指令