在 QEMU 下调试 POSIX-UEFI 引导加载程序:使用 GDB 与魔术标记自动加载符号
文章目录
在 QEMU 下调试 POSIX-UEFI 引导加载程序
本文介绍如何在 QEMU 下调试 POSIX‑UEFI 引导加载程序(或任何 EFI 应用)而无需硬编码加载地址。通过让你的 EFI 二进制文件将一个“魔术标记”及其运行时基址写入已知的 RAM 位置,你可以设置一个监视点,然后向 GDB 准确报告你的代码位于内存中的位置,随后自动加载符号。
QEMU 设置
- 使用
-serial选项为虚拟机提供串行控制台。你将在终端中获得控制台日志,并且可以通过简单端口写入将调试跟踪输出到串行控制台。 - 使用
-s选项启用内置 GDB stub,它将在 TCP 端口 1234 上等待连接。 - 使用
-S选项在启动时暂停虚拟 CPU,直到调试器连接。
像往常一样启动 QEMU,提供固件和磁盘映像的路径。例如:
qemu-system-x86_64 -drive file=boot.img -bios firmware/OVMF.fd -m 256M -smp 4 -machine q35 -net none -s -S -serial mon:stdio
QEMU 将启动并在端口 1234 上等待传入的 GDB 连接。在你使用 GDB 连接并发出 continue 命令之前,虚拟机不会开始执行。
如果你使用 libvirt,可以将以下内容添加到 /etc/libvirt/qemu/ 中 XML 配置文件的 <domain> 部分,以为虚拟机启用 gdb 服务器:
<qemu:commandline> <qemu:arg value='-s'/> <qemu:arg value='-S'/> </qemu:commandline>
使用 POSIX UEFI 库的示例应用程序
调试 UEFI 二进制文件可能具有挑战性,因为你通常不知道你的映像在运行时将被加载到哪个地址,这使得设置初始断点和加载符号都变得复杂。 一种变通方法是让你的应用程序将其加载基址连同一个标记值写入已知的内存位置,这样 GDB 可以监视该位置并在正确的地址重新加载符号。
此示例使用 POSIX-UEFI 在 64 位环境中编写,不过该方法应该可以修改以适用于任何实现。
int main(int argc, char **argv){ efi_loaded_image_protocol_t *loaded_image; efi_status_t status; // Define the GUID variable (cannot pass macro directly) efi_guid_t LoadedImageProtocolGUID = EFI_LOADED_IMAGE_PROTOCOL_GUID; // Retrieve the Loaded Image Protocol using HandleProtocol status = BS->HandleProtocol(IM, &LoadedImageProtocolGUID, (void **)&loaded_image); if (EFI_ERROR(status)) { printf("HandleProtocol failed: 0x%lx\n", status); return status; } // Print the actual base address of the loaded image printf("Image loaded at: 0x%lx\n", (uint64_t)loaded_image->ImageBase); // Write image base and marker for GDB volatile uint64_t *marker_ptr = (uint64_t *)0x10000; volatile uint64_t *image_base_ptr = (uint64_t *)0x10008; *image_base_ptr = (uint64_t)loaded_image->ImageBase; // Store ImageBase *marker_ptr = 0xDEADBEEF; // Set marker printf("Hello, world!\n"); return EFI_SUCCESS; }
当此代码执行时,它会将 0xDEADBEEF 写入地址 0x10000,并将映像基址写入地址 0x10008,使你能够在 GDB 中检测到重新加载符号的时刻和地址。
GDB 所需的下一项是带符号的可执行映像。如果你仔细检查了构建日志和 Makefile,你应该会注意到,当从 ELF 共享对象文件创建 EFI 可执行文件时,只有一组有限的节被复制到生成的映像中:
.text .sdata .reloc .data .dynamic .dynsym
为了获得调试符号,我们还需要以下这些节(如果你使用 "-ggdb" 选项编译了文件):
.debug_info .debug_abbrev .debug_loc .debug_aranges .debug_line .debug_macinfo .debug_str etc
但如果你创建的 EFI 二进制文件额外包含这些节,EFI 固件将无法执行它。幸运的是,我们不需要在目标机器上拥有调试符号,因为无论如何我们都会使用 gdb 进行远程调试。相反,我们可以创建两个 EFI 二进制文件——一个只包含必需的节以便上传到目标系统,另一个包含调试符号以便与 GDB 一起使用。在实践中,这只需要使用不同的节集合再次运行 objcopy 工具,--only-keep-debug 标志方便地提供了复制功能,并使用不同的输出文件。以下是修改 POSIX-UEFI makefile 的示例:
ifneq ($(USE_GCC),) $(OBJCOPY) -j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel -j .rela -j .rel.* -j .rela.* -j .reloc --output-target $(EFIARCH) --subsystem=10 $^ $(addprefix $(OUTDIR),$@) || echo target: $(EFIARCH) $(OBJCOPY) --only-keep-debug $^ $(addprefix $(OUTDIR),$@.debug) @rm $(addprefix $(OUTDIR),$(TARGET).so) endif
现在你可以启动 GDB。由于你的映像将被固件重定位,你需要让 GDB 知道正确的地址。
$ gdb (gdb) target remote localhost:1234 # Set a watchpoint for the marker write (gdb) watch *(unsigned long long*)0x10000 == 0xDEADBEEF (gdb) continue # Execution will break as soon as the marker is written. # Now, fetch the relocated base address: (gdb) set $base = *(unsigned long long*)0x10008 # Reload symbols for the image at the correct address: (gdb) add-symbol-file bootloader/main.efi.debug -o $base # Now you can step, set breakpoints, etc. (gdb) si
$ gdb
GNU gdb (GDB) 16.3
Copyright (C) 2024 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 "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
--Type <RET> for more, q to quit, c to continue without paging--
rd".
(gdb) target remote localhost:1234
Remote debugging using localhost:1234
warning: No executable has been specified and target does not support
determining executable automatically. Try using the "file" command.
0x000000000000fff0 in ?? ()
(gdb) watch *(unsigned long long*)0x10000 == 0xDEADBEEF
Hardware watchpoint 1: *(unsigned long long*)0x10000 == 0xDEADBEEF
(gdb) continue
Continuing.
Thread 1 hit Hardware watchpoint 1: *(unsigned long long*)0x10000 == 0xDEADBEEF
Old value = 0
New value = 1
0x000000000e059518 in ?? ()
(gdb) set $base = *(unsigned long long*)0x10008
(gdb) add-symbol-file bootloader/main.efi.debug -o $base
add symbol table from file "bootloader/main.efi.debug" with all sections offset by 0xe056000
(y or n) y
Reading symbols from bootloader/main.efi.debug...
(gdb) si
0x000000000e05951f 265 printf("Hello, world!\n");
(gdb) list
260 volatile uint64_t *marker_ptr = (uint64_t *)0x10000;
261 volatile uint64_t *image_base_ptr = (uint64_t *)0x10008;
262 *image_base_ptr = (uint64_t)loaded_image->ImageBase; // Store ImageBase
263 *marker_ptr = 0xDEADBEEF; // Set marker
264
265 printf("Hello, world!\n");
266
267 return EFI_SUCCESS;
268 }
269
(gdb)
或者,如果你想避免每次手动输入这些命令,你也可以将它们放在项目目录中的 .gdbinit 文件中。GDB 在启动时会自动执行这些命令,从而简化你的调试工作流。
例如:
target remote localhost:1234 watch *(unsigned long long)0x10000 == 0xDEADBEEF continue set $base = *(unsigned long long)0x10008 add-symbol-file bootloader/main.efi.debug -o $base
来源与翻译说明
本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。
原文:Debugging UEFI applications with GDB;离线修订号:30286。

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论