为什么GCC不会将.section添加到程序集中

时间:2015-07-19 22:41:20

标签: c gcc assembly x86 gas

如果你看一下这个程序的第二行,它只是说" .text"。当我编写汇编程序时,我不得不放置" .section .text"为什么GCC省略" .section"。我也注意到它在声明rodata bellow" .section .rodata"之前包括它。

还想知道" .type sum@function"呢?我今天早上没有它就写了一个汇编函数,它执行得很好。

.file   "test.c"
        .text
        .globl  sum
        .type   sum, @function
    sum:
    .LFB0:
        .cfi_startproc
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register 6
        movss   %xmm0, -4(%rbp)
        movss   %xmm1, -8(%rbp)
        movss   -4(%rbp), %xmm0
        mulss   -8(%rbp), %xmm0
        cvttss2si   %xmm0, %eax
        popq    %rbp
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc
    .LFE0:
        .size   sum, .-sum
        .section    .rodata
    .LC2:
        .string "%d\n"
        .text
        .globl  main
        .type   main, @function
    main:
    .LFB1:
        .cfi_startproc
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register 6
        subq    $16, %rsp
        movss   .LC0(%rip), %xmm1
        movss   .LC1(%rip), %xmm0
        call    sum
        movl    %eax, -4(%rbp)
        movl    -4(%rbp), %eax
        movl    %eax, %esi
        movl    $.LC2, %edi
        movl    $0, %eax
        call    printf
        movl    $0, %eax
        leave
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc
    .LFE1:
        .size   main, .-main
        .section    .rodata
        .align 4
    .LC0:
        .long   1092930765
        .align 4
    .LC1:
        .long   1092825907
        .ident  "GCC: (Ubuntu 4.9.2-10ubuntu13) 4.9.2"
        .section    .note.GNU-stack,"",@progbits

1 个答案:

答案 0 :(得分:1)

在答案中收集一些评论:

在任意节名称出现之前,.text.data.bss是汇编程序指令。现在,您可以改为编写.section .text。这应记录在案in the GNU as manual。 (链接到最新版本)。

    .type   sum, @function

设置一些ELF符号类型的东西。 IDK如果这对于动态链接很重要,但它不适用于静态链接。编译器发出了一些很多的内容,但实际上并不需要运行代码。这不是一件坏事。

对于gcc asm输出中的其他内容,请查看我对GCC Assembly Optimizations - Why are these equivalent?

的回答