Links to "in-depth understanding of computer system 03" programs

Source: Internet
Author: User

Article Three: links to Programs

The origin of the > linker

After the advent of high-level languages, multiple people need to develop different modules.

Link steps:

1) determining symbolic reference relationships

Determining the definition of a symbol

2) merging related. o files

Same address space, schedule virtual address space

3) Determine the address of each symbol

4) Enter the new address in the instruction

Benefits of using Links:

1. Modular

2. Improve compilation efficiency and save memory space (reuse of shared libraries)

Only the modified source program files need to be recompiled

> Destination file format

Classification:

1. The target file can be relocated:. o file, address and data starting from 0

2. Executable target file: can be copied directly into memory execution. Address mapping has occurred here and the program can be accessed.

3.. So in the shared target file Linux, can be dynamically loaded into memory. Shared library files under Windows. dll

Focus: Differentiate between relocatable target files and executable target files, ELF

Take Linux as an example

Each module has code and data (initialization/Uninitialized global variables, static variables, local variables)

Gcc-o2-g

Linking process essence: Merging the same sections

The different relocatable objects in the target file will be

. text

. Data

. BSS

Classify the merge to get the executable file.

Executable file storage image

Merge to virtual address space (not memory)

Format of the destination file:

Keywords:

Target Code

Target file

. BSS uninitialized global variables and local static variables

. Data already initialized global variables and local static variables

A section is the smallest unit of processing that has the same characteristics in an elf file.

From two points of view:

1. Linked View--relocatable target file

ELF

The. BSS section does not occupy disk space and does not have to record the initial value, declaring only the occupied memory space

ELF Head:

Divided into 64-bit and 32-bit editions

Header information: The first four bytes is the magic number

Read Mode readelf-h MAIN.O

ELF Section Head table:

Records the starting position, size, and alignment of each section.

32-bit table entry each size is 40B

There are four sections that allocate storage space :

. text

. Data

. BSS

. Rodata (Read only data)

Alignment : Different alignment will have different results for the allocation of memory address arrangement.

2016-09-14

2. Elf execution View--executable file format

Focus: Elf Head program Header table: Expressing the relationship between section and segment

Memory mappings for executable files:

> third section symbol table and symbol parsing

Use an experiment to understand symbolic parsing:

When an executable is generated, symbolic parsing is required, and a symbol table is needed to determine all the symbols that are defined in the program module.

>> symbol Table:

There are three types of symbols:

1. Global symbol: The name of a non-static global variable that is referenced by another module.

2. External symbols: external function names and external variable names defined in other modules.

3. Local symbol: static function and global variable name.

>> symbol parsing:

1. Global symbol Strength Type

The initialized global variable name is strongly signed, and the uninitialized global variable name is a weak symbol.

Rules:

1. Strong symbolic duplicate definition links will conflict.

2. Strong definition encountered weak definition, obey strong symbol definition

3. If you have more than one symbol definition, select either.

Experiment One:

Main.c

#include <stdio.h>

int x=10;

void P1 (void);

int main ()

{

P1 ();

printf ("x=%d \ n", x);

return 0;

}

TEST.c

int x;

void P1 ()

{

x=200;

}

> Gcc-o Test MAIN.O TEST.O

>./test

x=200

Explanation: The x address in the P1 module equals the address of the X in the main module, that is, the two are treated as the same variable.

Experiment Two:

Main.c

#include <stdio.h>

int x=10;

void P1 (void);

int main ()

{

char a = ' a ';

P1 ();

printf ("x=%d \ n", x);

printf ("x size is%d\n", sizeof (x));

printf ("\ ' x\ ' ASCII =%d \ n", ' X ');

printf ("Size of char is%d\n", sizeof (a));

return 0;

}

TEST.c

char x;

void P1 ()

{

x = ' x ';

}

$ gcc-o Test MAIN.O TEST.O

$./test

x=120

X size is 4

' x ' ASCII = 120

Size of char is 1

Explanation: Strong and weak to see.

Other than that:

printf ("%d", sizeof (' a '));

The output here is 4, because ' a ' will first be converted to the corresponding ASCII 96, and as an int, the size becomes 4. You can declare a variable char,sizeof (char) or 1 first;

>> links to Static libraries

The resulting executable can be loaded directly into the memory execution without the need to dynamically link other modules at load or run time.

> Relocation

Relocation: Merges all associated target modules on the basis of symbolic resolution and determines the address of each defined symbol in the virtual address space at run time, repositioning the referenced address at the reference to the defined symbol.

Section and define the relocation of symbols, as well as the reference to the symbol, to trace.

Relocation information: see. rel.text section and. Rel.data section

Relocation process: Slightly

> Executable file loading

Call up the loader, according to the Program Header table information in the executable target file, map the contents of the relevant section in the executable target file with the read-only code snippet and read-write data segment in the virtual address space through the page table, and then start the execution of the first instruction in the executable target file.

Continuous read-only code snippets and read-write data segments, which makes it easy for the loader to page and initialize contiguous areas.

At load time, the read-only code snippet and the writable data segment corresponding to the page table are initialized to a page that is not cached (involving the operating system and the principle of composition), pointing to somewhere in the destination file on disk. So, during the program loading process, there is no real load of code and data from the disk into main memory , only the corresponding page table entries are created. The code and data are not loaded from the disk until a fault occurs during code execution.

Key technologies: Virtual Storage Management

> Dynamic Links

Shared library, "sharing" "dynamic"

Loader: One Operating system

Dynamic linker

Summary

Two ways of linking:

Static links

Dynamic links

The three target file formats involved in the link are:

1. Can relocate the target file

2. Executable Target file

3. Share the destination file

Two elf target file formats:

1. Linked view: Can relocate the target file

2. Execution view: Executable target file format

Work in the linking process:

1. Symbolic parsing: Associating a symbol reference with a definition.

2. Relocation: Locate the location in the virtual address space to determine the final storage address for each symbol.

Here are the main tools to use:

OBJDUMP

Readelf–elf Show Relocation

Look again at the link essence:

Links to "in-depth understanding of computer system 03" programs

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.