我在想我理解在链接器脚本中使用位置计数器,但我想它并非如此。我刚做了一个简单的测试来确认我的理解。我写了一个简单的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
答案 0 :(得分: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部分刚刚被编译器插入。