15a207
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
15a207
From: Peter Jones <pjones@redhat.com>
15a207
Date: Thu, 25 Jun 2015 15:11:36 -0400
15a207
Subject: [PATCH] Make a "gdb" dprintf that tells us load addresses.
15a207
15a207
This makes a grub_dprintf() call during platform init and during module
15a207
loading that tells us the virtual addresses of the .text and .data
15a207
sections of grub-core/kernel.exec and any modules it loads.
15a207
15a207
Specifically, it displays them in the gdb "add-symbol-file" syntax, with
15a207
the presumption that there's a variable $grubdir that reflects the path
15a207
to any such binaries.
15a207
15a207
Signed-off-by: Peter Jones <pjones@redhat.com>
15a207
---
15a207
 grub-core/kern/dl.c       | 50 +++++++++++++++++++++++++++++++++++++++++++++++
15a207
 grub-core/kern/efi/efi.c  |  4 ++--
15a207
 grub-core/kern/efi/init.c | 26 +++++++++++++++++++++++-
15a207
 include/grub/efi/efi.h    |  2 +-
15a207
 4 files changed, 78 insertions(+), 4 deletions(-)
15a207
15a207
diff --git a/grub-core/kern/dl.c b/grub-core/kern/dl.c
15a207
index 5028d157c46..eb8b969cded 100644
15a207
--- a/grub-core/kern/dl.c
15a207
+++ b/grub-core/kern/dl.c
15a207
@@ -501,6 +501,23 @@ grub_dl_find_section (Elf_Ehdr *e, const char *name)
15a207
       return s;
15a207
   return NULL;
15a207
 }
15a207
+static long
15a207
+grub_dl_find_section_index (Elf_Ehdr *e, const char *name)
15a207
+{
15a207
+  Elf_Shdr *s;
15a207
+  const char *str;
15a207
+  unsigned i;
15a207
+
15a207
+  s = (Elf_Shdr *) ((char *) e + e->e_shoff + e->e_shstrndx * e->e_shentsize);
15a207
+  str = (char *) e + s->sh_offset;
15a207
+
15a207
+  for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
15a207
+       i < e->e_shnum;
15a207
+       i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
15a207
+    if (grub_strcmp (str + s->sh_name, name) == 0)
15a207
+      return (long)i;
15a207
+  return -1;
15a207
+}
15a207
 
15a207
 /* Me, Vladimir Serbinenko, hereby I add this module check as per new
15a207
    GNU module policy. Note that this license check is informative only.
15a207
@@ -644,6 +661,37 @@ grub_dl_relocate_symbols (grub_dl_t mod, void *ehdr)
15a207
 
15a207
   return GRUB_ERR_NONE;
15a207
 }
15a207
+static void
15a207
+grub_dl_print_gdb_info (grub_dl_t mod, Elf_Ehdr *e)
15a207
+{
15a207
+  void *text, *data = NULL;
15a207
+  long idx;
15a207
+
15a207
+  idx = grub_dl_find_section_index (e, ".text");
15a207
+  if (idx < 0)
15a207
+    return;
15a207
+
15a207
+  text = grub_dl_get_section_addr (mod, idx);
15a207
+  if (!text)
15a207
+    return;
15a207
+
15a207
+  idx = grub_dl_find_section_index (e, ".data");
15a207
+  if (idx >= 0)
15a207
+    data = grub_dl_get_section_addr (mod, idx);
15a207
+
15a207
+  if (data)
15a207
+    grub_qdprintf ("gdb", "add-symbol-file \\\n"
15a207
+		          "/usr/lib/debug/usr/lib/grub/%s-%s/%s.debug "
15a207
+			  "\\\n %p -s .data %p\n",
15a207
+		  GRUB_TARGET_CPU, GRUB_PLATFORM,
15a207
+		  mod->name, text, data);
15a207
+  else
15a207
+    grub_qdprintf ("gdb", "add-symbol-file \\\n"
15a207
+			   "/usr/lib/debug/usr/lib/grub/%s-%s/%s.debug "
15a207
+			   "\\\n%p\n",
15a207
+		  GRUB_TARGET_CPU, GRUB_PLATFORM,
15a207
+		  mod->name, text);
15a207
+}
15a207
 
15a207
 /* Load a module from core memory.  */
15a207
 grub_dl_t
15a207
@@ -703,6 +751,8 @@ grub_dl_load_core_noinit (void *addr, grub_size_t size)
15a207
   grub_dprintf ("modules", "module name: %s\n", mod->name);
15a207
   grub_dprintf ("modules", "init function: %p\n", mod->init);
15a207
 
15a207
+  grub_dl_print_gdb_info (mod, e);
15a207
+
15a207
   if (grub_dl_add (mod))
15a207
     {
15a207
       grub_dl_unload (mod);
15a207
diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c
15a207
index bcae7f4699d..a2a732ffc0d 100644
15a207
--- a/grub-core/kern/efi/efi.c
15a207
+++ b/grub-core/kern/efi/efi.c
15a207
@@ -283,7 +283,7 @@ grub_efi_get_variable (const char *var, const grub_efi_guid_t *guid,
15a207
 /* Search the mods section from the PE32/PE32+ image. This code uses
15a207
    a PE32 header, but should work with PE32+ as well.  */
15a207
 grub_addr_t
15a207
-grub_efi_modules_addr (void)
15a207
+grub_efi_section_addr (const char *section_name)
15a207
 {
15a207
   grub_efi_loaded_image_t *image;
15a207
   struct grub_pe32_header *header;
15a207
@@ -308,7 +308,7 @@ grub_efi_modules_addr (void)
15a207
        i < coff_header->num_sections;
15a207
        i++, section++)
15a207
     {
15a207
-      if (grub_strcmp (section->name, "mods") == 0)
15a207
+      if (grub_strcmp (section->name, section_name) == 0)
15a207
 	break;
15a207
     }
15a207
 
15a207
diff --git a/grub-core/kern/efi/init.c b/grub-core/kern/efi/init.c
15a207
index 71d2279a0c1..e6183a4c44d 100644
15a207
--- a/grub-core/kern/efi/init.c
15a207
+++ b/grub-core/kern/efi/init.c
15a207
@@ -59,10 +59,33 @@ grub_efi_env_init (void)
15a207
   grub_free (envblk_s.buf);
15a207
 }
15a207
 
15a207
+static void
15a207
+grub_efi_print_gdb_info (void)
15a207
+{
15a207
+  grub_addr_t text;
15a207
+  grub_addr_t data;
15a207
+
15a207
+  text = grub_efi_section_addr (".text");
15a207
+  if (!text)
15a207
+    return;
15a207
+
15a207
+  data = grub_efi_section_addr (".data");
15a207
+  if (data)
15a207
+    grub_qdprintf ("gdb",
15a207
+		  "add-symbol-file /usr/lib/debug/usr/lib/grub/%s-%s/"
15a207
+		  "kernel.exec %p -s .data %p\n",
15a207
+		  GRUB_TARGET_CPU, GRUB_PLATFORM, (void *)text, (void *)data);
15a207
+  else
15a207
+    grub_qdprintf ("gdb",
15a207
+		  "add-symbol-file /usr/lib/debug/usr/lib/grub/%s-%s/"
15a207
+		  "kernel.exec %p\n",
15a207
+		  GRUB_TARGET_CPU, GRUB_PLATFORM, (void *)text);
15a207
+}
15a207
+
15a207
 void
15a207
 grub_efi_init (void)
15a207
 {
15a207
-  grub_modbase = grub_efi_modules_addr ();
15a207
+  grub_modbase = grub_efi_section_addr ("mods");
15a207
   /* First of all, initialize the console so that GRUB can display
15a207
      messages.  */
15a207
   grub_console_init ();
15a207
@@ -74,6 +97,7 @@ grub_efi_init (void)
15a207
 	      0, 0, 0, NULL);
15a207
 
15a207
   grub_efi_env_init ();
15a207
+  grub_efi_print_gdb_info ();
15a207
   grub_efidisk_init ();
15a207
 }
15a207
 
15a207
diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h
dbfd2e
index 09a18e56302..570a69361a5 100644
15a207
--- a/include/grub/efi/efi.h
15a207
+++ b/include/grub/efi/efi.h
dbfd2e
@@ -132,7 +132,7 @@ grub_err_t grub_armxx_efi_linux_check_image(struct linux_armxx_kernel_header *lh
dbfd2e
 grub_err_t grub_armxx_efi_linux_boot_image(grub_addr_t addr, char *args);
15a207
 #endif
15a207
 
15a207
-grub_addr_t grub_efi_modules_addr (void);
15a207
+grub_addr_t grub_efi_section_addr (const char *section);
15a207
 
15a207
 void grub_efi_mm_init (void);
15a207
 void grub_efi_mm_fini (void);