请输入图片描述

环境

Ubuntu 1604 x64

交叉编译

需要交叉编译的话,首先要安装arm-gcc,在ubuntu里可以直接apt安装。

$ sudo apt install gcc-arm-linux-gnueabi

安装好后就可以交叉编译arm程序了。

$ arm-linux-gnueabi-gcc helloworld.c -o arm_helloworld

运行arm程序

运行arm程序需要借助qemu,首先需要安装qemu一系列的东西

$ sudo apt install qemu qemu-kvm qemu-system-arm

安装好后,可以通过下列命令执行

$ qemu-arm ./arm_helloworld
Hello World!

有可能会遇到下列问题:

$ qemu-arm ./arm_helloworld
/lib/ld-linux.so.3: No such file or directory

这个情况的话,可以参考:http://stackoverflow.com/a/16722248/5187702 这个回答解决问题。

调试arm程序

还需要安装一个arm的gdb。

$ sudo apt install gdb-arm-none-eabi 

安装好后,执行下列命令开启远程调试

$ qemu-arm -g 1234 arm_helloworld

然后新开一个终端就可以了:

$ arm-none-eabi-gdb arm_helloworld 
GNU gdb (7.10-1ubuntu3+9) 7.10
Copyright (C) 2015 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=x86_64-linux-gnu --target=arm-none-eabi".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...

warning: A handler for the OS ABI "GNU/Linux" is not built into this configuration
of GDB.  Attempting to continue with the default armv5t settings.

Reading symbols from arm_helloworld...(no debugging symbols found)...done.
(gdb) b main
Breakpoint 1 at 0x1043c
(gdb) r
Don't know how to run.  Try "help target".
(gdb) target remote localhost:1234
Remote debugging using localhost:1234
warning: A handler for the OS ABI "GNU/Linux" is not built into this configuration
of GDB.  Attempting to continue with the default armv5t settings.

0xf67ceb00 in ?? ()
(gdb) b main
Note: breakpoint 1 also set at pc 0x1043c.
Breakpoint 2 at 0x1043c
(gdb) r
The "remote" target does not support "run".  Try "help target" or "continue".
(gdb) c
Continuing.

Breakpoint 1, 0x0001043c in main ()
(gdb) diss
Undefined command: "diss".  Try "help".
(gdb) di
directory    disable      disassemble  disconnect   display      
(gdb) di
directory    disable      disassemble  disconnect   display      
(gdb) disassemble
Dump of assembler code for function main:
   0x00010434 <+0>: push    {r11, lr}
   0x00010438 <+4>: add r11, sp, #4
=> 0x0001043c <+8>: ldr r0, [pc, #12]   ; 0x10450 <main+28>
   0x00010440 <+12>:    bl  0x102dc <puts@plt>
   0x00010444 <+16>:    mov r3, #0
   0x00010448 <+20>:    mov r0, r3
   0x0001044c <+24>:    pop {r11, pc}
   0x00010450 <+28>:    andeq   r0, r1, r4, asr #9
End of assembler dump.

请输入图片描述