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?
Compilers and executable binaries are remotely related. (the actual executable is built by the linker ld
, not the compiler).
On Linux systems, the linux kernel use copy-on-write and demand-paging techniques to lazily load the program pages, for ELF executables. Shared libraries may be dynamically loaded and preferably contain position independent code.
You could be interested in reading about compiler construction, Levine's book on linkers & loaders, the Linux Assembly Howto, the Program Library Howto, the ldd(1), execve(2), intro(2), fork(2), mmap(2), dlopen(3), elf(5), proc(5), signal(7) man pages.
Try also to understand what cat /proc/self/maps
is showing you (the memory map of the process doing that cat
). You can also play with objdump
.
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.

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
)
来源:https://stackoverflow.com/questions/10098152/program-loading-execution