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