I was thinking that I understand the working the location counter in linker script but I guess it's not so. I just did a simple test to confirm what I understand. I wrote a simple c program without any library calls and compiled it with gcc. Then I linked it using a linker script with the location counter set to value in the beginning. Here is the program
int a = 6;
int main(){
return 0;
}
Following is the linker script
ENTRY(main)
addr = 0x8048000;
SECTIONS
{
.text addr :
ALIGN(0x1000)
{
*(text*);
*(.rodata*);
}
.data :
ALIGN(0x1000)
{
*(.data*);
}
}
I didn't want to execute it but just see the objdump output. I was thinking that when I do objdump -s
on the elf it should show the starting address as 0x8048000. However I always see the starting address as 0000
Contents of section .text:
0000 b8000000 00c3 ......
Contents of section .data:
1000 06000000 ....
Contents of section .comment:
0000 4743433a 20285562 756e7475 20342e34 GCC: (Ubuntu 4.4
0010 2e332d34 7562756e 74753529 20342e34 .3-4ubuntu5) 4.4
0020 2e3300
Besides there is a comment section which also starts from 0000. I don't understand what is going on.
Here is the output of objdump without the linker script (still without any libraries)
Contents of section .text:
8048094 b8000000 00c3 ......
Contents of section .data:
804909c 06000000 ....
Contents of section .comment:
0000 4743433a 20285562 756e7475 20342e34 GCC: (Ubuntu 4.4
0010 2e332d34 7562756e 74753529 20342e34 .3-4ubuntu5) 4.4
0020 2e3300