Where do I find the assembly code for a procedure called in the GCC assembly output?

后端 未结 3 694
萌比男神i
萌比男神i 2021-01-16 13:32

I\'ve been playing around with the assembly output switch for GCC:

gcc -S -c helloworld.c

helloworld.c:

#i         


        
相关标签:
3条回答
  • 2021-01-16 13:54

    EDIT: this is not actually an answer to the original question, but since it's getting upvoted the info seems to be useful, so...


    It's an optimization by GCC. Since your string does not contain any formatting characters and ends with a newline, GCC replaces the call with puts which produces the same output but is much faster (since it doesn't need to scan the string for formatting specifiers). Try something like:

    int main(int argc, char *argv[]){
        printf("Hello World!\nargc=%d", argc);
        return 0;
    }
    

    And you will see your printf in the assembly.

    0 讨论(0)
  • 2021-01-16 13:56

    answer:

    use the gcc flags -fno-builtin.

    Explaination:

    To improve the performance, GCC, Glibc will together make some modification to the standard C library. Because GCC and Glibc works together for almost all the userspace applications, so the builtin function is turned on default, which will convert printf() function call with only one parameter with puts() function call.

    You can also tell GCC not to convert a specific builtin function call by using flags like -fno-builtin-printf. Here is the complete list of the builtin functions which you can stop GCC to convert.

    here is the detail explanation of the flag.

    0 讨论(0)
  • 2021-01-16 13:56

    As user unwind mentioned:

    This is because your C program is expecting to be linked with the standard C runtime library, which has puts() in it. You can of course dig up the library file on your system as disassemble it, if you feel like it.

    So I used objdump to disassemble libc.a and found the assembly code under the <puts> section: objdump -d /usr/lib/x86_64-linux-gnu/libc.a

    0 讨论(0)
提交回复
热议问题