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 01/15] tools: Add gendwarfksyms
Date: Mon, 17 Jun 2024 17:58:20 +0000 [thread overview]
Message-ID: <20240617175818.58219-18-samitolvanen@google.com> (raw)
In-Reply-To: <20240617175818.58219-17-samitolvanen@google.com>
Add a basic DWARF parser, which uses libdw to traverse the debugging
information in an object file and looks for functions and variables.
In follow-up patches, this will be expanded to produce symbol versions
for CONFIG_MODVERSIONS from DWARF. This tool is needed mainly for Rust
kernel module compatibility.
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
---
tools/Makefile | 11 +--
tools/gendwarfksyms/Build | 2 +
tools/gendwarfksyms/Makefile | 49 +++++++++++
tools/gendwarfksyms/gendwarfksyms.c | 128 ++++++++++++++++++++++++++++
tools/gendwarfksyms/gendwarfksyms.h | 71 +++++++++++++++
tools/gendwarfksyms/types.c | 87 +++++++++++++++++++
6 files changed, 343 insertions(+), 5 deletions(-)
create mode 100644 tools/gendwarfksyms/Build
create mode 100644 tools/gendwarfksyms/Makefile
create mode 100644 tools/gendwarfksyms/gendwarfksyms.c
create mode 100644 tools/gendwarfksyms/gendwarfksyms.h
create mode 100644 tools/gendwarfksyms/types.c
diff --git a/tools/Makefile b/tools/Makefile
index 276f5d0d53a4..60806ff753b7 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -17,6 +17,7 @@ help:
@echo ' firewire - the userspace part of nosy, an IEEE-1394 traffic sniffer'
@echo ' firmware - Firmware tools'
@echo ' freefall - laptop accelerometer program for disk protection'
+ @echo ' gendwarfksyms - generates symbol versions from DWARF'
@echo ' gpio - GPIO tools'
@echo ' hv - tools used when in Hyper-V clients'
@echo ' iio - IIO tools'
@@ -68,7 +69,7 @@ acpi: FORCE
cpupower: FORCE
$(call descend,power/$@)
-counter firewire hv guest bootconfig spi usb virtio mm bpf iio gpio objtool leds wmi pci firmware debugging tracing: FORCE
+counter firewire hv guest bootconfig spi usb virtio mm bpf iio gendwarfksyms gpio objtool leds wmi pci firmware debugging tracing: FORCE
$(call descend,$@)
bpf/%: FORCE
@@ -154,8 +155,8 @@ freefall_install:
kvm_stat_install:
$(call descend,kvm/$(@:_install=),install)
-install: acpi_install counter_install cpupower_install gpio_install \
- hv_install firewire_install iio_install \
+install: acpi_install counter_install cpupower_install gendwarfksyms_install \
+ gpio_install hv_install firewire_install iio_install \
perf_install selftests_install turbostat_install usb_install \
virtio_install mm_install bpf_install x86_energy_perf_policy_install \
tmon_install freefall_install objtool_install kvm_stat_install \
@@ -168,7 +169,7 @@ acpi_clean:
cpupower_clean:
$(call descend,power/cpupower,clean)
-counter_clean hv_clean firewire_clean bootconfig_clean spi_clean usb_clean virtio_clean mm_clean wmi_clean bpf_clean iio_clean gpio_clean objtool_clean leds_clean pci_clean firmware_clean debugging_clean tracing_clean:
+counter_clean hv_clean firewire_clean bootconfig_clean spi_clean usb_clean virtio_clean mm_clean wmi_clean bpf_clean iio_clean gendwarfksyms_clean gpio_clean objtool_clean leds_clean pci_clean firmware_clean debugging_clean tracing_clean:
$(call descend,$(@:_clean=),clean)
libapi_clean:
@@ -211,7 +212,7 @@ build_clean:
clean: acpi_clean counter_clean cpupower_clean hv_clean firewire_clean \
perf_clean selftests_clean turbostat_clean bootconfig_clean spi_clean usb_clean virtio_clean \
mm_clean bpf_clean iio_clean x86_energy_perf_policy_clean tmon_clean \
- freefall_clean build_clean libbpf_clean libsubcmd_clean \
+ freefall_clean build_clean libbpf_clean libsubcmd_clean gendwarfksyms_clean \
gpio_clean objtool_clean leds_clean wmi_clean pci_clean firmware_clean debugging_clean \
intel-speed-select_clean tracing_clean thermal_clean thermometer_clean thermal-engine_clean
diff --git a/tools/gendwarfksyms/Build b/tools/gendwarfksyms/Build
new file mode 100644
index 000000000000..805591b6df80
--- /dev/null
+++ b/tools/gendwarfksyms/Build
@@ -0,0 +1,2 @@
+gendwarfksyms-y += gendwarfksyms.o
+gendwarfksyms-y += types.o
diff --git a/tools/gendwarfksyms/Makefile b/tools/gendwarfksyms/Makefile
new file mode 100644
index 000000000000..1138ebd8bd0f
--- /dev/null
+++ b/tools/gendwarfksyms/Makefile
@@ -0,0 +1,49 @@
+# SPDX-License-Identifier: GPL-2.0
+include ../scripts/Makefile.include
+include ../scripts/Makefile.arch
+
+ifeq ($(srctree),)
+srctree := $(patsubst %/,%,$(dir $(CURDIR)))
+srctree := $(patsubst %/,%,$(dir $(srctree)))
+endif
+
+GENDWARFKSYMS := $(OUTPUT)gendwarfksyms
+GENDWARFKSYMS_IN := $(GENDWARFKSYMS)-in.o
+
+LIBDW_FLAGS := $(shell $(HOSTPKG_CONFIG) libdw --cflags 2>/dev/null)
+LIBDW_LIBS := $(shell $(HOSTPKG_CONFIG) libdw --libs 2>/dev/null || echo -ldw -lelf)
+
+all: $(GENDWARFKSYMS)
+
+INCLUDES := -I$(srctree)/tools/include
+
+WARNINGS := -Wall -Wno-unused-value
+GENDWARFKSYMS_CFLAGS := -Werror $(WARNINGS) $(KBUILD_HOSTCFLAGS) -g $(INCLUDES) $(LIBDW_FLAGS)
+GENDWARFKSYMS_LDFLAGS := $(LIBDW_LIBS) $(KBUILD_HOSTLDFLAGS)
+
+# Always want host compilation.
+HOST_OVERRIDES := CC="$(HOSTCC)" LD="$(HOSTLD)" AR="$(HOSTAR)"
+
+ifeq ($(V),1)
+ Q =
+else
+ Q = @
+endif
+
+export srctree OUTPUT
+include $(srctree)/tools/build/Makefile.include
+
+$(GENDWARFKSYMS_IN): FORCE
+ $(Q)$(MAKE) $(build)=gendwarfksyms $(HOST_OVERRIDES) CFLAGS="$(GENDWARFKSYMS_CFLAGS)" \
+ LDFLAGS="$(GENDWARFKSYMS_LDFLAGS)"
+
+
+$(GENDWARFKSYMS): $(GENDWARFKSYMS_IN)
+ $(QUIET_LINK)$(HOSTCC) $(GENDWARFKSYMS_IN) $(GENDWARFKSYMS_LDFLAGS) -o $@
+
+clean:
+ $(call QUIET_CLEAN, gendwarfksyms) $(RM) $(GENDWARFKSYMS)
+
+FORCE:
+
+.PHONY: clean FORCE
diff --git a/tools/gendwarfksyms/gendwarfksyms.c b/tools/gendwarfksyms/gendwarfksyms.c
new file mode 100644
index 000000000000..4a2dea307849
--- /dev/null
+++ b/tools/gendwarfksyms/gendwarfksyms.c
@@ -0,0 +1,128 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2024 Google LLC
+ */
+
+#include <fcntl.h>
+#include <errno.h>
+#include <stdarg.h>
+#include <string.h>
+#include "gendwarfksyms.h"
+
+/*
+ * Options
+ */
+
+/* Print type descriptions and debugging output to stderr */
+bool debug;
+
+static const struct {
+ const char *arg;
+ bool *flag;
+} options[] = {
+ { "--debug", &debug },
+};
+
+static int usage(void)
+{
+ error("usage: gendwarfksyms [options] elf-object-file < symbol-list");
+ return -1;
+}
+
+static int parse_options(int argc, const char **argv, const char **filename)
+{
+ *filename = NULL;
+
+ for (int i = 1; i < argc; i++) {
+ bool found = false;
+
+ for (int j = 0; j < ARRAY_SIZE(options); j++) {
+ if (!strcmp(argv[i], options[j].arg)) {
+ *options[j].flag = true;
+ found = true;
+ break;
+ }
+ }
+
+ if (!found) {
+ if (!*filename)
+ *filename = argv[i];
+ else
+ return -1;
+ }
+ }
+
+ return *filename ? 0 : -1;
+}
+
+static int process_modules(Dwfl_Module *mod, void **userdata, const char *name,
+ Dwarf_Addr base, void *arg)
+{
+ Dwarf_Addr dwbias;
+ Dwarf_Die cudie;
+ Dwarf_CU *cu = NULL;
+ Dwarf *dbg;
+ int res;
+
+ debug("%s", name);
+ dbg = dwfl_module_getdwarf(mod, &dwbias);
+
+ do {
+ res = dwarf_get_units(dbg, cu, &cu, NULL, NULL, &cudie, NULL);
+ if (res < 0) {
+ error("dwarf_get_units failed: no debugging information?");
+ return -1;
+ } else if (res == 1) {
+ return DWARF_CB_OK; /* No more units */
+ }
+
+ check(process_module(mod, dbg, &cudie));
+ } while (cu);
+
+ return DWARF_CB_OK;
+}
+
+static const Dwfl_Callbacks callbacks = {
+ .section_address = dwfl_offline_section_address,
+ .find_debuginfo = dwfl_standard_find_debuginfo,
+};
+
+int main(int argc, const char **argv)
+{
+ const char *filename = NULL;
+ Dwfl *dwfl;
+ int fd;
+
+ if (parse_options(argc, argv, &filename) < 0)
+ return usage();
+
+ fd = open(filename, O_RDONLY);
+ if (fd == -1) {
+ error("open failed for '%s': %s", filename, strerror(errno));
+ return -1;
+ }
+
+ dwfl = dwfl_begin(&callbacks);
+ if (!dwfl) {
+ error("dwfl_begin failed for '%s': %s", filename,
+ dwarf_errmsg(-1));
+ return -1;
+ }
+
+ if (!dwfl_report_offline(dwfl, filename, filename, fd)) {
+ error("dwfl_report_offline failed for '%s': %s", filename,
+ dwarf_errmsg(-1));
+ return -1;
+ }
+
+ dwfl_report_end(dwfl, NULL, NULL);
+
+ if (dwfl_getmodules(dwfl, &process_modules, NULL, 0)) {
+ error("dwfl_getmodules failed for '%s'", filename);
+ return -1;
+ }
+
+ dwfl_end(dwfl);
+
+ return 0;
+}
diff --git a/tools/gendwarfksyms/gendwarfksyms.h b/tools/gendwarfksyms/gendwarfksyms.h
new file mode 100644
index 000000000000..44e94f1d9671
--- /dev/null
+++ b/tools/gendwarfksyms/gendwarfksyms.h
@@ -0,0 +1,71 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2024 Google LLC
+ */
+
+#include <dwarf.h>
+#include <elfutils/libdw.h>
+#include <elfutils/libdwfl.h>
+#include <linux/hashtable.h>
+#include <inttypes.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#ifndef __GENDWARFKSYMS_H
+#define __GENDWARFKSYMS_H
+
+/*
+ * Options -- in gendwarfksyms.c
+ */
+extern bool debug;
+
+/*
+ * Output helpers
+ */
+#define __PREFIX "gendwarfksyms: "
+#define __println(prefix, format, ...) \
+ fprintf(stderr, prefix __PREFIX "%s: " format "\n", __func__, \
+ ##__VA_ARGS__)
+
+#define debug(format, ...) \
+ do { \
+ if (debug) \
+ __println("", format, ##__VA_ARGS__); \
+ } while (0)
+
+#define warn(format, ...) __println("warning: ", format, ##__VA_ARGS__)
+#define error(format, ...) __println("error: ", format, ##__VA_ARGS__)
+
+/*
+ * Error handling helpers
+ */
+#define check(expr) \
+ ({ \
+ int __res = expr; \
+ if (__res < 0) { \
+ error("`%s` failed: %d", #expr, __res); \
+ return __res; \
+ } \
+ __res; \
+ })
+
+/*
+ * types.c
+ */
+
+struct state {
+ Dwfl_Module *mod;
+ Dwarf *dbg;
+};
+
+typedef int (*die_callback_t)(struct state *state, Dwarf_Die *die);
+typedef bool (*die_match_callback_t)(Dwarf_Die *die);
+extern bool match_all(Dwarf_Die *die);
+
+extern int process_die_container(struct state *state, Dwarf_Die *die,
+ die_callback_t func,
+ die_match_callback_t match);
+
+extern int process_module(Dwfl_Module *mod, Dwarf *dbg, Dwarf_Die *cudie);
+
+#endif /* __GENDWARFKSYMS_H */
diff --git a/tools/gendwarfksyms/types.c b/tools/gendwarfksyms/types.c
new file mode 100644
index 000000000000..2a8e45ae911c
--- /dev/null
+++ b/tools/gendwarfksyms/types.c
@@ -0,0 +1,87 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2024 Google LLC
+ */
+
+#include "gendwarfksyms.h"
+
+/*
+ * Type string processing
+ */
+static int process(struct state *state, const char *s)
+{
+ s = s ?: "<null>";
+
+ if (debug)
+ fputs(s, stderr);
+
+ return 0;
+}
+
+bool match_all(Dwarf_Die *die)
+{
+ return true;
+}
+
+int process_die_container(struct state *state, Dwarf_Die *die,
+ die_callback_t func, die_match_callback_t match)
+{
+ Dwarf_Die current;
+ int res;
+
+ res = check(dwarf_child(die, ¤t));
+ while (!res) {
+ if (match(¤t))
+ check(func(state, ¤t));
+ res = check(dwarf_siblingof(¤t, ¤t));
+ }
+
+ return 0;
+}
+
+/*
+ * Symbol processing
+ */
+static int process_subprogram(struct state *state, Dwarf_Die *die)
+{
+ return check(process(state, "subprogram;\n"));
+}
+
+static int process_variable(struct state *state, Dwarf_Die *die)
+{
+ return check(process(state, "variable;\n"));
+}
+
+static int process_exported_symbols(struct state *state, Dwarf_Die *die)
+{
+ int tag = dwarf_tag(die);
+
+ switch (tag) {
+ /* Possible containers of exported symbols */
+ case DW_TAG_namespace:
+ case DW_TAG_class_type:
+ case DW_TAG_structure_type:
+ return check(process_die_container(
+ state, die, process_exported_symbols, match_all));
+
+ /* Possible exported symbols */
+ case DW_TAG_subprogram:
+ case DW_TAG_variable:
+ if (tag == DW_TAG_subprogram)
+ check(process_subprogram(state, die));
+ else
+ check(process_variable(state, die));
+
+ return 0;
+ default:
+ return 0;
+ }
+}
+
+int process_module(Dwfl_Module *mod, Dwarf *dbg, Dwarf_Die *cudie)
+{
+ struct state state = { .mod = mod, .dbg = dbg };
+
+ return check(process_die_container(
+ &state, cudie, process_exported_symbols, match_all));
+}
--
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 ` Sami Tolvanen [this message]
2024-06-17 17:58 ` [PATCH 02/15] gendwarfksyms: Add symbol list input handling Sami Tolvanen
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-18-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).