From: Sami Tolvanen <samitolvanen@google.com>
To: Masahiro Yamada <masahiroy@kernel.org>,
Luis Chamberlain <mcgrof@kernel.org>,
Miguel Ojeda <ojeda@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Matthew Maurer <mmaurer@google.com>,
Alex Gaynor <alex.gaynor@gmail.com>,
Wedson Almeida Filho <wedsonaf@gmail.com>,
Gary Guo <gary@garyguo.net>,
linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org,
Sami Tolvanen <samitolvanen@google.com>
Subject: [PATCH 02/15] gendwarfksyms: Add symbol list input handling
Date: Mon, 17 Jun 2024 17:58:21 +0000 [thread overview]
Message-ID: <20240617175818.58219-19-samitolvanen@google.com> (raw)
In-Reply-To: <20240617175818.58219-17-samitolvanen@google.com>
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
diff --git a/tools/gendwarfksyms/Build b/tools/gendwarfksyms/Build
index 805591b6df80..a83c59bfef8b 100644
--- a/tools/gendwarfksyms/Build
+++ b/tools/gendwarfksyms/Build
@@ -1,2 +1,3 @@
gendwarfksyms-y += gendwarfksyms.o
+gendwarfksyms-y += symbols.o
gendwarfksyms-y += types.o
diff --git a/tools/gendwarfksyms/gendwarfksyms.c b/tools/gendwarfksyms/gendwarfksyms.c
index 4a2dea307849..4a1bd9239182 100644
--- a/tools/gendwarfksyms/gendwarfksyms.c
+++ b/tools/gendwarfksyms/gendwarfksyms.c
@@ -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));
diff --git a/tools/gendwarfksyms/gendwarfksyms.h b/tools/gendwarfksyms/gendwarfksyms.h
index 44e94f1d9671..b77855cc94a7 100644
--- a/tools/gendwarfksyms/gendwarfksyms.h
+++ b/tools/gendwarfksyms/gendwarfksyms.h
@@ -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);
diff --git a/tools/gendwarfksyms/symbols.c b/tools/gendwarfksyms/symbols.c
new file mode 100644
index 000000000000..2cae61bcede7
--- /dev/null
+++ b/tools/gendwarfksyms/symbols.c
@@ -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;
+}
diff --git a/tools/gendwarfksyms/types.c b/tools/gendwarfksyms/types.c
index 2a8e45ae911c..f1ce7bfcf510 100644
--- a/tools/gendwarfksyms/types.c
+++ b/tools/gendwarfksyms/types.c
@@ -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
--
2.45.2.627.g7a2c4fd464-goog
next prev parent reply other threads:[~2024-06-17 17:58 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-17 17:58 [PATCH 00/15] Implement MODVERSIONS for Rust Sami Tolvanen
2024-06-17 17:58 ` [PATCH 01/15] tools: Add gendwarfksyms Sami Tolvanen
2024-06-17 17:58 ` Sami Tolvanen [this message]
2024-06-17 17:58 ` [PATCH 03/15] gendwarfksyms: Add CRC calculation Sami Tolvanen
2024-06-17 17:58 ` [PATCH 04/15] gendwarfksyms: Expand base_type Sami Tolvanen
2024-06-17 17:58 ` [PATCH 05/15] gendwarfksyms: Add a cache Sami Tolvanen
2024-06-17 17:58 ` [PATCH 06/15] gendwarfksyms: Expand type modifiers and typedefs Sami Tolvanen
2024-06-17 17:58 ` [PATCH 07/15] gendwarfksyms: Add pretty-printing Sami Tolvanen
2024-06-17 17:58 ` [PATCH 08/15] gendwarfksyms: Expand subroutine_type Sami Tolvanen
2024-06-17 17:58 ` [PATCH 09/15] gendwarfksyms: Expand array_type Sami Tolvanen
2024-06-17 17:58 ` [PATCH 10/15] gendwarfksyms: Expand structure types Sami Tolvanen
2024-06-17 17:58 ` [PATCH 11/15] gendwarfksyms: Limit structure expansion Sami Tolvanen
2024-06-17 17:58 ` [PATCH 12/15] gendwarfksyms: Add inline debugging Sami Tolvanen
2024-06-17 17:58 ` [PATCH 13/15] modpost: Add support for hashing long symbol names Sami Tolvanen
2024-06-18 16:47 ` Masahiro Yamada
2024-06-18 20:07 ` Sami Tolvanen
2024-06-17 17:58 ` [PATCH 14/15] module: Support hashed symbol names when checking modversions Sami Tolvanen
2024-06-17 17:58 ` [PATCH 15/15] kbuild: Use gendwarfksyms to generate Rust symbol versions Sami Tolvanen
2024-06-18 16:28 ` [PATCH 00/15] Implement MODVERSIONS for Rust Masahiro Yamada
2024-06-18 20:05 ` Sami Tolvanen
2024-06-18 16:44 ` Greg Kroah-Hartman
2024-06-18 16:50 ` Masahiro Yamada
2024-06-18 17:18 ` Greg Kroah-Hartman
2024-06-18 19:03 ` Masahiro Yamada
2024-06-18 20:19 ` Sami Tolvanen
2024-06-18 19:42 ` Luis Chamberlain
2024-06-18 21:19 ` Sami Tolvanen
2024-06-18 23:32 ` Luis Chamberlain
2024-07-10 7:30 ` Petr Pavlu
2024-07-15 20:39 ` Sami Tolvanen
2024-07-16 7:12 ` Greg Kroah-Hartman
2024-07-18 17:04 ` Sami Tolvanen
2024-07-22 8:20 ` Petr Pavlu
2024-07-26 21:05 ` Sami Tolvanen
2024-07-31 20:46 ` Neal Gompa
2024-08-01 11:22 ` Petr Pavlu
2024-08-01 19:38 ` Sami Tolvanen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240617175818.58219-19-samitolvanen@google.com \
--to=samitolvanen@google.com \
--cc=alex.gaynor@gmail.com \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-modules@vger.kernel.org \
--cc=masahiroy@kernel.org \
--cc=mcgrof@kernel.org \
--cc=mmaurer@google.com \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=wedsonaf@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).