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

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