Loader in C/C++
Loader is the program of the operating system which loads the executable from the disk into the primary memory(RAM) for execution. It allocates the memory space to the executable module in main memory and then transfers control to the beginning instruction of the program .
Example:
akash @aix(/ u / akash) #cat./ ak1.cpp #include<stdio.h> int main() { printf("Testing of Loader !"); return 0; } |
Compiling by xlC compiler:
akash @aix(/ u / akash) #xlC – o ak1.out./ ak1.cpp akash @aix(/ u / akash) #ls – lrt ak1 * -rw – rw – r– 1 akash dev 74 Nov 12 06 : 10 ak1.cpp
– rwxrwxr – x 1 akash dev 8562 Nov 12 06 : 34 ak1.out akash @aix(/ u / akash) #
What really happens while running the executable: One could also use strace command for the same.
akash@aix(/u/akash)# truss ./ak1.out
execve(“./ak1.out”, 0x2FF20A00, 0x200138A8) argc: 1
read_sysconfig(0xF06F8278, 0x00000010, 0xFFFFFFF9, 0x10000000, 0x200007BC, 0x000000C0, 0x06010000, 0xF076A0F0) = 0x00000000
sbrk(0x00000000) = 0x20000998
vmgetinfo(0x2FF20350, 7, 16) = 0
sbrk(0x00000000) = 0x20000998
sbrk(0x00000008) = 0x20000998
__libc_sbrk(0x00000000) = 0x200009A0
loadquery(2, 0x200009C8, 0x00001000) = 0
__loadx(0x0A040000, 0xF06F599C, 0x00000000, 0xF05BE208, 0x20001D20) = 0xF05BFD64
loadbind(0, 0xF0760BBC, 0xF06D0E54) = 0
kfcntl(0, F_GETFL, 0x00000000) = 67110914
kfcntl(1, F_GETFL, 0x00000000) = 67110914
kfcntl(2, F_GETFL, 0x00000000) = 67110914
kfcntl(2, F_GETFL, 0x00000000) = 67110914
kioctl(1, 22528, 0x00000000, 0x00000000) = 0
Testing of Loader !kwrite(1, ” T e s t i n g o f L”.., 19) = 19
kfcntl(1, F_GETFL, 0x00000070) = 67110914
kfcntl(2, F_GETFL, 0x2FF22FFC) = 67110914
_exit(0)
The first call which is displayed is ‘execve()‘ which actually is the loader . This loader creates the process which involves:
- Reading the file and creating an address space for the process.
- Page table entries for the instructions, data and program stack are created and the register set is initialized.
- Then, Executes a jump instruction to the first instruction of the program which generally causes a page fault and the first page of your instructions is brought into memory.
Below two points are not related to loader and are for just more information:
- Another thing we got is the kwrite call with the argument value which one passed to the printf function in our program. kwrite is system call which actually gets called from the printf function with the value passed to it and this is function is responsible to display the value to console with value passed to it.
- We also got the _exit(0) call at last instruction which is the _exit system call with argument status as 0 which signifies to return back to operating system with successful signal. This _exit got called from return(0) statement.
Recommended Posts:
- Equation of straight line passing through a given point which bisects it into two equal line segments
- Maximum absolute difference in an array
- Count the pairs of vowels in the given string
- X-Macros in C
- Lex program to check whether a given number is even or odd
- Lex Program to print the total characters, white spaces, tabs in the given input file
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- LEX program to count the number of vowels and consonants in a given string
- Print path from root to all nodes in a Complete Binary Tree
- Compiler Design | Recursive Descent Parser
- Count the number of 1's and 0's in a binary array using STL in C++ ?
- Find elements of an Array which are Odd and Even using STL in C++
- Find elements of an array which are divisible by N using STL in C++
- All reverse permutations of an array using STL in C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



