Home > Backend Development > C++ > How to Generate Assembly Code from C/C using GCC and objdump?

How to Generate Assembly Code from C/C using GCC and objdump?

Mary-Kate Olsen
Release: 2024-12-27 02:48:14
Original
541 people have browsed it

How to Generate Assembly Code from C/C   using GCC and objdump?

How to Output Assembler Code from C/C Source in GCC

When analyzing compiled code, accessing the generated assembly code is essential. In GCC, this can be achieved using the "-S" option.

Using the -S Option

By default, "-S" runs the preprocessor on the source file, performs initial compilation but stops before invoking the assembler. Combined with "-fverbose-asm," this option associates C code with the assembly code as a comment, especially useful at optimization level -O0.

gcc -S helloworld.c
Copy after login

Customizing Output File

The output file is typically named after the source file with a .s extension. To customize the output file's location, use the "-o" option:

gcc -S -o my_asm_output.s helloworld.c
Copy after login

Alternatively, you can output to the console by using "-o -":

gcc -S -o - helloworld.c | less
Copy after login

Accessing Assembly for Executable Objects

If you do not have the original source code but have the object file, use objdump with the "--disassemble" option (-d):

objdump -S --disassemble helloworld > helloworld.dump
Copy after login

Enable debugging in the original compilation (-g) for enhanced disassembly output with source line references.

Additional Objdump Options

Consider using these objdump options for more detailed analysis:

  • -rwC: Symbol relocations, disable line-wrapping, and demangle C names
  • -Mintel: Intel syntax for x86 disassembly

For example, the following command provides rich disassembly output:

objdump -drwC -Mintel -S foo.o | less
Copy after login

Remember, "-r" is crucial to display symbol references in a .o file with placeholders.

The above is the detailed content of How to Generate Assembly Code from C/C using GCC and objdump?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template