In-depth understanding of computer system architecture--Links

Source: Internet
Author: User

A link is the process of collecting and combining a wide variety of code and data departments into a single file that can be loaded (or copied) into memory and executed. Links can be executed at compile time, that is, when the source code is translated into machine code, or when the facilitates is executed, that is, when the program is loaded by the loader and executed, or even executed at run time by the application.

1 Compiler Driver

Consider the following two source files: Main.c and swap.c.

Main.c

void swap (); int buf[2]={1,2}; int Main () {    swap ();     return 0 ;}

Swap.c

extern int buf[]; int *bufp0=buf; int *bufp1; void swap () {    int  temp;        BUFP1=&buf[1];    Temp=*bufp0;     *bufp0=*bufp1;     *bufp1=temp;}

Most compiled systems provide a compiler driver, which represents the language preprocessor, compiler, assembler, and linker that the user invokes when needed.

For example:

Gcc-02-g--o P MAIN.C swap.c

The driver first calls the C preprocessor (CPP), which translates the C source program main.c into an intermediate file main.i:

CPP [Other arguments] main.c/tmp/main.i

Next, the driver runs the C compiler (CC1), which translates main.i into an assembly-language file Main.s

Cc1/tmp/main.i main.c-02 [Other arguments]-o/tmp/main.s

The driver assembler (AS) then translates the main.s into a relocatable target file main.o:

As [other arguments]-o/tmp/main.o/tmp/main.s

The driver generates SWAP.O through the same process. Finally, it runs the linker program LD, which combines MAIN.O and SWAP.O with some of the necessary system target files to create an executable target file P:

Ld-o p [System object files and args]/tmp/main.o/tmp/swap.o

To run the executable p, we just need to enter./P

The shell invokes a function called the loader in the operating system, which copies the code and data from the executable p to the memory, and then transfers the control to the beginning of the program.

2 static links

In-depth understanding of computer system architecture--Links

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.