I was wondering how to use GCC on my C source file to dump a mnemonic version of the machine code so I could see what my code was being compiled into. You can do this with Java but I haven’t been able to find a way with GCC.
I am trying to re-write a C method in assembly and seeing how GCC does it would be a big help.
If you compile with debug symbols (add
-gto your GCC command line, even if you’re also using-O31),you can use
objdump -Sto produce a more readable disassembly interleaved with C source.objdump -drwC -Mintelis nice:-rshows symbol names on relocations (so you’d seeputsin thecallinstruction below)-Rshows dynamic-linking relocations / symbol names (useful on shared libraries)-Cdemangles C++ symbol names-wis "wide" mode: it doesn’t line-wrap the machine-code bytes-Mintel: use GAS/binutils MASM-like.intel_syntax noprefixsyntax instead of AT&T-S: interleave source lines with disassembly.You could put something like
alias disas="objdump -drwCS -Mintel"in your~/.bashrc. If not on x86, or if you like AT&T syntax, omit-Mintel.Example:
Note that this isn’t using
-rso thecall rel32=-4isn’t annotated with theputssymbol name. And looks like a brokencallthat jumps into the middle of the call instruction in main. Remember that therel32displacement in the call encoding is just a placeholder until the linker fills in a real offset (to a PLT stub in this case, unless you statically link libc).Footnote 1: Interleaving source can be messy and not very helpful in optimized builds; for that, consider https://godbolt.org/ or other ways of visualizing which instructions go with which source lines. In optimized code there’s not always a single source line that accounts for an instruction but the debug info will pick one source line for each asm instruction.