assembly - How does a loader in operating system work? -


i know loader program loads program main memory. so,how works? happens exactly? when loader loads program, entry in pcb created , program put in job pool. how executable codes of program copied main memory? in simple how load codes of file main memory using c or c++ ?

this largely depends on operating system. write here linux specific, similar things happens on other operating systems.

first, fork() call initiated, creating new process (and appropriate pcb entry). next step calling exec system call hard work. i'll assume we're talking elf executables here.

in case, after recognizing elf executable (by inspecting magic number) exec call load_elf_binary (http://lxr.free-electrons.com/source/fs/binfmt_elf.c#l664)

the argument struct linux_binprm *bprm passed function contains metadata binary (already filled exec) such executable name, environment info, etc. (http://lxr.free-electrons.com/source/include/linux/binfmts.h#l14)

the elf program loading complex task, , requires understanding of elf format.

the resource on can found here

in nutshell, these interesting steps kernel performing:

  • checks elf headers find if there's program interpreter specified binary (ld.so used dynamically linking required libraries, peforms relocations, calls initialization functions linked libraries).

  • setup new executable environment (setup the new credentials, mark point of no return, example)

  • setup memory layout (like randomize stack) , map pages executable memory

  • calls start_thread , starts either program or interpreter (ld.so)

good document on understanding of elf interpreters can found here

resources:


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -