nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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