* 13. deep understanding of computer system notes: Program links (2)

Source: Internet
Author: User

1, Executable target file

Note:Elf(Executable and linkable format)

We can see that,CodeThe segment address is always smaller than the segment address.

2Load the executable target file

AnyUNIXProgramYou can callExecveFunction to call the loader. The loader copies the code and data in the executable target file from the disk to the memory, and then jumps to1Command, that is, the entry point (Entry Point) To run the program. The process of copying a program to the memory and running is called loading (Loading).

Each UNIX Each program has a runtime memory image. In Linux In the system, the code segment is always from the address Zero X 08048000 . The data segment is in the next 4 kb Alignment address. Heap in the next read / The first one after the write segment 4 kb Alignment address, and by calling Malloc Database growth. Slave Zero X 4000000 The segment is reserved for the shared library. User stack always FROM address 0 xbfffffff And grow down (the High address increases to the low address ). Starting from the top of the stack at the address 0xc0000000 The segment is reserved for the code and data of the part of the memory where the Operating System resides (that is, the kernel.

3Dynamic Link shared library

A shared library is a target module that can be loaded to any local memory address during runtime and linked to a program in the memory. This process is called Dynamic Link (Dynamic LinkingIs called a dynamic linker (Dynamic linker.

A Shared Library is also called a shared object (Shared object), InUNIXCommon Systems. SoSuffix. (InMS OSIsDLLFile)

Note: The difference between static links and Dynamic Links: static links are used to copy and embed the library code and data required by the program into the executable files that reference them; dynamic Links are shared by all executable files that reference the library.. So (DLL)File Code and data.

4Load and link shared libraries from applications

Through several functions,DlopenLoad and link shared libraries,DlsymReturn the symbol address by entering the symbol name of the shared library;DlcloseDetach a shared library,DlerrorReturns the execution of the previous function.

Sample Code

 
# Include <stdio. h> # include <dlfcn. h> int X [2] = {1, 2}; int y [2] = {3, 4}; int Z [2]; int main () {void * handle; void (* addvec) (int *, INT); char * error; * dynamically load the shared library that contains addvec () */handle = dlopen (". /libvector. so ", rtld_lazy); If (! Handle) {fprintf (stderr, "% s \ n", dlerror (); exit (1) ;}/ * Get a pointer to the addvec () function we just loaded */addvec = dlsym (handle, "addvec"); If (error = dlerror ())! = NULL) {fprintf (stderr, "% s \ n", error); exit (1);}/* now we can call addvec () it just like any other function */addvec (X, Y, Z, 2); printf ("z = [% d] \ n", Z [0], Z [1]);/* unload the shared library */If (dlclose (handle) <0) {fprintf (stderr, "% s \ n", dlerror ()); exit (1);} return 0 ;}

5,PIC(Location-independent code,Position-independent code)

The primary purpose of a shared library is to allow multiple running processes to share the same library code in the memory, thus saving memory resources.

PIC: Compile the library code. You can load and execute the code at any address without modifying the library code.

InIa32In the system, calls to the process in the same target module do not need special processing, because the reference isPCRelated, known offset, isPIC. However, the process calls to external definitions and references to global variables are usually notPICBecause they all require relocation during the link.

How to generate global variablesPICWhat about references? Based on the fact that no matter where we load a target module (including a shared module) in the memory, the data segment is always allocated to be followed by the code segment. Therefore, the distance between any command in the code segment and any variable in the data segment is a runtime constant.

For more information, see7.12Section.

6Tools used to process the target file

GNU binutilsPackage. For exampleObjdump, AR, LDD.

7The link can be completed by the static compiler during compilation, or by the dynamic compiler for loading and running. The linker processes binary files called target files in three different forms: relocable, executable, and shared. A relocated target file is combined by a static linker into an executable target file, which can be loaded into the memory and executed. The shared target file (shared library) is linked and loaded by the dynamic linker at runtime, or implicitly called in the program when the calling program is loaded and started, or as neededDlopenLibrary function.

The main tasks of linkers areSymbol resolution, Where each global symbol is

Bound to a unique definition, and relocation, where the ultimate(Final)Memory

Address for each symbol is determined and where references to those objects are

Modified.

Computer Systems: A programmer's perspective >

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.