Open In App

Loader in C/C++

The 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 the main memory and then transfers control to the beginning instruction of the program. 

The loader is an important part of the system, as it loads the program, places them in the memory, and prepares them for the final execution. Loading a program includes reading the content of the executable file that has program instructions from the memory, then it performs some other preparatory task to execute the task. 



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

The strace command could be used for loading the executable file from RAM for the execution.

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 loader is actually the first call that is displayed as ‘execve()‘. The loader is responsible for loading programs and libraries when the program started its execution. This loader loads the process that involves:

Below are two points that are not related to the loader and are for just more information:

Article Tags :