Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

not clear with the job of the linker

I'm using C language on windows. This question was previously part of What happens to identifiers in a program? . I broke it to reduce no. of questions. This is a standalone query (doesn't depend on the previous question)

If there is nothing to link (i.e.. I'm not using any libraries. I know it wont be of any use.) will the linker change the object code output of assembler? If so what does it change?

I heard that LINKER also does the operation of some memory mapping. I don't understand how. The program is not running, its just in the manufacturing stage. How could linker map to memory? How would it look like? What all are the functions of LINKER?

When people refer to "relocation" , "address binding". I don't really get what they mean. What is it & what is its purpose?

Some debuggers show info like : call stack: 0xfffef32 , 0xf3234fe etc.. Its at the run time right? or is the the memory addresses of so called "memory mapping" of linker?

when people refer to something like symbols or symbol table. Do they mean identifiers(variable names, constant names, function names)?

I searched info on internet but couldn't find anything useful. May be I'm not sure what to search for. I don't want to read big books on this. But if there are any articles, tutorials which clear concepts. That would also be helpful.

I'm a novice programmer. So, it would be great you can explain in simple but technical terms.

like image 567
Alice Avatar asked Dec 31 '09 18:12

Alice


People also ask

What is the job of the linker?

Linker is a program in a system which helps to link object modules of a program into a single object file. It performs the process of linking. Linkers are also called as link editors. Linking is a process of collecting and maintaining piece of code and data into a single file.

What is a linker?

A utility program that connects a compiled or assembled program to a particular environment. Also known as a "link editor," the linker unites references between program modules and libraries of subroutines. Its output is a load module, which is executable code ready to run in the computer.

What is the role of linker in ARM processor?

The purpose of the ARM Linker is to combine the contents of one or more object files (the output of a compiler or assembler) with selected parts of one or more object libraries, to produce an executable program.

What is linker and its types?

The linker takes the input as the object code which would be generated by a compiler/assembler. The process of linking can be understood as a method to combine different snippets of code in order to obtain executable code. There are two types of linkers available: Linkage Editor and Dynamic Linker.


1 Answers

When you compile a source file, it is usually divided up by the compiler/assembler into several sections. As a hypothetical example imagine that the following sections are used:

  • .text - contains all the executable code
  • .const - contains constant data
  • .data - contains read/write initialized data
  • .bss - contains read/write uninitialized data

In a single source file, the compiler/assembler allocates the appropriate stuff to the appropriate sections and gives the symbols that are used offsets in the section starting from zero.

For example:

int i;
const j = 3;
int k = 4;
int l;
int main()
{
return 1;
}

This could result in the following symbol table:

Symbol Section Offset
i      .bss    0
j      .const  0
k      .data   0
l      .bss    4
main   .text   0

In the object file, in addition to the symbol table, the data in each section could be kept. In this example, the .text section would contain the object code for "return 1", the const section would contain 3, the data section would contain 4. The .bss section would not need to be in the object file, because the variables haven't been initialized.

The first thing a linker might do is to concatenate all the sections of the input object file and adjust the symbol offsets accordingly.

Now we get to what called "relocation" or "address binding". Let's say that in a hypothetical system, executable code starts at address 0x1000. Let's also say that the data sections of a program want to start at an even page boundary after the executable code. The linker would assign 0x1000 as the base of the concatenated .text sections and adjust all the symbols. Then the base of the .const, .data, and .bss sections similarly to place them in appropriate places in memory.

Sometimes there are symbolic references in a section. These references have to be updated by the linker to reflect the final position of the symbol referred to. The object file could contain "relocation records" that look like

section offset symbol
.text   0x1234 foo

The linker will go to each offset in each section and update the value there to reflect the final symbol value.

After all this is done, the resulting "absolute" object file can be loaded into memory (at the proper spot, of course!) and executed.

like image 94
Richard Pennington Avatar answered Dec 26 '22 21:12

Richard Pennington