Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

program loading/execution

Tags:

c++

linux

gcc

llvm

elf

I'm a beginner in compilers but I'm very interested in learning about how a program is structured (the binary) and how it is read and loaded in memory for execution. What ebooks/books/tutorials do you guys suggest me reading for a quick start?

like image 434
JohnTortugo Avatar asked Jan 16 '23 23:01

JohnTortugo


1 Answers

ELF File Layout

An ELF file has two views:

  • The program header shows the segments used at run-time
  • The section header lists the set of sections of the binary

Each ELF file is made up of one ELF header, followed by file data.

The file data can include:

  • Program header table, describing zero or more segments
  • Section header table, describing zero or more sections
  • Data referred to by entries in the program header table or section header table

The segments contain information that is necessary for runtime execution of the file, while sections contain important data for linking and relocation. Any byte in the entire file can be owned by at most one section, and there can be orphan bytes which are not owned by any section.

enter image description here

Loading a program to memory

In computing, a loader is the part of an operating system that is responsible for loading programs.

It is one of the essential stages in the process of starting a program, as it places programs into memory and prepares them for execution.

Loading a program involves :

  • reading the contents of executable file, the file containing the program text, into memory
  • carrying out other required preparatory tasks to prepare the executable for running.

Once loading is complete, the operating system starts the program by passing control to the loaded program code.

The *NIX way

In Unix, the loader is the handler for the system call execve().

The Unix loader's tasks include:

  • validation (permissions, memory requirements etc.)
  • copying the program image from the disk into main memory
  • copying the command-line arguments on the stack
  • initializing registers (e.g., the stack pointer)
  • jumping to the program entry point (_start)
like image 182
Dr.Kameleon Avatar answered Jan 25 '23 11:01

Dr.Kameleon