Summary of basic knowledge of operating system (seven) - link

1. Build system

The following is a hello.c program:

#include <stdio.h>

int main()
{
    printf("hello, world\n");
    return 0;
}

On Unix systems, the compiler converts source files into object files:

gcc -o hello hello.c

The process is roughly as follows:

  • Preprocessing stage: process preprocessing commands starting with #;
  • Compilation phase: translate into assembly files ;
  • Assembly stage: translate assembly files into relocatable object files;
  • Linking stage: Merge the relocatable object file and the separately precompiled object file such as printf.o to obtain the final executable object file.

2. Static linking

A static linker takes as input a set of relocatable object files and produces as output a fully linked executable object file . The linker mainly completes the following two tasks:

  • Symbol resolution: Each symbol corresponds to a function, a global variable or a static variable, and the purpose of symbol resolution is to associate each symbol reference with a symbol definition.
  • Relocation: The linker works by associating each symbol definition with a memory location, and then modifies all references to those symbols so that they point to that memory location.

3. Object file 

  • Executable object file : can be executed directly in memory ;
  • Relocatable object files : can be combined with other relocatable object files during the link phase to create an executable object file;
  • Shared object file : This is a special relocatable object file that can be dynamically loaded into memory and linked at runtime;

4. Dynamic linking

Static libraries have the following two problems:

  • When the static library is updated, the entire program must be relinked;
  • For a standard function library such as printf, if each program needs to have code, it will be a huge waste of resources.

Shared libraries are designed to solve these two problems of static libraries. They are usually denoted by the .so suffix on Linux systems, and they are called DLLs on Windows systems. It has the following characteristics:

  • A library has only one file in a given file system, and all executable object files that reference the library share this file, and it will not be copied to executable files that reference it;
  • In memory, a single copy of the .text section (the machine code of the compiled program) of a shared library can be shared by different running processes.

References:

  • Tanenbaum A S, Bos H. Modern operating systems[M]. Prentice Hall Press, 2014.
  • Tang Ziying, Zhe Fengping, Tang Xiaodan. Computer Operating System[M]. Xidian University Press, 2001.
  • Bryant, RE, & O'Hallaron, DR (2004). Deep understanding of computer systems.
  • Stevens. Advanced Programming in UNIX Environment [M]. People's Posts and Telecommunications Press, 2014.

Guess you like

Origin blog.csdn.net/daydayup858/article/details/129353665