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

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