What is the reason function names are prefixed with an underscore by the compiler?

后端 未结 3 1896
广开言路
广开言路 2020-12-01 13:49

When I see the assembly code of a C app, like this:

emacs hello.c
clang -S -O hello.c -o hello.s
cat hello.s

Function names are prefixed wi

3条回答
  •  旧时难觅i
    2020-12-01 14:16

    At first glance the operating system is a Unix/Unix-like running on a PC. According to me, there is nothing much surprising to find _printf in the generated assembly language. C printf is a function which performs an I/O. So it is the responsibility of the kernel + driver to perform the requested I/O.

    The machine instructions path taken on any Unix/Unix-like OS is the following:

    printf (C code)-> _printf (libc) -> trap -> kernel + driver work -> return from trap -> return from _printf (libc) -> printf completion and return -> next machine instruction in C code

    In the case of this assembly code extract, it looks like the C printf is inlined by the compilateur which caused the _printf entry point to be visible in the assembly code.

    To make sure the C printf does not get decorated with a prefix (an underscore in this case), best if searching in all C headers for a _printf with a command like:

    find /usr/include -name *.h -exec grep _printf {} \; -print

提交回复
热议问题