user16255957
user16255957

Reputation:

Name of C function changes?

I had the following C code:

void test_func()
{
    __asm__ ("mov    $0x2,%rax");
    __asm__ ("mov    $0x6000dd,%rdi");
    __asm__ ("mov    $0x0,%rsi");
    __asm__ ("syscall");
}
int main(int argc, char *argv[]) {
    test_func();
    return 0;
}

when I compile it like this gcc mine.cxx -S -no-pie I get in assembly file the following:

.globl _Z9test_funcv

Why does the name of my function change and how to prevent this / predict its new name?

Upvotes: 0

Views: 169

Answers (1)

phuclv
phuclv

Reputation: 41874

You're compiling it as C++ code, not C. In 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

See What is name mangling, and how does it work?

You need to compile the code as C. The compiler usually determines the language based on the extension, and in 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

Note that you should never put instructions in separate __asm__ statements like that because the compiler is allowed to put arbitrary instructions between them

Upvotes: 4

Related Questions