Qemu下的一個Hello World程序

題外話:前一段看了一個文章很有感觸,說是很多優秀的開源開發者到了google都停止了開發。爲什麼呢,因爲開源一個很大的動力來自於對現有開發環境的不滿,而google提供了一個近似理想的開發環境,所以大家就懈怠了。有意思,呵呵。

 

今天學習在Qemu下運行一個簡單的Hello World程序,整個過程還是參照了

http://balau82.wordpress.com/2010/02/28/hello-world-for-bare-metal-arm-using-qemu/

 

基本過程是產生Hello World源程序,產生內存映射文件,然後編譯下載。這個實驗是針對ARM的,很多源程序不是特別明白,就當一個練習吧。

 

1. test.c 文件


volatile unsigned int * const UART0DR = (unsigned int *)0x101f1000;

void print_uart0(const char *s) {

while(*s != '\0') { /* Loop until end of string */

*UART0DR = (unsigned int)(*s); /* Transmit char */

s++; /* Next char */

}

}

void c_entry() {

print_uart0("Hello world!\n");

}

2. startup.s文件


.global _Reset
_Reset:
LDR sp, =stack_top
BL c_entry
B .

3. test.ld文件


ENTRY(_Reset)
SECTIONS
{
. = 0x10000;
.text : {
startup.o
*(.text)
}
.data : { *(.data) }
.bss : { *(.bss) }
. = . + 0x1000; /* 4kB of stack memory */
stack_top = .;
}

4. 編譯命令

arm-linux-uclibcgnueabi-as -mcpu=arm926ej-s -g startup.s -o startup.o arm-linux-uclibcgnueabi-gcc -c -mcpu=arm926ej-s -g test.c -o test.o arm-linux-uclibcgnueabi-ld -T test.ld test.o startup.o -o test.elf arm-linux-uclibcgnueabi-objcopy -O binary test.elf test.bin

 

5. 開始仿真

qemu-system-arm -M versatilepb -nographic -kernel test.bin運行後出現了HelloWorld的字樣。結束Qemu的仿真使用ctrl+a然後x 6. 使用gdb進行遠程調試在qemu中啓動debug功能qemu-system-arm -M versatilepb -nographic -s -S -kernel test.bin

這時qemu會等待gdb命令

 

7. 新開一個窗口,運行

arm-linux-uclibcgnueabi-gdb

target remote localhost:1234 file test.elfexit可以退出debug
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章