'Questions regarding address binding

There are three different address binding techniques :

Compile time binding Load time binding Run time binding

I have many questions regarding them : 1- According to my understanding, every OS uses a certain address binding technique to implement. Modern OSes uses run time binding and MS-DOS uses Compile time binding. Is that right ? Or the programmer can select which address binding to use ?

2- At compile time binding is used if the compiler knows in advance where the program will resides in main memory. How do the compiler know this future information ? Is it given by the programmer ?

3- Run time binding is used if the program will always change his location during execution time . Is for example swapping and segmentation compaction are examples why the programs will change their location during execution , or they are different concepts here ?



Solution 1:[1]

1- According to my understanding, every OS uses a certain address binding technique to implement. Modern OSes uses run time binding and MS-DOS uses Compile time binding. Is that right ? Or the programmer can select which address binding to use ?

Actually, even the most recent OS uses all the binding/linking types. It isn't really a matter of the OS but mostly a matter of user mode implementation. It is mostly the programmer which will decide which linking type to use but it isn't an explicit choice. It is chosen by the programmer implicitly by the way it programs in high level code. For example, if the programmer decides to use virtual functions in C++ then the address to jump to will be determined at runtime based on the type of the object referenced instead of the type of the reference. It is useful/recommended to know about linking and how it works to write more efficient code and to know what you are doing.

2- At compile time binding is used if the compiler knows in advance where the program will resides in main memory. How do the compiler know this future information ? Is it given by the programmer ?

It is quite complex and there is a lot to say about that. Today, you have paging that is always in use. A program has access to the whole address space. This means that a compiler doesn't have to care much about the position of an executable. It cares mostly about the start address and the rest is position independent or absolute. Position independent code doesn't even care all that much about the start address. The start address mostly provides a position to find the _start symbol but that's it. The rest of the code is using relative jumps to some position in the text segment and RIP-relative addressing to access global/static data.

It isn't that much about where the program will reside. It is a matter of if the function is present and implemented within your own code or in another library. If a function is present in another library, then the compiler will leave an unresolved symbol in the executable for the linker to resolve before execution. This is called dynamic linking an example of load time binding. The dynamic linker will find that dynamic library and place it somewhere in RAM. The dynamic linker will resolve where to jump to get to that function.

3- Run time binding is used if the program will always change his location during execution time . Is for example swapping and segmentation compaction are examples why the programs will change their location during execution , or they are different concepts here ?

There is no "program that will always change his location". Run-time binding is mostly present in C++ and is only a matter of virtual functions. All linking is done at compile time or at load time except for virtual functions. Virtual functions allow to call a function in an object by determining the type of the object referenced instead of using the type of the reference itself. This is used to provide polymorphism because the same function signature can have several implementations and which one to call will be determined based on the type of the object referenced. (For more info see: https://cs.stackexchange.com/questions/145444/what-attributes-are-bound-statically-vs-dynamically/145470#145470)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user123