D
D
DVoropaev2019-03-15 00:43:10
linux
DVoropaev, 2019-03-15 00:43:10

What do these lines mean in assembler?

I wrote a C program that simply exits with code 0:

int main(){
   return 0;
}

With the help of gcc, I overtook the sish code into assembler code:
gcc -S ./main
I got this:
.file	"main.c"
  .text
  .globl	main
  .type	main, @function
main:
.LFB0:
  .cfi_startproc
  pushq	%rbp
  .cfi_def_cfa_offset 16
  .cfi_offset 6, -16
  movq	%rsp, %rbp
  .cfi_def_cfa_register 6
  movl	$0, %eax
  popq	%rbp
  .cfi_def_cfa 7, 8
  ret
  .cfi_endproc
.LFE0:
  .size	main, .-main
  .ident	"GCC: (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0"
  .section	.note.GNU-stack,"",@progbits

These lines intrigued me:
.file	"main.c"
...
.ident	"GCC: (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0"

Why are they? what do they give? Why in the code is the name of the file with the source code and the version of my ubunta?
Do they get into the executable file during the final compilation and why are they used?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2019-03-15
@DVoropaev

.file "main.c"
Why is the name of the source code file in the code

To generate debug information in DWARF format.
...and my ubuntu version?
It's not your ubuntu version, it's the compiler identification string, which turned out to be the name of your OS.
The file names enter the symbol table as symbols of the FILE type and into the debug information section:
$ readelf -a hello | grep FILE
    28: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
    37: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS hello.c
    38: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
    41: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS

If the debug information includes line numbers, then they will also refer to the file name, associating the address range with the line number in the particular source file.
The compiler identification string goes into the section .commentof the object file, and from there into the executable file:
$ objdump -s -j .comment hello

hello:     file format elf64-x86-64

Contents of section .comment:
 0000 4743433a 20284465 6269616e 20362e33  GCC: (Debian 6.3
 0010 2e302d31 382b6465 62397531 2920362e  .0-18+deb9u1) 6.
 0020 332e3020 32303137 30353136 00        3.0 20170516.

All this can be cut off from the executable file with the strip, objcopy command or the corresponding linker script.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question