Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1nCdJX-0008Ry-Ti for mharc-grub-devel@gnu.org; Wed, 26 Jan 2022 03:06:08 -0500 Received: from eggs.gnu.org ([209.51.188.92]:45080) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nCdIz-0008JC-SH for grub-devel@gnu.org; Wed, 26 Jan 2022 03:05:26 -0500 Received: from [2607:f8b0:4864:20::1035] (port=34583 helo=mail-pj1-x1035.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nCdIx-0005XH-2Z for grub-devel@gnu.org; Wed, 26 Jan 2022 03:05:25 -0500 Received: by mail-pj1-x1035.google.com with SMTP id l24-20020a17090aec1800b001b55738f633so2688469pjy.1 for ; Wed, 26 Jan 2022 00:05:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficientek-com.20210112.gappssmtp.com; s=20210112; h=date:from:to:cc:subject:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=im5JK8klgT8r1vfYBQvcgFVKEvzzbhTPTLRa0xw2Y+M=; b=IOPx3Yuf1hkDLtncVBgtIoQjJpdxIdVzZwMApdZvBtd7h6bwLnfPRNoovYtYHtwqVg k+L5yGpqqByhqDCHxzWcUXJ4nxtGnfpRxl0qDZfq5d+WuyUDC5/wzv5R30F0zbnY6QJl IftwHZnwmuSq8wmRnlkHVWU8PmhLjui6l8+b6gMCWABV8ULUcaXVXNJTLLshgnLSiQaS ZbSIxp9snHPdnC2XVb9gxZ3dISY8cs9Pbo2q1HGNY3sOYop6QpUmW8TqHauALG8dTF2O S9zEbaWG6sl1yv/3fOzucLzaC07LNmOmje+RX3+RyhlA+FvcH9/CnMuj78o+LDEkz226 I2Fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=im5JK8klgT8r1vfYBQvcgFVKEvzzbhTPTLRa0xw2Y+M=; b=R4cZ8EcpKSh8m7xP3186ELGqrUlsW352ew2siaqPChxLdMM/JvO31ss/Ys70lrIGMb A/yjsN8wLIg4G+vHhYmhxhJU/OmhNUuJ970UevG/Tvt2S6qZIZi2uug+f5iBQKOpF8vl NkQXppg7zD56PwgD9Lh0yJ8quOtjNtsvqhQLGtIAk0isspQc/lsbUx6iMZiRL8dl7O0o fwAEmsl3KME99v96jljh+7+RhRoDz9ubAlPwvCDnKTQMGUBYlM4R1IjHsYDFzsUMV6K4 GSQT6Gk02ir22gByvOk2fBZgsCWRJxKLtSs+cFMpNlpH1esEaOE3yTLtWz7FPoI5d3zr BPqg== X-Gm-Message-State: AOAM533KET4aky9M0lDfNvSNW54qtl/J1yrGv9++OBUW8lTS+KVtRDfD HZLlCXymLt0jf+hvs32LYZ1YRA== X-Google-Smtp-Source: ABdhPJzD7g+9k3KJPLCdFgJDLPesdRZ4WFmHF5fgZ6/CNIVnmUcYhwZgBDUf+evdiQkUzb7yv60PWA== X-Received: by 2002:a17:90a:2b4a:: with SMTP id y10mr4745948pjc.63.1643184305416; Wed, 26 Jan 2022 00:05:05 -0800 (PST) Received: from crass-HP-ZBook-15-G2 (yal.riseup.net. [199.58.83.9]) by smtp.gmail.com with ESMTPSA id z13sm1249313pfe.20.2022.01.26.00.05.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jan 2022 00:05:05 -0800 (PST) Date: Wed, 26 Jan 2022 02:02:45 -0600 From: Glenn Washburn To: Robbie Harwood Cc: The development of GNU GRUB , Peter Jones Subject: Re: [PATCH v2 2/2] Make a "gdb" dprintf that tells us load addresses Message-ID: <20220126020245.0952a968@crass-HP-ZBook-15-G2> In-Reply-To: <20211103182207.118313-3-rharwood@redhat.com> References: <20211103182207.118313-1-rharwood@redhat.com> <20211103182207.118313-3-rharwood@redhat.com> Reply-To: development@efficientek.com X-Mailer: Claws Mail 3.18.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::1035 (failed) Received-SPF: pass client-ip=2607:f8b0:4864:20::1035; envelope-from=development@efficientek.com; helo=mail-pj1-x1035.google.com X-Spam_score_int: -10 X-Spam_score: -1.1 X-Spam_bar: - X-Spam_report: (-1.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, PDS_HP_HELO_NORDNS=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: Wed, 26 Jan 2022 08:05:34 -0000 On Wed, 3 Nov 2021 14:22:07 -0400 Robbie Harwood wrote: > From: Peter Jones > > Add a grub_dprintf() call during platform init and during module loading > that tells us the virtual addresses of the .text and .data sections of > grub-core/kernel.exec and any modules it loads. > > Specifically, it displays them in the gdb "add-symbol-file" syntax, with > the presumption that there's a variable $grubdir that reflects the path > to any such binaries. I don't think this patch has the $grubdir that's mentioned here. I think this is useful information that is output in this patch, but I would prefer a different mechanism. How about printing normally (ie use grub_printf instead of grub_dprintf), have the code conditionally compiled in if a certain macro is defined. So here have something like... > > Signed-off-by: Peter Jones > [rharwood@redhat.com: remove custom function, adjust commit message] > Signed-off-by: Robbie Harwood > --- > grub-core/kern/dl.c | 50 +++++++++++++++++++++++++++++++++++++++ > grub-core/kern/efi/efi.c | 4 ++-- > grub-core/kern/efi/init.c | 26 +++++++++++++++++++- > include/grub/efi/efi.h | 2 +- > 4 files changed, 78 insertions(+), 4 deletions(-) > > diff --git a/grub-core/kern/dl.c b/grub-core/kern/dl.c > index 6a52de168..a5def4ea3 100644 > --- a/grub-core/kern/dl.c > +++ b/grub-core/kern/dl.c > @@ -447,6 +447,23 @@ grub_dl_find_section (Elf_Ehdr *e, const char *name) > return s; > return NULL; > } > +static long > +grub_dl_find_section_index (Elf_Ehdr *e, const char *name) > +{ > + Elf_Shdr *s; > + const char *str; > + unsigned i; > + > + s = (Elf_Shdr *) ((char *) e + e->e_shoff + e->e_shstrndx * e->e_shentsize); > + str = (char *) e + s->sh_offset; > + > + for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff); > + i < e->e_shnum; > + i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize)) > + if (grub_strcmp (str + s->sh_name, name) == 0) > + return (long)i; > + return -1; > +} > > /* Me, Vladimir Serbinenko, hereby I add this module check as per new > GNU module policy. Note that this license check is informative only. > @@ -607,6 +624,37 @@ grub_dl_relocate_symbols (grub_dl_t mod, void *ehdr) > > return GRUB_ERR_NONE; > } #ifdef PRINT_GDB_SYM_LOAD_CMD > +static void > +grub_dl_print_gdb_info (grub_dl_t mod, Elf_Ehdr *e) > +{ > + void *text, *data = NULL; > + long idx; > + > + idx = grub_dl_find_section_index (e, ".text"); > + if (idx < 0) > + return; > + > + text = grub_dl_get_section_addr (mod, idx); > + if (!text) > + return; > + > + idx = grub_dl_find_section_index (e, ".data"); > + if (idx >= 0) > + data = grub_dl_get_section_addr (mod, idx); > + > + if (data) > + grub_dprintf ("gdb", "add-symbol-file \\\n" > + "/usr/lib/debug/usr/lib/grub/%s-%s/%s.debug " > + "\\\n %p -s .data %p\n", > + GRUB_TARGET_CPU, GRUB_PLATFORM, > + mod->name, text, data); > + else > + grub_dprintf ("gdb", "add-symbol-file \\\n" > + "/usr/lib/debug/usr/lib/grub/%s-%s/%s.debug " > + "\\\n%p\n", > + GRUB_TARGET_CPU, GRUB_PLATFORM, > + mod->name, text); > +} #endif > > /* Load a module from core memory. */ > grub_dl_t > @@ -666,6 +714,8 @@ grub_dl_load_core_noinit (void *addr, grub_size_t size) > grub_dprintf ("modules", "module name: %s\n", mod->name); > grub_dprintf ("modules", "init function: %p\n", mod->init); > #ifdef PRINT_GDB_SYM_LOAD_CMD > + grub_dl_print_gdb_info (mod, e); #endif > + > if (grub_dl_add (mod)) > { > grub_dl_unload (mod); > diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c > index 8cff7be02..f12fd5893 100644 > --- a/grub-core/kern/efi/efi.c > +++ b/grub-core/kern/efi/efi.c > @@ -291,7 +291,7 @@ grub_efi_get_variable (const char *var, const grub_efi_guid_t *guid, > /* Search the mods section from the PE32/PE32+ image. This code uses > a PE32 header, but should work with PE32+ as well. */ > grub_addr_t > -grub_efi_modules_addr (void) > +grub_efi_section_addr (const char *section_name) > { > grub_efi_loaded_image_t *image; > struct grub_pe32_header *header; > @@ -316,7 +316,7 @@ grub_efi_modules_addr (void) > i < coff_header->num_sections; > i++, section++) > { > - if (grub_strcmp (section->name, "mods") == 0) > + if (grub_strcmp (section->name, section_name) == 0) > break; > } > > diff --git a/grub-core/kern/efi/init.c b/grub-core/kern/efi/init.c > index 7facacf09..35f787915 100644 > --- a/grub-core/kern/efi/init.c > +++ b/grub-core/kern/efi/init.c > @@ -82,10 +82,33 @@ stack_protector_init (void) > > grub_addr_t grub_modbase; > #ifdef PRINT_GDB_SYM_LOAD_CMD > +static void > +grub_efi_print_gdb_info (void) > +{ > + grub_addr_t text; > + grub_addr_t data; > + > + text = grub_efi_section_addr (".text"); > + if (!text) > + return; > + > + data = grub_efi_section_addr (".data"); > + if (data) > + grub_dprintf ("gdb", > + "add-symbol-file /usr/lib/debug/usr/lib/grub/%s-%s/" > + "kernel.exec %p -s .data %p\n", > + GRUB_TARGET_CPU, GRUB_PLATFORM, (void *)text, (void *)data); > + else > + grub_dprintf ("gdb", > + "add-symbol-file /usr/lib/debug/usr/lib/grub/%s-%s/" > + "kernel.exec %p\n", > + GRUB_TARGET_CPU, GRUB_PLATFORM, (void *)text); > +} #endif > + > void > grub_efi_init (void) > { > - grub_modbase = grub_efi_modules_addr (); > + grub_modbase = grub_efi_section_addr ("mods"); > /* First of all, initialize the console so that GRUB can display > messages. */ > grub_console_init (); > @@ -108,6 +131,7 @@ grub_efi_init (void) > efi_call_4 (grub_efi_system_table->boot_services->set_watchdog_timer, > 0, 0, 0, NULL); > #ifdef PRINT_GDB_SYM_LOAD_CMD > + grub_efi_print_gdb_info (); #endif > grub_efidisk_init (); > } > > diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h > index 83d958f99..73b754177 100644 > --- a/include/grub/efi/efi.h > +++ b/include/grub/efi/efi.h > @@ -105,7 +105,7 @@ grub_err_t grub_arch_efi_linux_boot_image(grub_addr_t addr, grub_size_t size, > char *args); > #endif > > -grub_addr_t grub_efi_modules_addr (void); > +grub_addr_t grub_efi_section_addr (const char *section); > > void grub_efi_mm_init (void); > void grub_efi_mm_fini (void); And then in config.h.in have something like: /* Define to 1 to enable printing of gdb command to load module symbols. */ #ifdef PRINT_GDB_SYM_LOAD_CMD 0 This should get rid of the need to modify the grub_real_dprintf in the previous patch. Would this satisfy you use case? Glenn