diff options
author | Richard Braun <rbraun@sceen.net> | 2018-06-26 22:08:09 +0200 |
---|---|---|
committer | Richard Braun <rbraun@sceen.net> | 2018-06-26 22:08:09 +0200 |
commit | 838d2f6f9bb85b849a382262e58f421268ad80ea (patch) | |
tree | fef4c1b3ef8d05a68e5d02247e0739b820076c19 | |
parent | 90d079aea8aad73d7b867a0e215f8c3b41a1113f (diff) |
Add support for an embedded symbol table
-rw-r--r-- | Kconfig | 12 | ||||
-rw-r--r-- | Makefile | 36 | ||||
-rw-r--r-- | kern/Makefile | 1 | ||||
-rw-r--r-- | kern/symbol.c | 49 | ||||
-rw-r--r-- | kern/symbol.h | 45 | ||||
-rwxr-xr-x | tools/gen_symtab.py | 30 |
6 files changed, 169 insertions, 4 deletions
@@ -47,6 +47,18 @@ config ASSERT ---help--- Enable assert() code generation. +config SYMTAB + bool "Build symbol table" + default y + ---help--- + Build and embed a symbol table in the kernel. + + This option allows the kernel to perform symbolic address + resolutions which are very convenient for debugging, at the + cost of additional data memory. + + If unsure, enable. + endmenu source "arch/$ARCH/Kconfig" @@ -85,10 +85,19 @@ define xbuild_link $(COMPILE) -o $@ $(1) $(XBUILD_LDFLAGS) endef +XBUILD_GEN_SYMTAB = $(SRCDIR)/tools/gen_symtab.py +XBUILD_GEN_SYMTAB_DEPS = $(XBUILD_GEN_SYMTAB) + +# $(call xbuild_gen_symtab) +define xbuild_gen_symtab + $(call xbuild_action,GEN,$@) \ + $(NM) -S -n $< | $(XBUILD_GEN_SYMTAB) > $@ +endef + define xbuild_clean - $(Q)rm -f x15 \ - $(x15_OBJDEPS) \ - $(x15_OBJECTS) \ + $(Q)rm -f x15 $(x15_NO_SYMTAB) \ + $(x15_OBJDEPS) $(x15_OBJECTS) \ + $(x15_SYMTAB_C) $(x15_SYMTAB_D) $(x15_SYMTAB_O) \ $(x15_LDS) endef @@ -225,6 +234,7 @@ TOOLCHAIN_PREFIX = $(TOOLCHAIN_NAME)- endif CPP := $(CC) -E +NM := $(TOOLCHAIN_PREFIX)nm CFLAGS ?= -O2 -g @@ -297,9 +307,13 @@ COMPILE := $(CC) $(XBUILD_CPPFLAGS) $(XBUILD_CFLAGS) # Don't change preprocessor and compiler flags from this point +x15_NO_SYMTAB := .x15.no_symtab x15_SOURCES := $(x15_SOURCES-y) x15_OBJDEPS := $(call xbuild_replace_source_suffix,d,$(x15_SOURCES)) x15_OBJECTS := $(call xbuild_replace_source_suffix,o,$(x15_SOURCES)) +x15_SYMTAB_C := .symtab.c +x15_SYMTAB_D := $(call xbuild_replace_source_suffix,d,$(x15_SYMTAB_C)) +x15_SYMTAB_O := $(call xbuild_replace_source_suffix,o,$(x15_SYMTAB_C)) x15_LDS := $(basename $(x15_LDS_S)) x15_LDS_D := $(x15_LDS).d @@ -331,9 +345,23 @@ x15_DEPS := $(x15_LDS) .x15.sorted_init_ops %.lds: %.lds.S include/generated/autoconf.h $(xbuild_gen_linker_script) -x15: $(x15_OBJECTS) $(x15_DEPS) +ifeq ($(CONFIG_SYMTAB),y) +x15_FIRST_PASS := $(x15_NO_SYMTAB) +else +x15_FIRST_PASS := x15 +endif + +$(x15_FIRST_PASS): $(x15_OBJECTS) $(x15_DEPS) $(call xbuild_link,$(x15_OBJECTS)) +ifeq ($(CONFIG_SYMTAB),y) +$(x15_SYMTAB_C): $(x15_FIRST_PASS) $(XBUILD_GEN_SYMTAB_DEPS) + $(call xbuild_gen_symtab) + +x15: $(x15_NO_SYMTAB) $(x15_SYMTAB_O) + $(call xbuild_link,$(x15_OBJECTS) $(x15_SYMTAB_O)) +endif + .PHONY: install-x15 install-x15: install -D -m 644 x15 $(DESTDIR)/boot/x15 diff --git a/kern/Makefile b/kern/Makefile index 5b04fcb3..ea7e3bff 100644 --- a/kern/Makefile +++ b/kern/Makefile @@ -29,6 +29,7 @@ x15_SOURCES-y += \ kern/spinlock.c \ kern/sref.c \ kern/string.c \ + kern/symbol.c \ kern/syscnt.c \ kern/task.c \ kern/thread.c \ diff --git a/kern/symbol.c b/kern/symbol.c new file mode 100644 index 00000000..30df091e --- /dev/null +++ b/kern/symbol.c @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2018 Agustina Arzille. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stddef.h> +#include <stdint.h> + +#include <kern/macros.h> +#include <kern/symbol.h> + +const size_t symbol_table_size __weak; +const struct symbol *symbol_table_ptr __weak; + +const struct symbol * +symbol_lookup(uintptr_t addr) +{ + const struct symbol *symbol; + uintptr_t start, end; + + for (size_t i = 0; i < symbol_table_size; i++) { + symbol = &symbol_table_ptr[i]; + + if (!symbol->name || (symbol->size == 0)) { + continue; + } + + start = symbol->addr; + end = symbol->addr + symbol->size; + + if ((addr >= start) && (addr < end)) { + return symbol; + } + } + + return NULL; +} diff --git a/kern/symbol.h b/kern/symbol.h new file mode 100644 index 00000000..61fbf3f0 --- /dev/null +++ b/kern/symbol.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2018 Agustina Arzille. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef KERN_SYMBOL_H +#define KERN_SYMBOL_H + +#include <stdint.h> + +#include <kern/macros.h> + +#define __symbol_table __section(".symbol") + +/* + * Symbol structure. + * + * This structure is public. + */ +struct symbol { + uintptr_t addr; + uintptr_t size; + const char *name; +}; + +/* + * Look up a symbol from an address. + * + * NULL is returned if no symbol was found for the given address. + */ +const struct symbol * symbol_lookup(uintptr_t addr); + +#endif /* KERN_SYMBOL_H */ diff --git a/tools/gen_symtab.py b/tools/gen_symtab.py new file mode 100755 index 00000000..b691da12 --- /dev/null +++ b/tools/gen_symtab.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 +''' +Embedded symbol table generator. +''' + +import sys + +symtab_size = 0 +symtab = [] + +for line in sys.stdin: + line = line.strip() + parts = line.split(' ') + del parts[2] + + if len(parts) != 3 or parts[2].startswith("__func__."): + continue + + symtab.append("{ 0x%s, 0x%s, \"%s\" }" % tuple(parts)) + symtab_size += 1 + +print("#include <kern/symbol.h>") +print("const struct symbol symbol_table[] __symbol_table = {") + +for elem in symtab: + print(" " + elem + ",",) + +print("};") +print("const size_t symbol_table_size = %d;" % symtab_size) +print("const struct symbol *symbol_table_ptr = symbol_table;") |