Comments (2)
Sorry to have missed this issue for a long time, let me make this a bit more clear.
-
If any variable is global to the process (is used by multiple objects), it has to be accessed through GOT. The reason is that the dynamic loader will select only one of its copies so that when one object modifies it, the other objects will see the change. It is important if the said variable is modified.
-
Which object will host the variable in run time depends on which libraries are loaded and in which order. The libraries and their dependencies are first loaded in depth-first-order (for each library, its dependencies are loaded before itself), then the dynamic linker performs symbol resolution.
For each GOT (unique per object) the dynamic linker crawls the libraries in a breadth-first-order. It is building the 'global lookup scope' this way. The first object containing the said variable will be its host. The GOT will be
- An optimization occurs: If a variable is declared in both shared library AND main executable file, we are going to select the instance in the main executable file. It is happening for two reasons:
- The executable file is always first in the global lookup scope, so we can guarantee that this is the good place to store the variable.
- This allows to bypass GOT usage and use absolute addresses inside the executable when addressing the said variable AND the executable is non-PIC. It is faster (GOT introduces overhead) and, in this context, it is safe. But in the dynamic library compiled with PIC enabled, you should always use GOT to address global-global variables because you can never know, where does this variable belong in run time.
You can check the said facts by declaring a variable in both executable file and a shared library. Then print its address and hang. Something like:
Library:
int g = 10;
void f(void) {
printf("%p\n", &g);
}
Executable:
int g;
int main(void) {
g++;
f();
while (1);
return 0;
}
Then compile it, link and launch in background. Use /proc/<PID>/maps
file to check where does this address belong: you are going to see that it belongs to the executable's .data section; moreover, in the disassembly, you are going to see that during the increment this variable is going to be addressed by its absolute address rather than through GOT.
from low-level-programming.
Thanks for your detailed explanation.
from low-level-programming.
Related Issues (20)
- Debian Virtual Machine: git clone: could not resolve host github.com HOT 2
- Question / Answers: Combined HOT 1
- Question 44 typo HOT 2
- SSH into VM not working HOT 2
- typo in errata HOT 2
- error when run on Mac HOT 1
- VirtualBox display resolution HOT 3
- Q34 HOT 1
- Q39 HOT 1
- Q40 HOT 1
- Q70 HOT 1
- Assignment: Dictionary
- Section 1.3.1 HOT 2
- Docker setup HOT 3
- Missing answers to questions HOT 1
- There is no need in having `pyc` files present in the repository
- Python scripts compatibility
- Virtual Machine
- Question 22 HOT 2
- Nasm can't create output file
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from low-level-programming.