Blame SOURCES/0120-make-better-backtraces.patch

8631a2
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8631a2
From: Peter Jones <pjones@redhat.com>
8631a2
Date: Mon, 6 Nov 2017 18:31:56 -0500
8631a2
Subject: [PATCH] make better backtraces
8631a2
8631a2
Signed-off-by: Peter Jones <pjones@redhat.com>
8631a2
---
8631a2
 Makefile.util.def                       |   6 ++
8631a2
 grub-core/Makefile.core.def             |  15 ++--
8631a2
 grub-core/{lib => commands}/backtrace.c |   2 +-
8631a2
 grub-core/gdb/cstub.c                   |   1 -
8631a2
 grub-core/kern/arm64/backtrace.c        |  94 ++++++++++++++++++++++++
8631a2
 grub-core/kern/backtrace.c              |  97 +++++++++++++++++++++++++
8631a2
 grub-core/kern/dl.c                     |  45 ++++++++++++
8631a2
 grub-core/kern/i386/backtrace.c         | 125 ++++++++++++++++++++++++++++++++
8631a2
 grub-core/kern/i386/pc/init.c           |   4 +-
8631a2
 grub-core/kern/ieee1275/init.c          |   1 -
8631a2
 grub-core/kern/misc.c                   |  13 ++--
8631a2
 grub-core/kern/mm.c                     |   6 +-
8631a2
 grub-core/lib/arm64/backtrace.c         |  62 ----------------
8631a2
 grub-core/lib/i386/backtrace.c          |  78 --------------------
8631a2
 include/grub/backtrace.h                |  10 ++-
8631a2
 include/grub/dl.h                       |   2 +
8631a2
 include/grub/kernel.h                   |   3 +
8631a2
 grub-core/kern/arm/efi/startup.S        |   2 +
8631a2
 grub-core/kern/arm/startup.S            |   2 +
8631a2
 grub-core/kern/arm64/efi/startup.S      |   2 +
8631a2
 grub-core/kern/i386/qemu/startup.S      |   3 +-
8631a2
 grub-core/kern/ia64/efi/startup.S       |   3 +-
8631a2
 grub-core/kern/sparc64/ieee1275/crt0.S  |   3 +-
8631a2
 grub-core/Makefile.am                   |   1 +
8631a2
 24 files changed, 414 insertions(+), 166 deletions(-)
8631a2
 rename grub-core/{lib => commands}/backtrace.c (98%)
8631a2
 create mode 100644 grub-core/kern/arm64/backtrace.c
8631a2
 create mode 100644 grub-core/kern/backtrace.c
8631a2
 create mode 100644 grub-core/kern/i386/backtrace.c
8631a2
 delete mode 100644 grub-core/lib/arm64/backtrace.c
8631a2
 delete mode 100644 grub-core/lib/i386/backtrace.c
8631a2
8631a2
diff --git a/Makefile.util.def b/Makefile.util.def
8631a2
index f4fbd250630..cbd661d6348 100644
8631a2
--- a/Makefile.util.def
8631a2
+++ b/Makefile.util.def
8631a2
@@ -49,6 +49,12 @@ library = {
8631a2
   common = grub-core/partmap/msdos.c;
8631a2
   common = grub-core/fs/proc.c;
8631a2
   common = grub-core/fs/archelp.c;
8631a2
+  common = grub-core/kern/backtrace.c;
8631a2
+
8631a2
+  x86 = grub-core/kern/i386/backtrace.c;
8631a2
+  i386_xen = grub-core/kern/i386/backtrace.c;
8631a2
+  x86_64_xen = grub-core/kern/i386/backtrace.c;
8631a2
+  arm64 = grub-core/kern/arm64/backtrace.c;
8631a2
 };
8631a2
 
8631a2
 library = {
8631a2
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
8631a2
index 067b97a4221..cb24f92a431 100644
8631a2
--- a/grub-core/Makefile.core.def
8631a2
+++ b/grub-core/Makefile.core.def
8631a2
@@ -130,6 +130,12 @@ kernel = {
8631a2
   common = kern/rescue_reader.c;
8631a2
   common = kern/term.c;
8631a2
   common = kern/qsort.c;
8631a2
+  common = kern/backtrace.c;
8631a2
+
8631a2
+  x86 = kern/i386/backtrace.c;
8631a2
+  i386_xen = kern/i386/backtrace.c;
8631a2
+  x86_64_xen = kern/i386/backtrace.c;
8631a2
+  arm64 = kern/arm64/backtrace.c;
8631a2
 
8631a2
   noemu = kern/compiler-rt.c;
8631a2
   noemu = kern/mm.c;
8631a2
@@ -176,9 +182,6 @@ kernel = {
8631a2
 
8631a2
   softdiv = lib/division.c;
8631a2
 
8631a2
-  x86 = lib/i386/backtrace.c;
8631a2
-  x86 = lib/backtrace.c;
8631a2
-
8631a2
   i386 = kern/i386/dl.c;
8631a2
   i386_xen = kern/i386/dl.c;
8631a2
 
8631a2
@@ -2277,13 +2280,11 @@ module = {
8631a2
 
8631a2
 module = {
8631a2
   name = backtrace;
8631a2
-  x86 = lib/i386/backtrace.c;
8631a2
-  i386_xen = lib/i386/backtrace.c;
8631a2
-  x86_64_xen = lib/i386/backtrace.c;
8631a2
-  common = lib/backtrace.c;
8631a2
+  common = commands/backtrace.c;
8631a2
   enable = x86;
8631a2
   enable = i386_xen;
8631a2
   enable = x86_64_xen;
8631a2
+  enable = arm64;
8631a2
 };
8631a2
 
8631a2
 module = {
8631a2
diff --git a/grub-core/lib/backtrace.c b/grub-core/commands/backtrace.c
8631a2
similarity index 98%
8631a2
rename from grub-core/lib/backtrace.c
8631a2
rename to grub-core/commands/backtrace.c
8631a2
index c0ad6ab8be1..8b5ec3913b5 100644
8631a2
--- a/grub-core/lib/backtrace.c
8631a2
+++ b/grub-core/commands/backtrace.c
8631a2
@@ -54,7 +54,7 @@ grub_cmd_backtrace (grub_command_t cmd __attribute__ ((unused)),
8631a2
 		    int argc __attribute__ ((unused)),
8631a2
 		    char **args __attribute__ ((unused)))
8631a2
 {
8631a2
-  grub_backtrace ();
8631a2
+  grub_backtrace (1);
8631a2
   return 0;
8631a2
 }
8631a2
 
8631a2
diff --git a/grub-core/gdb/cstub.c b/grub-core/gdb/cstub.c
8631a2
index b64acd70fee..99281472d36 100644
8631a2
--- a/grub-core/gdb/cstub.c
8631a2
+++ b/grub-core/gdb/cstub.c
8631a2
@@ -215,7 +215,6 @@ grub_gdb_trap (int trap_no)
8631a2
       grub_printf ("Unhandled exception 0x%x at ", trap_no);
8631a2
       grub_backtrace_print_address ((void *) grub_gdb_regs[PC]);
8631a2
       grub_printf ("\n");
8631a2
-      grub_backtrace_pointer ((void *) grub_gdb_regs[EBP]);
8631a2
       grub_fatal ("Unhandled exception");
8631a2
     }
8631a2
 
8631a2
diff --git a/grub-core/kern/arm64/backtrace.c b/grub-core/kern/arm64/backtrace.c
8631a2
new file mode 100644
8631a2
index 00000000000..019c6fdfef2
8631a2
--- /dev/null
8631a2
+++ b/grub-core/kern/arm64/backtrace.c
8631a2
@@ -0,0 +1,94 @@
8631a2
+/*
8631a2
+ *  GRUB  --  GRand Unified Bootloader
8631a2
+ *  Copyright (C) 2009  Free Software Foundation, Inc.
8631a2
+ *
8631a2
+ *  GRUB is free software: you can redistribute it and/or modify
8631a2
+ *  it under the terms of the GNU General Public License as published by
8631a2
+ *  the Free Software Foundation, either version 3 of the License, or
8631a2
+ *  (at your option) any later version.
8631a2
+ *
8631a2
+ *  GRUB is distributed in the hope that it will be useful,
8631a2
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
8631a2
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8631a2
+ *  GNU General Public License for more details.
8631a2
+ *
8631a2
+ *  You should have received a copy of the GNU General Public License
8631a2
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
8631a2
+ */
8631a2
+
8631a2
+#include <grub/misc.h>
8631a2
+#include <grub/command.h>
8631a2
+#include <grub/err.h>
8631a2
+#include <grub/dl.h>
8631a2
+#include <grub/mm.h>
8631a2
+#include <grub/term.h>
8631a2
+#include <grub/backtrace.h>
8631a2
+
8631a2
+#define MAX_STACK_FRAME 102400
8631a2
+
8631a2
+struct fplr
8631a2
+{
8631a2
+  void *lr;
8631a2
+  struct fplr *fp;
8631a2
+};
8631a2
+
8631a2
+void
8631a2
+grub_backtrace_pointer (void *frame, unsigned int skip)
8631a2
+{
8631a2
+  unsigned int x = 0;
8631a2
+  struct fplr *fplr = (struct fplr *)frame;
8631a2
+
8631a2
+  while (fplr)
8631a2
+    {
8631a2
+      const char *name = NULL;
8631a2
+      char *addr = NULL;
8631a2
+
8631a2
+      grub_dprintf("backtrace", "fp is %p next_fp is %p\n",
8631a2
+		   fplr, fplr->fp);
8631a2
+
8631a2
+      if (x >= skip)
8631a2
+	{
8631a2
+	  name = grub_get_symbol_by_addr (fplr->lr, 1);
8631a2
+	  if (name)
8631a2
+	    addr = grub_resolve_symbol (name);
8631a2
+	  grub_backtrace_print_address (fplr->lr);
8631a2
+
8631a2
+	  if (addr && addr != fplr->lr)
8631a2
+	    grub_printf (" %s() %p+%p \n", name ? name : "unknown", addr,
8631a2
+			 (void *)((grub_uint64_t)fplr->lr - (grub_uint64_t)addr));
8631a2
+	  else
8631a2
+	    grub_printf(" %s() %p \n", name ? name : "unknown", addr);
8631a2
+
8631a2
+	}
8631a2
+
8631a2
+      x += 1;
8631a2
+
8631a2
+      if (fplr->fp < fplr ||
8631a2
+	  (grub_uint64_t)fplr->fp - (grub_uint64_t)fplr > MAX_STACK_FRAME ||
8631a2
+	  fplr->fp == fplr)
8631a2
+	{
8631a2
+	  break;
8631a2
+	}
8631a2
+      fplr = fplr->fp;
8631a2
+    }
8631a2
+}
8631a2
+
8631a2
+asm ("\t.global \"_text\"\n"
8631a2
+     "_text:\n"
8631a2
+     "\t.quad .text\n"
8631a2
+     "\t.global \"_data\"\n"
8631a2
+     "_data:\n"
8631a2
+     "\t.quad .data\n"
8631a2
+     );
8631a2
+
8631a2
+extern grub_uint64_t _text;
8631a2
+extern grub_uint64_t _data;
8631a2
+
8631a2
+void
8631a2
+grub_backtrace_arch (unsigned int skip)
8631a2
+{
8631a2
+  grub_printf ("Backtrace (.text %p .data %p):\n",
8631a2
+	       (void *)_text, (void *)_data);
8631a2
+  skip += 1;
8631a2
+  grub_backtrace_pointer(__builtin_frame_address(0), skip);
8631a2
+}
8631a2
diff --git a/grub-core/kern/backtrace.c b/grub-core/kern/backtrace.c
8631a2
new file mode 100644
8631a2
index 00000000000..4a82e865cc6
8631a2
--- /dev/null
8631a2
+++ b/grub-core/kern/backtrace.c
8631a2
@@ -0,0 +1,97 @@
8631a2
+/*
8631a2
+ *  GRUB  --  GRand Unified Bootloader
8631a2
+ *  Copyright (C) 2009  Free Software Foundation, Inc.
8631a2
+ *
8631a2
+ *  GRUB is free software: you can redistribute it and/or modify
8631a2
+ *  it under the terms of the GNU General Public License as published by
8631a2
+ *  the Free Software Foundation, either version 3 of the License, or
8631a2
+ *  (at your option) any later version.
8631a2
+ *
8631a2
+ *  GRUB is distributed in the hope that it will be useful,
8631a2
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
8631a2
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8631a2
+ *  GNU General Public License for more details.
8631a2
+ *
8631a2
+ *  You should have received a copy of the GNU General Public License
8631a2
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
8631a2
+ */
8631a2
+
8631a2
+#include <grub/misc.h>
8631a2
+#include <grub/command.h>
8631a2
+#include <grub/err.h>
8631a2
+#include <grub/dl.h>
8631a2
+#include <grub/mm.h>
8631a2
+#include <grub/term.h>
8631a2
+#include <grub/backtrace.h>
8631a2
+
8631a2
+GRUB_MOD_LICENSE ("GPLv3+");
8631a2
+
8631a2
+static void
8631a2
+grub_backtrace_print_address_default (void *addr)
8631a2
+{
8631a2
+#ifndef GRUB_UTIL
8631a2
+  grub_dl_t mod;
8631a2
+  void *start_addr;
8631a2
+
8631a2
+  FOR_DL_MODULES (mod)
8631a2
+  {
8631a2
+    grub_dl_segment_t segment;
8631a2
+    for (segment = mod->segment; segment; segment = segment->next)
8631a2
+      if (segment->addr <= addr && (grub_uint8_t *) segment->addr
8631a2
+	  + segment->size > (grub_uint8_t *) addr)
8631a2
+	{
8631a2
+	  grub_printf ("%s.%x+%" PRIxGRUB_SIZE, mod->name,
8631a2
+		       segment->section,
8631a2
+		       (grub_size_t)
8631a2
+		       ((grub_uint8_t *)addr - (grub_uint8_t *)segment->addr));
8631a2
+	  return;
8631a2
+	}
8631a2
+  }
8631a2
+
8631a2
+  start_addr = grub_resolve_symbol ("_start");
8631a2
+  if (start_addr && start_addr < addr)
8631a2
+    grub_printf ("kernel+%" PRIxGRUB_SIZE,
8631a2
+		 (grub_size_t)
8631a2
+		  ((grub_uint8_t *)addr - (grub_uint8_t *)start_addr));
8631a2
+  else
8631a2
+#endif
8631a2
+    grub_printf ("%p", addr);
8631a2
+}
8631a2
+
8631a2
+static void
8631a2
+grub_backtrace_pointer_default (void *frame __attribute__((__unused__)),
8631a2
+				unsigned int skip __attribute__((__unused__)))
8631a2
+{
8631a2
+  return;
8631a2
+}
8631a2
+
8631a2
+void
8631a2
+grub_backtrace_pointer (void *frame, unsigned int skip)
8631a2
+     __attribute__((__weak__,
8631a2
+		    __alias__(("grub_backtrace_pointer_default"))));
8631a2
+
8631a2
+void
8631a2
+grub_backtrace_print_address (void *addr)
8631a2
+     __attribute__((__weak__,
8631a2
+		    __alias__(("grub_backtrace_print_address_default"))));
8631a2
+
8631a2
+static void
8631a2
+grub_backtrace_arch_default(unsigned int skip)
8631a2
+{
8631a2
+  grub_backtrace_pointer(__builtin_frame_address(0), skip + 1);
8631a2
+}
8631a2
+
8631a2
+void grub_backtrace_arch (unsigned int skip)
8631a2
+     __attribute__((__weak__, __alias__(("grub_backtrace_arch_default"))));
8631a2
+
8631a2
+void grub_backtrace (unsigned int skip)
8631a2
+{
8631a2
+  grub_backtrace_arch(skip + 1);
8631a2
+}
8631a2
+
8631a2
+void grub_debug_backtrace (const char * const debug,
8631a2
+			   unsigned int skip)
8631a2
+{
8631a2
+  if (grub_debug_enabled (debug))
8631a2
+    grub_backtrace (skip + 1);
8631a2
+}
8631a2
diff --git a/grub-core/kern/dl.c b/grub-core/kern/dl.c
8631a2
index 621070918d4..5028d157c46 100644
8631a2
--- a/grub-core/kern/dl.c
8631a2
+++ b/grub-core/kern/dl.c
8631a2
@@ -124,6 +124,50 @@ grub_dl_resolve_symbol (const char *name)
8631a2
   return 0;
8631a2
 }
8631a2
 
8631a2
+void *
8631a2
+grub_resolve_symbol (const char *name)
8631a2
+{
8631a2
+	grub_symbol_t sym;
8631a2
+
8631a2
+	sym = grub_dl_resolve_symbol (name);
8631a2
+	if (sym)
8631a2
+		return sym->addr;
8631a2
+	return NULL;
8631a2
+}
8631a2
+
8631a2
+const char *
8631a2
+grub_get_symbol_by_addr(const void *addr, int isfunc)
8631a2
+{
8631a2
+  unsigned int i;
8631a2
+  grub_symbol_t before = NULL, after = NULL;
8631a2
+  for (i = 0; i < GRUB_SYMTAB_SIZE; i++)
8631a2
+    {
8631a2
+      grub_symbol_t sym;
8631a2
+      for (sym = grub_symtab[i]; sym; sym = sym->next)
8631a2
+	{
8631a2
+	  //grub_printf ("addr 0x%08llx symbol %s\n", (unsigned long long)sym->addr, sym->name);
8631a2
+	  if (sym->addr > addr)
8631a2
+	    {
8631a2
+	      if (!after || sym->addr > after->addr)
8631a2
+		after = sym;
8631a2
+	    }
8631a2
+
8631a2
+	  if (isfunc != sym->isfunc)
8631a2
+	    continue;
8631a2
+	  if (sym->addr > addr)
8631a2
+	    continue;
8631a2
+
8631a2
+	  if ((!before && sym->addr <= addr) || (before && before->addr <= sym->addr))
8631a2
+	    before = sym;
8631a2
+	}
8631a2
+    }
8631a2
+
8631a2
+  if (before && addr < after->addr)
8631a2
+    return before->name;
8631a2
+
8631a2
+  return NULL;
8631a2
+}
8631a2
+
8631a2
 /* Register a symbol with the name NAME and the address ADDR.  */
8631a2
 grub_err_t
8631a2
 grub_dl_register_symbol (const char *name, void *addr, int isfunc,
8631a2
@@ -336,6 +380,7 @@ grub_dl_resolve_symbols (grub_dl_t mod, Elf_Ehdr *e)
8631a2
   const char *str;
8631a2
   Elf_Word size, entsize;
8631a2
 
8631a2
+  grub_dprintf ("modules", "Resolving symbols for \"%s\"\n", mod->name);
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
diff --git a/grub-core/kern/i386/backtrace.c b/grub-core/kern/i386/backtrace.c
8631a2
new file mode 100644
8631a2
index 00000000000..2413f9a57db
8631a2
--- /dev/null
8631a2
+++ b/grub-core/kern/i386/backtrace.c
8631a2
@@ -0,0 +1,125 @@
8631a2
+/*
8631a2
+ *  GRUB  --  GRand Unified Bootloader
8631a2
+ *  Copyright (C) 2009  Free Software Foundation, Inc.
8631a2
+ *
8631a2
+ *  GRUB is free software: you can redistribute it and/or modify
8631a2
+ *  it under the terms of the GNU General Public License as published by
8631a2
+ *  the Free Software Foundation, either version 3 of the License, or
8631a2
+ *  (at your option) any later version.
8631a2
+ *
8631a2
+ *  GRUB is distributed in the hope that it will be useful,
8631a2
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
8631a2
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8631a2
+ *  GNU General Public License for more details.
8631a2
+ *
8631a2
+ *  You should have received a copy of the GNU General Public License
8631a2
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
8631a2
+ */
8631a2
+
8631a2
+#include <grub/misc.h>
8631a2
+#include <grub/command.h>
8631a2
+#include <grub/err.h>
8631a2
+#include <grub/dl.h>
8631a2
+#include <grub/mm.h>
8631a2
+#include <grub/term.h>
8631a2
+#include <grub/backtrace.h>
8631a2
+
8631a2
+#define MAX_STACK_FRAME 102400
8631a2
+
8631a2
+void
8631a2
+grub_backtrace_pointer (void *frame, unsigned int skip)
8631a2
+{
8631a2
+  void **ebp = (void **)frame;
8631a2
+  unsigned long x = 0;
8631a2
+
8631a2
+  while (ebp)
8631a2
+    {
8631a2
+      void **next_ebp = (void **)ebp[0];
8631a2
+      const char *name = NULL;
8631a2
+      char *addr = NULL;
8631a2
+
8631a2
+      grub_dprintf("backtrace", "ebp is %p next_ebp is %p\n", ebp, next_ebp);
8631a2
+
8631a2
+      if (x >= skip)
8631a2
+	{
8631a2
+	  name = grub_get_symbol_by_addr (ebp[1], 1);
8631a2
+	  if (name)
8631a2
+	    addr = grub_resolve_symbol (name);
8631a2
+	  grub_backtrace_print_address (ebp[1]);
8631a2
+
8631a2
+	  if (addr && addr != ebp[1])
8631a2
+	    grub_printf (" %s() %p+%p \n", name ? name : "unknown", addr,
8631a2
+			 (char *)((char *)ebp[1] - addr));
8631a2
+	  else
8631a2
+	    grub_printf(" %s() %p \n", name ? name : "unknown", addr);
8631a2
+
8631a2
+#if 0
8631a2
+	  grub_printf ("(");
8631a2
+	  for (i = 0, arg = ebp[2]; arg != next_ebp && i < 12; arg++, i++)
8631a2
+	    grub_printf ("%p,", arg);
8631a2
+	  grub_printf (")\n");
8631a2
+#endif
8631a2
+	}
8631a2
+
8631a2
+      x += 1;
8631a2
+
8631a2
+      if (next_ebp < ebp || next_ebp - ebp > MAX_STACK_FRAME || next_ebp == ebp)
8631a2
+	{
8631a2
+	  //grub_printf ("Invalid stack frame at %p (%p)\n", ebp, next_ebp);
8631a2
+	  break;
8631a2
+	}
8631a2
+      ebp = next_ebp;
8631a2
+    }
8631a2
+}
8631a2
+
8631a2
+#if defined (__x86_64__)
8631a2
+asm ("\t.global \"_text\"\n"
8631a2
+     "_text:\n"
8631a2
+     "\t.quad .text\n"
8631a2
+     "\t.global \"_data\"\n"
8631a2
+     "_data:\n"
8631a2
+     "\t.quad .data\n"
8631a2
+     );
8631a2
+#elif defined(__i386__)
8631a2
+asm ("\t.global \"_text\"\n"
8631a2
+     "_text:\n"
8631a2
+     "\t.long .text\n"
8631a2
+     "\t.global \"_data\"\n"
8631a2
+     "_data:\n"
8631a2
+     "\t.long .data\n"
8631a2
+     );
8631a2
+#else
8631a2
+#warning I dunno...
8631a2
+#endif
8631a2
+
8631a2
+extern unsigned long _text;
8631a2
+extern unsigned long _data;
8631a2
+
8631a2
+#ifdef GRUB_UTIL
8631a2
+#define EXT_C(x) x
8631a2
+#endif
8631a2
+
8631a2
+void
8631a2
+grub_backtrace_arch (unsigned int skip)
8631a2
+{
8631a2
+  grub_printf ("Backtrace (.text %p .data %p):\n",
8631a2
+	       (void *)_text, (void *)_data);
8631a2
+  skip += 1;
8631a2
+#if defined (__x86_64__)
8631a2
+  asm volatile ("movq %%rbp, %%rdi\n"
8631a2
+		"movq 0, %%rsi\n"
8631a2
+		"movl %0, %%esi\n"
8631a2
+		"call " EXT_C("grub_backtrace_pointer")
8631a2
+		:
8631a2
+		: "r" (skip));
8631a2
+#elif defined(__i386__)
8631a2
+  asm volatile ("addl $8, %%esp\n"
8631a2
+		"pushl %0\n"
8631a2
+		"pushl %%ebp\n"
8631a2
+		"call " EXT_C("grub_backtrace_pointer")
8631a2
+		:
8631a2
+		: "r" (skip));
8631a2
+#else
8631a2
+  grub_backtrace_pointer(__builtin_frame_address(0), skip);
8631a2
+#endif
8631a2
+}
8631a2
diff --git a/grub-core/kern/i386/pc/init.c b/grub-core/kern/i386/pc/init.c
8631a2
index 27bc68b8a53..b51d0abfa6e 100644
8631a2
--- a/grub-core/kern/i386/pc/init.c
8631a2
+++ b/grub-core/kern/i386/pc/init.c
8631a2
@@ -153,7 +153,7 @@ compact_mem_regions (void)
8631a2
 }
8631a2
 
8631a2
 grub_addr_t grub_modbase;
8631a2
-extern grub_uint8_t _start[], _edata[];
8631a2
+extern grub_uint8_t _edata[];
8631a2
 
8631a2
 /* Helper for grub_machine_init.  */
8631a2
 static int
8631a2
@@ -217,7 +217,7 @@ grub_machine_init (void)
8631a2
   /* This has to happen before any BIOS calls. */
8631a2
   grub_via_workaround_init ();
8631a2
 
8631a2
-  grub_modbase = GRUB_MEMORY_MACHINE_DECOMPRESSION_ADDR + (_edata - _start);
8631a2
+  grub_modbase = GRUB_MEMORY_MACHINE_DECOMPRESSION_ADDR + (_edata - (grub_uint8_t *)_start);
8631a2
 
8631a2
   /* Initialize the console as early as possible.  */
8631a2
   grub_console_init ();
8631a2
diff --git a/grub-core/kern/ieee1275/init.c b/grub-core/kern/ieee1275/init.c
8631a2
index e01bc6eab19..e731a57a47b 100644
8631a2
--- a/grub-core/kern/ieee1275/init.c
8631a2
+++ b/grub-core/kern/ieee1275/init.c
8631a2
@@ -60,7 +60,6 @@
8631a2
 #define HEAP_MAX_ADDR		(unsigned long) (32 * 1024 * 1024)
8631a2
 #endif
8631a2
 
8631a2
-extern char _start[];
8631a2
 extern char _end[];
8631a2
 
8631a2
 #ifdef __sparc__
8631a2
diff --git a/grub-core/kern/misc.c b/grub-core/kern/misc.c
8631a2
index e758ab3416d..5c2d2039d0b 100644
8631a2
--- a/grub-core/kern/misc.c
8631a2
+++ b/grub-core/kern/misc.c
8631a2
@@ -1110,15 +1110,15 @@ grub_xasprintf (const char *fmt, ...)
8631a2
 }
8631a2
 
8631a2
 /* Abort GRUB. This function does not return.  */
8631a2
-static void __attribute__ ((noreturn))
8631a2
+static inline void __attribute__ ((noreturn))
8631a2
 grub_abort (void)
8631a2
 {
8631a2
-#ifndef GRUB_UTIL
8631a2
-#if (defined(__i386__) || defined(__x86_64__)) && !defined(GRUB_MACHINE_EMU)
8631a2
-  grub_backtrace();
8631a2
+#if !defined(GRUB_MACHINE_EMU) && !defined(GRUB_UTIL)
8631a2
+  grub_backtrace (1);
8631a2
+#else
8631a2
+  grub_printf ("\n");
8631a2
 #endif
8631a2
-#endif
8631a2
-  grub_printf ("\nAborted.");
8631a2
+  grub_printf ("Aborted.");
8631a2
 
8631a2
 #ifndef GRUB_UTIL
8631a2
   if (grub_term_inputs)
8631a2
@@ -1145,6 +1145,7 @@ grub_fatal (const char *fmt, ...)
8631a2
 {
8631a2
   va_list ap;
8631a2
 
8631a2
+  grub_printf ("\n");
8631a2
   va_start (ap, fmt);
8631a2
   grub_vprintf (_(fmt), ap);
8631a2
   va_end (ap);
8631a2
diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c
8631a2
index ee88ff61187..002cbfa4f3d 100644
8631a2
--- a/grub-core/kern/mm.c
8631a2
+++ b/grub-core/kern/mm.c
8631a2
@@ -95,13 +95,13 @@ get_header_from_pointer (void *ptr, grub_mm_header_t *p, grub_mm_region_t *r)
8631a2
       break;
8631a2
 
8631a2
   if (! *r)
8631a2
-    grub_fatal ("out of range pointer %p", ptr);
8631a2
+    grub_fatal ("out of range pointer %p\n", ptr);
8631a2
 
8631a2
   *p = (grub_mm_header_t) ptr - 1;
8631a2
   if ((*p)->magic == GRUB_MM_FREE_MAGIC)
8631a2
-    grub_fatal ("double free at %p", *p);
8631a2
+    grub_fatal ("double free at %p\n", *p);
8631a2
   if ((*p)->magic != GRUB_MM_ALLOC_MAGIC)
8631a2
-    grub_fatal ("alloc magic is broken at %p: %lx", *p,
8631a2
+    grub_fatal ("alloc magic is broken at %p: %lx\n", *p,
8631a2
 		(unsigned long) (*p)->magic);
8631a2
 }
8631a2
 
8631a2
diff --git a/grub-core/lib/arm64/backtrace.c b/grub-core/lib/arm64/backtrace.c
8631a2
deleted file mode 100644
8631a2
index 1079b5380e1..00000000000
8631a2
--- a/grub-core/lib/arm64/backtrace.c
8631a2
+++ /dev/null
8631a2
@@ -1,62 +0,0 @@
8631a2
-/*
8631a2
- *  GRUB  --  GRand Unified Bootloader
8631a2
- *  Copyright (C) 2009  Free Software Foundation, Inc.
8631a2
- *
8631a2
- *  GRUB is free software: you can redistribute it and/or modify
8631a2
- *  it under the terms of the GNU General Public License as published by
8631a2
- *  the Free Software Foundation, either version 3 of the License, or
8631a2
- *  (at your option) any later version.
8631a2
- *
8631a2
- *  GRUB is distributed in the hope that it will be useful,
8631a2
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
8631a2
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8631a2
- *  GNU General Public License for more details.
8631a2
- *
8631a2
- *  You should have received a copy of the GNU General Public License
8631a2
- *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
8631a2
- */
8631a2
-
8631a2
-#include <grub/misc.h>
8631a2
-#include <grub/command.h>
8631a2
-#include <grub/err.h>
8631a2
-#include <grub/dl.h>
8631a2
-#include <grub/mm.h>
8631a2
-#include <grub/term.h>
8631a2
-#include <grub/backtrace.h>
8631a2
-
8631a2
-#define MAX_STACK_FRAME 102400
8631a2
-
8631a2
-void
8631a2
-grub_backtrace_pointer (int frame)
8631a2
-{
8631a2
-  while (1)
8631a2
-    {
8631a2
-      void *lp = __builtin_return_address (frame);
8631a2
-      if (!lp)
8631a2
-	break;
8631a2
-
8631a2
-      lp = __builtin_extract_return_addr (lp);
8631a2
-
8631a2
-      grub_printf ("%p: ", lp);
8631a2
-      grub_backtrace_print_address (lp);
8631a2
-      grub_printf (" (");
8631a2
-      for (i = 0; i < 2; i++)
8631a2
-	grub_printf ("%p,", ((void **)ptr) [i + 2]);
8631a2
-      grub_printf ("%p)\n", ((void **)ptr) [i + 2]);
8631a2
-      nptr = *(void **)ptr;
8631a2
-      if (nptr < ptr || (void **) nptr - (void **) ptr > MAX_STACK_FRAME
8631a2
-	  || nptr == ptr)
8631a2
-	{
8631a2
-	  grub_printf ("Invalid stack frame at %p (%p)\n", ptr, nptr);
8631a2
-	  break;
8631a2
-	}
8631a2
-      ptr = nptr;
8631a2
-    }
8631a2
-}
8631a2
-
8631a2
-void
8631a2
-grub_backtrace (void)
8631a2
-{
8631a2
-  grub_backtrace_pointer (1);
8631a2
-}
8631a2
-
8631a2
diff --git a/grub-core/lib/i386/backtrace.c b/grub-core/lib/i386/backtrace.c
8631a2
deleted file mode 100644
8631a2
index c67273db3ae..00000000000
8631a2
--- a/grub-core/lib/i386/backtrace.c
8631a2
+++ /dev/null
8631a2
@@ -1,78 +0,0 @@
8631a2
-/*
8631a2
- *  GRUB  --  GRand Unified Bootloader
8631a2
- *  Copyright (C) 2009  Free Software Foundation, Inc.
8631a2
- *
8631a2
- *  GRUB is free software: you can redistribute it and/or modify
8631a2
- *  it under the terms of the GNU General Public License as published by
8631a2
- *  the Free Software Foundation, either version 3 of the License, or
8631a2
- *  (at your option) any later version.
8631a2
- *
8631a2
- *  GRUB is distributed in the hope that it will be useful,
8631a2
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
8631a2
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8631a2
- *  GNU General Public License for more details.
8631a2
- *
8631a2
- *  You should have received a copy of the GNU General Public License
8631a2
- *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
8631a2
- */
8631a2
-#include <config.h>
8631a2
-#ifdef GRUB_UTIL
8631a2
-#define REALLY_GRUB_UTIL GRUB_UTIL
8631a2
-#undef GRUB_UTIL
8631a2
-#endif
8631a2
-
8631a2
-#include <grub/symbol.h>
8631a2
-#include <grub/dl.h>
8631a2
-
8631a2
-#ifdef REALLY_GRUB_UTIL
8631a2
-#define GRUB_UTIL REALLY_GRUB_UTIL
8631a2
-#undef REALLY_GRUB_UTIL
8631a2
-#endif
8631a2
-
8631a2
-#include <grub/misc.h>
8631a2
-#include <grub/command.h>
8631a2
-#include <grub/err.h>
8631a2
-#include <grub/mm.h>
8631a2
-#include <grub/term.h>
8631a2
-#include <grub/backtrace.h>
8631a2
-
8631a2
-#define MAX_STACK_FRAME 102400
8631a2
-
8631a2
-void
8631a2
-grub_backtrace_pointer (void *ebp)
8631a2
-{
8631a2
-  void *ptr, *nptr;
8631a2
-  unsigned i;
8631a2
-
8631a2
-  ptr = ebp;
8631a2
-  while (1)
8631a2
-    {
8631a2
-      grub_printf ("%p: ", ptr);
8631a2
-      grub_backtrace_print_address (((void **) ptr)[1]);
8631a2
-      grub_printf (" (");
8631a2
-      for (i = 0; i < 2; i++)
8631a2
-	grub_printf ("%p,", ((void **)ptr) [i + 2]);
8631a2
-      grub_printf ("%p)\n", ((void **)ptr) [i + 2]);
8631a2
-      nptr = *(void **)ptr;
8631a2
-      if (nptr < ptr || (void **) nptr - (void **) ptr > MAX_STACK_FRAME
8631a2
-	  || nptr == ptr)
8631a2
-	{
8631a2
-	  grub_printf ("Invalid stack frame at %p (%p)\n", ptr, nptr);
8631a2
-	  break;
8631a2
-	}
8631a2
-      ptr = nptr;
8631a2
-    }
8631a2
-}
8631a2
-
8631a2
-void
8631a2
-grub_backtrace (void)
8631a2
-{
8631a2
-#ifdef __x86_64__
8631a2
-  asm volatile ("movq %%rbp, %%rdi\n"
8631a2
-		"callq *%%rax": :"a"(grub_backtrace_pointer));
8631a2
-#else
8631a2
-  asm volatile ("movl %%ebp, %%eax\n"
8631a2
-		"calll *%%ecx": :"c"(grub_backtrace_pointer));
8631a2
-#endif
8631a2
-}
8631a2
-
8631a2
diff --git a/include/grub/backtrace.h b/include/grub/backtrace.h
8631a2
index 395519762f0..275cf85e2d3 100644
8631a2
--- a/include/grub/backtrace.h
8631a2
+++ b/include/grub/backtrace.h
8631a2
@@ -19,8 +19,14 @@
8631a2
 #ifndef GRUB_BACKTRACE_HEADER
8631a2
 #define GRUB_BACKTRACE_HEADER	1
8631a2
 
8631a2
-void grub_backtrace (void);
8631a2
-void grub_backtrace_pointer (void *ptr);
8631a2
+#include <grub/symbol.h>
8631a2
+#include <grub/types.h>
8631a2
+
8631a2
+void EXPORT_FUNC(grub_debug_backtrace) (const char * const debug,
8631a2
+					unsigned int skip);
8631a2
+void EXPORT_FUNC(grub_backtrace) (unsigned int skip);
8631a2
+void grub_backtrace_arch (unsigned int skip);
8631a2
+void grub_backtrace_pointer (void *ptr, unsigned int skip);
8631a2
 void grub_backtrace_print_address (void *addr);
8631a2
 
8631a2
 #endif
8631a2
diff --git a/include/grub/dl.h b/include/grub/dl.h
8631a2
index b1ed3c33317..7b5bfb07ce6 100644
8631a2
--- a/include/grub/dl.h
8631a2
+++ b/include/grub/dl.h
8631a2
@@ -244,6 +244,8 @@ grub_dl_get (const char *name)
8631a2
 
8631a2
 #endif
8631a2
 
8631a2
+void * EXPORT_FUNC(grub_resolve_symbol) (const char *name);
8631a2
+const char * EXPORT_FUNC(grub_get_symbol_by_addr) (const void *addr, int isfunc);
8631a2
 grub_err_t grub_dl_register_symbol (const char *name, void *addr,
8631a2
 				    int isfunc, grub_dl_t mod);
8631a2
 
8631a2
diff --git a/include/grub/kernel.h b/include/grub/kernel.h
8631a2
index ecd88ca72c6..ae69218af20 100644
8631a2
--- a/include/grub/kernel.h
8631a2
+++ b/include/grub/kernel.h
8631a2
@@ -108,6 +108,9 @@ grub_addr_t grub_modules_get_end (void);
8631a2
 
8631a2
 #endif
8631a2
 
8631a2
+void EXPORT_FUNC(start) (void);
8631a2
+void EXPORT_FUNC(_start) (void);
8631a2
+
8631a2
 /* The start point of the C code.  */
8631a2
 void grub_main (void) __attribute__ ((noreturn));
8631a2
 
8631a2
diff --git a/grub-core/kern/arm/efi/startup.S b/grub-core/kern/arm/efi/startup.S
8631a2
index 9f8265315a9..f3bc41f9d0f 100644
8631a2
--- a/grub-core/kern/arm/efi/startup.S
8631a2
+++ b/grub-core/kern/arm/efi/startup.S
8631a2
@@ -23,6 +23,8 @@
8631a2
 	.file 	"startup.S"
8631a2
 	.text
8631a2
 	.arm
8631a2
+	.globl	start, _start
8631a2
+FUNCTION(start)
8631a2
 FUNCTION(_start)
8631a2
 	/*
8631a2
 	 *  EFI_SYSTEM_TABLE and EFI_HANDLE are passed in r1/r0.
8631a2
diff --git a/grub-core/kern/arm/startup.S b/grub-core/kern/arm/startup.S
8631a2
index 3946fe8e183..5679a1d00ad 100644
8631a2
--- a/grub-core/kern/arm/startup.S
8631a2
+++ b/grub-core/kern/arm/startup.S
8631a2
@@ -48,6 +48,8 @@
8631a2
 	
8631a2
 	.text
8631a2
 	.arm
8631a2
+	.globl	start, _start
8631a2
+FUNCTION(start)
8631a2
 FUNCTION(_start)
8631a2
 	b	codestart
8631a2
 	
8631a2
diff --git a/grub-core/kern/arm64/efi/startup.S b/grub-core/kern/arm64/efi/startup.S
8631a2
index 666a7ee3c92..41676bdb2b8 100644
8631a2
--- a/grub-core/kern/arm64/efi/startup.S
8631a2
+++ b/grub-core/kern/arm64/efi/startup.S
8631a2
@@ -19,7 +19,9 @@
8631a2
 #include <grub/symbol.h>
8631a2
 
8631a2
 	.file 	"startup.S"
8631a2
+	.globl start, _start
8631a2
 	.text
8631a2
+FUNCTION(start)
8631a2
 FUNCTION(_start)
8631a2
 	/*
8631a2
 	 *  EFI_SYSTEM_TABLE and EFI_HANDLE are passed in x1/x0.
8631a2
diff --git a/grub-core/kern/i386/qemu/startup.S b/grub-core/kern/i386/qemu/startup.S
8631a2
index 0d89858d9b3..939f182fc74 100644
8631a2
--- a/grub-core/kern/i386/qemu/startup.S
8631a2
+++ b/grub-core/kern/i386/qemu/startup.S
8631a2
@@ -24,7 +24,8 @@
8631a2
 
8631a2
 	.text
8631a2
 	.code32
8631a2
-	.globl _start
8631a2
+	.globl start, _start
8631a2
+start:
8631a2
 _start:
8631a2
 	jmp	codestart
8631a2
 
8631a2
diff --git a/grub-core/kern/ia64/efi/startup.S b/grub-core/kern/ia64/efi/startup.S
8631a2
index d75c6d7cc74..8f2a593e529 100644
8631a2
--- a/grub-core/kern/ia64/efi/startup.S
8631a2
+++ b/grub-core/kern/ia64/efi/startup.S
8631a2
@@ -24,8 +24,9 @@
8631a2
 	.psr lsb
8631a2
 	.lsb
8631a2
 
8631a2
-	.global _start
8631a2
+	.global start, _start
8631a2
 	.proc _start
8631a2
+start:
8631a2
 _start:
8631a2
 	alloc loc0=ar.pfs,2,4,0,0
8631a2
 	mov loc1=rp
8631a2
diff --git a/grub-core/kern/sparc64/ieee1275/crt0.S b/grub-core/kern/sparc64/ieee1275/crt0.S
8631a2
index 03b916f0534..701bf63abcf 100644
8631a2
--- a/grub-core/kern/sparc64/ieee1275/crt0.S
8631a2
+++ b/grub-core/kern/sparc64/ieee1275/crt0.S
8631a2
@@ -22,7 +22,8 @@
8631a2
 
8631a2
 	.text
8631a2
 	.align	4
8631a2
-	.globl	_start
8631a2
+	.globl	start, _start
8631a2
+start:
8631a2
 _start:
8631a2
 	ba	codestart
8631a2
 	 mov  %o4, %o0
8631a2
diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
8631a2
index 0108c0d4233..f36200bd688 100644
8631a2
--- a/grub-core/Makefile.am
8631a2
+++ b/grub-core/Makefile.am
8631a2
@@ -66,6 +66,7 @@ CLEANFILES += grub_script.yy.c grub_script.yy.h
8631a2
 
8631a2
 include $(srcdir)/Makefile.core.am
8631a2
 
8631a2
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/backtrace.h
8631a2
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/cache.h
8631a2
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/command.h
8631a2
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/device.h