Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which function in glibc calls the main function

Tags:

c

linux

gcc

glibc

I am trying to understand how Linux launches a program. I read somewhere that some function in glibc calls the main function. Profiling with callgrind and looking at the call-graphs in Kcachegrind, I see below main which calls main. But I don't understand this, a function can't be named such. So my question is which function in the glibc actually starts the main function.

like image 756
pythonic Avatar asked May 17 '12 20:05

pythonic


People also ask

Which function calls the main function?

In 'C', the "main" function is called by the operating system when the user runs the program and it is treated the same way as every function, it has a return type. Although you can call the main() function within itself and it is called recursion.

Who gives a call to main function?

The operating system calls the main() function.

What is a libc function?

The term "libc" is commonly used as a shorthand for the "standard C library", a library of standard functions that can be used by all C programs (and sometimes by programs in other languages).

Where is __ Libc_start_main?

After this the value __libc_start_main function is called. __libc_start_main is defined in the glibc sources sysdeps/generic/libc-start.


1 Answers

Following valgrind's own help you'll find this explanation for the option --show-below-main:

By default, stack traces for errors do not show any functions that appear beneath main because most of the time it's uninteresting C library stuff and/or gobbledygook. Alternatively, if main is not present in the stack trace, stack traces will not show any functions below main-like functions such as glibc's __libc_start_main. Furthermore, if main-like functions are present in the trace, they are normalised as (below main), in order to make the output more deterministic.

As such, below main is not the function which calls main itself, but __libc_start_main.

like image 104
Claudio Avatar answered Sep 28 '22 09:09

Claudio