What are linkers and loaders in programming?

923 views

If you had to explain in the shortest and simplest way possible, how would you put it?

In: Engineering

4 Answers

Anonymous 0 Comments

When you start a process it will create its own memory space called a virtual memory. The CPU actually have a map to translate between virtual memory and physical memory for the current process. This means that the process have full range over the entire memory address space and can put things wherever it wants, it does not even have to worry about the physical space available. The reason this is done is because the code very often refers to a fixed place in memory and this only works when that fixed memory address actually contains what you expect. So an executable file contains a map over all the memory areas it needs to load, some memory needs to be the code or data from the file and some memory needs to be available but not populated. However when the compiler compiles the code it does not know any of the memory layout. So whenever it needs to write a static memory location it just use a temporary value and notes the location in a table with a label as to where it should eventually point to. When you finally merge the code from multiple compiled sources together you need to go through and set the final values everywhere. This is what the linker does as it links the code together. The job of loading the process memory space with the correct data is what the loader does. However even at loading time you might not have everything linked together correctly. This is because applications often share common libraries which are their own executable files except they do not have an entry point. So the loader also have to link together different code. Different operating systems does this differently as you do run the risk that different libraries claim the same address spaces which does not work as they have to run in the same process. Windows will go ahead and relink the libraries whenever this happens while Linux require all shared libraries avoid the use of static memory addresses and rather use dynamic linking which use relative memory locations and lookup tables so it can run at any memory address.

You are viewing 1 out of 4 answers, click here to view all answers.