The role of the linker (linker)--csapp 7th Chapter Reading notes

Source: Internet
Author: User
Tags mathematical functions

First of all, why do I have to read this chapter? This semester open OS class, in Morden Operating system read the content and process related to see such a sentence: "Process is fundamentally a container that holds all the infor Mation needed to run a program. " At that moment, I thought of the ambiguous "target executable" that I had seen before in Csapp, so I read it again in the 7th chapter.

To understand the role of linker, the first thing to understand is what position he is in the whole computer system.

The process of how a program is removed from code farmers ' hands to the memory can run out of the program is no longer an excessive narrative, this article is only focused on the linker of this part of the story. As we can see, the accepted input of linker is a number of. o files, simply known as the machine code generated after the assembler compiled, called "relocatable object file (relocatable target files)", the concept of similar titles have "module". The output of the assembler is a binary file called "Executable object Program", which is characterized by the ability to copy directly into memory and run without any changes . So we can start by studying what the role of linker is--what does he need to do to enter several relocatable target files in order to construct the final target executable file?

There are two main functions of linker:

(1) Symbolic parsing (symbol resolution): the definition of each symbol and the reference to each symbol are linked together. (Let the system understand, when the program run, the specific variables or function names, they come from the definition of which file?) Is this yourself? Or the other files that you enter linker together? )

(2) Relocation (relocation): Link each symbol definition to a specific location in the memory, and then modify all references to these symbols so that they point to the location of the memory and reposition the sections. (After you have made a reference to each symbol and defined the connection, you want to bind the definition of the symbol to a specific address in the memory)

The interpretation of the symbols in the book is not too clear, at least I did not quite understand the concept at the outset, in this context, I use my own words to summarize my understanding of the concept. "Symbols" can be divided into 3 categories:

1, the "global symbol" defined by the module and can be referenced by other modules. The "global symbols" here correspond to non-static functions and global variables in the C language.

2. The "global symbol" referenced by the module is defined by the other module. Explanation Ibid.

3. "Global symbols" defined by the module and cannot be referenced by other modules. Corresponds to a static variable in the C language, that is the static variable. The static keyword is equivalent to "private" in the C language, which is a global variable that can only be used by its own file (module) .

It should be noted that the variables here are all global variables rather than private variables inside the function, and the private variables are managed by the runtime stack store, and linker is not interested in her:)

So after understanding the concept of the symbol, in order to understand the specific linker of the target executable file to do some things, you need to understand the Relocatable object file structure (how he recorded his various symbolic information?) Right

The first time you see this diagram do not be afraid, in fact, this is the assembler (assembler) The compiler to process the source code files for further compilation or after the compilation of the resulting relocatable target executable file . A small lattice of this file is a "section", they store the program's various information, here I will only explain a few I think the understanding of the role of linker is necessary to the sections.

. Text: The machine Code of the compiled program.

. Data: A global c variable that has been initialized.

. BSS: uninitialized global C variable. Here the symbol is just a placeholder, it does not occupy any memory space.

. Symtab: A symbol table that holds information about functions and all global variables that are defined and referenced in the program.

. Rel.test: The relocation entry (relocation entry) that holds the code.

. Rel.data: The relocation entry that holds the data.

All of the above are part of the basic knowledge of this chapter, understanding the above content, it is easy to understand the linker of the target executable files to do the operation.

1. Symbol parsing

The linker parsing notation is to associate a reference to each symbol with one of all the symbols defined in the. Symtab section of all input Relocatable object file.

 1.1 How does the linker interpret global symbols for multiple definitions?

For the definition and reference are in a module of the symbol, linker operation is very simple, do not need to refer to the change to change, and the real drill is to define and reference the symbol is not in the same file, especially when the found symbol definition has the same name. The linker approach to this is:

(1) Define the concept of strong and weak symbols. The function and the initialized variable are strongly signed, and the variable being initialized is a weak symbol.

(2) Set the rules. When there are multiple definition conflicts, the rules that linker follows are:

One: multiple strong symbol definitions are not allowed

One: If you have a strong symbol and multiple weak symbol definitions, select the strong symbol definition

Three: If there are multiple weak symbol definitions, then choose a

1.2 Linking to static libraries

Why is there a "static library" (libraries) concept?

First of all, in C programming, we need to implement a wide range of functions, and we will use a variety of function interfaces. In ANSI C, for example, it defines a broad set of standard I/O, string manipulation, and integer mathematical functions such as atoi, printf, scanf, strcpy, Rand. They are in the LIBC.A library and are available for every C program. Instead of using static libraries, let's look at what other programming developers can do to provide these functions to the user.

One way to do this is to have the compiler directly recognize the call to the function and generate the corresponding code directly-which is obviously not feasible, there are a lot of functions in C, which obviously makes the design of the compiler quite complex and requires a new version of the compiler each time a function is added, modified, or deleted. While this is convenient for programmers, all standard functions are directly available.

Another way to do this is to put all of these functions into a single relocatable target executable, which has the advantage of separating the design of the compiler from the implementation of the standard function and, to some extent, the convenience of the programmer. But the disadvantage of this is that each time you run the program, the ROF file of the load function is copied into memory, and this is a waste of memory space. And also will be such a large number of functions to a file, each maintenance to reproduce the compilation of the entire source file, this is a considerable amount of work.

What is a static library?

In Unix, a static library with archive this special file format exists on disk and is a set of relocatable object files that are connected together.

  

1.3 How the linker uses static libraries to parse references

  Maintenance of an algorithm based on (U,E,D) three sets

2. Re-positioning

  In this procedure, the module is merged and the runtime address is assigned to each symbol. Relocation consists of two steps:

Here is a concept that needs to be understood is the relocation entry (relocation entry). When the assembler generates a Relocatable target module, it generates a relocation entry for the symbol when it encounters a undefined symbol that does not know where the data or code ultimately resides in the memory. This is what is logged in the. Rel.text and. Rel.data Two tables in the relocatable destination file that you described earlier.

The role of the linker (linker)--csapp 7th Chapter Reading notes

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.