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

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