program loading/execution

断了今生、忘了曾经 提交于 2019-12-04 10:12:15

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)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!