How does one do this?
If I want to analyze how something is getting compiled, how would I get the emitted assembly code?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use the -S option to
gcc(org++), optionally with -fverbose-asm which works well at the default -O0 to attach C names to asm operands as comments. It works less well at any optimization level, which you normally want to use to get asm worth looking at.This will run the preprocessor (cpp) over helloworld.c, perform the initial compilation and then stop before the assembler is run. For useful compiler options to use in that case, see How to remove "noise" from GCC/clang assembly output? (or just look at your code on Matt Godbolt’s online Compiler Explorer which filters out directives and stuff, and has highlighting to match up source lines with asm using debug information.)
By default, this will output the file
helloworld.s. The output file can be still be set by using the -o option, including-o -to write to standard output for pipe into less.Of course, this only works if you have the original source. An alternative if you only have the resultant object file is to use objdump, by setting the
--disassembleoption (or-dfor the abbreviated form).-Sinterleaves source lines with normal disassembly output, so this option works best if debugging option is enabled for the object file (-g at compilation time) and the file hasn’t been stripped.Running
file helloworldwill give you some indication as to the level of detail that you will get by using objdump.Other useful
objdumpoptions include-rwC(to show symbol relocations, disable line-wrapping of long machine code, and demangle C++ names). And if you don’t like AT&T syntax for x86,-Mintel. See the man page.So for example,
objdump -drwC -Mintel -S foo.o | less.-ris very important with a.othat only has00 00 00 00placeholders for symbol references, as opposed to a linked executable.