gcc源代码好乱_如何使用GCC从源代码生成混合的源代码和程序集列表

gcc源代码好乱

When debugging and optimizing programs, developers sometimes need to generate and investigate into the assembly generated by the compiler. Generating a mixed source and assembly list will help a lot for debugging and optimization. gcc can achieve this by working with the assembler.

在调试和优化程序时,开发人员有时需要生成并调查编译器生成的程序集。 生成混合的源代码和程序集列表将有助于调试和优化。 gcc可以通过与汇编程序一起实现此目的。

生成与源代码混合的程序集列表 (Generate assembly list mixed with the source code)

Just add these gcc compile options:

只需添加以下gcc编译选项:

-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:

helloworld.c:

#include <stdio.h>
int main()
{
  printf("Hello world!\n");
  return 0;
}

The command:

命令:

$ gcc -Wa,-adhln -g helloworld.c > helloworld.s

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("Hello world!\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  **** }

    ...

另一个示例包含多个源文件 (Another example with more than 1 source files)

You may also use the method here for compiling programs from more than 1 C source files. For example, a project contains 2 source files as follows.

您也可以使用此处的方法从1个以上的C源文件编译程序。 例如,一个项目包含2个源文件,如下所示。

fun1.c:

fun1.c:

#include <stdio.h>

void fun1()
{
    printf("Hello world!\n");
}

main.c:

main.c:

#include <stdio.h>
void fun1();
int main()
{
    fun1();
    return 0;
}

You can generate the source-assembly list by

您可以通过以下方式生成源程序集列表

$ gcc -Wa,-adhln -g fun1.c main.c -o a > helloworld.s

For your reference, the helloworld.s file is as follows.

供参考,helloworld.s文件如下。

1                    .file   "fun1.c"
   2                    .text
   3                .Ltext0:
   4                    .section    .rodata
   5                .LC0:
   6 0000 48656C6C      .string "Hello world!"
   6      6F20776F 
   6      726C6421 
   6      00
   7                    .text
   8                    .globl  fun1
  10                fun1:
  11                .LFB0:
  12                    .file 1 "fun1.c"
   1:fun1.c        **** #include <stdio.h>
   2:fun1.c        **** 
   3:fun1.c        **** void fun1()
   4:fun1.c        **** {
  13                    .loc 1 4 0
  14                    .cfi_startproc
  15 0000 55            pushq   %rbp
  16                    .cfi_def_cfa_offset 16
  17                    .cfi_offset 6, -16
  18 0001 4889E5        movq    %rsp, %rbp
  19                    .cfi_def_cfa_register 6
   5:fun1.c        ****     printf("Hello world!\n");
  20                    .loc 1 5 0
  21 0004 BF000000      movl    $.LC0, %edi
  21      00
  22 0009 E8000000      call    puts
  22      00
   6:fun1.c        **** }
  23                    .loc 1 6 0
  24 000e 5D            popq    %rbp
  25                    .cfi_def_cfa 7, 8
  26 000f C3            ret
  27                    .cfi_endproc
  28                .LFE0:
  30                .Letext0:
   1                    .file   "main.c"
   2                    .text
   3                .Ltext0:
   4                    .globl  main
   6                main:
   7                .LFB0:
   8                    .file 1 "main.c"
   1:main.c        **** #include <stdio.h>
   2:main.c        **** void fun1();
   3:main.c        **** int main()
   4:main.c        **** {
   9                    .loc 1 4 0
  10                    .cfi_startproc
  11 0000 55            pushq   %rbp
  12                    .cfi_def_cfa_offset 16
  13                    .cfi_offset 6, -16
  14 0001 4889E5        movq    %rsp, %rbp
  15                    .cfi_def_cfa_register 6
   5:main.c        ****     fun1();
  16                    .loc 1 5 0
  17 0004 B8000000      movl    $0, %eax
  17      00
  18 0009 E8000000      call    fun1
  18      00
   6:main.c        ****     return 0;
  19                    .loc 1 6 0
  20 000e B8000000      movl    $0, %eax
  20      00
   7:main.c        **** }
  21                    .loc 1 7 0
  22 0013 5D            popq    %rbp
  23                    .cfi_def_cfa 7, 8
  24 0014 C3            ret
  25                    .cfi_endproc
  26                .LFE0:
  28                .Letext0:

翻译自: https://www.systutorials.com/generate-a-mixed-source-and-assembly-listing-using-gcc/

gcc源代码好乱

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值