3

我在想我了解链接器脚本中位置计数器的工作原理,但我想并非如此。我只是做了一个简单的测试来确认我的理解。我写了一个没有任何库调用的简单 c 程序,并用 gcc 编译它。然后我使用链接器脚本将其链接起来,并在开始时将位置计数器设置为值。这是程序

int a = 6;
int main(){
return 0;
}

以下是链接描述文件

ENTRY(main)

addr = 0x8048000;

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

 }

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

我不想执行它,只是查看 objdump 输出。我在想,当我objdump -s在精灵上做时,它应该将起始地址显示为 0x8048000。但是我总是看到起始地址为 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   

此外还有一个评论部分也是从 0000 开始的。我不明白发生了什么。

这是没有链接描述文件的 objdump 的输出(仍然没有任何库)

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  
4

1 回答 1

1

你的地图文件是什么样的?你是如何建造它的?我尝试重现您的问题,我相信这一切都按预期工作:

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.              

哦,编译器刚刚插入了 .comment 部分。

于 2012-05-22T11:44:44.277 回答