Understand Dynamic Links (3)-global variables of shared modules and dynamic global variables

Source: Internet
Author: User

Understand Dynamic Links (3)-global variables of shared modules and dynamic global variables

Suppose that the global variable defined in a shared module is referenced in module. c:

1 extern int global; 2 3 int foo () {4 global = 1; 5}

The compiler cannot determine whether the global variable is defined inside or outside the module. Assume that module. c is a source file of the executable file. The executable program is not PIC and will not be relocated. The linker creates a copy of the global variable in the. bss segment, which causes the same variable to exist in multiple locations at the same time. The solution is to direct all access to the global variable to the copy in the executable file.

When the ELF shared library is compiled, all global variables are defined in other modules by default, and external access is implemented through the GOT table. When a shared module is loaded, if a global variable has a copy in the executable file, the dynamic linker points the corresponding address in the GOT table to the copy. If the variable does not have a copy in the executable file, the corresponding address in the GOT table points to the copy of the variable inside the module.

Assume that libx. so defines A global variable G. Both process A and process B use libx. so. When libx. when so is loaded by two processes, its data segment has an independent copy in each process. Therefore, process A and process B access A copy of the global variable G in their own processes, they do not affect each other. But if it is the same process thread A and B, they access the same copy.

Sometimes we want different threads in the same process to access different copies of global variables, which can avoid interference between threads on global variables or avoid thread synchronization. This can be achieved through Thread Local Storage (TLS. In the Android system, TLS is implemented by using the coprocessor. You can see the TLS code in the implementation of functions such as Linker and getpid.

Sometimes you want multiple processes to share copies of the same global variable to implement inter-process communication. Remember that when I wrote Windows DLL, the concept of "sharing data segments" was implemented.

Learning Materials: programmer self-cultivation-links, loading and libraries

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.