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

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