In this video, we'll be looking into what is the use of underscore stat in C.
We know that underscore stat is the entry point entry point of your program, but from the programmer's
perspective, the main function is the entry point for your program.
It is a from OS perspective, underscore stat is a usual entry point.
This.
Who is providing this underscore stat how it is getting included.
Because from our C file we are not writing underscore stat.
So this is provided by a file on a CRT 1.0.
CRT stands for C Runtime Environment and this contains the startup code for the C runtime environment.
So you remember right, we have in our bare metal programming, we wrote uh, the uh startup code,
which is used to copy the data from flash to the Ram.
So there is uh, and it also initialized the stack pointer and arrow data, all those.
So here also, uh, we have a C runtime, uh, startup code for C runtime environment, which sets up
the C runtime, and it is present in this file if you go and run this command.
Here you can see there is a function known as underscore start and it is calling libc start main which
is not defined here libc CSU finish.
And if you remember we have run some commands in the first very initial thing.
We have executed this command uh, where we were compiled our object file.
And then we also added CRT 1.0, CRT end point zero and CRT zero and C library.
So we gave to the linker.
And linker has merged all these sections and performed relocation which is section placement and section
merging.
So if you see in command CRT one has start.
It has undefined symbol for main and libc.
Statman is not here.
It is undefined.
So it should be in other files like uh CRT it does not have, it has any and any uh and CRT n does not
have any symbols.
And then we have this one.
It is a file format because it is some link to that C library.
And C library will have libc CSU uh start main C library will have start main and which actually calls
our main.
So this is uh defined in the CRT 1.0 CRT one is the C runtime.
If you do not want to use the C runtime environment, you need to provide your own.
You need to write underscore start function.
And while you know, because you are not, uh, using the gcc startup code, you need to say no start
files while compiling your C code.
So this is what is happening in underscore start.
So it is setting up the C environment.
It is calling C start main from the C library which is internally calling your main.
And when your main exits uh C library stop main call is called.
And then underscore exit is where the process cleanup happens.
What is done in the C start mean the C start main checks if the effective UID is equals to real UID.
For security reasons, it calls the constructor and destructor for our program.
And finally it calls the main program with the correct arguments.
Whatever you are passing from the command line to the RC, argv and envp and then it calls the exit
with the return value of main.
So this is the functionality of libc start main.
Now who is calling this underscore start.
We know uh underscore start calls libc start main libc start main calls main.
That is fine.
But who is calling the underscore start.
So whenever you run a program uh shell or GUI it internally uses a system call known as exec V.
Exec V is a system call to execute create a new process exec v if you see this man page for exec V.
So this is a second system.
Call it whatever the file name you give it, it will execute that file and it will create a it will
create a new process replacing the program.
It will also initialize a stack heap and the data segments.
Okay.
It will check file name is valid.
Uh, all it will perform whether the file format is in Elf or not.
And, uh, it creates memory descriptor for executable whatever the file we passed.
And it will fill the appropriate values for stack and heap.
And this is defined in the kernel in the this particular thing.
And it is architecture specific for x86 64.
It is here.
And internally it performs a lot of things.
And finally it will the code executes in the kernel.
And then context switch happens and comes to the user space and executes underscore start.
So now let's again I'll show you what uh, we'll do is we'll keep a breakpoint.
Uh.
Uh, here what we were doing gdb user space and start.
I will stop at the start.
Now I will check the process of where it is running PSR UX.
Give me the PID and I will now check ls minus l proc 58035 fd.
Sorry I gave the wrong PID.
So when the star underscore start is not even started I mean it has.
We have worked here, we have stopped here.
Program is stopped before executing anything here.
It is still uh, the file descriptors, the standard file descriptors have been assigned zero, one,
two and as well as the memory map is also created.
So memory map is also created where the Elf file is mapped to the virtual address space and stack is
also created, and loader is also added.
So see, a lot of things are happening before even start executes.
The file descriptors are.
Standard file descriptors are assigned uh and uh.
Also the memory descriptor is created and you have stack created initialize.
Everything is done in the memory descriptor.
So linker is doing a lot.
Linker is doing.
So Lincoln is doing a lot here.
Uh, we can verify.
What is Lincoln doing with the linker and loader.
There are two things here.
Uh, loader is doing your relocation.
Linker is only doing the section merging in all as well as relocation is where when you.
That startup code is all the logic of the startup code is what here.
So if you do an LD verbose, it will give you, uh, the linker script which GCC is trying to use.
And if you see it is a very big, uh, script has a lot of sections, text section is there, and uh,
a lot of other sections are there.
And if you prefer entry, it is saying this, this will start.
Our C code will start at the underscore start.
So this linker script is informing us.