0

If you look at the second line of this program it just says ".text". When I write assembly programs I though that you had to put ".section .text" Why does GCC omit the ".section". I also noticed that it includes it before declaring rodata bellow ".section .rodata".

Also just wondering what ".type sum, @function" does? I wrote an assembly function this morning without it and it executed fine.

.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
Mr. Perfectionist
  • 2,605
  • 2
  • 24
  • 35
chasep255
  • 11,745
  • 8
  • 58
  • 115
  • 2
    You can use `.text` too. The assembler doesn't know it has been put there by a compiler or a human ;) It's basically an alias for `.section .text` if you don't specify a subsection number. The `.type` just sets the symbol type so that it is marked as defining a function (mostly relevant for shared libraries). – Jester Jul 19 '15 at 22:50
  • 4
    You may also find the gnu Assembler reference helpful: https://sourceware.org/binutils/docs-2.20/as/ – Michael Burr Jul 19 '15 at 22:53
  • 5
    The names of the standard sections are keywords, too for simplicity and backwards compatibility. Note some earlier object file formats did not allow for arbitrary sections and had only `.text`, `.bss`, `.data`. – too honest for this site Jul 19 '15 at 23:11
  • I don't understand the downvoting for this question. It's entirely reasonable, given that both notations work, and gcc on OSX/Darwin *does* emit `.section` directives for Mach-O assembler - which uses an essentially unmodified x86-64 ELF ABI. – Brett Hale Jul 20 '15 at 13:03

1 Answers1

2

Collecting up some comments into an answer:

Before arbitrary section names were possible, .text, .data, and .bss were assembler directives. Now, you can write .section .text instead. This should all be documented in the GNU as manual. (linked to latest version).

    .type   sum, @function

sets some ELF symbol-type stuff. IDK if this matters for dynamic linking, but it doesn't for static linkage. There's a lot of stuff the compiler emits but that you don't actually need for your code to run. This is not a bad thing.

For the other things in gcc asm output, have a look at my answer to GCC Assembly Optimizations - Why are these equivalent?

Community
  • 1
  • 1
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847