From patchwork Thu Nov 21 20:42:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13882339 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 64E9B1C4A35 for ; Thu, 21 Nov 2024 20:42:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221760; cv=none; b=HNRK7nwbwUliygRXVxV/tM6gz1AqsbvxUWe9BICG5Metln+z5n4jKF1+shpZUKuHFfnswBzQol16RGXV8YPINfXJq0TlAqHg6xwKihkgDVCtD5D8ijY9gAFtzWEAKO/faYmd56yl9yUTJcb9bTgnRabfUl+t42f8Q3lwcEyh5qs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221760; c=relaxed/simple; bh=bcgGey3GJghoJQ+MJYS36aN2TomUmIOIZEu8ecAlP1E=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RwqZrnU7546Dx6tU9ECj6wHclqPBcJWNt7PvT3AOPcksuHbtc++tdR3LgoiiWird4/Opmv9AzkHDpNLGt3AmLtvP+P7znl/HaSVOl0NlZrr/beAZP+ghBDCLH5+RfWL/9+0BjSUoD7EZJN3wfjv0xcKtI9zjSkRf3+dmGknDjd8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=OXGbSI4C; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="OXGbSI4C" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-7ee22af5bb7so1270677a12.0 for ; Thu, 21 Nov 2024 12:42:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732221758; x=1732826558; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=kID1y1a3UKsIHE7codP4W2zuWLJeogjB7Bw+IfFDISI=; b=OXGbSI4CGfbQy5ixfHOXHJ7IQfRu9k3gRklKzvbVjXCqcoXK9ROedlqazosbqhNkZJ o402V/qRuYcTNQzY51wNvkbfkN7zgLro8bWwQnqjhJRLB8NeCych6SpYkNULSOIQl5OX wWdgPwVOb78xslxfk++FiEnaJmsyTLsxZZRF635CAmVDs+Gkrz6kHgF5bORnRfwsZCdO oBPpCnDCp6Tro1tjF7eXEFhT3bnvUVd/eyjso3kMDH7C8hKv+1pcav3gEMkWOJGR2iOI 7/Xw7OfsDxgWiKH06Zgi8VC31i8AwGvLFa9uk/y5TMcKPsEALFDSZ02g3HqH3vEZXOxN wJOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732221758; x=1732826558; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=kID1y1a3UKsIHE7codP4W2zuWLJeogjB7Bw+IfFDISI=; b=DlyccbfAqjam1yAW04EKfMtQfy8x0hcF8IyiMppB980ssIVZMYW/JoOJrwcaeuiXmC 8VCgyxnVGuqVpeu8eK2Y1QXvx3OgFA8sSpNuC6wmlRt8jjuV2+juF3YXzAl+JeRE9x+C S5q1ac44rT6OYE78Y5qdML6ZCXAeuXUlUJ9eTIy0MDiaCh2WD/lRrkMWizljTVOAkhPQ ne8FY/fWDked/8y7IJpGo25DqVNY8wk2co6sctmbbnAq0FT7+xXN7woHpmH2NuD4D2sD EeagAkzqHWwFpKH2HQsPUl1aXxpEi4x1tAIp0ctbf9SDvyD7ACcl5DqIwKwlIhLCfnQG 0igA== X-Forwarded-Encrypted: i=1; AJvYcCUDhH2d7aTiIIo5Z/24X5t1z3u6ZCLaHvYIEiWpCnmpAT54Sj71/6d7/i2VELDvMiaCx4MvmKIyv4qr/2gT@vger.kernel.org X-Gm-Message-State: AOJu0Yyek29omdHSt+Zw6SCBnO5CoInLQlH0T0VUNOFlnBkkXzR2rfVy 016x0lP5VHL+1+jBQQdSMmuj31VqC9hWEGMU2+D1zZT0pfzwE2JV/qzivkY3khzyqm0QvzCiaKZ h8tYFJscpbCl/EGKJ28pZoNZ5jQ== X-Google-Smtp-Source: AGHT+IHbRpJ9w1nVpyL7/0O+RJYtKl6NBU6/iZtsm1i4fKWm2E7NLa1pnbM97TmexV61nFOk6kYgo3Ny1XDoJiiETF8= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a63:b551:0:b0:7ea:71a0:c2c3 with SMTP id 41be03b00d2f7-7fbccbd48dcmr296a12.7.1732221757708; Thu, 21 Nov 2024 12:42:37 -0800 (PST) Date: Thu, 21 Nov 2024 20:42:22 +0000 In-Reply-To: <20241121204220.2378181-20-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121204220.2378181-20-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=14986; i=samitolvanen@google.com; h=from:subject; bh=bcgGey3GJghoJQ+MJYS36aN2TomUmIOIZEu8ecAlP1E=; b=owGbwMvMwCEWxa662nLh8irG02pJDOn2s3V/Ly93cjObn/zn5J6zYctOiVUFM/xZLXop/24Ge 8EV7fedHaUsDGIcDLJiiiwtX1dv3f3dKfXV5yIJmDmsTCBDGLg4BWAiJ/0Y/plFrHNU3Ro2O+L2 Z9O/GavYdz21PMPF9F33OpPv7zLWqmaGf1ZfrVW+6Ep4/tVenMF7/C+v/ZN/f1J763+vkdDIP+d 4jBcA X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121204220.2378181-21-samitolvanen@google.com> Subject: [PATCH v6 01/18] tools: Add gendwarfksyms From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen 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. Signed-off-by: Sami Tolvanen Reviewed-by: Petr Pavlu --- kernel/module/Kconfig | 8 ++ scripts/Makefile | 1 + scripts/gendwarfksyms/.gitignore | 2 + scripts/gendwarfksyms/Makefile | 8 ++ scripts/gendwarfksyms/dwarf.c | 166 ++++++++++++++++++++++++++ scripts/gendwarfksyms/gendwarfksyms.c | 126 +++++++++++++++++++ scripts/gendwarfksyms/gendwarfksyms.h | 100 ++++++++++++++++ scripts/gendwarfksyms/symbols.c | 96 +++++++++++++++ 8 files changed, 507 insertions(+) create mode 100644 scripts/gendwarfksyms/.gitignore create mode 100644 scripts/gendwarfksyms/Makefile create mode 100644 scripts/gendwarfksyms/dwarf.c create mode 100644 scripts/gendwarfksyms/gendwarfksyms.c create mode 100644 scripts/gendwarfksyms/gendwarfksyms.h create mode 100644 scripts/gendwarfksyms/symbols.c diff --git a/kernel/module/Kconfig b/kernel/module/Kconfig index 7c6588148d42..f9e5f82fa88b 100644 --- a/kernel/module/Kconfig +++ b/kernel/module/Kconfig @@ -169,6 +169,14 @@ config MODVERSIONS make them incompatible with the kernel you are running. If unsure, say N. +config GENDWARFKSYMS + bool + depends on DEBUG_INFO + # Requires full debugging information, split DWARF not supported. + depends on !DEBUG_INFO_REDUCED && !DEBUG_INFO_SPLIT + # Requires ELF object files. + depends on !LTO + config ASM_MODVERSIONS bool default HAVE_ASM_MODVERSIONS && MODVERSIONS diff --git a/scripts/Makefile b/scripts/Makefile index 6bcda4b9d054..d7fec46d38c0 100644 --- a/scripts/Makefile +++ b/scripts/Makefile @@ -54,6 +54,7 @@ targets += module.lds subdir-$(CONFIG_GCC_PLUGINS) += gcc-plugins subdir-$(CONFIG_MODVERSIONS) += genksyms +subdir-$(CONFIG_GENDWARFKSYMS) += gendwarfksyms subdir-$(CONFIG_SECURITY_SELINUX) += selinux subdir-$(CONFIG_SECURITY_IPE) += ipe diff --git a/scripts/gendwarfksyms/.gitignore b/scripts/gendwarfksyms/.gitignore new file mode 100644 index 000000000000..0927f8d3cd96 --- /dev/null +++ b/scripts/gendwarfksyms/.gitignore @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0 +/gendwarfksyms diff --git a/scripts/gendwarfksyms/Makefile b/scripts/gendwarfksyms/Makefile new file mode 100644 index 000000000000..9f8fec4fd39b --- /dev/null +++ b/scripts/gendwarfksyms/Makefile @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0 +hostprogs-always-y += gendwarfksyms + +gendwarfksyms-objs += gendwarfksyms.o +gendwarfksyms-objs += dwarf.o +gendwarfksyms-objs += symbols.o + +HOSTLDLIBS_gendwarfksyms := -ldw -lelf diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c new file mode 100644 index 000000000000..81df3e2ad3ae --- /dev/null +++ b/scripts/gendwarfksyms/dwarf.c @@ -0,0 +1,166 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Google LLC + */ + +#include "gendwarfksyms.h" + +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); +} + +#define DEFINE_GET_STRING_ATTR(attr) \ + static const char *get_##attr##_attr(Dwarf_Die *die) \ + { \ + Dwarf_Attribute da; \ + if (dwarf_attr(die, DW_AT_##attr, &da)) \ + return dwarf_formstring(&da); \ + return NULL; \ + } + +DEFINE_GET_STRING_ATTR(name) +DEFINE_GET_STRING_ATTR(linkage_name) + +static const char *get_symbol_name(Dwarf_Die *die) +{ + const char *name; + + /* rustc uses DW_AT_linkage_name for exported symbols */ + name = get_linkage_name_attr(die); + if (!name) + name = get_name_attr(die); + + return name; +} + +static bool match_export_symbol(struct state *state, Dwarf_Die *die) +{ + Dwarf_Die *source = die; + Dwarf_Die origin; + + /* If the DIE has an abstract origin, use it for type information. */ + if (get_ref_die_attr(die, DW_AT_abstract_origin, &origin)) + source = &origin; + + state->sym = symbol_get(get_symbol_name(die)); + + /* Look up using the origin name if there are no matches. */ + if (!state->sym && source != die) + state->sym = symbol_get(get_symbol_name(source)); + + state->die = *source; + return !!state->sym; +} + +/* + * Type string processing + */ +static void process(const char *s) +{ + s = s ?: ""; + + if (dump_dies) + fputs(s, stderr); +} + +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 = checkp(dwarf_child(die, ¤t)); + while (!res) { + if (match(¤t)) { + /* <0 = error, 0 = continue, >0 = stop */ + res = checkp(func(state, ¤t)); + if (res) + return res; + } + + res = checkp(dwarf_siblingof(¤t, ¤t)); + } + + return 0; +} + +/* + * Exported symbol processing + */ +static void process_symbol(struct state *state, Dwarf_Die *die, + die_callback_t process_func) +{ + debug("%s", state->sym->name); + check(process_func(state, die)); + if (dump_dies) + fputs("\n", stderr); +} + +static int __process_subprogram(struct state *state, Dwarf_Die *die) +{ + process("subprogram"); + return 0; +} + +static void process_subprogram(struct state *state, Dwarf_Die *die) +{ + process_symbol(state, die, __process_subprogram); +} + +static int __process_variable(struct state *state, Dwarf_Die *die) +{ + process("variable "); + return 0; +} + +static void process_variable(struct state *state, Dwarf_Die *die) +{ + process_symbol(state, die, __process_variable); +} + +static int process_exported_symbols(struct state *unused, 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( + NULL, die, process_exported_symbols, match_all)); + + /* Possible exported symbols */ + case DW_TAG_subprogram: + case DW_TAG_variable: { + struct state state; + + if (!match_export_symbol(&state, die)) + return 0; + + if (tag == DW_TAG_subprogram) + process_subprogram(&state, &state.die); + else + process_variable(&state, &state.die); + + return 0; + } + default: + return 0; + } +} + +void process_cu(Dwarf_Die *cudie) +{ + check(process_die_container(NULL, cudie, process_exported_symbols, + match_all)); +} diff --git a/scripts/gendwarfksyms/gendwarfksyms.c b/scripts/gendwarfksyms/gendwarfksyms.c new file mode 100644 index 000000000000..f84fa98fcbdb --- /dev/null +++ b/scripts/gendwarfksyms/gendwarfksyms.c @@ -0,0 +1,126 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Google LLC + */ + +#include +#include +#include +#include +#include +#include +#include "gendwarfksyms.h" + +/* + * Options + */ + +/* Print debugging information to stderr */ +int debug; +/* Dump DIE contents */ +int dump_dies; + +static void usage(void) +{ + fputs("Usage: gendwarfksyms [options] elf-object-file ... < symbol-list\n\n" + "Options:\n" + " -d, --debug Print debugging information\n" + " --dump-dies Dump DWARF DIE contents\n" + " -h, --help Print this message\n" + "\n", + stderr); +} + +static int process_module(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?"); + if (res == 1) + break; /* No more units */ + + process_cu(&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, char **argv) +{ + unsigned int n; + int opt; + + struct option opts[] = { { "debug", 0, NULL, 'd' }, + { "dump-dies", 0, &dump_dies, 1 }, + { "help", 0, NULL, 'h' }, + { 0, 0, NULL, 0 } }; + + while ((opt = getopt_long(argc, argv, "dh", opts, NULL)) != EOF) { + switch (opt) { + case 0: + break; + case 'd': + debug = 1; + break; + case 'h': + usage(); + return 0; + default: + usage(); + return 1; + } + } + + if (optind >= argc) { + usage(); + error("no input files?"); + } + + symbol_read_exports(stdin); + + for (n = optind; n < argc; n++) { + Dwfl *dwfl; + int fd; + + fd = open(argv[n], O_RDONLY); + if (fd == -1) + error("open failed for '%s': %s", argv[n], + strerror(errno)); + + dwfl = dwfl_begin(&callbacks); + if (!dwfl) + error("dwfl_begin failed for '%s': %s", argv[n], + dwarf_errmsg(-1)); + + if (!dwfl_report_offline(dwfl, argv[n], argv[n], fd)) + error("dwfl_report_offline failed for '%s': %s", + argv[n], dwarf_errmsg(-1)); + + dwfl_report_end(dwfl, NULL, NULL); + + if (dwfl_getmodules(dwfl, &process_module, NULL, 0)) + error("dwfl_getmodules failed for '%s'", argv[n]); + + dwfl_end(dwfl); + } + + symbol_free(); + + return 0; +} diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h new file mode 100644 index 000000000000..23e484af5d22 --- /dev/null +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -0,0 +1,100 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2024 Google LLC + */ + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#ifndef __GENDWARFKSYMS_H +#define __GENDWARFKSYMS_H + +/* + * Options -- in gendwarfksyms.c + */ +extern int debug; +extern int dump_dies; + +/* + * 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, ...) \ + do { \ + __println("error: ", format, ##__VA_ARGS__); \ + exit(1); \ + } while (0) + +/* + * Error handling helpers + */ +#define __check(expr, test) \ + ({ \ + int __res = expr; \ + if (test) \ + error("`%s` failed: %d", #expr, __res); \ + __res; \ + }) + +/* Error == non-zero values */ +#define check(expr) __check(expr, __res) +/* Error == negative values */ +#define checkp(expr) __check(expr, __res < 0) + +/* + * symbols.c + */ + +struct symbol { + const char *name; + struct hlist_node name_hash; +}; + +typedef void (*symbol_callback_t)(struct symbol *, void *arg); + +void symbol_read_exports(FILE *file); +struct symbol *symbol_get(const char *name); +void symbol_free(void); + +/* + * dwarf.c + */ + +struct state { + struct symbol *sym; + Dwarf_Die die; +}; + +typedef int (*die_callback_t)(struct state *state, Dwarf_Die *die); +typedef bool (*die_match_callback_t)(Dwarf_Die *die); +bool match_all(Dwarf_Die *die); + +int process_die_container(struct state *state, Dwarf_Die *die, + die_callback_t func, die_match_callback_t match); + +void process_cu(Dwarf_Die *cudie); + +#endif /* __GENDWARFKSYMS_H */ diff --git a/scripts/gendwarfksyms/symbols.c b/scripts/gendwarfksyms/symbols.c new file mode 100644 index 000000000000..2c901670224b --- /dev/null +++ b/scripts/gendwarfksyms/symbols.c @@ -0,0 +1,96 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Google LLC + */ + +#include "gendwarfksyms.h" + +#define SYMBOL_HASH_BITS 15 +static HASHTABLE_DEFINE(symbol_names, 1 << SYMBOL_HASH_BITS); + +static unsigned int for_each(const char *name, symbol_callback_t func, + void *data) +{ + struct hlist_node *tmp; + struct symbol *match; + + if (!name || !*name) + return 0; + + hash_for_each_possible_safe(symbol_names, match, tmp, name_hash, + hash_str(name)) { + if (strcmp(match->name, name)) + continue; + + if (func) + func(match, data); + + return 1; + } + + return 0; +} + +static bool is_exported(const char *name) +{ + return for_each(name, NULL, NULL) > 0; +} + +void symbol_read_exports(FILE *file) +{ + struct symbol *sym; + char *line = NULL; + char *name = NULL; + size_t size = 0; + int nsym = 0; + + while (getline(&line, &size, file) > 0) { + if (sscanf(line, "%ms\n", &name) != 1) + error("malformed input line: %s", line); + + if (is_exported(name)) { + /* Ignore duplicates */ + free(name); + continue; + } + + sym = xcalloc(1, sizeof(struct symbol)); + sym->name = name; + + hash_add(symbol_names, &sym->name_hash, hash_str(sym->name)); + ++nsym; + + debug("%s", sym->name); + } + + free(line); + debug("%d exported symbols", nsym); +} + +static void get_symbol(struct symbol *sym, void *arg) +{ + struct symbol **res = arg; + + *res = sym; +} + +struct symbol *symbol_get(const char *name) +{ + struct symbol *sym = NULL; + + for_each(name, get_symbol, &sym); + return sym; +} + +void symbol_free(void) +{ + struct hlist_node *tmp; + struct symbol *sym; + + hash_for_each_safe(symbol_names, sym, tmp, name_hash) { + free((void *)sym->name); + free(sym); + } + + hash_init(symbol_names); +} From patchwork Thu Nov 21 20:42:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13882340 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 200931D14E4 for ; Thu, 21 Nov 2024 20:42:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221762; cv=none; b=KP916PcsUYIgCuy7AyJlOaQaWl6+qJNBx9+DDKGtSCtpYJOHsH5chX0bEvaXNaQeu+zEP7BJ/mk4gBybRZW5P5OijMHXAd+jtJp2CNVgJUlOm4xiVWr/BoFEiJ7wOgYSLpBiymLyT0pA8l4aU3gBPRfmpfRCF47ZFiHNiiW2IvU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221762; c=relaxed/simple; bh=Y1QCUP2Re4IRy6UVi3VRwspdBWOnfLDfsjech3YcH1k=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=dkKKnH4pRseUp9/besbgqZqNiwjQ4uCgELRy2yQfRi4qXNvHVTCsCfl08V4OTcDVELLf7yGNOW9KLBm7FAFxeA+q+afNoGsaEoka2O01jLqo3hkPYOXBORPG4mBcBKlUh9rxgxdGR+4gtkCDkORwx8RmmVvuuCzcIpkFO9fYmrk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=4BJXWc8g; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="4BJXWc8g" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6eeb23afb49so23194057b3.3 for ; Thu, 21 Nov 2024 12:42:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732221759; x=1732826559; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=qQqxFjP+g20UF2jMStPlLsQIq6BeQ7yhwkU25eTMd54=; b=4BJXWc8gkMJtLkIyNTe4cuBUrhEEcfGQzD1BFTDQU7MIOeVivw38izwvu8AXXTWPXP 24eYzdt3OdcO9ukwlkPZAIJnSLQfMjbH3D4F+4rE60Lhczx+QG+3RGFIytQg3GdmeYYJ +Ghmpel3IvHelXCAWtVJlQN8CQ3l0ffBsjDbCUieY20Y1CsJo0ilLpAjWvTZfZs54AgS nVI6lyeyJvr0RbvYtD3aIseHs/FE9bHWLczNhPR5t9g1gAHmVOZ2/rVF+Kxb3nvLNTV8 wcjaRe0fbg3ZgzV9If7J22wv6oKzEjlhwmUmqkjoGQMQJabge56WFMpscyP3WmN4sgk8 xp6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732221759; x=1732826559; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=qQqxFjP+g20UF2jMStPlLsQIq6BeQ7yhwkU25eTMd54=; b=O7BH3NSXfU7pbei+58g8dScB2c05GYAg/ADwU+Zt/B/pZWC37LiygUMWE8y5nMGHEf 1ulpPpCAyeNJjVCABhKn1upd4z5ueHxEgnj6EfBlJcPGYMO7mVN2d0tuFJGpuFDB6kqa 4VHh0jp6pDgjsbHMzmBuQ6unhKtfEqZ0wVJXspXQVQa3Konix6x0HqTfIBkUjC8la8i7 KgdT7pR6cI0KcaCTXWzKs2BVeZ5imnPEeCw9ncGoSffLqE5oUZLM676Orml83rLqapzh dqO+O74boRUrQFgqm60ijGN/sZwCEWEy6lxrT0avgzxa6yRojac/dftaNJlXV56edP7C upnA== X-Forwarded-Encrypted: i=1; AJvYcCVvzGvDANstLO8KqrHx6h2IRm/gtYm1EUpksxRX4Ma13k+vs97jKC/48AtuO14uBRKDAuS139S+zCqikfpD@vger.kernel.org X-Gm-Message-State: AOJu0Yya7BzpbBQRIOl0EtnfC747rrRB4ft/RRiimHM36KHHAsXPfxup V27P7y4Hpwz7zHUdvfBzPaAxy7kFypCXbPSj1prItn1ITYVcHVzU6Y6CqcxJz5uWrvaUMyV3d+A 277Ry5E2PIhzn+4XCElyMNC4HCw== X-Google-Smtp-Source: AGHT+IEoa76F55eo3HyrISkKkt6UN1lROXcZ/ENWl8eQ58qrymBXkLQ2Gb/EQ+zL4QtprGCeNONXUmu6vJU43cfZO6Q= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a05:690c:988:b0:6e3:b93:3ae2 with SMTP id 00721157ae682-6eee0874a83mr3757b3.1.1732221759350; Thu, 21 Nov 2024 12:42:39 -0800 (PST) Date: Thu, 21 Nov 2024 20:42:23 +0000 In-Reply-To: <20241121204220.2378181-20-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121204220.2378181-20-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=7286; i=samitolvanen@google.com; h=from:subject; bh=Y1QCUP2Re4IRy6UVi3VRwspdBWOnfLDfsjech3YcH1k=; b=owGbwMvMwCEWxa662nLh8irG02pJDOn2s3Vvubi0HurytY2ZLsP3w8lE4mz5M//6yg1vI37Lb lrj6VXfUcrCIMbBICumyNLydfXW3d+dUl99LpKAmcPKBDKEgYtTACYiepLhf5G+iZXkHeNi4XXb fjvtrSt+vOmfy77K2xmTQ3tNW0LUHzP807sq/Ku58I7g+9dOF1k/q3lle52SVquJWt2enb9+1fI SZgA= X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121204220.2378181-22-samitolvanen@google.com> Subject: [PATCH v6 02/18] gendwarfksyms: Add address matching From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen The compiler may choose not to emit type information in DWARF for all aliases, but it's possible for each alias to be exported separately. To ensure we find type information for the aliases as well, read {section, address} tuples from the symbol table and match symbols also by address. Signed-off-by: Sami Tolvanen Reviewed-by: Petr Pavlu --- scripts/gendwarfksyms/gendwarfksyms.c | 2 + scripts/gendwarfksyms/gendwarfksyms.h | 13 +++ scripts/gendwarfksyms/symbols.c | 161 ++++++++++++++++++++++++++ 3 files changed, 176 insertions(+) diff --git a/scripts/gendwarfksyms/gendwarfksyms.c b/scripts/gendwarfksyms/gendwarfksyms.c index f84fa98fcbdb..1763234b6329 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.c +++ b/scripts/gendwarfksyms/gendwarfksyms.c @@ -103,6 +103,8 @@ int main(int argc, char **argv) error("open failed for '%s': %s", argv[n], strerror(errno)); + symbol_read_symtab(fd); + dwfl = dwfl_begin(&callbacks); if (!dwfl) error("dwfl_begin failed for '%s': %s", argv[n], diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h index 23e484af5d22..c9e268b21777 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -68,14 +68,27 @@ extern int dump_dies; * symbols.c */ +static inline unsigned int addr_hash(uintptr_t addr) +{ + return hash_ptr((const void *)addr); +} + +struct symbol_addr { + uint32_t section; + Elf64_Addr address; +}; + struct symbol { const char *name; + struct symbol_addr addr; + struct hlist_node addr_hash; struct hlist_node name_hash; }; typedef void (*symbol_callback_t)(struct symbol *, void *arg); void symbol_read_exports(FILE *file); +void symbol_read_symtab(int fd); struct symbol *symbol_get(const char *name); void symbol_free(void); diff --git a/scripts/gendwarfksyms/symbols.c b/scripts/gendwarfksyms/symbols.c index 2c901670224b..e7343394248b 100644 --- a/scripts/gendwarfksyms/symbols.c +++ b/scripts/gendwarfksyms/symbols.c @@ -6,8 +6,39 @@ #include "gendwarfksyms.h" #define SYMBOL_HASH_BITS 15 + +/* struct symbol_addr -> struct symbol */ +static HASHTABLE_DEFINE(symbol_addrs, 1 << SYMBOL_HASH_BITS); +/* name -> struct symbol */ static HASHTABLE_DEFINE(symbol_names, 1 << SYMBOL_HASH_BITS); +static inline unsigned int symbol_addr_hash(const struct symbol_addr *addr) +{ + return hash_32(addr->section ^ addr_hash(addr->address)); +} + +static unsigned int __for_each_addr(struct symbol *sym, symbol_callback_t func, + void *data) +{ + struct hlist_node *tmp; + struct symbol *match = NULL; + unsigned int processed = 0; + + hash_for_each_possible_safe(symbol_addrs, match, tmp, addr_hash, + symbol_addr_hash(&sym->addr)) { + if (match == sym) + continue; /* Already processed */ + + if (match->addr.section == sym->addr.section && + match->addr.address == sym->addr.address) { + func(match, data); + ++processed; + } + } + + return processed; +} + static unsigned int for_each(const char *name, symbol_callback_t func, void *data) { @@ -22,9 +53,13 @@ static unsigned int for_each(const char *name, symbol_callback_t func, if (strcmp(match->name, name)) continue; + /* Call func for the match, and all address matches */ if (func) func(match, data); + if (match->addr.section != SHN_UNDEF) + return __for_each_addr(match, func, data) + 1; + return 1; } @@ -56,6 +91,7 @@ void symbol_read_exports(FILE *file) sym = xcalloc(1, sizeof(struct symbol)); sym->name = name; + sym->addr.section = SHN_UNDEF; hash_add(symbol_names, &sym->name_hash, hash_str(sym->name)); ++nsym; @@ -82,6 +118,130 @@ struct symbol *symbol_get(const char *name) return sym; } +typedef void (*elf_symbol_callback_t)(const char *name, GElf_Sym *sym, + Elf32_Word xndx, void *arg); + +static void elf_for_each_global(int fd, elf_symbol_callback_t func, void *arg) +{ + size_t sym_size; + GElf_Shdr shdr_mem; + GElf_Shdr *shdr; + Elf_Data *xndx_data = NULL; + Elf_Scn *scn; + Elf *elf; + + if (elf_version(EV_CURRENT) != EV_CURRENT) + error("elf_version failed: %s", elf_errmsg(-1)); + + elf = elf_begin(fd, ELF_C_READ_MMAP, NULL); + if (!elf) + error("elf_begin failed: %s", elf_errmsg(-1)); + + scn = elf_nextscn(elf, NULL); + + while (scn) { + shdr = gelf_getshdr(scn, &shdr_mem); + if (!shdr) + error("gelf_getshdr failed: %s", elf_errmsg(-1)); + + if (shdr->sh_type == SHT_SYMTAB_SHNDX) { + xndx_data = elf_getdata(scn, NULL); + if (!xndx_data) + error("elf_getdata failed: %s", elf_errmsg(-1)); + break; + } + + scn = elf_nextscn(elf, scn); + } + + sym_size = gelf_fsize(elf, ELF_T_SYM, 1, EV_CURRENT); + scn = elf_nextscn(elf, NULL); + + while (scn) { + shdr = gelf_getshdr(scn, &shdr_mem); + if (!shdr) + error("gelf_getshdr failed: %s", elf_errmsg(-1)); + + if (shdr->sh_type == SHT_SYMTAB) { + unsigned int nsyms; + unsigned int n; + Elf_Data *data = elf_getdata(scn, NULL); + + if (!data) + error("elf_getdata failed: %s", elf_errmsg(-1)); + + if (shdr->sh_entsize != sym_size) + error("expected sh_entsize (%lu) to be %zu", + shdr->sh_entsize, sym_size); + + nsyms = shdr->sh_size / shdr->sh_entsize; + + for (n = 1; n < nsyms; ++n) { + const char *name = NULL; + Elf32_Word xndx = 0; + GElf_Sym sym_mem; + GElf_Sym *sym; + + sym = gelf_getsymshndx(data, xndx_data, n, + &sym_mem, &xndx); + + if (!sym || + GELF_ST_BIND(sym->st_info) == STB_LOCAL) + continue; + + if (sym->st_shndx != SHN_XINDEX) + xndx = sym->st_shndx; + + name = elf_strptr(elf, shdr->sh_link, + sym->st_name); + if (!name) + error("elf_strptr failed: %s", + elf_errmsg(-1)); + + /* Skip empty symbol names */ + if (*name) + func(name, sym, xndx, arg); + } + } + + scn = elf_nextscn(elf, scn); + } + + check(elf_end(elf)); +} + +static void set_symbol_addr(struct symbol *sym, void *arg) +{ + struct symbol_addr *addr = arg; + + if (sym->addr.section == SHN_UNDEF) { + sym->addr = *addr; + hash_add(symbol_addrs, &sym->addr_hash, + symbol_addr_hash(&sym->addr)); + + debug("%s -> { %u, %lx }", sym->name, sym->addr.section, + sym->addr.address); + } else if (sym->addr.section != addr->section || + sym->addr.address != addr->address) { + warn("multiple addresses for symbol %s?", sym->name); + } +} + +static void elf_set_symbol_addr(const char *name, GElf_Sym *sym, + Elf32_Word xndx, void *arg) +{ + struct symbol_addr addr = { .section = xndx, .address = sym->st_value }; + + /* Set addresses for exported symbols */ + if (addr.section != SHN_UNDEF) + for_each(name, set_symbol_addr, &addr); +} + +void symbol_read_symtab(int fd) +{ + elf_for_each_global(fd, elf_set_symbol_addr, NULL); +} + void symbol_free(void) { struct hlist_node *tmp; @@ -92,5 +252,6 @@ void symbol_free(void) free(sym); } + hash_init(symbol_addrs); hash_init(symbol_names); } From patchwork Thu Nov 21 20:42:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13882341 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F3E5F1D9598 for ; Thu, 21 Nov 2024 20:42:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221763; cv=none; b=Cv4NdmW9yNMoQinjoXTYknNYb4nfkEelGW+e8TnodGLdhJRD+Xe+Jz67L3Rn23tr/i2lXMwb/18bTcnhb1t6M8pPu1RsTcjmfowO3ikwunHdXHIn/vQsZ6zw64hgBEfcOSIGJPXrnXFJtO5xfkhBWY1PlHHorvqrAEK1zcp4fm0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221763; c=relaxed/simple; bh=YLJdfdCFIGfBHV32JhYUDE8cBaZqbdrie/OHz/meoLY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=JOldTHcAq6SDxzJehHpJG1Um2qGACah+cVhEGkahzeVAbVC2L2Exr+xBPaGtBIMYR+kPelo860LA7o9rL73B7dUazCoNxRZJo5Y7bqSctQohXZh1OpAdtgx2Cfu4bVV700gUCc1T88PzZdIVgq0tFbKfhp+I7dfhWFJ+kUdLVxo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=2q9I/cjj; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="2q9I/cjj" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e02fff66a83so1970785276.0 for ; Thu, 21 Nov 2024 12:42:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732221761; x=1732826561; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=i1SGd7zuO0/DZ7HGvsMr06GJxdYWCHn3MBdi/eJHnfA=; b=2q9I/cjjqTzC8shB8ULaB6YlFqAUfjal22nVx8XjdKSV8D4abY77gGTM/yMlHCjKHF CV1GmQpb7yvbEiu5q9biuOQ+U1FRTWaE36aWEI1Rm3TJedpy/PRmXhZoYF7GvCsj1uc6 UGYeMDAArWEF5KUwDU/N0jegSqKjkuxgpqmFZzgVwhYhPsWsWyjyjadpIJiUVybDOVtD Xgkg3DT86XvS9jKL2/CIYYGJXELdsE8BUhQ/1kQMzFIwZoVpB0Bembq/2mUpZce7eW/L 6H4RAt3ck/mKicNufCpfJlL7sMsWe333XiPYAS2fABke6uLx4M7V5jo5F9Ha/To7qNIh foww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732221761; x=1732826561; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=i1SGd7zuO0/DZ7HGvsMr06GJxdYWCHn3MBdi/eJHnfA=; b=MA/Fde0n9Hy6qLWIRt7pu6EvoWTWcm4SO+Ls2oSN9TM0RHnO+YtGWHwpLIq/+oIyQB I+NjI+ViI4r57BRn6AMUNGORAgRoDFga/CiaCAIuBBWzjXKcBLiyqc9oqalBv2PavZ2q DxRe0WTa6UwLRejZFjtYM/kDOnXotGqRg2sL6DOIUJ1jyaeD1pv4ZocbSHhImXTACwZa HyEEgsnkw8I+3Emn5oyzYUCpUuN1WJfB5Bld/EpiOECv/zNbcQQe2tOu+qlTfGKeNagP D9wWfMxrdqQlbj/tWv5dV7cxFToGqtXFRcCClgJ2Lv1HMu7YU/EgGRUrTKVCGJS072+f K3eg== X-Forwarded-Encrypted: i=1; AJvYcCVmiYnlro+KJiwEirAbWFKS2tS6rUl7h2Wfomb8mTDNIfqUPoO0rCw0+ENE40CQLjD8nCyFnvDOUWcvZNBt@vger.kernel.org X-Gm-Message-State: AOJu0Yz8PeglXah8ZUdNf9Cs5/VufcWBF1aW6sWru5HDUhoha82NoVtm WQcS9UaIr2mAImllZd1WOfIE/XNcyGSBWcE3IKhh7TsuynCLAuipLBFUuxJh0/X/sglu/njUcaQ ZXNxD/ptsddccPTR8kdM5chVm1w== X-Google-Smtp-Source: AGHT+IH4Q1/bX4EFh9SebSmqU9mdVJG1sem5OGMVNVYHf6JS02iswSSOxbwRKS0FKpeZfWZXlxIAPRNpJS49psl1FKc= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a25:accf:0:b0:e38:efbd:21af with SMTP id 3f1490d57ef6-e38f887d422mr160276.0.1732221761078; Thu, 21 Nov 2024 12:42:41 -0800 (PST) Date: Thu, 21 Nov 2024 20:42:24 +0000 In-Reply-To: <20241121204220.2378181-20-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121204220.2378181-20-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=5381; i=samitolvanen@google.com; h=from:subject; bh=YLJdfdCFIGfBHV32JhYUDE8cBaZqbdrie/OHz/meoLY=; b=owGbwMvMwCEWxa662nLh8irG02pJDOn2s/VO3bH7VOG0oDed5+li3m1TOkqPcTC2cnvwGPHMz ZHOmHOio5SFQYyDQVZMkaXl6+qtu787pb76XCQBM4eVCWQIAxenAEyky4nhv1PDxZWq+cE3F66u kd/INDHLS0fPZa/4dO2GfKnnxSpM3xkZHr2d6hjnN23qpivGN+2L5UTvyzR+kynk0Z2+xzf6hks QAwA= X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121204220.2378181-23-samitolvanen@google.com> Subject: [PATCH v6 03/18] gendwarfksyms: Expand base_type From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Start making gendwarfksyms more useful by adding support for expanding DW_TAG_base_type types and basic DWARF attributes. Example: $ echo loops_per_jiffy | \ scripts/gendwarfksyms/gendwarfksyms \ --debug --dump-dies vmlinux.o ... gendwarfksyms: process_symbol: loops_per_jiffy variable base_type unsigned long byte_size(8) encoding(7) ... Signed-off-by: Sami Tolvanen Reviewed-by: Petr Pavlu --- scripts/gendwarfksyms/dwarf.c | 159 ++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index 81df3e2ad3ae..35fd1dfeeadc 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -3,8 +3,20 @@ * Copyright (C) 2024 Google LLC */ +#include #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(udata, Dwarf_Word) + static bool get_ref_die_attr(Dwarf_Die *die, unsigned int id, Dwarf_Die *value) { Dwarf_Attribute da; @@ -67,6 +79,109 @@ static void process(const char *s) fputs(s, stderr); } +#define MAX_FMT_BUFFER_SIZE 128 + +static void process_fmt(const char *fmt, ...) +{ + char buf[MAX_FMT_BUFFER_SIZE]; + va_list args; + + va_start(args, fmt); + + if (checkp(vsnprintf(buf, sizeof(buf), fmt, args)) >= sizeof(buf)) + error("vsnprintf overflow: increase MAX_FMT_BUFFER_SIZE"); + + process(buf); + va_end(args); +} + +#define MAX_FQN_SIZE 64 + +/* Get a fully qualified name from DWARF scopes */ +static char *get_fqn(Dwarf_Die *die) +{ + const char *list[MAX_FQN_SIZE]; + Dwarf_Die *scopes = NULL; + bool has_name = false; + char *fqn = NULL; + char *p; + int count = 0; + int len = 0; + int res; + int i; + + res = checkp(dwarf_getscopes_die(die, &scopes)); + if (!res) { + list[count] = get_name_attr(die); + + if (!list[count]) + return NULL; + + len += strlen(list[count]); + count++; + + goto done; + } + + for (i = res - 1; i >= 0 && count < MAX_FQN_SIZE; i--) { + if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit) + continue; + + list[count] = get_name_attr(&scopes[i]); + + if (list[count]) { + has_name = true; + } else { + list[count] = ""; + has_name = false; + } + + len += strlen(list[count]); + count++; + + if (i > 0) { + list[count++] = "::"; + len += 2; + } + } + + free(scopes); + + if (count == MAX_FQN_SIZE) + warn("increase MAX_FQN_SIZE: reached the maximum"); + + /* Consider the DIE unnamed if the last scope doesn't have a name */ + if (!has_name) + return NULL; +done: + fqn = xmalloc(len + 1); + *fqn = '\0'; + + p = fqn; + for (i = 0; i < count; i++) + p = stpcpy(p, list[i]); + + return fqn; +} + +static void process_fqn(Dwarf_Die *die) +{ + process(" "); + process(get_fqn(die) ?: ""); +} + +#define DEFINE_PROCESS_UDATA_ATTRIBUTE(attribute) \ + static void process_##attribute##_attr(Dwarf_Die *die) \ + { \ + Dwarf_Word value; \ + if (get_udata_attr(die, DW_AT_##attribute, &value)) \ + process_fmt(" " #attribute "(%" PRIu64 ")", value); \ + } + +DEFINE_PROCESS_UDATA_ATTRIBUTE(alignment) +DEFINE_PROCESS_UDATA_ATTRIBUTE(byte_size) +DEFINE_PROCESS_UDATA_ATTRIBUTE(encoding) + bool match_all(Dwarf_Die *die) { return true; @@ -93,6 +208,49 @@ int process_die_container(struct state *state, Dwarf_Die *die, return 0; } +static int process_type(struct state *state, Dwarf_Die *die); + +static void process_type_attr(struct state *state, Dwarf_Die *die) +{ + Dwarf_Die type; + + if (get_ref_die_attr(die, DW_AT_type, &type)) { + check(process_type(state, &type)); + return; + } + + /* Compilers can omit DW_AT_type -- print out 'void' to clarify */ + process("base_type void"); +} + +static void process_base_type(struct state *state, Dwarf_Die *die) +{ + process("base_type"); + process_fqn(die); + process_byte_size_attr(die); + process_encoding_attr(die); + process_alignment_attr(die); +} + +#define PROCESS_TYPE(type) \ + case DW_TAG_##type##_type: \ + process_##type##_type(state, die); \ + break; + +static int process_type(struct state *state, Dwarf_Die *die) +{ + int tag = dwarf_tag(die); + + switch (tag) { + PROCESS_TYPE(base) + default: + debug("unimplemented type: %x", tag); + break; + } + + return 0; +} + /* * Exported symbol processing */ @@ -119,6 +277,7 @@ static void process_subprogram(struct state *state, Dwarf_Die *die) static int __process_variable(struct state *state, Dwarf_Die *die) { process("variable "); + process_type_attr(state, die); return 0; } From patchwork Thu Nov 21 20:42:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13882342 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 49BA51DD0F2 for ; Thu, 21 Nov 2024 20:42:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221767; cv=none; b=rxJW3EGMYoUxArllAG/GPuWxBxQC9/f15Gl6i+Qswu30tT4Yjwl3NzG95GvvSexebGzyiVKpyah5p3XAr9Kfe3ZdQK3CXKb1ixVu4+xxGSVW8+kDo5PI68ze1vnrq7UYeip3vVjBFAik785lH6UL8RFGHQ0e2pPH/wKpN94kReg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221767; c=relaxed/simple; bh=2bHXvNuWgbxgItnTocYyzXf6yos8BVnyD154tNBfkfk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=peyp7B1a/RRv43LhYEzrXkafhZSdfx4fWRX7LbNmeWP/8fUxbaJPOpfSeqpBz8qFEkjcSO3JlGo7XPy4PEFvpScr5VFO57/yZ5khy5Gn1ZlsG9qn551tgs8dcI6aZ8/yxxuApvALw8X0qpQUwwrfpybafCWbH0ZZTshJW2nN21Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=IOwlDtZ5; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="IOwlDtZ5" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6eec923c760so17034907b3.1 for ; Thu, 21 Nov 2024 12:42:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732221763; x=1732826563; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=JtYAZAU375gbYRbo6MLxG5dFymV2qUIscVG15MsR9vg=; b=IOwlDtZ5pW3Lv2iXZ6JQUuV31pDdkYAupBSyani3FCnxS/+xVxImYaw4K2O2PWvsK0 Fa++vmi4DHg6L7Qc+YhVui45pV2aGbAeOddsQRxtyzbN2bnLk6v8bDD3SGt4Q+hThViM HMvCQzt5Jv6dizFdbJ1KSCIY6H7W2NRomrtIDze6yOOet9NVvFTx1TzN/UIACUlDcJrJ 94FP3mRsfSI/6NnywkJX10RrScN4S+vfq977LtnndlnJL2rQZjwTSiSoR8w1YaKWuTBa wGkoUVP9TzDMCFM0FWhiZC/6SZ2LOIVKDRa/GRsIh4BDh0JCSkhxZ6lYRJpyhr7WIj2u xz9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732221763; x=1732826563; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=JtYAZAU375gbYRbo6MLxG5dFymV2qUIscVG15MsR9vg=; b=OlxVFQZdmgxczuF8k57hPqKtcTwU5H19WuEbaIuiL2D2OlmUXBaLI3QH3czCXlYO1r 3daVSO/g8EK4XWN0ZhWpx5tIE9GkL/I89cP4OFfdn9SvIjixPP+ZDYPtSdECxEftjRX7 pAClMEqi3Uv16fZExrIl3sLoDDV0arCZwp/mWwN3YaLoX0gdzeKw6VqcqGnh4ogjFB9b 2SaePfchc9A28cyFWmehChRTj+aW8vTvrGDjiEk1qJtXboI6dkFMryRbtJflHkfdD01Y S+6L6WYPnSFVRMpVQDqAyK8KSjTjK5957l2RXlmyGy1LVSa71HmynEV5W3+TXKMeWFKZ IHBA== X-Forwarded-Encrypted: i=1; AJvYcCV26kMtadr2aTCbN0L98l5VOOUDQZ2TsrsZGMdWsSJC477jsfsyxXLX7aONey/tyEGssCCtaLR6hS1E/O5I@vger.kernel.org X-Gm-Message-State: AOJu0YxWoTfAQKWxda6QrLixFOz6a/3OMDmEBJ9v5OlqaplRUug2BgPm PhchPuXgGGh0SbWBrc5T34OhgmyfOEWP1r5/ngGSx42cn8Jc0VR776ryh3pkDH1/BCXOsoRiI5h T5KERh+bOxnEKQNxLR9lL+bBJLA== X-Google-Smtp-Source: AGHT+IFMqIcJxmR6v1MVg0u1OiaiXXANBpT5Tu4PyabUIGIO69woF7poDyCQhWlc2BABb34QhnjTolV9KXJhwReGh+A= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a25:6850:0:b0:e38:8f16:6465 with SMTP id 3f1490d57ef6-e38f8bfde8fmr136276.8.1732221762756; Thu, 21 Nov 2024 12:42:42 -0800 (PST) Date: Thu, 21 Nov 2024 20:42:25 +0000 In-Reply-To: <20241121204220.2378181-20-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121204220.2378181-20-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=15461; i=samitolvanen@google.com; h=from:subject; bh=2bHXvNuWgbxgItnTocYyzXf6yos8BVnyD154tNBfkfk=; b=owGbwMvMwCEWxa662nLh8irG02pJDOn2s/W+pkR9vrs8+N+OCzGPpq/fuJ1J7NzqLq9vjLbzN jvarKqe0FHKwiDGwSArpsjS8nX11t3fnVJffS6SgJnDygQyhIGLUwAmUmjMyLBh6slZ8yKFKh/f tay6Ju1w1e/clCcKjNPt67vm6J9pvX+PkeFYwzKhSKeWedJbN/J/ni5405jl8+aFVmsNHFo9dzK +FWYDAA== X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121204220.2378181-24-samitolvanen@google.com> Subject: [PATCH v6 04/18] gendwarfksyms: Add a cache for processed DIEs From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Basic types in DWARF repeat frequently and traversing the DIEs using libdw is relatively slow. Add a simple hashtable based cache for the processed DIEs. Signed-off-by: Sami Tolvanen Reviewed-by: Petr Pavlu --- scripts/gendwarfksyms/Makefile | 1 + scripts/gendwarfksyms/die.c | 143 ++++++++++++++++++++++++++ scripts/gendwarfksyms/dwarf.c | 136 +++++++++++++++++------- scripts/gendwarfksyms/gendwarfksyms.c | 6 ++ scripts/gendwarfksyms/gendwarfksyms.h | 63 +++++++++++- 5 files changed, 308 insertions(+), 41 deletions(-) create mode 100644 scripts/gendwarfksyms/die.c diff --git a/scripts/gendwarfksyms/Makefile b/scripts/gendwarfksyms/Makefile index 9f8fec4fd39b..c0d4ce50fc27 100644 --- a/scripts/gendwarfksyms/Makefile +++ b/scripts/gendwarfksyms/Makefile @@ -2,6 +2,7 @@ hostprogs-always-y += gendwarfksyms gendwarfksyms-objs += gendwarfksyms.o +gendwarfksyms-objs += die.o gendwarfksyms-objs += dwarf.o gendwarfksyms-objs += symbols.o diff --git a/scripts/gendwarfksyms/die.c b/scripts/gendwarfksyms/die.c new file mode 100644 index 000000000000..28d89fce89fc --- /dev/null +++ b/scripts/gendwarfksyms/die.c @@ -0,0 +1,143 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Google LLC + */ + +#include +#include "gendwarfksyms.h" + +#define DIE_HASH_BITS 20 + +/* {die->addr, state} -> struct die * */ +static HASHTABLE_DEFINE(die_map, 1 << DIE_HASH_BITS); + +static unsigned int map_hits; +static unsigned int map_misses; + +static inline unsigned int die_hash(uintptr_t addr, enum die_state state) +{ + return hash_32(addr_hash(addr) ^ (unsigned int)state); +} + +static void init_die(struct die *cd) +{ + cd->state = DIE_INCOMPLETE; + cd->fqn = NULL; + cd->tag = -1; + cd->addr = 0; + INIT_LIST_HEAD(&cd->fragments); +} + +static struct die *create_die(Dwarf_Die *die, enum die_state state) +{ + struct die *cd; + + cd = xmalloc(sizeof(struct die)); + init_die(cd); + cd->addr = (uintptr_t)die->addr; + + hash_add(die_map, &cd->hash, die_hash(cd->addr, state)); + return cd; +} + +int __die_map_get(uintptr_t addr, enum die_state state, struct die **res) +{ + struct die *cd; + + hash_for_each_possible(die_map, cd, hash, die_hash(addr, state)) { + if (cd->addr == addr && cd->state == state) { + *res = cd; + return 0; + } + } + + return -1; +} + +struct die *die_map_get(Dwarf_Die *die, enum die_state state) +{ + struct die *cd; + + if (__die_map_get((uintptr_t)die->addr, state, &cd) == 0) { + map_hits++; + return cd; + } + + map_misses++; + return create_die(die, state); +} + +static void reset_die(struct die *cd) +{ + struct die_fragment *tmp; + struct die_fragment *df; + + list_for_each_entry_safe(df, tmp, &cd->fragments, list) { + if (df->type == FRAGMENT_STRING) + free(df->data.str); + free(df); + } + + if (cd->fqn && *cd->fqn) + free(cd->fqn); + init_die(cd); +} + +void die_map_free(void) +{ + struct hlist_node *tmp; + unsigned int stats[DIE_LAST + 1]; + struct die *cd; + int i; + + memset(stats, 0, sizeof(stats)); + + hash_for_each_safe(die_map, cd, tmp, hash) { + stats[cd->state]++; + reset_die(cd); + free(cd); + } + hash_init(die_map); + + if (map_hits + map_misses > 0) + debug("hits %u, misses %u (hit rate %.02f%%)", map_hits, + map_misses, + (100.0f * map_hits) / (map_hits + map_misses)); + + for (i = 0; i <= DIE_LAST; i++) + debug("%s: %u entries", die_state_name(i), stats[i]); +} + +static struct die_fragment *append_item(struct die *cd) +{ + struct die_fragment *df; + + df = xmalloc(sizeof(struct die_fragment)); + df->type = FRAGMENT_EMPTY; + list_add_tail(&df->list, &cd->fragments); + return df; +} + +void die_map_add_string(struct die *cd, const char *str) +{ + struct die_fragment *df; + + if (!cd) + return; + + df = append_item(cd); + df->data.str = xstrdup(str); + df->type = FRAGMENT_STRING; +} + +void die_map_add_die(struct die *cd, struct die *child) +{ + struct die_fragment *df; + + if (!cd) + return; + + df = append_item(cd); + df->data.addr = child->addr; + df->type = FRAGMENT_DIE; +} diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index 35fd1dfeeadc..c2cd4743515e 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -71,17 +71,19 @@ static bool match_export_symbol(struct state *state, Dwarf_Die *die) /* * Type string processing */ -static void process(const char *s) +static void process(struct die *cache, const char *s) { s = s ?: ""; if (dump_dies) fputs(s, stderr); + + die_map_add_string(cache, s); } #define MAX_FMT_BUFFER_SIZE 128 -static void process_fmt(const char *fmt, ...) +static void process_fmt(struct die *cache, const char *fmt, ...) { char buf[MAX_FMT_BUFFER_SIZE]; va_list args; @@ -91,7 +93,7 @@ static void process_fmt(const char *fmt, ...) if (checkp(vsnprintf(buf, sizeof(buf), fmt, args)) >= sizeof(buf)) error("vsnprintf overflow: increase MAX_FMT_BUFFER_SIZE"); - process(buf); + process(cache, buf); va_end(args); } @@ -164,18 +166,28 @@ static char *get_fqn(Dwarf_Die *die) return fqn; } -static void process_fqn(Dwarf_Die *die) +static void update_fqn(struct die *cache, Dwarf_Die *die) +{ + if (!cache->fqn) + cache->fqn = get_fqn(die) ?: ""; +} + +static void process_fqn(struct die *cache, Dwarf_Die *die) { - process(" "); - process(get_fqn(die) ?: ""); + update_fqn(cache, die); + if (*cache->fqn) + process(cache, " "); + process(cache, cache->fqn); } -#define DEFINE_PROCESS_UDATA_ATTRIBUTE(attribute) \ - static void process_##attribute##_attr(Dwarf_Die *die) \ - { \ - Dwarf_Word value; \ - if (get_udata_attr(die, DW_AT_##attribute, &value)) \ - process_fmt(" " #attribute "(%" PRIu64 ")", value); \ +#define DEFINE_PROCESS_UDATA_ATTRIBUTE(attribute) \ + static void process_##attribute##_attr(struct die *cache, \ + Dwarf_Die *die) \ + { \ + Dwarf_Word value; \ + if (get_udata_attr(die, DW_AT_##attribute, &value)) \ + process_fmt(cache, " " #attribute "(%" PRIu64 ")", \ + value); \ } DEFINE_PROCESS_UDATA_ATTRIBUTE(alignment) @@ -187,8 +199,9 @@ 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) +int process_die_container(struct state *state, struct die *cache, + Dwarf_Die *die, die_callback_t func, + die_match_callback_t match) { Dwarf_Die current; int res; @@ -197,7 +210,7 @@ int process_die_container(struct state *state, Dwarf_Die *die, while (!res) { if (match(¤t)) { /* <0 = error, 0 = continue, >0 = stop */ - res = checkp(func(state, ¤t)); + res = checkp(func(state, cache, ¤t)); if (res) return res; } @@ -208,39 +221,78 @@ int process_die_container(struct state *state, Dwarf_Die *die, return 0; } -static int process_type(struct state *state, Dwarf_Die *die); +static int process_type(struct state *state, struct die *parent, + Dwarf_Die *die); -static void process_type_attr(struct state *state, Dwarf_Die *die) +static void process_type_attr(struct state *state, struct die *cache, + Dwarf_Die *die) { Dwarf_Die type; if (get_ref_die_attr(die, DW_AT_type, &type)) { - check(process_type(state, &type)); + check(process_type(state, cache, &type)); return; } /* Compilers can omit DW_AT_type -- print out 'void' to clarify */ - process("base_type void"); + process(cache, "base_type void"); +} + +static void process_base_type(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + process(cache, "base_type"); + process_fqn(cache, die); + process_byte_size_attr(cache, die); + process_encoding_attr(cache, die); + process_alignment_attr(cache, die); } -static void process_base_type(struct state *state, Dwarf_Die *die) +static void process_cached(struct state *state, struct die *cache, + Dwarf_Die *die) { - process("base_type"); - process_fqn(die); - process_byte_size_attr(die); - process_encoding_attr(die); - process_alignment_attr(die); + struct die_fragment *df; + Dwarf_Die child; + + list_for_each_entry(df, &cache->fragments, list) { + switch (df->type) { + case FRAGMENT_STRING: + process(NULL, df->data.str); + break; + case FRAGMENT_DIE: + if (!dwarf_die_addr_die(dwarf_cu_getdwarf(die->cu), + (void *)df->data.addr, &child)) + error("dwarf_die_addr_die failed"); + check(process_type(state, NULL, &child)); + break; + default: + error("empty die_fragment"); + } + } } -#define PROCESS_TYPE(type) \ - case DW_TAG_##type##_type: \ - process_##type##_type(state, die); \ +#define PROCESS_TYPE(type) \ + case DW_TAG_##type##_type: \ + process_##type##_type(state, cache, die); \ break; -static int process_type(struct state *state, Dwarf_Die *die) +static int process_type(struct state *state, struct die *parent, Dwarf_Die *die) { + struct die *cache; int tag = dwarf_tag(die); + /* + * If we have the DIE already cached, use it instead of walking + * through DWARF. + */ + cache = die_map_get(die, DIE_COMPLETE); + + if (cache->state == DIE_COMPLETE) { + process_cached(state, cache, die); + die_map_add_die(parent, cache); + return 0; + } + switch (tag) { PROCESS_TYPE(base) default: @@ -248,6 +300,11 @@ static int process_type(struct state *state, Dwarf_Die *die) break; } + /* Update cache state and append to the parent (if any) */ + cache->tag = tag; + cache->state = DIE_COMPLETE; + die_map_add_die(parent, cache); + return 0; } @@ -258,14 +315,15 @@ static void process_symbol(struct state *state, Dwarf_Die *die, die_callback_t process_func) { debug("%s", state->sym->name); - check(process_func(state, die)); + check(process_func(state, NULL, die)); if (dump_dies) fputs("\n", stderr); } -static int __process_subprogram(struct state *state, Dwarf_Die *die) +static int __process_subprogram(struct state *state, struct die *cache, + Dwarf_Die *die) { - process("subprogram"); + process(cache, "subprogram"); return 0; } @@ -274,10 +332,11 @@ static void process_subprogram(struct state *state, Dwarf_Die *die) process_symbol(state, die, __process_subprogram); } -static int __process_variable(struct state *state, Dwarf_Die *die) +static int __process_variable(struct state *state, struct die *cache, + Dwarf_Die *die) { - process("variable "); - process_type_attr(state, die); + process(cache, "variable "); + process_type_attr(state, cache, die); return 0; } @@ -286,7 +345,8 @@ static void process_variable(struct state *state, Dwarf_Die *die) process_symbol(state, die, __process_variable); } -static int process_exported_symbols(struct state *unused, Dwarf_Die *die) +static int process_exported_symbols(struct state *unused, struct die *cache, + Dwarf_Die *die) { int tag = dwarf_tag(die); @@ -296,7 +356,7 @@ static int process_exported_symbols(struct state *unused, Dwarf_Die *die) case DW_TAG_class_type: case DW_TAG_structure_type: return check(process_die_container( - NULL, die, process_exported_symbols, match_all)); + NULL, cache, die, process_exported_symbols, match_all)); /* Possible exported symbols */ case DW_TAG_subprogram: @@ -320,6 +380,6 @@ static int process_exported_symbols(struct state *unused, Dwarf_Die *die) void process_cu(Dwarf_Die *cudie) { - check(process_die_container(NULL, cudie, process_exported_symbols, + check(process_die_container(NULL, NULL, cudie, process_exported_symbols, match_all)); } diff --git a/scripts/gendwarfksyms/gendwarfksyms.c b/scripts/gendwarfksyms/gendwarfksyms.c index 1763234b6329..7552fde495ef 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.c +++ b/scripts/gendwarfksyms/gendwarfksyms.c @@ -43,6 +43,10 @@ static int process_module(Dwfl_Module *mod, void **userdata, const char *name, debug("%s", name); dbg = dwfl_module_getdwarf(mod, &dwbias); + /* + * Look for exported symbols in each CU, follow the DIE tree, and add + * the entries to die_map. + */ do { res = dwarf_get_units(dbg, cu, &cu, NULL, NULL, &cudie, NULL); if (res < 0) @@ -53,6 +57,8 @@ static int process_module(Dwfl_Module *mod, void **userdata, const char *name, process_cu(&cudie); } while (cu); + die_map_free(); + return DWARF_CB_OK; } diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h index c9e268b21777..3b42616ae9b5 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -92,6 +92,61 @@ void symbol_read_symtab(int fd); struct symbol *symbol_get(const char *name); void symbol_free(void); +/* + * die.c + */ + +enum die_state { + DIE_INCOMPLETE, + DIE_COMPLETE, + DIE_LAST = DIE_COMPLETE +}; + +enum die_fragment_type { + FRAGMENT_EMPTY, + FRAGMENT_STRING, + FRAGMENT_DIE +}; + +struct die_fragment { + enum die_fragment_type type; + union { + char *str; + uintptr_t addr; + } data; + struct list_head list; +}; + +#define CASE_CONST_TO_STR(name) \ + case name: \ + return #name; + +static inline const char *die_state_name(enum die_state state) +{ + switch (state) { + CASE_CONST_TO_STR(DIE_INCOMPLETE) + CASE_CONST_TO_STR(DIE_COMPLETE) + } + + error("unexpected die_state: %d", state); +} + +struct die { + enum die_state state; + char *fqn; + int tag; + uintptr_t addr; + struct list_head fragments; + struct hlist_node hash; +}; + +int __die_map_get(uintptr_t addr, enum die_state state, struct die **res); +struct die *die_map_get(Dwarf_Die *die, enum die_state state); +void die_map_add_string(struct die *pd, const char *str); +void die_map_add_linebreak(struct die *pd, int linebreak); +void die_map_add_die(struct die *pd, struct die *child); +void die_map_free(void); + /* * dwarf.c */ @@ -101,12 +156,14 @@ struct state { Dwarf_Die die; }; -typedef int (*die_callback_t)(struct state *state, Dwarf_Die *die); +typedef int (*die_callback_t)(struct state *state, struct die *cache, + Dwarf_Die *die); typedef bool (*die_match_callback_t)(Dwarf_Die *die); bool match_all(Dwarf_Die *die); -int process_die_container(struct state *state, Dwarf_Die *die, - die_callback_t func, die_match_callback_t match); +int process_die_container(struct state *state, struct die *cache, + Dwarf_Die *die, die_callback_t func, + die_match_callback_t match); void process_cu(Dwarf_Die *cudie); From patchwork Thu Nov 21 20:42:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13882343 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD6AD1DDA37 for ; Thu, 21 Nov 2024 20:42:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221768; cv=none; b=e2DOsHjIQ+GfLo9c/IB8+OcH0hm82YVxJu6cR5VAXSMpTs/Uf+vt1Ko4iZnqjxHjAYFG0++Ntk2Wx8I6iK6IXdXVb8IcpNkCcDUCWBvKah+nEQLnJVCQp4VU0MQvRug8umI7oxy4geroQ0VDSPvier9S2XhfJOjOCcCuyghFkr8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221768; c=relaxed/simple; bh=rLGOZcCiqj2V3Mb8xMD1NQdUVcgO11MXqSLXG9dDXtw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MsesdyBezsAPFouM7wAq2hCQVtw27EbJdrJjNtILgYk7YYEORiKw3Trc/GgcfRGZ0EF17NIADg3GivTW+FPIsfAcXddZqKEKUR/An88t3IKm76nMWbQG3AmacBp/U+PLp37dYUt/T/EdqTCMx0pNZY4f3dXa/gfEEUe6JtHBWiQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=UGmYKELq; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="UGmYKELq" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6ee6e1f2d25so22466147b3.0 for ; Thu, 21 Nov 2024 12:42:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732221765; x=1732826565; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=IXC/lLCMpqr61ebokAMnr0ouaH8AUtLBWDJT+cQ9yus=; b=UGmYKELqk61m6YFlNQ6rtyrObdQzOE4logY5rEsnomfxdpYv/WIkCHSsool4rygxqJ quauoS3mqwuB+h+tx4E1mUyToOCS8+8jw4DZmjExF3kYZGuRviJqhmtxxCpgnAkDiw3n zdogu7LspTUpI3Ao/r5Xa+B851D96nKc+uXuc51c193UzCaNdg+MROfmYSO5F6um5FHq qEQeiLp0y9Ni1kcl+VgkVP2nd02LwsXXEnp2ueIy7Ko0OEXkE/YD48CSxIt+eOIZlkOl kbs3Dis2Ez59KY0iZ1rJhlf77IjKra2D0v8SWAQMproF0uaLGqG3bu7v2E88fur/Dsj5 cIUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732221765; x=1732826565; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=IXC/lLCMpqr61ebokAMnr0ouaH8AUtLBWDJT+cQ9yus=; b=fl7Vo8c2lNeZckQaXhTaPBs0J1px5XbVUYEfwoEBH7dWd8ZNqh3ZbpgJO2qF+kvK0t srwMd+Dv54NQg8/DAEAItqb4Jh5K5yzKepjrAVmahyr7WeN/UJytUer3tkhzbwdrJQ3c 2V5d7ggU7fi0UA+0mdZ/DfGiWo1GoFqdQR5o49cwvo+Z6heOSRNpG7wD542x3oPZ7Adm ewZGEOl4MehJasfrf8sOvPdIhdYEqqs1PaJzyKTzMYbhBar8etokPq0aIHnwgV+Jd8Fo m9nQl/QE6CHydxEpUJeC0fBESueMCQoMV5kbdOQU7yGWYgKnn8BiORIp90QFliCWWqRK IufA== X-Forwarded-Encrypted: i=1; AJvYcCXeFfKQ/LETMX8x+XeawKyyHZQ1ngV37cdCDF1+rB7Dje/3aifcltSEvc2jmgM1w3VkjZBljve9L3Pe9ihj@vger.kernel.org X-Gm-Message-State: AOJu0YxjREm/iD+zgFWgmEih/0N8b5uwqid5IxSxYYt5/heL1FikNKUq iWXr8lVcFJ8qJYTx7tgXvxhj+/EqWnOx5AdQmvwAHOFmLXsaxwYvgn3JvocbZDTLMvBvTWnJh4/ q+ZAHR0PyBkKPyPrPLh8t4QqQMg== X-Google-Smtp-Source: AGHT+IEkojIJInD/1rIHSjDlLsk5eEDcRjgdNOo3km0wIkB8Z2dVerWqP3VHxREIu+UWKvbzRGycxhqM7QHRBwikOgk= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a05:690c:dc3:b0:6ec:b74d:9c59 with SMTP id 00721157ae682-6eee0875aa0mr120007b3.2.1732221765025; Thu, 21 Nov 2024 12:42:45 -0800 (PST) Date: Thu, 21 Nov 2024 20:42:26 +0000 In-Reply-To: <20241121204220.2378181-20-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121204220.2378181-20-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=5425; i=samitolvanen@google.com; h=from:subject; bh=rLGOZcCiqj2V3Mb8xMD1NQdUVcgO11MXqSLXG9dDXtw=; b=owGbwMvMwCEWxa662nLh8irG02pJDOn2s/Xn3wtb+l2KIfUP+zbO7+Y65ovsry8tmym0jP/4p 0vJM3kPdpSyMIhxMMiKKbK0fF29dfd3p9RXn4skYOawMoEMYeDiFICJTDrK8D+WZ+GB2S5XLoYU sPoeNoosd7V5eyzlW+NHv31qGz0nMdgw/PermfeMR+RRzLzzGWu7S01UOe11BAoOTzRzKtfPup+ 6mxMA X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121204220.2378181-25-samitolvanen@google.com> Subject: [PATCH v6 05/18] gendwarfksyms: Expand type modifiers and typedefs From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Add support for expanding DWARF type modifiers, such as pointers, const values etc., and typedefs. These types all have DW_AT_type attribute pointing to the underlying type, and thus produce similar output. Also add linebreaks and indentation to debugging output to make it more readable. Signed-off-by: Sami Tolvanen Reviewed-by: Petr Pavlu --- scripts/gendwarfksyms/die.c | 12 +++++ scripts/gendwarfksyms/dwarf.c | 67 +++++++++++++++++++++++++++ scripts/gendwarfksyms/gendwarfksyms.h | 5 ++ 3 files changed, 84 insertions(+) diff --git a/scripts/gendwarfksyms/die.c b/scripts/gendwarfksyms/die.c index 28d89fce89fc..2829387fd815 100644 --- a/scripts/gendwarfksyms/die.c +++ b/scripts/gendwarfksyms/die.c @@ -130,6 +130,18 @@ void die_map_add_string(struct die *cd, const char *str) df->type = FRAGMENT_STRING; } +void die_map_add_linebreak(struct die *cd, int linebreak) +{ + struct die_fragment *df; + + if (!cd) + return; + + df = append_item(cd); + df->data.linebreak = linebreak; + df->type = FRAGMENT_LINEBREAK; +} + void die_map_add_die(struct die *cd, struct die *child) { struct die_fragment *df; diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index c2cd4743515e..1d67ee18a388 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -6,6 +6,17 @@ #include #include "gendwarfksyms.h" +static bool do_linebreak; +static int indentation_level; + +/* Line breaks and indentation for pretty-printing */ +static void process_linebreak(struct die *cache, int n) +{ + indentation_level += n; + do_linebreak = true; + die_map_add_linebreak(cache, n); +} + #define DEFINE_GET_ATTR(attr, type) \ static bool get_##attr##_attr(Dwarf_Die *die, unsigned int id, \ type *value) \ @@ -75,6 +86,12 @@ static void process(struct die *cache, const char *s) { s = s ?: ""; + if (dump_dies && do_linebreak) { + fputs("\n", stderr); + for (int i = 0; i < indentation_level; i++) + fputs(" ", stderr); + do_linebreak = false; + } if (dump_dies) fputs(s, stderr); @@ -238,6 +255,40 @@ static void process_type_attr(struct state *state, struct die *cache, process(cache, "base_type void"); } +/* Container types with DW_AT_type */ +static void __process_type(struct state *state, struct die *cache, + Dwarf_Die *die, const char *type) +{ + process(cache, type); + process_fqn(cache, die); + process(cache, " {"); + process_linebreak(cache, 1); + process_type_attr(state, cache, die); + process_linebreak(cache, -1); + process(cache, "}"); + process_byte_size_attr(cache, die); + process_alignment_attr(cache, die); +} + +#define DEFINE_PROCESS_TYPE(type) \ + static void process_##type##_type(struct state *state, \ + struct die *cache, Dwarf_Die *die) \ + { \ + __process_type(state, cache, die, #type "_type"); \ + } + +DEFINE_PROCESS_TYPE(atomic) +DEFINE_PROCESS_TYPE(const) +DEFINE_PROCESS_TYPE(immutable) +DEFINE_PROCESS_TYPE(packed) +DEFINE_PROCESS_TYPE(pointer) +DEFINE_PROCESS_TYPE(reference) +DEFINE_PROCESS_TYPE(restrict) +DEFINE_PROCESS_TYPE(rvalue_reference) +DEFINE_PROCESS_TYPE(shared) +DEFINE_PROCESS_TYPE(volatile) +DEFINE_PROCESS_TYPE(typedef) + static void process_base_type(struct state *state, struct die *cache, Dwarf_Die *die) { @@ -259,6 +310,9 @@ static void process_cached(struct state *state, struct die *cache, case FRAGMENT_STRING: process(NULL, df->data.str); break; + case FRAGMENT_LINEBREAK: + process_linebreak(NULL, df->data.linebreak); + break; case FRAGMENT_DIE: if (!dwarf_die_addr_die(dwarf_cu_getdwarf(die->cu), (void *)df->data.addr, &child)) @@ -294,7 +348,20 @@ static int process_type(struct state *state, struct die *parent, Dwarf_Die *die) } switch (tag) { + /* Type modifiers */ + PROCESS_TYPE(atomic) + PROCESS_TYPE(const) + PROCESS_TYPE(immutable) + PROCESS_TYPE(packed) + PROCESS_TYPE(pointer) + PROCESS_TYPE(reference) + PROCESS_TYPE(restrict) + PROCESS_TYPE(rvalue_reference) + PROCESS_TYPE(shared) + PROCESS_TYPE(volatile) + /* Other types */ PROCESS_TYPE(base) + PROCESS_TYPE(typedef) default: debug("unimplemented type: %x", tag); break; diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h index 3b42616ae9b5..a6b9a697bb17 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -64,6 +64,9 @@ extern int dump_dies; /* Error == negative values */ #define checkp(expr) __check(expr, __res < 0) +/* Consistent aliases (DW_TAG__type) for DWARF tags */ +#define DW_TAG_typedef_type DW_TAG_typedef + /* * symbols.c */ @@ -105,6 +108,7 @@ enum die_state { enum die_fragment_type { FRAGMENT_EMPTY, FRAGMENT_STRING, + FRAGMENT_LINEBREAK, FRAGMENT_DIE }; @@ -112,6 +116,7 @@ struct die_fragment { enum die_fragment_type type; union { char *str; + int linebreak; uintptr_t addr; } data; struct list_head list; From patchwork Thu Nov 21 20:42:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13882344 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3A69C1DDC2F for ; Thu, 21 Nov 2024 20:42:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221769; cv=none; b=diFUc1I5hmO1F2lkeWSg7QheGpyvOBJvShiuUuhXLOYcNLt1Q2nvBiHVUcO0RARweZ1Z4Ulrmqnw/8oVoToU1tGVb8kLulLXcqnX3pWQSTFZGOj7qMMYldHljOecG241cJ5Jf47skWbtNSAfkk897wY6w2+YQoVWAfyUSy/BYvM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221769; c=relaxed/simple; bh=K2ie+K5BAHLttox7QOvj2Jlq5yVsu82vs8hCoTiovzM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=htM7aRdR3kQivr/pAjrOXb020SF/p27CjrVK8+CdMqT8muxouTflQ5TT2B5tVtBDcd9D7mjxV4xtxRLPOavk9UL82tDpMZzpEgwgmKplaoWDhCMp9H2EpkDC8LLFJ0OhcLAjk5jgaxljWNPxVFVUvjm9LCKK+ci8ofluEh+BBgI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=o94OEAQV; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="o94OEAQV" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-7fb966ee0cdso1007239a12.1 for ; Thu, 21 Nov 2024 12:42:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732221766; x=1732826566; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=R5PScQuWHy6DjhROn8PczIndaWix8yzkVmRsn1uGhjo=; b=o94OEAQVDVbMV/K8Emp3pz5YzZ37kl/48tHM9WAZF2YKeO+npPgdrI9KRLlBiLXTPC Yr9Jpud/mlAPrrDrpc3RzB1aH5CPJAedxEK4GOcLJP3jsDT3k3hE7sBRM9dTy+Sm0EYc kINl50XoJd4eI7FBN99adPHokhw5nnkXvrbgIgb1M79hrfLcKWbAAkiFjElV9i9VGWft T8NxYDgBr63wIB7OcJQvQ4P0CG7EBt+S+DEpC8LXbQy2zC4bc7zJ/f4ZZQ27clKzHaC2 C1u/Z+MsiBdD0OLqh2Xl5N8qrFPDPv7t+2Lz1rWR2xh8DKOq2RpVPOX21A+iHVtFtAUD RzCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732221766; x=1732826566; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=R5PScQuWHy6DjhROn8PczIndaWix8yzkVmRsn1uGhjo=; b=sLs5d7KDhTPPtxzD+i5NU7nfZ3M28+zsnEFp0KWwOV+Z2xbx1p6KpFvQ6N09G3RoZB hzQ+vkbrLogfqz7y0GnRkj9wy/7IXg/6s7YGgWQygEivojg3umAx/hM1kzBRQJZ5aJyz YtHi0pcSXbipHnqg1xZ/sEgslquEs3iPSRCWIc62PANqgobYGDXAAE7MZlboT8SJvCS/ ixsNPrfQulQ4oiR7mAivz5dUjQm2KWXmJWcup8ap9BKGDD+mLkaYJUU2K7RoarrC3t8I t5WRUN8v4ibUtT/W6TCaG5xg/kV6nWFDiWtuJNGZ1psRFlGmzvxlQeLrD91boRDzpQBo Dakg== X-Forwarded-Encrypted: i=1; AJvYcCVMncqpupvl3vRAX+9fl5WYQfoIs77i0Ia86LHxAUfLIi4Yfpelu/6q2MMC+GZJFBbZ1nQReNx45xPvVS7q@vger.kernel.org X-Gm-Message-State: AOJu0YwAYkTk5XeSLETDs3cg1IqbnyvHnneiFeOXXJzcF1sfHG25UC3k W9k+vv66ZpR5W6MbwnvZV+ty6MA8p0N2+/cRVG+SXjyOZViAL7pstyI9QhpOJ4arJMj5MtEqZHc uNjZ4HBuupLU/SdUm4kc7/bEtGw== X-Google-Smtp-Source: AGHT+IF3193DVDNoTowEC34YpIeXHuT6+LHh9FOvMUqYp9Gv+NbxW05RZWgElbfmVyDREsBBIVfSTX89nLZeLEUq1k0= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a63:5921:0:b0:7ea:68e2:176b with SMTP id 41be03b00d2f7-7fbb44ac1d9mr2886a12.1.1732221766514; Thu, 21 Nov 2024 12:42:46 -0800 (PST) Date: Thu, 21 Nov 2024 20:42:27 +0000 In-Reply-To: <20241121204220.2378181-20-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121204220.2378181-20-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=5759; i=samitolvanen@google.com; h=from:subject; bh=K2ie+K5BAHLttox7QOvj2Jlq5yVsu82vs8hCoTiovzM=; b=owGbwMvMwCEWxa662nLh8irG02pJDOn2s/UrKq3LtlTGmF/emH9nRrDQ6s2nEss3Na59sfTFl F/rT70x7ShlYRDjYJAVU2Rp+bp66+7vTqmvPhdJwMxhZQIZwsDFKQA3mYPhf4iKpIT7us+O4txT RX13HsrRvr5+Z+1uZ5u7088WqdyM/8XIsO2zf07YmwCeio5fnzMlOA/fMo78MuWWk1ylzZkeqXv JPAA= X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121204220.2378181-26-samitolvanen@google.com> Subject: [PATCH v6 06/18] gendwarfksyms: Expand subroutine_type From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Add support for expanding DW_TAG_subroutine_type and the parameters in DW_TAG_formal_parameter. Use this to also expand subprograms. Example output with --dump-dies: subprogram ( formal_parameter pointer_type { const_type { base_type char byte_size(1) encoding(6) } } ) -> base_type unsigned long byte_size(8) encoding(7) Signed-off-by: Sami Tolvanen Reviewed-by: Petr Pavlu --- scripts/gendwarfksyms/dwarf.c | 84 ++++++++++++++++++++++++++- scripts/gendwarfksyms/gendwarfksyms.h | 4 ++ 2 files changed, 85 insertions(+), 3 deletions(-) diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index 1d67ee18a388..7e6b477d7c12 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -211,6 +211,15 @@ DEFINE_PROCESS_UDATA_ATTRIBUTE(alignment) DEFINE_PROCESS_UDATA_ATTRIBUTE(byte_size) DEFINE_PROCESS_UDATA_ATTRIBUTE(encoding) +/* Match functions -- die_match_callback_t */ +#define DEFINE_MATCH(type) \ + static bool match_##type##_type(Dwarf_Die *die) \ + { \ + return dwarf_tag(die) == DW_TAG_##type##_type; \ + } + +DEFINE_MATCH(formal_parameter) + bool match_all(Dwarf_Die *die) { return true; @@ -223,19 +232,28 @@ int process_die_container(struct state *state, struct die *cache, Dwarf_Die current; int res; + /* Track the first item in lists. */ + if (state) + state->first_list_item = true; + res = checkp(dwarf_child(die, ¤t)); while (!res) { if (match(¤t)) { /* <0 = error, 0 = continue, >0 = stop */ res = checkp(func(state, cache, ¤t)); if (res) - return res; + goto out; } res = checkp(dwarf_siblingof(¤t, ¤t)); } - return 0; + res = 0; +out: + if (state) + state->first_list_item = false; + + return res; } static int process_type(struct state *state, struct die *parent, @@ -255,6 +273,40 @@ static void process_type_attr(struct state *state, struct die *cache, process(cache, "base_type void"); } +static void process_list_comma(struct state *state, struct die *cache) +{ + if (state->first_list_item) { + state->first_list_item = false; + } else { + process(cache, " ,"); + process_linebreak(cache, 0); + } +} + +/* Comma-separated with DW_AT_type */ +static void __process_list_type(struct state *state, struct die *cache, + Dwarf_Die *die, const char *type) +{ + const char *name = get_name_attr(die); + + process_list_comma(state, cache); + process(cache, type); + process_type_attr(state, cache, die); + if (name) { + process(cache, " "); + process(cache, name); + } +} + +#define DEFINE_PROCESS_LIST_TYPE(type) \ + static void process_##type##_type(struct state *state, \ + struct die *cache, Dwarf_Die *die) \ + { \ + __process_list_type(state, cache, die, #type " "); \ + } + +DEFINE_PROCESS_LIST_TYPE(formal_parameter) + /* Container types with DW_AT_type */ static void __process_type(struct state *state, struct die *cache, Dwarf_Die *die, const char *type) @@ -289,6 +341,29 @@ DEFINE_PROCESS_TYPE(shared) DEFINE_PROCESS_TYPE(volatile) DEFINE_PROCESS_TYPE(typedef) +static void __process_subroutine_type(struct state *state, struct die *cache, + Dwarf_Die *die, const char *type) +{ + process(cache, type); + process(cache, " ("); + process_linebreak(cache, 1); + /* Parameters */ + check(process_die_container(state, cache, die, process_type, + match_formal_parameter_type)); + process_linebreak(cache, -1); + process(cache, ")"); + process_linebreak(cache, 0); + /* Return type */ + process(cache, "-> "); + process_type_attr(state, cache, die); +} + +static void process_subroutine_type(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + __process_subroutine_type(state, cache, die, "subroutine_type"); +} + static void process_base_type(struct state *state, struct die *cache, Dwarf_Die *die) { @@ -359,8 +434,11 @@ static int process_type(struct state *state, struct die *parent, Dwarf_Die *die) PROCESS_TYPE(rvalue_reference) PROCESS_TYPE(shared) PROCESS_TYPE(volatile) + /* Subtypes */ + PROCESS_TYPE(formal_parameter) /* Other types */ PROCESS_TYPE(base) + PROCESS_TYPE(subroutine) PROCESS_TYPE(typedef) default: debug("unimplemented type: %x", tag); @@ -390,7 +468,7 @@ static void process_symbol(struct state *state, Dwarf_Die *die, static int __process_subprogram(struct state *state, struct die *cache, Dwarf_Die *die) { - process(cache, "subprogram"); + __process_subroutine_type(state, cache, die, "subprogram"); return 0; } diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h index a6b9a697bb17..39bdc9e121b5 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -65,6 +65,7 @@ extern int dump_dies; #define checkp(expr) __check(expr, __res < 0) /* Consistent aliases (DW_TAG__type) for DWARF tags */ +#define DW_TAG_formal_parameter_type DW_TAG_formal_parameter #define DW_TAG_typedef_type DW_TAG_typedef /* @@ -159,6 +160,9 @@ void die_map_free(void); struct state { struct symbol *sym; Dwarf_Die die; + + /* List expansion */ + bool first_list_item; }; typedef int (*die_callback_t)(struct state *state, struct die *cache, From patchwork Thu Nov 21 20:42:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13882345 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C9C481DE2C2 for ; Thu, 21 Nov 2024 20:42:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221770; cv=none; b=SCoLApQ7WpIyDlG31LI1DnOhKh1Hl8P+Elc1mlN2yVlZ/8yTDvadNk75qxKEIGJGkaQhOUy8FGcb0hNVES46HdgKiSKVp6YtL3cZfyW8PX95PceSlQ9GCMBcl+3UwIyrjfyyURL7PcejAlUnLlqXnR+2IuLBz2HgowRpJk/PQko= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221770; c=relaxed/simple; bh=aJnHoVU7AKnaZ23w+Dc4SlHAPIMvZ400/m3njLPupx8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sbMcoQ4OZCc3JgnxxBFL1gG8a84bS3YcK0sZGRqqD8XbSrFY0GJy7O4Hi8s2wYRNbAONwyeK+dGNRsBmT0z8j31WO8TQPcAX9MTfmRV2qwSUqjv1BE1QCZnTfVmDL+Mp7zy2K1ZEezsP4KaW4K6X5t2eZdIt47bxtdYXWABykPA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=1SgySmAk; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="1SgySmAk" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6eeb754bc7cso22048207b3.3 for ; Thu, 21 Nov 2024 12:42:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732221768; x=1732826568; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=7pZ/4xogXJ4cKm1OQ4EOa6mZ/jPcnYnoZlpn2PBS83k=; b=1SgySmAkoCFv1I756RgfYYU63O+1c86hLRMgFwySLVGBJRQBYmhZXwY4rSkIMNjgff IWwLarLU2iAICxrSF5QHzgv0avUGxdIRKMW2BlEVD3uOzBKhaojNrDMKM3MKWOwJfgod lij+nj/BV3DTpWN0CjpwsIwoWXos1wnCi6V7cb7Qyj5Zk1VRVBgZFVelGkvo4M4jx727 USRFjrh5OW3Lq+7ylDy/zk8yrFTOLXIQUB0MwmQzTk2arnFrL66G6q06fvjZBsKxv9re 2+BYlolhg6y9QPHZWt4MfiY6sSRUsNiuCrVNU4SHamJD48YwDb4TYOpabzGCq13QMS/L /Zyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732221768; x=1732826568; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7pZ/4xogXJ4cKm1OQ4EOa6mZ/jPcnYnoZlpn2PBS83k=; b=nvLHzj0lnauvCgECt5zjfS27J7b/p2z50pGh1WEuPEtsQhXy1dIX56HIM2FL7TlDE7 5WumO6jKoJGbPmHjJ2PpobihPcKw7xhsZQj2X3a+LNRjuy1azxPWbrzuduEeKSU8M3Fg g1qQNvJiffzbINRLtiFt2z9jIGUNIPP0A03YD+/ErOp37sKLmHiJJJ8U6bYG2QT0DbyA q54JtMpb9Zhlf8nWZYeXzeSeZ0cjGNvzIoyRU4xzR+WLxTTpuHqGGuoxgcPdrXuu4cC+ TENwr24o09mpQSPGon0JX1B1EBxH/pMvdnmRmCGrYAIYqUp57EQkDYHU6k/rD5Uz+flH BfPw== X-Forwarded-Encrypted: i=1; AJvYcCXj76eDFGa8SPl0h2jC9Sz/Tft/+sOpzcuZgMAoqAOghhHt8+do1BpE1of7id5/fyqrhwvtNnLpkUIDuiTw@vger.kernel.org X-Gm-Message-State: AOJu0Yy4LQg6y1PIbxaIhnf2tiaLdeS5q3TS2eY5icf5+STmFhKysw7j EgPVpa91dp2NfJ2rc/yMbDb2BjJmHBcCibq3NglUGwyurgr5g9REl0V/bxVW7DnGqHpzSh8ZKK2 PuVF0iG+arHggycVK3xootCYatQ== X-Google-Smtp-Source: AGHT+IFUmYZ94k6o/pSgAkQo+n4cA2gB6D9v55LEtbY+hxIdzVqLBZoYtltcx5dTBWlE7mWKQXRUOqPQVTx6pAsX9dk= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a05:690c:2706:b0:6ea:7a32:8c14 with SMTP id 00721157ae682-6eee0a5b0ffmr28517b3.7.1732221767889; Thu, 21 Nov 2024 12:42:47 -0800 (PST) Date: Thu, 21 Nov 2024 20:42:28 +0000 In-Reply-To: <20241121204220.2378181-20-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121204220.2378181-20-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=2300; i=samitolvanen@google.com; h=from:subject; bh=aJnHoVU7AKnaZ23w+Dc4SlHAPIMvZ400/m3njLPupx8=; b=owGbwMvMwCEWxa662nLh8irG02pJDOn2sw1ikia+s28P87hiuTzQ7Zfqt4umt29sPjcz6ZPLn O7nqdZLO0pZGMQ4GGTFFFlavq7euvu7U+qrz0USMHNYmUCGMHBxCsBEDs9j+J9WUDr3hulKz1Mb /qfY9j5l9ZnfY72hOYpnU9PrPj6T1esZ/gdt/7OCy6H72jpRvwLDOCs17piznqGdYu/md6TaHcv YzwoA X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121204220.2378181-27-samitolvanen@google.com> Subject: [PATCH v6 07/18] gendwarfksyms: Expand array_type From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Add support for expanding DW_TAG_array_type, and the subrange type indicating array size. Example source code: const char *s[34]; Output with --dump-dies: variable array_type[34] { pointer_type { const_type { base_type char byte_size(1) encoding(6) } } byte_size(8) } Signed-off-by: Sami Tolvanen Reviewed-by: Petr Pavlu --- scripts/gendwarfksyms/dwarf.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index 7e6b477d7c12..ade9b3b7b119 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -219,6 +219,7 @@ DEFINE_PROCESS_UDATA_ATTRIBUTE(encoding) } DEFINE_MATCH(formal_parameter) +DEFINE_MATCH(subrange) bool match_all(Dwarf_Die *die) { @@ -341,6 +342,33 @@ DEFINE_PROCESS_TYPE(shared) DEFINE_PROCESS_TYPE(volatile) DEFINE_PROCESS_TYPE(typedef) +static void process_subrange_type(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + Dwarf_Word count = 0; + + if (get_udata_attr(die, DW_AT_count, &count)) + process_fmt(cache, "[%" PRIu64 "]", count); + else if (get_udata_attr(die, DW_AT_upper_bound, &count)) + process_fmt(cache, "[%" PRIu64 "]", count + 1); + else + process(cache, "[]"); +} + +static void process_array_type(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + process(cache, "array_type"); + /* Array size */ + check(process_die_container(state, cache, die, process_type, + match_subrange_type)); + process(cache, " {"); + process_linebreak(cache, 1); + process_type_attr(state, cache, die); + process_linebreak(cache, -1); + process(cache, "}"); +} + static void __process_subroutine_type(struct state *state, struct die *cache, Dwarf_Die *die, const char *type) { @@ -436,7 +464,9 @@ static int process_type(struct state *state, struct die *parent, Dwarf_Die *die) PROCESS_TYPE(volatile) /* Subtypes */ PROCESS_TYPE(formal_parameter) + PROCESS_TYPE(subrange) /* Other types */ + PROCESS_TYPE(array) PROCESS_TYPE(base) PROCESS_TYPE(subroutine) PROCESS_TYPE(typedef) From patchwork Thu Nov 21 20:42:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13882346 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 602711DDC1D for ; Thu, 21 Nov 2024 20:42:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221772; cv=none; b=AYHXhGfNmZNCEHkhq6gE0/ucjK08+bzFH/crRJb2c9kUIjzTIs5weo/FPQ0ZhHd7RHEVdiIRB4dFv8uqqAewBQP4KwiWPuSf512FEvwsabxc4Xyx0MHm515XrBMRS/R9qRi1A7RGKtBe7z+DqGNzvTLxexCaLNEkvUCRqVQiZrk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221772; c=relaxed/simple; bh=EpEiwt7t8/l21Jj3miJP0gC9vVnTUFc+quBwCd1Ktlw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lZJGwAsA8QPrxsYdPoJhcPPFnMXW88eThkL4ZoInFsPIkB6gIb4pQ3FkNJDg/xYP+pX3S/XhfewB3iSaVNT9GBTcSfnHA909ll7u+Cat8ZGWlgqH7xZD4R84zR/qFBLTUgOXxuu99p46S19DVS78DN4zptvvKRBuDSch8JY0kgE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=17jb6RIl; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="17jb6RIl" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6eec33c5c50so22475497b3.1 for ; Thu, 21 Nov 2024 12:42:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732221769; x=1732826569; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=+FP9o/YfL4D1uYj1bzcTvHkYKEts/YT26fpG2G23v2E=; b=17jb6RIlr1ofR2WLf12OSGGCNUEgJQbvpLbaru9hJcpDyTT083lj1KdzzRHoBpt/5h mLnElzH8TpNuJRbKBVa4zoYr5eyjz5tsQUNoqMGFNquhdeGGmlNDMYeIj7Pn6m7VQY65 xzwv/8AnQdcAB9V/GnQN1g8tPmDDMx2+61Xu3egChwdFP21QxX4G1wwr7zT+wsgQ+HY4 286EYRpw+5Wjj9SJHtrdXVgFIsViXS0X3KwagTbQFKv81Ns2lrQmgCf0ZvSGWbfpoTU3 5mZN9MGfETtJRVCzlBDx+bk3SIzi5mQDbER1CK1x+jquH6BVCeXHem6IkFNjFxyTseVl AMPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732221769; x=1732826569; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=+FP9o/YfL4D1uYj1bzcTvHkYKEts/YT26fpG2G23v2E=; b=l4sW4ITBomR5talvJiPRxE1n4piggzb4oM87iOU5OWafcPq6JNYYWD0mzxJDb+Wd4v d2RfOKQYVKQoBbH2ufJ4E4TlEvIQBY6rZ5REpECxh8tk4BPEEdCyA/LwySyk8OHcQ9Ft DiYUew8nHpktpmn3QWKNf1KRWMqfJuU2GH+95DCmcnyzNMqU6+qcnUhAHIIwHDGMH0O+ BS57wFaOkjQU0n5p4zz+CfX4g3LNsM421RByi2K/ElHxXs/C7wkgJpb7xX+VVRPF70LC si1BetEHzaMIKjVnqlrFrCKChbYZWTmu1aE3TeVK6TeaYdXvkE4rQT+9BxpdlJVRc1W1 4d+g== X-Forwarded-Encrypted: i=1; AJvYcCX/uSdhVzEPwGUgM398Bc8+b9Bfp0xt6J+SJpw5Aq4x/Rk3IleIB4CSN036gYE1E30Gn/5B7hYk8znnIRl2@vger.kernel.org X-Gm-Message-State: AOJu0YwQx9qcTHSLzcKoK4EWXtn2JuGdAePsGv4/dtx+C+TvsHPl5cCp VNrkKjuAG1WK2hnwciT1fRdxqAsn8bzFmuoD5JKGCUpbtqP+eKARk7iDCy/KSI+8YZyaTLAwWL6 93ihgzx61ZyNW2EDoQ9robZ7qEA== X-Google-Smtp-Source: AGHT+IG4g8PJ8qDS8zrUZBIJ1tgSXB0jAGKPKnAwvcXPyRB1CTbmME2/hkupudXyRd8g1ALcyVsXWApn4i0ip7pp+eY= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a05:690c:67c1:b0:6e3:d670:f62a with SMTP id 00721157ae682-6eee0a1c484mr57817b3.3.1732221769533; Thu, 21 Nov 2024 12:42:49 -0800 (PST) Date: Thu, 21 Nov 2024 20:42:29 +0000 In-Reply-To: <20241121204220.2378181-20-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121204220.2378181-20-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=8481; i=samitolvanen@google.com; h=from:subject; bh=EpEiwt7t8/l21Jj3miJP0gC9vVnTUFc+quBwCd1Ktlw=; b=owGbwMvMwCEWxa662nLh8irG02pJDOn2sw3tdHvFJjmG71xjoH1xx+alB08c7Hob/SOn4EJwV mq1GM+6jlIWBjEOBlkxRZaWr6u37v7ulPrqc5EEzBxWJpAhDFycAjARtn+MDL+5dppzlvHopWi9 /Nm+T0aYZ8HJ27Ha1rUsrGxdFinOWQz/XSdsiQj+1NR0Zanbk00PDpypPlEprnNliuUBRcXA6Nb tbAA= X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121204220.2378181-28-samitolvanen@google.com> Subject: [PATCH v6 08/18] gendwarfksyms: Expand structure types From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Recursively expand DWARF structure types, i.e. structs, unions, and enums. Also include relevant DWARF attributes in type strings to encode structure layout, for example. Example output with --dump-dies: subprogram ( formal_parameter structure_type &str { member pointer_type { base_type u8 byte_size(1) encoding(7) } data_ptr data_member_location(0) , member base_type usize byte_size(8) encoding(7) length data_member_location(8) } byte_size(16) alignment(8) msg ) -> base_type void Signed-off-by: Sami Tolvanen Reviewed-by: Petr Pavlu --- scripts/gendwarfksyms/dwarf.c | 138 +++++++++++++++++++++++++- scripts/gendwarfksyms/gendwarfksyms.h | 5 + 2 files changed, 141 insertions(+), 2 deletions(-) diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index ade9b3b7b119..98191ca7cef0 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -207,9 +207,14 @@ static void process_fqn(struct die *cache, Dwarf_Die *die) value); \ } +DEFINE_PROCESS_UDATA_ATTRIBUTE(accessibility) DEFINE_PROCESS_UDATA_ATTRIBUTE(alignment) +DEFINE_PROCESS_UDATA_ATTRIBUTE(bit_size) DEFINE_PROCESS_UDATA_ATTRIBUTE(byte_size) DEFINE_PROCESS_UDATA_ATTRIBUTE(encoding) +DEFINE_PROCESS_UDATA_ATTRIBUTE(data_bit_offset) +DEFINE_PROCESS_UDATA_ATTRIBUTE(data_member_location) +DEFINE_PROCESS_UDATA_ATTRIBUTE(discr_value) /* Match functions -- die_match_callback_t */ #define DEFINE_MATCH(type) \ @@ -218,7 +223,9 @@ DEFINE_PROCESS_UDATA_ATTRIBUTE(encoding) return dwarf_tag(die) == DW_TAG_##type##_type; \ } +DEFINE_MATCH(enumerator) DEFINE_MATCH(formal_parameter) +DEFINE_MATCH(member) DEFINE_MATCH(subrange) bool match_all(Dwarf_Die *die) @@ -297,6 +304,10 @@ static void __process_list_type(struct state *state, struct die *cache, process(cache, " "); process(cache, name); } + process_accessibility_attr(cache, die); + process_bit_size_attr(cache, die); + process_data_bit_offset_attr(cache, die); + process_data_member_location_attr(cache, die); } #define DEFINE_PROCESS_LIST_TYPE(type) \ @@ -307,6 +318,7 @@ static void __process_list_type(struct state *state, struct die *cache, } DEFINE_PROCESS_LIST_TYPE(formal_parameter) +DEFINE_PROCESS_LIST_TYPE(member) /* Container types with DW_AT_type */ static void __process_type(struct state *state, struct die *cache, @@ -339,6 +351,7 @@ DEFINE_PROCESS_TYPE(reference) DEFINE_PROCESS_TYPE(restrict) DEFINE_PROCESS_TYPE(rvalue_reference) DEFINE_PROCESS_TYPE(shared) +DEFINE_PROCESS_TYPE(template_type_parameter) DEFINE_PROCESS_TYPE(volatile) DEFINE_PROCESS_TYPE(typedef) @@ -392,6 +405,107 @@ static void process_subroutine_type(struct state *state, struct die *cache, __process_subroutine_type(state, cache, die, "subroutine_type"); } +static void process_variant_type(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + process_list_comma(state, cache); + process(cache, "variant {"); + process_linebreak(cache, 1); + check(process_die_container(state, cache, die, process_type, + match_member_type)); + process_linebreak(cache, -1); + process(cache, "}"); + process_discr_value_attr(cache, die); +} + +static void process_variant_part_type(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + process_list_comma(state, cache); + process(cache, "variant_part {"); + process_linebreak(cache, 1); + check(process_die_container(state, cache, die, process_type, + match_all)); + process_linebreak(cache, -1); + process(cache, "}"); +} + +static int ___process_structure_type(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + switch (dwarf_tag(die)) { + case DW_TAG_member: + case DW_TAG_variant_part: + return check(process_type(state, cache, die)); + case DW_TAG_class_type: + case DW_TAG_enumeration_type: + case DW_TAG_structure_type: + case DW_TAG_template_type_parameter: + case DW_TAG_union_type: + case DW_TAG_subprogram: + /* Skip non-member types, including member functions */ + return 0; + default: + error("unexpected structure_type child: %x", dwarf_tag(die)); + } +} + +static void __process_structure_type(struct state *state, struct die *cache, + Dwarf_Die *die, const char *type, + die_callback_t process_func, + die_match_callback_t match_func) +{ + process(cache, type); + process_fqn(cache, die); + process(cache, " {"); + process_linebreak(cache, 1); + + check(process_die_container(state, cache, die, process_func, + match_func)); + + process_linebreak(cache, -1); + process(cache, "}"); + + process_byte_size_attr(cache, die); + process_alignment_attr(cache, die); +} + +#define DEFINE_PROCESS_STRUCTURE_TYPE(structure) \ + static void process_##structure##_type( \ + struct state *state, struct die *cache, Dwarf_Die *die) \ + { \ + __process_structure_type(state, cache, die, \ + #structure "_type", \ + ___process_structure_type, \ + match_all); \ + } + +DEFINE_PROCESS_STRUCTURE_TYPE(class) +DEFINE_PROCESS_STRUCTURE_TYPE(structure) +DEFINE_PROCESS_STRUCTURE_TYPE(union) + +static void process_enumerator_type(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + Dwarf_Word value; + + process_list_comma(state, cache); + process(cache, "enumerator"); + process_fqn(cache, die); + + if (get_udata_attr(die, DW_AT_const_value, &value)) { + process(cache, " = "); + process_fmt(cache, "%" PRIu64, value); + } +} + +static void process_enumeration_type(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + __process_structure_type(state, cache, die, "enumeration_type", + process_type, match_enumerator_type); +} + static void process_base_type(struct state *state, struct die *cache, Dwarf_Die *die) { @@ -402,6 +516,16 @@ static void process_base_type(struct state *state, struct die *cache, process_alignment_attr(cache, die); } +static void process_unspecified_type(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + /* + * These can be emitted for stand-alone assembly code, which means we + * might run into them in vmlinux.o. + */ + process(cache, "unspecified_type"); +} + static void process_cached(struct state *state, struct die *cache, Dwarf_Die *die) { @@ -462,17 +586,27 @@ static int process_type(struct state *state, struct die *parent, Dwarf_Die *die) PROCESS_TYPE(rvalue_reference) PROCESS_TYPE(shared) PROCESS_TYPE(volatile) + /* Container types */ + PROCESS_TYPE(class) + PROCESS_TYPE(structure) + PROCESS_TYPE(union) + PROCESS_TYPE(enumeration) /* Subtypes */ + PROCESS_TYPE(enumerator) PROCESS_TYPE(formal_parameter) + PROCESS_TYPE(member) PROCESS_TYPE(subrange) + PROCESS_TYPE(template_type_parameter) + PROCESS_TYPE(variant) + PROCESS_TYPE(variant_part) /* Other types */ PROCESS_TYPE(array) PROCESS_TYPE(base) PROCESS_TYPE(subroutine) PROCESS_TYPE(typedef) + PROCESS_TYPE(unspecified) default: - debug("unimplemented type: %x", tag); - break; + error("unexpected type: %x", tag); } /* Update cache state and append to the parent (if any) */ diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h index 39bdc9e121b5..2646e11595ab 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -65,8 +65,13 @@ extern int dump_dies; #define checkp(expr) __check(expr, __res < 0) /* Consistent aliases (DW_TAG__type) for DWARF tags */ +#define DW_TAG_enumerator_type DW_TAG_enumerator #define DW_TAG_formal_parameter_type DW_TAG_formal_parameter +#define DW_TAG_member_type DW_TAG_member +#define DW_TAG_template_type_parameter_type DW_TAG_template_type_parameter #define DW_TAG_typedef_type DW_TAG_typedef +#define DW_TAG_variant_part_type DW_TAG_variant_part +#define DW_TAG_variant_type DW_TAG_variant /* * symbols.c From patchwork Thu Nov 21 20:42:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13882347 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1F4BF1DE4C4 for ; Thu, 21 Nov 2024 20:42:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221774; cv=none; b=OEwYLFMGTTIZAxEeA7SapDnxkYI1/x6p15lVp0HP+xyNWACmTvWoEW7JOdAwOXRF+c46I/xz5JRvR+vREiNVQaCZ6erDlgyvZDZp7v6AUVxLKR6PqzGWEyrlrNE5RwDRAoaMg5sEjJd11bzE91uF/LZ6hbxt6o2sm0BjIMnxe6M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221774; c=relaxed/simple; bh=0Y8pkFmBKWKmkeXL7oR1i75uVUGXc1J2v0ihg0pFsvw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=OPgi5XjGk8T9yg5X28VjJNyJGeIaaPp31jCE435Xbx9xJ+u+JF0pzVmF7q7kXs5NV6h9JT4FW2jYhd4soSZTtIRDVw4KGifv5ZBCWsvV9es4a9oQeDhiOUdaDj56eAzi/NYj6OfTCyHVySj1NKMIChmE7yt27JYaPBoha7hL+kA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=SUl207io; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="SUl207io" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e388c4bd970so2533280276.2 for ; Thu, 21 Nov 2024 12:42:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732221771; x=1732826571; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=q5azjlSatcsILNEOwLs19EdjKbFua2p8W1Y3HIRQJu0=; b=SUl207iobr0TdExLkbBlYo4JbM5fjUTBN2qihqmj0z2SeENMHhR8N7VTh8ZzMk0DqT dV3m38gGPm4R3ikmEQ3LgCIWXShzIS+yiZkZYq4Npkb3E6A+Xl9KW8xonlS5G3wS6JAU 92GIacTSAWatZ6okC8tJVxTXJUpC+5YInP/mKGAkxhUMfNK2ajw09zTDi/7tJk491tUL humjjMcsAaK3FbOB+DeueHmYD1NLyMstKQpLbMPp8Ow2MmU2i1Il6bTeklmKX3aeRTM3 hJz2CkuyKPI6lrBaHk2lPVW1Ds6CjKCpNzSuS8t+/dskQ43PxeCyX4rYcZM1ZeJn3J6l UCzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732221771; x=1732826571; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=q5azjlSatcsILNEOwLs19EdjKbFua2p8W1Y3HIRQJu0=; b=q6y/wpXCQ8AVW3t1kQejPkLjic+mJ60UlZo/b37cnnU2fYUySgM7F2dKZrDh557dR0 29Nt6NuQ7WY15fSsfg3Sc+/9UmYtl4wXM4pT3UNr61LV+wNsZFiVxlRZ086rNoPI96di oJZ/Csx9evdid3Ag++PENwAT+6I7bkdnBYU4UCqr/P4sAUthYcpmnar5+s4jSusOBjyW 92+o+x8V20psAJmutyYE39zv05oG9pjlCL9c4diyKjY4sswcNFxX/KfsiN9mF2n2Hej9 4kk3CkEjL84GsjsSP9NGMhQdfcdzriQfsIekQYVeCD24OR/ay+Jql0M8hreNrEmH/zXQ I+zQ== X-Forwarded-Encrypted: i=1; AJvYcCWZ019MyT8/gevnDCMlrv9Fgbq1DZg2zhumrZSwN2c7MfG80vkuRLWAZZLwe9T3zVjpARTQcBRhhzy47DOq@vger.kernel.org X-Gm-Message-State: AOJu0YyGm8NAQEQeuWEqT/BKJVneYVb06McGrDvt5MQIW7zHaHUrGZkL YuZCtVmLsoqiJwPjYQMUmcYmMFZf5jj02Bp9Wu+lapwiRZYk/3j7x8ffHLU5398t77itKhEBm43 Nw+oW+i+NgLnY3k1KvEkyWd/mJw== X-Google-Smtp-Source: AGHT+IHgq9XpTMYQNtf/KX084e49hvq9rsLmljwkPdv6gNljLLB41fg1reynmmiE/vgiVbx4alCO7ISYrjluh72vaJI= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a25:6984:0:b0:e2b:cd55:11b3 with SMTP id 3f1490d57ef6-e38f8b9e6e4mr101276.5.1732221771249; Thu, 21 Nov 2024 12:42:51 -0800 (PST) Date: Thu, 21 Nov 2024 20:42:30 +0000 In-Reply-To: <20241121204220.2378181-20-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121204220.2378181-20-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=10445; i=samitolvanen@google.com; h=from:subject; bh=0Y8pkFmBKWKmkeXL7oR1i75uVUGXc1J2v0ihg0pFsvw=; b=owGbwMvMwCEWxa662nLh8irG02pJDOn2sw3v/87eLtjAPt03Juag7ZG9LVw6pdWrlv6YuHtHb HDFpDflHaUsDGIcDLJiiiwtX1dv3f3dKfXV5yIJmDmsTCBDGLg4BWAiHWqMDC+fyp/OPHaJhzFN fsfMjeULdnaoTUu+IPt53sNtRqbfzhcw/BX/b/vq4Lyqw52qU/culGV38E5Ubr5zI94xq76Eac7 SIj4A X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121204220.2378181-29-samitolvanen@google.com> Subject: [PATCH v6 09/18] gendwarfksyms: Limit structure expansion From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Expand each structure type only once per exported symbol. This is necessary to support self-referential structures, which would otherwise result in infinite recursion, and it's sufficient for catching ABI changes. Types defined in .c files are opaque to external users and thus cannot affect the ABI. Consider type definitions in .c files to be declarations to prevent opaque types from changing symbol versions. Signed-off-by: Sami Tolvanen Reviewed-by: Petr Pavlu --- scripts/gendwarfksyms/Makefile | 1 + scripts/gendwarfksyms/cache.c | 51 +++++++++++ scripts/gendwarfksyms/dwarf.c | 125 ++++++++++++++++++++++++-- scripts/gendwarfksyms/gendwarfksyms.h | 46 ++++++++++ 4 files changed, 215 insertions(+), 8 deletions(-) create mode 100644 scripts/gendwarfksyms/cache.c diff --git a/scripts/gendwarfksyms/Makefile b/scripts/gendwarfksyms/Makefile index c0d4ce50fc27..c06145d84df8 100644 --- a/scripts/gendwarfksyms/Makefile +++ b/scripts/gendwarfksyms/Makefile @@ -2,6 +2,7 @@ hostprogs-always-y += gendwarfksyms gendwarfksyms-objs += gendwarfksyms.o +gendwarfksyms-objs += cache.o gendwarfksyms-objs += die.o gendwarfksyms-objs += dwarf.o gendwarfksyms-objs += symbols.o diff --git a/scripts/gendwarfksyms/cache.c b/scripts/gendwarfksyms/cache.c new file mode 100644 index 000000000000..c9c19b86a686 --- /dev/null +++ b/scripts/gendwarfksyms/cache.c @@ -0,0 +1,51 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Google LLC + */ + +#include "gendwarfksyms.h" + +struct cache_item { + unsigned long key; + int value; + struct hlist_node hash; +}; + +void cache_set(struct cache *cache, unsigned long key, int value) +{ + struct cache_item *ci; + + ci = xmalloc(sizeof(struct cache_item)); + ci->key = key; + ci->value = value; + hash_add(cache->cache, &ci->hash, hash_32(key)); +} + +int cache_get(struct cache *cache, unsigned long key) +{ + struct cache_item *ci; + + hash_for_each_possible(cache->cache, ci, hash, hash_32(key)) { + if (ci->key == key) + return ci->value; + } + + return -1; +} + +void cache_init(struct cache *cache) +{ + hash_init(cache->cache); +} + +void cache_free(struct cache *cache) +{ + struct hlist_node *tmp; + struct cache_item *ci; + + hash_for_each_safe(cache->cache, ci, tmp, hash) { + free(ci); + } + + hash_init(cache->cache); +} diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index 98191ca7cef0..3ea7583e6c3f 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -26,6 +26,7 @@ static void process_linebreak(struct die *cache, int n) !dwarf_form##attr(&da, value); \ } +DEFINE_GET_ATTR(flag, bool) DEFINE_GET_ATTR(udata, Dwarf_Word) static bool get_ref_die_attr(Dwarf_Die *die, unsigned int id, Dwarf_Die *value) @@ -79,6 +80,55 @@ static bool match_export_symbol(struct state *state, Dwarf_Die *die) return !!state->sym; } +/* DW_AT_decl_file -> struct srcfile */ +static struct cache srcfile_cache; + +static bool is_definition_private(Dwarf_Die *die) +{ + Dwarf_Word filenum; + Dwarf_Files *files; + Dwarf_Die cudie; + const char *s; + int res; + + /* + * Definitions in .c files cannot change the public ABI, + * so consider them private. + */ + if (!get_udata_attr(die, DW_AT_decl_file, &filenum)) + return false; + + res = cache_get(&srcfile_cache, filenum); + if (res >= 0) + return !!res; + + if (!dwarf_cu_die(die->cu, &cudie, NULL, NULL, NULL, NULL, NULL, NULL)) + error("dwarf_cu_die failed: '%s'", dwarf_errmsg(-1)); + + if (dwarf_getsrcfiles(&cudie, &files, NULL)) + error("dwarf_getsrcfiles failed: '%s'", dwarf_errmsg(-1)); + + s = dwarf_filesrc(files, filenum, NULL, NULL); + if (!s) + error("dwarf_filesrc failed: '%s'", dwarf_errmsg(-1)); + + s = strrchr(s, '.'); + res = s && !strcmp(s, ".c"); + cache_set(&srcfile_cache, filenum, res); + + return !!res; +} + +static bool is_declaration(Dwarf_Die *die) +{ + bool value; + + if (get_flag_attr(die, DW_AT_declaration, &value) && value) + return true; + + return is_definition_private(die); +} + /* * Type string processing */ @@ -455,19 +505,27 @@ static void __process_structure_type(struct state *state, struct die *cache, die_callback_t process_func, die_match_callback_t match_func) { + bool is_decl; + process(cache, type); process_fqn(cache, die); process(cache, " {"); process_linebreak(cache, 1); - check(process_die_container(state, cache, die, process_func, - match_func)); + is_decl = is_declaration(die); + + if (!is_decl && state->expand.expand) { + check(process_die_container(state, cache, die, process_func, + match_func)); + } process_linebreak(cache, -1); process(cache, "}"); - process_byte_size_attr(cache, die); - process_alignment_attr(cache, die); + if (!is_decl && state->expand.expand) { + process_byte_size_attr(cache, die); + process_alignment_attr(cache, die); + } } #define DEFINE_PROCESS_STRUCTURE_TYPE(structure) \ @@ -552,6 +610,30 @@ static void process_cached(struct state *state, struct die *cache, } } +static void state_init(struct state *state) +{ + state->expand.expand = true; + cache_init(&state->expansion_cache); +} + +static void expansion_state_restore(struct expansion_state *state, + struct expansion_state *saved) +{ + state->expand = saved->expand; +} + +static void expansion_state_save(struct expansion_state *state, + struct expansion_state *saved) +{ + expansion_state_restore(saved, state); +} + +static bool is_expanded_type(int tag) +{ + return tag == DW_TAG_class_type || tag == DW_TAG_structure_type || + tag == DW_TAG_union_type || tag == DW_TAG_enumeration_type; +} + #define PROCESS_TYPE(type) \ case DW_TAG_##type##_type: \ process_##type##_type(state, cache, die); \ @@ -559,18 +641,39 @@ static void process_cached(struct state *state, struct die *cache, static int process_type(struct state *state, struct die *parent, Dwarf_Die *die) { + enum die_state want_state = DIE_COMPLETE; struct die *cache; + struct expansion_state saved; int tag = dwarf_tag(die); + expansion_state_save(&state->expand, &saved); + /* - * If we have the DIE already cached, use it instead of walking + * Structures and enumeration types are expanded only once per + * exported symbol. This is sufficient for detecting ABI changes + * within the structure. + */ + if (is_expanded_type(tag)) { + if (cache_was_expanded(&state->expansion_cache, die->addr)) + state->expand.expand = false; + + if (state->expand.expand) + cache_mark_expanded(&state->expansion_cache, die->addr); + else + want_state = DIE_UNEXPANDED; + } + + /* + * If we have want_state already cached, use it instead of walking * through DWARF. */ - cache = die_map_get(die, DIE_COMPLETE); + cache = die_map_get(die, want_state); - if (cache->state == DIE_COMPLETE) { + if (cache->state == want_state) { process_cached(state, cache, die); die_map_add_die(parent, cache); + + expansion_state_restore(&state->expand, &saved); return 0; } @@ -611,9 +714,10 @@ static int process_type(struct state *state, struct die *parent, Dwarf_Die *die) /* Update cache state and append to the parent (if any) */ cache->tag = tag; - cache->state = DIE_COMPLETE; + cache->state = want_state; die_map_add_die(parent, cache); + expansion_state_restore(&state->expand, &saved); return 0; } @@ -675,11 +779,14 @@ static int process_exported_symbols(struct state *unused, struct die *cache, if (!match_export_symbol(&state, die)) return 0; + state_init(&state); + if (tag == DW_TAG_subprogram) process_subprogram(&state, &state.die); else process_variable(&state, &state.die); + cache_free(&state.expansion_cache); return 0; } default: @@ -691,4 +798,6 @@ void process_cu(Dwarf_Die *cudie) { check(process_die_container(NULL, NULL, cudie, process_exported_symbols, match_all)); + + cache_free(&srcfile_cache); } diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h index 2646e11595ab..7b29d8f07aa0 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -107,6 +107,7 @@ void symbol_free(void); enum die_state { DIE_INCOMPLETE, + DIE_UNEXPANDED, DIE_COMPLETE, DIE_LAST = DIE_COMPLETE }; @@ -136,6 +137,7 @@ static inline const char *die_state_name(enum die_state state) { switch (state) { CASE_CONST_TO_STR(DIE_INCOMPLETE) + CASE_CONST_TO_STR(DIE_UNEXPANDED) CASE_CONST_TO_STR(DIE_COMPLETE) } @@ -158,16 +160,60 @@ void die_map_add_linebreak(struct die *pd, int linebreak); void die_map_add_die(struct die *pd, struct die *child); void die_map_free(void); +/* + * cache.c + */ + +#define CACHE_HASH_BITS 11 + +/* A cache for addresses we've already seen. */ +struct cache { + HASHTABLE_DECLARE(cache, 1 << CACHE_HASH_BITS); +}; + +void cache_set(struct cache *cache, unsigned long key, int value); +int cache_get(struct cache *cache, unsigned long key); +void cache_init(struct cache *cache); +void cache_free(struct cache *cache); + +static inline void __cache_mark_expanded(struct cache *cache, uintptr_t addr) +{ + cache_set(cache, addr, 1); +} + +static inline bool __cache_was_expanded(struct cache *cache, uintptr_t addr) +{ + return cache_get(cache, addr) == 1; +} + +static inline void cache_mark_expanded(struct cache *cache, void *addr) +{ + __cache_mark_expanded(cache, (uintptr_t)addr); +} + +static inline bool cache_was_expanded(struct cache *cache, void *addr) +{ + return __cache_was_expanded(cache, (uintptr_t)addr); +} + /* * dwarf.c */ +struct expansion_state { + bool expand; +}; + struct state { struct symbol *sym; Dwarf_Die die; /* List expansion */ bool first_list_item; + + /* Structure expansion */ + struct expansion_state expand; + struct cache expansion_cache; }; typedef int (*die_callback_t)(struct state *state, struct die *cache, From patchwork Thu Nov 21 20:42:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13882348 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DAAEA1DE889 for ; Thu, 21 Nov 2024 20:42:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221776; cv=none; b=VC+6hEwYYf7OdUAS+mGJGiZuLC3zDwswjdnqsWFzePqmkzqESAnvK89uqsA4Ladg3ua+O8/d7DCW5wGuIyexEj/nVxXVTMpGmcLybDwxhmWp3dKcRzah1FoeIKdN7OFalEIWyOO6qdC83O5w1PsM1/JbEy8cqTVUVGc4A2HAT08= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221776; c=relaxed/simple; bh=ZtfhRREvYeeJIRTnypn2kVeALnWdluQMXmr8x8azY3w=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=P26ouG1UdCl2xPh/0Dcz7SDgKm8dnD62Ze3BI/HRC6nznPuQxbURyHA5tTmcqDEftNo+q+vn9NYj8suwVXvw6fG72Z94de8iTa6oG6ORQGaxcqhLYJrw75Vn6ZO3SfVH3Z0t7UERRStomILZgIkjuO5tpluYVu+nTVwyrz5KEkI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Lasm8BwA; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Lasm8BwA" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e388f173db4so2543034276.3 for ; Thu, 21 Nov 2024 12:42:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732221773; x=1732826573; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=wO2hIdiG7CrcRjoffVNYAvcAwiMWtXmgICIm5eEBWSg=; b=Lasm8BwA2daDeeKXB/tNlZKoSwjc+Ey/TosZTho/SYTUPlfVt1Jcf/kURVZM2LyP3y hUjUKo4Fu5GVcC/37xobiWik5CZyX+tuuS8+3QSbzxjoBTeTuUlap/C+UU/pVRIWcqcJ SJuytnz0E7RoQyRJpzOW0v4uxcg5BJmEMAIIXb1uohv7o7MMyQ3XuLdkEhoJuZBHMJBS R7koDXxBDQoaSIZx6u/IcknTzVDl3FlueT2oriHaG14gaIogcuDLboO6TJEy144kSmuj StF5iJKSCsvjPaU+/uCGKHIEGxODM7uuFnWirYiaOjA9w/IRiMSUa51ryNhOJXacFjsi Ju7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732221773; x=1732826573; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=wO2hIdiG7CrcRjoffVNYAvcAwiMWtXmgICIm5eEBWSg=; b=miKKjp6Vm5EcJp9xJTnffyfm4+BYoYnueMKiZ0IX22Ng4rvTAF5ia+JvWtaQv8QHJg a2OP7WiP0cqn6QNJ0dfJ6fOlhbaT4yMcyDU1AgYM3n/xglUnec7644pmxX9lGcI85nIK vTDfLqvRvC4bSa+puqklrKXKZH+pZScYYrqUoA7I4PG4vmHUS5ou8SDHBxED+WPWviXv fY8FwmCqxpApg7fJsGusTswJsZ+8CElWKUR9rPEnUq1VQ8iB4HyOT++d3pstGQH7lFFM rWiba1lH80zC3MeRd+jyBaQ0ELGjFq6fjbzlcrTXqVNLc4TSeKnsUmWrR87JX7FWTHOG UWDw== X-Forwarded-Encrypted: i=1; AJvYcCX4QkMo2grWLn7Z9jcssgAkBmRihStyM3IyiNe/ftSatK0Yjsss3uk5MEMUaSejajZ95av5fVkrEzFu1c7j@vger.kernel.org X-Gm-Message-State: AOJu0YwI0S63AP/IHxQAHKRbtdv21/2GdN/W3B2agZhZOb2iLqTI7Q6h /nFFUc2W3JUdOHA5k6o5wiXgWXwLj3F+i/ijC0dsmSPhpcQUgSXfoI0Hc6hLho2cmYJJ76l/gAd y4dmc/Nxz5Q9SmN5y72cXpMAafQ== X-Google-Smtp-Source: AGHT+IGsvhnsnrZz7y6skRItADeCqn8aWPQ51xtGa1NURNHB9v1UiuF4WhjQJfrq8s7XEv92esfmgAi1sMsb7EbrY8A= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a25:9a43:0:b0:e38:164f:5bf2 with SMTP id 3f1490d57ef6-e38f8bffec0mr123276.9.1732221772935; Thu, 21 Nov 2024 12:42:52 -0800 (PST) Date: Thu, 21 Nov 2024 20:42:31 +0000 In-Reply-To: <20241121204220.2378181-20-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121204220.2378181-20-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=5088; i=samitolvanen@google.com; h=from:subject; bh=ZtfhRREvYeeJIRTnypn2kVeALnWdluQMXmr8x8azY3w=; b=owGbwMvMwCEWxa662nLh8irG02pJDOn2s42iDr5bcna6UMhtz/d1vz0EFWw31nZeKz9+w7Td+ fnVqreTO0pZGMQ4GGTFFFlavq7euvu7U+qrz0USMHNYmUCGMHBxCsBEFq9mZLhUXOVTfsaB82X1 PA/2x6LfVh2N8vjcLdgjzXHt8ROFyicM/zSN1+99Kqzcuuxl92+vXz3B7G93+D8TFvi6+qiNmYM ELzcA X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121204220.2378181-30-samitolvanen@google.com> Subject: [PATCH v6 10/18] gendwarfksyms: Add die_map debugging From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Debugging the DWARF processing can be somewhat challenging, so add more detailed debugging output for die_map operations. Add the --dump-die-map flag, which adds color coded tags to the output for die_map changes. Signed-off-by: Sami Tolvanen Reviewed-by: Petr Pavlu --- scripts/gendwarfksyms/dwarf.c | 15 +++++++++++++++ scripts/gendwarfksyms/gendwarfksyms.c | 7 +++++++ scripts/gendwarfksyms/gendwarfksyms.h | 13 +++++++++++++ 3 files changed, 35 insertions(+) diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index 3ea7583e6c3f..cf6d2c2844dc 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -145,6 +145,8 @@ static void process(struct die *cache, const char *s) if (dump_dies) fputs(s, stderr); + if (cache) + die_debug_r("cache %p string '%s'", cache, s); die_map_add_string(cache, s); } @@ -593,6 +595,8 @@ static void process_cached(struct state *state, struct die *cache, list_for_each_entry(df, &cache->fragments, list) { switch (df->type) { case FRAGMENT_STRING: + die_debug_b("cache %p STRING '%s'", cache, + df->data.str); process(NULL, df->data.str); break; case FRAGMENT_LINEBREAK: @@ -602,6 +606,8 @@ static void process_cached(struct state *state, struct die *cache, if (!dwarf_die_addr_die(dwarf_cu_getdwarf(die->cu), (void *)df->data.addr, &child)) error("dwarf_die_addr_die failed"); + die_debug_b("cache %p DIE addr %" PRIxPTR " tag %x", + cache, df->data.addr, dwarf_tag(&child)); check(process_type(state, NULL, &child)); break; default: @@ -670,6 +676,9 @@ static int process_type(struct state *state, struct die *parent, Dwarf_Die *die) cache = die_map_get(die, want_state); if (cache->state == want_state) { + die_debug_g("cached addr %p tag %x -- %s", die->addr, tag, + die_state_name(cache->state)); + process_cached(state, cache, die); die_map_add_die(parent, cache); @@ -677,6 +686,9 @@ static int process_type(struct state *state, struct die *parent, Dwarf_Die *die) return 0; } + die_debug_g("addr %p tag %x -- %s -> %s", die->addr, tag, + die_state_name(cache->state), die_state_name(want_state)); + switch (tag) { /* Type modifiers */ PROCESS_TYPE(atomic) @@ -712,6 +724,9 @@ static int process_type(struct state *state, struct die *parent, Dwarf_Die *die) error("unexpected type: %x", tag); } + die_debug_r("parent %p cache %p die addr %p tag %x", parent, cache, + die->addr, tag); + /* Update cache state and append to the parent (if any) */ cache->tag = tag; cache->state = want_state; diff --git a/scripts/gendwarfksyms/gendwarfksyms.c b/scripts/gendwarfksyms/gendwarfksyms.c index 7552fde495ef..ed6d8c585268 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.c +++ b/scripts/gendwarfksyms/gendwarfksyms.c @@ -19,6 +19,8 @@ int debug; /* Dump DIE contents */ int dump_dies; +/* Print debugging information about die_map changes */ +int dump_die_map; static void usage(void) { @@ -26,6 +28,7 @@ static void usage(void) "Options:\n" " -d, --debug Print debugging information\n" " --dump-dies Dump DWARF DIE contents\n" + " --dump-die-map Print debugging information about die_map changes\n" " -h, --help Print this message\n" "\n", stderr); @@ -74,6 +77,7 @@ int main(int argc, char **argv) struct option opts[] = { { "debug", 0, NULL, 'd' }, { "dump-dies", 0, &dump_dies, 1 }, + { "dump-die-map", 0, &dump_die_map, 1 }, { "help", 0, NULL, 'h' }, { 0, 0, NULL, 0 } }; @@ -93,6 +97,9 @@ int main(int argc, char **argv) } } + if (dump_die_map) + dump_dies = 1; + if (optind >= argc) { usage(); error("no input files?"); diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h index 7b29d8f07aa0..10fb476a7b76 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -26,6 +26,7 @@ */ extern int debug; extern int dump_dies; +extern int dump_die_map; /* * Output helpers @@ -48,6 +49,18 @@ extern int dump_dies; exit(1); \ } while (0) +#define __die_debug(color, format, ...) \ + do { \ + if (dump_dies && dump_die_map) \ + fprintf(stderr, \ + "\033[" #color "m<" format ">\033[39m", \ + __VA_ARGS__); \ + } while (0) + +#define die_debug_r(format, ...) __die_debug(91, format, __VA_ARGS__) +#define die_debug_g(format, ...) __die_debug(92, format, __VA_ARGS__) +#define die_debug_b(format, ...) __die_debug(94, format, __VA_ARGS__) + /* * Error handling helpers */ From patchwork Thu Nov 21 20:42:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13882349 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 82D3C1DED43 for ; Thu, 21 Nov 2024 20:42:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221778; cv=none; b=AdRnjdcDl7fsRKLSRIadn/vv9L9ExhfwacGrA+E6KKB1o0Kb7vRPANHlC9cAqETOoHcJYjSRldoMBHx8h8PHmsZCSTyf70EVCWurJToHZN6+KDdOmefgf4eGHDJNkwzFysBPLqFbN7YsUgM8OftiOCLd5US74rJ3JnuWO3L3QSU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221778; c=relaxed/simple; bh=+87wRWX8FfsTtq4xSYLpb+JNh3+dyVdlqbxELomxVag=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=E8aH1SzTJvsdgcU4seoW9bLTGhxI4YIBE7pqeUYtDrhjAYwG/xAwHG20+od9tEtoTbRsHpEpZMNNkK84hddVzZ62r/H80Gu569xO7S8mn2LbhDLP59iJa/kLvHJLrrIdzKeQ1SseZRCw80ne1NuA0o6PPH6t2I2gcbhrl13jVtw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Zpdv0lB6; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Zpdv0lB6" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-212120f90e8so13200155ad.2 for ; Thu, 21 Nov 2024 12:42:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732221775; x=1732826575; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=vSBdFEgbOrqYxqatdw90LLZcTNyeQtzBA1zNti6V7OI=; b=Zpdv0lB63wc2oP+RWDcxGY2fnK5fTJ06qJRWdi/iR2rfKVzFguRWPqYpC7aLNnG6Eg ZK8JI0GhqC4LR7B9bjb/XqTuwZp2IrdE09M9bL7X1r5xtgTdadAwZVW1BxEajs1vPtd1 IMueWMMi5iIP04esUv4tn2HQD0WO9g0q+MrA3Mqa2brpGgDXqYZMvlbeS8Kru6PAuRqC 6j89WhyBiPizXugXYgueSOMqmajZAO4duuOq/DWmnoZPgcwF8ZeRR0nNdEFzWBTNE7XB 4ZAEnKRr9++ne5pqAMqg+u1eF1rzFdb+ay1Z71O9eBGIbf9q0sV6mf6bvcUzfycjYmBE tREg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732221775; x=1732826575; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=vSBdFEgbOrqYxqatdw90LLZcTNyeQtzBA1zNti6V7OI=; b=HqhZYTix1Zj0z7ZIWG3LZHDRme9BEaGUBbSh2sK2t0JcQGUHZIHxh6AxwjOCTMitl4 PeJlFac6zcFFCzVk1RLP+yBTAkBz32d182ZC5/hSNlfXWKdoWLNNhexanYnO7nYSsVzf 1WFGUIZ5xLWWx4MnNtV1+pENUj7FQAdcgf8LOW/G+wTzmaOBl+Ni4xDNPdITAVj8QDv6 ilxpgMSnKP/w4yiMLyA5P5kAx8fyX1V+tgd7ZlQrAs6PZuJoSdLihJ4gpCqJtOwON6mR iM8hR+MM0Lh19uY9we7YvR3NGzRXY/MBlNtYqojBAfIJIVLl503y2EVPxAt+M6qMhfbH joXw== X-Forwarded-Encrypted: i=1; AJvYcCUXGG+M7vOWjxG/Gy4RO4z2l3fMUaVGjVug1tHuNlenXlpP2o1iioaDLxvaGWECZIxY3KwFZr8ULPSQUSS3@vger.kernel.org X-Gm-Message-State: AOJu0YyPUL5QNrzLU0hWkJ/POKd0AGm2dkRj0yOvZU2UJRKy43DrBWQx yOSCmA1NXMee5wfMgz32dLTC7N3fKyTAhSW+UBLaZDfddLPHZASWQ17SxL8guOVY6XXkoTQnIz2 5VDB7kXJ+aO15xnHZ/penDuIMuw== X-Google-Smtp-Source: AGHT+IF59PpuAkqWOFNmN7S7Fz06WzIx7bGFa+nImvct3hmlYntcXhO6BBiDf9SqB9/zAUWFxuv9z14FKtFlbEY9VnQ= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a17:903:22c9:b0:20c:62e1:635d with SMTP id d9443c01a7336-2129f27f694mr2765ad.7.1732221774743; Thu, 21 Nov 2024 12:42:54 -0800 (PST) Date: Thu, 21 Nov 2024 20:42:32 +0000 In-Reply-To: <20241121204220.2378181-20-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121204220.2378181-20-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=16900; i=samitolvanen@google.com; h=from:subject; bh=+87wRWX8FfsTtq4xSYLpb+JNh3+dyVdlqbxELomxVag=; b=owGbwMvMwCEWxa662nLh8irG02pJDOn2s41aN4dWrA/qzuKYedRgx5M482s669zMr8oJ/D7+c 9r6ubsXdpSyMIhxMMiKKbK0fF29dfd3p9RXn4skYOawMoEMYeDiFICJtPgyMqzv4rZ1vf/sQOsC vwllsx1K4sqkN7zgm6CyZomURsmchmcM/z3Ln5z533Rff5KHdNER1T32Vac478T1MOv/6X09q5R TnQMA X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121204220.2378181-31-samitolvanen@google.com> Subject: [PATCH v6 11/18] gendwarfksyms: Add symtypes output From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Add support for producing genksyms-style symtypes files. Process die_map to find the longest expansions for each type, and use symtypes references in type definitions. The basic file format is similar to genksyms, with two notable exceptions: 1. Type names with spaces (common with Rust) in references are wrapped in single quotes. E.g.: s#'core::result::Result' 2. The actual type definition is the simple parsed DWARF format we output with --dump-dies, not the preprocessed C-style format genksyms produces. Signed-off-by: Sami Tolvanen Reviewed-by: Petr Pavlu --- scripts/gendwarfksyms/Makefile | 1 + scripts/gendwarfksyms/die.c | 11 + scripts/gendwarfksyms/dwarf.c | 1 + scripts/gendwarfksyms/gendwarfksyms.c | 33 ++- scripts/gendwarfksyms/gendwarfksyms.h | 19 ++ scripts/gendwarfksyms/symbols.c | 4 +- scripts/gendwarfksyms/types.c | 359 ++++++++++++++++++++++++++ 7 files changed, 425 insertions(+), 3 deletions(-) create mode 100644 scripts/gendwarfksyms/types.c diff --git a/scripts/gendwarfksyms/Makefile b/scripts/gendwarfksyms/Makefile index c06145d84df8..6540282dc746 100644 --- a/scripts/gendwarfksyms/Makefile +++ b/scripts/gendwarfksyms/Makefile @@ -6,5 +6,6 @@ gendwarfksyms-objs += cache.o gendwarfksyms-objs += die.o gendwarfksyms-objs += dwarf.o gendwarfksyms-objs += symbols.o +gendwarfksyms-objs += types.o HOSTLDLIBS_gendwarfksyms := -ldw -lelf diff --git a/scripts/gendwarfksyms/die.c b/scripts/gendwarfksyms/die.c index 2829387fd815..df1ca3a032bb 100644 --- a/scripts/gendwarfksyms/die.c +++ b/scripts/gendwarfksyms/die.c @@ -22,6 +22,7 @@ static inline unsigned int die_hash(uintptr_t addr, enum die_state state) static void init_die(struct die *cd) { cd->state = DIE_INCOMPLETE; + cd->mapped = false; cd->fqn = NULL; cd->tag = -1; cd->addr = 0; @@ -83,6 +84,16 @@ static void reset_die(struct die *cd) init_die(cd); } +void die_map_for_each(die_map_callback_t func, void *arg) +{ + struct hlist_node *tmp; + struct die *cd; + + hash_for_each_safe(die_map, cd, tmp, hash) { + func(cd, arg); + } +} + void die_map_free(void) { struct hlist_node *tmp; diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index cf6d2c2844dc..18e42dbbfa3c 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -744,6 +744,7 @@ static void process_symbol(struct state *state, Dwarf_Die *die, { debug("%s", state->sym->name); check(process_func(state, NULL, die)); + state->sym->state = SYMBOL_MAPPED; if (dump_dies) fputs("\n", stderr); } diff --git a/scripts/gendwarfksyms/gendwarfksyms.c b/scripts/gendwarfksyms/gendwarfksyms.c index ed6d8c585268..76a38b733ad2 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.c +++ b/scripts/gendwarfksyms/gendwarfksyms.c @@ -21,6 +21,11 @@ int debug; int dump_dies; /* Print debugging information about die_map changes */ int dump_die_map; +/* Print out type strings (i.e. type_map) */ +int dump_types; +/* Write a symtypes file */ +int symtypes; +static const char *symtypes_file; static void usage(void) { @@ -29,6 +34,8 @@ static void usage(void) " -d, --debug Print debugging information\n" " --dump-dies Dump DWARF DIE contents\n" " --dump-die-map Print debugging information about die_map changes\n" + " --dump-types Dump type strings\n" + " -T, --symtypes file Write a symtypes file\n" " -h, --help Print this message\n" "\n", stderr); @@ -41,6 +48,7 @@ static int process_module(Dwfl_Module *mod, void **userdata, const char *name, Dwarf_Die cudie; Dwarf_CU *cu = NULL; Dwarf *dbg; + FILE *symfile = arg; int res; debug("%s", name); @@ -60,6 +68,10 @@ static int process_module(Dwfl_Module *mod, void **userdata, const char *name, process_cu(&cudie); } while (cu); + /* + * Use die_map to expand type strings and write them to `symfile`. + */ + generate_symtypes(symfile); die_map_free(); return DWARF_CB_OK; @@ -72,22 +84,29 @@ static const Dwfl_Callbacks callbacks = { int main(int argc, char **argv) { + FILE *symfile = NULL; unsigned int n; int opt; struct option opts[] = { { "debug", 0, NULL, 'd' }, { "dump-dies", 0, &dump_dies, 1 }, { "dump-die-map", 0, &dump_die_map, 1 }, + { "dump-types", 0, &dump_types, 1 }, + { "symtypes", 1, NULL, 'T' }, { "help", 0, NULL, 'h' }, { 0, 0, NULL, 0 } }; - while ((opt = getopt_long(argc, argv, "dh", opts, NULL)) != EOF) { + while ((opt = getopt_long(argc, argv, "dT:h", opts, NULL)) != EOF) { switch (opt) { case 0: break; case 'd': debug = 1; break; + case 'T': + symtypes = 1; + symtypes_file = optarg; + break; case 'h': usage(); return 0; @@ -107,6 +126,13 @@ int main(int argc, char **argv) symbol_read_exports(stdin); + if (symtypes_file) { + symfile = fopen(symtypes_file, "w"); + if (!symfile) + error("fopen failed for '%s': %s", symtypes_file, + strerror(errno)); + } + for (n = optind; n < argc; n++) { Dwfl *dwfl; int fd; @@ -129,12 +155,15 @@ int main(int argc, char **argv) dwfl_report_end(dwfl, NULL, NULL); - if (dwfl_getmodules(dwfl, &process_module, NULL, 0)) + if (dwfl_getmodules(dwfl, &process_module, symfile, 0)) error("dwfl_getmodules failed for '%s'", argv[n]); dwfl_end(dwfl); } + if (symfile) + check(fclose(symfile)); + symbol_free(); return 0; diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h index 10fb476a7b76..7b35043d28ff 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -27,6 +27,8 @@ extern int debug; extern int dump_dies; extern int dump_die_map; +extern int dump_types; +extern int symtypes; /* * Output helpers @@ -95,6 +97,11 @@ static inline unsigned int addr_hash(uintptr_t addr) return hash_ptr((const void *)addr); } +enum symbol_state { + SYMBOL_UNPROCESSED, + SYMBOL_MAPPED, +}; + struct symbol_addr { uint32_t section; Elf64_Addr address; @@ -105,6 +112,8 @@ struct symbol { struct symbol_addr addr; struct hlist_node addr_hash; struct hlist_node name_hash; + enum symbol_state state; + uintptr_t die_addr; }; typedef void (*symbol_callback_t)(struct symbol *, void *arg); @@ -159,6 +168,7 @@ static inline const char *die_state_name(enum die_state state) struct die { enum die_state state; + bool mapped; char *fqn; int tag; uintptr_t addr; @@ -166,10 +176,13 @@ struct die { struct hlist_node hash; }; +typedef void (*die_map_callback_t)(struct die *, void *arg); + int __die_map_get(uintptr_t addr, enum die_state state, struct die **res); struct die *die_map_get(Dwarf_Die *die, enum die_state state); void die_map_add_string(struct die *pd, const char *str); void die_map_add_linebreak(struct die *pd, int linebreak); +void die_map_for_each(die_map_callback_t func, void *arg); void die_map_add_die(struct die *pd, struct die *child); void die_map_free(void); @@ -240,4 +253,10 @@ int process_die_container(struct state *state, struct die *cache, void process_cu(Dwarf_Die *cudie); +/* + * types.c + */ + +void generate_symtypes(FILE *file); + #endif /* __GENDWARFKSYMS_H */ diff --git a/scripts/gendwarfksyms/symbols.c b/scripts/gendwarfksyms/symbols.c index e7343394248b..6ed0c4769e6f 100644 --- a/scripts/gendwarfksyms/symbols.c +++ b/scripts/gendwarfksyms/symbols.c @@ -92,6 +92,7 @@ void symbol_read_exports(FILE *file) sym = xcalloc(1, sizeof(struct symbol)); sym->name = name; sym->addr.section = SHN_UNDEF; + sym->state = SYMBOL_UNPROCESSED; hash_add(symbol_names, &sym->name_hash, hash_str(sym->name)); ++nsym; @@ -107,7 +108,8 @@ static void get_symbol(struct symbol *sym, void *arg) { struct symbol **res = arg; - *res = sym; + if (sym->state == SYMBOL_UNPROCESSED) + *res = sym; } struct symbol *symbol_get(const char *name) diff --git a/scripts/gendwarfksyms/types.c b/scripts/gendwarfksyms/types.c new file mode 100644 index 000000000000..57ef5cbcfd40 --- /dev/null +++ b/scripts/gendwarfksyms/types.c @@ -0,0 +1,359 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Google LLC + */ + +#include "gendwarfksyms.h" + +static struct cache expansion_cache; + +/* + * A simple linked list of shared or owned strings to avoid copying strings + * around when not necessary. + */ +struct type_list_entry { + const char *str; + void *owned; + struct list_head list; +}; + +static void type_list_free(struct list_head *list) +{ + struct type_list_entry *entry; + struct type_list_entry *tmp; + + list_for_each_entry_safe(entry, tmp, list, list) { + if (entry->owned) + free(entry->owned); + free(entry); + } + + INIT_LIST_HEAD(list); +} + +static int type_list_append(struct list_head *list, const char *s, void *owned) +{ + struct type_list_entry *entry; + + if (!s) + return 0; + + entry = xmalloc(sizeof(struct type_list_entry)); + entry->str = s; + entry->owned = owned; + list_add_tail(&entry->list, list); + + return strlen(entry->str); +} + +static void type_list_write(struct list_head *list, FILE *file) +{ + struct type_list_entry *entry; + + list_for_each_entry(entry, list, list) { + if (entry->str) + checkp(fputs(entry->str, file)); + } +} + +/* + * An expanded type string in symtypes format. + */ +struct type_expansion { + char *name; + size_t len; + struct list_head expanded; + struct hlist_node hash; +}; + +static void type_expansion_init(struct type_expansion *type) +{ + type->name = NULL; + type->len = 0; + INIT_LIST_HEAD(&type->expanded); +} + +static inline void type_expansion_free(struct type_expansion *type) +{ + free(type->name); + type->name = NULL; + type->len = 0; + type_list_free(&type->expanded); +} + +static void type_expansion_append(struct type_expansion *type, const char *s, + void *owned) +{ + type->len += type_list_append(&type->expanded, s, owned); +} + +/* + * type_map -- the longest expansions for each type. + * + * const char *name -> struct type_expansion * + */ +#define TYPE_HASH_BITS 16 +static HASHTABLE_DEFINE(type_map, 1 << TYPE_HASH_BITS); + +static int type_map_get(const char *name, struct type_expansion **res) +{ + struct type_expansion *e; + + hash_for_each_possible(type_map, e, hash, hash_str(name)) { + if (!strcmp(name, e->name)) { + *res = e; + return 0; + } + } + + return -1; +} + +static void type_map_add(const char *name, struct type_expansion *type) +{ + struct type_expansion *e; + + if (type_map_get(name, &e)) { + e = xmalloc(sizeof(struct type_expansion)); + type_expansion_init(e); + e->name = xstrdup(name); + + hash_add(type_map, &e->hash, hash_str(e->name)); + + if (dump_types) + debug("adding %s", e->name); + } else { + /* Use the longest available expansion */ + if (type->len <= e->len) + return; + + type_list_free(&e->expanded); + + if (dump_types) + debug("replacing %s", e->name); + } + + /* Take ownership of type->expanded */ + list_replace_init(&type->expanded, &e->expanded); + e->len = type->len; + + if (dump_types) { + checkp(fputs(e->name, stderr)); + checkp(fputs(" ", stderr)); + type_list_write(&e->expanded, stderr); + checkp(fputs("\n", stderr)); + } +} + +static void type_map_write(FILE *file) +{ + struct type_expansion *e; + struct hlist_node *tmp; + + if (!file) + return; + + hash_for_each_safe(type_map, e, tmp, hash) { + checkp(fputs(e->name, file)); + checkp(fputs(" ", file)); + type_list_write(&e->expanded, file); + checkp(fputs("\n", file)); + } +} + +static void type_map_free(void) +{ + struct type_expansion *e; + struct hlist_node *tmp; + + hash_for_each_safe(type_map, e, tmp, hash) { + type_expansion_free(e); + free(e); + } + + hash_init(type_map); +} + +/* + * Type reference format: #, where prefix: + * s -> structure + * u -> union + * e -> enum + * t -> typedef + * + * Names with spaces are additionally wrapped in single quotes. + */ +static char get_type_prefix(int tag) +{ + switch (tag) { + case DW_TAG_class_type: + case DW_TAG_structure_type: + return 's'; + case DW_TAG_union_type: + return 'u'; + case DW_TAG_enumeration_type: + return 'e'; + case DW_TAG_typedef_type: + return 't'; + default: + return 0; + } +} + +static char *get_type_name(struct die *cache) +{ + const char *quote; + char prefix; + char *name; + + if (cache->state == DIE_INCOMPLETE) { + warn("found incomplete cache entry: %p", cache); + return NULL; + } + if (!cache->fqn || !*cache->fqn) + return NULL; + + prefix = get_type_prefix(cache->tag); + if (!prefix) + return NULL; + + /* Wrap names with spaces in single quotes */ + quote = strstr(cache->fqn, " ") ? "'" : ""; + + /* #\0 */ + if (asprintf(&name, "%c#%s%s%s", prefix, quote, cache->fqn, quote) < 0) + error("asprintf failed for '%s'", cache->fqn); + + return name; +} + +static void __type_expand(struct die *cache, struct type_expansion *type, + bool recursive); + +static void type_expand_child(struct die *cache, struct type_expansion *type, + bool recursive) +{ + struct type_expansion child; + char *name; + + name = get_type_name(cache); + if (!name) { + __type_expand(cache, type, recursive); + return; + } + + if (recursive && !__cache_was_expanded(&expansion_cache, cache->addr)) { + __cache_mark_expanded(&expansion_cache, cache->addr); + type_expansion_init(&child); + __type_expand(cache, &child, true); + type_map_add(name, &child); + type_expansion_free(&child); + } + + type_expansion_append(type, name, name); +} + +static void __type_expand(struct die *cache, struct type_expansion *type, + bool recursive) +{ + struct die_fragment *df; + struct die *child; + + list_for_each_entry(df, &cache->fragments, list) { + switch (df->type) { + case FRAGMENT_STRING: + type_expansion_append(type, df->data.str, NULL); + break; + case FRAGMENT_DIE: + /* Use a complete die_map expansion if available */ + if (__die_map_get(df->data.addr, DIE_COMPLETE, + &child) && + __die_map_get(df->data.addr, DIE_UNEXPANDED, + &child)) + error("unknown child: %" PRIxPTR, + df->data.addr); + + type_expand_child(child, type, recursive); + break; + case FRAGMENT_LINEBREAK: + /* + * Keep whitespace in the symtypes format, but avoid + * repeated spaces. + */ + if (list_is_last(&df->list, &cache->fragments) || + list_next_entry(df, list)->type != + FRAGMENT_LINEBREAK) + type_expansion_append(type, " ", NULL); + break; + default: + error("empty die_fragment in %p", cache); + } + } +} + +static void type_expand(struct die *cache, struct type_expansion *type, + bool recursive) +{ + type_expansion_init(type); + __type_expand(cache, type, recursive); + cache_free(&expansion_cache); +} + +static void expand_type(struct die *cache, void *arg) +{ + struct type_expansion type; + char *name; + + if (cache->mapped) + return; + + cache->mapped = true; + + /* + * Skip unexpanded die_map entries if there's a complete + * expansion available for this DIE. + */ + if (cache->state == DIE_UNEXPANDED && + !__die_map_get(cache->addr, DIE_COMPLETE, &cache)) { + if (cache->mapped) + return; + + cache->mapped = true; + } + + name = get_type_name(cache); + if (!name) + return; + + debug("%s", name); + type_expand(cache, &type, true); + type_map_add(name, &type); + + type_expansion_free(&type); + free(name); +} + +void generate_symtypes(FILE *file) +{ + cache_init(&expansion_cache); + + /* + * die_map processing: + * + * 1. die_map contains all types referenced in exported symbol + * signatures, but can contain duplicates just like the original + * DWARF, and some references may not be fully expanded depending + * on how far we processed the DIE tree for that specific symbol. + * + * For each die_map entry, find the longest available expansion, + * and add it to type_map. + */ + die_map_for_each(expand_type, NULL); + + /* + * 2. If a symtypes file is requested, write type_map contents to + * the file. + */ + type_map_write(file); + type_map_free(); +} From patchwork Thu Nov 21 20:42:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13882350 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4328F1DEFC2 for ; Thu, 21 Nov 2024 20:42:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221779; cv=none; b=fNb6rXZRDAHSgudZt88B4pq4FSDdkBbOugjO3qoUwphVkzLhLlat++yXipOS6wIZ9Pr7Iv3qEV6vCSzK3u6jrB+OeEU8vqkbEa7pBqbq7ycmys92/HeUYY9MFigfqCVtmwg0sYrCdSnJKD5QOau1Zhe1wNs+COPR0eRXhCnson0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221779; c=relaxed/simple; bh=d61WNq/I69SxlkMnqNFV2qlrhf/2SXHOZM1SrnlAnY0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mnfQpZvropK2/Nsjx+xkqNTMZgx62AgCats06XfM7Grm8A3HUaGFgSYWPff09X9Tt5Yxarbup6RUh02JlqJ4K3VZH52vYrhfkbDFv/q9GkaNN9An4AXZFSOwJLr9gt2OKhTbQToIJOmtfkq+bxG60gLnQt4d9IO6itS+ElEmrJA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=3/EQsi60; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="3/EQsi60" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6eea70c89cbso16214777b3.1 for ; Thu, 21 Nov 2024 12:42:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732221776; x=1732826576; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=CA7kZhd6lCBwuA638WTbbaGGt/3ZtUV9FFVcc8mA6YY=; b=3/EQsi60cshZLN2x3NVzBY0E9afWaeY39IuiJ4jOdQYB2Z2pR5OrNTB8DrZd/YmogA 6saauw1SocblquyNcLSjAbeTORHD43xahn1dgq8pk8DEePUYakl1Sl3M/AufHcsYcAwS fSOA+TbgnjxvY19w7Ca89kiuSUS4lkmoSInEl1REcCwkFAGK3aNtr2uz0WmrTjOQ06+J qaYfQdP5OaivK8OtpPu1PDJsy3xpqhJaTkILCbchfLWnxY1dA9Q5/k9CfViqF3ypEKTm 2GdT2rcwLhgMM9PtFVxvdcLVGheeCgZOb3rFrUwSOXoXhNOzIwyz+k8Fh/yT4QmHaGcl N9Gg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732221776; x=1732826576; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=CA7kZhd6lCBwuA638WTbbaGGt/3ZtUV9FFVcc8mA6YY=; b=wHBn6jSfaIlP4xf6yi1XraAwdlXR8C1DooggSQw/1MPRcAAaiqnXRRpvv9s4izHG18 9dCKyT0kNw5dwL/ffECpioJeohxriw9yw8I2UOUswwDK94Yjg/zcTaMQQZLnYseu7i3J ffyuaqvgUJmeyCUtShu+ADEiuWGHqFEsTgoJJOuzun13HfNLb2pQQIoyXZ2oAx/QOv+U fQN40m8c6Ae09ig0+mJOMawwSmJh9BaGmEy1nb5jPNOdj0HpjgfieBn8jPhemx7Aej0F DWh+faSJLD+Lcm60qhK3W5O4KQYVqokf0pIK6SKclwT56U4WdeV1A+ktM0NgE/0n0cxA Hrqg== X-Forwarded-Encrypted: i=1; AJvYcCV/VJwRisuNmtIdGeDrqEM2u35mbKThDvbEJQk3GY3B8ev2iBqw5gzNPyqi+aXmHwneMN0Ci9BSPaIczsRW@vger.kernel.org X-Gm-Message-State: AOJu0YyOHse8z2us12s1m7qfJuNGUmbSAVpcKCb7IHsAdmu1BlpqzdU3 tDO0SdDgYNk3Yac8SVwycLlMQG5C0hYMa+Z4llm0aNAwtkuSoj891w+0aN+80CUKBVK5GX4D3aX XeGL+zkmNrPee5L139kx7a9Dkww== X-Google-Smtp-Source: AGHT+IGIStUYQFtNfnZeiVXqDFo80Ip7WtFIXyK4N4MXRIGGeS6hxLMBaHs6zF3f1OjEbxIyOQ+1lv7QOLz3Oqf1xgk= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a05:690c:2b90:b0:6ee:61ea:a40e with SMTP id 00721157ae682-6eecd2d1508mr356447b3.2.1732221776432; Thu, 21 Nov 2024 12:42:56 -0800 (PST) Date: Thu, 21 Nov 2024 20:42:33 +0000 In-Reply-To: <20241121204220.2378181-20-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121204220.2378181-20-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=12899; i=samitolvanen@google.com; h=from:subject; bh=d61WNq/I69SxlkMnqNFV2qlrhf/2SXHOZM1SrnlAnY0=; b=owGbwMvMwCEWxa662nLh8irG02pJDOn2s42DA3iFb1wX75nIVs3l9+5wFZPyvY+vk7NYC7SYV 1h5JXzqKGVhEONgkBVTZGn5unrr7u9Oqa8+F0nAzGFlAhnCwMUpABNh+83wTyfy/rvbHy02Lu7Y Kb7mdpD7uQmSL72XHalVeR+RGrl43xFGhp09nT8bOj7f3dgcvtoy5CNL/asvk9WZFlrxi8rfZWU S5wAA X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121204220.2378181-32-samitolvanen@google.com> Subject: [PATCH v6 12/18] gendwarfksyms: Add symbol versioning From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Calculate symbol versions from the fully expanded type strings in type_map, and output the versions in a genksyms-compatible format. Signed-off-by: Sami Tolvanen Reviewed-by: Petr Pavlu --- scripts/gendwarfksyms/Makefile | 2 +- scripts/gendwarfksyms/dwarf.c | 25 +++++- scripts/gendwarfksyms/gendwarfksyms.c | 10 ++- scripts/gendwarfksyms/gendwarfksyms.h | 13 ++- scripts/gendwarfksyms/symbols.c | 53 +++++++++++ scripts/gendwarfksyms/types.c | 122 +++++++++++++++++++++++++- 6 files changed, 216 insertions(+), 9 deletions(-) diff --git a/scripts/gendwarfksyms/Makefile b/scripts/gendwarfksyms/Makefile index 6540282dc746..e889b958957b 100644 --- a/scripts/gendwarfksyms/Makefile +++ b/scripts/gendwarfksyms/Makefile @@ -8,4 +8,4 @@ gendwarfksyms-objs += dwarf.o gendwarfksyms-objs += symbols.o gendwarfksyms-objs += types.o -HOSTLDLIBS_gendwarfksyms := -ldw -lelf +HOSTLDLIBS_gendwarfksyms := -ldw -lelf -lz diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index 18e42dbbfa3c..a35c351391ad 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -739,12 +739,33 @@ static int process_type(struct state *state, struct die *parent, Dwarf_Die *die) /* * Exported symbol processing */ +static struct die *get_symbol_cache(struct state *state, Dwarf_Die *die) +{ + struct die *cache; + + cache = die_map_get(die, DIE_SYMBOL); + + if (cache->state != DIE_INCOMPLETE) + return NULL; /* We already processed a symbol for this DIE */ + + cache->tag = dwarf_tag(die); + return cache; +} + static void process_symbol(struct state *state, Dwarf_Die *die, die_callback_t process_func) { + struct die *cache; + + symbol_set_die(state->sym, die); + + cache = get_symbol_cache(state, die); + if (!cache) + return; + debug("%s", state->sym->name); - check(process_func(state, NULL, die)); - state->sym->state = SYMBOL_MAPPED; + check(process_func(state, cache, die)); + cache->state = DIE_SYMBOL; if (dump_dies) fputs("\n", stderr); } diff --git a/scripts/gendwarfksyms/gendwarfksyms.c b/scripts/gendwarfksyms/gendwarfksyms.c index 76a38b733ad2..fd2429ea198f 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.c +++ b/scripts/gendwarfksyms/gendwarfksyms.c @@ -23,6 +23,8 @@ int dump_dies; int dump_die_map; /* Print out type strings (i.e. type_map) */ int dump_types; +/* Print out expanded type strings used for symbol versions */ +int dump_versions; /* Write a symtypes file */ int symtypes; static const char *symtypes_file; @@ -35,6 +37,7 @@ static void usage(void) " --dump-dies Dump DWARF DIE contents\n" " --dump-die-map Print debugging information about die_map changes\n" " --dump-types Dump type strings\n" + " --dump-versions Dump expanded type strings used for symbol versions\n" " -T, --symtypes file Write a symtypes file\n" " -h, --help Print this message\n" "\n", @@ -69,9 +72,10 @@ static int process_module(Dwfl_Module *mod, void **userdata, const char *name, } while (cu); /* - * Use die_map to expand type strings and write them to `symfile`. + * Use die_map to expand type strings, write them to `symfile`, and + * calculate symbol versions. */ - generate_symtypes(symfile); + generate_symtypes_and_versions(symfile); die_map_free(); return DWARF_CB_OK; @@ -92,6 +96,7 @@ int main(int argc, char **argv) { "dump-dies", 0, &dump_dies, 1 }, { "dump-die-map", 0, &dump_die_map, 1 }, { "dump-types", 0, &dump_types, 1 }, + { "dump-versions", 0, &dump_versions, 1 }, { "symtypes", 1, NULL, 'T' }, { "help", 0, NULL, 'h' }, { 0, 0, NULL, 0 } }; @@ -164,6 +169,7 @@ int main(int argc, char **argv) if (symfile) check(fclose(symfile)); + symbol_print_versions(); symbol_free(); return 0; diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h index 7b35043d28ff..4fd166908cbc 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -28,6 +28,7 @@ extern int debug; extern int dump_dies; extern int dump_die_map; extern int dump_types; +extern int dump_versions; extern int symtypes; /* @@ -100,6 +101,7 @@ static inline unsigned int addr_hash(uintptr_t addr) enum symbol_state { SYMBOL_UNPROCESSED, SYMBOL_MAPPED, + SYMBOL_PROCESSED }; struct symbol_addr { @@ -114,6 +116,7 @@ struct symbol { struct hlist_node name_hash; enum symbol_state state; uintptr_t die_addr; + unsigned long crc; }; typedef void (*symbol_callback_t)(struct symbol *, void *arg); @@ -121,6 +124,10 @@ typedef void (*symbol_callback_t)(struct symbol *, void *arg); void symbol_read_exports(FILE *file); void symbol_read_symtab(int fd); struct symbol *symbol_get(const char *name); +void symbol_set_die(struct symbol *sym, Dwarf_Die *die); +void symbol_set_crc(struct symbol *sym, unsigned long crc); +void symbol_for_each(symbol_callback_t func, void *arg); +void symbol_print_versions(void); void symbol_free(void); /* @@ -131,7 +138,8 @@ enum die_state { DIE_INCOMPLETE, DIE_UNEXPANDED, DIE_COMPLETE, - DIE_LAST = DIE_COMPLETE + DIE_SYMBOL, + DIE_LAST = DIE_SYMBOL }; enum die_fragment_type { @@ -161,6 +169,7 @@ static inline const char *die_state_name(enum die_state state) CASE_CONST_TO_STR(DIE_INCOMPLETE) CASE_CONST_TO_STR(DIE_UNEXPANDED) CASE_CONST_TO_STR(DIE_COMPLETE) + CASE_CONST_TO_STR(DIE_SYMBOL) } error("unexpected die_state: %d", state); @@ -257,6 +266,6 @@ void process_cu(Dwarf_Die *cudie); * types.c */ -void generate_symtypes(FILE *file); +void generate_symtypes_and_versions(FILE *file); #endif /* __GENDWARFKSYMS_H */ diff --git a/scripts/gendwarfksyms/symbols.c b/scripts/gendwarfksyms/symbols.c index 6ed0c4769e6f..e0c9007f7250 100644 --- a/scripts/gendwarfksyms/symbols.c +++ b/scripts/gendwarfksyms/symbols.c @@ -66,6 +66,36 @@ static unsigned int for_each(const char *name, symbol_callback_t func, return 0; } +static void set_crc(struct symbol *sym, void *data) +{ + unsigned long *crc = data; + + if (sym->state == SYMBOL_PROCESSED && sym->crc != *crc) + warn("overriding version for symbol %s (crc %lx vs. %lx)", + sym->name, sym->crc, *crc); + + sym->state = SYMBOL_PROCESSED; + sym->crc = *crc; +} + +void symbol_set_crc(struct symbol *sym, unsigned long crc) +{ + if (for_each(sym->name, set_crc, &crc) == 0) + error("no matching symbols: '%s'", sym->name); +} + +static void set_die(struct symbol *sym, void *data) +{ + sym->die_addr = (uintptr_t)((Dwarf_Die *)data)->addr; + sym->state = SYMBOL_MAPPED; +} + +void symbol_set_die(struct symbol *sym, Dwarf_Die *die) +{ + if (for_each(sym->name, set_die, die) == 0) + error("no matching symbols: '%s'", sym->name); +} + static bool is_exported(const char *name) { return for_each(name, NULL, NULL) > 0; @@ -120,6 +150,16 @@ struct symbol *symbol_get(const char *name) return sym; } +void symbol_for_each(symbol_callback_t func, void *arg) +{ + struct hlist_node *tmp; + struct symbol *sym; + + hash_for_each_safe(symbol_names, sym, tmp, name_hash) { + func(sym, arg); + } +} + typedef void (*elf_symbol_callback_t)(const char *name, GElf_Sym *sym, Elf32_Word xndx, void *arg); @@ -244,6 +284,19 @@ void symbol_read_symtab(int fd) elf_for_each_global(fd, elf_set_symbol_addr, NULL); } +void symbol_print_versions(void) +{ + struct hlist_node *tmp; + struct symbol *sym; + + hash_for_each_safe(symbol_names, sym, tmp, name_hash) { + if (sym->state != SYMBOL_PROCESSED) + warn("no information for symbol %s", sym->name); + + printf("#SYMVER %s 0x%08lx\n", sym->name, sym->crc); + } +} + void symbol_free(void) { struct hlist_node *tmp; diff --git a/scripts/gendwarfksyms/types.c b/scripts/gendwarfksyms/types.c index 57ef5cbcfd40..f4dbd21b83e6 100644 --- a/scripts/gendwarfksyms/types.c +++ b/scripts/gendwarfksyms/types.c @@ -3,6 +3,7 @@ * Copyright (C) 2024 Google LLC */ +#include #include "gendwarfksyms.h" static struct cache expansion_cache; @@ -174,6 +175,33 @@ static void type_map_free(void) hash_init(type_map); } +/* + * CRC for a type, with an optional fully expanded type string for + * debugging. + */ +struct version { + struct type_expansion type; + unsigned long crc; +}; + +static void version_init(struct version *version) +{ + version->crc = crc32(0, NULL, 0); + type_expansion_init(&version->type); +} + +static void version_free(struct version *version) +{ + type_expansion_free(&version->type); +} + +static void version_add(struct version *version, const char *s) +{ + version->crc = crc32(version->crc, (void *)s, strlen(s)); + if (dump_versions) + type_expansion_append(&version->type, s, NULL); +} + /* * Type reference format: #, where prefix: * s -> structure @@ -183,6 +211,12 @@ static void type_map_free(void) * * Names with spaces are additionally wrapped in single quotes. */ +static inline bool is_type_prefix(const char *s) +{ + return (s[0] == 's' || s[0] == 'u' || s[0] == 'e' || s[0] == 't') && + s[1] == '#'; +} + static char get_type_prefix(int tag) { switch (tag) { @@ -210,6 +244,8 @@ static char *get_type_name(struct die *cache) warn("found incomplete cache entry: %p", cache); return NULL; } + if (cache->state == DIE_SYMBOL) + return NULL; if (!cache->fqn || !*cache->fqn) return NULL; @@ -227,6 +263,39 @@ static char *get_type_name(struct die *cache) return name; } +static void __calculate_version(struct version *version, struct list_head *list) +{ + struct type_list_entry *entry; + struct type_expansion *e; + + /* Calculate a CRC over an expanded type string */ + list_for_each_entry(entry, list, list) { + if (is_type_prefix(entry->str)) { + check(type_map_get(entry->str, &e)); + + /* + * It's sufficient to expand each type reference just + * once to detect changes. + */ + if (cache_was_expanded(&expansion_cache, e)) { + version_add(version, entry->str); + } else { + cache_mark_expanded(&expansion_cache, e); + __calculate_version(version, &e->expanded); + } + } else { + version_add(version, entry->str); + } + } +} + +static void calculate_version(struct version *version, struct list_head *list) +{ + version_init(version); + __calculate_version(version, list); + cache_free(&expansion_cache); +} + static void __type_expand(struct die *cache, struct type_expansion *type, bool recursive); @@ -333,7 +402,49 @@ static void expand_type(struct die *cache, void *arg) free(name); } -void generate_symtypes(FILE *file) +static void expand_symbol(struct symbol *sym, void *arg) +{ + struct type_expansion type; + struct version version; + struct die *cache; + + /* + * No need to expand again unless we want a symtypes file entry + * for the symbol. Note that this means `sym` has the same address + * as another symbol that was already processed. + */ + if (!symtypes && sym->state == SYMBOL_PROCESSED) + return; + + if (__die_map_get(sym->die_addr, DIE_SYMBOL, &cache)) + return; /* We'll warn about missing CRCs later. */ + + type_expand(cache, &type, false); + + /* If the symbol already has a version, don't calculate it again. */ + if (sym->state != SYMBOL_PROCESSED) { + calculate_version(&version, &type.expanded); + symbol_set_crc(sym, version.crc); + debug("%s = %lx", sym->name, version.crc); + + if (dump_versions) { + checkp(fputs(sym->name, stderr)); + checkp(fputs(" ", stderr)); + type_list_write(&version.type.expanded, stderr); + checkp(fputs("\n", stderr)); + } + + version_free(&version); + } + + /* These aren't needed in type_map unless we want a symtypes file. */ + if (symtypes) + type_map_add(sym->name, &type); + + type_expansion_free(&type); +} + +void generate_symtypes_and_versions(FILE *file) { cache_init(&expansion_cache); @@ -351,7 +462,14 @@ void generate_symtypes(FILE *file) die_map_for_each(expand_type, NULL); /* - * 2. If a symtypes file is requested, write type_map contents to + * 2. For each exported symbol, expand the die_map type, and use + * type_map expansions to calculate a symbol version from the + * fully expanded type string. + */ + symbol_for_each(expand_symbol, NULL); + + /* + * 3. If a symtypes file is requested, write type_map contents to * the file. */ type_map_write(file); From patchwork Thu Nov 21 20:42:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13882351 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 334DC1DF243 for ; Thu, 21 Nov 2024 20:42:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221781; cv=none; b=tyIqWi0/UQvdc1HnReUtTpEWxBFPGLlMk5y6I6HQaV/PZKI3g63L2C9Hin3hEnoO8/N/FYosErlpa0zF6jYH4fWgoo0Ww0ESrqU0oe722uc6pcQTpAwlS9xc4MenLjiQCZ/HI73IfRjkVROaPZJvhyqD4TQ0rSd2cH6wI9dj7qc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221781; c=relaxed/simple; bh=lOPLoiX2fvGtodwJcA3yLof2Xs1fEabO5h2esQDkR5k=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XjsRpqPINIiJvtU8UA3fwR/0U0x7k8s1b75uhaePqmOZVUmyelFkvmoWZj1Thd6iIn6lnLrFMg18fTQWLoHxY9fDx1mIXCAnW3jJy819GZhDsknvwJ7o+RuUBqVYqfxLSZPy6W2xWZwysdGuARyswg95oyoWS2sGb7/WSWtnVRM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=UQoDc7WD; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="UQoDc7WD" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e3891b4a68bso2286407276.2 for ; Thu, 21 Nov 2024 12:42:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732221778; x=1732826578; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=2fnEhMax+hvKkublzO3JjrVIV9AK+N/c7/FlPsCvcbw=; b=UQoDc7WDxbg/M6Bhp/DFjdPJRtviT2F7ZKvi3Zd1iZKplcV67SDXXxggW0FW/vRYfR rMTEXhUixEOhzMAmV1/5pwELZTio+NeLTu8pLCf3PRS41EAttYJIw6K46Zp8Nw4BZ7B1 s7qC03b6RjgE1M96tQ97OpjJtJNEd7+x+SjuXULKn0OqxuBVux8rN6mxkB1obBanLLg3 M/8Kx1w79KFdCxP93hVrzBNLt8UvfgO60LAh0caMNYKpXxODY5lsaEvgGhpzIJ85+ry6 AejL2AXeDnW06Y9As55Oh8RjMSMO6tgM8OK5iC4g90SuqnUWnz9KHPmhWNMIhRfwmh8/ ipkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732221778; x=1732826578; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=2fnEhMax+hvKkublzO3JjrVIV9AK+N/c7/FlPsCvcbw=; b=RM0MJwvTSkoyJTh7GltFrGEfm31+ytNmOkOAZO+SYpwj0QeMj7AnA+IK++dsGzUYQS A7tCd+Q9e1yz4YQjjJixHXd6hIR34thvXI+E+qsWL9VIWKNDw7BMuuY5yRty5LcxRZLU H/O/c2xAPYfHqe5elrlrkTlnahpO7US64Bztw9Y1Uym8AQP9spDnz7fgfwLK0tJkixVw /ymYXc0KdZoA2gi1Yo+jeOePKyt4xY/0Lqbafbe8tXNHWj+WXx7FGDmW/QhboqReGpem o8GYyWnxXzLCHhenwIoQ6wR7s+4x5N8hrOND465b3KY6Ri5dNUV0ri8sw3Uy8lAfV45y zCMA== X-Forwarded-Encrypted: i=1; AJvYcCX4H3epoHXNbTGaHXTlTu52bNwnlYSVeypY0MNaDMcLzNVLT62M+vWlIEXVBD92FjiDmi+KvmFRBIp6Ii2g@vger.kernel.org X-Gm-Message-State: AOJu0YxK6Fii7gAcUoa2bMaX2veWl6Ic/s+AXQ2mJP2Hve1xnF/2N8Xc 5Adj1lFZLDnmgm5XYTd1Qco8EJfBFTq6FgG1QWav3otu/4yhVekkbn+4cW/2YztoOewUCvX2SSU EkAMc73pJUqKlhuG6OkmQNCQa4w== X-Google-Smtp-Source: AGHT+IGBOdNeVrcqdEMTmYur7nsXQnsxA9pbI5NBRS2J92Z2vi6jlERog26KSRH4JXEvyOuEbJQZyO5xg5PcWMOiR8Q= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a25:ce49:0:b0:e2e:3031:3f0c with SMTP id 3f1490d57ef6-e38f8bdd701mr95276.7.1732221778217; Thu, 21 Nov 2024 12:42:58 -0800 (PST) Date: Thu, 21 Nov 2024 20:42:34 +0000 In-Reply-To: <20241121204220.2378181-20-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121204220.2378181-20-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=21270; i=samitolvanen@google.com; h=from:subject; bh=lOPLoiX2fvGtodwJcA3yLof2Xs1fEabO5h2esQDkR5k=; b=owGbwMvMwCEWxa662nLh8irG02pJDOn2s40rytVvPFb2kQ9fc0856c4f2ciVN0Iu3GGfovzg6 Epmg51zOkpZGMQ4GGTFFFlavq7euvu7U+qrz0USMHNYmUCGMHBxCsBE3kxg+B8WdOA0/86Abx9j 6zY5LJLV/dD+on+nXKJjQ/hDrvf7Qj8zMmw0iXuqzXCESyXz+MKfU+tdDm+2WZBeZdK+5+A9J5O jCWwA X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121204220.2378181-33-samitolvanen@google.com> Subject: [PATCH v6 13/18] gendwarfksyms: Add support for kABI rules From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Distributions that want to maintain a stable kABI need the ability to make ABI compatible changes to kernel without affecting symbol versions, either because of LTS updates or backports. With genksyms, developers would typically hide these changes from version calculation with #ifndef __GENKSYMS__, which would result in the symbol version not changing even though the actual type has changed. When we process precompiled object files, this isn't an option. To support this use case, add a --stable command line flag that gates kABI stability features that are not needed in mainline kernels, but can be useful for distributions, and add support for kABI rules, which can be used to restrict gendwarfksyms output. The rules are specified as a set of null-terminated strings stored in the .discard.gendwarfksyms.kabi_rules section. Each rule consists of four strings as follows: "version\0type\0target\0value" The version string ensures the structure can be changed in a backwards compatible way. The type string indicates the type of the rule, and target and value strings contain rule-specific data. Initially support two simple rules: 1. Declaration-only types A type declaration can change into a full definition when additional includes are pulled in to the TU, which changes the versions of any symbol that references the type. Add support for defining declaration-only types whose definition is not expanded during versioning. 2. Ignored enumerators It's possible to add new enum fields without changing the ABI, but as the fields are included in symbol versioning, this would change the versions. Add support for ignoring specific fields. 3. Overridden enumerator values Add support for overriding enumerator values when calculating versions. This may be needed when the last field of the enum is used as a sentinel and new fields must be added before it. Add examples for using the rules under the examples/ directory. Signed-off-by: Sami Tolvanen --- scripts/gendwarfksyms/Makefile | 1 + scripts/gendwarfksyms/dwarf.c | 25 +- scripts/gendwarfksyms/examples/kabi.h | 70 +++++ scripts/gendwarfksyms/examples/kabi_ex.c | 14 + scripts/gendwarfksyms/examples/kabi_ex.h | 64 +++++ scripts/gendwarfksyms/gendwarfksyms.c | 11 +- scripts/gendwarfksyms/gendwarfksyms.h | 14 + scripts/gendwarfksyms/kabi.c | 333 +++++++++++++++++++++++ 8 files changed, 528 insertions(+), 4 deletions(-) create mode 100644 scripts/gendwarfksyms/examples/kabi.h create mode 100644 scripts/gendwarfksyms/examples/kabi_ex.c create mode 100644 scripts/gendwarfksyms/examples/kabi_ex.h create mode 100644 scripts/gendwarfksyms/kabi.c diff --git a/scripts/gendwarfksyms/Makefile b/scripts/gendwarfksyms/Makefile index e889b958957b..6334c7d3c4d5 100644 --- a/scripts/gendwarfksyms/Makefile +++ b/scripts/gendwarfksyms/Makefile @@ -5,6 +5,7 @@ gendwarfksyms-objs += gendwarfksyms.o gendwarfksyms-objs += cache.o gendwarfksyms-objs += die.o gendwarfksyms-objs += dwarf.o +gendwarfksyms-objs += kabi.o gendwarfksyms-objs += symbols.o gendwarfksyms-objs += types.o diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index a35c351391ad..04b38caa846f 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -119,13 +119,16 @@ static bool is_definition_private(Dwarf_Die *die) return !!res; } -static bool is_declaration(Dwarf_Die *die) +static bool is_declaration(struct die *cache, Dwarf_Die *die) { bool value; if (get_flag_attr(die, DW_AT_declaration, &value) && value) return true; + if (kabi_is_declonly(cache->fqn)) + return true; + return is_definition_private(die); } @@ -514,9 +517,10 @@ static void __process_structure_type(struct state *state, struct die *cache, process(cache, " {"); process_linebreak(cache, 1); - is_decl = is_declaration(die); + is_decl = is_declaration(cache, die); if (!is_decl && state->expand.expand) { + state->expand.current_fqn = cache->fqn; check(process_die_container(state, cache, die, process_func, match_func)); } @@ -547,13 +551,26 @@ DEFINE_PROCESS_STRUCTURE_TYPE(union) static void process_enumerator_type(struct state *state, struct die *cache, Dwarf_Die *die) { + bool overridden = false; Dwarf_Word value; + if (stable) { + /* Get the fqn before we process anything */ + update_fqn(cache, die); + + if (kabi_is_enumerator_ignored(state->expand.current_fqn, + cache->fqn)) + return; + + overridden = kabi_get_enumerator_value( + state->expand.current_fqn, cache->fqn, &value); + } + process_list_comma(state, cache); process(cache, "enumerator"); process_fqn(cache, die); - if (get_udata_attr(die, DW_AT_const_value, &value)) { + if (overridden || get_udata_attr(die, DW_AT_const_value, &value)) { process(cache, " = "); process_fmt(cache, "%" PRIu64, value); } @@ -619,6 +636,7 @@ static void process_cached(struct state *state, struct die *cache, static void state_init(struct state *state) { state->expand.expand = true; + state->expand.current_fqn = NULL; cache_init(&state->expansion_cache); } @@ -626,6 +644,7 @@ static void expansion_state_restore(struct expansion_state *state, struct expansion_state *saved) { state->expand = saved->expand; + state->current_fqn = saved->current_fqn; } static void expansion_state_save(struct expansion_state *state, diff --git a/scripts/gendwarfksyms/examples/kabi.h b/scripts/gendwarfksyms/examples/kabi.h new file mode 100644 index 000000000000..fcd0300e5b58 --- /dev/null +++ b/scripts/gendwarfksyms/examples/kabi.h @@ -0,0 +1,70 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2024 Google LLC + * + * Example macros for maintaining kABI stability. + * + * This file is based on android_kabi.h, which has the following notice: + * + * Heavily influenced by rh_kabi.h which came from the RHEL/CENTOS kernel + * and was: + * Copyright (c) 2014 Don Zickus + * Copyright (c) 2015-2018 Jiri Benc + * Copyright (c) 2015 Sabrina Dubroca, Hannes Frederic Sowa + * Copyright (c) 2016-2018 Prarit Bhargava + * Copyright (c) 2017 Paolo Abeni, Larry Woodman + */ + +#ifndef __KABI_H__ +#define __KABI_H__ + +/* Kernel macros for userspace testing. */ +#ifndef __aligned +#define __aligned(x) __attribute__((__aligned__(x))) +#endif +#ifndef __used +#define __used __attribute__((__used__)) +#endif +#ifndef __section +#define __section(section) __attribute__((__section__(section))) +#endif +#ifndef __PASTE +#define ___PASTE(a, b) a##b +#define __PASTE(a, b) ___PASTE(a, b) +#endif +#ifndef __stringify +#define __stringify_1(x...) #x +#define __stringify(x...) __stringify_1(x) +#endif + +#define __KABI_RULE(hint, target, value) \ + static const char __PASTE(__gendwarfksyms_rule_, \ + __COUNTER__)[] __used __aligned(1) \ + __section(".discard.gendwarfksyms.kabi_rules") = \ + "1\0" #hint "\0" #target "\0" #value + +/* + * KABI_DECLONLY(fqn) + * Treat the struct/union/enum fqn as a declaration, i.e. even if + * a definition is available, don't expand the contents. + */ +#define KABI_DECLONLY(fqn) __KABI_RULE(declonly, fqn, ) + +/* + * KABI_ENUMERATOR_IGNORE(fqn, field) + * When expanding enum fqn, skip the provided field. This makes it + * possible to hide added enum fields from versioning. + */ +#define KABI_ENUMERATOR_IGNORE(fqn, field) \ + __KABI_RULE(enumerator_ignore, fqn field, ) + +/* + * KABI_ENUMERATOR_VALUE(fqn, field, value) + * When expanding enum fqn, use the provided value for the + * specified field. This makes it possible to override enumerator + * values when calculating versions. + */ +#define KABI_ENUMERATOR_VALUE(fqn, field, value) \ + __KABI_RULE(enumerator_value, fqn field, value) + +#endif /* __KABI_H__ */ diff --git a/scripts/gendwarfksyms/examples/kabi_ex.c b/scripts/gendwarfksyms/examples/kabi_ex.c new file mode 100644 index 000000000000..799552ea6679 --- /dev/null +++ b/scripts/gendwarfksyms/examples/kabi_ex.c @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * kabi_ex.c + * + * Copyright (C) 2024 Google LLC + * + * Examples for kABI stability features with --stable. See kabi_ex.h + * for details. + */ + +#include "kabi_ex.h" + +struct s e0; +enum e e1; diff --git a/scripts/gendwarfksyms/examples/kabi_ex.h b/scripts/gendwarfksyms/examples/kabi_ex.h new file mode 100644 index 000000000000..fca1e07c78e2 --- /dev/null +++ b/scripts/gendwarfksyms/examples/kabi_ex.h @@ -0,0 +1,64 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * kabi_ex.h + * + * Copyright (C) 2024 Google LLC + * + * Examples for kABI stability features with --stable. + */ + +/* + * The comments below each example contain the expected gendwarfksyms + * output, which can be verified using LLVM's FileCheck tool: + * + * https://llvm.org/docs/CommandGuide/FileCheck.html + * + * Usage: + * + * $ gcc -g -c examples/kabi_ex.c -o examples/kabi_ex.o + * + * $ nm examples/kabi_ex.o | awk '{ print $NF }' | \ + * ./gendwarfksyms --stable --dump-dies \ + * examples/kabi_ex.o 2>&1 >/dev/null | \ + * FileCheck examples/kabi_ex.h --check-prefix=STABLE + */ + +#ifndef __KABI_EX_H__ +#define __KABI_EX_H__ + +#include "kabi.h" + +/* + * Example: kABI rules + */ + +struct s { + int a; +}; + +KABI_DECLONLY(s); + +/* + * STABLE: variable structure_type s { + * STABLE-NEXT: } + */ + +enum e { + A, + B, + C, + D, +}; + +KABI_ENUMERATOR_IGNORE(e, B); +KABI_ENUMERATOR_IGNORE(e, C); +KABI_ENUMERATOR_VALUE(e, D, 123456789); + +/* + * STABLE: variable enumeration_type e { + * STABLE-NEXT: enumerator A = 0 , + * STABLE-NEXT: enumerator D = 123456789 + * STABLE-NEXT: } byte_size(4) + */ + +#endif /* __KABI_EX_H__ */ diff --git a/scripts/gendwarfksyms/gendwarfksyms.c b/scripts/gendwarfksyms/gendwarfksyms.c index fd2429ea198f..25865d9eab56 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.c +++ b/scripts/gendwarfksyms/gendwarfksyms.c @@ -25,6 +25,8 @@ int dump_die_map; int dump_types; /* Print out expanded type strings used for symbol versions */ int dump_versions; +/* Support kABI stability features */ +int stable; /* Write a symtypes file */ int symtypes; static const char *symtypes_file; @@ -38,6 +40,7 @@ static void usage(void) " --dump-die-map Print debugging information about die_map changes\n" " --dump-types Dump type strings\n" " --dump-versions Dump expanded type strings used for symbol versions\n" + " -s, --stable Support kABI stability features\n" " -T, --symtypes file Write a symtypes file\n" " -h, --help Print this message\n" "\n", @@ -97,17 +100,21 @@ int main(int argc, char **argv) { "dump-die-map", 0, &dump_die_map, 1 }, { "dump-types", 0, &dump_types, 1 }, { "dump-versions", 0, &dump_versions, 1 }, + { "stable", 0, NULL, 's' }, { "symtypes", 1, NULL, 'T' }, { "help", 0, NULL, 'h' }, { 0, 0, NULL, 0 } }; - while ((opt = getopt_long(argc, argv, "dT:h", opts, NULL)) != EOF) { + while ((opt = getopt_long(argc, argv, "dsT:h", opts, NULL)) != EOF) { switch (opt) { case 0: break; case 'd': debug = 1; break; + case 's': + stable = 1; + break; case 'T': symtypes = 1; symtypes_file = optarg; @@ -148,6 +155,7 @@ int main(int argc, char **argv) strerror(errno)); symbol_read_symtab(fd); + kabi_read_rules(fd); dwfl = dwfl_begin(&callbacks); if (!dwfl) @@ -164,6 +172,7 @@ int main(int argc, char **argv) error("dwfl_getmodules failed for '%s'", argv[n]); dwfl_end(dwfl); + kabi_free(); } if (symfile) diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h index 4fd166908cbc..7185d8ffb18e 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -29,6 +29,7 @@ extern int dump_dies; extern int dump_die_map; extern int dump_types; extern int dump_versions; +extern int stable; extern int symtypes; /* @@ -237,6 +238,7 @@ static inline bool cache_was_expanded(struct cache *cache, void *addr) struct expansion_state { bool expand; + const char *current_fqn; }; struct state { @@ -268,4 +270,16 @@ void process_cu(Dwarf_Die *cudie); void generate_symtypes_and_versions(FILE *file); +/* + * kabi.c + */ + +bool kabi_is_enumerator_ignored(const char *fqn, const char *field); +bool kabi_get_enumerator_value(const char *fqn, const char *field, + unsigned long *value); +bool kabi_is_declonly(const char *fqn); + +void kabi_read_rules(int fd); +void kabi_free(void); + #endif /* __GENDWARFKSYMS_H */ diff --git a/scripts/gendwarfksyms/kabi.c b/scripts/gendwarfksyms/kabi.c new file mode 100644 index 000000000000..2c6670ff1ac9 --- /dev/null +++ b/scripts/gendwarfksyms/kabi.c @@ -0,0 +1,333 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Google LLC + */ + +#include +#include "gendwarfksyms.h" + +#define KABI_RULE_SECTION ".discard.gendwarfksyms.kabi_rules" +#define KABI_RULE_VERSION "1" + +/* + * The rule section consists of four null-terminated strings per + * entry: + * + * 1. version + * Entry format version. Must match KABI_RULE_VERSION. + * + * 2. type + * Type of the kABI rule. Must be one of the tags defined below. + * + * 3. target + * Rule-dependent target, typically the fully qualified name of + * the target DIE. + * + * 4. value + * Rule-dependent value. + */ +#define KABI_RULE_MIN_ENTRY_SIZE \ + (/* version\0 */ 2 + /* type\0 */ 2 + /* target\0" */ 1 + \ + /* value\0 */ 1) +#define KABI_RULE_EMPTY_VALUE "" + +/* + * Rule: declonly + * - For the struct/enum/union in the target field, treat it as a + * declaration only even if a definition is available. + */ +#define KABI_RULE_TAG_DECLONLY "declonly" + +/* + * Rule: enumerator_ignore + * - For the enum_field in the target field, ignore the enumerator. + */ +#define KABI_RULE_TAG_ENUMERATOR_IGNORE "enumerator_ignore" + +/* + * Rule: enumerator_value + * - For the fqn_field in the target field, set the value to the + * unsigned integer in the value field. + */ +#define KABI_RULE_TAG_ENUMERATOR_VALUE "enumerator_value" + +enum kabi_rule_type { + KABI_RULE_TYPE_UNKNOWN, + KABI_RULE_TYPE_DECLONLY, + KABI_RULE_TYPE_ENUMERATOR_IGNORE, + KABI_RULE_TYPE_ENUMERATOR_VALUE, +}; + +#define RULE_HASH_BITS 10 + +struct rule { + enum kabi_rule_type type; + const char *target; + const char *value; + struct hlist_node hash; +}; + +/* { type, target } -> struct rule */ +static HASHTABLE_DEFINE(rules, 1 << RULE_HASH_BITS); + +static inline unsigned int rule_values_hash(enum kabi_rule_type type, + const char *target) +{ + return hash_32(type) ^ hash_str(target); +} + +static inline unsigned int rule_hash(const struct rule *rule) +{ + return rule_values_hash(rule->type, rule->target); +} + +static inline const char *get_rule_field(const char **pos, ssize_t *left) +{ + const char *start = *pos; + size_t len; + + if (*left <= 0) + error("unexpected end of kABI rules"); + + len = strnlen(start, *left) + 1; + *pos += len; + *left -= len; + + return start; +} + +void kabi_read_rules(int fd) +{ + GElf_Shdr shdr_mem; + GElf_Shdr *shdr; + Elf_Data *rule_data = NULL; + Elf_Scn *scn; + Elf *elf; + size_t shstrndx; + const char *rule_str; + ssize_t left; + int i; + + const struct { + enum kabi_rule_type type; + const char *tag; + } rule_types[] = { + { + .type = KABI_RULE_TYPE_DECLONLY, + .tag = KABI_RULE_TAG_DECLONLY, + }, + { + .type = KABI_RULE_TYPE_ENUMERATOR_IGNORE, + .tag = KABI_RULE_TAG_ENUMERATOR_IGNORE, + }, + { + .type = KABI_RULE_TYPE_ENUMERATOR_VALUE, + .tag = KABI_RULE_TAG_ENUMERATOR_VALUE, + }, + }; + + if (!stable) + return; + + if (elf_version(EV_CURRENT) != EV_CURRENT) + error("elf_version failed: %s", elf_errmsg(-1)); + + elf = elf_begin(fd, ELF_C_READ_MMAP, NULL); + if (!elf) + error("elf_begin failed: %s", elf_errmsg(-1)); + + if (elf_getshdrstrndx(elf, &shstrndx) < 0) + error("elf_getshdrstrndx failed: %s", elf_errmsg(-1)); + + scn = elf_nextscn(elf, NULL); + + while (scn) { + const char *sname; + + shdr = gelf_getshdr(scn, &shdr_mem); + if (!shdr) + error("gelf_getshdr failed: %s", elf_errmsg(-1)); + + sname = elf_strptr(elf, shstrndx, shdr->sh_name); + if (!sname) + error("elf_strptr failed: %s", elf_errmsg(-1)); + + if (!strcmp(sname, KABI_RULE_SECTION)) { + rule_data = elf_getdata(scn, NULL); + if (!rule_data) + error("elf_getdata failed: %s", elf_errmsg(-1)); + break; + } + + scn = elf_nextscn(elf, scn); + } + + if (!rule_data) { + debug("kABI rules not found"); + check(elf_end(elf)); + return; + } + + rule_str = rule_data->d_buf; + left = shdr->sh_size; + + if (left < KABI_RULE_MIN_ENTRY_SIZE) + error("kABI rule section too small: %zd bytes", left); + + if (rule_str[left - 1] != '\0') + error("kABI rules are not null-terminated"); + + while (left > KABI_RULE_MIN_ENTRY_SIZE) { + enum kabi_rule_type type = KABI_RULE_TYPE_UNKNOWN; + const char *field; + struct rule *rule; + + /* version */ + field = get_rule_field(&rule_str, &left); + + if (strcmp(field, KABI_RULE_VERSION)) + error("unsupported kABI rule version: '%s'", field); + + /* type */ + field = get_rule_field(&rule_str, &left); + + for (i = 0; i < ARRAY_SIZE(rule_types); i++) { + if (!strcmp(field, rule_types[i].tag)) { + type = rule_types[i].type; + break; + } + } + + if (type == KABI_RULE_TYPE_UNKNOWN) + error("unsupported kABI rule type: '%s'", field); + + rule = xmalloc(sizeof(struct rule)); + + rule->type = type; + rule->target = xstrdup(get_rule_field(&rule_str, &left)); + rule->value = xstrdup(get_rule_field(&rule_str, &left)); + + hash_add(rules, &rule->hash, rule_hash(rule)); + + debug("kABI rule: type: '%s', target: '%s', value: '%s'", field, + rule->target, rule->value); + } + + if (left > 0) + warn("unexpected data at the end of the kABI rules section"); + + check(elf_end(elf)); +} + +bool kabi_is_declonly(const char *fqn) +{ + struct rule *rule; + + if (!stable) + return false; + if (!fqn || !*fqn) + return false; + + hash_for_each_possible(rules, rule, hash, + rule_values_hash(KABI_RULE_TYPE_DECLONLY, fqn)) { + if (rule->type == KABI_RULE_TYPE_DECLONLY && + !strcmp(fqn, rule->target)) + return true; + } + + return false; +} + +static char *get_enumerator_target(const char *fqn, const char *field) +{ + char *target = NULL; + + if (asprintf(&target, "%s %s", fqn, field) < 0) + error("asprintf failed for '%s %s'", fqn, field); + + return target; +} + +static unsigned long get_ulong_value(const char *value) +{ + unsigned long result = 0; + char *endptr = NULL; + + errno = 0; + result = strtoul(value, &endptr, 10); + + if (errno || *endptr) + error("invalid unsigned value '%s'", value); + + return result; +} + +bool kabi_is_enumerator_ignored(const char *fqn, const char *field) +{ + bool match = false; + struct rule *rule; + char *target; + + if (!stable) + return false; + if (!fqn || !*fqn || !field || !*field) + return false; + + target = get_enumerator_target(fqn, field); + + hash_for_each_possible( + rules, rule, hash, + rule_values_hash(KABI_RULE_TYPE_ENUMERATOR_IGNORE, target)) { + if (rule->type == KABI_RULE_TYPE_ENUMERATOR_IGNORE && + !strcmp(target, rule->target)) { + match = true; + break; + } + } + + free(target); + return match; +} + +bool kabi_get_enumerator_value(const char *fqn, const char *field, + unsigned long *value) +{ + bool match = false; + struct rule *rule; + char *target; + + if (!stable) + return false; + if (!fqn || !*fqn || !field || !*field) + return false; + + target = get_enumerator_target(fqn, field); + + hash_for_each_possible(rules, rule, hash, + rule_values_hash(KABI_RULE_TYPE_ENUMERATOR_VALUE, + target)) { + if (rule->type == KABI_RULE_TYPE_ENUMERATOR_VALUE && + !strcmp(target, rule->target)) { + *value = get_ulong_value(rule->value); + match = true; + break; + } + } + + free(target); + return match; +} + +void kabi_free(void) +{ + struct hlist_node *tmp; + struct rule *rule; + + hash_for_each_safe(rules, rule, tmp, hash) { + free((void *)rule->target); + free((void *)rule->value); + free(rule); + } + + hash_init(rules); +} From patchwork Thu Nov 21 20:42:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13882353 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 495F21DF27E for ; Thu, 21 Nov 2024 20:43:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221785; cv=none; b=FqmAJwBnUL16Gq0UUnSdFJ0rVwmVnYrzHVo7khVN+PgwNy8kHfJsoR2h1DAnpw0e4rclcjzr12Jt7FAwU77jz6w4YItjepmr4IF3qPnYF9Dvl6J46cjVAjlS6FoZHhlfc+rB0sHL3SrTXofnDxncUPyWe00riMcKFY6rRckB1Jc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221785; c=relaxed/simple; bh=FVlQiHUbBFyuMGSOxTxhhsP7LYDK1cTgwW8tGYBcRKg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pHisRaYPeqZXVvu0E6nA2OelJxwglRRMvBszMDpI2LJh6BQ/XoD3yoyFHLY0G14HNSnZVjrOvG4JYLcwJngAL/Ae223/idX9SnqADVAaNkHnmTGdTWg/cv00IKk+5d4loOKYD3XZQqR3auiIFcUo5JJE+YRyGVshq7N5duF2P8I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=UYw4ZPDy; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="UYw4ZPDy" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e381c19246fso2338580276.3 for ; Thu, 21 Nov 2024 12:43:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732221780; x=1732826580; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=rJaJGkeMdABjwuKeOzQhmMOjSV1XWy7sgNfEDvwgqM8=; b=UYw4ZPDyvD5yGO0B559ihYkvYI25swUPHLa0LX8mi8O/urNhy+LcbbWdG18SNH5uiv 99vRdB7hw1o0jPYRUMPWSCCqOfCGUmDy2Qz1pqkYSI/fr1E1MoUxlE37vDjwWipFF+6W OSCrfrP0VKg+yeeJmyxnWJxBjsnnzWtXjpDXGTTxLLedj1MZir/nej98GpD3YG8jeNSy gSJyJw21G/k5+e7NyRpxbDoVsr+UzTE8FLn7MfDYRF9wK0QsMZt2LS4CaA53nx+Pwnby n8zYRSCwb6Jnsvd2eAoYSmBXByAlI2S1j6EvIwmLNZAIDOhX3oYncIdUFaY60J1ZzdZ+ 2MIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732221780; x=1732826580; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=rJaJGkeMdABjwuKeOzQhmMOjSV1XWy7sgNfEDvwgqM8=; b=iiP7dyAW2AyCOct+gytly04wkmDACiPj7z/PnQJxuX0iHAOqcF7ITHGD55ifSZKBTx Sn1jmSDV5x5KM5gVsPZB2qUajtFFiP6Sl44SNbpPMJSUfRtAaO/ueYpWgANPbzV+PRBS X0ZkOWw8YB70oBQgEd/SyrZG1WGLQWeGiJdfTOHB2MaTB1j9E6eLFs8ZtT/VKqNR5tU1 iGsLq7ssa0v3yPzhIlnh6dplbxdgN+pGqeBM6NhIGjhVGq0CusnGTr9kk6qvp3+6OUFM 8Ced71FFBP2c6s/1xuTLL+tdSJcl0owLO4A5dO5BA4yEtkt/MsDugMYdvLFeKMKaef/g pzFw== X-Forwarded-Encrypted: i=1; AJvYcCXIeytWB2e+1SM/RN+hH8hrH9dzZKxx5wVc9RcBYGB7NCUFXlgFAsIo5BiAqIM72mAkjm9liKdkyF0qVjIZ@vger.kernel.org X-Gm-Message-State: AOJu0Yx3Ne/Tke/Xoc4y5TapORuJU3A1wyM9bL8UtBVM3ZhcVrA9gaRZ JCYQEmsQGvNGwh4SYkwW3XHX5pFRNv/Pd2jXe7HXA9vJ0b6pYd4AJWF81POeZXD+QBoU9v/scgF nf/ShDIF/sKYI5RSMeAsQzRP1Ig== X-Google-Smtp-Source: AGHT+IFnbYLBVuzU3ihe5ilQNw2joANYeugd/YNo64eIhN9YVipykDtcy6kQLBpHiGdARI8kCadwqtJwgl9oBn7J3Rw= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a05:6902:c7:b0:e28:f2a5:f1d with SMTP id 3f1490d57ef6-e38f8b13cd6mr153276.4.1732221780048; Thu, 21 Nov 2024 12:43:00 -0800 (PST) Date: Thu, 21 Nov 2024 20:42:35 +0000 In-Reply-To: <20241121204220.2378181-20-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121204220.2378181-20-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=22866; i=samitolvanen@google.com; h=from:subject; bh=FVlQiHUbBFyuMGSOxTxhhsP7LYDK1cTgwW8tGYBcRKg=; b=owGbwMvMwCEWxa662nLh8irG02pJDOn2s00S1U86PDy7rPlG8PdZEyPj77yf67RSWLKn7aBu8 TJOx3DfjlIWBjEOBlkxRZaWr6u37v7ulPrqc5EEzBxWJpAhDFycAjCRDeqMDNvqn732KXgSrLxP X/X4PlPr3o7DAtxZyjvKzj+SXKrRzM/I8G6GhH6KWalHw6otPq4PTSccOpO5LTwuOsvz6if7qZ3 MvAA= X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121204220.2378181-34-samitolvanen@google.com> Subject: [PATCH v6 14/18] gendwarfksyms: Add support for reserved and ignored fields From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Distributions that want to maintain a stable kABI need the ability to make ABI compatible changes to kernel data structures without affecting symbol versions, either because of LTS updates or backports. With genksyms, developers would typically hide these changes from version calculation with #ifndef __GENKSYMS__, which would result in the symbol version not changing even though the actual type has changed. When we process precompiled object files, this isn't an option. Change union processing to recognize field name prefixes that allow the user to ignore the union completely during symbol versioning with a __kabi_ignored prefix in a field name, or to replace the type of a placeholder field using a __kabi_reserved field name prefix. For example, assume we want to add a new field to an existing alignment hole in a data structure, and ignore the new field when calculating symbol versions: struct struct1 { int a; /* a 4-byte alignment hole */ unsigned long b; }; To add `int n` to the alignment hole, we can add a union that includes a __kabi_ignored field that causes gendwarfksyms to ignore the entire union: struct struct1 { int a; union { char __kabi_ignored_0; int n; }; unsigned long b; }; With --stable, both structs produce the same symbol version. Alternatively, when a distribution expects future modification to a data structure, they can explicitly add reserved fields: struct struct2 { long a; long __kabi_reserved_0; /* reserved for future use */ }; To take the field into use, we can again replace it with a union, with one of the fields keeping the __kabi_reserved name prefix to indicate the original type: struct struct2 { long a; union { long __kabi_reserved_0; struct { int b; int v; }; }; Here gendwarfksyms --stable replaces the union with the type of the placeholder field when calculating versions. Signed-off-by: Sami Tolvanen Reviewed-by: Petr Pavlu --- scripts/gendwarfksyms/dwarf.c | 248 ++++++++++++++++++++++- scripts/gendwarfksyms/examples/kabi.h | 87 ++++++++ scripts/gendwarfksyms/examples/kabi_ex.c | 16 ++ scripts/gendwarfksyms/examples/kabi_ex.h | 199 ++++++++++++++++++ scripts/gendwarfksyms/gendwarfksyms.h | 9 + 5 files changed, 558 insertions(+), 1 deletion(-) diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index 04b38caa846f..2c96caf8d6e2 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -3,9 +3,32 @@ * Copyright (C) 2024 Google LLC */ +#include #include #include "gendwarfksyms.h" +/* See get_union_kabi_status */ +#define KABI_PREFIX "__kabi_" +#define KABI_PREFIX_LEN (sizeof(KABI_PREFIX) - 1) +#define KABI_RESERVED_PREFIX "reserved" +#define KABI_RESERVED_PREFIX_LEN (sizeof(KABI_RESERVED_PREFIX) - 1) +#define KABI_RENAMED_PREFIX "renamed" +#define KABI_RENAMED_PREFIX_LEN (sizeof(KABI_RENAMED_PREFIX) - 1) +#define KABI_IGNORED_PREFIX "ignored" +#define KABI_IGNORED_PREFIX_LEN (sizeof(KABI_IGNORED_PREFIX) - 1) + +static inline bool is_kabi_prefix(const char *name) +{ + return name && !strncmp(name, KABI_PREFIX, KABI_PREFIX_LEN); +} + +enum kabi_status { + /* >0 to stop DIE processing */ + KABI_NORMAL = 1, + KABI_RESERVED, + KABI_IGNORED, +}; + static bool do_linebreak; static int indentation_level; @@ -352,13 +375,23 @@ static void __process_list_type(struct state *state, struct die *cache, { const char *name = get_name_attr(die); + if (stable) { + if (is_kabi_prefix(name)) + name = NULL; + state->kabi.orig_name = NULL; + } + process_list_comma(state, cache); process(cache, type); process_type_attr(state, cache, die); + + if (stable && state->kabi.orig_name) + name = state->kabi.orig_name; if (name) { process(cache, " "); process(cache, name); } + process_accessibility_attr(cache, die); process_bit_size_attr(cache, die); process_data_bit_offset_attr(cache, die); @@ -485,11 +518,208 @@ static void process_variant_part_type(struct state *state, struct die *cache, process(cache, "}"); } +static int get_kabi_status(Dwarf_Die *die, const char **suffix) +{ + const char *name = get_name_attr(die); + + if (suffix) + *suffix = NULL; + + if (is_kabi_prefix(name)) { + name += KABI_PREFIX_LEN; + + if (!strncmp(name, KABI_RESERVED_PREFIX, + KABI_RESERVED_PREFIX_LEN)) + return KABI_RESERVED; + if (!strncmp(name, KABI_IGNORED_PREFIX, + KABI_IGNORED_PREFIX_LEN)) + return KABI_IGNORED; + + if (!strncmp(name, KABI_RENAMED_PREFIX, + KABI_RENAMED_PREFIX_LEN)) { + if (suffix) { + name += KABI_RENAMED_PREFIX_LEN; + *suffix = name; + } + return KABI_RESERVED; + } + } + + return KABI_NORMAL; +} + +static int check_struct_member_kabi_status(struct state *state, + struct die *__unused, Dwarf_Die *die) +{ + int res; + + assert(dwarf_tag(die) == DW_TAG_member_type); + + /* + * If the union member is a struct, expect the __kabi field to + * be the first member of the structure, i.e..: + * + * union { + * type new_member; + * struct { + * type __kabi_field; + * } + * }; + */ + res = get_kabi_status(die, &state->kabi.orig_name); + + if (res == KABI_RESERVED && + !get_ref_die_attr(die, DW_AT_type, &state->kabi.placeholder)) + error("structure member missing a type?"); + + return res; +} + +static int check_union_member_kabi_status(struct state *state, + struct die *__unused, Dwarf_Die *die) +{ + Dwarf_Die type; + int res; + + assert(dwarf_tag(die) == DW_TAG_member_type); + + if (!get_ref_die_attr(die, DW_AT_type, &type)) + error("union member missing a type?"); + + /* + * We expect a union with two members. Check if either of them + * has a __kabi name prefix, i.e.: + * + * union { + * ... + * type memberN; // <- type, N = {0,1} + * ... + * }; + * + * The member can also be a structure type, in which case we'll + * check the first structure member. + * + * In any case, stop processing after we've seen two members. + */ + res = get_kabi_status(die, &state->kabi.orig_name); + + if (res == KABI_RESERVED) + state->kabi.placeholder = type; + if (res != KABI_NORMAL) + return res; + + if (dwarf_tag(&type) == DW_TAG_structure_type) + res = checkp(process_die_container( + state, NULL, &type, check_struct_member_kabi_status, + match_member_type)); + + if (res <= KABI_NORMAL && ++state->kabi.members < 2) + return 0; /* Continue */ + + return res; +} + +static int get_union_kabi_status(Dwarf_Die *die, Dwarf_Die *placeholder, + const char **orig_name) +{ + struct state state; + int res; + + if (!stable) + return KABI_NORMAL; + + /* + * To maintain a stable kABI, distributions may choose to reserve + * space in structs for later use by adding placeholder members, + * for example: + * + * struct s { + * u32 a; + * // an 8-byte placeholder for future use + * u64 __kabi_reserved_0; + * }; + * + * When the reserved member is taken into use, the type change + * would normally cause the symbol version to change as well, but + * if the replacement uses the following convention, gendwarfksyms + * continues to use the placeholder type for versioning instead, + * thus maintaining the same symbol version: + * + * struct s { + * u32 a; + * union { + * // placeholder replaced with a new member `b` + * struct t b; + * struct { + * // the placeholder type that is still + * // used for versioning + * u64 __kabi_reserved_0; + * }; + * }; + * }; + * + * I.e., as long as the replaced member is in a union, and the + * placeholder has a __kabi_reserved name prefix, we'll continue + * to use the placeholder type (here u64) for version calculation + * instead of the union type. + * + * It's also possible to ignore new members from versioning if + * they've been added to alignment holes, for example, by + * including them in a union with another member that uses the + * __kabi_ignored name prefix: + * + * struct s { + * u32 a; + * // an alignment hole is used to add `n` + * union { + * u32 n; + * // hide the entire union member from versioning + * u8 __kabi_ignored_0; + * }; + * u64 b; + * }; + * + * Note that the user of this feature is responsible for ensuring + * that the structure actually remains ABI compatible. + */ + memset(&state.kabi, 0, sizeof(struct kabi_state)); + + res = checkp(process_die_container(&state, NULL, die, + check_union_member_kabi_status, + match_member_type)); + + if (res == KABI_RESERVED) { + if (placeholder) + *placeholder = state.kabi.placeholder; + if (orig_name) + *orig_name = state.kabi.orig_name; + } + + return res; +} + +static bool is_kabi_ignored(Dwarf_Die *die) +{ + Dwarf_Die type; + + if (!stable) + return false; + + if (!get_ref_die_attr(die, DW_AT_type, &type)) + error("member missing a type?"); + + return dwarf_tag(&type) == DW_TAG_union_type && + checkp(get_union_kabi_status(&type, NULL, NULL)) == KABI_IGNORED; +} + static int ___process_structure_type(struct state *state, struct die *cache, Dwarf_Die *die) { switch (dwarf_tag(die)) { case DW_TAG_member: + if (is_kabi_ignored(die)) + return 0; + return check(process_type(state, cache, die)); case DW_TAG_variant_part: return check(process_type(state, cache, die)); case DW_TAG_class_type: @@ -546,7 +776,23 @@ static void __process_structure_type(struct state *state, struct die *cache, DEFINE_PROCESS_STRUCTURE_TYPE(class) DEFINE_PROCESS_STRUCTURE_TYPE(structure) -DEFINE_PROCESS_STRUCTURE_TYPE(union) + +static void process_union_type(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + Dwarf_Die placeholder; + + int res = checkp(get_union_kabi_status(die, &placeholder, + &state->kabi.orig_name)); + + if (res == KABI_RESERVED) + check(process_type(state, cache, &placeholder)); + if (res > KABI_NORMAL) + return; + + __process_structure_type(state, cache, die, "union_type", + ___process_structure_type, match_all); +} static void process_enumerator_type(struct state *state, struct die *cache, Dwarf_Die *die) diff --git a/scripts/gendwarfksyms/examples/kabi.h b/scripts/gendwarfksyms/examples/kabi.h index fcd0300e5b58..97a5669b083d 100644 --- a/scripts/gendwarfksyms/examples/kabi.h +++ b/scripts/gendwarfksyms/examples/kabi.h @@ -43,6 +43,28 @@ __section(".discard.gendwarfksyms.kabi_rules") = \ "1\0" #hint "\0" #target "\0" #value +#define __KABI_NORMAL_SIZE_ALIGN(_orig, _new) \ + union { \ + _Static_assert( \ + sizeof(struct { _new; }) <= sizeof(struct { _orig; }), \ + __FILE__ ":" __stringify(__LINE__) ": " __stringify( \ + _new) " is larger than " __stringify(_orig)); \ + _Static_assert( \ + __alignof__(struct { _new; }) <= \ + __alignof__(struct { _orig; }), \ + __FILE__ ":" __stringify(__LINE__) ": " __stringify( \ + _orig) " is not aligned the same as " __stringify(_new)); \ + } + +#define __KABI_REPLACE(_orig, _new) \ + union { \ + _new; \ + struct { \ + _orig; \ + }; \ + __KABI_NORMAL_SIZE_ALIGN(_orig, _new); \ + } + /* * KABI_DECLONLY(fqn) * Treat the struct/union/enum fqn as a declaration, i.e. even if @@ -67,4 +89,69 @@ #define KABI_ENUMERATOR_VALUE(fqn, field, value) \ __KABI_RULE(enumerator_value, fqn field, value) +/* + * KABI_RESERVE + * Reserve some "padding" in a structure for use by LTS backports. + * This is normally placed at the end of a structure. + * number: the "number" of the padding variable in the structure. Start with + * 1 and go up. + */ +#define KABI_RESERVE(n) unsigned long __kabi_reserved##n + +/* + * KABI_RESERVE_ARRAY + * Same as _BACKPORT_RESERVE but allocates an array with the specified + * size in bytes. + */ +#define KABI_RESERVE_ARRAY(n, s) \ + unsigned char __aligned(8) __kabi_reserved##n[s] + +/* + * KABI_IGNORE + * Add a new field that's ignored in versioning. + */ +#define KABI_IGNORE(n, _new) \ + union { \ + _new; \ + unsigned char __kabi_ignored##n; \ + } + +/* + * KABI_REPLACE + * Replace a field with a compatible new field. + */ +#define KABI_REPLACE(_oldtype, _oldname, _new) \ + __KABI_REPLACE(_oldtype __kabi_renamed##_oldname, struct { _new; }) + +/* + * KABI_USE(number, _new) + * Use a previous padding entry that was defined with KABI_RESERVE + * number: the previous "number" of the padding variable + * _new: the variable to use now instead of the padding variable + */ +#define KABI_USE(number, _new) __KABI_REPLACE(KABI_RESERVE(number), _new) + +/* + * KABI_USE2(number, _new1, _new2) + * Use a previous padding entry that was defined with KABI_RESERVE for + * two new variables that fit into 64 bits. This is good for when you do not + * want to "burn" a 64bit padding variable for a smaller variable size if not + * needed. + */ +#define KABI_USE2(number, _new1, _new2) \ + __KABI_REPLACE( \ + KABI_RESERVE(number), struct { \ + _new1; \ + _new2; \ + }) +/* + * KABI_USE_ARRAY(number, bytes, _new) + * Use a previous padding entry that was defined with KABI_RESERVE_ARRAY + * number: the previous "number" of the padding variable + * bytes: the size in bytes reserved for the array + * _new: the variable to use now instead of the padding variable + */ +#define KABI_USE_ARRAY(number, bytes, _new) \ + __KABI_REPLACE(KABI_RESERVE_ARRAY(number, bytes), _new) + #endif /* __KABI_H__ */ diff --git a/scripts/gendwarfksyms/examples/kabi_ex.c b/scripts/gendwarfksyms/examples/kabi_ex.c index 799552ea6679..0b7ffd830541 100644 --- a/scripts/gendwarfksyms/examples/kabi_ex.c +++ b/scripts/gendwarfksyms/examples/kabi_ex.c @@ -12,3 +12,19 @@ struct s e0; enum e e1; + +struct ex0a ex0a; +struct ex0b ex0b; +struct ex0c ex0c; + +struct ex1a ex1a; +struct ex1b ex1b; +struct ex1c ex1c; + +struct ex2a ex2a; +struct ex2b ex2b; +struct ex2c ex2c; + +struct ex3a ex3a; +struct ex3b ex3b; +struct ex3c ex3c; diff --git a/scripts/gendwarfksyms/examples/kabi_ex.h b/scripts/gendwarfksyms/examples/kabi_ex.h index fca1e07c78e2..1736e0f65208 100644 --- a/scripts/gendwarfksyms/examples/kabi_ex.h +++ b/scripts/gendwarfksyms/examples/kabi_ex.h @@ -59,6 +59,205 @@ KABI_ENUMERATOR_VALUE(e, D, 123456789); * STABLE-NEXT: enumerator A = 0 , * STABLE-NEXT: enumerator D = 123456789 * STABLE-NEXT: } byte_size(4) +*/ + +/* + * Example: Reserved fields + */ +struct ex0a { + int a; + KABI_RESERVE(0); + KABI_RESERVE(1); +}; + +/* + * STABLE: variable structure_type ex0a { + * STABLE-NEXT: member base_type int byte_size(4) encoding(5) a data_member_location(0) , + * STABLE-NEXT: member base_type [[ULONG:long unsigned int|unsigned long]] byte_size(8) encoding(7) data_member_location(8) , + * STABLE-NEXT: member base_type [[ULONG]] byte_size(8) encoding(7) data_member_location(16) + * STABLE-NEXT: } byte_size(24) + */ + +struct ex0b { + int a; + KABI_RESERVE(0); + KABI_USE2(1, int b, int c); +}; + +/* + * STABLE: variable structure_type ex0b { + * STABLE-NEXT: member base_type int byte_size(4) encoding(5) a data_member_location(0) , + * STABLE-NEXT: member base_type [[ULONG]] byte_size(8) encoding(7) data_member_location(8) , + * STABLE-NEXT: member base_type [[ULONG]] byte_size(8) encoding(7) data_member_location(16) + * STABLE-NEXT: } byte_size(24) + */ + +struct ex0c { + int a; + KABI_USE(0, void *p); + KABI_USE2(1, int b, int c); +}; + +/* + * STABLE: variable structure_type ex0c { + * STABLE-NEXT: member base_type int byte_size(4) encoding(5) a data_member_location(0) , + * STABLE-NEXT: member base_type [[ULONG]] byte_size(8) encoding(7) data_member_location(8) , + * STABLE-NEXT: member base_type [[ULONG]] byte_size(8) encoding(7) data_member_location(16) + * STABLE-NEXT: } byte_size(24) + */ + +/* + * Example: A reserved array + */ + +struct ex1a { + unsigned int a; + KABI_RESERVE_ARRAY(0, 64); +}; + +/* + * STABLE: variable structure_type ex1a { + * STABLE-NEXT: member base_type unsigned int byte_size(4) encoding(7) a data_member_location(0) , + * STABLE-NEXT: member array_type[64] { + * STABLE-NEXT: base_type unsigned char byte_size(1) encoding(8) + * STABLE-NEXT: } data_member_location(8) + * STABLE-NEXT: } byte_size(72) + */ + +struct ex1b { + unsigned int a; + KABI_USE_ARRAY( + 0, 64, struct { + void *p; + KABI_RESERVE_ARRAY(1, 56); + }); +}; + +/* + * STABLE: variable structure_type ex1b { + * STABLE-NEXT: member base_type unsigned int byte_size(4) encoding(7) a data_member_location(0) , + * STABLE-NEXT: member array_type[64] { + * STABLE-NEXT: base_type unsigned char byte_size(1) encoding(8) + * STABLE-NEXT: } data_member_location(8) + * STABLE-NEXT: } byte_size(72) + */ + +struct ex1c { + unsigned int a; + KABI_USE_ARRAY(0, 64, void *p[8]); +}; + +/* + * STABLE: variable structure_type ex1c { + * STABLE-NEXT: member base_type unsigned int byte_size(4) encoding(7) a data_member_location(0) , + * STABLE-NEXT: member array_type[64] { + * STABLE-NEXT: base_type unsigned char byte_size(1) encoding(8) + * STABLE-NEXT: } data_member_location(8) + * STABLE-NEXT: } byte_size(72) + */ + +/* + * Example: An ignored field added to an alignment hole + */ + +struct ex2a { + int a; + unsigned long b; + int c; + unsigned long d; +}; + +/* + * STABLE: variable structure_type ex2a { + * STABLE-NEXT: member base_type int byte_size(4) encoding(5) a data_member_location(0) , + * STABLE-NEXT: member base_type [[ULONG:long unsigned int|unsigned long]] byte_size(8) encoding(7) b data_member_location(8) + * STABLE-NEXT: member base_type int byte_size(4) encoding(5) c data_member_location(16) , + * STABLE-NEXT: member base_type [[ULONG]] byte_size(8) encoding(7) d data_member_location(24) + * STABLE-NEXT: } byte_size(32) + */ + +struct ex2b { + int a; + KABI_IGNORE(0, unsigned int n); + unsigned long b; + int c; + unsigned long d; +}; + +_Static_assert(sizeof(struct ex2a) == sizeof(struct ex2b), "ex2a size doesn't match ex2b"); + +/* + * STABLE: variable structure_type ex2b { + * STABLE-NEXT: member base_type int byte_size(4) encoding(5) a data_member_location(0) , + * STABLE-NEXT: member base_type [[ULONG]] byte_size(8) encoding(7) b data_member_location(8) + * STABLE-NEXT: member base_type int byte_size(4) encoding(5) c data_member_location(16) , + * STABLE-NEXT: member base_type [[ULONG]] byte_size(8) encoding(7) d data_member_location(24) + * STABLE-NEXT: } byte_size(32) + */ + +struct ex2c { + int a; + KABI_IGNORE(0, unsigned int n); + unsigned long b; + int c; + KABI_IGNORE(1, unsigned int m); + unsigned long d; +}; + +_Static_assert(sizeof(struct ex2a) == sizeof(struct ex2c), "ex2a size doesn't match ex2c"); + +/* + * STABLE: variable structure_type ex2c { + * STABLE-NEXT: member base_type int byte_size(4) encoding(5) a data_member_location(0) , + * STABLE-NEXT: member base_type [[ULONG]] byte_size(8) encoding(7) b data_member_location(8) + * STABLE-NEXT: member base_type int byte_size(4) encoding(5) c data_member_location(16) , + * STABLE-NEXT: member base_type [[ULONG]] byte_size(8) encoding(7) d data_member_location(24) + * STABLE-NEXT: } byte_size(32) + */ + + +/* + * Example: A replaced field + */ + +struct ex3a { + unsigned long a; + unsigned long unused; +}; + +/* + * STABLE: variable structure_type ex3a { + * STABLE-NEXT: member base_type [[ULONG:long unsigned int|unsigned long]] byte_size(8) encoding(7) a data_member_location(0) + * STABLE-NEXT: member base_type [[ULONG]] byte_size(8) encoding(7) unused data_member_location(8) + * STABLE-NEXT: } byte_size(16) + */ + +struct ex3b { + unsigned long a; + KABI_REPLACE(unsigned long, unused, unsigned long renamed); +}; + +_Static_assert(sizeof(struct ex3a) == sizeof(struct ex3b), "ex3a size doesn't match ex3b"); + +/* + * STABLE: variable structure_type ex3b { + * STABLE-NEXT: member base_type [[ULONG]] byte_size(8) encoding(7) a data_member_location(0) + * STABLE-NEXT: member base_type [[ULONG]] byte_size(8) encoding(7) unused data_member_location(8) + * STABLE-NEXT: } byte_size(16) + */ + +struct ex3c { + unsigned long a; + KABI_REPLACE(unsigned long, unused, long replaced); +}; + +_Static_assert(sizeof(struct ex3a) == sizeof(struct ex3c), "ex3a size doesn't match ex3c"); + +/* + * STABLE: variable structure_type ex3c { + * STABLE-NEXT: member base_type [[ULONG]] byte_size(8) encoding(7) a data_member_location(0) + * STABLE-NEXT: member base_type [[ULONG]] byte_size(8) encoding(7) unused data_member_location(8) + * STABLE-NEXT: } byte_size(16) */ #endif /* __KABI_EX_H__ */ diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h index 7185d8ffb18e..5fd743108b6f 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -241,6 +241,12 @@ struct expansion_state { const char *current_fqn; }; +struct kabi_state { + int members; + Dwarf_Die placeholder; + const char *orig_name; +}; + struct state { struct symbol *sym; Dwarf_Die die; @@ -251,6 +257,9 @@ struct state { /* Structure expansion */ struct expansion_state expand; struct cache expansion_cache; + + /* Reserved or ignored members */ + struct kabi_state kabi; }; typedef int (*die_callback_t)(struct state *state, struct die *cache, From patchwork Thu Nov 21 20:42:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13882352 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7D1441DF744 for ; Thu, 21 Nov 2024 20:43:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221784; cv=none; b=MmDuJEv++FOGU7idIIXGoy67RYsof63Ct3FmwSFG5yQ/1M/bNWWq0pPMsyZz8Yb06KxC0xx6iZsYLdUQtpZWunlNjbC+RBxNoK+IpRyK6el+UbP90ZoaFYtwmNF9R8JOsysiNmQ4t0g0fTc6j9/hpjnS61HPRQPjSgEZof3/wkc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221784; c=relaxed/simple; bh=/hdGw2AMYmpvv/2VNqR8g2SLZ1yvF33svcNLehCNsFo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=G/CHhiUozYz7RVgEl1jd9GxoTeL6hCVPFmnZSWrropt7Oe/CVi0Nv0MQfpgK6b1hLNplzFIsv6NUybpuSJfHhW/QSENmCX2m1KM4k4BvWPUQKqgTX/X0Idkj4Y+xFrOfFRPe873aEZbNgBJDlYJC1DCondEWeyRb7bWAvZNNW8g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=G3+zDeRD; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="G3+zDeRD" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ea3f3121c3so1335961a91.1 for ; Thu, 21 Nov 2024 12:43:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732221782; x=1732826582; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=iGEg1qXK77lm4N1fnb9Y00A/EET9bECvkjvbo/F/zjw=; b=G3+zDeRD7vTFBZ1pzkX4aoalQKj6VD0pcjOVinlQsAPON7rEAPq16p3PqbCUcL9Tjg MWByM0uVF5M2dKrAr54juBQUYthj3+hiRDtEaDrfoevZXqnBrKhEhrP9PsH4sSYHxhJn nG2pGR3oY64WoXTaVPGtSwILF4cC+Db1Dft2oWFiSf68EnzGGD22RGSDoPYQoj9PP39f 2ZktpQaqCv5Wx5TN7+1wuxRF6VDkuiRK39sVI0az27sJ7rwAApQf8A9qQfubKHvqa9TB pQGKYLUHzwnhT+9Bn2hQdLVhk1H8MH7/UjBWdFKBeJ6JiaFDx6hYjn02aNxw/NdxPvI9 E/8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732221782; x=1732826582; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=iGEg1qXK77lm4N1fnb9Y00A/EET9bECvkjvbo/F/zjw=; b=YVNiiLvov7UsqKlUUYFR54JvSQBdn28SdQHRBNllIaQJ8CwoF2ylG2PwTTI4Tbpc5A 4SnhlrPpYE0fpLpGd/AWRvH05LSgMuK1ak9tZEIKNq8ki3wds8o0t03py5S9gSXSxZ0S M0c9cx64m/T6Mzmyfqf2PjANmD5D+Kkv2d0whMRYdnGS6XSK3iLLHO42Yw9epzTvVezW 6xY098LcptOz4EWx/fcc7th06zsF0XCzUXmLaJWsw12xE1SRPQVyUCsgXpCRtbaRT0tr zfg75T8NTl48HAaFYXVT3SWj8EtU8ogqspkyFbukMNS0EYTDuBV2s4AAMjaOrk/uEXyH yEEg== X-Forwarded-Encrypted: i=1; AJvYcCW7cHLoIKODT4CulbLifguNy8fPlCPTdtEf/0jeGyhPaSmVe/CIkZtFNRpPh2pUyrIasRSbaTJ7L3IvWRhI@vger.kernel.org X-Gm-Message-State: AOJu0Yyecl3JJ7gVfc+GSZTrFtFG8gz8KBr9eKDkG6kqCYQpV7Qy+vYO aIXqRb1LuA7BPSBGb4n3W+lr6YlR1sdtN+VD8Lg/aRAaAv5PiKyCExCQXBk8xUsWufPjENx+m9U ue2fwH1vGMbHHbw2ICYZhKv1JoA== X-Google-Smtp-Source: AGHT+IHfwjcoY8jkfISWJK/O1nY0fH9JfL5LYeGGFHC4bx6+x3AmKF8Bm+QLBomODF7fBL2XX9uILvlwCN85kw1XO2g= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a17:90b:2784:b0:2e2:cc47:f1b7 with SMTP id 98e67ed59e1d1-2eb0e0169dcmr122a91.1.1732221781805; Thu, 21 Nov 2024 12:43:01 -0800 (PST) Date: Thu, 21 Nov 2024 20:42:36 +0000 In-Reply-To: <20241121204220.2378181-20-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121204220.2378181-20-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=7893; i=samitolvanen@google.com; h=from:subject; bh=/hdGw2AMYmpvv/2VNqR8g2SLZ1yvF33svcNLehCNsFo=; b=owGbwMvMwCEWxa662nLh8irG02pJDOn2s03EM776qTVYfl7fUBawp22/UL8Ev/fBQ0uF3087U CwjVbGlo5SFQYyDQVZMkaXl6+qtu787pb76XCQBM4eVCWQIAxenAEwkzZGRYfbHhOXKl7XtJ875 c0hk4hzDxn790p2+DqpLJjOt87tbu5+RYdYDhnlS2WWbVtR8+5RevbTwgwJzQ8ux3+uZV6toWuz 2YgUA X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121204220.2378181-35-samitolvanen@google.com> Subject: [PATCH v6 15/18] gendwarfksyms: Add support for symbol type pointers From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen The compiler may choose not to emit type information in DWARF for external symbols. Clang, for example, does this for symbols not defined in the current TU. To provide a way to work around this issue, add support for __gendwarfksyms_ptr_ pointers that force the compiler to emit the necessary type information in DWARF also for the missing symbols. Example usage: #define GENDWARFKSYMS_PTR(sym) \ static typeof(sym) *__gendwarfksyms_ptr_##sym __used \ __section(".discard.gendwarfksyms") = &sym; extern int external_symbol(void); GENDWARFKSYMS_PTR(external_symbol); Signed-off-by: Sami Tolvanen Reviewed-by: Petr Pavlu --- scripts/gendwarfksyms/dwarf.c | 55 +++++++++++++++++++++- scripts/gendwarfksyms/examples/symbolptr.c | 33 +++++++++++++ scripts/gendwarfksyms/gendwarfksyms.h | 7 +++ scripts/gendwarfksyms/symbols.c | 27 +++++++++++ 4 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 scripts/gendwarfksyms/examples/symbolptr.c diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index 2c96caf8d6e2..38842903f84b 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -1060,6 +1060,31 @@ static void process_variable(struct state *state, Dwarf_Die *die) process_symbol(state, die, __process_variable); } +static void save_symbol_ptr(struct state *state) +{ + Dwarf_Die ptr_type; + Dwarf_Die type; + + if (!get_ref_die_attr(&state->die, DW_AT_type, &ptr_type) || + dwarf_tag(&ptr_type) != DW_TAG_pointer_type) + error("%s must be a pointer type!", + get_symbol_name(&state->die)); + + if (!get_ref_die_attr(&ptr_type, DW_AT_type, &type)) + error("%s pointer missing a type attribute?", + get_symbol_name(&state->die)); + + /* + * Save the symbol pointer DIE in case the actual symbol is + * missing from the DWARF. Clang, for example, intentionally + * omits external symbols from the debugging information. + */ + if (dwarf_tag(&type) == DW_TAG_subroutine_type) + symbol_set_ptr(state->sym, &type); + else + symbol_set_ptr(state->sym, &ptr_type); +} + static int process_exported_symbols(struct state *unused, struct die *cache, Dwarf_Die *die) { @@ -1083,7 +1108,9 @@ static int process_exported_symbols(struct state *unused, struct die *cache, state_init(&state); - if (tag == DW_TAG_subprogram) + if (is_symbol_ptr(get_symbol_name(&state.die))) + save_symbol_ptr(&state); + else if (tag == DW_TAG_subprogram) process_subprogram(&state, &state.die); else process_variable(&state, &state.die); @@ -1096,10 +1123,36 @@ static int process_exported_symbols(struct state *unused, struct die *cache, } } +static void process_symbol_ptr(struct symbol *sym, void *arg) +{ + struct state state; + Dwarf *dwarf = arg; + + if (sym->state != SYMBOL_UNPROCESSED || !sym->ptr_die_addr) + return; + + debug("%s", sym->name); + state_init(&state); + state.sym = sym; + + if (!dwarf_die_addr_die(dwarf, (void *)sym->ptr_die_addr, &state.die)) + error("dwarf_die_addr_die failed for symbol ptr: '%s'", + sym->name); + + if (dwarf_tag(&state.die) == DW_TAG_subroutine_type) + process_subprogram(&state, &state.die); + else + process_variable(&state, &state.die); + + cache_free(&state.expansion_cache); +} + void process_cu(Dwarf_Die *cudie) { check(process_die_container(NULL, NULL, cudie, process_exported_symbols, match_all)); + symbol_for_each(process_symbol_ptr, dwarf_cu_getdwarf(cudie->cu)); + cache_free(&srcfile_cache); } diff --git a/scripts/gendwarfksyms/examples/symbolptr.c b/scripts/gendwarfksyms/examples/symbolptr.c new file mode 100644 index 000000000000..88bc1bd60da8 --- /dev/null +++ b/scripts/gendwarfksyms/examples/symbolptr.c @@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Google LLC + * + * Example for symbol pointers. When compiled with Clang, gendwarfkyms + * uses a symbol pointer for `f`. + * + * $ clang -g -c examples/symbolptr.c -o examples/symbolptr.o + * $ echo -e "f\ng\np" | ./gendwarfksyms -d examples/symbolptr.o + */ + +/* Kernel macros for userspace testing. */ +#ifndef __used +#define __used __attribute__((__used__)) +#endif +#ifndef __section +#define __section(section) __attribute__((__section__(section))) +#endif + +#define __GENDWARFKSYMS_EXPORT(sym) \ + static typeof(sym) *__gendwarfksyms_ptr_##sym __used \ + __section(".discard.gendwarfksyms") = &sym; + +extern void f(unsigned int arg); +void g(int *arg); +void g(int *arg) {} + +struct s; +extern struct s *p; + +__GENDWARFKSYMS_EXPORT(f); +__GENDWARFKSYMS_EXPORT(g); +__GENDWARFKSYMS_EXPORT(p); diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h index 5fd743108b6f..86b3a3f2f558 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -94,6 +94,10 @@ extern int symtypes; * symbols.c */ +/* See symbols.c:is_symbol_ptr */ +#define SYMBOL_PTR_PREFIX "__gendwarfksyms_ptr_" +#define SYMBOL_PTR_PREFIX_LEN (sizeof(SYMBOL_PTR_PREFIX) - 1) + static inline unsigned int addr_hash(uintptr_t addr) { return hash_ptr((const void *)addr); @@ -117,14 +121,17 @@ struct symbol { struct hlist_node name_hash; enum symbol_state state; uintptr_t die_addr; + uintptr_t ptr_die_addr; unsigned long crc; }; typedef void (*symbol_callback_t)(struct symbol *, void *arg); +bool is_symbol_ptr(const char *name); void symbol_read_exports(FILE *file); void symbol_read_symtab(int fd); struct symbol *symbol_get(const char *name); +void symbol_set_ptr(struct symbol *sym, Dwarf_Die *ptr); void symbol_set_die(struct symbol *sym, Dwarf_Die *die); void symbol_set_crc(struct symbol *sym, unsigned long crc); void symbol_for_each(symbol_callback_t func, void *arg); diff --git a/scripts/gendwarfksyms/symbols.c b/scripts/gendwarfksyms/symbols.c index e0c9007f7250..e00c86fa0ba2 100644 --- a/scripts/gendwarfksyms/symbols.c +++ b/scripts/gendwarfksyms/symbols.c @@ -39,6 +39,20 @@ static unsigned int __for_each_addr(struct symbol *sym, symbol_callback_t func, return processed; } +/* + * For symbols without debugging information (e.g. symbols defined in other + * TUs), we also match __gendwarfksyms_ptr_ symbols, which the + * kernel uses to ensure type information is present in the TU that exports + * the symbol. A __gendwarfksyms_ptr pointer must have the same type as the + * exported symbol, e.g.: + * + * typeof(symname) *__gendwarf_ptr_symname = &symname; + */ +bool is_symbol_ptr(const char *name) +{ + return name && !strncmp(name, SYMBOL_PTR_PREFIX, SYMBOL_PTR_PREFIX_LEN); +} + static unsigned int for_each(const char *name, symbol_callback_t func, void *data) { @@ -47,6 +61,8 @@ static unsigned int for_each(const char *name, symbol_callback_t func, if (!name || !*name) return 0; + if (is_symbol_ptr(name)) + name += SYMBOL_PTR_PREFIX_LEN; hash_for_each_possible_safe(symbol_names, match, tmp, name_hash, hash_str(name)) { @@ -84,6 +100,17 @@ void symbol_set_crc(struct symbol *sym, unsigned long crc) error("no matching symbols: '%s'", sym->name); } +static void set_ptr(struct symbol *sym, void *data) +{ + sym->ptr_die_addr = (uintptr_t)((Dwarf_Die *)data)->addr; +} + +void symbol_set_ptr(struct symbol *sym, Dwarf_Die *ptr) +{ + if (for_each(sym->name, set_ptr, ptr) == 0) + error("no matching symbols: '%s'", sym->name); +} + static void set_die(struct symbol *sym, void *data) { sym->die_addr = (uintptr_t)((Dwarf_Die *)data)->addr; From patchwork Thu Nov 21 20:42:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13882354 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4A5531DF75E for ; Thu, 21 Nov 2024 20:43:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221786; cv=none; b=eUCSQD87ePgnAEP0QVRYMtDtPEOsIRPBryF3iPXAbi5qyhFqRDi3e86AMvhrPEmlvs0F8ro9AtNjwyHkAljb7HL9R9JJv8sz2IoGYfJgUuAJDWDDkS+K941aHqRkbqVSY7ODfnYi0dbHDlUMkBBAcREI88Ot2ampKLjXsuZ5PPE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221786; c=relaxed/simple; bh=xtaSIvxHknHitZ36vqFnUTb8roe1ES/1Z1TTODMFmEs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=efLhY9hF4OjjmSPmbZjdnxvhxyEM2EbCa2CSSYNEHabQrHCiyrTLHwjJ1svKkIL/6/UKuGmhiIXQ8QTuhzJCVHDz1/N2zLoukVvW5VW4JOk1rFf+feRD11MrZ5oQ2LjiKtE7b+PLLfzpBopt/9dT3Y/alFW0qlvnKIGIlMUqi6U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=S38/b6dN; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="S38/b6dN" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-20d417fc584so14365955ad.1 for ; Thu, 21 Nov 2024 12:43:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732221783; x=1732826583; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=BV+aUvfqJMcZq8xGaB9yJiMSnj11evO//RiyZr5bI/4=; b=S38/b6dNAR/mRk63yOTZVUW4y/viR1xVPg5QkwAouQqD+CwPoLwuMINSL14i3KEMZc IKPfwYy178GEVWs/kCurYHokn+hEW/H3BgkN9YRufqFbkNtzUM/XmP2TRpuPAdJExg89 A8qU7LJfXGn2Dp9vZww8HOhm59/P1x6NXUOLB/fowTp+qw6/OqfW07r6EO4i3LS7BGf8 EoP5vrLYXcOqjN2blmSJsO5HRCgpTTSRs/29fP1LpFwCf74SYqiJteI8UWj436c9wxt8 ZrjVU/SI/tezhwZE3Ai3UPtz+8H9cV03WNRHNAnR/6LP98axX++CFqV5AJrbSLXU7L5G Ei4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732221783; x=1732826583; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=BV+aUvfqJMcZq8xGaB9yJiMSnj11evO//RiyZr5bI/4=; b=QdT6zjNh8hXJB3WaIv0y4M2Pcf4ffKRDlQIu9uzDqrKhNDPfGiGrmKquq6g9ngounK PID0NuwDfJ3ie/TaumlhLQ8mhyZBl5Q117GOT+X39PAR3D4azTgGoI3v8VWSnZLw5YHd pmISMEh4GIkOMQd7cWn2AGCz1/22kOvJRUD4PPjTrfrnWLDahoi/ukieDN9ekcygyai7 tnUqV6y3R67/MR0CvZTZeG7H+iaAs61e8ThdgI+d7XE8LF4zbqQrvV+c8X9goWfpVjEY M3eTSdDjNlIZGQ8/AYiO8cn2qVmrbCeszKEmcapaRn+0SCtR9dLgrTUm/ko2h/dozD5o dYRA== X-Forwarded-Encrypted: i=1; AJvYcCUmtjk5cbLgK5ZfKzuB60DCTcVtKcGeynzYkBxAI/qI6d5upYzcOxmDuuiD1NZ2cyDzdpRvHD5KFMIpuR/I@vger.kernel.org X-Gm-Message-State: AOJu0YzgE0btPDeR03116pHWijefksjbKUrFF97LIobm2SEYJ/9+7ynr YmVG9/WgrIPT3lWciB1kirtND+R3gefUDlTme4nGBCKBMTefOZLtLXVcrMHo1p5I2WTR1xeUL1+ kEV4niMRajxd5g+XvtFncsRl0Tg== X-Google-Smtp-Source: AGHT+IEL8dfJrjeB7OOZP3DGVEJNUwuVUxTwOq2yx0IpeIeDGWPIyVAbuB88MKF3tMDhLZ35CoQVhQdB25OFLx22EmU= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a17:902:facc:b0:212:673a:c18e with SMTP id d9443c01a7336-2129f20e86dmr2275ad.2.1732221783527; Thu, 21 Nov 2024 12:43:03 -0800 (PST) Date: Thu, 21 Nov 2024 20:42:37 +0000 In-Reply-To: <20241121204220.2378181-20-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121204220.2378181-20-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=2005; i=samitolvanen@google.com; h=from:subject; bh=xtaSIvxHknHitZ36vqFnUTb8roe1ES/1Z1TTODMFmEs=; b=owGbwMvMwCEWxa662nLh8irG02pJDOn2s01DnoisXl+/y+usjtDHjTJL9TPntS5fHLCojZMtu Pxch01XRykLgxgHg6yYIkvL19Vbd393Sn31uUgCZg4rE8gQBi5OAZjIPgmGf3p2aUsZb0SxHjge XHf1TNqhRoH9OVde/Tj/1sjBfJd3dCDDP8vyeRd2KClw+Lw69uqgzdFf80++vRYtvHvjk2+5AUZ LmpgA X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121204220.2378181-36-samitolvanen@google.com> Subject: [PATCH v6 16/18] export: Add __gendwarfksyms_ptr_ references to exported symbols From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen With gendwarfksyms, we need each TU where the EXPORT_SYMBOL() macro is used to also contain DWARF type information for the symbols it exports. However, as a TU can also export external symbols and compilers may choose not to emit debugging information for symbols not defined in the current TU, the missing types will result in missing symbol versions. Stand-alone assembly code also doesn't contain type information for exported symbols, so we need to compile a temporary object file with asm-prototypes.h instead, and similarly need to ensure the DWARF in the temporary object file contains the necessary types. To always emit type information for external exports, add explicit __gendwarfksyms_ptr_ references to them in EXPORT_SYMBOL(). gendwarfksyms will use the type information for __gendwarfksyms_ptr_* if needed. Discard the pointers from the final binary to avoid further bloat. Signed-off-by: Sami Tolvanen --- include/linux/export.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/include/linux/export.h b/include/linux/export.h index 0bbd02fd351d..cf71d3202e5b 100644 --- a/include/linux/export.h +++ b/include/linux/export.h @@ -52,9 +52,24 @@ #else +#ifdef CONFIG_GENDWARFKSYMS +/* + * With CONFIG_GENDWARFKSYMS, ensure the compiler emits debugging + * information for all exported symbols, including those defined in + * different TUs, by adding a __gendwarfksyms_ptr_ pointer + * that's discarded during the final link. + */ +#define __GENDWARFKSYMS_EXPORT(sym) \ + static typeof(sym) *__gendwarfksyms_ptr_##sym __used \ + __section(".discard.gendwarfksyms") = &sym; +#else +#define __GENDWARFKSYMS_EXPORT(sym) +#endif + #define __EXPORT_SYMBOL(sym, license, ns) \ extern typeof(sym) sym; \ __ADDRESSABLE(sym) \ + __GENDWARFKSYMS_EXPORT(sym) \ asm(__stringify(___EXPORT_SYMBOL(sym, license, ns))) #endif From patchwork Thu Nov 21 20:42:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13882355 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 13C9A1DF998 for ; Thu, 21 Nov 2024 20:43:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221788; cv=none; b=SY8XdDxDhn4yHVyN6xcVhvXNBqgofJkL9IydcKUtWmQ+CRMeFwbK61L8wdKyl4WTRA18E9znwa64nbYgY1m251YWlos+Vj1sf4hkt8I4Hv+tAOxTK1mCD+mG7xVvSAExTrfWRVU8/lMnayoZEv0MQPd9lsw95hM9VAI7fB4OxQQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221788; c=relaxed/simple; bh=jbxFhuvey1WTvyXfp5U6GmMsYsEYqr1Ay/4F5G1PdfI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=LRdK1FYc2EsATbvN+tn2uvFhEsQqmUH7wDIYThkHs/kJ1gDOvqbKTY0AtttMpPOIFzoXnckokiP9AlEWtOVFw8dxBw748CTHcCTrHvS8YbOTWcMhrV0TsHsIbzp8/B4bJBYenMHdYvCTY2IwvkpZDaFopny31fg/Wtv/IYI3oSg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=a5pg+LcL; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="a5pg+LcL" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-7eb07db7812so1069593a12.0 for ; Thu, 21 Nov 2024 12:43:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732221785; x=1732826585; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=N5MCNRfop5nOfUUV7hHmV5AoBzZh1cegVvykqd2dnJM=; b=a5pg+LcL/X+sn2EgvG91smFjKUDL1dSI//qcJIpSO3ue5zbS12rO2nyRj7yO7RNrIj 4GEwifCMMJCjRkqtb1YdzlxjQ9QFR+qV4aA9F1Ks0ILDQ4PW/m0ikb/jlaK7CR69cTxK VIVmPCRs7ggvx13yoY8tSOnFnTFdqd86KASIjTotM2x/qpu1U0Zatk6W4mKNjprrYuYp +F9HFaSaINtsUV7CLGdC+LUxeIekSoVn7DnnxajvUuYcLVCNHZPEx5mf0q9QyAUVYMKB pvPC/su0YMua79azbrALEZIWAaqMztRHDn0GdAuvqdqs3rJWLMI6Mn0ciXEkWqTgzFvD 17FQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732221785; x=1732826585; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=N5MCNRfop5nOfUUV7hHmV5AoBzZh1cegVvykqd2dnJM=; b=AdKnw/V/VzxnO6fWNpHINUCI1I7Tp6kDkThxMI14YeX/jwTik1ZrDQmE3QBEp/T84y 2NWoriVakqLhw70sYgk9okXC06leU2kUWIRTSG4x+qtFPO3uv1TRNXEZESAMAu2zWIoY ocom+n/Yjk1OsPRKJ9NcKfaXv9jjGabbdDYi25VeCfLRmmcCUWO+5br99jUQZEPg0O3n awfzI5sVhjsozVkanRN4A1O8e7fWi0EKRDvhgEwn8V0i1Ae3phEy/+D9r9xzKlrCL22O eWO/nTT38KcbVFYXqlSzlIs3f6tvChNRNi0WLdEsefx8HrnqOoT31P8D7sQveyUiEnCn jhMg== X-Forwarded-Encrypted: i=1; AJvYcCXUiyr5m5YwFgihpUhZmZL/bZDTqzmXHvWp9kISbr33cl3UDiVC2CVyyfc/XYZDMWY5OFnucwyn1eaya2NO@vger.kernel.org X-Gm-Message-State: AOJu0Yz+4E3ZFAPn4UqrodyY8KFOM5+RxTA2DyzHxgviwX7Gdbb0c8J3 QZv/OSQQSTSlYcxRRFSNTjaajat0pHSDn1sEqzswbDCkDPULC06jfCXsY5gx6NvIueUs3/FE9Jr e9rDxQz1Mm9lgEt+uLwsmHtIH+g== X-Google-Smtp-Source: AGHT+IHybJoSX8i6NWVVo83CFQI1wGyIHwg1VGSqiPTrJEHr5+EdtAXeiKNNUzkyQFUirqk0zWW0qCwUXRK0ndBz7QI= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a63:b54c:0:b0:7e9:c9d5:4d98 with SMTP id 41be03b00d2f7-7fbccbd3a38mr276a12.5.1732221785245; Thu, 21 Nov 2024 12:43:05 -0800 (PST) Date: Thu, 21 Nov 2024 20:42:38 +0000 In-Reply-To: <20241121204220.2378181-20-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121204220.2378181-20-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=4744; i=samitolvanen@google.com; h=from:subject; bh=jbxFhuvey1WTvyXfp5U6GmMsYsEYqr1Ay/4F5G1PdfI=; b=owGbwMvMwCEWxa662nLh8irG02pJDOn2s02/Ciw89OEv3yVPw15n/tALmZNm6cdOWyUbZ9Kq+ 91yYVRfRwkLgxgHg6yYIkvL19Vbd393Sn31uUgCZg4rE8gQBi5OAZhItg4jQ4Nd88ufWXa2rLdb dCPcWct/sPfqb1xxb3n2rjdfnA7VbWT48TLg4JuLa5h5HL8WFp+I/pEXv6Ly7//m9qiTLQxcHY8 4AQ== X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121204220.2378181-37-samitolvanen@google.com> Subject: [PATCH v6 17/18] kbuild: Add gendwarfksyms as an alternative to genksyms From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen When MODVERSIONS is enabled, allow selecting gendwarfksyms as the implementation, but default to genksyms. Signed-off-by: Sami Tolvanen --- kernel/module/Kconfig | 25 ++++++++++++++++++++++++- scripts/Makefile | 2 +- scripts/Makefile.build | 35 +++++++++++++++++++++++++++++------ 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/kernel/module/Kconfig b/kernel/module/Kconfig index f9e5f82fa88b..e6b2427e5c19 100644 --- a/kernel/module/Kconfig +++ b/kernel/module/Kconfig @@ -169,13 +169,36 @@ config MODVERSIONS make them incompatible with the kernel you are running. If unsure, say N. +choice + prompt "Module versioning implementation" + depends on MODVERSIONS + default GENKSYMS + help + Select the tool used to calculate symbol versions for modules. + + If unsure, select GENKSYMS. + +config GENKSYMS + bool "genksyms (from source code)" + help + Calculate symbol versions from pre-processed source code using + genksyms. + + If unsure, say Y. + config GENDWARFKSYMS - bool + bool "gendwarfksyms (from debugging information)" depends on DEBUG_INFO # Requires full debugging information, split DWARF not supported. depends on !DEBUG_INFO_REDUCED && !DEBUG_INFO_SPLIT # Requires ELF object files. depends on !LTO + help + Calculate symbol versions from DWARF debugging information using + gendwarfksyms. Requires DEBUG_INFO to be enabled. + + If unsure, say N. +endchoice config ASM_MODVERSIONS bool diff --git a/scripts/Makefile b/scripts/Makefile index d7fec46d38c0..8533f4498885 100644 --- a/scripts/Makefile +++ b/scripts/Makefile @@ -53,7 +53,7 @@ hostprogs += unifdef targets += module.lds subdir-$(CONFIG_GCC_PLUGINS) += gcc-plugins -subdir-$(CONFIG_MODVERSIONS) += genksyms +subdir-$(CONFIG_GENKSYMS) += genksyms subdir-$(CONFIG_GENDWARFKSYMS) += gendwarfksyms subdir-$(CONFIG_SECURITY_SELINUX) += selinux subdir-$(CONFIG_SECURITY_IPE) += ipe diff --git a/scripts/Makefile.build b/scripts/Makefile.build index f41ce2131979..d6b211a7b261 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -107,13 +107,24 @@ cmd_cpp_i_c = $(CPP) $(c_flags) -o $@ $< $(obj)/%.i: $(obj)/%.c FORCE $(call if_changed_dep,cpp_i_c) +getexportsymbols = $(NM) $@ | sed -n 's/.* __export_symbol_\(.*\)/$(1)/p' + +gendwarfksyms = $(objtree)/scripts/gendwarfksyms/gendwarfksyms \ + $(if $(KBUILD_SYMTYPES), --symtypes $(@:.o=.symtypes)) \ + $(if $(KBUILD_GENDWARFKSYMS_STABLE), --stable) + genksyms = $(objtree)/scripts/genksyms/genksyms \ $(if $(KBUILD_SYMTYPES), -T $(@:.o=.symtypes)) \ $(if $(KBUILD_PRESERVE), -p) \ $(addprefix -r , $(wildcard $(@:.o=.symref))) # These mirror gensymtypes_S and co below, keep them in synch. +ifdef CONFIG_GENDWARFKSYMS +cmd_gensymtypes_c = $(if $(skip_gendwarfksyms),, \ + $(call getexportsymbols,\1) | $(gendwarfksyms) $@) +else cmd_gensymtypes_c = $(CPP) -D__GENKSYMS__ $(c_flags) $< | $(genksyms) +endif # CONFIG_GENDWARFKSYMS # LLVM assembly # Generate .ll files from .c @@ -279,14 +290,26 @@ $(obj)/%.ll: $(obj)/%.rs FORCE # This is convoluted. The .S file must first be preprocessed to run guards and # expand names, then the resulting exports must be constructed into plain # EXPORT_SYMBOL(symbol); to build our dummy C file, and that gets preprocessed -# to make the genksyms input. +# to make the genksyms input or compiled into an object for gendwarfksyms. # # These mirror gensymtypes_c and co above, keep them in synch. -cmd_gensymtypes_S = \ - { echo "\#include " ; \ - echo "\#include " ; \ - $(NM) $@ | sed -n 's/.* __export_symbol_\(.*\)/EXPORT_SYMBOL(\1);/p' ; } | \ - $(CPP) -D__GENKSYMS__ $(c_flags) -xc - | $(genksyms) +getasmexports = \ + { echo "\#include " ; \ + echo "\#include " ; \ + echo "\#include " ; \ + $(call getexportsymbols,EXPORT_SYMBOL(\1);) ; } + +ifdef CONFIG_GENDWARFKSYMS +cmd_gensymtypes_S = \ + $(getasmexports) | \ + $(CC) $(c_flags) -c -o $(@:.o=.gendwarfksyms.o) -xc -; \ + $(call getexportsymbols,\1) | \ + $(gendwarfksyms) $(@:.o=.gendwarfksyms.o) +else +cmd_gensymtypes_S = \ + $(getasmexports) | \ + $(CPP) -D__GENKSYMS__ $(c_flags) -xc - | $(genksyms) +endif # CONFIG_GENDWARFKSYMS quiet_cmd_cpp_s_S = CPP $(quiet_modtag) $@ cmd_cpp_s_S = $(CPP) $(a_flags) -o $@ $< From patchwork Thu Nov 21 20:42:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13882356 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 560D71DFDA6 for ; Thu, 21 Nov 2024 20:43:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221789; cv=none; b=jU5r7r70RKV80dqNOhJH/JnZO0mwaf/qQLDD7wryxbBL3bdND9rAqWYE2B7JS5+Ycmb+Uhyp6Qevs+1ZE0ZIbQXRtvG49DjlTFD+NvdO7k+CikCYxVaDCNmLCym+RY4csIF+xaMskMP2xpm9GuZsZgeiwuY52Mb/M1jhYRNK/hw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221789; c=relaxed/simple; bh=zW7908MQu2FtNUVy2Cx6yEPpWxLbyw7Hr6NCDOhP/to=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Otkc+yncajzGff4e5eUD2yS4XKzZL0ief9iCv60J5WX5xb5gCOMGS4WmGNeuIwOP702CTl+JV6dGw0BS1mqqUHgXit5ChNltqSxPK4Z/clsJV9zzNK/pfRZcGvZspaeStMTLKijAfVB7sM7iCuFM1C/zZ5L3pLITk1JudahwcPQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=yW1qtGNi; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="yW1qtGNi" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-7fbb11b2760so1236659a12.2 for ; Thu, 21 Nov 2024 12:43:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732221787; x=1732826587; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=qGwjZnzxYsO+I8a1Rm02akWiDmIevBjaewf7s2sRAbk=; b=yW1qtGNiFQBIivV0ZUwrwQYPsXasmfxiXPtOuzXfdblRjuJ9SZ7yjiOrSaf3X5T48R V2BFJl7GX1TtDNq28sGRQxa4/N1ezL8y/dJbLxYHpIn0r66DdeVJHwWG4xTRGFVKZV5H O/feoi396wCCba1elg7xtBR/1WRO/Uz+4FZLVE1lFLT1vhPgInwPN4ZKp9fwm+8GL+11 9N37/6jHTTbXfCstkv7W0knzI+MoCzYGG5S+V72lEjMm8eYwB1D34N/EJBO98CPYuByH EsU/yVUm3aZxmNpPyKAI6ZXGpjuCLRaIINZqQvmzO0tRJ8CsEH44+OPfdC4d2HcTYADR 6csQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732221787; x=1732826587; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=qGwjZnzxYsO+I8a1Rm02akWiDmIevBjaewf7s2sRAbk=; b=JMG5Ud5U0P8j9Rsy64LIDudzqf7w2jkP0tR7/5Su4hA5rniAvQabmeakTagfQCD8Ev 13tDoxuIzgFMdXaiup/VnHm0oi8f+i7bkslcIp3HLNX1vJmrq91ylnsM5PivwUaejnXx kq2CstDK6CuqGI61L09RndALae5ZXG/lCEHpu6bCJsesM5hKAjFjFOc7B53NJEcoQT5r a74958gEeJRlznvD8DOFNX3JISa1+nu0Rdr/3p0ni/twSxC2JmYslSnLI5I6+pPd3+6R LsnlpoDZtkMfr2QyACyEj1NNBmj8zu/29CF4dp063ESQjxVjKYxK3fd5lFz8nEdrw/jC s2Nw== X-Forwarded-Encrypted: i=1; AJvYcCXmlSiXx6H1B+LzSwlXJYGyBMhSfoOJiGSzV8HOQ8DCVlcrhpJwBVOBz5TkDEboq78jbJBDWXFh4MITTIco@vger.kernel.org X-Gm-Message-State: AOJu0Yw+2W0dNXd0jEX7kc4oy+IUigvrTQsXxzcuhIQd+agWsapoCp5Y 7AeoqSgFdv0CM7rSmLKTnZKPWnK2GBTr4X+/oMJYD6/cZJ7UOsGbyCvxiOoyAgJKPMrzBC8Z6ln zOnZDvCA5cb3OoP55Ied9O4FeIQ== X-Google-Smtp-Source: AGHT+IE3IxQ3cOARO0M97W7yu9tXaYeWL9jdgIb1dFEltrt1+1/yhEFtSG0hqz6HzV9bQmSbVOsshG7ru3OpVYZGEUo= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a65:4003:0:b0:7db:497:6cea with SMTP id 41be03b00d2f7-7fbccad9410mr305a12.6.1732221786696; Thu, 21 Nov 2024 12:43:06 -0800 (PST) Date: Thu, 21 Nov 2024 20:42:39 +0000 In-Reply-To: <20241121204220.2378181-20-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121204220.2378181-20-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=13479; i=samitolvanen@google.com; h=from:subject; bh=zW7908MQu2FtNUVy2Cx6yEPpWxLbyw7Hr6NCDOhP/to=; b=owGbwMvMwCEWxa662nLh8irG02pJDOn2s820ub6vNVw5Y0HlKsd1DypKbY2nhHDPl98ka2Qak 58b/Nygo5SFQYyDQVZMkaXl6+qtu787pb76XCQBM4eVCWQIAxenAExkviQjwyJ+DUfhpbvinvXL dF6ouf9iseJ0SfuzDhNKNq+d9qd49ieG/0lXTu1Yt7zN7Hk/c8qqOr/6NEWZmRd+XGOUPhPz/rp DPR8A X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121204220.2378181-38-samitolvanen@google.com> Subject: [PATCH v6 18/18] Documentation/kbuild: Add DWARF module versioning From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Add documentation for gendwarfksyms changes, and the kABI stability features that can be useful for distributions even though they're not used in mainline kernels. Signed-off-by: Sami Tolvanen --- Documentation/kbuild/gendwarfksyms.rst | 308 +++++++++++++++++++++++++ Documentation/kbuild/index.rst | 1 + 2 files changed, 309 insertions(+) create mode 100644 Documentation/kbuild/gendwarfksyms.rst diff --git a/Documentation/kbuild/gendwarfksyms.rst b/Documentation/kbuild/gendwarfksyms.rst new file mode 100644 index 000000000000..7725d7f57131 --- /dev/null +++ b/Documentation/kbuild/gendwarfksyms.rst @@ -0,0 +1,308 @@ +======================= +DWARF module versioning +======================= + +1. Introduction +=============== + +When CONFIG_MODVERSIONS is enabled, symbol versions for modules +are typically calculated from preprocessed source code using the +**genksyms** tool. However, this is incompatible with languages such +as Rust, where the source code has insufficient information about +the resulting ABI. With CONFIG_GENDWARFKSYMS (and CONFIG_DEBUG_INFO) +selected, **gendwarfksyms** is used instead to calculate symbol versions +from the DWARF debugging information, which contains the necessary +details about the final module ABI. + +1.1. Usage +========== + +gendwarfksyms accepts a list of object files on the command line, and a +list of symbol names (one per line) in standard input:: + + Usage: gendwarfksyms [options] elf-object-file ... < symbol-list + + Options: + -d, --debug Print debugging information + --dump-dies Dump DWARF DIE contents + --dump-die-map Print debugging information about die_map changes + --dump-types Dump type strings + --dump-versions Dump expanded type strings used for symbol versions + -s, --stable Support kABI stability features + -T, --symtypes file Write a symtypes file + -h, --help Print this message + + +2. Type information availability +================================ + +While symbols are typically exported in the same translation unit (TU) +where they're defined, it's also perfectly fine for a TU to export +external symbols. For example, this is done when calculating symbol +versions for exports in stand-alone assembly code. + +To ensure the compiler emits the necessary DWARF type information in the +TU where symbols are actually exported, gendwarfksyms adds a pointer +to exported symbols in the `EXPORT_SYMBOL()` macro using the following +macro:: + + #define __GENDWARFKSYMS_EXPORT(sym) \ + static typeof(sym) *__gendwarfksyms_ptr_##sym __used \ + __section(".discard.gendwarfksyms") = &sym; + + +When a symbol pointer is found in DWARF, gendwarfksyms can use its +type for calculating symbol versions even if the symbol is defined +elsewhere. The name of the symbol pointer is expected to start with +`__gendwarfksyms_ptr_`, followed by the name of the exported symbol. + +3. Symtypes output format +========================= + +Similarly to genksyms, gendwarfksyms supports writing a symtypes +file for each processed object that contain types for exported +symbols and each referenced type that was used in calculating symbol +versions. These files can be useful when trying to determine what +exactly caused symbol versions to change between builds. To generate +symtypes files during a kernel build, set `KBUILD_SYMTYPES=1`. + +Matching the existing format, the first column of each line contains +either a type reference or a symbol name. Type references have a +one-letter prefix followed by "#" and the name of the type. Four +reference types are supported:: + + e# = enum + s# = struct + t# = typedef + u# = union + +Type names with spaces in them are wrapped in single quotes, e.g.:: + + s#'core::result::Result' + +The rest of the line contains a type string. Unlike with genksyms that +produces C-style type strings, gendwarfksyms uses the same simple parsed +DWARF format produced by **--dump-dies**, but with type references +instead of fully expanded strings. + +4. Maintaining a stable kABI +============================ + +Distribution maintainers often need the ability to make ABI compatible +changes to kernel data structures due to LTS updates or backports. Using +the traditional `#ifndef __GENKSYMS__` to hide these changes from symbol +versioning won't work when processing object files. To support this +use case, gendwarfksyms provides kABI stability features designed to +hide changes that won't affect the ABI when calculating versions. These +features are all gated behind the **--stable** command line flag and are +not used in the mainline kernel. To use stable features during a kernel +build, set `KBUILD_GENDWARFKSYMS_STABLE=1`. + +Examples for using these features are provided in the +**scripts/gendwarfksyms/examples** directory, including helper macros +for source code annotation. Note that as these features are only used to +transform the inputs for symbol versioning, the user is responsible for +ensuring that their changes actually won't break the ABI. + +4.1. kABI rules +=============== + +kABI rules allow distributions to fine-tune certain parts +of gendwarfksyms output and thus control how symbol +versions are calculated. These rules are defined in the +`.discard.gendwarfksyms.kabi_rules` section of the object file and +consist of simple null-terminated strings with the following structure:: + + version\0type\0target\0value\0 + +This string sequence is repeated as many times as needed to express all +the rules. The fields are as follows: + +- `version`: Ensures backward compatibility for future changes to the + structure. Currently expected to be "1". +- `type`: Indicates the type of rule being applied. +- `target`: Specifies the target of the rule, typically the fully + qualified name of the DWARF Debugging Information Entry (DIE). +- `value`: Provides rule-specific data. + +The following helper macro, for example, can be used to specify rules +in the source code:: + + #define __KABI_RULE(hint, target, value) \ + static const char __PASTE(__gendwarfksyms_rule_, \ + __COUNTER__)[] __used __aligned(1) \ + __section(".discard.gendwarfksyms.kabi_rules") = \ + "1\0" #hint "\0" #target "\0" #value + + +Currently, only the rules discussed in this section are supported, but +the format is extensible enough to allow further rules to be added as +need arises. + +4.1.1. Managing definition visibility +===================================== + +A declaration can change into a full definition when additional includes +are pulled into the translation unit. This changes the versions of any +symbol that references the type even if the ABI remains unchanged. As +it may not be possible to drop includes without breaking the build, the +`declonly` rule can be used to specify a type as declaration-only, even +if the debugging information contains the full definition. + +The rule fields are expected to be as follows: + +- `type`: "declonly" +- `target`: The fully qualified name of the target data structure + (as shown in **--dump-dies** output). +- `value`: This field is ignored. + +Using the `__KABI_RULE` macro, this rule can be defined as:: + + #define KABI_DECLONLY(fqn) __KABI_RULE(declonly, fqn, ) + +Example usage:: + + struct s { + /* definition */ + }; + + KABI_DECLONLY(s); + +4.1.2. Adding enumerators +========================= + +For enums, all enumerators and their values are included in calculating +symbol versions, which becomes a problem if we later need to add more +enumerators without changing symbol versions. The `enumerator_ignore` +rule allows us to hide named enumerators from the input. + +The rule fields are expected to be as follows: + +- `type`: "enumerator_ignore" +- `target`: The fully qualified name of the target enum + (as shown in **--dump-dies** output) and the name of the + enumerator field separated by a space. +- `value`: This field is ignored. + +Using the `__KABI_RULE` macro, this rule can be defined as:: + + #define KABI_ENUMERATOR_IGNORE(fqn, field) \ + __KABI_RULE(enumerator_ignore, fqn field, ) + +Example usage:: + + enum e { + A, B, C, D, + }; + + KABI_ENUMERATOR_IGNORE(e, B); + KABI_ENUMERATOR_IGNORE(e, C); + +If the enum additionally includes an end marker and new values must +be added in the middle, we may need to use the old value for the last +enumerator when calculating versions. The `enumerator_value` rule allows +us to override the value of an enumerator for version calculation: + +- `type`: "enumerator_value" +- `target`: The fully qualified name of the target enum + (as shown in **--dump-dies** output) and the name of the + enumerator field separated by a space. +- `value`: Integer value used for the field. + +Using the `__KABI_RULE` macro, this rule can be defined as:: + + #define KABI_ENUMERATOR_VALUE(fqn, field, value) \ + __KABI_RULE(enumerator_value, fqn field, value) + +Example usage:: + + enum e { + A, B, C, LAST, + }; + + KABI_ENUMERATOR_IGNORE(e, C); + KABI_ENUMERATOR_VALUE(e, LAST, 2); + +4.3. Adding structure members +============================= + +Perhaps the most common ABI compatible changeis adding a member to a +kernel data structure. When changes to a structure are anticipated, +distribution maintainers can pre-emptively reserve space in the +structure and take it into use later without breaking the ABI. If +changes are needed to data structures without reserved space, existing +alignment holes can potentially be used instead. While kABI rules could +be added for these type of changes, using unions is typically a more +natural method. This section describes gendwarfksyms support for using +reserved space in data structures and hiding members that don't change +the ABI when calculating symbol versions. + +4.3.1. Reserving space and replacing members +============================================ + +Space is typically reserved for later use by appending integer types, or +arrays, to the end of the data structure, but any type can be used. Each +reserved member needs a unique name, but as the actual purpose is usually +not known at the time the space is reserved, for convenience, names that +start with `__kabi_` are left out when calculating symbol versions:: + + struct s { + long a; + long __kabi_reserved_0; /* reserved for future use */ + }; + +The reserved space can be taken into use by wrapping the member in a +union, which includes the original type and the replacement member:: + + struct s { + long a; + union { + long __kabi_reserved_0; /* original type */ + struct b b; /* replaced field */ + }; + }; + +If the `__kabi_` naming scheme was used when reserving space, the name +of the first member of the union must start with `__kabi_reserved`. This +ensures the original type is used when calculating versions, but the name +is again left out. The rest of the union is ignored. + +If we're replacing a member that doesn't follow this naming convention, +we also need to preserve the original name to avoid changing versions, +which we can do by changing the first union member's name to start with +`__kabi_renamed` followed by the original name. + +The examples include `KABI_(RESERVE|USE|REPLACE)*` macros that help +simplify the process and also ensure the replacement member is correctly +aligned and its size won't exceed the reserved space. + +4.3.2. Hiding members +===================== + +Predicting which structures will require changes during the support +timeframe isn't always possible, in which case one might have to resort +to placing new members into existing alignment holes:: + + struct s { + int a; + /* a 4-byte alignment hole */ + unsigned long b; + }; + + +While this won't change the size of the data structure, one needs to +be able to hide the added members from symbol versioning. Similarly +to reserved fields, this can be accomplished by wrapping the added +member to a union where one of the fields has a name starting with +`__kabi_ignored`:: + + struct s { + int a; + union { + char __kabi_ignored_0; + int n; + }; + unsigned long b; + }; + +With **--stable**, both versions produce the same symbol version. diff --git a/Documentation/kbuild/index.rst b/Documentation/kbuild/index.rst index cee2f99f734b..e82af05cd652 100644 --- a/Documentation/kbuild/index.rst +++ b/Documentation/kbuild/index.rst @@ -21,6 +21,7 @@ Kernel Build System reproducible-builds gcc-plugins llvm + gendwarfksyms .. only:: subproject and html