3
votes

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  
1
What do you get without linker scripts?Alexey Frunze

1 Answers

1
votes

What does your map file look like? How are you building it? I tried reproducing your problem and I believe it all worked as expected:

x:~/temp> cat test.c && cat test.x && gcc test.c -Wl,-T,./test.x -nostdlib -o test.exe && readelf -S test.exe | grep .text && objdump -s test.exe
int a = 6;
int main(){
return 0;
}

ENTRY(main)

addr = 0x8048000;

SECTIONS
{
 .text addr :
 ALIGN(0x1000)
 {
  *(text*);
  *(.rodata*);

 }

 .data :
 ALIGN(0x1000)
 {
   *(.data*);
 } 
}

  [ 2] .text             PROGBITS        08048000 001000 00000a 00  AX  0   0 4096

test.exe:     file format elf32-i386

Contents of section .data:
 8049000 06000000                             ....            
Contents of section .text:
 8048000 5589e5b8 00000000 5dc3               U.......].      
Contents of section .comment:
 0000 00474343 3a202847 4e552920 342e352e  .GCC: (GNU) 4.5.
 0010 3100                                 1.              

Oh, and the .comment section is just inserted by the compiler.