Link, chain home

Source: Internet
Author: User

Link, chain home

When compiling a c or c ++ program, the gcc command is always used. Gcc actually calls the pre-compiler ccl, assembler as, and linker ld based on different parameters. The pre-compiler ccl compiles source code into assembly code. The assembler as converts the assembly code into machine commands to generate the target file. The linker ld connects the target file to an executable file.

 

The assembler as has converted the assembly code into executable machine commands. Why is the connection required? When developing a project, many people may collaborate, and many functions may be independent modules. The general practice is to separate independent modules, compile them independently, and then assemble different modules. In this case, you need a tool that can assemble code from different modules into a complete executable file. This tool is the linker ld, and the assembly process is the link.

 

Before learning about the connection, let's take a look at the structure of the target file to see what is in the target file and how it is organized. The target file is a binary file that is organized in segments. At the beginning of the target file, it is a fixed-length file header, which records the basic information of the file, including the entry address of the file, the location of the field table, and the length of the field table. You can find the field table through the file header, and find all the segments in the file through the field table to traverse the entire file. The main segments in the file include code segments and data segments. The sub-tables store the execution commands and data of the file.

File Header

Field table

Code segment

Data Segment

Symbol table

 

There is a table named symbol in the target file. the symbol table records the global symbols defined in the target file and the global symbols referenced in the target file. Global symbols refer to global functions or global variables. During the link process, you can find all the global symbols that are not in this module through the symbol table, and organize different modules through the Global symbols.

 

Static connections are a waste of computer memory and disk space. Almost every program uses public library functions such as printf () and scanf (). How many processes are running means how many copies of printf () and scanf () the code is stored in the memory. The Code of printf () and scanf () is stored in the hard disk, which leads to a waste of system resources. Static links cause a lot of trouble to update, deploy, and release programs. Once any module is updated in the program, the whole program needs to be re-linked and then published to the user.

 

To solve the problems of space waste and updating difficulties, the simplest way is to separate the modules of the program and link them only when the program runs. This is the basic idea of dynamic link.

 

In a dynamic link, the linker ld is mapped to the address space of the process like a common shared object. When the system starts running the program, the system first gives the control to the linker ld, after all the links are completed, the control is handed over to the process.

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.