grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 5/8] gdb: Add functions to make loading from dynamically position


From: Glenn Washburn
Subject: [PATCH 5/8] gdb: Add functions to make loading from dynamically positioned targets easier
Date: Sun, 13 Feb 2022 21:42:42 -0600

Many targets, such as EFI, load GRUB at addresses that are determined at
runtime. So the load addresses in kernel.exec will almost certainly be
wrong. Given the address of the start of the text and data segments, these
functions will tell GDB to load the symbols at the proper locations.
It is left up to the user to determine how to get the test and data
addresses.

Signed-off-by: Glenn Washburn <development@efficientek.com>
---
 grub-core/gdb_grub.in | 54 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)

diff --git a/grub-core/gdb_grub.in b/grub-core/gdb_grub.in
index dd1e86bf2..f3c6faf94 100644
--- a/grub-core/gdb_grub.in
+++ b/grub-core/gdb_grub.in
@@ -9,6 +9,60 @@
 ### Lubomir Kundrak <lkudrak@skosi.org>
 ###
 
+define dynamic_load_kernel_exec_symbols
+       shell rm -f .remove-kernel.exec.symfile.gdb
+
+       # Loading symbols is complicated by the fact that kernel.exec is an ELF
+       # ELF binary, but the UEFI runtime is PE32+. All the data sections of
+       # the ELF binary are concatenated (accounting for ELF section alignment)
+       # and put into one .data section in the PE32+ runtime image. So given
+       # the load address of the .data PE32+ section we can determine the
+       # addresses each ELF data section maps to.
+       shell bash -c ' \
+       ( \
+         export PE_TEXT=$1 PE_DATA=$2; \
+         alignup() { \
+           PAD=1; \
+           if [ "$(($1%$2))" -eq 0 ]; then \
+             PAD=0; \
+           fi; \
+           printf "0x%x\n" $(((($1/$2)+$PAD)*$2)); \
+         }; \
+         printf "add-symbol-file kernel.exec ${PE_TEXT}"; \
+         objdump -h kernel.exec | tail -n +6 | \
+           while read IDX NAME SIZE _ _ OFFSET ALIGN; do \
+             read FLAGS; \
+             if [ -n "$FLAGS" ] && [ -z "${FLAGS%%*DATA*}" -o "$NAME" = .bss 
]; then \
+               OFF=$(alignup ${OFF:-0} $ALIGN); \
+               echo -n " -s $NAME (${PE_DATA}+$(printf "0x%x" $OFF))"; \
+               OFF=$((${OFF} + 0x${SIZE})); \
+             fi; \
+           done \
+       )' script $arg0 $arg1 >.kernel.exec.loadsym.gdb
+       source .kernel.exec.loadsym.gdb
+end
+document dynamic_load_kernel_exec_symbols
+       Load debugging symbols from kernel.exec given the address of the
+       .text and .data segments of the UEFI binary.
+end
+
+define dynamic_load_symbols
+       dynamic_load_kernel_exec_symbols $arg0 $arg1
+
+       # We may have been very late to loading the kernel.exec symbols and
+       # and modules may already be loaded. So load symbols for any already
+       # loaded.
+       load_all_modules
+
+       runtime_load_module
+end
+document dynamic_load_symbols
+       Load debugging symbols from kernel.exec and any loaded modules given
+       the address of the .text and .data segments of the UEFI binary. Also
+       setup session to automatically load module symbols for modules loaded
+       in the future.
+end
+
 # Add section numbers and addresses to .segments.tmp
 define dump_module_sections
        set $mod = $arg0
-- 
2.27.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]