Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1npeTO-00077a-LB for mharc-grub-devel@gnu.org; Fri, 13 May 2022 19:13:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:56128) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1npeTM-00070Y-O1 for grub-devel@gnu.org; Fri, 13 May 2022 19:13:24 -0400 Received: from mail-ua1-x934.google.com ([2607:f8b0:4864:20::934]:40634) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1npeTK-0007Ov-Pj for grub-devel@gnu.org; Fri, 13 May 2022 19:13:24 -0400 Received: by mail-ua1-x934.google.com with SMTP id z15so3724514uad.7 for ; Fri, 13 May 2022 16:13:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficientek-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=87vq4sMCT/51bJaMPtnt2kmPI/u/lG5aQy4Wykhw4Mc=; b=y+PP1BhG+dWshzypLX7+ZULjZuT0YgPLWLX6HEBH55KdLvCSZ1jW/jK+XvoH3Tc0PR A2beitjKJAl7PT/lMG2SwnO/x+3oOYyjmtO9uNo78ZU54aLQVJst3vnEdXseVUSQmWpt C3RWdknDecIrN7Ekr4RRSqT6jrp5M5bFAMcXxHCjUX9FIfGjCbgc/RJPmZ0ie4X+uzb7 nrikZ58a5zYD1AgGMNA17hDh+KvDG9Wgmf53hPaC06POmeLLV1wC9/xhW36dHL75FCEI vFFIYNqOG6wDSip7haFcayvSR3AFPi8RGGr0o2udIf64VHuvwYCutMf4gMUagMG7BwjF aitA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=87vq4sMCT/51bJaMPtnt2kmPI/u/lG5aQy4Wykhw4Mc=; b=pn1VwmMkKlLeeWXh+TAahhRJkAO9ZiR87tmct1KGFAQuHNXEt9tWuG06wqINp6r4jX O2PN+U2EhD/Q5uQjVxGP2Vfda+smcBcde9K1PC8I0AzEa3B7uP7hSCP4f/nCR9I2n3j9 tb3SVbDMag3LTowJS7qRjDeWJvkHIFQTCD3IX+qVoWvV3yglEOsOdpRnWeHJCcQxH8zy sn8yrO21ZrIRlaQ8Z0DaWBqh/SoffUIZmcVWrB/Ns/bVXmR9j5DyoXICLMZZbeE7AudZ N1WPALEuhU0gS+/0Lzc/l3MUN3J5kvlxtzxXAr/RZmjjRqgjkY3CpeERpuszHzIf0wK5 jCng== X-Gm-Message-State: AOAM530ocRG+vRsMtZd1wTQEZiI/sAQtebZUJ0Be0xeEsdVuwxW/nz0a g5n7kXVgjptVLfz13AXNMOW/ctjmN7U4WoCo X-Google-Smtp-Source: ABdhPJyCrREQLfWOmPfj5ub/yyMkaWPg3B4d8QxtMBFB8KmkeSYN6mFfBLU0rlOYSHeShUwL4f2jng== X-Received: by 2002:ab0:15ae:0:b0:359:6107:43b8 with SMTP id i43-20020ab015ae000000b00359610743b8mr3188177uae.23.1652483601759; Fri, 13 May 2022 16:13:21 -0700 (PDT) Received: from localhost.localdomain ([37.218.244.249]) by smtp.gmail.com with ESMTPSA id 129-20020a1f1387000000b0034e6f1fd067sm423865vkt.49.2022.05.13.16.13.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 16:13:21 -0700 (PDT) From: Glenn Washburn To: grub-devel@gnu.org, Daniel Kiper Cc: Peter Jones , Robbie Harwood , Glenn Washburn Subject: [PATCH v2 07/15] gdb: Add functions to make loading from dynamically positioned targets easier Date: Fri, 13 May 2022 18:12:40 -0500 Message-Id: <11b687f39f403aeabf12b338629640079b796ce9.1652482822.git.development@efficientek.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::934; envelope-from=development@efficientek.com; helo=mail-ua1-x934.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 13 May 2022 23:13:25 -0000 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 segment, 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 text address. Signed-off-by: Glenn Washburn --- grub-core/Makefile.core.def | 6 ++++ grub-core/gdb_grub.in | 27 ++++++++++++++++ grub-core/gdb_helper.sh.in | 62 +++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 grub-core/gdb_helper.sh.in diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index 715994872..574afa50c 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -24,6 +24,12 @@ transform_data = { common = gmodule.pl.in; }; +transform_data = { + installdir = platform; + name = gdb_helper.sh; + common = gdb_helper.sh.in; +}; + transform_data = { installdir = platform; name = gdb_grub; diff --git a/grub-core/gdb_grub.in b/grub-core/gdb_grub.in index 521e3ae6c..7891c6c07 100644 --- a/grub-core/gdb_grub.in +++ b/grub-core/gdb_grub.in @@ -9,6 +9,33 @@ ### Lubomir Kundrak ### +define dynamic_load_kernel_exec_symbols + shell rm -f .remove-kernel.exec.symfile.gdb + shell sh gdb_helper.sh gen_kernel_exec_loadsym $arg0 >.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 segment of the UEFI binary in memory. +end + +define dynamic_load_symbols + dynamic_load_kernel_exec_symbols $arg0 + + # 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 segment of the UEFI binary in memory. 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_helper set $mod = $arg0 diff --git a/grub-core/gdb_helper.sh.in b/grub-core/gdb_helper.sh.in new file mode 100644 index 000000000..b37d5adfc --- /dev/null +++ b/grub-core/gdb_helper.sh.in @@ -0,0 +1,62 @@ +### +### Helper functions for GRUB's GDB script. +### + +alignup() { + PAD=1 + if [ "$(($1%$2))" -eq 0 ]; then + PAD=0 + fi + printf "0x%x\n" "$(((($1/$2)+$PAD)*$2))" +} + +exp() { + BASE=${1%%\*\**} + EXP=${1##*\*\*} + RES=1 + while [ "$EXP" -gt 0 ]; do + RES=$(($RES*$BASE)) + EXP=$(($EXP - 1)) + done + echo $RES +} + +# 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. The UEFI application is +# loaded into memory just as it is laid out in the file. It is not +# assumed that the binary is available, but it is known that the .text +# section directly precedes the .data section and that .data is EFI +# page aligned. Using this, the .data offset from .text can be found. +gen_kernel_exec_loadsym() { + PE_SECTION_ALIGN=$((1<<12)) + PE_TEXT=$1 + TSIZE=$((0x$(objdump -h kernel.exec | grep -E " \.text\b" | \ + (read _ _ SIZE _; echo $SIZE)))) + PE_DATA_OFF=$(printf "0x%x" $(($(alignup ${TSIZE} ${PE_SECTION_ALIGN})))) + + 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} $(exp $ALIGN)) + printf " -s $NAME (${PE_TEXT}+${PE_DATA_OFF}+0x%x)" "$OFF" + OFF=$((${OFF} + 0x${SIZE})) + fi + done +} + +if type "$1" 2>/dev/null | grep -q 'is a shell function'; then + if [ "x${GRUB_GDB_TRACE}" = "xy" ]; then + exec 2>>gdb_helper.trace + set -x + fi + + "$@" +else + exit 1 +fi -- 2.34.1