In our previous video, we have found like linker performs two operations whereas uh symbol resolution,
the other is relocation.
In this video we will be looking into relocation.
Relocation is the process of changing addresses already assigned to the labels.
So whatever addresses which is already assigned to labels will be changed in the relocation process.
This will also involve patching up all label references to reflect the newly assigned addresses.
I think this is reflect.
So primarily the this primarily relocation is performed for the two following reasons.
One is section merging.
Section placement.
So what is the sections we need to understand first what is the section.
So for example code can be placed in read only memory and data can be placed in read write memory.
So it is good that if we do not mix the code and data right, which we are doing in the first videos,
first examples, we are mixing the code in the data.
We are everything were kept in the one read only memory which is the flash.
So.
So it will be convenient if code and data is not mixed.
So programs are divided into sections.
Most programs have at least two sections.
One is text for code section, dollar data for data section, and you have two assembler directives
which are used to switch back and forth between the two sections.
You have dollar text and dollar data.
So imagine each section as a bucket.
When assembler sees this particular directive, it puts the code or data following the directive in
the selected bucket.
So let's see an example.
How it looks is.
If you see this one.
I have two assembler directives, dollar data and dollar text.
So if anything, if assembler sees this as a uh, this directive, it will place this particular code
in one particular section.
Now it sees dollar text.
So it will place this in the text section.
Text section.
Now again it says data section.
So this will not be placed in the uh data section.
Text section.
It will be placed in the data section.
So you can use it I mean so you can switch back between the sections.
And if you see the code now as assembler sees this.
So as I uh, is keeping one by one, first it kept array and length and but the result is here right.
So the result is also added here result which was here and text section it is having first two and then
this remaining so and addresses are also different.
If the addresses are like this is also having zero, this is also having zero.
So what we here is we need to understand the concept of sections.
We can have two sections each and every program can have at least two sections.
One is to store the code.
The other is to store the data.
The section for storing the code is text section.
The section for storing the data is the data section.
Now.
Let's back.
So we are trying to understand the relocation and suction merging.
Okay.
So when dealing with multi file programs, sections with the same name appear in each file the text.
So linker is responsible for merging sections from the input files into sections of the output files.
So by default, sections with the same name from each file is placed contiguously, and the label differences
are patched to reflect the new values.
So how can I observe this section?
Merging.
I can look at the symbol table of the object files.
So let's see this again.
I mean we had also seen here uh, we have this uh, we'll copy this object files which we have main
dot o and some sub dot o.
Now we'll have used.
Main dot zero.
We have seen this one both are having.
Currently we only have the text segment.
We don't have data segment.
So if you see T stands for text segment.
And capital T is like it's a global variable.
So because it's a global it is capital T.
And this are the local variables local to the file.
It cannot be accessed outside from other assembly files.
This can be accessed because it is capital T.
Now if you see uh.
A loop symbol is having a truss for in this particular thing.
An error is having for now.
When I give this to an linker which performs the relocation, it will also it performs a section merging.
Let's see what will be the output.
I will say some dot elf main dot zero.
Some sub dot zero.
Now let's see the.
Elf thing.
Now if you see a RR was having for it is having four, but EOA was having seven.
It is having seven.
Start was having eight, start is having eight, stop is having 14, stop is having 14.
Here some was having undefined and some was having zero.
Now some got 20 and loop is having.
24.
So if you see all this got added into one and the addresses have been changed, I mean this addresses
has because we gave main dot first, this addresses did not change.
But uh some sub zero addresses have been changed.
If you see the loop address is changed from 4 to 24 and some here the undefined is all.
There is no undefined.
Now this is an undefined start.
It's okay.
Uh, but there is no undefined for some here.
Some got an address of 20.
This is what I was saying.
Like 20 is like.
Here, I got 20.
It's okay.
So we have merged the two text sections because in the code we only had uh.
Uh, we had no data section, we only had text section.
And the responsibility of linker is section merging.
Section merging in relocation it will do two operations, uh which is section merging.
So linker performs uh symbol resolution like undefined symbol is resolved.
And it also performs section merging where it will merge all the sections.
Currently it has only merged the code section.
It will also merge sections of the same name.
If you have dot text, it will merge all files dot text into the final Elf file and if you have download
data, you will merge all the download data sections into the final Elf file.