GNU ARM汇编--(十三)GNU ARM汇编下的linker script
ldr指令:
LDR load word into a register Rd <- mem32[address]
ldr伪指令:
LDR Rd, =constant
LDR load constant pseudoinstruction Rd=32-bit constant
adr伪指令:
ADR Rd, label
ADR load address pseudoinstruction Rd=32-bit relative address
我们的程序从nandflash启动,运行在s3c2440的4K大小的SRAM中,linker script的.text放在. = 0x00000000;处.
测试代码如下:
[cpp]view plaincopy
- ldrpc,_main@a
- ldrpc,=_main@b
- ldrpc,main@c
- ldrpc,=main@d
- adrpc,_main@e
- adrpc,main@e
- _main:.wordmain
main中放置的一个流水灯.分别测试这六种情况:
a.成功跳转,分析下反汇编:
[cpp]view plaincopy
- 128:e51ff004ldrpc,[pc,#-4];12c<_main>
- 0000012c<_main>:
- 12c:000002d4.word0x000002d4
- ......
- 000002d4
:
b.无法跳转,分析下反汇编:
[cpp]view plaincopy
- 128:e59ff244ldrpc,[pc,#580];374
- 0000012c<_main>:
- 12c:000002d4.word0x000002d4
- ......
- 000002d4
:
c.无法跳转,分析下反汇编:
[cpp]view plaincopy
- 128:e59ff1a4ldrpc,[pc,#420];2d4
- 0000012c<_main>:
- 12c:000002d4.word0x000002d4
- ......
- 000002d4
:
d.成功跳转,分析下反汇编:
[cpp]view plaincopy
- 128:e59ff244ldrpc,[pc,#580];374
- 0000012c<_main>:
- 12c:000002d4.word0x000002d4
- ......
- 000002d4
: - ......
- 374:000002d4.word0x000002d4
e.无法跳转,分析下反汇编:
[cpp]view plaincopy
- 128:e24ff004subpc,pc,#4;0x4
- 000012c<_main>:
- 12c:000002d4.word0x000002d4
f.成功跳转,分下下反汇编:
[cpp]view plaincopy
- 128:e28fff69addpc,pc,#420;0x1a4
- 0000012c<_main>:
- 12c:000002d4.word0x000002d4
- ......
- 000002d4
:
pc=pc+420+8=0x00000128+420+8=0x00002d4 所以成功跳转.
完全理解相对跳转和绝对跳转是为了后面的linker script做准备的,linker script的理论只是可以看下gnu.org的官方文档,下面才开始这次的正题.
给出两种linker script的写法:
1.
[cpp]view plaincopy
- OUTPUT_FORMAT("elf32-littlearm","elf32-littlearm","elf32-littlearm")
- OUTPUT_ARCH(arm)
- ENTRY(_start)
- SECTIONS{
- .=0x30000000;
- .textALIGN(4):{*(.text)}
- .rodataALIGN(4):{*(.rodata)}
- .dataALIGN(4):{*(.data)}
- .bssALIGN(4):{*(.bss)*(COMMON)}
- }
vma给的是0x30000000,那么跳转就这么跳:
[cpp]view plaincopy
- ldrpc,=on_sdram
- n_sdram:
- blclearsram
为了证实后面跑的代码是sdram中的,在跳到sdram后将sram都清除了.
2.
[cpp]view plaincopy
- OUTPUT_FORMAT("elf32-littlearm","elf32-littlearm","elf32-littlearm")
- OUTPUT_ARCH(arm)
- ENTRY(_start)
- SECTIONS{
- .=0x00000000;
- .textALIGN(4):{*(.text)}
- .rodataALIGN(4):{*(.rodata)}
- .dataALIGN(4):{*(.data)}
- .bssALIGN(4):{*(.bss)*(COMMON)}
- }
[cpp]view plaincopy
vma的地址用的是0x00000000,跳转这样写:
[cpp]view plaincopy
- ldrr0,=on_sdram
- addr0,r0,#0x30000000
- movpc,r0
- n_sdram:
- blclearsram
通过两种链接脚本的对比,这个vma lma,绝对跳转的概念那应该就很清楚了.太晚了,明天给出一个有意思的链接脚本.
今天晚上有时间,补充一个有点意思的链接脚本:
[cpp]view plaincopy
- OUTPUT_FORMAT("elf32-littlearm","elf32-littlearm","elf32-littlearm")
- OUTPUT_ARCH(arm)
- ENTRY(_start)
- SECTIONS{
- .text10x00000000:
- {
- start.o(.text)
- }
- .text20x30000000:AT(2048)
- {
- main.o(.text)
- }
- }
编译出来的二进制文件会大很多,因为.text2段的LMA地址是2048.
bin档大小为2184
arm-linux-readelf -a sram2sdram_elf,结果是:
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
LOAD 0x008000 0x00000000 0x00000000 0x003f0 0x003f0 R E 0x8000
LOAD 0x010000 0x30000000 0x00000800 0x00088 0x00088 R E 0x8000
Section to Segment mapping:
Segment Sections...
00 .text1
01 .text2
ARM汇编linkerscrip 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)