From nobody Wed Nov 27 18:37:19 2024 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 6EC8C212D3B for ; Tue, 8 Oct 2024 18:38:41 +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=1728412723; cv=none; b=aJNKwPOx0tFTym8YFVwVfzNzDh916UHupalb8DHh8+thGgU5fDjUkLLi0GIERXFfeN6Rx+a0Abg74E5ZiVK17NLC5Xt+rf5g05LHNOE2nRMAfh/S5EHXQ7PRZ7Py6qzvIjgFOLvgyypWpKudtzK8e8fZIiQBU6asqXwVgyRxdjs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412723; c=relaxed/simple; bh=eo+R2sBJnv+m6zmCr9UjsnOOCyqB0aRtDd5rSDkYDD8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Ws8i056kKUB1PbaDcny06OmG9KvOX8l32n0Hi6mC3D//hG402FMjnUPDO1OOJFOmTVpQoXo+I6ydMJAR6j1lvHTGk96SSDFRaQIHl3fAHEBjqlDEoMwN4UbM7pjoHkSbywwCcHyo8QrDJ3iVRhFLl/ZGbdOJCuD4hxD/EVGDxvs= 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=oXFGw465; 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="oXFGw465" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e28edea9af6so1615353276.3 for ; Tue, 08 Oct 2024 11:38:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728412720; x=1729017520; 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=4mOYxux7phJ57C24xHjYVDdst1lbeaQ8YtQCEcOZURM=; b=oXFGw465rCCjoDAJe7fD2FTtbcrK69haMWE5FlItCPj3jTK+SYAktRsI0A/rbkNkJl 9B1dl/vwRE3BpPWAn0m8foOqNfTuVsgTw9k3lyctRjuxW6RHTV9f3ic8EypIErt37dXa 1Q8m7J+YQWEzTDJAOpwrdvOUd5GOSl3Jim/eV+IFcEtUSyPDehSR/XA2GMMTr5OIcjfJ SaIJJaNb7oQluWKoYZKWalyWWs9zb6h6gTRoQ73PEGtG2EJQ8Rtt8vKc/36SqaI2a8Qk kD+2VctcFLWbWPsWUqTGhMz+53zDbBw/bSsEVONbaIZYEfgC7A4JNXNghVJFUNUj9eFH PS5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728412720; x=1729017520; 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=4mOYxux7phJ57C24xHjYVDdst1lbeaQ8YtQCEcOZURM=; b=Z8cKrfd0s3ldZKPpzIginbcuon9OchmqO2CLr/2gdrDAPU+iW5MKlwDyMeoep9n9mv vKUV02GzDxsF2wJVpdXZYSWbKaBzRusfAoeRT7jqMp6EEvO2vBZirM/UhO9MqoJzDHe5 J/xmMpKKn6ADtKCRRWtEtwaVyWXc6iqFdA90V5MTtiBWmHcx3BaraIJa9vlegXNKtcuU ifvJIqyjBz4tyVR22VlpcfO/CreO54WFMabdxij0cU2hM/SYgPRRbJVHn2GQ/fKKokcc Is7RXWRRgtF1+wzGxkV37hfNQE0VczWVDCUQhoQQckGlPAmkS/cg5lu+3EF7f07BKzzF tOOg== X-Forwarded-Encrypted: i=1; AJvYcCVoBoPKbMpCdQT+JJtmCPkBeeUNydcJ965kpnw1Q7GQEwHPFOQ+IgmClJVjUkeYYzNeuWDgrzusgi7icCc=@vger.kernel.org X-Gm-Message-State: AOJu0Yz+i1RMPGh5zNErqEZC0tI6YE95vmsVx+oPaXUFD8oOu99Mc6At M+pFIUHjSJqrSr1h31Nq7pqPRfwrzgh3yjRFPUpno3csU2/xuN5Gks6+T67XJAtBv4bZtrPTcBI /R6l4VkVj3n52Mke1Qx8Ls3k+4A== X-Google-Smtp-Source: AGHT+IEjOmD/ny7LxZkSR1lU2BjEtLi/IZV0CgcW3UIR7JYU+gd4v9F3fU7wjm6TlX6DjkneDCKcmkDU89MzYFcPdVc= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a25:ed0b:0:b0:e17:8e73:866c with SMTP id 3f1490d57ef6-e28fe421ddfmr75276.10.1728412720467; Tue, 08 Oct 2024 11:38:40 -0700 (PDT) Date: Tue, 8 Oct 2024 18:38:26 +0000 In-Reply-To: <20241008183823.36676-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241008183823.36676-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=14725; i=samitolvanen@google.com; h=from:subject; bh=eo+R2sBJnv+m6zmCr9UjsnOOCyqB0aRtDd5rSDkYDD8=; b=owGbwMvMwCEWxa662nLh8irG02pJDOmsNYpCQUr7DucuZTJd98ams+OZfaBd/Qbhlun7PznzS j6Nls3vKGVhEONgkBVTZGn5unrr7u9Oqa8+F0nAzGFlAhnCwMUpABPxMWZkmD/513W7o68D836d ERBunB2iyiLJvPlF9FUFljUPNb+7/2b4n9Pzq6Mn2bwjSMtQreqrzebiK9UnDQ3OKtcWucx+0vu LCQA= X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241008183823.36676-23-samitolvanen@google.com> Subject: [PATCH v4 02/19] 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Acked-by: Neal Gompa 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 | 124 +++++++++++++++++++ scripts/gendwarfksyms/gendwarfksyms.h | 97 +++++++++++++++ scripts/gendwarfksyms/symbols.c | 83 +++++++++++++ 8 files changed, 489 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. =20 +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 +=3D module.lds =20 subdir-$(CONFIG_GCC_PLUGINS) +=3D gcc-plugins subdir-$(CONFIG_MODVERSIONS) +=3D genksyms +subdir-$(CONFIG_GENDWARFKSYMS) +=3D gendwarfksyms subdir-$(CONFIG_SECURITY_SELINUX) +=3D selinux subdir-$(CONFIG_SECURITY_IPE) +=3D ipe =20 diff --git a/scripts/gendwarfksyms/.gitignore b/scripts/gendwarfksyms/.giti= gnore 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 +=3D gendwarfksyms + +gendwarfksyms-objs +=3D gendwarfksyms.o +gendwarfksyms-objs +=3D dwarf.o +gendwarfksyms-objs +=3D symbols.o + +HOSTLDLIBS_gendwarfksyms :=3D -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 *v= alue) +{ + 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 =3D get_linkage_name_attr(die); + if (!name) + name =3D get_name_attr(die); + + return name; +} + +static bool match_export_symbol(struct state *state, Dwarf_Die *die) +{ + Dwarf_Die *source =3D 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 =3D &origin; + + state->sym =3D symbol_get(get_symbol_name(die)); + + /* Look up using the origin name if there are no matches. */ + if (!state->sym && source !=3D die) + state->sym =3D symbol_get(get_symbol_name(source)); + + state->die =3D *source; + return !!state->sym; +} + +/* + * Type string processing + */ +static void process(const char *s) +{ + s =3D 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 =3D checkp(dwarf_child(die, ¤t)); + while (!res) { + if (match(¤t)) { + /* <0 =3D error, 0 =3D continue, >0 =3D stop */ + res =3D checkp(func(state, ¤t)); + if (res) + return res; + } + + res =3D 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 =3D 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 =3D=3D 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..1a9be8fa18c8 --- /dev/null +++ b/scripts/gendwarfksyms/gendwarfksyms.c @@ -0,0 +1,124 @@ +// 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 *n= ame, + Dwarf_Addr base, void *arg) +{ + Dwarf_Addr dwbias; + Dwarf_Die cudie; + Dwarf_CU *cu =3D NULL; + Dwarf *dbg; + int res; + + debug("%s", name); + dbg =3D dwfl_module_getdwarf(mod, &dwbias); + + do { + res =3D dwarf_get_units(dbg, cu, &cu, NULL, NULL, &cudie, NULL); + if (res < 0) + error("dwarf_get_units failed: no debugging information?"); + if (res =3D=3D 1) + break; /* No more units */ + + process_cu(&cudie); + } while (cu); + + return DWARF_CB_OK; +} + +static const Dwfl_Callbacks callbacks =3D { + .section_address =3D dwfl_offline_section_address, + .find_debuginfo =3D dwfl_standard_find_debuginfo, +}; + +int main(int argc, char **argv) +{ + unsigned int n; + int opt; + + struct option opts[] =3D { { "debug", 0, NULL, 'd' }, + { "dump-dies", 0, &dump_dies, 1 }, + { "help", 0, NULL, 'h' }, + { 0, 0, NULL, 0 } }; + + while ((opt =3D getopt_long(argc, argv, "dh", opts, NULL)) !=3D EOF) { + switch (opt) { + case 0: + break; + case 'd': + debug =3D 1; + break; + case 'h': + usage(); + return 0; + default: + usage(); + return 1; + } + } + + if (optind >=3D argc) { + usage(); + error("no input files?"); + } + + symbol_read_exports(stdin); + + for (n =3D optind; n < argc; n++) { + Dwfl *dwfl; + int fd; + + fd =3D open(argv[n], O_RDONLY); + if (fd =3D=3D -1) + error("open failed for '%s': %s", argv[n], + strerror(errno)); + + dwfl =3D 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); + } + + return 0; +} diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/= gendwarfksyms.h new file mode 100644 index 000000000000..1a10d18f178e --- /dev/null +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -0,0 +1,97 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2024 Google LLC + */ + +#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 =3D expr; \ + if (test) \ + error("`%s` failed: %d", #expr, __res); \ + __res; \ + }) + +/* Error =3D=3D non-zero values */ +#define check(expr) __check(expr, __res) +/* Error =3D=3D 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); + +/* + * 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/symbol= s.c new file mode 100644 index 000000000000..4df685deb9e0 --- /dev/null +++ b/scripts/gendwarfksyms/symbols.c @@ -0,0 +1,83 @@ +// 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 =3D NULL; + char *name =3D NULL; + size_t size =3D 0; + int nsym =3D 0; + + while (getline(&line, &size, file) > 0) { + if (sscanf(line, "%ms\n", &name) !=3D 1) + error("malformed input line: %s", line); + + if (is_exported(name)) { + /* Ignore duplicates */ + free(name); + continue; + } + + sym =3D xcalloc(1, sizeof(struct symbol)); + sym->name =3D 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 =3D arg; + + *res =3D sym; +} + +struct symbol *symbol_get(const char *name) +{ + struct symbol *sym =3D NULL; + + for_each(name, get_symbol, &sym); + return sym; +} --=20 2.47.0.rc0.187.ge670bccf7e-goog