@@ -1,2 +1,3 @@
gendwarfksyms-y += gendwarfksyms.o
+gendwarfksyms-y += symbols.o
gendwarfksyms-y += types.o
@@ -96,6 +96,8 @@ int main(int argc, const char **argv)
if (parse_options(argc, argv, &filename) < 0)
return usage();
+ check(symbol_read_list(stdin));
+
fd = open(filename, O_RDONLY);
if (fd == -1) {
error("open failed for '%s': %s", filename, strerror(errno));
@@ -49,6 +49,21 @@ extern bool debug;
__res; \
})
+/*
+ * symbols.c
+ */
+
+/* Exported symbol -- matching either the name or the address */
+struct symbol {
+ const char *name;
+ uintptr_t addr;
+ struct hlist_node addr_hash;
+ struct hlist_node name_hash;
+};
+
+extern int symbol_read_list(FILE *file);
+extern struct symbol *symbol_get(uintptr_t addr, const char *name);
+
/*
* types.c
*/
@@ -56,6 +71,8 @@ extern bool debug;
struct state {
Dwfl_Module *mod;
Dwarf *dbg;
+ struct symbol *sym;
+ Dwarf_Die origin;
};
typedef int (*die_callback_t)(struct state *state, Dwarf_Die *die);
new file mode 100644
@@ -0,0 +1,130 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2024 Google LLC
+ */
+
+#include <string.h>
+#include <linux/jhash.h>
+#include "gendwarfksyms.h"
+
+/* Hash tables for looking up requested symbols by address and name */
+#define SYMBOL_HASH_BITS 7
+DEFINE_HASHTABLE(symbol_addrs, SYMBOL_HASH_BITS);
+DEFINE_HASHTABLE(symbol_names, SYMBOL_HASH_BITS);
+
+static u32 name_hash(const char *name)
+{
+ return jhash(name, strlen(name), 0);
+}
+
+/* symbol_for_each callback -- return true to stop, false to continue */
+typedef bool (*symbol_callback_t)(struct symbol *, void *arg);
+
+static bool for_each_addr(uintptr_t addr, symbol_callback_t func, void *data)
+{
+ struct symbol *sym;
+ bool found = false;
+
+ if (addr == UINTPTR_MAX)
+ return false;
+
+ hash_for_each_possible(symbol_addrs, sym, addr_hash, addr) {
+ if (sym->addr == addr) {
+ if (func(sym, data))
+ return true;
+ found = true;
+ }
+ }
+
+ return found;
+}
+
+static bool for_each_name(const char *name, symbol_callback_t func, void *data)
+{
+ struct symbol *sym;
+ bool found = false;
+
+ if (!name)
+ return false;
+
+ hash_for_each_possible(symbol_names, sym, name_hash, name_hash(name)) {
+ if (!strcmp(sym->name, name)) {
+ if (func(sym, data))
+ return true;
+ found = true;
+ }
+ }
+
+ return found;
+}
+
+static bool for_each(uintptr_t addr, const char *name, symbol_callback_t func,
+ void *data)
+{
+ bool found = false;
+
+ if (for_each_addr(addr, func, data))
+ found = true;
+ if (for_each_name(name, func, data))
+ found = true;
+
+ return found;
+}
+
+int symbol_read_list(FILE *file)
+{
+ struct symbol *sym;
+ char *line = NULL;
+ char *name = NULL;
+ uint64_t addr;
+ size_t size = 0;
+
+ while (getline(&line, &size, file) > 0) {
+ if (sscanf(line, "%" PRIx64 " %ms\n", &addr, &name) != 2) {
+ error("malformed input line (expected 'address symbol-name'): %s",
+ line);
+ return -1;
+ }
+
+ free(line);
+ line = NULL;
+
+ sym = malloc(sizeof(struct symbol));
+ if (!sym) {
+ error("malloc failed");
+ return -1;
+ }
+
+ debug("adding { %lx, \"%s\" }", addr, name);
+
+ sym->addr = (uintptr_t)addr;
+ sym->name = name;
+ name = NULL;
+
+ hash_add(symbol_addrs, &sym->addr_hash, sym->addr);
+ hash_add(symbol_names, &sym->name_hash, name_hash(sym->name));
+ }
+
+ if (line)
+ free(line);
+
+ return 0;
+}
+
+static bool return_symbol(struct symbol *sym, void *arg)
+{
+ struct symbol **res = (struct symbol **)arg;
+
+ *res = sym;
+ return true; /* Stop -- return the first match */
+}
+
+struct symbol *symbol_get(uintptr_t addr, const char *name)
+{
+ struct symbol *sym;
+
+ if (for_each(addr, name, return_symbol, &sym))
+ return sym;
+
+ return NULL;
+}
@@ -5,6 +5,68 @@
#include "gendwarfksyms.h"
+#define DEFINE_GET_ATTR(attr, type) \
+ static bool get_##attr##_attr(Dwarf_Die *die, unsigned int id, \
+ type *value) \
+ { \
+ Dwarf_Attribute da; \
+ return dwarf_attr(die, id, &da) && \
+ !dwarf_form##attr(&da, value); \
+ }
+
+DEFINE_GET_ATTR(addr, Dwarf_Addr)
+
+static bool get_ref_die_attr(Dwarf_Die *die, unsigned int id, Dwarf_Die *value)
+{
+ Dwarf_Attribute da;
+
+ /* dwarf_formref_die returns a pointer instead of an error value. */
+ return dwarf_attr(die, id, &da) && dwarf_formref_die(&da, value);
+}
+
+static const char *get_name(Dwarf_Die *die)
+{
+ Dwarf_Attribute attr;
+
+ /* rustc uses DW_AT_linkage_name for exported symbols */
+ if (dwarf_attr(die, DW_AT_linkage_name, &attr) ||
+ dwarf_attr(die, DW_AT_name, &attr)) {
+ return dwarf_formstring(&attr);
+ }
+
+ return NULL;
+}
+
+static Dwarf_Die *get_exported(struct state *state, Dwarf_Die *die)
+{
+ Dwarf_Die *origin = NULL;
+ Dwarf_Word addr = UINTPTR_MAX;
+
+ state->sym = NULL;
+
+ /* If the DIE has an abstract origin, use it for type expansion. */
+ if (get_ref_die_attr(die, DW_AT_abstract_origin, &state->origin))
+ origin = &state->origin;
+
+ /*
+ * Only one name is emitted for aliased functions, so we must match
+ * the address too, if available.
+ */
+ if (get_addr_attr(die, DW_AT_low_pc, &addr) &&
+ dwfl_module_relocate_address(state->mod, &addr) < 0) {
+ error("dwfl_module_relocate_address failed");
+ return NULL;
+ }
+
+ state->sym = symbol_get(addr, get_name(die));
+
+ /* Look up using the origin name if there are no matches. */
+ if (!state->sym && origin)
+ state->sym = symbol_get(addr, get_name(origin));
+
+ return origin ? origin : die;
+}
+
/*
* Type string processing
*/
@@ -40,7 +102,7 @@ int process_die_container(struct state *state, Dwarf_Die *die,
}
/*
- * Symbol processing
+ * Exported symbol processing
*/
static int process_subprogram(struct state *state, Dwarf_Die *die)
{
@@ -67,6 +129,12 @@ static int process_exported_symbols(struct state *state, Dwarf_Die *die)
/* Possible exported symbols */
case DW_TAG_subprogram:
case DW_TAG_variable:
+ die = get_exported(state, die);
+ if (!die || !state->sym)
+ return 0;
+
+ debug("%s (@ %lx)", state->sym->name, state->sym->addr);
+
if (tag == DW_TAG_subprogram)
check(process_subprogram(state, die));
else
Support passing a list of exported symbols to gendwarfksyms via stdin and filter non-exported symbols from the output. The symbol list input has the format 'symbol-address symbol-name' to allow the parser to discover exported symbols also by address. This is necessary for aliased symbols, where only one name appears in the debugging information. Signed-off-by: Sami Tolvanen <samitolvanen@google.com> --- tools/gendwarfksyms/Build | 1 + tools/gendwarfksyms/gendwarfksyms.c | 2 + tools/gendwarfksyms/gendwarfksyms.h | 17 ++++ tools/gendwarfksyms/symbols.c | 130 ++++++++++++++++++++++++++++ tools/gendwarfksyms/types.c | 70 ++++++++++++++- 5 files changed, 219 insertions(+), 1 deletion(-) create mode 100644 tools/gendwarfksyms/symbols.c