Search code examples
clinuxgdbelf

What's the difference between `__libc_start_main` and `__libc_start_call_main`?


I recently learnt about the __libc_start_main() function. I thought that __libc_start_main() calls the main() function as described in this answer, but when I checked the stack pointer $rsp after inserting a breakpoint in the main() function in the test program below, it's the address of __libc_start_call_main().

What's the difference between __libc_start_main() and __libc_start_call_main()?

Source code

#include <stdio.h>

int main(void) 
{
    puts("Sunghyeon Lee");
}

gdb output

──(kali㉿kali)-[~]
└─$ gdb test   
GNU gdb (Debian 12.1-3) 12.1
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...
(No debugging symbols found in test)
(gdb) b *main
Breakpoint 1 at 0x1139
(gdb) r
Starting program: /home/kali/test 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Breakpoint 1, 0x0000555555555139 in main ()
(gdb) x/a $rsp
0x7fffffffdec8: 0x7ffff7dd920a <__libc_start_call_main+122>

I searched for information about the difference between __libc_start_main() and __libc_start_call_main() but I haven't found an explanation.


Solution

  • I have never found the explanation about it.

    Take a look at the commit which created __libc_start_call_main.

    Effectively a chunk of __libc_start_main was split out into a separate routine.