"In-depth understanding of computer Systems" chapter Seventh study notes

Source: Internet
Author: User

"in-depth understanding of computer Systems" Chapter Seventh study notes

20135109 Gao Yi Tong

First, about the link

1.连接:将各种代码和数据部分收集起来并组合成为一个单一文件的过程。This file can be loaded or copied to memory and executed.

2.连接可以执行于编译时,也就是在源代码被翻译成机器代码。也可以执行于加载时,也就是程序被加载器加载到存储器并执行时Executed at run time, there is an application to execute.

3.连接是由链接器的程序自动执行的。

4.连接使分离编译成为可能。second, compiler driver

1.大部分编译系统提供编译驱动程序:代表用户在需要时调用语言预处理器、编译器、汇编器和链接器。

    (1)C预处理器:源程序main.c->ASCII码中间文件main.i     (2)C编译器:main.i->ASCII码汇编语言文件main.s     (3)C汇编器:main.s->可重定位目标文件 2.运行链接器程序ld,将各种.o文件以及必要的系统目标文件组合起来,创建可执行文件 3.运行可执行文件:./可执行文件名字 4.外壳调用操作系统中加载器函数,拷贝可执行文件中的代码和数据到存储器,将控制转移到这个程序的开头

Third, static link

1.以一组可重定位目标文件和命令行参数作为输入,生成一个完全链接的可以加载和运行的可执行目标文件作为输出。

2.输入的可重定位的目标文件由各种不同的代码和数据节组成。

3.指令在一个节中,初始化的全局变量在另一个节中,而未初始化的变量又在另外一个节中。-Symbolic parsing

The target file definition and reference symbol. The purpose of symbolic parsing is to associate each symbol reference with exactly one symbol definition.

-Reposition

The compiler and assembler generate hungry code and data sections starting at address 0. The linker repositions These sections by linking each symbol definition to a memory location and then modifying all references to those symbols so that they point to the memory location.

Iv. Target Documents

Three forms of the target file

    • relocatable target file (compiler and assembler can be generated)
    • Executable target file (linker can be generated)
    • Shared destination file (compiler and assembler can be generated)

Five, can relocate the target file

  • . Text: The machine code of the compiled program.
  • . Rodata: Read-only data, such as format strings in printf statements and jump tables for switch statements.
  • . Data: A global C variable that has been initialized.
  • . BSS: Uninitialized global C variable. In the target file, this section does not occupy the actual space, it is just a placeholder.
  • . symtab: A symbol table that holds information about functions and global variables that are defined and referenced in the program.
  • . Rel.text: A list of locations in the. Text section, which you need to modify when the linker combines this target file with other files.
  • . Rel.data: Relocation information for any global variables referenced or defined by the module.
  • . Debug: A debug symbol table whose entries are local variables and type definitions defined in the program, global variables defined and referenced in the program, and the original C source file. This table is only available when the build driver is called with the-G option.
  • . Line: The mapping between the row numbers in the original C source program and the machine directives in the. Text section.
  • . strtab: A string table whose contents include: the symbol table in the. Symtab and. Debug sections, and the section name in the section header. The string table is a null-terminated sequence of strings.

Vi. Symbols and Symbols table

每个可重定位目标模块m都有一个符号表,包含m所定义和引用的符号的信息。

在链接器的上下文中,三种不同的符号: 1.有m定义并能被其他模块引用的全局符号。全局链接器对应于非静态的C函数以及被定义为Cstatic 属性的全局变量。 2.有其他模块定义并被模块m以引用的全局符号——外部符号,对应于定义在其他模块中的C函数和变量 3.只被模块m定义和引用的本地符号。Seven, symbolic analysis based on the definition of strong and weak symbols, the UNIX linker uses the following rules to handle symbols for multiple definitions:
    • Rule 1: Multiple strong symbols are not allowed.
    • Rule 2: If you have a strong symbol and multiple weak symbols, select the strong symbol.
    • Rule 3: If there are multiple weak symbols, select one of these weak symbols.
during the symbolic parsing phase, the linker scans the relocatable destination and archive files in the same order that they appear on the compiler driver command line, from left to right. In this scan, the linker maintains a set of relocatable target files E (the files in this collection are merged to form an executable file), an unresolved symbol (that is, a symbol that references but not yet defined), and a set of symbols defined in the previous input file, D. Initially, E, U, and D are empty.

Eight, re-positioning

Relocation consists of two steps: 1. Reposition sections and symbol definitions. In this step, the linker merges all sections of the same type into a new aggregation section of the same type. The linker then assigns the run-time memory address to the new aggregation section, assigns each section defined by the input module, and assigns each symbol to the input module definition. When this step is complete, each instruction and global variable in the program has a unique runtime memory address. 2. Reposition the symbol reference in the section. In this step, the linker modifies the reference to each symbol in the Code section and data section so that they point to the correct run-time address. To perform this step, the linker relies on the data structure in the Relocatable target module called the relocation entry.

IX. executable Target file

X. Loading executable target files

The loader copies the execution code and data from the disk into storage in the executable target file, and then runs the program by jumping to the first instruction or entry point of the program. The process of copying the program to memory and running it is called loading.

    • In a 32-bit Linux system, the code snippet always starts at the address 0x08048000.
    • The data segment is located at the next 4KB aligned address.
    • The run-time heap grows on the next first 4KB aligned address after the read/write segment, and increases by calling the malloc library.
    • One segment is reserved for the shared library.
    • The user stack is always the largest legitimate user address to begin with, increasing downward (to the low memory address direction). The segment that starts at the top of the stack is reserved for code and data for the part of the operating system that resides in the memory (that is, the kernel).

Xi. Dynamic Link Shared library

1, the disadvantages of the static library:

    • When a static library is updated, the program that uses the library needs to be re-linked with the updated library.
    • Because programs that use static libraries copy the target modules that are referenced by the application in the static library when they are linked, the code for functions such as printf and scanf are copied to the text segment of each running process at run time, creating redundancy and wasting scarce memory resources.

2. Shared Library

    • A shared library is a target module that, at run time, can be loaded into any memory address and linked to a program in memory. This process, called dynamic linking, is performed by a program called a dynamic linker.
    • Shared libraries are also known as shared destinations, which are typically represented by the. So suffix in Unix systems. Microsoft's operating system uses a large number of shared libraries, which are called DLLs (dynamic-link libraries).
    • Shared libraries are "shared" in two different ways (called "implicit linking" and "Show link" in Windows, respectively).
    • First, in any given file system, there is only one. So file for a library. All executable target files referencing the library share the code and data in this. so file, rather than being copied and embedded in the executable file that references them as the contents of the static library. Second, in memory, a copy of the. Text section of a shared library can be shared by a different running process.

13. Tools for processing target files

"In-depth understanding of computer Systems" chapter Seventh study notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.