Blame SOURCES/0125-Make-a-gdb-dprintf-that-tells-us-load-addresses.patch

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