最后是Makefile
all: sdram.elf
sdram.elf :
arm-linux-gcc -c -g -O2 -o head.o head.s
arm-linux-g++ -c -g -O2 -o sdram.o sdram.cpp
arm-linux-ld -Ttext 0x30000000 -o sdram.elf head.o sdram.o
arm-linux-objcopy -O binary -S sdram.elf sdram.bin
arm-linux-objdump -D -m arm sdram.elf > sdram.dis
clean:
rm -f *.o *.elf *.dis *.bin
注意其中 -Ttext 0x30000000
这些解释有些费劲,配合 sdram.dis 看看就好说了(节)
sdram.elf: file format elf32-littlearm
Disassembly of section .text:
30000000 <_start>:
30000000: eb000005 bl 3000001c <kill_dog>
30000004: eb000008 bl 3000002c <control_mem>
30000008: eb000027 bl 300000ac <copy2sdram>
3000000c: e59ff0b8 ldr pc, [pc, #184] ; 300000cc <loop+0x14>
30000010 <sdram>:
30000010: e3a0d30d mov sp, #872415232 ; 0x34000000
30000014: eb000105 bl 30000430 <main>
30000018 <_end>:
30000018: eafffffe b 30000018 <_end>
......
300000ac <copy2sdram>:
300000ac: e3a00000 mov r0, #0 ; 0x0
300000b0: e3a01203 mov r1, #805306368 ; 0x30000000
300000b4: e3a02a01 mov r2, #4096 ; 0x1000
300000b8 <loop>:
300000b8: e4903004 ldr r3, [r0], #4
300000bc: e4813004 str r3, [r1], #4
300000c0: e1500002 cmp r0, r2
300000c4: 1afffffb bne 300000b8 <loop>
300000c8: e1a0f00e mov pc, lr
300000cc: 30000010 .word 0x30000010
300000d0: 22111112 .word 0x22111112
300000d4: 00018009 .word 0x00018009
300000d8: 008e04eb .word 0x008e04eb
......
30000430 <main>:
30000430: e24dd008 sub sp, sp, #8 ; 0x8
30000434: e3a03000 mov r3, #0 ; 0x0
30000438: e58d3004 str r3, [sp, #4]
3000043c: e59d2004 ldr r2, [sp, #4]
30000440: e3a00c27 mov r0, #9984 ; 0x2700
30000444: e280000f add r0, r0, #15 ; 0xf
30000448: e3a01456 mov r1, #1442840576 ; 0x56000000
3000044c: e1520000 cmp r2, r0
30000450: e2433021 sub r3, r3, #33 ; 0x21
30000454: e3a02b55 mov r2, #87040 ; 0x15400
30000458: e5813014 str r3, [r1, #20]
3000045c: e5812010 str r2, [r1, #16]
30000460: ca000006 bgt 30000480 <main+0x50>
_start 开始位置为何是 30000000?因为 -Ttext 0x30000000。再看 ldr pc, =sdram 被翻译成什么了?
3000000c: e59ff0b8 ldr pc, [pc, #184] ; 300000cc <loop+0x14>
而 300000cc 处是什么呢?
300000cc: 30000010 .word 0x30000010
这 意味着 pc 将被赋值 0x30000010, 这显然是编译器计算出的位置相关代码。而之前都是位置无关代码,虽然他们期望的运行基址是0x30000000,但因为位置无关,放在哪里也都行了,这正 是从flash 拷贝到 sdram 所需要的,因为reset 后pc是指向 0 的。
下面是 arm-linux-gdb 的调试过程,这里需要一些“新”指令。
首先,看看你的板子从哪里启动的,这里假设是从nand,片内ram为始于0x00000000的4k。
[szs@szs mine]$ arm-linux-gdb
GNU gdb (Sourcery G++ Lite 2008q3-72) 6.8.50.20080821-cvs
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "--host=i686-pc-linux-gnu --target=arm-none-linux-gnueabi".
For bug reporting instructions, please see:
<https://support.codesourcery.com/GNUToolchain/>.
(gdb) target remote 127.0.0.1:3333
Remote debugging using 127.0.0.1:3333
0x00000000 in ?? ()
(gdb) load sdram.elf
Loading section .text, size 0xc84 lma 0x30000000
Loading section .ARM.exidx, size 0x68 lma 0x30000c84
Start address 0x30000000, load size 3308
Transfer rate: 28 KB/sec, 1654 bytes/write.
(gdb) i r
r0 0x1b4 436
r1 0x200 512
r2 0xdb 219
r3 0x40 64
r4 0x57600 357888
r5 0x0 0
r6 0x60000 393216
r7 0x0 0
r8 0x33edffe0 871235552
r9 0x33fdd970 872274288
r10 0x33fd7600 872248832
r11 0x0 0
r12 0x100 256
sp 0x33f0ffd4 0x33f0ffd4
lr 0x7e0 2016
pc 0x30000000 0x30000000
fps 0x0 0
cpsr 0x800000d3 2147483859
默 认的load将.text 放在了0x30000000处,这是因为链接时指定了 -Ttext 0x30000000,pc 也被设置到了 0x30000000,但这对应一个刚刚 reset halt 过的开发板,0x30000000 显然尚不可用!!做过load后的片内ram里有什么呢
(gdb) x/16xw 0
0x0: 0xea000014 0xe59ff014 0xe59ff014 0xe59ff014
0x10: 0xe59ff014 0xe59ff014 0xe59ff014 0xe59ff014
0x20: 0x33f80260 0x33f802c0 0x33f80320 0x33f80380
0x30: 0x33f803e0 0x33f80440 0x33f80460 0xdeadbeef
对比一下sdram.bin显然不是你的机器码,这样就算把 pc 设置到0也不会运行你的程序的。how?
(gdb) load sdram.elf 0xd0000000
Loading section .text, size 0xc84 lma 0x0
Loading section .ARM.exidx, size 0x68 lma 0xc84
Start address 0x30000000, load size 3308
Transfer rate: 40 KB/sec, 1654 bytes/write.
(gdb) set $pc=0
(gdb) i r
r0 0x194 404
r1 0x200 512
r2 0xcb 203
r3 0x4e000000 1308622848
r4 0x57200 356864
r5 0x0 0
r6 0x60000 393216
r7 0x0 0
r8 0x33edffe0 871235552
r9 0x33fdd970 872274288
r10 0x33fd7200 872247808
r11 0x0 0
r12 0x100 256
sp 0x33f0ffd4 0x33f0ffd4
lr 0x7e0 2016
pc 0x0 0
fps 0x0 0
cpsr 0x800000d3 2147483859
(gdb) si
0x0000001c in ?? ()
(gdb) si
0x00000020 in ?? ()
(gdb) si
0x00000024 in ?? ()
(gdb) si
0x00000028 in ?? ()
(gdb) si
0x00000004 in ?? ()
(gdb) c
Continuing.
load sdram.elf 时加个参数 0xd0000000,为何?因为你想让gdb 帮您把运行地址为0x30000000的段装载到0位置去,你需要给它一个偏移,0x30000000加 什么能得到0呢,只有靠溢出了。随后还要
set $pc=0
如果你有兴趣再
x/16xw 0
对比一下你的 sdram.bin 吧。通过 si 可以看到 pc 再经历 0 -> 1c -> 20 -> 24 -> 28 后又回到4 了,显然是杀狗回来了。恭喜,c 后看着 LED 给你弹小调吧。
PS: 64M sdram 是如何算出的?
S3C2440A 给 sdram 的数据线有32根D0~D31,这意味着一次可以取出4个字节;但地址线只有13根A2~A14,如何寻址64M呢?原来选址不是一次完成的,先行再列,额外还有个页。先用A2~A14指定行,再以约定时序用A2~A10指定列,配合A24~A25指定的页,一个可以寻址 13 + 9 + 2 = 24 即 16M 地址空间。因为位宽是16bit,故这代表着 32MB sdram 容量,再把2片这样的 sdram 并连寻址,用D0~D31顺序连接获取输出就能得到 64M了。
