gcc生成二进制文件供uboot的go命令执行

来源:互联网 发布:stc编程软件 编辑:程序博客网 时间:2024/05/17 04:18

原文地址: http://blog.csdn.net/joans123/article/details/7380906

使用gcc编译出二进制文件给uboot中go的命令执行.


test.c 文件如下 
================ Start of test.c =======================================
#include <stdio.h>

typedef void (*pr)(const char *fmt, ...);

int main(int argc, char **argv)
{
    // 0x80e96d8c 是uboot中 printf的地址, 查看uboot下面的 System.map可知.
    pr print = (pr)0x80e96d8c;

    print("hello wolrd");
    while (1);
    return 0;
}
================ End of test.c =======================================


Makefile 文件如下: 
1. 使用gcc的-c选项声场.o文件
2. 使用ld并指定lds文件生成elf文件
3. 使用objcopy去掉符号表, 生成二进制文件
================ Start of Makefile =======================================
CROSS_COMPILE ?= /opt/toolchain/arm-eabi-4.4.0/bin/arm-eabi-

CC := ${CROSS_COMPILE}gcc
LD  := ${CROSS_COMPILE}ld
NM  := ${CROSS_COMPILE}nm
OBJCOPY := ${CROSS_COMPILE}objcopy
OBJDUMP := ${CROSS_COMPILE}objdump

CFLAGS := -fno-builtin -Wall -Wstrict-prototypes -fno-stack-protector -fno-common -nostdinc -static -fPIC
CFLAGS += -isystem /opt/toolchain/arm-eabi-4.4.0/bin/../lib/gcc/arm-eabi/4.4.0/include  
CFLAGS +=  -marm -mabi=aapcs-linux -mno-thumb-interwork -march=armv5

#LDFLAGS := -Ttext 0x82000000 -e main --oformat binary
LDFLAGS :=  -Bstatic -T test.lds -v

# output map file
LDFLAGS += -Map test.map 

LDFLAGS += -L /opt/toolchain/arm-eabi-4.4.0/bin/../lib/gcc/arm-eabi/4.4.0 -lgcc

all:
    $(CC) ${CFLAGS} -c go_test.c -o go_test.o
    $(LD) ${LDFLAGS} go_test.o -o go_test.elf
    $(NM) -n go_test.elf > go_test.map
    $(OBJCOPY) -S -O binary go_test.elf go_test.bin
    cp go_test.bin /tftpboot/

================ End of Makefile =======================================


test.lds 文件如下:
OUTPUT_FORMAT : 输出的文件格式
OUTPUT_ARCH   : 输出文件的cpu类型
ENTRY         : 输出文件的入口, 与ld中的-e选项等效
指定其实地址为 : 0x82000000, 根据uboot中加载地址而定
================ Start of test.lds =======================================
OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm")
OUTPUT_ARCH(arm)
ENTRY(main)
SECTIONS
{
. = 0x82000000;
.text : { *(.text) }
.data : { *(.data) }
.bss  : { *(.bss)  }
}
================ End of test.lds =======================================



使用上面3个件便可编译出 go_test.bin的二进制文件, 然后拷贝到 /tftpboot目录下, 并在uboot中输入:
#tftp 82000000 go_test.bin
#go 82000000 
执行这条命令后输出如下:
## Starting application at 0x82000000 ...
hello wolrd
可见go_test.bin正常执行.



错误解决:
1. go_test.o:(.ARM.exidx+0x0): undefined reference to `__aeabi_unwind_cpp_pr1'  : 更换一个gcc工具链即可, 原来使用arm-2010q1 出现这个错误. 后来更换成arm-eabi-4.4.0就可以了. 不过arm-eabi-4.4.0不能生成linux可执行的elf文件.

2. go_test.o:could not read symbols: File in wrong format    :  由于Makefile编写错误造成的, 没个CC变量赋值,而使用i386 gcc编译. 使用file命令可查看go_test.o文件的格式.
0 0