Generating a Mixed Source and Assembly List Using GCC
When do debugging and optimization, we sometimes need to generate and investigate into the assembly generated by the compiler. Generating a mixed source and assembly list will help the programmer a lot for debugging and optimization. gcc can achieve this by working the the assembler.
Generate assembly list mixed with the source code
Just add these gcc compile options:
-Wa,-adhln -g
The command:
$ gcc -Wa,-adhln -g source_code.c > assembly_list.s
The options:
-g: Produce debugging information -Wa,option: Pass option as an option to the assembler -adhln: a: turn on listings d: omit debugging directives; n: omit forms processing h: include high-level source l: include assembly
One example
The source code:
helloworld.c:
#include <stdio.h>
int main()
{
printf("Helloworld!n");
return 0;
}
The command:
$ gcc -Wa,-adhln -g helloworld.c > helloworld.s
helloworld.s:
...
0:helloworld.c **** #include <stdio.h>
1:helloworld.c ****
2:helloworld.c **** int main()
3:helloworld.c **** {
19 .loc 1 4 0
20 .cfi_startproc
21 0000 55 pushq %rbp
22 .LCFI0:
23 .cfi_def_cfa_offset 16
24 0001 4889E5 movq %rsp, %rbp
25 .cfi_offset 6, -16
26 .LCFI1:
27 .cfi_def_cfa_register 6
4:helloworld.c **** printf("Helloworld!n");
28 .loc 1 5 0
29 0004 BF000000 movl $.LC0, %edi
29 00
30 0009 E8000000 call puts
30 00
5:helloworld.c **** return 0;
31 .loc 1 6 0
32 000e B8000000 movl $0, %eax
32 00
6:helloworld.c **** }
...
Updated history
17 Mar. 2010. Add example source code.
24 Feb. 2010.
Oct. 19, 2011. revise and make the writting better.
Author: Zhiqiang Ma
Last updated on: Oct 19, 2011
Posted on: Nov 2, 2009
Views: 1,284
Tags: gcc, Linux, Programming, Tutorial
Tags: gcc, Linux, Programming, Tutorial