Getting Assembler Output from C/C Source in GCC
To analyze how code is compiled, it's beneficial to retrieve the emitted assembly code. In GCC, this can be achieved using the -S option.
gcc -S helloworld.c
This will invoke the preprocessor and initial compilation, but halt before running the assembler.
For more comprehensive assembly output, consider using -fverbose-asm alongside -O0. However, note that this works best at default optimization levels.
The default output file is helloworld.s, but can be customized using -o. Additionally, -o - can be used to direct output directly to standard output.
gcc -S -o my_asm_output.s helloworld.c
If only the object file is available, objdump with the --disassemble option can be employed:
objdump -S --disassemble helloworld > helloworld.dump
This will interleave source lines with disassembly output. Enabling debugging (-g) and avoiding stripping can enhance the disassembly details.
For even more customization, consider options such as -rwC (symbol relocations), -Mintel (Intel syntax), and -r (important for object files with placeholder symbol references).
For instance, the following command provides a detailed dump using Intel syntax:
objdump -drwC -Mintel -S foo.o | less
The above is the detailed content of How Can I Get Assembly Code Output from GCC Compiling C/C Source Code?. For more information, please follow other related articles on the PHP Chinese website!