编译一下: C:>ccarm -c -O2 -mcpu=arm710 -mlittle-endian -nostdlib -fvolatile -nostdinc –static sam1.c C:>ldarm -o sam1.out -Ttext 10000000 sam1.o ldarm: warning: cannot find entry symbol _start; defaulting to 10000000 sam1.o(.text+0xc):fake: undefined reference to `__gccmain' sam1.o(.text+0xc):fake: relocation truncated to fit: ARM_26 __gccmain 我们发现应该把main定义成_start #define DWORD unsigned int void start(void) //gcc需要把它定义成_start,vxworks的egcs要把它定义成start。 { register DWORD dwHardwareBase; asm ("_my_start: mov r14, #0x70 mcr p15, 0, r14, c1, c0, 0 /* MMU disabled, 32 Bit mode, Little endian */ mrs r14, cpsr bic r14, r14, #0x1f /* Mask */ orr r14, r14, #0xc0 + 0x13 /* Diasble IRQ and FIQ, SVC32 Mode */ msr cpsr, r14 ldr r13, =0xc0020000 /* Setup Stack */ "); dwHardwareBase = (DWORD)0x80000000; } 编译一下: C:>ccarm -c -O2 -mcpu=arm710 -mlittle-endian -nostdlib -fvolatile -nostdinc –static sam1.c C:>ldarm -o sam1.out -Ttext 10000000 sam1.o C:>objdumparm -d sam1.out > sam1.asm 现在来看看汇编的源代码: sam1.out: file format coff-arm-little Disassembly of section .text: 10000000 <_start>: 10000000: e1a0c00d mov ip, sp 10000004: e92dd800 stmdb sp!, {fp, ip, lr, pc} 10000008: e24cb004 sub fp, ip, #4 1000000c <_my_start>: 1000000c: e3a0e070 mov lr, #70 10000010: ee01ef10 mcr 15, 0, lr, cr1, cr0, {0} 10000014: e10fe000 mrs lr, cpsr 10000018: e3cee01f bic lr, lr, #1f 1000001c: e38ee0d3 orr lr, lr, #d3 10000020: e129f00e msr cpsr, lr 10000024: e59fd000 ldr sp, 1000002c <$$lit__1> 10000028: e91ba800 ldmdb fp, {fp, sp, pc} 1000002c <$$lit__1>: 1000002c: c0020000 andgt r0, r2, r0 10000030 <__CTOR_LIST__>: 10000030: ffffffff swinv 0x00ffffff 10000034: 00000000 andeq r0, r0, r0 10000038 <__DTOR_LIST__>: 10000038: ffffffff swinv 0x00ffffff 1000003c: 00000000 andeq r0, r0, r0 哈哈!在<_start>和<_my_start>之间的代码在干什么?是在保存现场吧,还用到了 堆栈。而这时堆栈还没初始化,向堆栈里写东西那不乱套了!应该屏蔽这段代码。 那就在<_start>之前放一个跳转指令跳到<_my_start>吧。 #define DWORD unsigned int asm (" .text .global _start _start: bl _my_start /* Omit the entry code for function c_start() */ "); void c_start(void) { register DWORD dwHardwareBase; asm ("_my_start: mov r14, #0x70 mcr p15, 0, r14, c1, c0, 0 /* MMU disabled, 32 Bit mode, Little endian */ mrs r14, cpsr bic r14, r14, #0x1f /* Mask */ orr r14, r14, #0xc0 + 0x13 /* Diasble IRQ and FIQ, SVC32 Mode */ msr cpsr, r14 ldr r13, =0xc0020000 /* Setup Stack */ "); dwHardwareBase = (DWORD)0x80000000; }
|