首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在QEMU用户模式下逐步调试动态链接的可执行文件?

如何在QEMU用户模式下逐步调试动态链接的可执行文件?
EN

Stack Overflow用户
提问于 2018-07-12 16:43:07
回答 1查看 1.8K关注 0票数 3

例如,对于ARM,如果我静态编译,所有工作都很好:

代码语言:javascript
复制
sudo apt-get install gdb-multiarch gcc-arm-linux-gnueabihf qemu-user
printf '
#include <stdio.h>
#include <stdlib.h>

int main() {
    puts("hello world");
    return EXIT_SUCCESS;
}
' >  hello_world.c
arm-linux-gnueabihf-gcc -ggdb3 -static -o hello_world hello_world.c
qemu-arm -L /usr/arm-linux-gnueabihf -g 1234 ./hello_world

在另一个航站楼:

代码语言:javascript
复制
gdb-multiarch -q --nh \
  -ex 'set architecture arm' \
  -ex 'set sysroot /usr/arm-linux-gnueabihf' \
  -ex 'file hello_world' \
  -ex 'target remote localhost:1234' \
  -ex 'break main' \
  -ex continue \
;

这样我就可以在main上看到源代码和步骤调试了。

但是,如果我删除-static,并保持其他所有内容不变,我的断点就不会被击中,程序将一直运行到完成:

代码语言:javascript
复制
The target architecture is assumed to be arm
Reading symbols from hello_world...done.
Remote debugging using localhost:1234
Reading symbols from /usr/arm-linux-gnueabihf/lib/ld-linux-armhf.so.3...(no debugging symbols found)...done.
0xff7b3b80 in ?? () from /usr/arm-linux-gnueabihf/lib/ld-linux-armhf.so.3
Breakpoint 1 at 0x50c: file hello_world.c, line 5.
Continuing.
[Inferior 1 (Remote target) exited normally]

但是,可执行文件本身工作正常:

代码语言:javascript
复制
qemu-arm -L /usr/arm-linux-gnueabihf ./hello_world

指纹:

代码语言:javascript
复制
hello world

我已经看到了:How to single step ARM assembler in GDB on Qemu?,但是它没有专门涵盖动态链接的可执行文件的情况。

在Ubuntu 18.04、gdb-Multiarch8.1-0 ubuntu3、gcc-arm-linux-gnueabihf 4:7.3.0-3 ubuntu2、qemu-用户1:2.11+dfsg-1 ubuntu7.3上进行了测试。

编辑:工作原始的交叉平台-ng安装程序

作为一个健全的检查,我试图得到一个干净的工具链与交叉-ng,这是有效的:

代码语言:javascript
复制
git clone https://github.com/crosstool-ng/crosstool-ng
cd crosstool-ng
git checkout d5900debd397b8909d9cafeb9a1093fb7a5dc6e6
export CT_PREFIX="$(pwd)/.build/ct_prefix"
./bootstrap
./configure --enable-local
./ct-ng arm-cortex_a15-linux-gnueabihf
# Has to be older than host kernel, which is 4.15.
printf "
CT_LINUX_V_4_14=y
CT_LINUX_VERSION=\"4.14.0\"
" >> .config
./ct-ng oldconfig
env -u LD_LIBRARY_PATH time ./ct-ng build -j`nproc`
cd ..
crosstool-ng/.build/ct_prefix/arm-cortex_a15-linux-gnueabihf/bin/arm-cortex_a15-linux-gnueabihf-gcc -ggdb3 -o hello_world hello_world.c -ggdb3 -static -o hello_world hello_world.c
qemu-arm -L crosstool-ng/.build/ct_prefix/arm-cortex_a15-linux-gnueabihf/arm-cortex_a15-linux-gnueabihf/sysroot -g 1234 ./hello_world

在另一个弹壳上:

代码语言:javascript
复制
./.build/ct_prefix/arm-cortex_a15-linux-gnueabihf/bin/arm-cortex_a15-linux-gnueabihf-gdb \
  -q --nh \
  -ex 'set architecture arm' \
  -ex 'set sysroot crosstool-ng/.build/ct_prefix/arm-cortex_a15-linux-gnueabihf/arm-cortex_a15-linux-gnueabihf/sysroot' \
  -ex 'file hello_world' \
  -ex 'target remote localhost:1234' \
  -ex 'break main' \
  -ex continue \
;

它还与发行版提供的gdb-multiarch一起工作。

EN

回答 1

Stack Overflow用户

发布于 2018-07-14 20:57:31

故障是由于-pie -fpie造成的,在:https://bugs.launchpad.net/qemu/+bug/1528239上有一个错误报告。

显式设置-no-pie -fno-pie使它可以同时工作在交叉台ng和Ubuntu主机上。

不同之处在于,Ubuntu在默认情况下使用-fpie构建了GCC,而我的交叉平台ng构建没有。

这可以通过以下方法进行检查:

代码语言:javascript
复制
gcc -v

在主持人GCC身上写着:

代码语言:javascript
复制
--enable-default-pie

但不是在交叉板凳上。

我是如何发现的:前几天我在玩-fpie,我注意到.text地址在这种情况下非常小:What is the -fPIE option for position-independent executables in gcc and ld?

然后,我发现打包的工具链的中断地址非常小,于是我做了链接。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51310756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档