Qemu 学习笔记

Posted by API Caller on February 19, 2020

qemu-user-static

arm 为例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
sudo apt-get install qemu-user-static

sudo apt-get install libc6-dev-arm64-cross libc6-dev-armel-armhf-cross libc6-dev-armel-cross libc6-dev-armhf-armel-cross libc6-dev-armhf-cross

# gcc-arm-linux-gnueabihf: GNU C compiler for the armhf architecture
sudo apt-get install gcc-arm-linux-gnueabihf
arm-linux-gnueabihf-gcc -o hello hello.c -static
qemu-arm-static hello

# gcc-arm-linux-gnueabi: GNU C compiler for the armel architecture
sudo apt-get install gcc-arm-linux-gnueabi
arm-linux-gnueabi-gcc-5 -o hello hello.c -static
qemu-arm-static hello

# gcc-aarch64-linux-gnu: GNU C compiler for the arm64 architecture
sudo apt-get install gcc-aarch64-linux-gnu
aarch64-linux-gnu-gcc -o hello hello.c -static
aarch64-linux-gnu-gcc-7 -o hello hello.c -static
qemu-aarch64-static hello



# gcc-mips-linux-gnu: GNU C compiler for the mips architecture

# gcc-mips64-linux-gnuabi64: GNU C compiler for the mips64 architecture

# gcc-mips64el-linux-gnuabi64: GNU C compiler for the mips64el architecture

# gcc-mipsel-linux-gnu: GNU C compiler for the mipsel architecture

qemu-user

arm 为例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
sudo apt-get install qemu-user

arm-linux-gnueabihf-gcc -o hello hello.c
qemu-arm -L /usr/arm-linux-gnueabihf hello


arm-linux-gnueabi-gcc-5 -o hello hello.c
qemu-arm -L /usr/arm-linux-gnueabi hello


aarch64-linux-gnu-gcc -o hello hello.c
aarch64-linux-gnu-gcc-7 -o hello hello.c
qemu-aarch64 -L /usr/aarch64-linux-gnu hello

# mips 不静态编译似乎跑不起来, 不继续分析了 
# [~] $ cat hello.c
# int main(void){}
# [~] $ mips-linux-gnu-gcc -o hello hello.c
# [~] $ file hello
# hello: ELF 32-bit MSB executable, MIPS, MIPS32 rel2 version 1 (SYSV), dynamically linked, interpreter /lib/ld., for GNU/Linux 3.2.0, not stripped
# [~] $ qemu-mips -L /usr/mips-linux-gnu ./hello
# qemu: uncaught target signal 11 (Segmentation fault) - core dumped
# Segmentation fault (core dumped)
# [~] $ qemu-mips -L /usr/mips-linux-gnu -g 12345 ./hello
# qemu: uncaught target signal 11 (Segmentation fault) - core dumped
# Segmentation fault (core dumped)


ldd tips1:

1
2
3
4
aarch64-linux-gnu-objdump -x hello | grep "NEEDED"
  # NEEDED               libc.so.6
aarch64-linux-gnu-readelf -d hello | grep "Shared library"
#  0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
sudo bash -c 'cat > /usr/bin/aarch64-linux-gnu-ldd << EOF
#!/bin/sh

if [ "\$1" = "" ] ;then
  echo "need input file"
  exit
fi

aarch64-linux-gnu-objdump -x \$1 | grep "NEEDED"
echo ""
aarch64-linux-gnu-readelf -d \$1 | grep "Shared library"
EOF'

sudo chmod a+x /usr/bin/aarch64-linux-gnu-ldd

aarch64-linux-gnu-ldd ./hello


sudo cp /usr/bin/aarch64-linux-gnu-ldd /usr/bin/arm-linux-gnueabi-ldd
sudo sed -i 's/aarch64-linux-gnu-/arm-linux-gnueabi-/g' /usr/bin/arm-linux-gnueabi-ldd

sudo cp /usr/bin/aarch64-linux-gnu-ldd /usr/bin/arm-linux-gnueabihf-ldd
sudo sed -i 's/aarch64-linux-gnu-/arm-linux-gnueabihf-/g' /usr/bin/arm-linux-gnueabihf-ldd

gdb-multiarch

1
2
3
4
5
6
7
8
9
10
11
12
sudo apt-get install gdb-multiarch

qemu-aarch64 -L /usr/aarch64-linux-gnu -g 10086 ./hello

gdb-multiarch

(gdb) set architecture aarch64
(gdb) target remote :10086
(gdb) i r pc


gef> gef-remote -q :10086

watchpoints

似乎 qemu-user 没支持, 会一直报 You may have requested too many hardware breakpoints/watchpoints.

留待阅读源码探究.

Ref