gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 05:15:47 32 4
gpt4 key购买 nike

例如对于 ARM,如果我静态编译,一切正常:

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

在另一个终端上:

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,并保持其他一切不变,我的断点将永远不会被击中,并且程序会一直运行直到完成:

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]

可执行文件本身确实可以正常工作:

qemu-arm -L /usr/arm-linux-gnueabihf ./hello_world

打印:

hello world

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

在 Ubuntu 18.04、gdb-multiarch 8.1-0ubuntu3、gcc-arm-linux-gnueabihf 4:7.3.0-3ubuntu2、qemu-user 1:2.11+dfsg-1ubuntu7.3 上测试。

编辑:工作原始 crosstool-ng 设置

作为健全性检查,我尝试使用 crosstool-ng 获得一个干净的工具链,它成功了:

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

在另一个外壳上:

./.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 提供的发行版。

最佳答案

失败是由于 -pie -fpie,并且在以下位置有错误报告:https://bugs.launchpad.net/qemu/+bug/1528239

显式设置 -no-pie -fno-pie 使其可以在 crosstool-ng 和 Ubuntu 主机上运行。

区别在于 Ubuntu 的 GCC 默认使用 -fpie,而我的 crosstool-ng 构建没有。

这可以通过以下方式检查:

gcc -v

在主机 GCC 上包含:

--enable-default-pie

但不在 crosstool-ng 构建中。

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

然后用打包好的工具链看到断点地址很小,就做了链接。

关于gdb - 如何在 QEMU 用户模式下使用 GDB 逐步调试动态链接的可执行文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51310756/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com