Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1nD03J-0005N2-Jq for mharc-grub-devel@gnu.org; Thu, 27 Jan 2022 03:22:48 -0500 Received: from eggs.gnu.org ([209.51.188.92]:56654) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nD03B-0005LU-H2 for grub-devel@gnu.org; Thu, 27 Jan 2022 03:22:37 -0500 Received: from [2607:f8b0:4864:20::52b] (port=36488 helo=mail-pg1-x52b.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nD02x-0004wE-Ty for grub-devel@gnu.org; Thu, 27 Jan 2022 03:22:35 -0500 Received: by mail-pg1-x52b.google.com with SMTP id e9so1663430pgb.3 for ; Thu, 27 Jan 2022 00:21:32 -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=U7QrX1Pg3q9JtHcwSWYDvC8ysrLUozLFNLdnE080rdw=; b=fKNSXSfyVRR9SNHIO73FLdxvjAuKV5WmjAl0Rns7wnhiMuhQITgz/QT3x6gDrU2hs4 v4SWs2ogsRa9gODAZTM817OdZ+iEOStxPqUwW6WgDm+VJuCRzCccF5EUm2lq4somUM5/ M6ioRmCR/B9zbh3GP4GM0KNv8nLi/1O7DNkxreC3UFkO4X0ldgpOC/swgN65jVnvWDoW vbnfhAki1yBvzuxWJedXvsswAbI9fIxcXSEmH+9vroZAPmg2lB0qPaKSRySvaylmS8y4 Dc4zdQ83ShMxW8LRPIxTH+fTT9ujxT3y0fYqvP2JB0CZwmUxY6o+2xXJ6gPUU4Pngy48 bwfQ== 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=U7QrX1Pg3q9JtHcwSWYDvC8ysrLUozLFNLdnE080rdw=; b=OQiUhBJlv3hfr/z1boIwQWsTNyIEhJ83VrLeD1lRHGxg0ycZhWtgCbb4oP+CH3IBRh atdGc6zYunUbB5JXRfUM2SxeEeJaIDhwynxujb4h5deilSfkHxQbW/xUvFd+NtAS7AvS X3+U3DjNbXXywE3APC99OSG+3LHhvhDeHd+tcWl6c8tOtssIJGdfiAN1KFjiD2nC0wQH vNfC0XxENj59H0z7lb7XZ81jJpJ+e+iCzuaxwS9toyTGrk8abxkbOYt45xXvkN1BCJ6j QiO4zJnKEfMQX/+dFBQuP+EGVL82U7HpFIXXhdXCObq0XCZoWkKetHVWTI+qojceFshj PMlQ== X-Gm-Message-State: AOAM533bbC881uqeriK8YKVNesrlmnX3MHlw0T03LZPng6VarcI/rk0m AkzQVAjNiMGhNSZ88BrsLdSBZg== X-Google-Smtp-Source: ABdhPJwV8PLrIaTBPFBxPg3DTErpsBIy9oqcONRy6LnJt3EidPzKn7DaIK1GzoLlH8bckgwvfB44Ig== X-Received: by 2002:a63:ff5d:: with SMTP id s29mr1992851pgk.368.1643271691327; Thu, 27 Jan 2022 00:21:31 -0800 (PST) Received: from crass-HP-ZBook-15-G2 (yal.riseup.net. [199.58.83.9]) by smtp.gmail.com with ESMTPSA id q12sm4919422pfk.199.2022.01.27.00.21.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jan 2022 00:21:30 -0800 (PST) Date: Thu, 27 Jan 2022 02:19:08 -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: <20220127021908.251c8fb9@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::52b (failed) Received-SPF: pass client-ip=2607:f8b0:4864:20::52b; envelope-from=development@efficientek.com; helo=mail-pg1-x52b.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: Thu, 27 Jan 2022 08:22:43 -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. > > 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; > } > +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); > +} > > /* 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); > > + grub_dl_print_gdb_info (mod, e); > + > 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; > > +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); As far as I can tell, this is an incorrect value for data. The value for the data segment that gets displayed is the location of the UEFI data segment (PE32+ format). But the location you want is of the ELF data segment in kernel.exec. The UEFI data segment is a combination of all the segments marked as data followed by bss from the ELF kernel.exec file (rodata, rodata.str1.1, data, module_license, and bss from what I'm seeing). Does your patch actually work for you? Function symbols will be fine, but any data variables should be not right (eg. grub_dl_head). Currently, GRUB doesn't have enough information to reconstruct where ELF data segments and BSS map into the PE32+ data segment. It needs the name, size and alignment of the ELF segments to figure it out. I see a couple solutions. 1. Have a function in gdb_grub that figures everything out based on the PE32+ data segment offset. I could use objdump to get the info from kernel.exec and then load the symbol file itself. It would be called like "grub_add_symbols ". 2. Have grub-mkimage insert the required data into the UEFI binary so GRUB can figure it out and print the add-symbol-file line as you're wanting to do in this patch. Perhaps it could go into a new section in the UEFI binary or patch it into a global variable (I'd prefer the former). Option 1 seems more hackish, but probably easier (I've got a working POC). Option 2 using another PE32+ section, seems like a cleaner approach, but (maybe) more work. Glenn > + 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); > +} > + > 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); > > + grub_efi_print_gdb_info (); > 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);