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

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