gcc static link

Source: Internet
Author: User

A link is the process of connecting multiple . o files into an executable file. The final part of the link is the spatial address allocation and symbolic parsing and repositioning.

First of all, the Space address assignment: Scan all the input target files, get their length, properties and location of each segment, and further collect all the symbol definitions and symbol references in the symbol table of the input target file, and put them into a global symbol list. In this step, the connector can get the lengths of all the input segments, combine them, calculate the length and position of the merges, and establish a mapping relationship.

Symbolic parsing and relocation: Use all the information gathered in the first step above, read short data in the input file, reposition the information, and parse and reposition the symbols, adjust the address in the code, and so on. The second step, in fact, is the core of the link process. Especially the relocation process.

L inux use ld a.o b.o-e main-o ab to link.

There is no virtual address in the . O file, the virtual address is generated after the link, and the symbolic address is determined. For BSS segments in an executable file, there is no space occupied in the elf file, but the BSS data segment is allocated virtual memory space during the link.

Here are a few important concepts in static linking:

Reposition table: Any undefined symbol in the . O file corresponds to an item in the relocation table, which is represented by the elf32-rel data structure, which includes the relocation entry offset ( The offset of the repositioned symbol relative to the segment ) and its corresponding symbol subscript in the symbol table.

COMMON BLOCK: We all know that the symbol type is transparent to the linker, that is, the linker does not know the type of the symbol. The problem occurs when more than one symbol type with the same name is inconsistent. A COMMON block mechanism is set up for this purpose , and when more than one weak symbol type with the same name is inconsistent, the one occupying the larger space will prevail. Strong symbols prevail when there is a strong sign and weak symbol type inconsistency, but a warning is issued when the weak symbol size is greater than the strong symbol.

Duplicate code removal issues: In the case of C + + template functions, a compilation unit may produce add<int> and add<double> type of code, which is also present in another compilation unit add<int> code, this time the linker will find and remove the duplicate code.

Function-level links, for some libraries, its unusually large, in the use of one of the functions of the entire library is included, in order to solve this problem can be used to generate a separate segment of the function, in the link process contains only useful parts, reduce the size of the executable file.

gcc static link

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.