利用 qemu user mode学习aarch64汇编
环境
x86-64 PC ubuntu 系统
工具准备
aarch64-linux-gnu-gcc
:可以通过下载 linaro 工具链 http://releases.linaro.org/components/toolchain/binaries/latest-7/aarch64-linux-gnu/- qemu-aarch64 - sudo apt install qemu-user
aarch64 汇编
创建文件head.S
,其内容如下
1 .global _start 2 3 _start: 4 ldr x0, loop 5 adr x1, loop 6 ldr x2, =loop 7 adrl x3, loop 8 9 loop: 10 nop 11 nop
然后编译并使用 qemu-aarch64 运行
1 aarch64-linux-gnu-gcc -nostdlib -nodefaultlibs -o head.elf head.S 2 3 zhiwei@zhiwei-pc:~/work/qemu-aarch64/examples$ file head.elf 4 head.elf: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, BuildID[sha1]=33d5c1030ba8161948a56be12aa694853537acc7, not stripped
调试 运行:
qemu-aarch64 -g 1234 -cpu cortex-a53 head.elf
另外一个terminal 里面启动 aarch64-linux-gnu-gdb进行调试
1 (gdb) target remote :1234 2 0x00000000004000d8 in ?? () 3 (gdb) file head.elf 4 5 (gdb) disass 6 Dump of assembler code for function _start: 7 => 0x00000000004000d8 <+0>: ldr x0, 0x4000e48 0x00000000004000dc <+4>: adr x1, 0x4000e4 9 0x00000000004000e0 <+8>: ldr x2, 0x4000e8 4> 10 End of assembler dump. 11 (gdb) si 12 0x00000000004000dc in _start () 13 (gdb) info r 14 x0 0x4000e4d503201f 18015381335777311 15 x1 0x0 0 16 x2 0x0 0
附件:系统调用汇编示例:
1 .section .text 2 .global _start 3 4 _start: 5 /* syscall write(int fd, const void *buf, size_t count) */ 6 mov x0, #1 7 ldr x1, =msg 8 ldr x2, =len 9 mov w8, #64 10 svc #0 11 12 /* syscall exit(int status) */ 13 mov x0, #0 14 mov w8, #93 15 svc #0 16 17 msg: 18 .ascii "Hello, ARM64!" 19 len = . - msg