[UP] add some viki files, knowledge.
[arrow.git] / viki / binutils / use-binutil-2bin.viki
bloba6c37bf27435fba232fd41daada720b796a2bae3
1 ref: http://hi.baidu.com/ttchong/blog/item/d6b958ee8267dbffb2fb955a.html
2 使用GNU binutils制作二进制文件
3 2008-02-18 21:51
5 因为自己制作的IXP425的板子能够跑uboot却不能引导Linux内核,连Uncompressing的字样也没有打出来。于是在内核head.S中初始化串口以后加入打印出简单字符的debug语句,也未见打印。为了确认跳转是否成功,于是想直接编写简单的程序,制作成二进制文件进行测试。
7 二进制程序文件,是cpu可以直接执行的文件,就像bootloader一样,如果将它存放到系统加点自动执行指令的地址,cpu就能执行文件。一般编译器完成编译链接以后生成的都是某种可执行文件格式,如linux下的ELF,a.out等,windows下则是PE(Portable Executibal)格式。
9 本程序在redboot和IXDPG425上测试通过。
10 Redboot> load -r -v -b 0×00800000 test.bin
11 Redboot> go 0×00800000
12 Hello world 0×123!
14 1、编写代码
15 测试代码来源于u-boot,利用u-boot中的printf函数,打印出hello world字样。将printf函数所有相关的函数,头文件等全部取出放到自己新建的源文件中。例如我新建了printf.c文件,存放了所有从u- boot源代码里拷贝过来的c语言代码,并添加了一个简短的调用过程:
16 int _main(void)
18 int i = 0×123;
19 printf(“Hello World 0x%x\n”, i);
20 return 1;
22 编写一个简单的head.S,跳转入_main
23 .section “.start”, #alloc, #execinstr
25 .align
26 start:
27 .type start,#function
28 b _main
30 2、链接脚本test.lds
31 修改自linux2.4.x/arch/arm/boot/compressed/vmlinux.lds
32 OUTPUT_FORMAT(”elf32-bigarm”, “elf32-bigarm”, “elf32-bigarm”)
33 OUTPUT_ARCH(arm)
34 /*ENTRY(_start)*/
35 SECTIONS
38 . = 0×800000;
40 .text : {
41 *(.start)
42 *(.text)
43 *(.fixup)
44 *(.gnu.warning)
45 *(.rodata)
46 *(.rodata.*)
47 *(.glue_7)
48 *(.glue_7t)
49 . = ALIGN(4);
52 _etext = .;
54 _got_start = .;
55 .got : { *(.got) }
56 _got_end = .;
57 .got.plt : { *(.got.plt) }
58 .data : { *(.data) }
59 _edata = .;
61 . = ALIGN(4);
62 __bss_start = .;
63 .bss : { *(.bss) }
64 _end = .;
66 .stack (NOLOAD) : { *(.stack) }
68 .stab 0 : { *(.stab) }
69 .stabstr 0 : { *(.stabstr) }
70 .stab.excl 0 : { *(.stab.excl) }
71 .stab.exclstr 0 : { *(.stab.exclstr) }
72 .stab.index 0 : { *(.stab.index) }
73 .stab.indexstr 0 : { *(.stab.indexstr) }
74 .comment 0 : { *(.comment) }
76 . = 0×00800000指定了程序被加载的地址,如果将其指定为0,则可以像bootloader一样,将其烧到flash的0地址,上电后cpu自动执行。
78 3、编译链接
79 参照u-boot和linux的编译
80 printf.c:
81 arm-linux-gcc -g -Os -fno-strict-aliasing -fno-common -ffixed-r8 -mshort-load-bytes -msoft-float -mbig-endian -D__KERNEL__ -DTEXT_BASE=0×00f80000 -I./ -I/home/tony/intel/uboot/u-boot-1.1.2/include -fno-builtin -ffreestanding -nostdinc -isystem /usr/local/lib/gcc-lib/arm-linux/3.2.1/include -pipe -DCONFIG_ARM -D__ARM__ -mbig-endian -mapcs-32 -march=armv4 -mtune=strongarm1100 -Wall -Wstrict-prototypes -c -o printf.o printf.c
83 head.S:
84 arm-linux-gcc -mbig-endian -D__ASSEMBLY__ -D__KERNEL__ -I/home/tony/intel/snapgear/snapgear/linux-2.4.x/include -mapcs-32 -D__LINUX_ARM_ARCH__=5 -mcpu=xscale -msoft-float -traditional -c -o head.o head.S
86 下面几个文件也是上述代码所依赖的,在作除法运算是需要用到
87 div0.c
88 arm-linux-gcc -g -Os -fno-strict-aliasing -fno-common -ffixed-r8 -mshort-load-bytes -msoft-float -mbig-endian -D__KERNEL__ -DTEXT_BASE=0×00f80000 -I./ -I/home/tony/intel/uboot/u-boot-1.1.2/include -fno-builtin -ffreestanding -nostdinc -isystem /usr/local/lib/gcc-lib/arm-linux/3.2.1/include -pipe -DCONFIG_ARM -D__ARM__ -mbig-endian -mapcs-32 -march=armv4 -mtune=strongarm1100 -Wall -Wstrict-prototypes -c -o div0.o div0.c
90 _udivsi3.S:
91 arm-linux-gcc -Wa,-gstabs -D__ASSEMBLY__ -g -Os -fno-strict-aliasing -fno-common -ffixed-r8 -mshort-load-bytes -msoft-float -mbig-endian -D__KERNEL__ -DTEXT_BASE=0×00f80000 -I/home/tony/intel/uboot/u-boot-1.1.2/include -fno-builtin -ffreestanding -nostdinc -isystem /usr/local/lib/gcc-lib/arm-linux/3.2.1/include -pipe -DCONFIG_ARM -D__ARM__ -mbig-endian -mapcs-32 -march=armv4 -mtune=strongarm1100 -c -o _udivsi3.o _udivsi3.S
93 _umodsi3.S:
94 arm-linux-gcc -Wa,-gstabs -D__ASSEMBLY__ -g -Os -fno-strict-aliasing -fno-common -ffixed-r8 -mshort-load-bytes -msoft-float -mbig-endian -D__KERNEL__ -DTEXT_BASE=0×00f80000 -I/home/tony/intel/uboot/u-boot-1.1.2/include -fno-builtin -ffreestanding -nostdinc -isystem /usr/local/lib/gcc-lib/arm-linux/3.2.1/include -pipe -DCONFIG_ARM -D__ARM__ -mbig-endian -mapcs-32 -march=armv4 -mtune=strongarm1100 -c -o _umodsi3.o _umodsi3.S
96 链接:
97 arm-linux-ld -p -X -T test.lds head.o printf.o div0.o _udivsi3.o _umodsi3.o -o test
98 arm-linux-objcopy -O binary -R .note -R .comment -S test test.bin
99 得到test.bin二进制文件
101 补充:本测试程序利用了bootloader的串口设置和堆栈设置,仅仅用作测试和演示二进制程序文件制作的过程。