How do I get a full assembly code from c file?

前端 未结 2 1541
一向
一向 2021-01-24 13:08

I\'m currently trying to figure out the way to produce equivalent assembly code from corresponding c source file. (pardon me for not being able to speak English fluently, I\'m n

相关标签:
2条回答
  • 2021-01-24 13:50

    _exp is the standard math library function double exp(double); apparently you're on a platform that prepends a leading underscore to C symbol names.

    Given a .s that calls some library functions, build it the same way you would a .c file that calls library functions:

    gcc foo.S -o foo  -lm
    

    You'll get a dynamic executable by default.


    But if you really want all the code in one file with no external dependencies, you can link your .c into a static executable and disassemble that.

    gcc -O3 -march=native foo.c -o foo -static -lm
    objdump -drwC -Mintel foo > foo.s
    

    There's no guarantee that the _exp implementation in libm.a (static library) is identical to the one you'd get in libm.so or libm.dll or whatever, because it's a different file. This is especially true for a function like memcpy where dynamic-linker tricks are often used to select an optimal version (for your CPU) at run-time.

    0 讨论(0)
  • 2021-01-24 14:12

    It is not possible in general, there are exceptions sure, I could craft one so that means other folks can too, but it isnt an interesting program.

    Normally your C program, your main() entry point is only a percentage of the code. There is a bootstrap that contains the actual entry point for the operating system to launch your program, this does some things that prepare your virtual memory space so that your program can run. Zeros .bss and other such things. that is often and or should be written in assembly language (otherwise you get a chicken and egg problem) but not an assembly language file you will see unless you go find the sources for the C library, you will often get an object as part of the toolchain along with other compiler libraries, etc.

    Then if you make any C calls or create code that results in a compiler library call (perform a divide on a platform that doesnt support divide, perform floating point on a platform that doesnt have floating point, etc) that is another object that came from some other C or assembly that is part of the library or compiler sources and is not something you will see during the compile/assemble/link (the chain in toolchain) process.

    So except for specifically crafted trivial programs or specifically crafted tools for this purpose (for specific likely baremetal platforms), you will not see your whole program turn into one big assembly source file before it gets assembled then linked.

    If not baremetal then there is of course the operating system layer which you certainly would not get to see as part of your source code, ultimately the C library calls that need the system will have a place where they do that, all compiled to object/lib before you use them, and the assembly sources for the operating system side is part of some other source and build process somewhere else.

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