Skip to contentSkip to conversation

ARM64: add symbol name lookup and print a backtrace on exception

PATCH New Caleb Connolly 1 participant 4 messages Aug 8, 2024

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover ARM64: add symbol name lookup and print a backtrace on exception

U-Boot already emits frame pointers on ARM64, but lacks the code to parse them, as well as a mechanism for looking up symbol names at runtime.

There was some (seemingly?) leftover code for symbols lookups in common/kallsyms.c and associated parts in the makefile, however it appears to be entirely unused and unsupported. It relied on generating one long string of all symbol addresses and names.

The approach taken here is instead largely based on the implementation in the Xen hypervisor, it performs basic compression using non-ASCII bytes to tokenize repeated string segments which can later be expanded back out at runtime.

This is then utilized in the ARM64 interrupt handling routine to dump a backtrace in the show_regs() debug function. As well as providing a general purpose unwind_stack() function which can be used for debugging.

== Relocation ==

Since U-Boot relocates itself at runtime, and can be built to be position independent in the first place (effectively "relocating" itself when it first starts too), we can't really rely on gd->reloc_off.

The approach taken here is to subtract CONFIG_TEXT_BASE from the address of each symbol in the lookup table (while it's being generated), then when decoding we just subtract the address of the _start label. Since this label address is updated to make U-Boot position independent and during relocation, it allows us to avoid re-implementing the relocation state handling stuff in the symbol decoder.

== Size ==

By default this feature is off, and will not effect the size of U-Boot binaries. The generated symbols object file is ~85k with the (fairly hefty) qcom_defconfig, so there is certainly a cost to be taken into account.

I hope that this implementation can be later extended for other platforms. However this is currently beyond my (skill, time) capabilities.

---
Changes in v2:
- Remove unused last_fp pointer
- Apply Tom's suggestions (cleanup hunks, don't guard unwind_stack()).
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Caleb Connolly (3):
drop unused kallsyms support
add support for symbol lookups
arm64: unwind stack on exception

 Makefile                      |  24 +-
 arch/arm/include/asm/ptrace.h |   2 +
 arch/arm/lib/interrupts_64.c  |  75 +++++
 common/Makefile               |   1 -
 common/kallsyms.c             |  43 ---
 common/system_map.c           |   8 -
 include/symbols.h             |  19 ++
 lib/Kconfig                   |   8 +
 lib/symbols.c                 | 126 ++++++++
 tools/Makefile                |   3 +
 tools/symbols.c               | 646 ++++++++++++++++++++++++++++++++++++++++++
 11 files changed, 892 insertions(+), 63 deletions(-)

---
change-id: 20240710-arm64-backtrace-2926f764dbdc
base-commit: 13f9c5668411aa18ef64846d5bc86e9e6be52082

// Caleb (they/them)

#1 lore raw

PATCH v2 1/3 drop unused kallsyms support

There was some very old code floating around for decoding symbols, it's incomplete and is never built. Remove it so we can add a new implementation.

---
 Makefile            | 11 -----------
 common/Makefile     |  1 -
 common/kallsyms.c   | 43 -------------------------------------------
 common/system_map.c |  8 --------
 4 files changed, 63 deletions(-)
Makefile +0 -11
index f5b2512f3690..fcb9565d2fff 100644
@@ -1786,21 +1786,10 @@ quiet_cmd_u-boot__ ?= LD      $@
 		$(PLATFORM_LIBS) -Map u-boot.map;				\
 		$(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true)
 endif
 
-quiet_cmd_smap = GEN     common/system_map.o
-cmd_smap = \
-	smap=`$(call SYSTEM_MAP,u-boot) | \
-		awk '$$2 ~ /[tTwW]/ {printf $$1 $$3 "\\\\000"}'` ; \
-	$(CC) $(c_flags) -DSYSTEM_MAP="\"$${smap}\"" \
-		-c $(srctree)/common/system_map.c -o common/system_map.o
-
 u-boot:	$(u-boot-init) $(u-boot-main) $(u-boot-keep-syms-lto) u-boot.lds FORCE
 	+$(call if_changed,u-boot__)
-ifeq ($(CONFIG_KALLSYMS),y)
-	$(call cmd,smap)
-	$(call cmd,u-boot__) common/system_map.o
-endif
 
 ifeq ($(CONFIG_RISCV),y)
 	@tools/prelink-riscv $@
 endif
common/Makefile +0 -1
index e98354734206..bca02e4c362f 100644
@@ -34,9 +34,8 @@ obj-$(CONFIG_USB_ONBOARD_HUB) += usb_onboard_hub.o
 obj-$(CONFIG_CONSOLE_MUX) += iomux.o
 obj-$(CONFIG_MTD_NOR_FLASH) += flash.o
 obj-$(CONFIG_CMD_KGDB) += kgdb.o kgdb_stubs.o
 obj-$(CONFIG_I2C_EDID) += edid.o
-obj-$(CONFIG_KALLSYMS) += kallsyms.o
 obj-y += splash.o
 obj-$(CONFIG_SPLASH_SOURCE) += splash_source.o
 obj-$(CONFIG_MENU) += menu.o
 obj-$(CONFIG_UPDATE_COMMON) += update.o
common/kallsyms.c deleted +0 -43

43 lines not on this page · raw message

common/system_map.c deleted +0 -8
deleted file mode 100644
index 8307293bf3ae..000000000000
@@ -1,8 +0,0 @@
-/*
- * The builtin symbol table for use with kallsyms
- *
- * Copyright (c) 2008-2009 Analog Devices Inc.
- * Licensed under the GPL-2 or later.
- */
-
-const char const system_map[] = SYSTEM_MAP;
Signature · 1 line
2.46.0
#2 lore raw

PATCH v2 2/3 add support for symbol lookups

In reply to Caleb Connolly

This is mostly a port of the Xen hypervisor implementation. The U-Boot binary is built as normal, then its symbol table is fed into tools/symbols to generate an optimised lookup table. U-Boot is rebuilt with the symbol table and handling code in lib/symbols.c.

Based on code from Xen at
c20850540ad6("x86/altcall: always use a temporary parameter stashing variable")

---
 Makefile          |  15 +-
 include/symbols.h |  19 ++
 lib/Kconfig       |   8 +
 lib/symbols.c     | 126 +++++++++++
 tools/Makefile    |   3 +
 tools/symbols.c   | 646 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 816 insertions(+), 1 deletion(-)
Makefile +14 -1
index fcb9565d2fff..52a11d586d43 100644
@@ -1781,16 +1781,29 @@ quiet_cmd_u-boot__ ?= LD      $@
       cmd_u-boot__ ?= $(LD) $(KBUILD_LDFLAGS) $(LDFLAGS_u-boot) -o $@		\
 		-T u-boot.lds $(u-boot-init)					\
 		--whole-archive							\
 			$(u-boot-main)						\
+			$(if $(shell [ "$@" = "u-boot" ] && echo "true"),lib/symbols.o u-boot.sym.o,)	\
 		--no-whole-archive						\
 		$(PLATFORM_LIBS) -Map u-boot.map;				\
 		$(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true)
 endif
 
-u-boot:	$(u-boot-init) $(u-boot-main) $(u-boot-keep-syms-lto) u-boot.lds FORCE
+u-boot.nosyms:	$(u-boot-init) $(u-boot-main) $(u-boot-keep-syms-lto) u-boot.lds FORCE
 	+$(call if_changed,u-boot__)
 
+ifeq ($(CONFIG_SYMBOL_LOOKUP),y)
+u-boot: u-boot.nosyms FORCE
+	@$(NM) -n -pa --format=sysv u-boot.nosyms | tools/symbols --all-symbols --sysv --sort > u-boot.sym.S
+	@$(CC) $(c_flags) -c $(srctree)/lib/symbols.c -o lib/symbols.o
+	@$(AS) u-boot.sym.S -o u-boot.sym.o
+	@$(call cmd,u-boot__)
+else
+u-boot: u-boot.nosyms FORCE
+	+$(call if_changed,copy)
+endif
+
+
 ifeq ($(CONFIG_RISCV),y)
 	@tools/prelink-riscv $@
 endif
 
include/symbols.h new file +19 -0
new file mode 100644
index 000000000000..b17f122be339
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+
+#include <string.h>
+#include <asm/types.h>
+
+#define KSYM_NAME_LEN 127
+
+#if IS_ENABLED(CONFIG_SYMBOL_LOOKUP)
+const char * __attribute__((weak)) symbols_lookup(unsigned long addr, unsigned long *symaddr, unsigned long *offset,
+			   char *namebuf);
+#else
+static inline const char *symbols_lookup(unsigned long addr, unsigned long *symaddr, unsigned long *offset,
+			   char *namebuf)
+{
+	strcpy(namebuf, "???");
+	namebuf[3] = '\0';
+	return namebuf;
+}
+#endif
lib/Kconfig +8 -0
index b3baa4b85b07..2eb61fe6177a 100644
@@ -977,8 +977,16 @@ config VPL_OF_LIBFDT_ASSUME_MASK
 	  are made, and libfdt is able to deal with malicious data. A value of
 	  0xff means all assumptions are made and any invalid data may cause
 	  unsafe execution. See FDT_ASSUME_PERFECT, etc. in libfdt_internal.h
 
+config SYMBOL_LOOKUP
+	bool "Enable symbol lookup"
+	depends on ARM64
+	help
+	  This enables support for looking up symbol names from addresses. When
+	  enabled U-Boot will print stack traces with function names when an
+	  exception occurs.
+
 menu "System tables"
 	depends on (!EFI && !SYS_COREBOOT) || (ARM && EFI_LOADER)
 
 config BLOBLIST_TABLES
lib/symbols.c new file +126 -0

126 lines not on this page · raw message

tools/Makefile +3 -0
index 6a4280e3668f..278539ea77be 100644
@@ -225,8 +225,11 @@ hostprogs-$(CONFIG_ARCH_MVEBU) += kwboot
 
 hostprogs-y += proftool
 proftool-objs = proftool.o generated/lib/abuf.o
 
+HOSTCFLAGS_symbols.o += -DCONFIG_TEXT_BASE=$(CONFIG_TEXT_BASE)
+hostprogs-$(CONFIG_SYMBOL_LOOKUP) += symbols
+
 hostprogs-$(CONFIG_STATIC_RELA) += relocate-rela
 hostprogs-$(CONFIG_RISCV) += prelink-riscv
 
 hostprogs-$(CONFIG_ARCH_OCTEON) += update_octeon_header
tools/symbols.c new file +646 -0

646 lines not on this page · raw message

Signature · 1 line
2.46.0
#3 lore raw

PATCH v2 3/3 arm64: unwind stack on exception

In reply to Caleb Connolly

We already build arm64 images with frame pointers. Let's finally make use of them in tandem with the new symbol lookup support by unwinding the stack when an exception occurs, producing a backtrace similar to those emitted by Linux.

In addition, introduce a dedicated unwind_stack() function which can be called from anywhere to print a backtrace.

---
 arch/arm/include/asm/ptrace.h |  2 ++
 arch/arm/lib/interrupts_64.c  | 75 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 77 insertions(+)
arch/arm/include/asm/ptrace.h +2 -0
index a836f6cc60db..d4a971211843 100644
@@ -22,8 +22,10 @@
 
 #ifdef __KERNEL__
 extern void show_regs(struct pt_regs *);
 
+void unwind_stack(void);
+
 #define predicate(x)	(x & 0xf0000000)
 #define PREDICATE_ALWAYS	0xe0000000
 
 #endif
arch/arm/lib/interrupts_64.c +75 -0

106 lines not on this page · raw message

Signature · 1 line
2.46.0