1

My system : Ubuntu 22.04.3 running on x86_64 CPU.

I have this C program in a file named test.c :

int main(){

    long int a = 10;
    long int b = 20;
}

I've compiled the file with "gcc test.c -fno-stack-protector -mno-red-zone -o test" and then I executed "objdump -dw -M suffix test" ( I only show the _start function ) :

0000000000001040 <_start>:
    1040:   f3 0f 1e fa             endbr64 
    1044:   31 ed                   xorl   %ebp,%ebp
    1046:   49 89 d1                movq   %rdx,%r9
    1049:   5e                      popq   %rsi
    104a:   48 89 e2                movq   %rsp,%rdx
    104d:   48 83 e4 f0             andq   $0xfffffffffffffff0,%rsp
    1051:   50                      pushq  %rax
    1052:   54                      pushq  %rsp
    1053:   45 31 c0                xorl   %r8d,%r8d
    1056:   31 c9                   xorl   %ecx,%ecx
    1058:   48 8d 3d ca 00 00 00    leaq   0xca(%rip),%rdi    # 1129 <main>
    105f:   ff 15 73 2f 00 00       callq  *0x2f73(%rip)   # 3fd8                                                           
                                                           <__libc_start_main@GLIBC_2.34>  
    1065:   f4                      hlt    
    1066:   66 2e 0f 1f 84 00 00 00 00 00   cs nopw 0x0(%rax,%rax,1)


My assumption is that we have the following call chain within which _start calls __libc_start_main which in turn calls main :

_start -> __libc_start_main -> main

My question :

Is the main function of my C program called by __libc_start_main in my specific Ubuntu system ?

2
  • 2
    It seems like that. But why do you wonder about such very implementation-specific details? As an application developer the execution starts with the user-provided main function, anything before that is irrelevant. Commented Aug 30, 2023 at 10:18
  • 3
    It's not distro-specific, that's just how glibc does it on GNU/Linux systems. So some of the startup code can be in libc.so instead of statically linked into each executable. I'm surprised google didn't find more useful results with a specific function name to search on; site:stackoverflow.com __libc_start_main finds multiple highly-relevant questions. Except that What is __libc_start_main and _start? is wrong, just a random guess at how things might work. (See my comments on pax's answer.) Commented Aug 30, 2023 at 10:28

1 Answer 1

4

Is the main function of my C program called by __libc_start_main in my specific Ubuntu system?

Yes. It takes a pointer to your main function as an argument. That is, the type int (*)(int, char **, char **).

This instruction uses instruction-pointer-relative addressing to refer to your main function, and puts the calculated result in the rdi register as an argument:

1058:   48 8d 3d ca 00 00 00    leaq   0xca(%rip),%rdi    # 1129 <main>

You may wonder, how is main's address of 0x1129 calculated? You simply get the current value of the instruction pointer 0x1058, add 0x7 (the length of the instruction itself), then the 0xCA offset:

0x1058 + 0x7 + 0xCA = 0x1129

libc_start_main would then call your function indirectly through the contents of that register.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.