From patchwork Thu Aug 15 17:39:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13765036 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 DC83547A4C for ; Thu, 15 Aug 2024 17:39:15 +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=1723743558; cv=none; b=aWTFGvo7iLUWAGpH5i2qjM9RNMgUjp0sFhVziYQS16q2/k7uMdbud6oyjAzQtfV1vtVaqzBEqp7V3zjTxsC3WgC3qSYP5uoV/ZV1WAVTAp+f7I5pjKaqzBM3cpcEEMF/1/Beyd5X2pR0XT3YuEdYVeojAFhcX1QZVVOX93p4YVM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743558; c=relaxed/simple; bh=bxNi2EoDMx8FMXT38cuDVktpfAT2lY3rDO58JAquwyI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=bpdmFeC7Om5oLdE2FCw/pNGhr6R8lPN89fhOhyHDQinC5UIWhlW99d1r77s4LHYYRH+rynPVqpH9ziB/apsbp2pTjDoQf/BM6NEColrVrbUZHBQr8VsixZNEg6LX2IvUeN4UtUrSLqSkgieGaafzihOhvlj9Q01sPcwqPghRuw4= 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=lRmZryVA; 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="lRmZryVA" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e035949cc4eso2189155276.1 for ; Thu, 15 Aug 2024 10:39:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723743555; x=1724348355; 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=qNdMzoDNg3SBOXN5kIH+J4L0OFXsoxTK6BvqXkHiFPI=; b=lRmZryVAuljBTeCQQ5Pg/29DbAh358SPMGDjmQoQYPniBMCNl06C/2+RO9c7NRzVcI ImzSKMs3ewupqw99eDAgTA3que5TnTssxim6Nw/NWNOV21Y1vgFFQ+uwVy8Gkd8f6Ccm DXzrqBPH+IiyBoPWWNWqKpj/wNWBFPSwheWR0KMzKYv4lyrIU8WXRsND/qwtLMFWfefQ wwCTQvvbyOkoE2vjCx5tP92AV9N8iRUHmSlJ12ExW9VJfx9MrwJVuOXRvfhX9+o2uuuG 6INwTVz89Ts1bYJOnRLq5C6h+epS21UTSJmtdNcRJo9SmP7TE0KDgGMVkXaVTxz18wnb mhnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723743555; x=1724348355; 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=qNdMzoDNg3SBOXN5kIH+J4L0OFXsoxTK6BvqXkHiFPI=; b=Ao3f91pOCRKuMqx6ZWxXHzXrRj5uGPlIqYZKpTWQWkdlOqYuJrY0IoA9cam2vopiZI 2+/LEwvN0sboGF+r1JpBHMXPMNGlHaFo5ScxUuK2UMNT7vS7m7di4U/dgZS7QRMaFLUX ysjMETFOF0kBHOsgiPJelHXK/LMbl1qaZ/TNeRhWIVHCH3BSQMPuNYuphufxYk/jJOAL 9KU4nQdKpHMvYMG8c0igsytPw1p2NvX4LTvRtmlO0DI3WpD435Di8t9m70AdOrQbDNV4 9YescLXkx6uDK8/8IsbX1c+KOLZdPxN3ATJnu/dtv8d8skoHN84SWZH4838turQUDXpl HUxA== X-Forwarded-Encrypted: i=1; AJvYcCUomEVX47GmApGO5tPYkskzkCFSVeWLloSYwdFj0gaEbzGw2YG3uvKyr+/iMOqWo2B+eO4eX/GEf3jCYbBqqq8d9xGulyzbdcMsAPCL X-Gm-Message-State: AOJu0Yy/W3KVHkIImvM/y04huj0IhV+psVlCh2vpRNJ2zfecP7DbY+Vn oDlaDTvn/R2UrCBqzA11DohKoMBlI8TCZo7Dz33aqV1oUrDk38fYZSKVw3qrurOz4ZUR6wVccLE eacFYzMkDkLd1VKU4VwKbypvDJA== X-Google-Smtp-Source: AGHT+IHPxCk3ILqts0dwN0ruCt7oiCn01RaYOO920w1P76dXAowMtUZ+z4/p1v1TXr/x0jIKsh3oPHsRIw4e5lInAac= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a25:8741:0:b0:e11:69f2:e39 with SMTP id 3f1490d57ef6-e1180f6c6e6mr24578276.9.1723743554965; Thu, 15 Aug 2024 10:39:14 -0700 (PDT) Date: Thu, 15 Aug 2024 17:39:05 +0000 In-Reply-To: <20240815173903.4172139-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240815173903.4172139-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=10595; i=samitolvanen@google.com; h=from:subject; bh=bxNi2EoDMx8FMXT38cuDVktpfAT2lY3rDO58JAquwyI=; b=owEB7QES/pANAwAKAUy19oSLvFbuAcsmYgBmvj03rj9tt5wrj9Oop0GTFoXQSq5wmqtqxKtMp Yzs8ToZK7mJAbMEAAEKAB0WIQQ1zPtjsoPW0663g5RMtfaEi7xW7gUCZr49NwAKCRBMtfaEi7xW 7mv2C/9J/fisIDuw3uC7zNCeENccXXNMNEhImxup017WiI9C1raumF4b/TiPX5pmVOdkHrJdeX4 6oogEA4PsDGD+qbP8sy4vD5fYnCEuna2ddzJdaWVJ6HuVQJltUoGfWVvPXXTUszFTmudEPJl0rk /Zmz0qeSwOHm6bffSaADMYck0ZHk4hs3UOQcgnDI1bB0kz7PuAZl3QQver9NXKuDznRVDZ7sTUJ mD/vGvuaijFMWy3aq7xpYknhl2lewDMrxeD6mvxcI62tubo/paaXe1ETXxflVEBPEAzj2JEXJ1I olO/JpOgWoNF4WXHHopciA6KbQSoRwjTDNcpvExHA4Fw05buwgqQlw4gN3NGok0IuGlzmDbDzQ6 bdZ4QkOWLpmjJfSG65LHtUmaQWUzo6jX90b4X2fdrk4Q6EIjuUd/lOj12i1uLldHrIn0I6mp1x0 Cmmjs/xYFQCoQX5Q9jq0JguOpEwQ8OF+5ZnfXYRAhKhZmxlLyEp/wfDTAm1QvuuEseQBo= X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240815173903.4172139-22-samitolvanen@google.com> Subject: [PATCH v2 01/19] tools: Add gendwarfksyms From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Wedson Almeida Filho , Gary Guo , Petr Pavlu , Neal Gompa , Hector Martin , Janne Grunau , Asahi Linux , 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 --- kernel/module/Kconfig | 8 ++ scripts/Makefile | 1 + scripts/gendwarfksyms/.gitignore | 2 + scripts/gendwarfksyms/Makefile | 7 ++ scripts/gendwarfksyms/dwarf.c | 87 +++++++++++++++ scripts/gendwarfksyms/gendwarfksyms.c | 146 ++++++++++++++++++++++++++ scripts/gendwarfksyms/gendwarfksyms.h | 78 ++++++++++++++ 7 files changed, 329 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 diff --git a/kernel/module/Kconfig b/kernel/module/Kconfig index 4047b6d48255..a506d4ac660f 100644 --- a/kernel/module/Kconfig +++ b/kernel/module/Kconfig @@ -168,6 +168,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 dccef663ca82..2fd0199662e9 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 # Let clean descend into subdirs diff --git a/scripts/gendwarfksyms/.gitignore b/scripts/gendwarfksyms/.gitignore new file mode 100644 index 000000000000..ab8c763b3afe --- /dev/null +++ b/scripts/gendwarfksyms/.gitignore @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-only +/gendwarfksyms diff --git a/scripts/gendwarfksyms/Makefile b/scripts/gendwarfksyms/Makefile new file mode 100644 index 000000000000..c1389c161f9c --- /dev/null +++ b/scripts/gendwarfksyms/Makefile @@ -0,0 +1,7 @@ +hostprogs-always-y += gendwarfksyms + +gendwarfksyms-objs += gendwarfksyms.o +gendwarfksyms-objs += dwarf.o + +HOST_EXTRACFLAGS := -I $(srctree)/tools/include +HOSTLDLIBS_gendwarfksyms := -ldw -lelf diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c new file mode 100644 index 000000000000..65a29d0bd8f4 --- /dev/null +++ b/scripts/gendwarfksyms/dwarf.c @@ -0,0 +1,87 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2024 Google LLC + */ + +#include "gendwarfksyms.h" + +/* + * Type string processing + */ +static int process(struct state *state, const char *s) +{ + s = s ?: ""; + + if (debug) + fputs(s, stderr); + + return 0; +} + +bool match_all(Dwarf_Die *die) +{ + return true; +} + +int process_die_container(struct state *state, Dwarf_Die *die, + die_callback_t func, die_match_callback_t match) +{ + Dwarf_Die current; + int res; + + res = checkp(dwarf_child(die, ¤t)); + while (!res) { + if (match(¤t)) + check(func(state, ¤t)); + res = checkp(dwarf_siblingof(¤t, ¤t)); + } + + return 0; +} + +/* + * Symbol processing + */ +static int process_subprogram(struct state *state, Dwarf_Die *die) +{ + return check(process(state, "subprogram;\n")); +} + +static int process_variable(struct state *state, Dwarf_Die *die) +{ + return check(process(state, "variable;\n")); +} + +static int process_exported_symbols(struct state *state, Dwarf_Die *die) +{ + int tag = dwarf_tag(die); + + switch (tag) { + /* Possible containers of exported symbols */ + case DW_TAG_namespace: + case DW_TAG_class_type: + case DW_TAG_structure_type: + return check(process_die_container( + state, die, process_exported_symbols, match_all)); + + /* Possible exported symbols */ + case DW_TAG_subprogram: + case DW_TAG_variable: + if (tag == DW_TAG_subprogram) + check(process_subprogram(state, die)); + else + check(process_variable(state, die)); + + return 0; + default: + return 0; + } +} + +int process_module(Dwfl_Module *mod, Dwarf *dbg, Dwarf_Die *cudie) +{ + struct state state = { .mod = mod, .dbg = dbg }; + + return check(process_die_container( + &state, cudie, process_exported_symbols, match_all)); +} diff --git a/scripts/gendwarfksyms/gendwarfksyms.c b/scripts/gendwarfksyms/gendwarfksyms.c new file mode 100644 index 000000000000..27f2d6423c45 --- /dev/null +++ b/scripts/gendwarfksyms/gendwarfksyms.c @@ -0,0 +1,146 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2024 Google LLC + */ + +#include +#include +#include +#include +#include +#include "gendwarfksyms.h" + +/* + * Options + */ + +/* Print out debugging information to stderr */ +bool debug; + +static const struct { + const char *arg; + bool *flag; + const char **param; +} options[] = { + { "--debug", &debug, NULL }, +}; + +static int usage(void) +{ + error("usage: gendwarfksyms [options] elf-object-file ..."); + return -1; +} + +static const char *object_files[MAX_INPUT_FILES]; +static unsigned int object_count; + +static int parse_options(int argc, const char **argv) +{ + for (int i = 1; i < argc; i++) { + bool flag = false; + + for (int j = 0; j < ARRAY_SIZE(options); j++) { + if (strcmp(argv[i], options[j].arg)) + continue; + + *options[j].flag = true; + + if (options[j].param) { + if (++i >= argc) { + error("%s needs an argument", + options[j].arg); + return -1; + } + + *options[j].param = argv[i]; + } + + flag = true; + break; + } + + if (!flag) + object_files[object_count++] = argv[i]; + } + + return object_count ? 0 : -1; +} + +static int process_modules(Dwfl_Module *mod, void **userdata, const char *name, + Dwarf_Addr base, void *arg) +{ + Dwarf_Addr dwbias; + Dwarf_Die cudie; + Dwarf_CU *cu = NULL; + Dwarf *dbg; + int res; + + debug("%s", name); + dbg = dwfl_module_getdwarf(mod, &dwbias); + + do { + res = dwarf_get_units(dbg, cu, &cu, NULL, NULL, &cudie, NULL); + if (res < 0) { + error("dwarf_get_units failed: no debugging information?"); + return -1; + } else if (res == 1) { + break; /* No more units */ + } + + check(process_module(mod, dbg, &cudie)); + } while (cu); + + return DWARF_CB_OK; +} + +static const Dwfl_Callbacks callbacks = { + .section_address = dwfl_offline_section_address, + .find_debuginfo = dwfl_standard_find_debuginfo, +}; + +int main(int argc, const char **argv) +{ + unsigned int n; + + if (parse_options(argc, argv) < 0) + return usage(); + + for (n = 0; n < object_count; n++) { + Dwfl *dwfl; + int fd; + + fd = open(object_files[n], O_RDONLY); + if (fd == -1) { + error("open failed for '%s': %s", object_files[n], + strerror(errno)); + return -1; + } + + dwfl = dwfl_begin(&callbacks); + if (!dwfl) { + error("dwfl_begin failed for '%s': %s", object_files[n], + dwarf_errmsg(-1)); + return -1; + } + + if (!dwfl_report_offline(dwfl, object_files[n], object_files[n], + fd)) { + error("dwfl_report_offline failed for '%s': %s", + object_files[n], dwarf_errmsg(-1)); + return -1; + } + + dwfl_report_end(dwfl, NULL, NULL); + + if (dwfl_getmodules(dwfl, &process_modules, NULL, 0)) { + error("dwfl_getmodules failed for '%s'", + object_files[n]); + return -1; + } + + dwfl_end(dwfl); + close(fd); + } + + return 0; +} diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h new file mode 100644 index 000000000000..5ab7ce7d4efb --- /dev/null +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -0,0 +1,78 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2024 Google LLC + */ + +#include +#include +#include +#include +#include +#include +#include + +#ifndef __GENDWARFKSYMS_H +#define __GENDWARFKSYMS_H + +/* + * Options -- in gendwarfksyms.c + */ +extern bool debug; + +#define MAX_INPUT_FILES 128 + +/* + * Output helpers + */ +#define __PREFIX "gendwarfksyms: " +#define __println(prefix, format, ...) \ + fprintf(stderr, prefix __PREFIX "%s: " format "\n", __func__, \ + ##__VA_ARGS__) + +#define debug(format, ...) \ + do { \ + if (debug) \ + __println("", format, ##__VA_ARGS__); \ + } while (0) + +#define warn(format, ...) __println("warning: ", format, ##__VA_ARGS__) +#define error(format, ...) __println("error: ", format, ##__VA_ARGS__) + +/* + * Error handling helpers + */ +#define __check(expr, test, rv) \ + ({ \ + int __res = expr; \ + if (test) { \ + error("`%s` failed: %d", #expr, __res); \ + return rv; \ + } \ + __res; \ + }) + +/* Error == non-zero values */ +#define check(expr) __check(expr, __res, -1) +/* Error == negative values */ +#define checkp(expr) __check(expr, __res < 0, __res) + +/* + * dwarf.c + */ + +struct state { + Dwfl_Module *mod; + Dwarf *dbg; +}; + +typedef int (*die_callback_t)(struct state *state, Dwarf_Die *die); +typedef bool (*die_match_callback_t)(Dwarf_Die *die); +extern bool match_all(Dwarf_Die *die); + +extern int process_die_container(struct state *state, Dwarf_Die *die, + die_callback_t func, + die_match_callback_t match); + +extern int process_module(Dwfl_Module *mod, Dwarf *dbg, Dwarf_Die *cudie); + +#endif /* __GENDWARFKSYMS_H */ From patchwork Thu Aug 15 17:39:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13765037 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 85FA752F70 for ; Thu, 15 Aug 2024 17:39:17 +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=1723743559; cv=none; b=Ihr0ASGQS0NFI+07UpIpNkXZNDHrAUd2/eUlAMvFt9IgA14ngdLXZmfrDuHDiR+U3JIqwhzbFN3KdWK0cBEbyaq3Ez/JYfNjkWSX8rsils8IQFiumZGThYouKpOrxL+KXLz/werGsNVt380dpJ+XFjQ75wZ5csd7Qe7DzfpoZwE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743559; c=relaxed/simple; bh=crG2z1QAVdcb8L1xI/ei1FGYqMQPcrm7Rlub8Vb9HSE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Bb60Md3N78e4cf14xkGBK4IVcO3a9zqwWXGztBBgoJ4A1cpCxxonV/n5bkLcJOEN/+ErO6ZqCKKF9VodFdPSiGlxO+Wawi575QHiFZtRNIlFfT+nhKvhMl/kJmdyMSXkxXR5bHa6UrOGFqwu/3pwv8YyZBJhFLi460oP2cC8X0o= 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=Rm0zeOK6; 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="Rm0zeOK6" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-1fc6db23c74so11072195ad.0 for ; Thu, 15 Aug 2024 10:39:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723743557; x=1724348357; 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=Uov0CRlxy4YygtcVstLoe0vAl7pu4tWa+P/EEpIdGls=; b=Rm0zeOK6+sBDP5UqQNzCWa8QyjYf5pTR5Amg4ZM7utjm8by5hJPtFOMAGgtVc6vyl8 0Oe8VUwD2e4DUwu0SmQ0PBBjv5reU0ytaE5y4ZmHeEpo0SUG/kyDkOPqNwD920G1c3M8 +QRmWyQJM1cMAJ5ZCv6fDffMS2rQRHhd/F5e/KcBfAnsYiSdVmrynsM/5bzVkXKSpYUF 6qNWjYcLToR5QbsGsZyAqbIOE32BaEnfxfnyHob88Tn3IS0pM7jm/7S5JXwLbbg1u0K+ Naq+cEftef3I1g5OevGcRMtJgd0DsObzYOpHaono+QK1CzrHwarT1C3c3N30GfpVBGAJ HXwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723743557; x=1724348357; 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=Uov0CRlxy4YygtcVstLoe0vAl7pu4tWa+P/EEpIdGls=; b=PkzKfPqrCBUrauzRnTcy2OGuEQS22BZAN0G9XWXz+Pdzf38tzRAsDF6k+s4Yb87kPq q8USGGPMcMg5HFhPiJydW+g45eCjLNvVbn2XzpuJVeazz3ZZlDomBqEME/iQ67DRe2zc 7tjzWtDFsmNZQnOGOcb+WBxlNxrIGgdQVYPxed/ktBlonKLr1Ek7Knu/FYEEBtgXpGwd qEMh0xb+pQ+ZugSYM91bu0dJjHtC5l2VufUubiPIlaG0qpMBwCPSrGrXkr4wzc/SfFng RgJXtrceGvTMsigWYouqOgzhB9jIJu0BpHhlo0BAgzS1ZWKpgEBm3YySAo8PxbLSFFVC y7Pw== X-Forwarded-Encrypted: i=1; AJvYcCVrHn6WUbJNPr6RlGEIPFV6OPguiKtajOoQoJWQVmzmqVSpiJT0rOdgFblWi0Or7eSykmqeQ7gP0CDlh7N4HmcSrd8SVeHszyBF7DKp X-Gm-Message-State: AOJu0YxfGQE9Cui4M64ls/pW21sl7tEFYgB57g1hQdMxorGmFyHaZNNX goGFsB5SHJ6g33a+jC8TfSE92XKq1TX18LeRdtx5n21a6lffp/ypuiYzI8jD4iK7x9RTLObN49i s3mxuNekNa+gPaade80snKOGnCg== X-Google-Smtp-Source: AGHT+IEwcQof9vpv5owFCL5xlAGrgWbmahy7qEGDiupJWRGzXkesK5qX16rh6dI20h9d+J+ztNbd8gVLvvi1kFjdtlI= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a17:903:247:b0:1fb:b3f:b9bf with SMTP id d9443c01a7336-20203b1111bmr437945ad.0.1723743556647; Thu, 15 Aug 2024 10:39:16 -0700 (PDT) Date: Thu, 15 Aug 2024 17:39:06 +0000 In-Reply-To: <20240815173903.4172139-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240815173903.4172139-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=7173; i=samitolvanen@google.com; h=from:subject; bh=crG2z1QAVdcb8L1xI/ei1FGYqMQPcrm7Rlub8Vb9HSE=; b=owEB7QES/pANAwAKAUy19oSLvFbuAcsmYgBmvj03h2SYn2IwipBF/1mhMjbft2puh5x1VQQtr hR01w5wx0KJAbMEAAEKAB0WIQQ1zPtjsoPW0663g5RMtfaEi7xW7gUCZr49NwAKCRBMtfaEi7xW 7pkjDACi1TmSidtdqQ3U+ev+m7j+aMyKSXe/Gpe/+wqmXzcKHpCrZV34O2WWIa2Bc5hGXypNm5V eHNXgPkLe+AXEwwgUwoaNU6rKfBCZAjmXHdVbOCdBphz+xu28FY0HSJFKjnoLw/xtSVaAbbLCAh I1yCgQBj4gqzMfYqha8ctveVZlw5W7dtQXhaTgUj+rGYPR2qgIEXDPW7eWVw7HfekSyLrVYWwBz L4Bfw+K81fUy1g/72UJla2QO91v4M3ifIHNPljAAHPYCqtMyJ5T6vuF0t15ZXozltgqNputiGft wzaMoje6OXefn1A5egVjauNrv2nlkM2kyYjVjUbUlOl3oxJ8dPad2cgyOloqrGjprhXXACyIoq3 JwfZG9i21qdpGPHhuYOoA4V/hYTf5MYMciXTqktnc+zxNv7HmjeVp7JIVRCO8z6iWwvfPmPTYuP SB4eGS+06dcO2UxmaFoWsIn1Zfgn4z1wulHhu0jABiCD7+q8lRLkSqsejCA2m1Dv41neQ= X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240815173903.4172139-23-samitolvanen@google.com> Subject: [PATCH v2 02/19] gendwarfksyms: Add symbol list handling From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Wedson Almeida Filho , Gary Guo , Petr Pavlu , Neal Gompa , Hector Martin , Janne Grunau , Asahi Linux , 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 passing a list of exported symbols to gendwarfksyms via stdin and filter out non-exported symbols from the output. Signed-off-by: Sami Tolvanen --- scripts/gendwarfksyms/Makefile | 1 + scripts/gendwarfksyms/dwarf.c | 53 ++++++++++++++- scripts/gendwarfksyms/gendwarfksyms.c | 4 +- scripts/gendwarfksyms/gendwarfksyms.h | 21 ++++++ scripts/gendwarfksyms/symbols.c | 96 +++++++++++++++++++++++++++ 5 files changed, 171 insertions(+), 4 deletions(-) create mode 100644 scripts/gendwarfksyms/symbols.c diff --git a/scripts/gendwarfksyms/Makefile b/scripts/gendwarfksyms/Makefile index c1389c161f9c..623f8fc975ea 100644 --- a/scripts/gendwarfksyms/Makefile +++ b/scripts/gendwarfksyms/Makefile @@ -2,6 +2,7 @@ hostprogs-always-y += gendwarfksyms gendwarfksyms-objs += gendwarfksyms.o gendwarfksyms-objs += dwarf.o +gendwarfksyms-objs += symbols.o HOST_EXTRACFLAGS := -I $(srctree)/tools/include HOSTLDLIBS_gendwarfksyms := -ldw -lelf diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index 65a29d0bd8f4..71cfab0553da 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -5,6 +5,48 @@ #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); +} + +static const char *get_name(Dwarf_Die *die) +{ + Dwarf_Attribute attr; + + /* rustc uses DW_AT_linkage_name for exported symbols */ + if (dwarf_attr(die, DW_AT_linkage_name, &attr) || + dwarf_attr(die, DW_AT_name, &attr)) { + return dwarf_formstring(&attr); + } + + return NULL; +} + +static bool is_export_symbol(struct state *state, Dwarf_Die *die) +{ + Dwarf_Die *source = die; + Dwarf_Die origin; + + state->sym = NULL; + + /* 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_name(die)); + + /* Look up using the origin name if there are no matches. */ + if (!state->sym && source != die) + state->sym = symbol_get(get_name(source)); + + state->die = *source; + return !!state->sym; +} + /* * Type string processing */ @@ -40,7 +82,7 @@ int process_die_container(struct state *state, Dwarf_Die *die, } /* - * Symbol processing + * Exported symbol processing */ static int process_subprogram(struct state *state, Dwarf_Die *die) { @@ -67,10 +109,15 @@ static int process_exported_symbols(struct state *state, Dwarf_Die *die) /* Possible exported symbols */ case DW_TAG_subprogram: case DW_TAG_variable: + if (!is_export_symbol(state, die)) + return 0; + + debug("%s", state->sym->name); + if (tag == DW_TAG_subprogram) - check(process_subprogram(state, die)); + check(process_subprogram(state, &state->die)); else - check(process_variable(state, die)); + check(process_variable(state, &state->die)); return 0; default: diff --git a/scripts/gendwarfksyms/gendwarfksyms.c b/scripts/gendwarfksyms/gendwarfksyms.c index 27f2d6423c45..d209b237766b 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.c +++ b/scripts/gendwarfksyms/gendwarfksyms.c @@ -27,7 +27,7 @@ static const struct { static int usage(void) { - error("usage: gendwarfksyms [options] elf-object-file ..."); + error("usage: gendwarfksyms [options] elf-object-file ... < symbol-list"); return -1; } @@ -105,6 +105,8 @@ int main(int argc, const char **argv) if (parse_options(argc, argv) < 0) return usage(); + check(symbol_read_exports(stdin)); + for (n = 0; n < object_count; n++) { Dwfl *dwfl; int fd; diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h index 5ab7ce7d4efb..03f3e408a839 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -7,9 +7,11 @@ #include #include #include +#include #include #include #include +#include #ifndef __GENDWARFKSYMS_H #define __GENDWARFKSYMS_H @@ -56,6 +58,23 @@ extern bool debug; /* Error == negative values */ #define checkp(expr) __check(expr, __res < 0, __res) +/* + * symbols.c + */ + +static inline u32 name_hash(const char *name) +{ + return jhash(name, strlen(name), 0); +} + +struct symbol { + const char *name; + struct hlist_node name_hash; +}; + +extern int symbol_read_exports(FILE *file); +extern struct symbol *symbol_get(const char *name); + /* * dwarf.c */ @@ -63,6 +82,8 @@ extern bool debug; struct state { Dwfl_Module *mod; Dwarf *dbg; + struct symbol *sym; + Dwarf_Die die; }; typedef int (*die_callback_t)(struct state *state, Dwarf_Die *die); diff --git a/scripts/gendwarfksyms/symbols.c b/scripts/gendwarfksyms/symbols.c new file mode 100644 index 000000000000..673ad9cf9e77 --- /dev/null +++ b/scripts/gendwarfksyms/symbols.c @@ -0,0 +1,96 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2024 Google LLC + */ + +#include "gendwarfksyms.h" + +#define SYMBOL_HASH_BITS 15 +static DEFINE_HASHTABLE(symbol_names, SYMBOL_HASH_BITS); + +typedef int (*symbol_callback_t)(struct symbol *, void *arg); + +static 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, + name_hash(name)) { + if (strcmp(match->name, name)) + continue; + + if (func) + check(func(match, data)); + + return 1; + } + + return 0; +} + +static bool is_exported(const char *name) +{ + return checkp(for_each(name, NULL, NULL)) > 0; +} + +int 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); + return -1; + } + + free(line); + line = NULL; + + if (is_exported(name)) + continue; /* Ignore duplicates */ + + sym = malloc(sizeof(struct symbol)); + if (!sym) { + error("malloc failed"); + return -1; + } + + sym->name = name; + name = NULL; + + hash_add(symbol_names, &sym->name_hash, name_hash(sym->name)); + ++nsym; + + debug("%s", sym->name); + } + + if (line) + free(line); + + debug("%d exported symbols", nsym); + return 0; +} + +static int get_symbol(struct symbol *sym, void *arg) +{ + struct symbol **res = arg; + + *res = sym; + return 0; +} + +struct symbol *symbol_get(const char *name) +{ + struct symbol *sym = NULL; + + for_each(name, get_symbol, &sym); + return sym; +} From patchwork Thu Aug 15 17:39:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13765038 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.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 6324D57CBC for ; Thu, 15 Aug 2024 17:39:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743562; cv=none; b=u8HsWcXjfu3hmmaG5mv37OWG1wm5ykKq/s/dWFuQp4ZQSWOmaLUAPI2YO03L3eX9a6xNPg25hKXbdP5o04x2FDpOIOBUFctVHPIZc1hZrj98x+1mhDKqsCWZkm0EUDj0dbWTlT12pdHiQlkdk9hS3iZlp1Bgd4qzokw2ybyG7Cg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743562; c=relaxed/simple; bh=hYmG5yt01O2vbqwBp7PP3hIYMSvbeyeHaHO3a1wFkH4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ECAK3NYvXQdSkcmL2xkE6oe3JDXwEPM0OaCJ5Zg+8bP4ksa5uziGBsLGj9PrlEWYYLp6SwxQ/+22cpJZjZz+jrCoLXQFo16g+ILjMDPlkAHwE16ngaVAn6Ou4jmjBHQof/qE7eZPfxvSyqkbHGBTWOWKYf7a954lXH2E0/7iX8o= 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=eGKc6Vog; arc=none smtp.client-ip=209.85.214.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="eGKc6Vog" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-201f89c6b21so6235635ad.1 for ; Thu, 15 Aug 2024 10:39:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723743559; x=1724348359; 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=UpexM9fykb8F2roek5WqlkwH0rKydnPc1uN1TQABN3Y=; b=eGKc6Vog3OywseZSr1HEFNxiFjUrLp3c4pbrYRipDPGFLP99uAibv8pusDk+HRK5Kk VvnwrAeLYzi3o6fZABwZkMsLNg2B+rTqV3CnM3/jeMgr33laDv/FVefWEFJ+5E0FF8D2 VKOEYnrWjPQvOj16DLynyrhNsjKqIYyNinBHVFpIVwZ43GjzKFES+XW0Q9x2pvUIvoVZ qAbbqMMeKQFx7iwd2utWiab4m/VTC3XGvJeIldWIZIRgpQcKQRghWrFDDb3rzz7W0gMW yK5kYpL86n/T+mlZlIutEZmmZ8OXvG1EW+t73UKGf8QOx+r9y/73CNC3wmE7krlh36S+ i/WA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723743559; x=1724348359; 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=UpexM9fykb8F2roek5WqlkwH0rKydnPc1uN1TQABN3Y=; b=Lywqi5LMz/mcYJcLg0RZGmENA2SXTP0ql/mQ4UxAFkZSGKMpPk2aGnAsh3jHjYv9Ef GoPpT5/PrNt88g/TwTioLIEzauwOyplaD4rBtKjBEwxHZ2SWCgMgZAYr5vhWOqxGa4s9 ycHlkNkg9505rqUkRYX8BD0q2DGLPsE5SAKyaO1dShbbHH16OUPQngPqXtEw/8CR8FR2 4ejim3pSzZ7ovp+ub006j1kyr7ITc2ZMqIzcer4Sl+0UEQjaKZSelXnTpMWYfuv2eqkU P03gzZ/4dD1Zb9ek08uETUYeF+s6WqZlAgepv5tOkJoNlr6iZh4comttjPFyKpzEzCew 8Y+A== X-Forwarded-Encrypted: i=1; AJvYcCWemZEfYKM8I5Z07nigafjA1fmvt0Bxo8KoT2I8PXdazWHlnyrfsF1FJ6iaVS0PNYmWIbaeP8ii3gAyUYNRX5jRchw2l6qzeQwa1jji X-Gm-Message-State: AOJu0Yy7/2f8o5XtXm7Oqjs7ZCfiBzXB6UQA1zJB0QNKa4pRndmRFsFD diT+3aVgUJn4UUpQNg2WDQbRYz7QqiVumUhqbnRIxgSjST6t39AoTla91rIU6rS8KmMXhHn6bb0 RZQvmPivB9N1GrBJO+FytiaBnwA== X-Google-Smtp-Source: AGHT+IH8/3eOMWCOeClNJqRJ6nF99J4r8CyoyyKEWH8ahJS3xT9dj2SLUFFnGqAYYgcxxGF2/tmYaOSqjFRejEAGQiU= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a17:902:f684:b0:1fb:56c8:f273 with SMTP id d9443c01a7336-20203e4798fmr440195ad.1.1723743558559; Thu, 15 Aug 2024 10:39:18 -0700 (PDT) Date: Thu, 15 Aug 2024 17:39:07 +0000 In-Reply-To: <20240815173903.4172139-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240815173903.4172139-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=7437; i=samitolvanen@google.com; h=from:subject; bh=hYmG5yt01O2vbqwBp7PP3hIYMSvbeyeHaHO3a1wFkH4=; b=owEB7QES/pANAwAKAUy19oSLvFbuAcsmYgBmvj04hD1u8vcutuUsraMRLPBZY8D62QWQnLdTw Jmu88k1VrCJAbMEAAEKAB0WIQQ1zPtjsoPW0663g5RMtfaEi7xW7gUCZr49OAAKCRBMtfaEi7xW 7vnwC/0RM0rg3Pr6UU/JjzL9sven5E/iF8J23JuqnOnrRU6yIssP2YfjaFvY4Z5EpUrgu+Zob9l u4Yh5xNbzYC1za3vo82V7VrxdIaqxvyTRCO28WPx7kdxN3xB3TB7n9VYX7d9IVQSUkbWA9sQwAL /icm/sXgV+lgdaje+mEKeguw8SK2fDl7OVoFQTWJZpC3fT62nqXGJDpgzOn4Gp865rhE2LDESS6 oJsM0/4qwF0OPMt2ELjCeHyVurVJKEHb1n/y783to+tBmM0C9CD7DcsJcAVZMbBnyRWPl3QwXyd lRrEgXfY9+Pr6TBaQJ9kebo61jHAXN71vlI9QPP2A3hHWOx4CDJ4a7kz27ni1htLW+YFr3BSsSe xI4Ta/8cUU1HPbjRLRTxV91taWgOx99uRp8+/6x1QgB6tBqZ2yU/M24ZLdZKkoDzN8OJpZu3Dk9 DdzXP8jwRSdrICESpYLvbVgjdtBDemlJ3R+xQJQ4JX59ljF1EUd5g4s2WWYZ+mivYw5T0= X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240815173903.4172139-24-samitolvanen@google.com> Subject: [PATCH v2 03/19] gendwarfksyms: Add address matching From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Wedson Almeida Filho , Gary Guo , Petr Pavlu , Neal Gompa , Hector Martin , Janne Grunau , Asahi Linux , 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 --- scripts/gendwarfksyms/gendwarfksyms.c | 2 + scripts/gendwarfksyms/gendwarfksyms.h | 7 ++ scripts/gendwarfksyms/symbols.c | 161 +++++++++++++++++++++++++- 3 files changed, 165 insertions(+), 5 deletions(-) diff --git a/scripts/gendwarfksyms/gendwarfksyms.c b/scripts/gendwarfksyms/gendwarfksyms.c index d209b237766b..e2f8ee5a4bf3 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.c +++ b/scripts/gendwarfksyms/gendwarfksyms.c @@ -118,6 +118,8 @@ int main(int argc, const char **argv) return -1; } + check(symbol_read_symtab(fd)); + dwfl = dwfl_begin(&callbacks); if (!dwfl) { error("dwfl_begin failed for '%s': %s", object_files[n], diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h index 03f3e408a839..cb9106dfddb9 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -61,6 +61,10 @@ extern bool debug; /* * symbols.c */ +struct symbol_addr { + uint32_t section; + Elf64_Addr address; +}; static inline u32 name_hash(const char *name) { @@ -69,10 +73,13 @@ static inline u32 name_hash(const char *name) struct symbol { const char *name; + struct symbol_addr addr; + struct hlist_node addr_hash; struct hlist_node name_hash; }; extern int symbol_read_exports(FILE *file); +extern int symbol_read_symtab(int fd); extern struct symbol *symbol_get(const char *name); /* diff --git a/scripts/gendwarfksyms/symbols.c b/scripts/gendwarfksyms/symbols.c index 673ad9cf9e77..f96acb941196 100644 --- a/scripts/gendwarfksyms/symbols.c +++ b/scripts/gendwarfksyms/symbols.c @@ -6,11 +6,43 @@ #include "gendwarfksyms.h" #define SYMBOL_HASH_BITS 15 + +/* struct symbol_addr -> struct symbol */ +static DEFINE_HASHTABLE(symbol_addrs, SYMBOL_HASH_BITS); +/* name -> struct symbol */ static DEFINE_HASHTABLE(symbol_names, SYMBOL_HASH_BITS); +static inline u32 symbol_addr_hash(const struct symbol_addr *addr) +{ + return jhash(addr, sizeof(struct symbol_addr), 0); +} + typedef int (*symbol_callback_t)(struct symbol *, void *arg); -static int for_each(const char *name, symbol_callback_t func, void *data) +static int __for_each_addr(struct symbol *sym, symbol_callback_t func, + void *data) +{ + struct hlist_node *tmp; + struct symbol *match = NULL; + 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) { + check(func(match, data)); + ++processed; + } + } + + return processed; +} + +static int for_each(const char *name, bool name_only, symbol_callback_t func, + void *data) { struct hlist_node *tmp; struct symbol *match; @@ -23,9 +55,13 @@ static int for_each(const char *name, symbol_callback_t func, void *data) if (strcmp(match->name, name)) continue; + /* Call func for the match, and all address matches */ if (func) check(func(match, data)); + if (!name_only && match->addr.section != SHN_UNDEF) + return checkp(__for_each_addr(match, func, data)) + 1; + return 1; } @@ -34,7 +70,7 @@ static int for_each(const char *name, symbol_callback_t func, void *data) static bool is_exported(const char *name) { - return checkp(for_each(name, NULL, NULL)) > 0; + return checkp(for_each(name, true, NULL, NULL)) > 0; } int symbol_read_exports(FILE *file) @@ -57,13 +93,14 @@ int symbol_read_exports(FILE *file) if (is_exported(name)) continue; /* Ignore duplicates */ - sym = malloc(sizeof(struct symbol)); + sym = calloc(1, sizeof(struct symbol)); if (!sym) { - error("malloc failed"); + error("calloc failed"); return -1; } sym->name = name; + sym->addr.section = SHN_UNDEF; name = NULL; hash_add(symbol_names, &sym->name_hash, name_hash(sym->name)); @@ -91,6 +128,120 @@ struct symbol *symbol_get(const char *name) { struct symbol *sym = NULL; - for_each(name, get_symbol, &sym); + for_each(name, false, get_symbol, &sym); return sym; } + +typedef int (*elf_symbol_callback_t)(const char *name, GElf_Sym *sym, + Elf32_Word xndx, void *arg); + +static int elf_for_each_symbol(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)); + return -1; + } + + elf = elf_begin(fd, ELF_C_READ_MMAP, NULL); + if (!elf) { + error("elf_begin failed: %s", elf_errmsg(-1)); + return -1; + } + + sym_size = gelf_getclass(elf) == ELFCLASS32 ? sizeof(Elf32_Sym) : + sizeof(Elf64_Sym); + + scn = elf_nextscn(elf, NULL); + + while (scn) { + shdr = gelf_getshdr(scn, &shdr_mem); + + if (shdr && shdr->sh_type == SHT_SYMTAB_SHNDX) { + xndx_data = elf_getdata(scn, NULL); + break; + } + + scn = elf_nextscn(elf, scn); + } + + scn = elf_nextscn(elf, NULL); + + while (scn) { + shdr = gelf_getshdr(scn, &shdr_mem); + + if (shdr && shdr->sh_type == SHT_SYMTAB) { + Elf_Data *data = elf_getdata(scn, NULL); + unsigned int nsyms = data->d_size / sym_size; + unsigned int n; + + for (n = 0; 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->st_shndx != SHN_XINDEX) + xndx = sym->st_shndx; + + name = elf_strptr(elf, shdr->sh_link, + sym->st_name); + + /* Skip empty symbol names */ + if (name && *name && + checkp(func(name, sym, xndx, arg)) > 0) + break; + } + } + + scn = elf_nextscn(elf, scn); + } + + return check(elf_end(elf)); +} + +static int set_symbol_addr(struct symbol *sym, void *arg) +{ + struct symbol_addr *addr = arg; + + if (sym->addr.section == SHN_UNDEF) { + sym->addr.section = addr->section; + sym->addr.address = addr->address; + 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 { + warn("multiple addresses for symbol %s?", sym->name); + } + + return 0; +} + +static int process_symbol(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 (GELF_ST_BIND(sym->st_info) != STB_LOCAL && + addr.section != SHN_UNDEF) + checkp(for_each(name, true, set_symbol_addr, &addr)); + + return 0; +} + +int symbol_read_symtab(int fd) +{ + return elf_for_each_symbol(fd, process_symbol, NULL); +} From patchwork Thu Aug 15 17:39:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13765039 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.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 070D6757EB for ; Thu, 15 Aug 2024 17:39:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743563; cv=none; b=UEBfDO7gbYmIfIwbwaoiFXAhrZdbpwP1KsKtNxbk9xvfIRfhDhJ3esNsUqkP8AEm0Ew8SbGgbpfLGeaUJf4VmF5O6OjnZkN831i/AZGV40hC0ATWhZBhuNNbrwuc2oqks2r1S7h1tD2e6RQUHkOUw5ty//tq+yUC1qaAmtjxMqE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743563; c=relaxed/simple; bh=Ahmj2O3IR+LSl3BiA52xKx3B/l6T0r/Fo4+Nt8cR26I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sOwoMxOSuwP4tidI06d1RCDyBpeFfbC6aBnhxmESwUASpe5RoPKD8UOgDSCHb2kGm8Z7ybbU8RauOfk2mPsA2suQQrvlgP10f6Mx6+A6ULApDQ2i0lmV0dNKjG9t2eJPn9SHX0bbHL1fYP1iqrN+uuEQ7tcSFq6RvBRI1X+5VhA= 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=mYqj+2oP; arc=none smtp.client-ip=209.85.215.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="mYqj+2oP" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-7bc8bb2f6f5so994693a12.0 for ; Thu, 15 Aug 2024 10:39:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723743560; x=1724348360; 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=mLQlJKBqT4/VzYmf9VfZ8u93RL60RGH7yZrLYBcRMXE=; b=mYqj+2oPpG6Dfn0tdKv50rvipmR1lNvUZEW/bFDN2nRGxkano25ikQ98iSZhDpZQE7 9c1Swuj+Py6hGLL5HvLEZ0ZyXnUWuKEfkQx7/sSedxaJ5touR5pf1ljRLh043hHtFemj juOM/4gOJmXb+02yEPRbA4+A/4LgevSjeAR2wQf4dRVQkyv6f2jVWb70l4qW2gM0wwvY fKm+iKNNHMsA8H5L/vPvp7j+TtZ4dLQ/T1but4MWqCFrfPogy5yX/XcDfazMBfLtloTg HjtiE8UtqaYOnGKk9gAv60kQVQZdNjxXDzmMiwW7RDMs8Q8/pmmw+QJFWjg+G/0GgqXx GjKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723743560; x=1724348360; 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=mLQlJKBqT4/VzYmf9VfZ8u93RL60RGH7yZrLYBcRMXE=; b=fQfUfyedbDeruCTiy2mGwA34ZmigUmZhf097Vaz87bxDvu3EENID8X5htv9hEThpxK kzLD2VvCyb5N6A1nmu0RmqMv8S5TrZnEv7qWwWi8dNSKcdFHQJTC787ZsnEjTerC1mah s338iFyDbvVApfWZbTVyXa+aJ1dmry4gQfJfnnAs6jYCQrodm7nfnoyj0wJYDkMyeLl/ 6vKoh03/om7MMjtC8l01LN9cha47mKaQxVkPWAWeFYzj8NH49YV26OxCVz+2uS3cl2kD iFBTS87nJYziz38nPo14NkDjJRgP35HETojSH/LsiXduaVswHWwoJ+o2Czr18vnXD0NX /XQQ== X-Forwarded-Encrypted: i=1; AJvYcCWOIVUv3QuDOz4dYdFOjbq+VxrQ2y7FrC4K0qVg6Driqe9oVUT/Ym+v16j1dsYEpg5cVW2QUq50vO6j9TsZEHdUkEDwQoSynKJIsNp+ X-Gm-Message-State: AOJu0YziZv0Nucl/bqABoEnz+QPNrLMCUQCeHvIFOTJve7VASYmE9r/V tLaOiujW5Lh7QMZmm9ECZ8+fxiPOMVOZTYMk5VqmzYxtxDr9oY5zNrtaK2XjUOqjQbVEN7Elh+O YzbQMyXaj5XjOmpZHyOeszL5oWA== X-Google-Smtp-Source: AGHT+IE1twvMiJGv9A9PF3ykXAMYl/52O8nOi2sdu4GF15yrKLi7qKJGQ8IRVIMXcNpJkBrcc/J9f+3K3535R6ZKioE= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a63:5ec5:0:b0:760:76b9:ade9 with SMTP id 41be03b00d2f7-7c9791e4e77mr64a12.1.1723743560144; Thu, 15 Aug 2024 10:39:20 -0700 (PDT) Date: Thu, 15 Aug 2024 17:39:08 +0000 In-Reply-To: <20240815173903.4172139-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240815173903.4172139-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=4474; i=samitolvanen@google.com; h=from:subject; bh=Ahmj2O3IR+LSl3BiA52xKx3B/l6T0r/Fo4+Nt8cR26I=; b=owEB7QES/pANAwAKAUy19oSLvFbuAcsmYgBmvj04aOj8csDviqueJ1eEjKR7eZPwgInAwuFoD 5HAQqspMuCJAbMEAAEKAB0WIQQ1zPtjsoPW0663g5RMtfaEi7xW7gUCZr49OAAKCRBMtfaEi7xW 7lcUC/9O1P8fFLBUCeOT4Cxm+rBpUW5+73RlFMMG8H+bGc4TSk35d91Su3NehrxYKKU/pT/PVPk DjVOLzLc8vrfvwl1Nt2wQ3EvSJwZZ2eqYER+zrUZkPmdpOjiOYowuBi+VOGnihjQZx8uQsuLUt7 zhIE6Y174Min92jA8j8LDjo3zgA5McorGyWANE2Ma2NOssLU4RB3vC9Cv2K0cJXR5QuckfadlW/ H8vQ6BvRvS05m2JGRSjmSdBZFGPGjJQ6ED/BTajeja7EOvAH4yaTiejmibxBkkf24sQYGkJtIFd 9JRGxIuuBDftKiPVQh0Oy5oK7Bp85TJ10jeczHneuITdV9P5glF6Y93MaLFT/zfIHRIna8eMMht pCs8UVnoc1wLnWExqcs3Q+pdqBXsMgfk0hGBoCKsZS1ot/5/0WRepl1NmMVX+hWHaY52dKgGU/K pA+68yDFF6TmIwfEZNO90AOtKj3WGCZWGirLJ7+tlFKpTSC07C7XYKELv14DkFgF8HGFQ= X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240815173903.4172139-25-samitolvanen@google.com> Subject: [PATCH v2 04/19] gendwarfksyms: Add support for type pointers From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Wedson Almeida Filho , Gary Guo , Petr Pavlu , Neal Gompa , Hector Martin , Janne Grunau , Asahi Linux , 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 --- scripts/gendwarfksyms/dwarf.c | 26 +++++++++++++++++++++++++- scripts/gendwarfksyms/gendwarfksyms.h | 6 ++++++ scripts/gendwarfksyms/symbols.c | 16 ++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index 71cfab0553da..956b30224316 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -94,6 +94,28 @@ static int process_variable(struct state *state, Dwarf_Die *die) return check(process(state, "variable;\n")); } +static int process_symbol_ptr(struct state *state, Dwarf_Die *die) +{ + Dwarf_Die ptr_type; + Dwarf_Die type; + + if (!get_ref_die_attr(die, DW_AT_type, &ptr_type) || + dwarf_tag(&ptr_type) != DW_TAG_pointer_type) { + error("%s must be a pointer type!", get_name(die)); + return -1; + } + + if (!get_ref_die_attr(&ptr_type, DW_AT_type, &type)) { + error("%s pointer missing a type attribute?", get_name(die)); + return -1; + } + + if (dwarf_tag(&type) == DW_TAG_subroutine_type) + return check(process_subprogram(state, &type)); + else + return check(process_variable(state, &ptr_type)); +} + static int process_exported_symbols(struct state *state, Dwarf_Die *die) { int tag = dwarf_tag(die); @@ -114,7 +136,9 @@ static int process_exported_symbols(struct state *state, Dwarf_Die *die) debug("%s", state->sym->name); - if (tag == DW_TAG_subprogram) + if (is_symbol_ptr(get_name(&state->die))) + check(process_symbol_ptr(state, &state->die)); + else if (tag == DW_TAG_subprogram) check(process_subprogram(state, &state->die)); else check(process_variable(state, &state->die)); diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h index cb9106dfddb9..8f6acd1b8f8f 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -61,6 +61,11 @@ extern bool debug; /* * symbols.c */ + +/* See symbols.c:is_symbol_ptr */ +#define SYMBOL_PTR_PREFIX "__gendwarfksyms_ptr_" +#define SYMBOL_PTR_PREFIX_LEN (sizeof(SYMBOL_PTR_PREFIX) - 1) + struct symbol_addr { uint32_t section; Elf64_Addr address; @@ -78,6 +83,7 @@ struct symbol { struct hlist_node name_hash; }; +extern bool is_symbol_ptr(const char *name); extern int symbol_read_exports(FILE *file); extern int symbol_read_symtab(int fd); extern struct symbol *symbol_get(const char *name); diff --git a/scripts/gendwarfksyms/symbols.c b/scripts/gendwarfksyms/symbols.c index f96acb941196..d6d016458ae1 100644 --- a/scripts/gendwarfksyms/symbols.c +++ b/scripts/gendwarfksyms/symbols.c @@ -41,6 +41,20 @@ static 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 int for_each(const char *name, bool name_only, symbol_callback_t func, void *data) { @@ -49,6 +63,8 @@ static int for_each(const char *name, bool name_only, 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, name_hash(name)) { From patchwork Thu Aug 15 17:39:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13765040 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 D1A6F74402 for ; Thu, 15 Aug 2024 17:39:22 +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=1723743564; cv=none; b=bM722aN3r4GuVvvujY/p/Ih0b2qsk0q90dY1+RAshxE4Wo3PhuYlbWmMP6Son0ByBpIlqGREhI3vaz6ldncS+dq1U6JIK1RwRbD3c6sRukoI946oq0gUqcpaB/3DoO+wwAxHDb3yE5OZ8FQ94NArpik/HaWVnLeTyFdpXd8IP1c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743564; c=relaxed/simple; bh=wAhZomTPmGa8ryvnda7uC8r92v4iCWnU4YEmuivMqjE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=CUP/GvLso7xFTL2SdmPCJ3sxHUIZxG6ALaSKgUy1SRboEbgut5ACZjwZsZmb+1KDn0Xhr2OdaIyP3g96fZvhd6eg0g3+Me7olX3iu6mPEF5cyIVB8DMD+EhBVyo1pugmd6k64vnCu3+fCeHa3Lbn4P720xIQKaGfnderQJIpFvQ= 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=ixZkCAkd; 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="ixZkCAkd" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-68d1d966ef7so20116117b3.1 for ; Thu, 15 Aug 2024 10:39:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723743562; x=1724348362; 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=zxg9NiKzqD/TwsM/Agf4zY5teCnPcigN7w5EkB7vCvE=; b=ixZkCAkdyJ4ZF6Ij6tAB5MLlutGqZgqAW39/NJ8zXj88XLzZLC47nKFLnCjOhmoUT9 /OWcmtzRmyO5Tqlc0M0+cko6A8inJqT3GKdQTXkv2LAgVtC35RRexnKpueeGOPFuNF36 Zq0B1J41Ho0y9M59t3HpsjEmw2kguoEliOrej6XlTFmmttb9yasRYy+FkHzyty8tiJ9B fwjBx8XDrf5wbl0SP3h3HRRNgqtpmSEOeHkAWViRrYDNiL7rlT74ZwmefGMf6avc8aDi kWZ8fm1x2RDZc2wZlGVWdJxpykgS9St6zlZLGQOD1qV1UaKpTvJ96RkJVyZNJf9LbFMX xDdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723743562; x=1724348362; 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=zxg9NiKzqD/TwsM/Agf4zY5teCnPcigN7w5EkB7vCvE=; b=meKEDz2WCae8BYTkMZD65jrxgzkiYiJyP033rFZHg4wzG3bFUWBgfJTL3i2i32zM0P R+qQswHwnFWyv7/+43HaGxuanm4hIAolxdB/bYPyK9J61pIBgWGGKl7hIeC9l2jdDD2M GTK4720TJBIcNCb3FJpdMNUdwA8lJyTXNUzq46XHOYldi3ppxIJRi2WQsXHiS246UWKI Z8L35CWQXvmxiC24CX7XLzK7xdTwuk9mEnHprIJPdII0gHrX7WQDTihTo+qal2QYfLrX mYLBrNnK32VplxNkFmAJU5l3ZzXnDL1J0plmDuYLEh0ZfWuua0NtbMCOXkZmcp/k9ibM CAeg== X-Forwarded-Encrypted: i=1; AJvYcCUq1WQjrxbWHGZXphzscIMGu7YBQ2oNyhOWR6r6LTU8jb3N/mdv8u+E++k5Z2l1lMVLZkiSLHycLlcUPfgfEr46sHm5ATPZQHI+7Gq3 X-Gm-Message-State: AOJu0YxMoVVvVIg4SG3A9P2Z8chtVwbtd8fy9yJ5Ly99lXYtLwStktmY czWj2odvOndy+gMh8rYtkPHS7Xp9h2j+8dsUeD4bDChgbSbeqUGOMuJVODXKi/hmS//REPHpS8m 35jOcMcb307K8Dwbsh5m7K1uYnQ== X-Google-Smtp-Source: AGHT+IEm2CkybTf4Spyo/MEFaqpksIWJ+D50fZxqYSU9rkny7yJCVb9f0Vx4fBHM5Be0k5Bc4nMPKMdD8tOzCxicIIM= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a81:b049:0:b0:62c:67f4:4f5 with SMTP id 00721157ae682-6b1bc3f9536mr38907b3.9.1723743562048; Thu, 15 Aug 2024 10:39:22 -0700 (PDT) Date: Thu, 15 Aug 2024 17:39:09 +0000 In-Reply-To: <20240815173903.4172139-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240815173903.4172139-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=4989; i=samitolvanen@google.com; h=from:subject; bh=wAhZomTPmGa8ryvnda7uC8r92v4iCWnU4YEmuivMqjE=; b=owEB7QES/pANAwAKAUy19oSLvFbuAcsmYgBmvj04SCNuEsbV9MTLnBds7D9v786rWkJb6EcPG a6z0E0GsvqJAbMEAAEKAB0WIQQ1zPtjsoPW0663g5RMtfaEi7xW7gUCZr49OAAKCRBMtfaEi7xW 7k5PC/98UAwkvYp0/qJJ5oId54CkqC3M3LFfbK9ZnGTiOW1giKs5ZBZG0ZqV74WC4cHKA8Z1I+4 PT71JPV4gtivRDmpW6IZwt9J0KNGDn2GIoHBer5yePFvSbqXBDls34A8d0OnDCKPnoIy4Kg5ISo cmRogwN7ABCvaQKO0Vw+IJxL0Yyq8wXjTI0ZoTeHkXPv3fHVxvnexvaKnkQ7rMNSLFp0Sb9SdXT 65ZFtkL61/K0DozliiRgTuNz2RNgwtyqs6gCbdn4J/1QNE7qgNLIvz4fevZi8j5A0pc9AbROhGs k91aBFnd5/bU5GslTX8XgofY4mqvCKSbOIqoTvr8CZL598auKvH7idFIOcfvoFcRVQanLkH0sgB sPeRaTfUF9bQjG2h/uoBn+YiKCDlXMCoy80iteYmyviXwD3ccyuVfdfockJ2P22uZgDEPQWeQw2 HwgHgltUS6CuI19QCKn0nCi3RrJO00a9HpskHvWnghNEtnWvbEuzoHHCTvI9JPJEMPjVI= X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240815173903.4172139-26-samitolvanen@google.com> Subject: [PATCH v2 05/19] gendwarfksyms: Expand base_type From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Wedson Almeida Filho , Gary Guo , Petr Pavlu , Neal Gompa , Hector Martin , Janne Grunau , Asahi Linux , 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 vmlinux.o ... gendwarfksyms: process_exported_symbols: loops_per_jiffy variable base_type unsigned long byte_size(8) encoding(7); ... Signed-off-by: Sami Tolvanen --- scripts/gendwarfksyms/dwarf.c | 121 +++++++++++++++++++++++++++++++++- 1 file changed, 120 insertions(+), 1 deletion(-) diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index 956b30224316..a37c9049d18e 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -5,6 +5,17 @@ #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; @@ -60,6 +71,74 @@ static int process(struct state *state, const char *s) return 0; } +#define MAX_FMT_BUFFER_SIZE 128 + +static int process_fmt(struct state *state, const char *fmt, ...) +{ + char buf[MAX_FMT_BUFFER_SIZE]; + va_list args; + int res; + + va_start(args, fmt); + + res = checkp(vsnprintf(buf, sizeof(buf), fmt, args)); + if (res >= MAX_FMT_BUFFER_SIZE - 1) { + error("vsnprintf overflow: increase MAX_FMT_BUFFER_SIZE"); + res = -1; + } else { + res = check(process(state, buf)); + } + + va_end(args); + return res; +} + +/* Process a fully qualified name from DWARF scopes */ +static int process_fqn(struct state *state, Dwarf_Die *die) +{ + Dwarf_Die *scopes = NULL; + const char *name; + int res; + int i; + + res = checkp(dwarf_getscopes_die(die, &scopes)); + if (!res) { + name = get_name(die); + name = name ?: ""; + return check(process(state, name)); + } + + for (i = res - 1; i >= 0; i--) { + if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit) + continue; + + name = get_name(&scopes[i]); + name = name ?: ""; + check(process(state, name)); + if (i > 0) + check(process(state, "::")); + } + + free(scopes); + return 0; +} + +#define DEFINE_PROCESS_UDATA_ATTRIBUTE(attribute) \ + static int process_##attribute##_attr(struct state *state, \ + Dwarf_Die *die) \ + { \ + Dwarf_Word value; \ + if (get_udata_attr(die, DW_AT_##attribute, &value)) \ + check(process_fmt(state, \ + " " #attribute "(%" PRIu64 ")", \ + value)); \ + return 0; \ + } + +DEFINE_PROCESS_UDATA_ATTRIBUTE(alignment) +DEFINE_PROCESS_UDATA_ATTRIBUTE(byte_size) +DEFINE_PROCESS_UDATA_ATTRIBUTE(encoding) + bool match_all(Dwarf_Die *die) { return true; @@ -81,6 +160,44 @@ 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_attr(struct state *state, Dwarf_Die *die) +{ + Dwarf_Die type; + + if (get_ref_die_attr(die, DW_AT_type, &type)) + return check(process_type(state, &type)); + + /* Compilers can omit DW_AT_type -- print out 'void' to clarify */ + return check(process(state, "base_type void")); +} + +static int process_base_type(struct state *state, Dwarf_Die *die) +{ + check(process(state, "base_type ")); + check(process_fqn(state, die)); + check(process_byte_size_attr(state, die)); + check(process_encoding_attr(state, die)); + return check(process_alignment_attr(state, die)); +} + +static int process_type(struct state *state, Dwarf_Die *die) +{ + int tag = dwarf_tag(die); + + switch (tag) { + case DW_TAG_base_type: + check(process_base_type(state, die)); + break; + default: + debug("unimplemented type: %x", tag); + break; + } + + return 0; +} + /* * Exported symbol processing */ @@ -91,7 +208,9 @@ static int process_subprogram(struct state *state, Dwarf_Die *die) static int process_variable(struct state *state, Dwarf_Die *die) { - return check(process(state, "variable;\n")); + check(process(state, "variable ")); + check(process_type_attr(state, die)); + return check(process(state, ";\n")); } static int process_symbol_ptr(struct state *state, Dwarf_Die *die) From patchwork Thu Aug 15 17:39:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13765041 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 E0AAF13CFAA for ; Thu, 15 Aug 2024 17:39:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743567; cv=none; b=Yr9Xwq0gVyM5/OHVOYFBnmxVUP97a/3fg+qjB0j1lK8TJyFsLKQL33oZdokchWdHGO1di7V3yDEPe5rL2Cly2Ls3dXLXBtxJck4zz+8PFfaFTaSQu519HXCO5w7RYNEjkOKLGGAoxaEm+9JbnhaRyCZhLH9I+JqPerz2arc8ATA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743567; c=relaxed/simple; bh=WcDCaJXjW0f2JTquZj6OPQ/VcqwyCDdlHz/KUsXci2w=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XuivHzV34gtBpFNZ4f+IVS8O96IxdFRMdusMBhz1J4y3I+Nmj5NzOmsv1fpOGH3VYZgy/HUGmU9EWz39bcAE4sEQFfeegxkpPfuOHiqupVilB9lYzniU6KVGV4r8Ho9+v7e8PQEcto3yGBGhMVx01MYJ+RLlHaocnVHGWxt/jzA= 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=LAahV7jA; arc=none smtp.client-ip=209.85.210.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="LAahV7jA" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-71065f49b10so1154626b3a.0 for ; Thu, 15 Aug 2024 10:39:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723743564; x=1724348364; 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=wSWLELxvFuyu6zxWt/XqlJxl649JinucJ0xWmbt0ecA=; b=LAahV7jA0+uz+vzP1+XcH6o8pzMs9c3pvY9D3SZbpGR2fZj95lk9n11a/5iREbJwmA xnh0OqutyHoAXZbRShp7VS783QpTS/T+WF4LE/pIM9xwhuYvvo7R5LZ6xl0ASNHww/wr LWJWJAJUASI2r3iOGTC2V3INNsSZ+UKrJ59/NOZZ0c2ih/7lTPhK+ilfNWWc2jntTNu3 vv2tPS1hBHBYett82DZ7Os5YVhP5K+S9PNWlKGtBxJIplGo+La17Wl6GkDvBVX+JHHNd T2uifsG43LM2KZDBufWwmOnaeF91sMvjg9Fmx2JQHvt2K/mv2jNsfnzmhJ4fLJYAdGqh k6hg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723743564; x=1724348364; 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=wSWLELxvFuyu6zxWt/XqlJxl649JinucJ0xWmbt0ecA=; b=XgVmbcfAyqXTcMBFWw1iRF5th1aVQpSywcc3xukWfoIlavPEZyjfQ5u52b44QkAwKZ 5oocNKMUbiVouDyJH4ZiArwwBWo5roDGg+zrcFLGQCAgZ1WWEYyS1BecCttrQ/mvYd+l fGI3NJldHvCzUh/aXLbpUzEOr8Ww615IL+klI2TpgRNHyDHGk5KYQGB+XWzJPPoVDcJm Fz7mq4gvqydgjWugOyMyeyfLfPP0lwJAO8M7gX2+V1OBs6Op+ZqkI1hAV5tI68PMWE/1 FYGjiODOC4QwiOYNTI02QL743h4SXxmlRTXqf1OXIPRzMH6/g5Lp9aQV0y1vYceZi7pD /MgQ== X-Forwarded-Encrypted: i=1; AJvYcCVctfgovegBTG77//xdcSJvaIQLgc+3ATgfYwGHOGoVzudahUA24lQhF+iztjX0GkBdZbtNKRFDcGN92Q8yOrYXopkyIy7gniAin+kz X-Gm-Message-State: AOJu0YwYf3nJYPqq5dzSx/5FjP6v4nuoLLkDAWNe2r/UnRPQq2yPCYZG SZfrPTqTCRWcXbrbr5Gi5B9kgjTuIZUJv33tmmxDHOpxtUM6B61kwF/hAV78INQXKAhkjxct5QG egKH1k9HY+fImkVYyLligosHWVg== X-Google-Smtp-Source: AGHT+IHR6SAt2JifNMS5dTXYZGoY8TilzPCnRBOfdyHIVDp2HaKQ+8Ln/k+YWaDzQ008IaORCtagkkX5o2KpaUIG5WQ= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a05:6a00:66d0:b0:710:5836:1a24 with SMTP id d2e1a72fcca58-713c534fffbmr936b3a.6.1723743563850; Thu, 15 Aug 2024 10:39:23 -0700 (PDT) Date: Thu, 15 Aug 2024 17:39:10 +0000 In-Reply-To: <20240815173903.4172139-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240815173903.4172139-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=17055; i=samitolvanen@google.com; h=from:subject; bh=WcDCaJXjW0f2JTquZj6OPQ/VcqwyCDdlHz/KUsXci2w=; b=owEB7QES/pANAwAKAUy19oSLvFbuAcsmYgBmvj04qdDtzVZLUBt/GmJyPrnJD8K2Ja+yO/fMy /bXS7xsnZuJAbMEAAEKAB0WIQQ1zPtjsoPW0663g5RMtfaEi7xW7gUCZr49OAAKCRBMtfaEi7xW 7pHhC/9FA6n54b79gRbgatKd3AW9gR5zZ8WIzyDerNT7r275dzEfQNMrcU7cZOtJNIXv4K2tAgC p8IC9m2EitARAwPAymS+fGJwjj9T6k1mfFiGhCkYWbi5kxOO+lYw4pEOFLKbVZmbWJUPwEEtnkR VvWJBmzgL8Edbc7kqCLMEM5SOcxRIVjhEXrnQ5hHdxYpxtn7WYtj1ygo8kzFnstqgUeEYF/duZj GpaPiYVD+AFNb4xWa/sv+r5ddkQZgUTDCiZP9HT594bEzlLvq5rwGLo+g5WAN9AJiVM1VNXrw9c zaxGfZoi5Nx3cQBmzLmaP3+/vNW/ol6m3pUNFQ27RtY13ONWWHr0rQ9wX16urPzCEYd7ZTUO9bT BCssKCWQHdqrrLIGhiV+A5k7+CuGdLrYKHxe2rCmzskHZtoiEyeEmxbduFT9vnilFT+TDrQ78dY 6jsUzCnXOojqkafkNPn1pqLjU5a/ax/GoD+MsrSiWhaLDX0SN/6LWvxTv90vGWPO0r66M= X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240815173903.4172139-27-samitolvanen@google.com> Subject: [PATCH v2 06/19] 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 , Wedson Almeida Filho , Gary Guo , Petr Pavlu , Neal Gompa , Hector Martin , Janne Grunau , Asahi Linux , 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 --- scripts/gendwarfksyms/Makefile | 1 + scripts/gendwarfksyms/die.c | 170 +++++++++++++++++++++++ scripts/gendwarfksyms/dwarf.c | 192 ++++++++++++++++++++------ scripts/gendwarfksyms/gendwarfksyms.c | 6 + scripts/gendwarfksyms/gendwarfksyms.h | 58 +++++++- 5 files changed, 382 insertions(+), 45 deletions(-) create mode 100644 scripts/gendwarfksyms/die.c diff --git a/scripts/gendwarfksyms/Makefile b/scripts/gendwarfksyms/Makefile index 623f8fc975ea..fcbac52ca00a 100644 --- a/scripts/gendwarfksyms/Makefile +++ b/scripts/gendwarfksyms/Makefile @@ -1,6 +1,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..ad6ba435d9dd --- /dev/null +++ b/scripts/gendwarfksyms/die.c @@ -0,0 +1,170 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2024 Google LLC + */ + +#include +#include "gendwarfksyms.h" + +#define DIE_HASH_BITS 20 + +/* uintptr_t die->addr -> struct die * */ +static DEFINE_HASHTABLE(die_map, DIE_HASH_BITS); + +static unsigned int map_hits; +static unsigned int map_misses; + +static int create_die(Dwarf_Die *die, struct die **res) +{ + struct die *cd; + + cd = malloc(sizeof(struct die)); + if (!cd) { + error("malloc failed"); + return -1; + } + + cd->state = INCOMPLETE; + cd->mapped = false; + cd->fqn = NULL; + cd->tag = -1; + cd->addr = (uintptr_t)die->addr; + cd->list = NULL; + + hash_add(die_map, &cd->hash, addr_hash(cd->addr)); + *res = cd; + return 0; +} + +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, addr_hash(addr)) { + if (cd->addr == addr && cd->state == state) { + *res = cd; + return 0; + } + } + + return -1; +} + +int die_map_get(Dwarf_Die *die, enum die_state state, struct die **res) +{ + if (__die_map_get((uintptr_t)die->addr, state, res) == 0) { + map_hits++; + return 0; + } + + map_misses++; + return check(create_die(die, res)); +} + +static void reset_die(struct die *cd) +{ + struct die_fragment *tmp; + struct die_fragment *df = cd->list; + + while (df) { + if (df->type == STRING) + free(df->data.str); + + tmp = df->next; + free(df); + df = tmp; + } + + cd->state = INCOMPLETE; + cd->mapped = false; + if (cd->fqn) + free(cd->fqn); + cd->fqn = NULL; + cd->tag = -1; + cd->addr = 0; + cd->list = NULL; +} + +void die_map_free(void) +{ + struct hlist_node *tmp; + unsigned int stats[LAST + 1]; + struct die *cd; + int i; + + memset(stats, 0, sizeof(stats)); + + hash_for_each_safe(die_map, i, tmp, cd, 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 <= LAST; i++) + debug("%s: %u entries", die_state_name(i), stats[i]); +} + +static int append_item(struct die *cd, struct die_fragment **res) +{ + struct die_fragment *prev; + struct die_fragment *df; + + df = malloc(sizeof(struct die_fragment)); + if (!df) { + error("malloc failed"); + return -1; + } + + df->type = EMPTY; + df->next = NULL; + + prev = cd->list; + while (prev && prev->next) + prev = prev->next; + + if (prev) + prev->next = df; + else + cd->list = df; + + *res = df; + return 0; +} + +int die_map_add_string(struct die *cd, const char *str) +{ + struct die_fragment *df; + + if (!cd) + return 0; + + check(append_item(cd, &df)); + + df->data.str = strdup(str); + if (!df->data.str) { + error("strdup failed"); + return -1; + } + + df->type = STRING; + return 0; +} + +int die_map_add_die(struct die *cd, struct die *child) +{ + struct die_fragment *df; + + if (!cd) + return 0; + + check(append_item(cd, &df)); + df->data.addr = child->addr; + df->type = DIE; + return 0; +} diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index a37c9049d18e..82b966269acd 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -61,19 +61,20 @@ static bool is_export_symbol(struct state *state, Dwarf_Die *die) /* * Type string processing */ -static int process(struct state *state, const char *s) +static int process(struct state *state, struct die *cache, const char *s) { s = s ?: ""; if (debug) fputs(s, stderr); - return 0; + return check(die_map_add_string(cache, s)); } #define MAX_FMT_BUFFER_SIZE 128 -static int process_fmt(struct state *state, const char *fmt, ...) +static int process_fmt(struct state *state, struct die *cache, const char *fmt, + ...) { char buf[MAX_FMT_BUFFER_SIZE]; va_list args; @@ -86,50 +87,103 @@ static int process_fmt(struct state *state, const char *fmt, ...) error("vsnprintf overflow: increase MAX_FMT_BUFFER_SIZE"); res = -1; } else { - res = check(process(state, buf)); + res = check(process(state, cache, buf)); } va_end(args); return res; } -/* Process a fully qualified name from DWARF scopes */ -static int process_fqn(struct state *state, Dwarf_Die *die) +#define MAX_FQN_SIZE 64 + +/* Get a fully qualified name from DWARF scopes */ +static int get_fqn(struct state *state, Dwarf_Die *die, char **fqn) { + const char *list[MAX_FQN_SIZE]; Dwarf_Die *scopes = NULL; - const char *name; + int count = 0; + int len = 0; int res; int i; + *fqn = NULL; + res = checkp(dwarf_getscopes_die(die, &scopes)); if (!res) { - name = get_name(die); - name = name ?: ""; - return check(process(state, name)); + list[count] = get_name(die); + + if (!list[count]) + return 0; + + len += strlen(list[count]); + count++; + + goto done; } - for (i = res - 1; i >= 0; i--) { + for (i = res - 1; i >= 0 && count < MAX_FQN_SIZE; i--) { if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit) continue; - name = get_name(&scopes[i]); - name = name ?: ""; - check(process(state, name)); - if (i > 0) - check(process(state, "::")); + /* + * If any of the DIEs in the scope is missing a name, consider + * the DIE to be unnamed. + */ + list[count] = get_name(&scopes[i]); + + if (!list[count]) { + free(scopes); + return 0; + } + + len += strlen(list[count]); + count++; + + if (i > 0) { + list[count++] = "::"; + len += 2; + } } + if (count == MAX_FQN_SIZE) + warn("increase MAX_FQN_SIZE: reached the maximum"); + free(scopes); + +done: + *fqn = malloc(len + 1); + if (!*fqn) { + error("malloc failed"); + return -1; + } + + **fqn = '\0'; + + for (i = 0; i < count; i++) + strcat(*fqn, list[i]); + return 0; } +static int process_fqn(struct state *state, struct die *cache, Dwarf_Die *die) +{ + const char *fqn; + + if (!cache->fqn) + check(get_fqn(state, die, &cache->fqn)); + + fqn = cache->fqn; + fqn = fqn ?: ""; + return check(process(state, cache, fqn)); +} + #define DEFINE_PROCESS_UDATA_ATTRIBUTE(attribute) \ - static int process_##attribute##_attr(struct state *state, \ - Dwarf_Die *die) \ + static int process_##attribute##_attr( \ + struct state *state, struct die *cache, Dwarf_Die *die) \ { \ Dwarf_Word value; \ if (get_udata_attr(die, DW_AT_##attribute, &value)) \ - check(process_fmt(state, \ + check(process_fmt(state, cache, \ " " #attribute "(%" PRIu64 ")", \ value)); \ return 0; \ @@ -144,8 +198,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; @@ -153,48 +208,99 @@ int process_die_container(struct state *state, Dwarf_Die *die, res = checkp(dwarf_child(die, ¤t)); while (!res) { if (match(¤t)) - check(func(state, ¤t)); + check(func(state, cache, ¤t)); res = checkp(dwarf_siblingof(¤t, ¤t)); } 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 int process_type_attr(struct state *state, Dwarf_Die *die) +static int 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)) - return check(process_type(state, &type)); + return check(process_type(state, cache, &type)); /* Compilers can omit DW_AT_type -- print out 'void' to clarify */ - return check(process(state, "base_type void")); + return check(process(state, cache, "base_type void")); } -static int process_base_type(struct state *state, Dwarf_Die *die) +static int process_base_type(struct state *state, struct die *cache, + Dwarf_Die *die) { - check(process(state, "base_type ")); - check(process_fqn(state, die)); - check(process_byte_size_attr(state, die)); - check(process_encoding_attr(state, die)); - return check(process_alignment_attr(state, die)); + check(process(state, cache, "base_type ")); + check(process_fqn(state, cache, die)); + check(process_byte_size_attr(state, cache, die)); + check(process_encoding_attr(state, cache, die)); + return check(process_alignment_attr(state, cache, die)); } -static int process_type(struct state *state, Dwarf_Die *die) +static int process_cached(struct state *state, struct die *cache, + Dwarf_Die *die) { + struct die_fragment *df = cache->list; + Dwarf_Die child; + + while (df) { + switch (df->type) { + case STRING: + check(process(state, NULL, df->data.str)); + break; + case DIE: + if (!dwarf_die_addr_die(state->dbg, + (void *)df->data.addr, + &child)) { + error("dwarf_die_addr_die failed"); + return -1; + } + check(process_type(state, NULL, &child)); + break; + default: + error("empty die_fragment"); + return -1; + } + df = df->next; + } + + return 0; +} + +static int process_type(struct state *state, struct die *parent, Dwarf_Die *die) +{ + struct die *cache = NULL; int tag = dwarf_tag(die); + /* + * If we have the DIE already cached, use it instead of walking + * through DWARF. + */ + check(die_map_get(die, COMPLETE, &cache)); + + if (cache->state == COMPLETE) { + check(process_cached(state, cache, die)); + check(die_map_add_die(parent, cache)); + return 0; + } + switch (tag) { case DW_TAG_base_type: - check(process_base_type(state, die)); + check(process_base_type(state, cache, die)); break; default: debug("unimplemented type: %x", tag); break; } + /* Update cache state and append to the parent (if any) */ + cache->tag = tag; + cache->state = COMPLETE; + check(die_map_add_die(parent, cache)); + return 0; } @@ -203,14 +309,14 @@ static int process_type(struct state *state, Dwarf_Die *die) */ static int process_subprogram(struct state *state, Dwarf_Die *die) { - return check(process(state, "subprogram;\n")); + return check(process(state, NULL, "subprogram;\n")); } static int process_variable(struct state *state, Dwarf_Die *die) { - check(process(state, "variable ")); - check(process_type_attr(state, die)); - return check(process(state, ";\n")); + check(process(state, NULL, "variable ")); + check(process_type_attr(state, NULL, die)); + return check(process(state, NULL, ";\n")); } static int process_symbol_ptr(struct state *state, Dwarf_Die *die) @@ -235,7 +341,8 @@ static int process_symbol_ptr(struct state *state, Dwarf_Die *die) return check(process_variable(state, &ptr_type)); } -static int process_exported_symbols(struct state *state, Dwarf_Die *die) +static int process_exported_symbols(struct state *state, struct die *cache, + Dwarf_Die *die) { int tag = dwarf_tag(die); @@ -244,8 +351,9 @@ static int process_exported_symbols(struct state *state, Dwarf_Die *die) case DW_TAG_namespace: case DW_TAG_class_type: case DW_TAG_structure_type: - return check(process_die_container( - state, die, process_exported_symbols, match_all)); + return check(process_die_container(state, cache, die, + process_exported_symbols, + match_all)); /* Possible exported symbols */ case DW_TAG_subprogram: @@ -273,5 +381,5 @@ int process_module(Dwfl_Module *mod, Dwarf *dbg, Dwarf_Die *cudie) struct state state = { .mod = mod, .dbg = dbg }; return check(process_die_container( - &state, cudie, process_exported_symbols, match_all)); + &state, NULL, cudie, process_exported_symbols, match_all)); } diff --git a/scripts/gendwarfksyms/gendwarfksyms.c b/scripts/gendwarfksyms/gendwarfksyms.c index e2f8ee5a4bf3..55a7fc902bf4 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.c +++ b/scripts/gendwarfksyms/gendwarfksyms.c @@ -78,6 +78,10 @@ static int process_modules(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) { @@ -90,6 +94,8 @@ static int process_modules(Dwfl_Module *mod, void **userdata, const char *name, check(process_module(mod, dbg, &cudie)); } while (cu); + die_map_free(); + return DWARF_CB_OK; } diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h index 8f6acd1b8f8f..b280acceb114 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -76,6 +76,11 @@ static inline u32 name_hash(const char *name) return jhash(name, strlen(name), 0); } +static inline u32 addr_hash(uintptr_t addr) +{ + return jhash(&addr, sizeof(addr), 0); +} + struct symbol { const char *name; struct symbol_addr addr; @@ -88,6 +93,52 @@ extern int symbol_read_exports(FILE *file); extern int symbol_read_symtab(int fd); extern struct symbol *symbol_get(const char *name); +/* + * die.c + */ + +enum die_state { INCOMPLETE, COMPLETE, LAST = COMPLETE }; +enum die_fragment_type { EMPTY, STRING, DIE }; + +struct die_fragment { + enum die_fragment_type type; + union { + char *str; + uintptr_t addr; + } data; + struct die_fragment *next; +}; + +#define CASE_CONST_TO_STR(name) \ + case name: \ + return #name; + +static inline const char *die_state_name(enum die_state state) +{ + switch (state) { + default: + CASE_CONST_TO_STR(INCOMPLETE) + CASE_CONST_TO_STR(COMPLETE) + } +} + +struct die { + enum die_state state; + bool mapped; + char *fqn; + int tag; + uintptr_t addr; + struct die_fragment *list; + struct hlist_node hash; +}; + +extern int __die_map_get(uintptr_t addr, enum die_state state, + struct die **res); +extern int die_map_get(Dwarf_Die *die, enum die_state state, struct die **res); +extern int die_map_add_string(struct die *pd, const char *str); +extern int die_map_add_die(struct die *pd, struct die *child); +extern void die_map_free(void); + /* * dwarf.c */ @@ -99,12 +150,13 @@ 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); extern bool match_all(Dwarf_Die *die); -extern int process_die_container(struct state *state, Dwarf_Die *die, - die_callback_t func, +extern int process_die_container(struct state *state, struct die *cache, + Dwarf_Die *die, die_callback_t func, die_match_callback_t match); extern int process_module(Dwfl_Module *mod, Dwarf *dbg, Dwarf_Die *cudie); From patchwork Thu Aug 15 17:39:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13765042 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.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 74EEF1B1427 for ; Thu, 15 Aug 2024 17:39:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743568; cv=none; b=OAMCY9elwAYQDpxerpRKkv/tvAH2P8Gt7gJEGePjKzRFwe1uINBhKfoz2e/mhC0G58MrjZR1WbU7uPYtqj+vLdnUWmRHuHy2SZy3rpVF8bAHYK/V3ByKpmVEk2K01be1C3mvnZ1/A/TpplSQXDVy/Wc+a6+t0yt6Xne3txq31Js= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743568; c=relaxed/simple; bh=pw/HqpAceG9KZJncGvbm0Tc+Y0NR2BksFQva9OpCIyo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=HhWm+T/bBB4vJGRXAmKwR9UNXo5vEh+0dRraRSNEBXDWfLaREQbKKjDZubAfbAwy+z3jJBEtEip7eT92lhZ/nKKEAEhtGVv3L8l05CG8j0mcwbMiT5pNZH9/DegRcjP0kKj0P8r/vSltso7yyLnuaeXmfexkUkQSiAHPHO1ZYaw= 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=uLcAAeNm; arc=none smtp.client-ip=209.85.215.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="uLcAAeNm" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-7a134fd9261so1020392a12.3 for ; Thu, 15 Aug 2024 10:39:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723743566; x=1724348366; 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=sqbrGwhfvS/o9WkR63NSSG+3ggv2pfYWpb4R8IiMfJY=; b=uLcAAeNmDnXP+Avd+Ze7cEyt12ynuITxuwM0FXswoVP5uwzuTrwGtHoY2Z84Sz76ZS dr4hVLP02+6Onz5jGJ/afGHxJ5Cnw0cab6ZHrf6BIMuwBnRqILhHS6wX0KwMF97aKG3p szstwiAcd8XRgD4IwjtV+Y8Q263RBXus17myvpzSm12n5bX5YqdAFRkXlaSjJ1+tdtAY XWmZs6TH/jJ115095/CzQuFWm60ORY12h2o82sJWChJlpCPCC52sKWE6XqCspOoWLl3T amGXgNx7GOARUrW/6CKl48yHla0iNGPxUI1Gg/l4dPQP2RESmcJSILN9MK/N3awshMwH gbIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723743566; x=1724348366; 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=sqbrGwhfvS/o9WkR63NSSG+3ggv2pfYWpb4R8IiMfJY=; b=JSfeAUPv8VNzSpHNjk59O4W35UPY2bJp5dVplw1CeRE/t/JQXkf/RkIQfTFhIyw8qT n/EKs9T5SwCv/xe907VqyhlRpHxSFdErwFLDkfMl90dr7YNojObhmRkPCHCNhwTIslgI mExsS4/hIY9GmfirVP/xInDsIT1Uc9AE+nEoi8KRYRx+sZcExjhsEZ88LNe0oQPO8xol LeYN3NMMF8JwO7bfvu8MMCZjZVB1DozEYs5XLMQeXSbAqo+kQWdIcgs1BPdczj7baWCK gZGTmxMcTEmgmQSYohT73/H/W+wVQiVzKIQJrauHLgi3GM9v992RkMx5dBGMueXpRrD+ 0Ijg== X-Forwarded-Encrypted: i=1; AJvYcCVjFgG59J2c6XnPCdo+FXMrGLPDbHdrhURDLEqTJnOKIJw19+0tAaKRTn3CngDs/Y6GRD9/KqUjuQozkyzWPPzZ+BZ+P8TafRFlaXfi X-Gm-Message-State: AOJu0YwpLu1QCJEaKipGPHiV/fmlGvueyAVqjyacwy5rL9+JQuN3VVll LhuDPizg+GwFzSv5RoSg/AWVnLVe1ZBSkLAThPIlPZ1+j11TykX7e5sFCoYdZtzbKqNZu57xqPE jrEP7GH+r1u5OG3N4LBcnvpBZXw== X-Google-Smtp-Source: AGHT+IEikh37+tKCQQfnwWQEfyYceqW0cvrSUqkS+CWOQoJktHenIYVMdrw9MA5y4VPH80UGrDlrydgt0ZOnnyEEye4= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a65:6712:0:b0:6ea:d240:23b0 with SMTP id 41be03b00d2f7-7c9796d9119mr52a12.4.1723743565745; Thu, 15 Aug 2024 10:39:25 -0700 (PDT) Date: Thu, 15 Aug 2024 17:39:11 +0000 In-Reply-To: <20240815173903.4172139-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240815173903.4172139-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=6513; i=samitolvanen@google.com; h=from:subject; bh=pw/HqpAceG9KZJncGvbm0Tc+Y0NR2BksFQva9OpCIyo=; b=owEB7QES/pANAwAKAUy19oSLvFbuAcsmYgBmvj04y5MroNixYi6Q4mKYktiBZJ/foUVldN1bP rlslcstbPCJAbMEAAEKAB0WIQQ1zPtjsoPW0663g5RMtfaEi7xW7gUCZr49OAAKCRBMtfaEi7xW 7twtC/9nSIzTqkeqA1dIqXL/6ZYfzQuM/gdLn/xttXn3ForrLsYhstOkf7ftk40qu3YyKnd54MH 9/xa4HXN7B3VDiKzg6G/SbamfK1l0vFPVOvb7iSaayKYd/bP2ox0uZlRdehPiMHxdJ1Ui0VgztQ 9+pAcpM6eUTRLkLtUf1UfJ6sHk975nEE4LH4+g1hDuQnegOvEUyYSW/JxuulltZWBsU3k2cmSOa GBh/DY4vlIRAhQHOK9w/HiunYEyxCgEFQWk0D2JIzQCoh5nY+jSotlHu3MUF5VJ41erIx3C3vtu xIfAAKUEKuF6Oxe/P9DZVP7Gskdh2r+1opz0YUk/Uv94uV3PPgwm2YH5BRf2EG8F3OqG8nKUkrg sY9G4A9XMnPZ71P8o5cJ+ilJOd6QcN4/GSI2X9CZBVDLqVC0n2QtD6yrOMPqFYmhCs3YuJuuDTc YRMGE4mR7Ceo4L+6BILOU74Db8LpWR2EcRaFGNUCy6YD4n7Rte4bWXGs8RBQ9IBD4nsQw= X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240815173903.4172139-28-samitolvanen@google.com> Subject: [PATCH v2 07/19] gendwarfksyms: Expand type modifiers and typedefs From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Wedson Almeida Filho , Gary Guo , Petr Pavlu , Neal Gompa , Hector Martin , Janne Grunau , Asahi Linux , 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 --- scripts/gendwarfksyms/die.c | 13 +++++ scripts/gendwarfksyms/dwarf.c | 76 +++++++++++++++++++++++++-- scripts/gendwarfksyms/gendwarfksyms.h | 7 ++- 3 files changed, 92 insertions(+), 4 deletions(-) diff --git a/scripts/gendwarfksyms/die.c b/scripts/gendwarfksyms/die.c index ad6ba435d9dd..fdd52df88fdd 100644 --- a/scripts/gendwarfksyms/die.c +++ b/scripts/gendwarfksyms/die.c @@ -156,6 +156,19 @@ int die_map_add_string(struct die *cd, const char *str) return 0; } +int die_map_add_linebreak(struct die *cd, int linebreak) +{ + struct die_fragment *df; + + if (!cd) + return 0; + + check(append_item(cd, &df)); + df->data.linebreak = linebreak; + df->type = LINEBREAK; + return 0; +} + int 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 82b966269acd..82185737fa2a 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -5,6 +5,17 @@ #include "gendwarfksyms.h" +static bool do_linebreak; +static int indentation_level; + +/* Line breaks and indentation for pretty-printing */ +static int process_linebreak(struct die *cache, int n) +{ + indentation_level += n; + do_linebreak = true; + return check(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) \ @@ -65,6 +76,12 @@ static int process(struct state *state, struct die *cache, const char *s) { s = s ?: ""; + if (debug && do_linebreak) { + fputs("\n", stderr); + for (int i = 0; i < indentation_level; i++) + fputs(" ", stderr); + do_linebreak = false; + } if (debug) fputs(s, stderr); @@ -230,6 +247,40 @@ static int process_type_attr(struct state *state, struct die *cache, return check(process(state, cache, "base_type void")); } +/* Container types with DW_AT_type */ +static int __process_type(struct state *state, struct die *cache, + Dwarf_Die *die, const char *type) +{ + check(process(state, cache, type)); + check(process_fqn(state, cache, die)); + check(process(state, cache, " {")); + check(process_linebreak(cache, 1)); + check(process_type_attr(state, cache, die)); + check(process_linebreak(cache, -1)); + check(process(state, cache, "}")); + check(process_byte_size_attr(state, cache, die)); + return check(process_alignment_attr(state, cache, die)); +} + +#define DEFINE_PROCESS_TYPE(type) \ + static int process_##type##_type(struct state *state, \ + struct die *cache, Dwarf_Die *die) \ + { \ + return __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 int process_base_type(struct state *state, struct die *cache, Dwarf_Die *die) { @@ -251,6 +302,9 @@ static int process_cached(struct state *state, struct die *cache, case STRING: check(process(state, NULL, df->data.str)); break; + case LINEBREAK: + check(process_linebreak(NULL, df->data.linebreak)); + break; case DIE: if (!dwarf_die_addr_die(state->dbg, (void *)df->data.addr, @@ -270,6 +324,11 @@ static int process_cached(struct state *state, struct die *cache, return 0; } +#define PROCESS_TYPE(type) \ + case DW_TAG_##type##_type: \ + check(process_##type##_type(state, cache, die)); \ + break; + static int process_type(struct state *state, struct die *parent, Dwarf_Die *die) { struct die *cache = NULL; @@ -288,9 +347,20 @@ static int process_type(struct state *state, struct die *parent, Dwarf_Die *die) } switch (tag) { - case DW_TAG_base_type: - check(process_base_type(state, cache, die)); - break; + /* 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 b280acceb114..e8b4da0fc9a8 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -58,6 +58,9 @@ extern bool debug; /* Error == negative values */ #define checkp(expr) __check(expr, __res < 0, __res) +/* Consistent aliases (DW_TAG__type) for DWARF tags */ +#define DW_TAG_typedef_type DW_TAG_typedef + /* * symbols.c */ @@ -98,12 +101,13 @@ extern struct symbol *symbol_get(const char *name); */ enum die_state { INCOMPLETE, COMPLETE, LAST = COMPLETE }; -enum die_fragment_type { EMPTY, STRING, DIE }; +enum die_fragment_type { EMPTY, STRING, LINEBREAK, DIE }; struct die_fragment { enum die_fragment_type type; union { char *str; + int linebreak; uintptr_t addr; } data; struct die_fragment *next; @@ -136,6 +140,7 @@ extern int __die_map_get(uintptr_t addr, enum die_state state, struct die **res); extern int die_map_get(Dwarf_Die *die, enum die_state state, struct die **res); extern int die_map_add_string(struct die *pd, const char *str); +extern int die_map_add_linebreak(struct die *pd, int linebreak); extern int die_map_add_die(struct die *pd, struct die *child); extern void die_map_free(void); From patchwork Thu Aug 15 17:39:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13765043 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 9BA091BB6BB for ; Thu, 15 Aug 2024 17:39:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743570; cv=none; b=eI/h94PFLBH8QI57ocWQ9zhDet+Gf0uQcXOhd9H54zG//QASoydAcu8qNyOqCWJ6xSoABtrwB22hgIWJZOdeNU94xf29iFfqrfrQyqULVfJH3SCV3pDB23l5AHYVV+y+buTSNQzstUo9L/hwDcnkUlEEQ5Z0DPmu5dR0B0K6EGc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743570; c=relaxed/simple; bh=IxWZKsLfDtf9kxevQsdRvL9iAtwcYd0l83K436mpqAw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=W76eJCqBxTTCF2eVTiLSrI6/GetOMSlqf8VfbeOs2ukljqWKbLNbYBNW5VhB+zXAhXtxgv3iLxZarzcdhE6K+10PvKCLrSUVMNrYFMGkvemEkBM5ok+iRnQW3+euWMK1yDzhh5Iznk07ohs/4+OFQJlsSpRrnY0dzSk5/WDI+WE= 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=3GmBb4rc; arc=none smtp.client-ip=209.85.216.73 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="3GmBb4rc" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2d3acc5b48bso980071a91.0 for ; Thu, 15 Aug 2024 10:39:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723743568; x=1724348368; 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=zfuqlaZpivgP61E8IUfDVjWbxuMIer3qaRAVkhI1EpI=; b=3GmBb4rc++vRWW/4KqKV9KTJWL3x6VLo/tP3ZfHIyt3SjWJy4o9mlP9T/odMErYMTC rLq7/uF4I1ziCckGX3kBjiQbgLfAfBYp4WSyqfppCdNKR//2lksivPJ1v0fJXXhREvWd npDqDCsiC6WMwXBWcmq0gjjWo+NXNFRnbyEM4jzg9eUV74FoZUfA8u4hw0zd/B48WtDC OmduN6GjtbJYmV79SIwJUtnKvnnsLytzkZgjkHQyPHiQPQfSu77a5wCctlAp/TVyo+jJ 97NJ2bupphxEHtLdVz4dfYSO7r0w2RHJ7Nttq7j7ucgCe3ecb2D0Xrzbr2bSgmgn6gNx RSWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723743568; x=1724348368; 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=zfuqlaZpivgP61E8IUfDVjWbxuMIer3qaRAVkhI1EpI=; b=ny5nLYvBq9asEEDn0I5IDRlTw7A1NH1WR6pEL+npXpsZrQI6Qbf6Wua+71OPURrm9/ KGItw0FGGxKNY7g5QPF+ipqlXf9PiCBcsQNfERaxV3Lzhsg45IaSe9q9rdFXv9WWB0en AJ4qJu8wK9yR11EQSOG5IHJUHvsqR3bUOO5iCtl/5Zdyo7bwfH8kj/8BxHOKsYB1AjUn mD+/BPBJIVfQZSyBhQ0GZ/NBb/5VOxTIbUitlU0t0q7xzSyHGPwRjNihZgo2T4rqoRAi 8VBnOuyh/dScg6dvfuPnIP8GgEYOG0aMRgrhvoEp16Y1JmVuguCdwOdxmJEZQbL5lsdA I76w== X-Forwarded-Encrypted: i=1; AJvYcCV+rE+qXTIYtIGohXRyEocYl5SNWqf/lYetZ1Q605oMCkjMLd/PChF6h3yBMz21+7SW+voQD4JXDN+sbPSesy5B/UHVAd69AevH9gZ1 X-Gm-Message-State: AOJu0YwL5qcXO+01wR+s8IX5MAhjSkkhGHeQ0wPonImh4WCrIe9Zs0Ky yDGmZgDy1VoNj/hcsnNYJVBECO1RvufbULLAhhUzoVyRt8qdk2D37RPSYZZk1N7r12C8wGTUtic UZUKXBEQEbUbC7Xlx5SZCAyZlpA== X-Google-Smtp-Source: AGHT+IFQfH4kooGic/QhjPlGgJQHvcaXF8cjadEJBNf7+K5TmqkgZbkyFvn+Yr2S6dOfoFZYy38IjvlUZRUzMCgrMbo= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a17:90a:d78e:b0:2d3:96b5:4940 with SMTP id 98e67ed59e1d1-2d3e20b22a9mr743a91.0.1723743567718; Thu, 15 Aug 2024 10:39:27 -0700 (PDT) Date: Thu, 15 Aug 2024 17:39:12 +0000 In-Reply-To: <20240815173903.4172139-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240815173903.4172139-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=4692; i=samitolvanen@google.com; h=from:subject; bh=IxWZKsLfDtf9kxevQsdRvL9iAtwcYd0l83K436mpqAw=; b=owEB7QES/pANAwAKAUy19oSLvFbuAcsmYgBmvj04GkWhb24TKJqKw4JVSD8OHepYkerwtLdQx qaACpYCrKWJAbMEAAEKAB0WIQQ1zPtjsoPW0663g5RMtfaEi7xW7gUCZr49OAAKCRBMtfaEi7xW 7sWTDACcHTL9GfULRb2s9iLakapapeokqgn87VvgRoaESLeX4qLc/RanJQknVLtcLG6YhJp8RYv qRY2Rko4zoQCzFOWW8UPaaT4qc+mgEicvOfhMlLL8Rnw9kzYr5FSGz1n1Z8ALY0EsEOx3wtW/1k zuEpLF63tTCSLwE7scyHu2pF5B6aAhXbt427egI0quLd1Svj8a1D0fYSZKkb1F76YHEbiMGwyan JoIluQ0Fn9iFOzcXfVeGQWMlTF55oEKVU0lPOrtgtEdwL8o4aLUqI+UF9abnadVnptTuxVJ8WV9 4u6bmfWXL6SXXQwMSDkHaE712B0M7EXa23aBV3cLpglNfnQXKvv7U3MgyjoYRDnbAHo40UMWKiR UMTWMR+DcoOXWQ14S0LaPEr+tsiuwpJCzBLwcUcmMkhrR0KPZI9l5OFCrMHaabH+r0+8NaARQpd dzycoPwoG+9DjaCujbi6uQFh4iPqACsslbf0N/X03KJOJcuZWkndB3IjqDwtl88LliXeg= X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240815173903.4172139-29-samitolvanen@google.com> Subject: [PATCH v2 08/19] gendwarfksyms: Expand subroutine_type From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Wedson Almeida Filho , Gary Guo , Petr Pavlu , Neal Gompa , Hector Martin , Janne Grunau , Asahi Linux , 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 --debug: subprogram( formal_parameter base_type usize byte_size(8), formal_parameter base_type usize byte_size(8), ) -> base_type void; Signed-off-by: Sami Tolvanen --- scripts/gendwarfksyms/dwarf.c | 57 ++++++++++++++++++++++++++- scripts/gendwarfksyms/gendwarfksyms.h | 1 + 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index 82185737fa2a..c81652426be8 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -210,6 +210,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; @@ -247,6 +256,25 @@ static int process_type_attr(struct state *state, struct die *cache, return check(process(state, cache, "base_type void")); } +/* Comma-separated with DW_AT_type */ +static int __process_list_type(struct state *state, struct die *cache, + Dwarf_Die *die, const char *type) +{ + check(process(state, cache, type)); + check(process_type_attr(state, cache, die)); + check(process(state, cache, ",")); + return check(process_linebreak(cache, 0)); +} + +#define DEFINE_PROCESS_LIST_TYPE(type) \ + static int process_##type##_type(struct state *state, \ + struct die *cache, Dwarf_Die *die) \ + { \ + return __process_list_type(state, cache, die, #type " "); \ + } + +DEFINE_PROCESS_LIST_TYPE(formal_parameter) + /* Container types with DW_AT_type */ static int __process_type(struct state *state, struct die *cache, Dwarf_Die *die, const char *type) @@ -281,6 +309,29 @@ DEFINE_PROCESS_TYPE(shared) DEFINE_PROCESS_TYPE(volatile) DEFINE_PROCESS_TYPE(typedef) +static int __process_subroutine_type(struct state *state, struct die *cache, + Dwarf_Die *die, const char *type) +{ + check(process(state, cache, type)); + check(process(state, cache, "(")); + check(process_linebreak(cache, 1)); + /* Parameters */ + check(process_die_container(state, cache, die, process_type, + match_formal_parameter_type)); + check(process_linebreak(cache, -1)); + check(process(state, cache, ")")); + process_linebreak(cache, 0); + /* Return type */ + check(process(state, cache, "-> ")); + return check(process_type_attr(state, cache, die)); +} + +static int process_subroutine_type(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + return check(__process_subroutine_type(state, cache, die, + "subroutine_type")); +} static int process_base_type(struct state *state, struct die *cache, Dwarf_Die *die) { @@ -358,8 +409,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); @@ -379,7 +433,8 @@ static int process_type(struct state *state, struct die *parent, Dwarf_Die *die) */ static int process_subprogram(struct state *state, Dwarf_Die *die) { - return check(process(state, NULL, "subprogram;\n")); + check(__process_subroutine_type(state, NULL, die, "subprogram")); + return check(process(state, NULL, ";\n")); } static int process_variable(struct state *state, Dwarf_Die *die) diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h index e8b4da0fc9a8..25f87485eb69 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -59,6 +59,7 @@ extern bool debug; #define checkp(expr) __check(expr, __res < 0, __res) /* 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 /* From patchwork Thu Aug 15 17:39:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13765044 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 623B31BC070 for ; Thu, 15 Aug 2024 17:39:30 +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=1723743572; cv=none; b=Rv+7eQGCAexdojOTpuAYRmE1ORR/cwKRQFPEdanap1F+2Kb4a+hDmuFj/7BojCVzlw9WydWp2tdrPUytQxd6tjOT6HrXbaejWFcv+inOLNTlZOubX+c9O0UitUNfwYusCMu5yjpMUsyq5XYmUgGG70PR5y1TjwYRslhCuCf9kQQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743572; c=relaxed/simple; bh=nTcTMkHUN/UCMnmRiFJQDXB1TxZAvoJP5PqJx/o9EQA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Qi8HJtPkbGe9eHU9lEI+k6cYhMUY1JEkJ9ZJW4dAxtWSv+1eLeEz54Ov7glXnO6Ujni8YS8gsMaJA24eRzvTCF1xVfTRCdmmTq2IEPu/JPHNizC2iCpv43KRRYNAWHaVu9EjfBnVV7GQKVkl9gr3KHU3agJBOizLlaK/dv8CEoE= 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=YVEqopoJ; 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="YVEqopoJ" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-672bea19c63so19852827b3.2 for ; Thu, 15 Aug 2024 10:39:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723743569; x=1724348369; 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=oQTVjPACp33eNMD9tpytKg6MWS9lRG5M8diY/YwwjwU=; b=YVEqopoJuKNqM9+3+scgzHZxH4H0bXe7DRFOrqMJy8CmMuUzHwuc9DKbX+XuaCSJo6 DJaTHjRh5AuNVmGiu7iATig2CVUhP5yNYv5QPiiRyBrOXIwZosI8MveqmLR0fz/FhBK6 oV0wETohq49mPv+trOtiuTQlWehCgSpb4SjudOom+DwGDP7ftsbUEfxYkeD79dfUoHJH 58L3DT28eWgAl02EsYCLrGV1Uj4ejYD1IurF8p97NbiPFXe+e4qstB/AbMbqiMOY+HuB BevFJNn7lGKKRf3441YiQJ2li+MKzneTXxyyGSAjF9fIQgCjh0w/1Mb1v6UYQfm21dc7 G3GQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723743569; x=1724348369; 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=oQTVjPACp33eNMD9tpytKg6MWS9lRG5M8diY/YwwjwU=; b=VrgICjP0kEhgVLOumr9n0kj3PUI6u3Atsj/1eSqtiHzlA7FBA7+cLAUgzeCQxSI6YA svi/m1hz/pm1rLdDENlbLyxbHINiWisVSxQsY02aoyVoz652hrdnAJz6c92MDyZlHjJw 5ZPijp3zsHwpYd0ss4E4Z8q+XjUgSzT8cG8HCWA2Sfz5/N1phH3i0MPa5ImoWbCy6QJ8 gZ+E8YkXsFkLo4RFiMqUaMxfGeZaXEQfWHJyVL9Y7wz5LKMrYrivDtaI9NU5eELWLkL2 j91yHNYjxX5neRhEwzCuzQB692lVc+yTpbTBIow8EaiFCPAz/bpm4j8Nw4IldfOr2Zxn L/eQ== X-Forwarded-Encrypted: i=1; AJvYcCX4R2NNpGIVimSloRd/XSPOQb1E29Xn2UB8YiDVJFnUimuH6QNApXFXRfprPXCffgMHZZ2QKxfngB2im6QJyaxiUaX0gg1jVS+Zy3S4 X-Gm-Message-State: AOJu0Yw8lb3v6RjBp62LPx9OZYYP74iz+DTMwQvYOHmdaxGwx+DqOibs 67CTjT7A7HtSzWkjPurAyrgb056j18y4MHRxc3GWJGm6qtsgZERmFJbEk7yyychsaaa9P0rJfUb bt0OdTy22Cu7Fw9YkmQwdBUW1UA== X-Google-Smtp-Source: AGHT+IF5CO+eSdgk4A1o3bwcX4SrUit3pZq5QnY92bhAjwQwlxVmeRJpF4eb4vxYwPpGR9RAkAYB0eRSdszPeH2WSe4= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a05:690c:4084:b0:667:8a45:d0f9 with SMTP id 00721157ae682-6b1b1e5d0dcmr159547b3.0.1723743569512; Thu, 15 Aug 2024 10:39:29 -0700 (PDT) Date: Thu, 15 Aug 2024 17:39:13 +0000 In-Reply-To: <20240815173903.4172139-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240815173903.4172139-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=2395; i=samitolvanen@google.com; h=from:subject; bh=nTcTMkHUN/UCMnmRiFJQDXB1TxZAvoJP5PqJx/o9EQA=; b=owEB7QES/pANAwAKAUy19oSLvFbuAcsmYgBmvj04gVH5i1wio3ZaU6SzQ4CTy530kpMAP58VQ UhjMnOXvnaJAbMEAAEKAB0WIQQ1zPtjsoPW0663g5RMtfaEi7xW7gUCZr49OAAKCRBMtfaEi7xW 7v2aC/9n9aNbg61o1QfjFxg33M09DzU+aiKnoMY1BZQpsmArZ07gstbc5sJ4RGtOy+Aee7yr2WN jqsWFwV3sUG9t3i6SGUMaGVu2mxTapdoeWboqRb3doOR6k5+DfqUPzJ0i8HgVXZXUUu6OXRO0Ec QpDy4+/THzSyrmvDOv9/Z2XcU6dEUoxCVtlkluQNKLuTO32SS52vpmchi+nHsM6ZDm2gcEhLy38 IIF4iyRWYNh3HDacVGmENL0cW5A52SYlkjTT7w6t4gIQ/WRfZqKIbXi5C11t66jrieHzzTShCFj 4mo68AFpqeOhauFRqus/fNT5aSkxtpXlFRCw6pXI1CKq9YGxSnBcOJHW0KtXbjMBJnIr2mJcuxU iLH5DMEuVxf97wyRRgszQDbNpY/MLLZdQjsm+vGSymrh9bGrbaMTXOr7xYTr8H6s90/es9bbjHx 4/9EH6V8K+B7SZ+VqSbXe5msNpzTwsBAFZjrp66RMnldstPBfaMJyyg3GG+LgndBNoYgw= X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240815173903.4172139-30-samitolvanen@google.com> Subject: [PATCH v2 09/19] gendwarfksyms: Expand array_type From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Wedson Almeida Filho , Gary Guo , Petr Pavlu , Neal Gompa , Hector Martin , Janne Grunau , Asahi Linux , 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 --debug: variable array_type [34] { pointer_type { const_type { base_type char byte_size(1) encoding(6) } } byte_size(8) }; Signed-off-by: Sami Tolvanen --- scripts/gendwarfksyms/dwarf.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index c81652426be8..4ec69fce95f3 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -218,6 +218,7 @@ DEFINE_PROCESS_UDATA_ATTRIBUTE(encoding) } DEFINE_MATCH(formal_parameter) +DEFINE_MATCH(subrange) bool match_all(Dwarf_Die *die) { @@ -309,6 +310,34 @@ DEFINE_PROCESS_TYPE(shared) DEFINE_PROCESS_TYPE(volatile) DEFINE_PROCESS_TYPE(typedef) +static int 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)) + return check(process_fmt(state, cache, "[%" PRIu64 "]", count)); + if (get_udata_attr(die, DW_AT_upper_bound, &count)) + return check( + process_fmt(state, cache, "[%" PRIu64 "]", count + 1)); + + return check(process(state, cache, "[]")); +} + +static int process_array_type(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + check(process(state, cache, "array_type ")); + /* Array size */ + check(process_die_container(state, cache, die, process_type, + match_subrange_type)); + check(process(state, cache, " {")); + check(process_linebreak(cache, 1)); + check(process_type_attr(state, cache, die)); + check(process_linebreak(cache, -1)); + return check(process(state, cache, "}")); +} + static int __process_subroutine_type(struct state *state, struct die *cache, Dwarf_Die *die, const char *type) { @@ -411,7 +440,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 Aug 15 17:39:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13765045 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 479371BC9EE for ; Thu, 15 Aug 2024 17:39:32 +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=1723743574; cv=none; b=fIoOunA0j4mXO4DpXaNgOyTyMU7GUY5IojsDRqkKBbE/PGdDYZtIuWxjlB+x0NEDHoxNojrpIlEtuQWJhNCrtVNHPV/OYuzvqkWvaSlaU2EXX5EERMmgMy7LyOgPvD6UGZD8GkE+1hWKk+jx2AHjHnqeMloZP7ljHLsavuPqGLU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743574; c=relaxed/simple; bh=seTjzndQnpborzCoviHr6CbsMyWBJvxYdHHqERTVcCw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=LeOxJZqHhOFTNB9VnrbAESQYrOazTTBfbCRfwIgx/QmtfSfrq+qKzDA7bP6B/SccXx6PyriUgmeepdYHz4fECzlLSnKYHbKbAdT4pRDNpN0X2eXIBP0exUlr/xGRZGhAru7tIwwK1wQ3pE6THA4sYcuAhr83ePVxuNeZ7jeOhhk= 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=bqHfhE/9; 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="bqHfhE/9" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-64c3f08ca52so18169267b3.3 for ; Thu, 15 Aug 2024 10:39:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723743571; x=1724348371; 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=VFwCK9X2QLe1/DNg8JobTxi3rYfg72O97+Gvq68riBQ=; b=bqHfhE/9QRsI5hQDqp3tdVS970gAut+katuwK1SDW6rEWaItmZg7uUmdLkMyusOImo qVFlJaDp2Ms2tRaa7yO6W4/sJosYHkXq4WHUkN9o0J2A9bX8H5aLximciX7G3WI5teRA /EqbkGN6D79qC1KxVP3AZRQQND0y3KeIg8mUDxykfFOA52Ewl2vdXBcsaSlb7v0jxsoO 0tkxZWGQ/C5jotXXQgvIR2DGtaOXBAKubdqGfi/1esACUSbZxyQcc7LIAs54gD88/Dum O4YOqtGNThBcDQN18pnl69ZQcF4oHtsslzeRlEs9KE/O8BFPmOxoDVPLno1PsLHnj3Za N/Sg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723743571; x=1724348371; 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=VFwCK9X2QLe1/DNg8JobTxi3rYfg72O97+Gvq68riBQ=; b=ESCUVvFJH+vWYhGKBUXSBUIjVbQdy3nEf3QOWYTK/5E6RKxInG6enGVmqVs/ndD6q3 1u5sPOlsCQmPLTkzXcirS5U5SsKujeZXYLajYwI9Csxbb770GrVF3LydPhtvFr+DsJEW 5BH7X7C4GdmAOAEBh2ETuK9dzMmFYEVJmN/c9olRlguCdDjcdPTF1KuAEG+vXDBbtwiz 2HPeTWnKMgmJqia1e4mnoQXHIKxdfEPnJM2xyxC/LAMuUJh6G2+VBGJCwahDZYvr1zyh yKRhfYXM6uncRPvkq28s6tvmCjtOCTKXYeyI1bMFzu4Yf5w5YfRROkXiSArQvabYkx4u ZtJg== X-Forwarded-Encrypted: i=1; AJvYcCXTDgV7nX/vBz0lHb6R2VVufNA8uj83DrsbdJVGEZ8lpfJvDOY5FWevwUYAhZYm2hiATeP14lOEQfY+YHu9hRThAlLk/Zb2NfZ+FA6j X-Gm-Message-State: AOJu0YxTuqE6oCsncHFL4v+PDXxrcoNEFkSgQ+551/bNfMG4nPAThUjS hq9pvKkF8bkrqdhMSXnSvNLO4oaPHy16RgARq1b0sDZ+wsQkLBXa56RIBAmPhx5MZ46+YL779dv 0qOsnim3cQz+qT7sHYhJY16oPGg== X-Google-Smtp-Source: AGHT+IFpDiu75N2mANtuo7v4YJMU2ippJB5ROuq6c/iYPG7F5aVX3ZilvaGAZuCVdos8P47ImkL+SqtNGTBwKkGZyaM= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a25:83d0:0:b0:e11:584f:c2ca with SMTP id 3f1490d57ef6-e1180e8eb8emr17972276.6.1723743571365; Thu, 15 Aug 2024 10:39:31 -0700 (PDT) Date: Thu, 15 Aug 2024 17:39:14 +0000 In-Reply-To: <20240815173903.4172139-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240815173903.4172139-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=8281; i=samitolvanen@google.com; h=from:subject; bh=seTjzndQnpborzCoviHr6CbsMyWBJvxYdHHqERTVcCw=; b=owEB7QES/pANAwAKAUy19oSLvFbuAcsmYgBmvj0538CHXm4iu8vyUzVodDzA+TFKe40vk7Xbk VkSEjAFJLWJAbMEAAEKAB0WIQQ1zPtjsoPW0663g5RMtfaEi7xW7gUCZr49OQAKCRBMtfaEi7xW 7pEVC/9i6tqmTNz0zbieOL75N1vV2Nnd1mHzQqaT48B8+NCvNgaDNkPuFp8Z8wAHc5M5uyCEDTC GojTEMtseuO7exYf2efh5bAUu4D+M490awuJX8ORnX6D8iFr1EUiBC70La83m4CzZuC5isF7h02 fWE6iNLUDDBcNBRu8v+ACj9pFOoC3T6LhvpC1Tq1L2W9+2x5xZkk1GAfv72p2q8ytGVKe7ybVQE w2fZTAMaa7neJpMi4441GzoWvCx9FImmOhkQOlepo8mh+ApdvUD34tswj0gVt+ltl4Xe23bMVYM loz8EY37GPSjntf9a5MVcYMBKB81CJ01EKQswuecTckA3bTd6aAgfFJmK8FMscwcC/lFEp4K62/ KsX0ZOZQrl0+Lrqpx8FGvACY5v0dIeNtv97pJkApX6vBcSSceAU6/CCSjOOmXTG2tRH1TNAe9+E BOm/zHZ05DkegFlucoBm+FTm7udVomn7th9ksLWe66z29iEzPQ89BC4R1QZ/X91nqV/k0= X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240815173903.4172139-31-samitolvanen@google.com> Subject: [PATCH v2 10/19] gendwarfksyms: Expand structure types From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Wedson Almeida Filho , Gary Guo , Petr Pavlu , Neal Gompa , Hector Martin , Janne Grunau , Asahi Linux , 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 --debug: subprogram( formal_parameter pointer_type *mut &str { structure_type &str { member pointer_type { base_type u8 byte_size(1) encoding(7) } data_member_location(0), member base_type usize byte_size(8) encoding(7) data_member_location(8), } byte_size(16) alignment(8) }, ) -> base_type void; Signed-off-by: Sami Tolvanen --- scripts/gendwarfksyms/dwarf.c | 129 +++++++++++++++++++++++++- scripts/gendwarfksyms/gendwarfksyms.h | 5 + 2 files changed, 132 insertions(+), 2 deletions(-) diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index 4ec69fce95f3..92b6ca4c5c91 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -206,9 +206,13 @@ static int process_fqn(struct state *state, struct die *cache, Dwarf_Die *die) return 0; \ } +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) /* Match functions -- die_match_callback_t */ #define DEFINE_MATCH(type) \ @@ -217,8 +221,11 @@ 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) +DEFINE_MATCH(variant) bool match_all(Dwarf_Die *die) { @@ -263,6 +270,10 @@ static int __process_list_type(struct state *state, struct die *cache, { check(process(state, cache, type)); check(process_type_attr(state, cache, die)); + check(process_accessibility_attr(state, cache, die)); + check(process_bit_size_attr(state, cache, die)); + check(process_data_bit_offset_attr(state, cache, die)); + check(process_data_member_location_attr(state, cache, die)); check(process(state, cache, ",")); return check(process_linebreak(cache, 0)); } @@ -275,6 +286,7 @@ static int __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 int __process_type(struct state *state, struct die *cache, @@ -307,6 +319,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) @@ -361,6 +374,108 @@ static int process_subroutine_type(struct state *state, struct die *cache, return check(__process_subroutine_type(state, cache, die, "subroutine_type")); } +static int process_variant_type(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + return check(process_die_container(state, cache, die, process_type, + match_member_type)); +} + +static int process_variant_part_type(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + check(process(state, cache, "variant_part {")); + check(process_linebreak(cache, 1)); + check(process_die_container(state, cache, die, process_type, + match_variant_type)); + check(process_linebreak(cache, -1)); + check(process(state, cache, "},")); + return check(process_linebreak(cache, 0)); +} + +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: + check(process_type(state, cache, die)); + check(process(state, cache, ",")); + return check(process_linebreak(cache, 0)); + case DW_TAG_subprogram: + return 0; /* Skip member functions */ + default: + error("unexpected structure_type child: %x", dwarf_tag(die)); + return -1; + } +} + +static int __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) +{ + check(process(state, cache, type)); + check(process_fqn(state, cache, die)); + check(process(state, cache, " {")); + check(process_linebreak(cache, 1)); + + check(process_die_container(state, cache, die, process_func, + match_func)); + + check(process_linebreak(cache, -1)); + check(process(state, cache, "}")); + + check(process_byte_size_attr(state, cache, die)); + check(process_alignment_attr(state, cache, die)); + + return 0; +} + +#define DEFINE_PROCESS_STRUCTURE_TYPE(structure) \ + static int process_##structure##_type( \ + struct state *state, struct die *cache, Dwarf_Die *die) \ + { \ + return check(__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 int process_enumerator_type(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + Dwarf_Word value; + + check(process(state, cache, "enumerator ")); + check(process_fqn(state, cache, die)); + + if (get_udata_attr(die, DW_AT_const_value, &value)) { + check(process(state, cache, " = ")); + check(process_fmt(state, cache, "%" PRIu64, value)); + } + + check(process(state, cache, ",")); + return check(process_linebreak(cache, 0)); +} + +static int process_enumeration_type(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + return check(__process_structure_type(state, cache, die, + "enumeration_type ", process_type, + match_enumerator_type)); +} + static int process_base_type(struct state *state, struct die *cache, Dwarf_Die *die) { @@ -438,17 +553,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) default: - debug("unimplemented type: %x", tag); - break; + error("unexpected type: %x", tag); + return -1; } /* Update cache state and append to the parent (if any) */ diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h index 25f87485eb69..7d32ccd590f8 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -59,8 +59,13 @@ extern bool debug; #define checkp(expr) __check(expr, __res < 0, __res) /* 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 Aug 15 17:39:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13765046 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.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 D411A1BD00C for ; Thu, 15 Aug 2024 17:39:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743576; cv=none; b=j08EzncExnvJHYmVPvHbDT34ZR1EkdsxQuQS5+23rCks+Idag4+1NSA7MjTvoiTKKNLMiVWcj5pBjYCqiSgCH11svBRXg5vhsKa0kG0rX9wSqnHgKKQcXLNuruvcMsq0WgRDuJnlLGrQ8Wggua3yD6p1uxU1TFLx8ZlKj5onl20= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743576; c=relaxed/simple; bh=IoI4F3wObxGX4LgHQUFQQdoaz1TRyk0IHL+WJGOTKzw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RHYggb2dDoE/It2aszRjI+QkbmUHbbcNWgKVyoY4ak09HVXzKhGFTgWq8JMuVHSlNPmVfF8etoyHuGFsJzCpB2i/nzq449mv8v+V/y8gnllxOpcLkhcWliMzncM/U1zrcR6l7P2ckLy14xZPZ93CgvuH3/CYqJ6ODSBVwVc1AN4= 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=NJ/Q/NG2; arc=none smtp.client-ip=209.85.214.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="NJ/Q/NG2" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1fd8a1a75e7so11184445ad.3 for ; Thu, 15 Aug 2024 10:39:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723743573; x=1724348373; 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=T2I7FT7XC1CLS96AVMtQrrnQsP2LCK4IFkNimJHZT1k=; b=NJ/Q/NG23shoJgIH4MFnTUtlo/Wzb8dxs3MJxGTTwa4M3vs4LwXRvmXjsQtOdybzzN lrktVN3e+wrqXOxihQApH12PH2WUNHeAD3cN6uSjQ7B0BG9ipl5xfO43vYlxfFbQgaNf irmfXXH40uXFEviIq3hbzz96RDaEDdZwa2G69ELQkmQcaAzbsbeJCL6dAN80UTFDVtyD yTjX/fphPJaAWH1gTXj6aoqsJSv3z3YEGqpXSmbMOUQKkAVRsE7yvgVBGMw8wbS8uMCX JUJZCSK0iEJmHo08lSGiM+xdV4AE8xt/kpC0bWnPmYrIP4hfj+MzEXrcQ81r5pB4AAXb oHVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723743573; x=1724348373; 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=T2I7FT7XC1CLS96AVMtQrrnQsP2LCK4IFkNimJHZT1k=; b=PdxeAK0VaxcNUJJlBRmttl22FNt7QUTIqCgjrAtOI/Pcf7MU2szhAEj0odnjlvkYOf w/K/PYL0T59iABIDYSmd95EYKkDWiXZ930YAf0WkTAOJIz9f0bUN6ZaE8Vm+D+IYk6O3 r5WkB5mpnURfwqX8Xjbkm52c1deFHzEZ35zMtwDD7CQMg0rRDK5orHpu0XOgFOPaILd8 ysLX6isbpqR4FRTorC8vV8ZeNw6KzTRiZ/VlMj2G4x63qV7mraNxqch1dhxYmaQFNzZP /iPbpaOg+LUlwyzOn6i7thucviWM01a3WD1RXALLvsPVJROTf5b2qJxgaMxzHHuSKaGP XM1g== X-Forwarded-Encrypted: i=1; AJvYcCVgv5qob43BT4oBKRnOXxztBPDLZOicPPIphwDFGLblr6pHJZj9QS9mdEsUZOb/GDeC4k3Po0E3U5HcHjq41bVT/UaqyKcMk+f+dJt7 X-Gm-Message-State: AOJu0YytO1d/k+ytSoQ5Fzto9iULbepdrqqAOJg1GVVKzI8mxhrg7XIG dZhSawIyzCsJ6U+t9Kk+NFpnzokVf4eJ4m8a4vgoX9UHpJUiVcWOZPQfeIShgrm5D2BPuWhcodk yqkfWJjnHst/6MVfFzSEuz2wHqw== X-Google-Smtp-Source: AGHT+IGe9pliul9/TcQbfTFZYC3NvVraMnHCkZgqO0axZCCnb7OvgeE4/zJ34X3uG8qzfevmce3mUDn4mCzjbOC5sZU= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a17:903:110e:b0:1ff:4618:36b8 with SMTP id d9443c01a7336-20203f54191mr314165ad.11.1723743573052; Thu, 15 Aug 2024 10:39:33 -0700 (PDT) Date: Thu, 15 Aug 2024 17:39:15 +0000 In-Reply-To: <20240815173903.4172139-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240815173903.4172139-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=10707; i=samitolvanen@google.com; h=from:subject; bh=IoI4F3wObxGX4LgHQUFQQdoaz1TRyk0IHL+WJGOTKzw=; b=owEB7QES/pANAwAKAUy19oSLvFbuAcsmYgBmvj05p/xVSrDsTqSQ6aJyo7Ou8bMUUGlmuI7/2 cxH2uubobaJAbMEAAEKAB0WIQQ1zPtjsoPW0663g5RMtfaEi7xW7gUCZr49OQAKCRBMtfaEi7xW 7tdFDACpBQFkUOEnk3TqYP/yswobNakprEz/Ru4ffXB9Dk7Jgct6Y9xTZt7Qj6esbqqaFSSo2XA p7+/ODjP/CSOZA8YwFF4tupQRUZLOYkbHPJrRrYSEQxGSnUyNZqF+a9mWMprBrnKCuznwjF2bDG 8ejYX6mI99w0jD9wLF1jZCQGxyjWNs8fGLAOIoxvfgoF9cQtu4e76BA0mjc5M9KLT1jfTYtZ8Kb aimIyp+qwqNC9jz3uDLLntL4AOOerU49E7n1sfiwTzH0w8y+L+284Vdj8vkq03ax2VNKR4hnJlb g/a8dA5k+nKO+YFxaFIHPsm3DCmLXBeHJQ8nLpL+GwGJAfLDQnzJ5EzpgWyM0nuxQYWjy/LE2/C jjpXdaN0Sfzq9fVCGQfeiabKZG4yN2zL2LaLUGxSxtxavDLMoY2+8BiOk1zUUoA8PSdiKLzZMEZ Ssjzmf0xqIEwquRLsZhtz3/JFCmHkdAMlcv6lcj7rOG+PdOGvdAmkcff4Y6Ggu4EXHclM= X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240815173903.4172139-32-samitolvanen@google.com> Subject: [PATCH v2 11/19] gendwarfksyms: Limit structure expansion From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Wedson Almeida Filho , Gary Guo , Petr Pavlu , Neal Gompa , Hector Martin , Janne Grunau , Asahi Linux , 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, but is still sufficient for catching ABI changes. For pointers to structure types, limit type expansion inside the pointer to two levels. This should be plenty for detecting ABI differences, but it stops us from pulling in half the kernel for types that contain pointers to large kernel data structures, like task_struct, for example. Signed-off-by: Sami Tolvanen --- scripts/gendwarfksyms/Makefile | 1 + scripts/gendwarfksyms/cache.c | 51 ++++++++++++ scripts/gendwarfksyms/dwarf.c | 108 ++++++++++++++++++++++++-- scripts/gendwarfksyms/gendwarfksyms.h | 38 ++++++++- 4 files changed, 189 insertions(+), 9 deletions(-) create mode 100644 scripts/gendwarfksyms/cache.c diff --git a/scripts/gendwarfksyms/Makefile b/scripts/gendwarfksyms/Makefile index fcbac52ca00a..681b42441840 100644 --- a/scripts/gendwarfksyms/Makefile +++ b/scripts/gendwarfksyms/Makefile @@ -1,6 +1,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..0a4efdcb8cda --- /dev/null +++ b/scripts/gendwarfksyms/cache.c @@ -0,0 +1,51 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2024 Google LLC + */ + +#include "gendwarfksyms.h" + +struct expanded { + uintptr_t addr; + struct hlist_node hash; +}; + +int __cache_mark_expanded(struct expansion_cache *ec, uintptr_t addr) +{ + struct expanded *es; + + es = malloc(sizeof(struct expanded)); + if (!es) { + error("malloc failed"); + return -1; + } + + es->addr = addr; + hash_add(ec->cache, &es->hash, addr_hash(addr)); + return 0; +} + +bool __cache_was_expanded(struct expansion_cache *ec, uintptr_t addr) +{ + struct expanded *es; + + hash_for_each_possible(ec->cache, es, hash, addr_hash(addr)) { + if (es->addr == addr) + return true; + } + + return false; +} + +void cache_clear_expanded(struct expansion_cache *ec) +{ + struct hlist_node *tmp; + struct expanded *es; + int i; + + hash_for_each_safe(ec->cache, i, tmp, es, hash) { + free(es); + } + + hash_init(ec->cache); +} diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index 92b6ca4c5c91..2f1601015c4e 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -25,6 +25,7 @@ static int 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) @@ -69,6 +70,13 @@ static bool is_export_symbol(struct state *state, Dwarf_Die *die) return !!state->sym; } +static bool is_declaration(Dwarf_Die *die) +{ + bool value; + + return get_flag_attr(die, DW_AT_declaration, &value) && value; +} + /* * Type string processing */ @@ -421,19 +429,26 @@ static int __process_structure_type(struct state *state, struct die *cache, die_callback_t process_func, die_match_callback_t match_func) { + bool is_decl = is_declaration(die); + check(process(state, cache, type)); check(process_fqn(state, cache, die)); check(process(state, cache, " {")); check(process_linebreak(cache, 1)); - check(process_die_container(state, cache, die, process_func, - match_func)); + if (!is_decl && state->expand.expand) { + check(cache_mark_expanded(&state->expansion_cache, die->addr)); + check(process_die_container(state, cache, die, process_func, + match_func)); + } check(process_linebreak(cache, -1)); check(process(state, cache, "}")); - check(process_byte_size_attr(state, cache, die)); - check(process_alignment_attr(state, cache, die)); + if (!is_decl && state->expand.expand) { + check(process_byte_size_attr(state, cache, die)); + check(process_alignment_attr(state, cache, die)); + } return 0; } @@ -519,6 +534,42 @@ static int process_cached(struct state *state, struct die *cache, return 0; } +static void state_init(struct state *state) +{ + state->expand.expand = true; + state->expand.in_pointer_type = false; + state->expand.ptr_expansion_depth = 0; + hash_init(state->expansion_cache.cache); +} + +static void expansion_state_restore(struct expansion_state *state, + struct expansion_state *saved) +{ + state->ptr_expansion_depth = saved->ptr_expansion_depth; + state->in_pointer_type = saved->in_pointer_type; + 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_pointer_type(int tag) +{ + return tag == DW_TAG_pointer_type || tag == DW_TAG_reference_type; +} + +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; +} + +/* The maximum depth for expanding structures in pointers */ +#define MAX_POINTER_EXPANSION_DEPTH 2 + #define PROCESS_TYPE(type) \ case DW_TAG_##type##_type: \ check(process_##type##_type(state, cache, die)); \ @@ -526,18 +577,56 @@ static int 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 = COMPLETE; struct die *cache = NULL; + 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 the exported symbol contains a pointer to a structure, + * at most MAX_POINTER_EXPANSION_DEPTH levels are expanded into + * the referenced structure. + */ + state->expand.in_pointer_type = saved.in_pointer_type || + is_pointer_type(tag); + + if (state->expand.in_pointer_type && + state->expand.ptr_expansion_depth >= MAX_POINTER_EXPANSION_DEPTH) + state->expand.expand = false; + else + state->expand.expand = + saved.expand && + !cache_was_expanded(&state->expansion_cache, die->addr); + + /* Keep track of pointer expansion depth */ + if (state->expand.expand && state->expand.in_pointer_type && + is_expanded_type(tag)) + state->expand.ptr_expansion_depth++; + + /* + * If we have want_state already cached, use it instead of walking * through DWARF. */ - check(die_map_get(die, COMPLETE, &cache)); + if (!state->expand.expand && is_expanded_type(tag)) + want_state = UNEXPANDED; + + check(die_map_get(die, want_state, &cache)); + + if (cache->state == want_state) { + if (want_state == COMPLETE && is_expanded_type(tag)) + check(cache_mark_expanded(&state->expansion_cache, + die->addr)); - if (cache->state == COMPLETE) { check(process_cached(state, cache, die)); check(die_map_add_die(parent, cache)); + + expansion_state_restore(&state->expand, &saved); return 0; } @@ -578,9 +667,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 = COMPLETE; + cache->state = want_state; check(die_map_add_die(parent, cache)); + expansion_state_restore(&state->expand, &saved); return 0; } @@ -643,6 +733,7 @@ static int process_exported_symbols(struct state *state, struct die *cache, return 0; debug("%s", state->sym->name); + state_init(state); if (is_symbol_ptr(get_name(&state->die))) check(process_symbol_ptr(state, &state->die)); @@ -651,6 +742,7 @@ static int process_exported_symbols(struct state *state, struct die *cache, else check(process_variable(state, &state->die)); + cache_clear_expanded(&state->expansion_cache); return 0; default: return 0; diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h index 7d32ccd590f8..6482503e7d6e 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -106,7 +106,7 @@ extern struct symbol *symbol_get(const char *name); * die.c */ -enum die_state { INCOMPLETE, COMPLETE, LAST = COMPLETE }; +enum die_state { INCOMPLETE, UNEXPANDED, COMPLETE, LAST = COMPLETE }; enum die_fragment_type { EMPTY, STRING, LINEBREAK, DIE }; struct die_fragment { @@ -128,6 +128,7 @@ static inline const char *die_state_name(enum die_state state) switch (state) { default: CASE_CONST_TO_STR(INCOMPLETE) + CASE_CONST_TO_STR(UNEXPANDED) CASE_CONST_TO_STR(COMPLETE) } } @@ -150,15 +151,50 @@ extern int die_map_add_linebreak(struct die *pd, int linebreak); extern int die_map_add_die(struct die *pd, struct die *child); extern void die_map_free(void); +/* + * cache.c + */ + +#define EXPANSION_CACHE_HASH_BITS 11 + +/* A cache for addresses we've already seen. */ +struct expansion_cache { + DECLARE_HASHTABLE(cache, EXPANSION_CACHE_HASH_BITS); +}; + +extern int __cache_mark_expanded(struct expansion_cache *ec, uintptr_t addr); +extern bool __cache_was_expanded(struct expansion_cache *ec, uintptr_t addr); + +static inline int cache_mark_expanded(struct expansion_cache *ec, void *addr) +{ + return __cache_mark_expanded(ec, (uintptr_t)addr); +} + +static inline bool cache_was_expanded(struct expansion_cache *ec, void *addr) +{ + return __cache_was_expanded(ec, (uintptr_t)addr); +} + +extern void cache_clear_expanded(struct expansion_cache *ec); + /* * dwarf.c */ +struct expansion_state { + bool expand; + bool in_pointer_type; + unsigned int ptr_expansion_depth; +}; struct state { Dwfl_Module *mod; Dwarf *dbg; struct symbol *sym; Dwarf_Die die; + + /* Structure expansion */ + struct expansion_state expand; + struct expansion_cache expansion_cache; }; typedef int (*die_callback_t)(struct state *state, struct die *cache, From patchwork Thu Aug 15 17:39:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13765047 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 A290D1BD51D for ; Thu, 15 Aug 2024 17:39:35 +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=1723743577; cv=none; b=uVWrF2TAho5gXhgXyEjBXa19GgtCRus/5yWa9fduAcbTECQHqrd790XVcVkhU6EnGAlKSlinZ8FgEyP5T7X2IVRTlrkUWTeCMW1FLHkITBhQtFcTWRu92Dss6jOlsRKN6PDOqpeXZRB7ojIY4AM34qfBkJAwCTpTKNQOc6gxMPM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743577; c=relaxed/simple; bh=1jJqo9o9aQH+E0GjG/ROahqjvj9Bo5ZqYdph8BSQRt0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hw9j2os+t/yjs3TB9i/c4m3zKQxS7vlitro8Bz0+iJfQsOAESWiS7F7bimj1q4nlFakfkmG9H8kwOaNCRwTnKP9sqxMujgmldu2S07T9FNpw5tE7WX0zlCdVrLaakKnGU/c1RDVU7y/wuXfLEaxb5vquoqrQxloBhpxbUS0lRA4= 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=JZaHQqBH; 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="JZaHQqBH" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-1ff3dfaa090so10997985ad.3 for ; Thu, 15 Aug 2024 10:39:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723743575; x=1724348375; 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=qtbsIaIfhBiE1DbP5u+JRQZBtsqBwwZART8vtMh98sw=; b=JZaHQqBHQ8g6oatqcBrmCMmZdtVOOa78cVVRTvXj52y+P42i/0rKH+On7UefoKYCh3 AsTI/Av+lWN5icxUuFk392Ph+QbaasPydbuVRIL4SqklPXummpWmYdgvW89/l5QAE41M 8um2uFJJD8EDdg5A945eiktS5+EkUSHDzAv6H1iXGNpnTcGH9+6s0jn7x/UHGdo3MizN 3f+9ZwhwaqkqVGVs/dnj4U7XfBfyy5/lW/lTO5sEVraGmMkNfJN6k+T/V752xAgM5b+G Cz+s4CyIl2bAPBE73nZYl68U8aUWAj/NLemoD9cSEcG4HotoOBjDmsCfPIRLtSNSE/Uq tC/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723743575; x=1724348375; 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=qtbsIaIfhBiE1DbP5u+JRQZBtsqBwwZART8vtMh98sw=; b=a7FCZU9rLrAPOBBRt+6b2elve1mXggNSGxJXrPkqsn0yNAMCJc5QXTiwkFqUiupDxT /p1OU88zxUecXdPyQyERHymv/X8GsXkB4POEw+Al4x7oJ4yyLYxnBd59CZ+mDweIxmvA G3IhR9J6CM/FSEnMbN/xTj5Svna4zvRSkLElQLReuwaAjc7+Nw7dXDVhRajid0zXWF1d IhhRCelWtmsFav1Es36CW1qLzMtZparjpeb7TBeC1LCf/7mnlQ2/Ndhqd5/2uqtoXyOc zQKOgPiXl+FWfPIPcjyMko3kLj9ttRJ1U2MBU+YnDwK/Eup1uMXHiBI8SFiWZeOLkeea XZeg== X-Forwarded-Encrypted: i=1; AJvYcCWLoNb1AuSRX7OcHV0Tm+/OhbgCLINAhgvcV4ChiWaPOQZ6hNwN8QNP4TumGtm1j+OMrPhcotWlVR/3VWu/NWSMKm44NpfmA7SVq9JH X-Gm-Message-State: AOJu0YwETqx5e7cio/Z8yTZCLz9IirleTBi9HeZ9jUCJqaS8hUusq4/M varOMs4EXa7SHLjyr/kaUIDzIkXvPZ8juN7K91cdsYxmRacLpmy7iC56ScH50hRKTgY5wN/d78j HdZIa3NQE1m4nvq75rIz46YnrjA== X-Google-Smtp-Source: AGHT+IF2vutxQWYsriPKvF7iI6jHIZpZUjx/Iv3JjPdtDdO8O3OmwJ4gQIgzjZnRFjJRknlMxQakessYN+u5iq/0Q34= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a17:902:da8a:b0:1fb:8c26:4b17 with SMTP id d9443c01a7336-20203f31506mr8565ad.9.1723743574840; Thu, 15 Aug 2024 10:39:34 -0700 (PDT) Date: Thu, 15 Aug 2024 17:39:16 +0000 In-Reply-To: <20240815173903.4172139-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240815173903.4172139-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=5220; i=samitolvanen@google.com; h=from:subject; bh=1jJqo9o9aQH+E0GjG/ROahqjvj9Bo5ZqYdph8BSQRt0=; b=owEB7QES/pANAwAKAUy19oSLvFbuAcsmYgBmvj05p4mX3gDUPwZMYwEx1Enne0Ag7prR0Wlf9 GzJChEBdZ6JAbMEAAEKAB0WIQQ1zPtjsoPW0663g5RMtfaEi7xW7gUCZr49OQAKCRBMtfaEi7xW 7s/sC/9MpvmBZcBA9Rv5mHdI+WO1ut2X7/+YdUhXZtWBz5unbj8Q0jRG1WT0/AAKbXHhGFhIEMC X48h5BwZq8SlLLyqhRsZjVyp1HsmW2h+tl+Q4fUPl1fF5p+jdUakcqxl6m3nZF4lsL+9NWu5JyZ s+ui6D+VRcrnaanyw68hKvjPehdgh16dBL4JBaEkHwFZcb6kUfewAwnLS73rV/+mpMEtt/eppVg tcYwIelS5xQcn7l/B47w0V3vpKEmoubCXJePF1QGoDIsei9LcUbRJdMpJhMXJ8Cl2FmxhVMXbrX dfWpegajVmmUL3KBFjw6ew3AbdEJ7YR/K7ZGcEgik61UJ4FATwBDbBpxNmn3WOVQfo+6u4iEgwc 9sfUaIROxzykz0kzS8LTmURr4Rlboz2p+03LGzJRfTTfJC5Tfuxx/NXCRoO//+HDGfTJyhzUtM9 pIwZNM8rkonwJS7VzTs8pRDeL9rC5MF5DV664B/DXAxhOoyHXXefgyDXA8e87h3bmm6Hk= X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240815173903.4172139-33-samitolvanen@google.com> Subject: [PATCH v2 12/19] gendwarfksyms: Add die_map debugging From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Wedson Almeida Filho , Gary Guo , Petr Pavlu , Neal Gompa , Hector Martin , Janne Grunau , Asahi Linux , 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. Move parsed die_map output behind --dump-dies to clean up the --debug output, and add a --dump-die-map flag, which adds highlighted tags to the output about die_map operations. Signed-off-by: Sami Tolvanen --- scripts/gendwarfksyms/dwarf.c | 19 +++++++++++++++++-- scripts/gendwarfksyms/gendwarfksyms.c | 9 +++++++++ scripts/gendwarfksyms/gendwarfksyms.h | 14 ++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index 2f1601015c4e..9bca21a71639 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -84,15 +84,17 @@ static int process(struct state *state, struct die *cache, const char *s) { s = s ?: ""; - if (debug && do_linebreak) { + if (dump_dies && do_linebreak) { fputs("\n", stderr); for (int i = 0; i < indentation_level; i++) fputs(" ", stderr); do_linebreak = false; } - if (debug) + if (dump_dies) fputs(s, stderr); + if (cache) + die_debug_r("cache %p string '%s'", cache, s); return check(die_map_add_string(cache, s)); } @@ -510,6 +512,8 @@ static int process_cached(struct state *state, struct die *cache, while (df) { switch (df->type) { case STRING: + die_debug_b("cache %p STRING '%s'", cache, + df->data.str); check(process(state, NULL, df->data.str)); break; case LINEBREAK: @@ -522,6 +526,8 @@ static int process_cached(struct state *state, struct die *cache, error("dwarf_die_addr_die failed"); return -1; } + die_debug_b("cache %p DIE addr %" PRIxPTR " tag %d", + cache, df->data.addr, dwarf_tag(&child)); check(process_type(state, NULL, &child)); break; default: @@ -619,6 +625,9 @@ static int process_type(struct state *state, struct die *parent, Dwarf_Die *die) check(die_map_get(die, want_state, &cache)); if (cache->state == want_state) { + die_debug_g("cached addr %p tag %d -- %s", die->addr, tag, + die_state_name(cache->state)); + if (want_state == COMPLETE && is_expanded_type(tag)) check(cache_mark_expanded(&state->expansion_cache, die->addr)); @@ -630,6 +639,9 @@ static int process_type(struct state *state, struct die *parent, Dwarf_Die *die) return 0; } + die_debug_g("addr %p tag %d -- INCOMPLETE -> %s", die->addr, tag, + die_state_name(want_state)); + switch (tag) { /* Type modifiers */ PROCESS_TYPE(atomic) @@ -665,6 +677,9 @@ static int process_type(struct state *state, struct die *parent, Dwarf_Die *die) return -1; } + die_debug_r("parent %p cache %p die addr %p tag %d", 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 55a7fc902bf4..1349e592783b 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.c +++ b/scripts/gendwarfksyms/gendwarfksyms.c @@ -16,6 +16,10 @@ /* Print out debugging information to stderr */ bool debug; +/* Print out die_map contents */ +bool dump_dies; +/* Print out inline debugging information about die_map changes */ +bool dump_die_map; static const struct { const char *arg; @@ -23,6 +27,8 @@ static const struct { const char **param; } options[] = { { "--debug", &debug, NULL }, + { "--dump-dies", &dump_dies, NULL }, + { "--dump-die-map", &dump_die_map, NULL }, }; static int usage(void) @@ -111,6 +117,9 @@ int main(int argc, const char **argv) if (parse_options(argc, argv) < 0) return usage(); + if (dump_die_map) + dump_dies = true; + check(symbol_read_exports(stdin)); for (n = 0; n < object_count; n++) { diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h index 6482503e7d6e..7cd907e3d5e3 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -20,6 +20,8 @@ * Options -- in gendwarfksyms.c */ extern bool debug; +extern bool dump_dies; +extern bool dump_die_map; #define MAX_INPUT_FILES 128 @@ -40,6 +42,18 @@ extern bool debug; #define warn(format, ...) __println("warning: ", format, ##__VA_ARGS__) #define error(format, ...) __println("error: ", format, ##__VA_ARGS__) +#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 Aug 15 17:39:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13765048 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 69DD658AD0 for ; Thu, 15 Aug 2024 17:39:37 +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=1723743579; cv=none; b=STntMxUAn35GUHXRd+dlOcfj6lpkrZopaEpPESbd08igL6Br8jRifprZk0qrrnf0fd2MsSg+IjDFzPUHvHwrfLz1qmInbwT80RfRLl2wwdulbV41a23rg2ys7wxScvMhBUidFvzdtbftaInFiNwUOfFAIBc5Ebx/GxoH8mmekrU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743579; c=relaxed/simple; bh=3OGd53ci+q4WoJQ7zoiq5FjPRZnrf/+Zl9zq4u0fJ80=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Um+MYjCc09pyC/rOe4BIbU8yIlG0XagrvrgRCb2nOzDYith9rQ+XAXNx/2vIiMWO5gqQZazfJqioV1ROF0G9Xv5kSQO4z6h460+iAsvAcWxJ7INY2w5O/OTTh4TcmKynq0GInjB3NlZylq5DWohqDu9jTR8cPmSDbZo4YHPPyoA= 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=zHOvvhAy; 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="zHOvvhAy" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-6e67742bee6so662515a12.1 for ; Thu, 15 Aug 2024 10:39:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723743577; x=1724348377; 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=R0+ZG5cfBXWxYUBN+iDOhSdjTCyCSwKpy98HChPuRi4=; b=zHOvvhAy8NrVa5Ur3z+Fwcj2vV0mJjZMOd2T43zbCL5N9LottMfeDQfm4uqctHdBXp BrOrmRGYXUKxYC88lMPuLdDbh63rLfipdSJti2pnSqhDonFiiGkEY1zK8gLdP/6XU+rH 4uzNqpOlTTsrL3hnZtMrovkYY6Fx1uBz27EefoKkAG1D2x6VQ0oInJ26KC+Xtg6qiuEH /zyhc5pHGwENjQFigF6DU9zslLzbH9HAN8q5jivR5HVUyhlMiPQ8Qs2pdVkXTXaWTRzy TA23/KBZd0Gu/aECnkbyT3zAMG5JUP2dNlAJCCXmsvil8TAC3MMnTY0age94Z5d8CauY 8jwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723743577; x=1724348377; 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=R0+ZG5cfBXWxYUBN+iDOhSdjTCyCSwKpy98HChPuRi4=; b=Gc0PHlKMx71/dCKyZlGEyaU4CwWV6l2P0Q4k8CxL+kWekqKwKFM8v7zAFv543Fv4k5 EFMXNrTxMCn2ZHQtksvFQQge4fYCktsZVaJ940ym1AuerzzI4sAsTmCkjyvnbAqxfNqC s2JpnoxYAyF2c3JcTJJZ0isbSpdOzsFEkJkRgxrA85SUCCTiPFH1kggCI2z2VVGWdM4p yf88QBmpo5rxEDABuN4YLCA9ybnwcUURw2jowOk9cgVWeXHpwFS1oUYoJPw5TdwYJzAB Q7xR3actRGRk1hrSY8Al+wf5Ia4a8kPoGvZUo/jJtrd1UO7xTg5XY7zUOC3yoNKyU9Jj jFuA== X-Forwarded-Encrypted: i=1; AJvYcCWDYJX8abti0nrImohSReaCjCMSpanxO7wy7v4bijhYCQa9eEvp0kDzEMhAjYOtjUvKXjoQhcWozKfbgk80IWVbnQza7rjdEKlakqpp X-Gm-Message-State: AOJu0YyPz9ChjAW9VFfK0wm4vlt5RZ1qKRNXFuUWniqx7H3gw312aTGq XiFHEZxEPFsYWxGtc5eQhYL2/89KAeOS62Xe5JBuwbJaJ9/hMispeQ+aNHk7ZgbL0TPOc4p4dnK C4uUnOQbBjZaVh2ZBxuhZ1CMXcQ== X-Google-Smtp-Source: AGHT+IEepXE2gDbPPAKQup7O1RDQKgqeMe4NyUQa83ZG0Cq67RXgdeF+09vux/MkGzQQSEDEiZkeA0UJvZni1RWq4M4= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a17:90b:19cf:b0:2d3:b3d3:9268 with SMTP id 98e67ed59e1d1-2d3dfac6178mr3402a91.0.1723743576435; Thu, 15 Aug 2024 10:39:36 -0700 (PDT) Date: Thu, 15 Aug 2024 17:39:17 +0000 In-Reply-To: <20240815173903.4172139-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240815173903.4172139-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=19478; i=samitolvanen@google.com; h=from:subject; bh=3OGd53ci+q4WoJQ7zoiq5FjPRZnrf/+Zl9zq4u0fJ80=; b=owEB7QES/pANAwAKAUy19oSLvFbuAcsmYgBmvj05aX7jT8BakrBurFol6kOrSU66GOurimWs6 hHHTgRIJ2aJAbMEAAEKAB0WIQQ1zPtjsoPW0663g5RMtfaEi7xW7gUCZr49OQAKCRBMtfaEi7xW 7ksSC/wJar82tdMdN5LwANOWeWbfeG8Ec0lJY4txtaBNntno/uLr2yeatJ8+//KE93k7EY5zted fmIfp56jRtaytcegBrL2CXrZeiWy85zecrQNsF7bAltiDgGLGOYonIGF4mQVrxQWZXPVALA9g+o Kpdct6Aahpy0UHLYEiMq9rAh92DqeC1ZC9GOg7YlkPPEZiipNGNfmXYd9IWZBsyM2tCxwcVR2jU qAQwhR7IC00bI4m6WaKGo6zGVOz8ANRMEfj3HwjsZTc7iCN/SNJRjBfOtaMrtZR27Zi3PxoNhWO dGvRx5dd/XxeEID3lnbfPT286/Md7M8+FBN5wSGkXngY4FoubjUrFJ9AeH23YVtHKNidBfLCza5 xWuvqbraXKi1+AAd3y1l1J9E5Zb1qC/Yx845vakV0KtzuIdDQM+EJlmCX76+fkSlsXM0eyp7c7G rIsV3upLFnFtE7gKgu7Kin0Ptl6zTJbkn9xM6jlsEsutrqiLblqgBAX5QA/lhhM7ZTveA= X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240815173903.4172139-34-samitolvanen@google.com> Subject: [PATCH v2 13/19] gendwarfksyms: Add symtypes output From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Wedson Almeida Filho , Gary Guo , Petr Pavlu , Neal Gompa , Hector Martin , Janne Grunau , Asahi Linux , 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 --- scripts/gendwarfksyms/Makefile | 1 + scripts/gendwarfksyms/die.c | 13 + scripts/gendwarfksyms/dwarf.c | 14 +- scripts/gendwarfksyms/gendwarfksyms.c | 28 +- scripts/gendwarfksyms/gendwarfksyms.h | 21 +- scripts/gendwarfksyms/symbols.c | 11 +- scripts/gendwarfksyms/types.c | 439 ++++++++++++++++++++++++++ 7 files changed, 517 insertions(+), 10 deletions(-) create mode 100644 scripts/gendwarfksyms/types.c diff --git a/scripts/gendwarfksyms/Makefile b/scripts/gendwarfksyms/Makefile index 681b42441840..4866a2fd0e46 100644 --- a/scripts/gendwarfksyms/Makefile +++ b/scripts/gendwarfksyms/Makefile @@ -5,6 +5,7 @@ gendwarfksyms-objs += cache.o gendwarfksyms-objs += die.o gendwarfksyms-objs += dwarf.o gendwarfksyms-objs += symbols.o +gendwarfksyms-objs += types.o HOST_EXTRACFLAGS := -I $(srctree)/tools/include HOSTLDLIBS_gendwarfksyms := -ldw -lelf diff --git a/scripts/gendwarfksyms/die.c b/scripts/gendwarfksyms/die.c index fdd52df88fdd..e40f04b70f7f 100644 --- a/scripts/gendwarfksyms/die.c +++ b/scripts/gendwarfksyms/die.c @@ -85,6 +85,19 @@ static void reset_die(struct die *cd) cd->list = NULL; } +int die_map_for_each(die_map_callback_t func, void *arg) +{ + struct die *cd; + struct hlist_node *tmp; + int i; + + hash_for_each_safe(die_map, i, tmp, cd, hash) { + check(func(cd, arg)); + } + + return 0; +} + void die_map_free(void) { struct hlist_node *tmp; diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index 9bca21a71639..62241cc97a76 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -60,11 +60,11 @@ static bool is_export_symbol(struct state *state, Dwarf_Die *die) if (get_ref_die_attr(die, DW_AT_abstract_origin, &origin)) source = &origin; - state->sym = symbol_get(get_name(die)); + state->sym = symbol_get_unprocessed(get_name(die)); /* Look up using the origin name if there are no matches. */ if (!state->sym && source != die) - state->sym = symbol_get(get_name(source)); + state->sym = symbol_get_unprocessed(get_name(source)); state->die = *source; return !!state->sym; @@ -384,6 +384,7 @@ static int process_subroutine_type(struct state *state, struct die *cache, return check(__process_subroutine_type(state, cache, die, "subroutine_type")); } + static int process_variant_type(struct state *state, struct die *cache, Dwarf_Die *die) { @@ -695,14 +696,16 @@ static int process_type(struct state *state, struct die *parent, Dwarf_Die *die) static int process_subprogram(struct state *state, Dwarf_Die *die) { check(__process_subroutine_type(state, NULL, die, "subprogram")); - return check(process(state, NULL, ";\n")); + state->sym->state = MAPPED; + return 0; } static int process_variable(struct state *state, Dwarf_Die *die) { check(process(state, NULL, "variable ")); check(process_type_attr(state, NULL, die)); - return check(process(state, NULL, ";\n")); + state->sym->state = MAPPED; + return 0; } static int process_symbol_ptr(struct state *state, Dwarf_Die *die) @@ -757,6 +760,9 @@ static int process_exported_symbols(struct state *state, struct die *cache, else check(process_variable(state, &state->die)); + if (dump_dies) + fputs("\n", stderr); + cache_clear_expanded(&state->expansion_cache); return 0; default: diff --git a/scripts/gendwarfksyms/gendwarfksyms.c b/scripts/gendwarfksyms/gendwarfksyms.c index 1349e592783b..6a219a54c342 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.c +++ b/scripts/gendwarfksyms/gendwarfksyms.c @@ -20,6 +20,11 @@ bool debug; bool dump_dies; /* Print out inline debugging information about die_map changes */ bool dump_die_map; +/* Print out type_map contents */ +bool dump_types; +/* Produce a symtypes file */ +bool symtypes; +static const char *symtypes_file; static const struct { const char *arg; @@ -29,6 +34,8 @@ static const struct { { "--debug", &debug, NULL }, { "--dump-dies", &dump_dies, NULL }, { "--dump-die-map", &dump_die_map, NULL }, + { "--dump-types", &dump_types, NULL }, + { "--symtypes", &symtypes, &symtypes_file }, }; static int usage(void) @@ -79,6 +86,7 @@ static int process_modules(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); @@ -100,6 +108,10 @@ static int process_modules(Dwfl_Module *mod, void **userdata, const char *name, check(process_module(mod, dbg, &cudie)); } while (cu); + /* + * Use die_map to expand type strings and write them to `symfile`. + */ + check(generate_symtypes(symfile)); die_map_free(); return DWARF_CB_OK; @@ -112,6 +124,7 @@ static const Dwfl_Callbacks callbacks = { int main(int argc, const char **argv) { + FILE *symfile = NULL; unsigned int n; if (parse_options(argc, argv) < 0) @@ -122,6 +135,16 @@ int main(int argc, const char **argv) check(symbol_read_exports(stdin)); + if (symtypes_file) { + symfile = fopen(symtypes_file, "w+"); + + if (!symfile) { + error("fopen failed for '%s': %s", symtypes_file, + strerror(errno)); + return -1; + } + } + for (n = 0; n < object_count; n++) { Dwfl *dwfl; int fd; @@ -151,7 +174,7 @@ int main(int argc, const char **argv) dwfl_report_end(dwfl, NULL, NULL); - if (dwfl_getmodules(dwfl, &process_modules, NULL, 0)) { + if (dwfl_getmodules(dwfl, &process_modules, symfile, 0)) { error("dwfl_getmodules failed for '%s'", object_files[n]); return -1; @@ -161,5 +184,8 @@ int main(int argc, const char **argv) close(fd); } + if (symfile) + fclose(symfile); + return 0; } diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h index 7cd907e3d5e3..6edbd6478e0f 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -22,6 +22,8 @@ extern bool debug; extern bool dump_dies; extern bool dump_die_map; +extern bool dump_types; +extern bool symtypes; #define MAX_INPUT_FILES 128 @@ -89,6 +91,12 @@ extern bool dump_die_map; #define SYMBOL_PTR_PREFIX "__gendwarfksyms_ptr_" #define SYMBOL_PTR_PREFIX_LEN (sizeof(SYMBOL_PTR_PREFIX) - 1) +/* See dwarf.c:is_declaration */ +#define SYMBOL_DECLONLY_PREFIX "__gendwarfksyms_declonly_" +#define SYMBOL_DECLONLY_PREFIX_LEN (sizeof(SYMBOL_DECLONLY_PREFIX) - 1) + +enum symbol_state { UNPROCESSED, MAPPED }; + struct symbol_addr { uint32_t section; Elf64_Addr address; @@ -109,12 +117,14 @@ struct symbol { struct symbol_addr addr; struct hlist_node addr_hash; struct hlist_node name_hash; + enum symbol_state state; + uintptr_t die_addr; }; extern bool is_symbol_ptr(const char *name); extern int symbol_read_exports(FILE *file); extern int symbol_read_symtab(int fd); -extern struct symbol *symbol_get(const char *name); +extern struct symbol *symbol_get_unprocessed(const char *name); /* * die.c @@ -157,12 +167,15 @@ struct die { struct hlist_node hash; }; +typedef int (*die_map_callback_t)(struct die *, void *arg); + extern int __die_map_get(uintptr_t addr, enum die_state state, struct die **res); extern int die_map_get(Dwarf_Die *die, enum die_state state, struct die **res); extern int die_map_add_string(struct die *pd, const char *str); extern int die_map_add_linebreak(struct die *pd, int linebreak); extern int die_map_add_die(struct die *pd, struct die *child); +extern int die_map_for_each(die_map_callback_t func, void *arg); extern void die_map_free(void); /* @@ -222,4 +235,10 @@ extern int process_die_container(struct state *state, struct die *cache, extern int process_module(Dwfl_Module *mod, Dwarf *dbg, Dwarf_Die *cudie); +/* + * types.c + */ + +extern int generate_symtypes(FILE *file); + #endif /* __GENDWARFKSYMS_H */ diff --git a/scripts/gendwarfksyms/symbols.c b/scripts/gendwarfksyms/symbols.c index d6d016458ae1..8cc04e6295a7 100644 --- a/scripts/gendwarfksyms/symbols.c +++ b/scripts/gendwarfksyms/symbols.c @@ -117,6 +117,7 @@ int symbol_read_exports(FILE *file) sym->name = name; sym->addr.section = SHN_UNDEF; + sym->state = UNPROCESSED; name = NULL; hash_add(symbol_names, &sym->name_hash, name_hash(sym->name)); @@ -132,19 +133,21 @@ int symbol_read_exports(FILE *file) return 0; } -static int get_symbol(struct symbol *sym, void *arg) +static int get_unprocessed(struct symbol *sym, void *arg) { struct symbol **res = arg; - *res = sym; + if (sym->state == UNPROCESSED) + *res = sym; + return 0; } -struct symbol *symbol_get(const char *name) +struct symbol *symbol_get_unprocessed(const char *name) { struct symbol *sym = NULL; - for_each(name, false, get_symbol, &sym); + for_each(name, false, get_unprocessed, &sym); return sym; } diff --git a/scripts/gendwarfksyms/types.c b/scripts/gendwarfksyms/types.c new file mode 100644 index 000000000000..7b9997d8322d --- /dev/null +++ b/scripts/gendwarfksyms/types.c @@ -0,0 +1,439 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2024 Google LLC + */ + +#include "gendwarfksyms.h" +#include "crc32.h" + +static struct expansion_cache expansion_cache; + +/* + * A simple linked list of shared or owned strings to avoid copying strings + * around when not necessary. + */ +struct type_list { + const char *str; + void *owned; + struct type_list *next; +}; + +static struct type_list *type_list_alloc(void) +{ + struct type_list *list; + + list = calloc(1, sizeof(struct type_list)); + if (!list) + error("calloc failed"); + + return list; +} + +static void type_list_free(struct type_list *list) +{ + struct type_list *tmp; + + while (list) { + if (list->owned) + free(list->owned); + + tmp = list; + list = list->next; + free(tmp); + } +} + +static int type_list_append(struct type_list *list, const char *s, void *owned) +{ + if (!list || !s) + return 0; + + while (list->next) + list = list->next; + + if (list->str) { + list->next = type_list_alloc(); + + if (!list->next) { + error("type_list_alloc failed"); + return -1; + } + + list = list->next; + } + + list->str = s; + list->owned = owned; + + return strlen(list->str); +} + +static int type_list_write(struct type_list *list, FILE *file) +{ + while (list) { + if (list->str) + checkp(fputs(list->str, file)); + list = list->next; + } + + return 0; +} + +/* + * An expanded type string in symtypes format. + */ +struct type_expansion { + char *name; + struct type_list *expanded; + struct type_list *last; + size_t len; + struct hlist_node hash; +}; + +static int type_expansion_init(struct type_expansion *type, bool alloc) +{ + memset(type, 0, sizeof(struct type_expansion)); + if (alloc) { + type->expanded = type_list_alloc(); + if (!type->expanded) + return -1; + + type->last = type->expanded; + } + return 0; +} + +static inline void type_expansion_free(struct type_expansion *type) +{ + free(type->name); + type_list_free(type->expanded); + type_expansion_init(type, false); +} + +static int type_expansion_append(struct type_expansion *type, const char *s, + void *owned) +{ + type->len += checkp(type_list_append(type->last, s, owned)); + + if (type->last->next) + type->last = type->last->next; + + return 0; +} + +/* + * type_map -- the longest expansions for each type. + * + * const char *name -> struct type_expansion * + */ +#define TYPE_HASH_BITS 16 +static DEFINE_HASHTABLE(type_map, 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, name_hash(name)) { + if (!strcmp(name, e->name)) { + *res = e; + return 0; + } + } + + return -1; +} + +static int type_map_add(const char *name, struct type_expansion *type) +{ + struct type_expansion *e; + + if (type_map_get(name, &e)) { + e = malloc(sizeof(struct type_expansion)); + if (!e) { + error("malloc failed"); + return -1; + } + + type_expansion_init(e, false); + + e->name = strdup(name); + if (!e->name) { + error("strdup failed"); + return -1; + } + + hash_add(type_map, &e->hash, name_hash(e->name)); + + if (dump_types) + debug("adding %s", e->name); + } else { + /* Use the longest available expansion */ + if (type->len <= e->len) + return 0; + + type_list_free(e->expanded); + + if (dump_types) + debug("replacing %s", e->name); + } + + /* Take ownership of type->expanded */ + e->expanded = type->expanded; + e->last = type->last; + e->len = type->len; + type->expanded = NULL; + type->last = NULL; + type->len = 0; + + if (dump_types) { + fputs(e->name, stderr); + fputs(" ", stderr); + type_list_write(e->expanded, stderr); + fputs("\n", stderr); + } + + return 0; +} + +static int type_map_write(FILE *file) +{ + struct type_expansion *e; + struct hlist_node *tmp; + int i; + + if (!file) + return 0; + + hash_for_each_safe(type_map, i, tmp, e, hash) { + checkp(fputs(e->name, file)); + checkp(fputs(" ", file)); + type_list_write(e->expanded, file); + checkp(fputs("\n", file)); + } + + return 0; +} + +static void type_map_free(void) +{ + struct type_expansion *e; + struct hlist_node *tmp; + int i; + + hash_for_each_safe(type_map, i, tmp, e, 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 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) { + 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 *format; + char prefix; + char *name; + size_t len; + + if (cache->state == INCOMPLETE) { + warn("found incomplete cache entry: %p", cache); + return NULL; + } + if (!cache->fqn) + return NULL; + + prefix = get_type_prefix(cache->tag); + if (!prefix) + return NULL; + + /* #\0 */ + len = 2 + strlen(cache->fqn) + 1; + + /* Wrap names with spaces in single quotes */ + if (strstr(cache->fqn, " ")) { + format = "%c#'%s'"; + len += 2; + } else { + format = "%c#%s"; + } + + name = malloc(len); + if (!name) { + error("malloc failed"); + return NULL; + } + + if (snprintf(name, len, format, prefix, cache->fqn) >= len) { + error("snprintf failed for '%s' (length %zu)", cache->fqn, + len); + free(name); + return NULL; + } + + return name; +} + +static int __type_expand(struct die *cache, struct type_expansion *type, + bool recursive); + +static int 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) + return check(__type_expand(cache, type, recursive)); + + if (recursive && !__cache_was_expanded(&expansion_cache, cache->addr)) { + check(__cache_mark_expanded(&expansion_cache, cache->addr)); + check(type_expansion_init(&child, true)); + check(__type_expand(cache, &child, true)); + check(type_map_add(name, &child)); + type_expansion_free(&child); + } + + check(type_expansion_append(type, name, name)); + return 0; +} + +static int __type_expand(struct die *cache, struct type_expansion *type, + bool recursive) +{ + struct die_fragment *df = cache->list; + struct die *child; + + while (df) { + switch (df->type) { + case STRING: + check(type_expansion_append(type, df->data.str, NULL)); + break; + case DIE: + /* Use a complete die_map expansion if available */ + if (__die_map_get(df->data.addr, COMPLETE, &child) && + __die_map_get(df->data.addr, UNEXPANDED, &child)) { + error("unknown child: %" PRIxPTR, + df->data.addr); + return -1; + } + + check(type_expand_child(child, type, recursive)); + break; + case LINEBREAK: + /* + * Keep whitespace in the symtypes format, but avoid + * repeated spaces. + */ + if (!df->next || df->next->type != LINEBREAK) + check(type_expansion_append(type, " ", NULL)); + break; + default: + error("empty die_fragment in %p", cache); + return -1; + } + + df = df->next; + } + + return 0; +} + +static int type_expand(struct die *cache, struct type_expansion *type, + bool recursive) +{ + check(type_expansion_init(type, true)); + check(__type_expand(cache, type, recursive)); + cache_clear_expanded(&expansion_cache); + return 0; +} + +static int expand_type(struct die *cache, void *arg) +{ + struct type_expansion type; + char *name; + + /* + * Skip unexpanded die_map entries if there's a complete + * expansion available for this DIE. + */ + if (cache->state == UNEXPANDED) + __die_map_get(cache->addr, COMPLETE, &cache); + + if (cache->mapped) + return 0; + + cache->mapped = true; + + name = get_type_name(cache); + if (!name) + return 0; + + debug("%s", name); + check(type_expand(cache, &type, true)); + check(type_map_add(name, &type)); + + type_expansion_free(&type); + free(name); + + return 0; +} + +int generate_symtypes(FILE *file) +{ + hash_init(expansion_cache.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. + */ + check(die_map_for_each(expand_type, NULL)); + + /* + * 2. If a symtypes file is requested, write type_map contents to + * the file. + */ + check(type_map_write(file)); + type_map_free(); + + return 0; +} From patchwork Thu Aug 15 17:39:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13765049 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.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 1CB9B1BE22B for ; Thu, 15 Aug 2024 17:39:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743581; cv=none; b=stAs0ds1GEPQyAHYQW8sw/677wDa7RlGHq+6SaGl7hLsJsHrFDm0jTgwhb5roVsbeO5D5MX1SHV+RIhV9Su50Ee2LRqJFTdimkaNGVOnyrKOTYV75+fDCSNOIIRMiDJqoa27iKpjjUh5Hre5rrBZNZvhBBTDKottOlCR+nw5LTc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743581; c=relaxed/simple; bh=b0pFv5khycwPrpj+Sp91w4FE2mSAFUzDjAXnCfWTad8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mdTdS0KN7VO+kSJuruYVb5wQtqeYCvv3CKJBwsQTSdDcDi2Rwwz+IK+5zUKSKQsNHiSUmT2Lh6MZTyFAexaa2XhXUJcMryvRh5TTwgV+5weaq7kXSwb3ef0168+f/ieGCWvWh8GJWMPRFB/GS5gsDuECXyTn0P71ZIpp8ukMDZ0= 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=a+C1CQ5g; arc=none smtp.client-ip=209.85.214.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="a+C1CQ5g" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1fc6db23c74so11075715ad.0 for ; Thu, 15 Aug 2024 10:39:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723743578; x=1724348378; 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=YxqJQnTjz3Mvxi0XpqIt/w4r1X6ESLhfJAqdHv9SK3k=; b=a+C1CQ5gX7oPW+wf7camqzqCA1kagAUSyYDj7R0/NXQJsZ1Z9DttAmFAq9TXd3xiSv FZru5u/Kc63+o2NU71+ljMzmH9Mpu79E+05YRqZYuYPyoGNkn3l6mVmpW8QYYaKWoDxo Xqd8O9Y5a/8jMnnalDXyRqbNaDYLRzph/fxfkwNz2CIfFGMUk3fr+0ZJA4I0lOOw/57+ vNrCUHwhyBF4TibbaPImZpzFFR1buo2rS3DzWGrPeif/zY7pfB9UwwZg4nU1v9qpTW45 vzEnvfO5YsLs6ZdxXtg1CKLwEi2dS2TNJ3fW9nN5QDuuPryf1/vUCxdu99G1jFM+nexd mC7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723743578; x=1724348378; 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=YxqJQnTjz3Mvxi0XpqIt/w4r1X6ESLhfJAqdHv9SK3k=; b=UXccMKSHDHbEf9/4cm1bvuuD8J9cmZbyeAS9c86vPNdLSAhl/yRTrYY3ees0kLRkd5 H3xg2k9J/7MKe4SLSC1NpN1mhGmmPCEPxWsyigGig5I9TPvnCLdwNcyUjhpxrO6HcwcS I6UTMpMlvvOtacX8BnY67TbOXcw2sVLP0/aNddeMP/jXI/UhNZ+dO+VKUZwSAiW8PBhl 0jjsCdE7v2+RNnl/KCJbitA5fz0O81IjmhLs5MUKls60UftRZEX4bNW0R0TkcXpPmyCy /ZsoE8/PfyMajI8V4bsJmciaHJSNlrk0Ydsw5l+US2qFrbNCDfXoRzOQHQNL/TmS/fac oWFg== X-Forwarded-Encrypted: i=1; AJvYcCV/vqVfqP4D+CyskhSUhDK7nava7TJmVKJ9aqgDa3jRMs14MawpJJQHGzj706WATv8ex2AWx0ONjnhV/mIGSbH2cgQuf6bewqP8glaK X-Gm-Message-State: AOJu0Yxrld5TMfglWGhKzCduP+dBdhz/eLx1xSKBAsjVGefAxiTzs7Jr BR9zrgm6zlRIdo1Gex3SLW7pNNfiRWY6M/fjszgU8fP609XxNqALFf4leqd+Uqs5vJeKCOswnEr VjFNzLROkCagMetJhlWjkwNdb2Q== X-Google-Smtp-Source: AGHT+IGF3I1u9mSx+8xCf1DA450XBvZOmSvJwKdoxJ1GRnRyz1OEjqQVcI5TNj2KVF0Ba1LZT+LRkrCRzG1G2A961vk= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a17:902:ce91:b0:1fb:57e7:5bbf with SMTP id d9443c01a7336-20203f35821mr339605ad.11.1723743578505; Thu, 15 Aug 2024 10:39:38 -0700 (PDT) Date: Thu, 15 Aug 2024 17:39:18 +0000 In-Reply-To: <20240815173903.4172139-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240815173903.4172139-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=19079; i=samitolvanen@google.com; h=from:subject; bh=b0pFv5khycwPrpj+Sp91w4FE2mSAFUzDjAXnCfWTad8=; b=owEB7QES/pANAwAKAUy19oSLvFbuAcsmYgBmvj05IDIGHnL6HkFJ0lHzVWy/qfqDTrqWKCAAn wzncuwsoq+JAbMEAAEKAB0WIQQ1zPtjsoPW0663g5RMtfaEi7xW7gUCZr49OQAKCRBMtfaEi7xW 7nU3C/48GVfofDpfkjx+EcecVtIoLr+wvOn5J9Ks6YegliWjbSfiI+WXC8XKL7rZoeyIbN1ZhTc kC/dRkyFXKtg4iyAEjeuN9ZQ6zdvh8UgVPo2A+Bn0a6bmgyBZNvpRLZi9i8w5yCHOT3jgb8as/W vQmLP01/vBvaGaiq2fR+ZW0TNRoNGdggWojQzDp3FiLm7pd6twAL2sc9PxsgI0WdbwSj1AlKFzw phDtvlhG+Ot928zpCuT2hKJH12/KIRf/TwIsIDBE81NZSy5Z3ki7a4o42e5zB1s15TbsPTy9ZDp HsbdP06W5AAz8UJsSftWcQDjLkKB5oOtYsmMBGuOLhu4paULacgvv1sW7pGUVeb6zMrg8cgoQHW ShLfv4R0Vws8j7Q3zxaRXU3hYfrktMDy3i1KZOk1UTnF6hm+bEXCA+lLyktt+Pt0oJOBME+K3eD kOv9irZT9n9lDhsht9OW/n5AJ/23VK5sdue06S8I9IOwIatjOo7o+f/fmmh4s+yWjoFXs= X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240815173903.4172139-35-samitolvanen@google.com> Subject: [PATCH v2 14/19] gendwarfksyms: Add symbol versioning From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Wedson Almeida Filho , Gary Guo , Petr Pavlu , Neal Gompa , Hector Martin , Janne Grunau , Asahi Linux , 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 CRC32 implementation adapted from genksyms, and produce matching output from symtypes strings in type_map. Signed-off-by: Sami Tolvanen --- scripts/gendwarfksyms/Makefile | 1 + scripts/gendwarfksyms/crc32.c | 69 ++++++++++++++ scripts/gendwarfksyms/crc32.h | 34 +++++++ scripts/gendwarfksyms/dwarf.c | 47 ++++++++-- scripts/gendwarfksyms/gendwarfksyms.c | 10 ++- scripts/gendwarfksyms/gendwarfksyms.h | 15 +++- scripts/gendwarfksyms/symbols.c | 65 ++++++++++++++ scripts/gendwarfksyms/types.c | 124 +++++++++++++++++++++++++- 8 files changed, 348 insertions(+), 17 deletions(-) create mode 100644 scripts/gendwarfksyms/crc32.c create mode 100644 scripts/gendwarfksyms/crc32.h diff --git a/scripts/gendwarfksyms/Makefile b/scripts/gendwarfksyms/Makefile index 4866a2fd0e46..2043601df736 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 += crc32.o gendwarfksyms-objs += die.o gendwarfksyms-objs += dwarf.o gendwarfksyms-objs += symbols.o diff --git a/scripts/gendwarfksyms/crc32.c b/scripts/gendwarfksyms/crc32.c new file mode 100644 index 000000000000..23b328cd74f2 --- /dev/null +++ b/scripts/gendwarfksyms/crc32.c @@ -0,0 +1,69 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Based on scripts/genksyms/genksyms.c, which has the following + * notice: + * + * Generate kernel symbol version hashes. + * Copyright 1996, 1997 Linux International. + * + * New implementation contributed by Richard Henderson + * Based on original work by Bjorn Ekwall + * + * This file was part of the Linux modutils 2.4.22: moved back into the + * kernel sources by Rusty Russell/Kai Germaschewski. + */ + +const unsigned int crctab32[] = { + 0x00000000U, 0x77073096U, 0xee0e612cU, 0x990951baU, 0x076dc419U, + 0x706af48fU, 0xe963a535U, 0x9e6495a3U, 0x0edb8832U, 0x79dcb8a4U, + 0xe0d5e91eU, 0x97d2d988U, 0x09b64c2bU, 0x7eb17cbdU, 0xe7b82d07U, + 0x90bf1d91U, 0x1db71064U, 0x6ab020f2U, 0xf3b97148U, 0x84be41deU, + 0x1adad47dU, 0x6ddde4ebU, 0xf4d4b551U, 0x83d385c7U, 0x136c9856U, + 0x646ba8c0U, 0xfd62f97aU, 0x8a65c9ecU, 0x14015c4fU, 0x63066cd9U, + 0xfa0f3d63U, 0x8d080df5U, 0x3b6e20c8U, 0x4c69105eU, 0xd56041e4U, + 0xa2677172U, 0x3c03e4d1U, 0x4b04d447U, 0xd20d85fdU, 0xa50ab56bU, + 0x35b5a8faU, 0x42b2986cU, 0xdbbbc9d6U, 0xacbcf940U, 0x32d86ce3U, + 0x45df5c75U, 0xdcd60dcfU, 0xabd13d59U, 0x26d930acU, 0x51de003aU, + 0xc8d75180U, 0xbfd06116U, 0x21b4f4b5U, 0x56b3c423U, 0xcfba9599U, + 0xb8bda50fU, 0x2802b89eU, 0x5f058808U, 0xc60cd9b2U, 0xb10be924U, + 0x2f6f7c87U, 0x58684c11U, 0xc1611dabU, 0xb6662d3dU, 0x76dc4190U, + 0x01db7106U, 0x98d220bcU, 0xefd5102aU, 0x71b18589U, 0x06b6b51fU, + 0x9fbfe4a5U, 0xe8b8d433U, 0x7807c9a2U, 0x0f00f934U, 0x9609a88eU, + 0xe10e9818U, 0x7f6a0dbbU, 0x086d3d2dU, 0x91646c97U, 0xe6635c01U, + 0x6b6b51f4U, 0x1c6c6162U, 0x856530d8U, 0xf262004eU, 0x6c0695edU, + 0x1b01a57bU, 0x8208f4c1U, 0xf50fc457U, 0x65b0d9c6U, 0x12b7e950U, + 0x8bbeb8eaU, 0xfcb9887cU, 0x62dd1ddfU, 0x15da2d49U, 0x8cd37cf3U, + 0xfbd44c65U, 0x4db26158U, 0x3ab551ceU, 0xa3bc0074U, 0xd4bb30e2U, + 0x4adfa541U, 0x3dd895d7U, 0xa4d1c46dU, 0xd3d6f4fbU, 0x4369e96aU, + 0x346ed9fcU, 0xad678846U, 0xda60b8d0U, 0x44042d73U, 0x33031de5U, + 0xaa0a4c5fU, 0xdd0d7cc9U, 0x5005713cU, 0x270241aaU, 0xbe0b1010U, + 0xc90c2086U, 0x5768b525U, 0x206f85b3U, 0xb966d409U, 0xce61e49fU, + 0x5edef90eU, 0x29d9c998U, 0xb0d09822U, 0xc7d7a8b4U, 0x59b33d17U, + 0x2eb40d81U, 0xb7bd5c3bU, 0xc0ba6cadU, 0xedb88320U, 0x9abfb3b6U, + 0x03b6e20cU, 0x74b1d29aU, 0xead54739U, 0x9dd277afU, 0x04db2615U, + 0x73dc1683U, 0xe3630b12U, 0x94643b84U, 0x0d6d6a3eU, 0x7a6a5aa8U, + 0xe40ecf0bU, 0x9309ff9dU, 0x0a00ae27U, 0x7d079eb1U, 0xf00f9344U, + 0x8708a3d2U, 0x1e01f268U, 0x6906c2feU, 0xf762575dU, 0x806567cbU, + 0x196c3671U, 0x6e6b06e7U, 0xfed41b76U, 0x89d32be0U, 0x10da7a5aU, + 0x67dd4accU, 0xf9b9df6fU, 0x8ebeeff9U, 0x17b7be43U, 0x60b08ed5U, + 0xd6d6a3e8U, 0xa1d1937eU, 0x38d8c2c4U, 0x4fdff252U, 0xd1bb67f1U, + 0xa6bc5767U, 0x3fb506ddU, 0x48b2364bU, 0xd80d2bdaU, 0xaf0a1b4cU, + 0x36034af6U, 0x41047a60U, 0xdf60efc3U, 0xa867df55U, 0x316e8eefU, + 0x4669be79U, 0xcb61b38cU, 0xbc66831aU, 0x256fd2a0U, 0x5268e236U, + 0xcc0c7795U, 0xbb0b4703U, 0x220216b9U, 0x5505262fU, 0xc5ba3bbeU, + 0xb2bd0b28U, 0x2bb45a92U, 0x5cb36a04U, 0xc2d7ffa7U, 0xb5d0cf31U, + 0x2cd99e8bU, 0x5bdeae1dU, 0x9b64c2b0U, 0xec63f226U, 0x756aa39cU, + 0x026d930aU, 0x9c0906a9U, 0xeb0e363fU, 0x72076785U, 0x05005713U, + 0x95bf4a82U, 0xe2b87a14U, 0x7bb12baeU, 0x0cb61b38U, 0x92d28e9bU, + 0xe5d5be0dU, 0x7cdcefb7U, 0x0bdbdf21U, 0x86d3d2d4U, 0xf1d4e242U, + 0x68ddb3f8U, 0x1fda836eU, 0x81be16cdU, 0xf6b9265bU, 0x6fb077e1U, + 0x18b74777U, 0x88085ae6U, 0xff0f6a70U, 0x66063bcaU, 0x11010b5cU, + 0x8f659effU, 0xf862ae69U, 0x616bffd3U, 0x166ccf45U, 0xa00ae278U, + 0xd70dd2eeU, 0x4e048354U, 0x3903b3c2U, 0xa7672661U, 0xd06016f7U, + 0x4969474dU, 0x3e6e77dbU, 0xaed16a4aU, 0xd9d65adcU, 0x40df0b66U, + 0x37d83bf0U, 0xa9bcae53U, 0xdebb9ec5U, 0x47b2cf7fU, 0x30b5ffe9U, + 0xbdbdf21cU, 0xcabac28aU, 0x53b39330U, 0x24b4a3a6U, 0xbad03605U, + 0xcdd70693U, 0x54de5729U, 0x23d967bfU, 0xb3667a2eU, 0xc4614ab8U, + 0x5d681b02U, 0x2a6f2b94U, 0xb40bbe37U, 0xc30c8ea1U, 0x5a05df1bU, + 0x2d02ef8dU +}; diff --git a/scripts/gendwarfksyms/crc32.h b/scripts/gendwarfksyms/crc32.h new file mode 100644 index 000000000000..89e4454b2a70 --- /dev/null +++ b/scripts/gendwarfksyms/crc32.h @@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Based on scripts/genksyms/genksyms.c, which has the following + * notice: + * + * Generate kernel symbol version hashes. + * Copyright 1996, 1997 Linux International. + * + * New implementation contributed by Richard Henderson + * Based on original work by Bjorn Ekwall + * + * This file was part of the Linux modutils 2.4.22: moved back into the + * kernel sources by Rusty Russell/Kai Germaschewski. + */ + +#ifndef __CRC32_H +#define __CRC32_H + +extern const unsigned int crctab32[]; + +static inline unsigned long partial_crc32_one(unsigned char c, + unsigned long crc) +{ + return crctab32[(crc ^ c) & 0xff] ^ (crc >> 8); +} + +static inline unsigned long partial_crc32(const char *s, unsigned long crc) +{ + while (*s) + crc = partial_crc32_one(*s++, crc); + return crc; +} + +#endif /* __CRC32_H */ diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index 62241cc97a76..677190ae18ef 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -693,18 +693,51 @@ static int process_type(struct state *state, struct die *parent, Dwarf_Die *die) /* * Exported symbol processing */ +static int get_symbol_cache(struct state *state, Dwarf_Die *die, + struct die **cache) +{ + checkp(symbol_set_die(state->sym, die)); + check(die_map_get(die, SYMBOL, cache)); + + if ((*cache)->state != INCOMPLETE) + return 1; /* We already processed a symbol for this DIE */ + + (*cache)->tag = dwarf_tag(die); + return 0; +} + static int process_subprogram(struct state *state, Dwarf_Die *die) { - check(__process_subroutine_type(state, NULL, die, "subprogram")); - state->sym->state = MAPPED; + struct die *cache; + + if (checkp(get_symbol_cache(state, die, &cache)) > 0) + return 0; + + debug("%s", state->sym->name); + check(__process_subroutine_type(state, cache, die, "subprogram")); + cache->state = SYMBOL; + + if (dump_dies) + fputs("\n", stderr); + return 0; } static int process_variable(struct state *state, Dwarf_Die *die) { - check(process(state, NULL, "variable ")); - check(process_type_attr(state, NULL, die)); - state->sym->state = MAPPED; + struct die *cache; + + if (checkp(get_symbol_cache(state, die, &cache)) > 0) + return 0; + + debug("%s", state->sym->name); + check(process(state, cache, "variable ")); + check(process_type_attr(state, cache, die)); + cache->state = SYMBOL; + + if (dump_dies) + fputs("\n", stderr); + return 0; } @@ -750,7 +783,6 @@ static int process_exported_symbols(struct state *state, struct die *cache, if (!is_export_symbol(state, die)) return 0; - debug("%s", state->sym->name); state_init(state); if (is_symbol_ptr(get_name(&state->die))) @@ -760,9 +792,6 @@ static int process_exported_symbols(struct state *state, struct die *cache, else check(process_variable(state, &state->die)); - if (dump_dies) - fputs("\n", stderr); - cache_clear_expanded(&state->expansion_cache); return 0; default: diff --git a/scripts/gendwarfksyms/gendwarfksyms.c b/scripts/gendwarfksyms/gendwarfksyms.c index 6a219a54c342..4a160d19d7df 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.c +++ b/scripts/gendwarfksyms/gendwarfksyms.c @@ -22,6 +22,8 @@ bool dump_dies; bool dump_die_map; /* Print out type_map contents */ bool dump_types; +/* Print out expanded type strings used for version calculations */ +bool dump_versions; /* Produce a symtypes file */ bool symtypes; static const char *symtypes_file; @@ -35,6 +37,7 @@ static const struct { { "--dump-dies", &dump_dies, NULL }, { "--dump-die-map", &dump_die_map, NULL }, { "--dump-types", &dump_types, NULL }, + { "--dump-versions", &dump_versions, NULL }, { "--symtypes", &symtypes, &symtypes_file }, }; @@ -109,9 +112,10 @@ static int process_modules(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. */ - check(generate_symtypes(symfile)); + check(generate_symtypes_and_versions(symfile)); die_map_free(); return DWARF_CB_OK; @@ -187,5 +191,7 @@ int main(int argc, const char **argv) if (symfile) fclose(symfile); + symbol_print_versions(); + return 0; } diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h index 6edbd6478e0f..f85e080a8634 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -23,6 +23,7 @@ extern bool debug; extern bool dump_dies; extern bool dump_die_map; extern bool dump_types; +extern bool dump_versions; extern bool symtypes; #define MAX_INPUT_FILES 128 @@ -95,7 +96,7 @@ extern bool symtypes; #define SYMBOL_DECLONLY_PREFIX "__gendwarfksyms_declonly_" #define SYMBOL_DECLONLY_PREFIX_LEN (sizeof(SYMBOL_DECLONLY_PREFIX) - 1) -enum symbol_state { UNPROCESSED, MAPPED }; +enum symbol_state { UNPROCESSED, MAPPED, PROCESSED }; struct symbol_addr { uint32_t section; @@ -119,18 +120,25 @@ struct symbol { struct hlist_node name_hash; enum symbol_state state; uintptr_t die_addr; + unsigned long crc; }; +typedef int (*symbol_callback_t)(struct symbol *, void *arg); + extern bool is_symbol_ptr(const char *name); extern int symbol_read_exports(FILE *file); extern int symbol_read_symtab(int fd); extern struct symbol *symbol_get_unprocessed(const char *name); +extern int symbol_set_die(struct symbol *sym, Dwarf_Die *die); +extern int symbol_set_crc(struct symbol *sym, unsigned long crc); +extern int symbol_for_each(symbol_callback_t func, void *arg); +extern void symbol_print_versions(void); /* * die.c */ -enum die_state { INCOMPLETE, UNEXPANDED, COMPLETE, LAST = COMPLETE }; +enum die_state { INCOMPLETE, UNEXPANDED, COMPLETE, SYMBOL, LAST = SYMBOL }; enum die_fragment_type { EMPTY, STRING, LINEBREAK, DIE }; struct die_fragment { @@ -154,6 +162,7 @@ static inline const char *die_state_name(enum die_state state) CASE_CONST_TO_STR(INCOMPLETE) CASE_CONST_TO_STR(UNEXPANDED) CASE_CONST_TO_STR(COMPLETE) + CASE_CONST_TO_STR(SYMBOL) } } @@ -239,6 +248,6 @@ extern int process_module(Dwfl_Module *mod, Dwarf *dbg, Dwarf_Die *cudie); * types.c */ -extern int generate_symtypes(FILE *file); +extern int generate_symtypes_and_versions(FILE *file); #endif /* __GENDWARFKSYMS_H */ diff --git a/scripts/gendwarfksyms/symbols.c b/scripts/gendwarfksyms/symbols.c index 8cc04e6295a7..c9889cfa89c4 100644 --- a/scripts/gendwarfksyms/symbols.c +++ b/scripts/gendwarfksyms/symbols.c @@ -84,6 +84,38 @@ static int for_each(const char *name, bool name_only, symbol_callback_t func, return 0; } +static int set_crc(struct symbol *sym, void *data) +{ + unsigned long *crc = data; + + if (sym->state == PROCESSED && sym->crc != *crc) + warn("overriding version for symbol %s (crc %lx vs. %lx)", + sym->name, sym->crc, *crc); + + sym->state = PROCESSED; + sym->crc = *crc; + return 0; +} + +int symbol_set_crc(struct symbol *sym, unsigned long crc) +{ + if (checkp(for_each(sym->name, false, set_crc, &crc)) > 0) + return 0; + return -1; +} + +static int set_die(struct symbol *sym, void *data) +{ + sym->die_addr = (uintptr_t)((Dwarf_Die *)data)->addr; + sym->state = MAPPED; + return 0; +} + +int symbol_set_die(struct symbol *sym, Dwarf_Die *die) +{ + return checkp(for_each(sym->name, false, set_die, die)); +} + static bool is_exported(const char *name) { return checkp(for_each(name, true, NULL, NULL)) > 0; @@ -151,6 +183,19 @@ struct symbol *symbol_get_unprocessed(const char *name) return sym; } +int symbol_for_each(symbol_callback_t func, void *arg) +{ + struct hlist_node *tmp; + struct symbol *sym; + int i; + + hash_for_each_safe(symbol_names, i, tmp, sym, name_hash) { + check(func(sym, arg)); + } + + return 0; +} + typedef int (*elf_symbol_callback_t)(const char *name, GElf_Sym *sym, Elf32_Word xndx, void *arg); @@ -264,3 +309,23 @@ int symbol_read_symtab(int fd) { return elf_for_each_symbol(fd, process_symbol, NULL); } + +void symbol_print_versions(void) +{ + struct hlist_node *tmp; + struct symbol *sym; + int i; + + hash_for_each_safe(symbol_names, i, tmp, sym, name_hash) { + if (sym->state != PROCESSED) + warn("no information for symbol %s", sym->name); + + printf("#SYMVER %s 0x%08lx\n", sym->name, sym->crc); + + free((void *)sym->name); + free(sym); + } + + hash_init(symbol_addrs); + hash_init(symbol_names); +} diff --git a/scripts/gendwarfksyms/types.c b/scripts/gendwarfksyms/types.c index 7b9997d8322d..6411b1279a28 100644 --- a/scripts/gendwarfksyms/types.c +++ b/scripts/gendwarfksyms/types.c @@ -228,6 +228,34 @@ 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 int version_init(struct version *version) +{ + version->crc = 0xffffffff; + return check(type_expansion_init(&version->type, dump_versions)); +} + +static void version_free(struct version *version) +{ + type_expansion_free(&version->type); +} + +static int version_add(struct version *version, const char *s) +{ + version->crc = partial_crc32(s, version->crc); + if (dump_versions) + checkp(type_expansion_append(&version->type, s, NULL)); + return 0; +} + /* * Type reference format: #, where prefix: * s -> structure @@ -271,7 +299,7 @@ static char *get_type_name(struct die *cache) warn("found incomplete cache entry: %p", cache); return NULL; } - if (!cache->fqn) + if (cache->state == SYMBOL || !cache->fqn) return NULL; prefix = get_type_prefix(cache->tag); @@ -305,6 +333,45 @@ static char *get_type_name(struct die *cache) return name; } +static int __calculate_version(struct version *version, struct type_list *list) +{ + struct type_expansion *e; + + /* Calculate a CRC over an expanded type string */ + while (list) { + if (is_type_prefix(list->str)) { + check(type_map_get(list->str, &e)); + + /* + * It's sufficient to expand each type reference just + * once to detect changes. + */ + if (cache_was_expanded(&expansion_cache, e)) { + check(version_add(version, list->str)); + } else { + check(cache_mark_expanded(&expansion_cache, e)); + check(__calculate_version(version, + e->expanded)); + } + } else { + check(version_add(version, list->str)); + } + + list = list->next; + } + + return 0; +} + +static int calculate_version(struct version *version, const char *name, + struct type_list *list) +{ + check(version_init(version)); + check(__calculate_version(version, list)); + cache_clear_expanded(&expansion_cache); + return 0; +} + static int __type_expand(struct die *cache, struct type_expansion *type, bool recursive); @@ -411,7 +478,51 @@ static int expand_type(struct die *cache, void *arg) return 0; } -int generate_symtypes(FILE *file) +static int 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 == PROCESSED) + return 0; + + if (__die_map_get(sym->die_addr, SYMBOL, &cache)) + return 0; /* We'll warn about missing CRCs later. */ + + check(type_expand(cache, &type, false)); + + /* If the symbol already has a version, don't calculate it again. */ + if (sym->state != PROCESSED) { + check(calculate_version(&version, sym->name, type.expanded)); + check(symbol_set_crc(sym, version.crc)); + debug("%s = %lx", sym->name, version.crc); + + if (dump_versions) { + fputs(sym->name, stderr); + fputs(" ", stderr); + type_list_write(version.type.expanded, stderr); + fputs("\n", stderr); + } + + version_free(&version); + } + + /* These aren't needed in type_map unless we want a symtypes file. */ + if (symtypes) + check(type_map_add(sym->name, &type)); + + type_expansion_free(&type); + + return 0; +} + +int generate_symtypes_and_versions(FILE *file) { hash_init(expansion_cache.cache); @@ -429,7 +540,14 @@ int generate_symtypes(FILE *file) check(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_cache type, and use + * type_map expansions to calculate a symbol version from the + * fully expanded type string. + */ + check(symbol_for_each(expand_symbol, NULL)); + + /* + * 3. If a symtypes file is requested, write type_map contents to * the file. */ check(type_map_write(file)); From patchwork Thu Aug 15 17:39:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13765050 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 25DDD1BE860 for ; Thu, 15 Aug 2024 17:39:41 +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=1723743583; cv=none; b=sVK+xaXESEezZY5U0Inv70AgR+KOvrnAVpYNaGSyoaJfRXAyTZKre7k+VPOwdsBV+YSWg6eozWxkQVm5DVjpn3pcEHde2yQ99SbLy/0mCCUsav1CYM4tbm6VpWLJS3oT/d/7tVix3U045tQHOYGEEfzPMv08tZEmUHJ+98lM+og= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743583; c=relaxed/simple; bh=MdCZyANU3cpN1KbL2Y+c7u2dfSk3DjTqLm/uKatG+wg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Qh3N9MvrhCMa5ClAZlv7KObBnZ24Rz7Pm0ueHOsKikrKJtsyB4UUeTWDnM1ctjI9ts/8l+K8MqgppfsenovWXYg3LUEBVO5bELm/fK2uK3JN45uVDEqECNYWMHXbdflK1XUBmUOlad0mb4vZ4+g7MVFRtkKwbXzGD8DmttWVbc8= 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=g25wtdYC; 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="g25wtdYC" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2d3c6b19444so988303a91.2 for ; Thu, 15 Aug 2024 10:39:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723743580; x=1724348380; 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=AM5wUjQsWxirPWho62bBzTbFDQPnvqvSrS68y5t7Rps=; b=g25wtdYCK/iMouWOVcx7e1hCQOD0bM7F76zWaDAbirFuagb9QWCFdfc/7FmqaFtIhY XLTW4nOp/MnqCms2ykWPUyS+nSydXpetPf7mwj8pHrVUEzlktqSgBZvlEsHY5wYRWWdt /gCvUzZ3Zlqe7RG/Z2oLkDfTU8C88QCd8mP4u+pgzxUVoSjigsZRMxi4+Lwygu4psTEm hgOuoohwdsPtmluIujVn1L16CFB1pbN3mX+GP+RygmC7Ci6GpJ2PUJiKp6vEWbOiahyy bH9Sk5AcTaxFQgitEZ5E+ee1nAwuKPuK0SyqCRvzh5VkFjYtk71cYIszsMtKByGQDq5m QU/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723743580; x=1724348380; 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=AM5wUjQsWxirPWho62bBzTbFDQPnvqvSrS68y5t7Rps=; b=wr3+MnqlurK867tl0gxM3vBxaEsM75g4058jZsGbyWGQRaGkhVgJhAPYPx0xRlZpSi eNGXuI7la4r1uPmX7K/817DT6rHQpQ1uLyhbIZ85pcnhyovaMu6ncmaFUl+GGfk7acHO C9a8zYp8IpXXzzLZ4aXKQsb1+JX2ga4u9094Jep1kGWmHK6Ap1QY384Zngd4scNmnNyG WceQGU+ZI9axWgaNahuBRkBkWG389MYjij3Zj/YPDFChefB/MwjbXXFzYt/grR12dCuF ipw9yxge2yYsuwjVpCuMd7aVkCjQg7+UD9CGdzz2iokMh+XaF+lZ+vev4iV4cjEKAGY8 TZoQ== X-Forwarded-Encrypted: i=1; AJvYcCXPZ4esDEX2Oi3kqnnm5xUjs1a1Sbah+Z+XEJlhGFnYwUww7DQf7DU+MlnS2Cwwi2r8lhQJS4K30IoSkoWxACU6jifOAio7TTyeZypI X-Gm-Message-State: AOJu0YxBaW8zwXpBnFYUvNKJjDAFw0fYa4acDAon9f6xqugw/BMVvyp9 97fj/yI0WtwR+BY5xiG/I8HsLfGA+l7G8PV/T/0zpRixLDlfYrMP2XusKZpbd/oRnXU4mHB/4an xeFqK+NkTLZMZOepstYxYHGmk3Q== X-Google-Smtp-Source: AGHT+IEa9IerFI45JnVORaoGhFuJF7bDHYfiVGOl6IiBF/KgDfLV7Sw3ufSt/NO4uLL5N9tOaKwbVaiwWR+mCH6/ASI= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a17:90b:4f86:b0:2d3:c743:1e5d with SMTP id 98e67ed59e1d1-2d3e041c0b8mr731a91.7.1723743580335; Thu, 15 Aug 2024 10:39:40 -0700 (PDT) Date: Thu, 15 Aug 2024 17:39:19 +0000 In-Reply-To: <20240815173903.4172139-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240815173903.4172139-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=9841; i=samitolvanen@google.com; h=from:subject; bh=MdCZyANU3cpN1KbL2Y+c7u2dfSk3DjTqLm/uKatG+wg=; b=owEB7QES/pANAwAKAUy19oSLvFbuAcsmYgBmvj059Pg7Q9O8Q4oa1rF1r5AuRgbl0eRJ6dE4O uywCFA1ZmSJAbMEAAEKAB0WIQQ1zPtjsoPW0663g5RMtfaEi7xW7gUCZr49OQAKCRBMtfaEi7xW 7qQQDACID+wzctpfuYuBToMDEYH6MiGIl/f2n24YCbYmOVyDavrd7LRzjV//CF9n+3CGV9VuhXS kesyXvcYQKKXs11p6qVm5EuQPtfjdz8+vyLWdJMSkgj55gm+6AnFDRGjWxdu2HMuweUCayScGq/ XZ1MxNLeqivVdMKu/eBFtwl+tfkm8eJdp4us7naIn6HFSUNnIWqxvVhldGu8nzXGxaEeoFvUhFr XITZTFrFFdwJu+tYSmWDFtpwdJ89ws42Sy3Y7Gd+E1k2Pui8jsPiebDDx29/DZS9WdzSgtiXMIv rKTTFkjTLuhjt1RD3LaeGn0Lv7uMeyjiYt3v9Fv90FoXrn7+U/OQrGdrRt+iTNmP63b+cQ1qzcF VGn3xmRmpwkwj4AGdHWouTtfVjWCwJeS4HvzAOwWPnPDLvbWRPj92G6gtjRkxMlPqPgmcEQinZ5 1dMxt7RsMAUVcT4N31VZiBGv/tTaHMHWfrKE34kZIq7+qDUec82xYk+NrHzHa5MTSksuI= X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240815173903.4172139-36-samitolvanen@google.com> Subject: [PATCH v2 15/19] gendwarfksyms: Add support for declaration-only data structures From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Wedson Almeida Filho , Gary Guo , Petr Pavlu , Neal Gompa , Hector Martin , Janne Grunau , Asahi Linux , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen If a source file refers to an opaque data structure, the DWARF debugging information in the resulting object file will only contain a structure declation, which means the contents of the structure are not included when computing symbol versions. For example: Source code: struct struct0; int func(struct struct0 *); gendwarfksyms --debug: subprogram( formal_parameter pointer_type { structure_type struct0 { } } byte_size(8), ) -> base_type int byte_size(4) encoding(5); #SYMVER func 0x7e8284f9 The declaration can change into a full definition when an additional include is added to the TU, which changes the version CRC, even though the ABI has not changed. If this happens during an LTS update, a distribution that wants to maintain a stable ABI needs a way to ensure symbol versions remain unchanged. With genksyms, the usual solution is to use #ifndef __GENKSYMS__ to skip the newly added header file when computing symbol versions, but that's not an option when we're processing a precompiled object file. To support this use case, add a --stable command line flag that gates kABI stability features that are not needed in mainline, but can be useful for distributions, and add support for symbol annotations that allow structures to always be treated as declarations when versions are computed. If a __gendwarfksyms_declonly_ symbol exists in the object file's symbol table, the "structname" structure is treated as a declaration only, and not expanded when computing versions. The symbol can be defined using the following macro, for example, which discards it from the final kernel binary: #define GENDWARFKSYMS_DECLONLY(structname) \ static void *__gendwarfksyms_declonly_##structname __used \ __section(".discard.gendwarfksyms") Now, if we include struct0 definition in our source code, and add a declaration-only annotation, we have: struct struct0 { int a; }; GENDWARFKSYMS_DECLONLY(struct0); int func(struct struct0 *); gendwarfksyms --debug reflects the added definition: subprogram( formal_parameter pointer_type { structure_type struct0 { member base_type int byte_size(4) encoding(5) data_member_location(0), } byte_size(4) } byte_size(8), ) -> base_type int byte_size(4) encoding(5); #SYMVER func 0xc0de983d But with --debug --stable, the definition is ignored and we again have the original symbol version: subprogram( formal_parameter pointer_type { structure_type struct0 { } } byte_size(8), ) -> base_type int byte_size(4) encoding(5); #SYMVER func 0x7e8284f9 Signed-off-by: Sami Tolvanen --- scripts/gendwarfksyms/dwarf.c | 25 +++++++++- scripts/gendwarfksyms/examples/declonly.c | 31 ++++++++++++ scripts/gendwarfksyms/gendwarfksyms.c | 4 ++ scripts/gendwarfksyms/gendwarfksyms.h | 4 ++ scripts/gendwarfksyms/symbols.c | 61 +++++++++++++++++++++++ 5 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 scripts/gendwarfksyms/examples/declonly.c diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index 677190ae18ef..bf28946c321e 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -74,7 +74,30 @@ static bool is_declaration(Dwarf_Die *die) { bool value; - return get_flag_attr(die, DW_AT_declaration, &value) && value; + /* + * If the object file has a __gendwarfksyms_declonly_ + * symbol, we treat structures named "structname" as declarations, + * i.e. they won't be expanded when calculating symbol versions. + * This helps distributions maintain a stable kABI e.g., if extra + * includes change a declaration into a definition. + * + * A simple way to mark a structure declaration-only in the source + * code is to define a discarded symbol: + * + * #define GENDWARFKSYMS_DECLONLY(structname) \ + * static void *__gendwarfksyms_declonly_##structname __used \ + * __section(".discard.gendwarfksyms") + * + * For example: + * + * struct struct0 { int a; } + * GENDWARFKSYMS_DECLONLY(struct0); + * + * Here, struct0 would be always be considered a declaration even + * though the definition is visible. + */ + return (get_flag_attr(die, DW_AT_declaration, &value) && value) || + is_struct_declonly(get_name(die)); } /* diff --git a/scripts/gendwarfksyms/examples/declonly.c b/scripts/gendwarfksyms/examples/declonly.c new file mode 100644 index 000000000000..b1b889e582da --- /dev/null +++ b/scripts/gendwarfksyms/examples/declonly.c @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2024 Google LLC + * + * Declaration-only data structure example. See dwarf.c:is_declaration + * for details. + * + * $ gcc -g -c examples/declonly.c + * $ echo exported | ./gendwarfksyms --dump-dies declonly.o + * variable structure_type struct0 { + * member base_type int byte_size(4) encoding(5) data_member_location(0), + * } byte_size(4) + * + * With --stable, struct0 is treated as a declaration: + * + * $ echo exported | ./gendwarfksyms --stable --dump-dies declonly.o + * variable structure_type struct0 { + * } + */ + +#define GENDWARFKSYMS_DECLONLY(structname) \ + static void *__gendwarfksyms_declonly_##structname \ + __attribute__((__used__)) \ + __attribute__((__section__(".discard.gendwarfksyms"))); + +struct struct0 { + int a; +}; + +struct struct0 exported; +GENDWARFKSYMS_DECLONLY(struct0); diff --git a/scripts/gendwarfksyms/gendwarfksyms.c b/scripts/gendwarfksyms/gendwarfksyms.c index 4a160d19d7df..10d1470383be 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.c +++ b/scripts/gendwarfksyms/gendwarfksyms.c @@ -24,6 +24,8 @@ bool dump_die_map; bool dump_types; /* Print out expanded type strings used for version calculations */ bool dump_versions; +/* Support kABI stability features */ +bool stable; /* Produce a symtypes file */ bool symtypes; static const char *symtypes_file; @@ -38,6 +40,7 @@ static const struct { { "--dump-die-map", &dump_die_map, NULL }, { "--dump-types", &dump_types, NULL }, { "--dump-versions", &dump_versions, NULL }, + { "--stable", &stable, NULL }, { "--symtypes", &symtypes, &symtypes_file }, }; @@ -186,6 +189,7 @@ int main(int argc, const char **argv) dwfl_end(dwfl); close(fd); + symbol_free_declonly(); } if (symfile) diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h index f85e080a8634..05b5c01b1c2a 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -24,6 +24,7 @@ extern bool dump_dies; extern bool dump_die_map; extern bool dump_types; extern bool dump_versions; +extern bool stable; extern bool symtypes; #define MAX_INPUT_FILES 128 @@ -134,6 +135,9 @@ extern int symbol_set_crc(struct symbol *sym, unsigned long crc); extern int symbol_for_each(symbol_callback_t func, void *arg); extern void symbol_print_versions(void); +extern bool is_struct_declonly(const char *name); +extern void symbol_free_declonly(void); + /* * die.c */ diff --git a/scripts/gendwarfksyms/symbols.c b/scripts/gendwarfksyms/symbols.c index c9889cfa89c4..03ba93ef9eac 100644 --- a/scripts/gendwarfksyms/symbols.c +++ b/scripts/gendwarfksyms/symbols.c @@ -5,12 +5,20 @@ #include "gendwarfksyms.h" +struct declonly { + const char *name; + struct hlist_node hash; +}; + #define SYMBOL_HASH_BITS 15 +#define DECLONLY_HASH_BITS 10 /* struct symbol_addr -> struct symbol */ static DEFINE_HASHTABLE(symbol_addrs, SYMBOL_HASH_BITS); /* name -> struct symbol */ static DEFINE_HASHTABLE(symbol_names, SYMBOL_HASH_BITS); +/* name -> struct declonly */ +static DEFINE_HASHTABLE(declonly_structs, DECLONLY_HASH_BITS); static inline u32 symbol_addr_hash(const struct symbol_addr *addr) { @@ -296,12 +304,36 @@ static int process_symbol(const char *name, GElf_Sym *sym, Elf32_Word xndx, void *arg) { struct symbol_addr addr = { .section = xndx, .address = sym->st_value }; + struct declonly *d; /* Set addresses for exported symbols */ if (GELF_ST_BIND(sym->st_info) != STB_LOCAL && addr.section != SHN_UNDEF) checkp(for_each(name, true, set_symbol_addr, &addr)); + if (!stable) + return 0; + + /* Process declonly structs */ + if (strncmp(name, SYMBOL_DECLONLY_PREFIX, SYMBOL_DECLONLY_PREFIX_LEN)) + return 0; + + d = malloc(sizeof(struct declonly)); + if (!d) { + error("malloc failed"); + return -1; + } + + name += SYMBOL_DECLONLY_PREFIX_LEN; + d->name = strdup(name); + if (!d->name) { + error("strdup failed"); + return -1; + } + + hash_add(declonly_structs, &d->hash, name_hash(d->name)); + debug("declaration-only: %s", d->name); + return 0; } @@ -310,6 +342,35 @@ int symbol_read_symtab(int fd) return elf_for_each_symbol(fd, process_symbol, NULL); } +bool is_struct_declonly(const char *name) +{ + struct declonly *d; + + if (!stable || !name) + return false; + + hash_for_each_possible(declonly_structs, d, hash, name_hash(name)) { + if (!strcmp(name, d->name)) + return true; + } + + return false; +} + +void symbol_free_declonly(void) +{ + struct hlist_node *tmp; + struct declonly *d; + int i; + + hash_for_each_safe(declonly_structs, i, tmp, d, hash) { + free((void *)d->name); + free(d); + } + + hash_init(declonly_structs); +} + void symbol_print_versions(void) { struct hlist_node *tmp; From patchwork Thu Aug 15 17:39:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13765051 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 4729B1BF328 for ; Thu, 15 Aug 2024 17:39:43 +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=1723743585; cv=none; b=XyHeLITBvQ0uzeVmAKUHYCPwl4gDq8xGjbvQZ2NtJQPqBloEpcsqDXVp4s4RR0Y24Lu1S57Qq46e6Y0GXDAYWqLniWvWbZT//blCvgJKRT+pbrF67RFa5eisgs9qQtZGLrmdNiAsra9AYvDMb6a8UpEL3vraPCAmtVRlSo5Jogc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743585; c=relaxed/simple; bh=6usSpOjyoB85v1SyjtEau1KushdvZAC0dNnzubRF5CM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=G2oz/RNtBUhZpGPQq7bIm7OidSfW+XnssJESMT20dcNf8B55E4xHzHIA59m37V2V4z3egcnZsxGjEkL4KLy+88Vgsd5zzuZgTZTeN6pcf1I8fgQv3+WDH8byyA/R0OrxZXJdhMr1iuCorLlh7kPtOzvzC4/Jys+4GUySBLu4oWU= 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=jO236v+7; 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="jO236v+7" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e1169005d9aso2042911276.0 for ; Thu, 15 Aug 2024 10:39:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723743582; x=1724348382; 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=2sPCSdATuewk8L2vh2ELF65Db7EveWl5UV4XCGQQVag=; b=jO236v+7UYB3Gp+aCNM29b3TpE4gidBBH3dSFfKxespEXI18DasXqFlUe5j3cJSgLn nCNM4xZm9D6VmU3pja6jIglkRNAQDgseFpgHWgXPK9of/TvX2hmMfEixuc4bcxBvWw2R 3d4jgcyjyZBgaUQ3w7y7EivtNUuzurIL9s2uAJux9vHy9lOG5GNXck1URya4yfVtooIb 96ZC3XCr86EhpzCuS9ujjzicSsszrPxjzzzJPkOc+fQG4IhY5NdDyCW0rDLR445KNwzV f7ITApuAL8tXX4YDxAFPYu8zEcLR6iYkyG3hTPXGTU8bmLDhGteu1Nt/ukQw75yVC1DD N7Kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723743582; x=1724348382; 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=2sPCSdATuewk8L2vh2ELF65Db7EveWl5UV4XCGQQVag=; b=Yw7bAuJyPkfgXMM64tb+b1lpX5HjdoWlJuMH7GptLiw40zArHRYTH6h9Ujxlh/44H2 /AOyYe9ocqIe799+IgaWZYrylkGx5LLgSQ8qGlgwvZ2XRp4351FX9qItLzT58VntM+Zh mSVxGcd+X08LZuWXsys4X8gcZGoMmFcA42l78RHbhj8ZIhuOVikbfR3Dwj1QX9PMzlXZ xU4yichaqZbLF3L9kFs2BC8e04WfylaUSlbk4kxFuRPDibbrQJOGIPuNJl79q9gGeGgG xE4pVSH3gp58R4EeOc07R26QspuIAtsRpW9r44VJcahceSxSnO49TOMmN5li+UsRqbNX cDmA== X-Forwarded-Encrypted: i=1; AJvYcCVM92TneLulAKGIiNnpKHEH23WR90qVgOOMbXMvzAgumWl9Y6TWaUIw3l3+fd1z92jnNuehbEJsmFwLqu1KHoVEUxDRYisOIGD4TfsK X-Gm-Message-State: AOJu0YzSOSy5TcW7zqf6qBLzJ4pdkzPxa3lxSLwc50LmfM0jcP5yeZ2/ wxkmWGXy89P2wxpZxbMZ0YtybwFM5PKqzr0vYjs+7xluFlISj1OoYdCCea697Wy5FFF1KwUuf5y ACexUZXoYBibvJqws+1eszPE7yQ== X-Google-Smtp-Source: AGHT+IGwomnqFK9vxpKveGL73310JYatfAzfvmWx1tXBSFjJAlloAq4/X3L67g58vXf4su4on0aLpKym7vCztU318gc= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a25:b206:0:b0:e11:7039:ff92 with SMTP id 3f1490d57ef6-e1180f85427mr578276.11.1723743582318; Thu, 15 Aug 2024 10:39:42 -0700 (PDT) Date: Thu, 15 Aug 2024 17:39:20 +0000 In-Reply-To: <20240815173903.4172139-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240815173903.4172139-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=10779; i=samitolvanen@google.com; h=from:subject; bh=6usSpOjyoB85v1SyjtEau1KushdvZAC0dNnzubRF5CM=; b=owEB7QES/pANAwAKAUy19oSLvFbuAcsmYgBmvj05/oGe5klinNb5fzf3qC/b17XaP5+AUar/1 rfb+HhZKKSJAbMEAAEKAB0WIQQ1zPtjsoPW0663g5RMtfaEi7xW7gUCZr49OQAKCRBMtfaEi7xW 7knjC/9Rj2rfRBLkbJQwNXdd+nLnKm+A7ijeY86ZAot9zhxhsC+79B78LgVqGAM763CtUyRBomL HoVlsjF+VA8PgDH2FYjBG7AneizoXz+8X3I6nutdyvpcjvCjdvLGdWM/sg5WMSMRh1GC1tlfSFd jzNdwm0xuaBDj/KRkaAag8d2mf8vWUH6H3wrKfPLb+pJrFh+N0jLSun+8aWCvueKV18lNC7E+RA QkMEDucNM1meSnviUaK9lTKvvJ4Js5cEwv1uwR561At+8NNGMowtw7Owh0Mwi7DeEvHOgTos08P 84Nju5yb8MVr4ibghgyWlHay6Cxn9fWsKsYomxZ4yoO9AFM21g4/BbNTa2q1aF1p2sxvIor1+IM QVk0eXSaJ4ucxSXxMpFYik77snFO1eFSx6Bp0+ipixkC3/HR7aMRWZfe/X+M0QQkTukH72nwFPG JWo674hXsfpS95FMkR8Io242ryKuayIhchn/NpPgF4PtEI8OaDI/0trSYUe5vLA87aoL4= X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240815173903.4172139-37-samitolvanen@google.com> Subject: [PATCH v2 16/19] gendwarfksyms: Add support for reserved structure fields From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Wedson Almeida Filho , Gary Guo , Petr Pavlu , Neal Gompa , Hector Martin , Janne Grunau , Asahi Linux , 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 add reserved fields to kernel data structures that they anticipate will be modified during the ABI support timeframe, either by LTS updates or backports. With genksyms, developers would typically hide changes to the reserved fields from version calculation with #ifndef __GENKSYMS__, which would result in the symbol version not changing even though the actual type of the reserved field changes. When we process precompiled object files, this is again not an option. To support stable symbol versions for reserved fields, change the union type processing to recognize field name prefixes, and if the union contains a field name that starts with __kabi_reserved, only use the type of that field for computing symbol versions. In other words, let's assume we have a structure where we want to reserve space for future changes: struct struct1 { long a; long __kabi_reserved_0; /* reserved for future use */ }; struct struct1 exported; gendwarfksyms --debug produces the following output: variable structure_type struct1 { member base_type long int byte_size(8) encoding(5) data_member_location(0), member base_type long int byte_size(8) encoding(5) data_member_location(8), } byte_size(16); #SYMVER exported 0x67997f89 To take the reserved field into use, a distribution would replace it with a union, with one of the fields keeping the __kabi_reserved name prefix for the original type: struct struct1 { long a; union { long __kabi_reserved_0; struct { int b; int v; }; }; gendwarfksyms --debug now produces the following output: variable structure_type struct1 { member base_type long int byte_size(8) encoding(5) data_member_location(0), member union_type { member base_type long int byte_size(8) encoding(5), member structure_type { member base_type int byte_size(4) encoding(5) data_member_location(0), member base_type int byte_size(4) encoding(5) data_member_location(4), } byte_size(8), } byte_size(8) data_member_location(8), } byte_size(16); #SYMVER exported 0x66916c41 But with --stable, gendwarfksyms only uses the reserved field for the version calculation, thus leaving the symbol version unchanged: variable structure_type struct1 { member base_type long int byte_size(8) encoding(5) data_member_location(0), member base_type long int byte_size(8) encoding(5) data_member_location(8), } byte_size(16); #SYMVER exported 0x67997f89 Signed-off-by: Sami Tolvanen --- scripts/gendwarfksyms/dwarf.c | 148 +++++++++++++++++++++- scripts/gendwarfksyms/examples/reserved.c | 66 ++++++++++ scripts/gendwarfksyms/gendwarfksyms.h | 18 +++ 3 files changed, 229 insertions(+), 3 deletions(-) create mode 100644 scripts/gendwarfksyms/examples/reserved.c diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index bf28946c321e..d6252194692d 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -274,8 +274,12 @@ int process_die_container(struct state *state, struct die *cache, res = checkp(dwarf_child(die, ¤t)); while (!res) { - if (match(¤t)) - check(func(state, cache, ¤t)); + if (match(¤t)) { + /* <0 = error, 0 = continue, >0 = stop */ + res = checkp(func(state, cache, ¤t)); + if (res) + return res; + } res = checkp(dwarf_siblingof(¤t, ¤t)); } @@ -490,7 +494,145 @@ static int __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 bool is_reserved_member(Dwarf_Die *die) +{ + const char *name = get_name(die); + + return name && !strncmp(name, RESERVED_PREFIX, RESERVED_PREFIX_LEN); +} + +static int __process_reserved_struct(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + Dwarf_Die type; + + /* + * If the union member is a struct, expect the placeholder type to + * be the first member, i.e.: + * + * union { + * type replaced_member; + * struct { + * type placeholder; // <- type + * } + * }; + * + * Stop processing if this member isn't reserved. + */ + if (!is_reserved_member(die)) + return NOT_RESERVED; + + if (!get_ref_die_attr(die, DW_AT_type, &type)) { + error("structure member missing a type?"); + return -1; + } + + check(process_type(state, cache, &type)); + return RESERVED; +} + +static int __process_reserved_union(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + int res = NOT_RESERVED; + Dwarf_Die type; + + if (!get_ref_die_attr(die, DW_AT_type, &type)) { + error("union member missing a type?"); + return -1; + } + + /* + * We expect a union with two members. Check if either of them + * has the reserved 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. + * + * Stop processing after we've seen two members. + */ + if (is_reserved_member(die)) { + check(process_type(state, cache, &type)); + return RESERVED; + } + + if (dwarf_tag(&type) == DW_TAG_structure_type) + res = checkp(process_die_container(state, cache, &type, + __process_reserved_struct, + match_member_type)); + if (res != RESERVED && ++state->reserved.members < 2) + return 0; /* Continue */ + + return res; +} + +static int process_reserved(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + if (!stable) + return NOT_RESERVED; + + /* + * To maintain a stable kABI, distributions may choose to reserve + * space in structs for later use by adding placeholder members, + * for example: + * + * struct s { + * u64 a; + * // placeholder + * 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 { + * u64 a; + * union { + * // replaced member + * struct t b; + * struct { + * // original placeholder + * 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. + * + * Note that the user is responsible for ensuring that the + * replacement type is ABI compatible with the placeholder type. + */ + state->reserved.members = 0; + + return checkp(process_die_container(state, cache, die, + __process_reserved_union, + match_member_type)); +} + +static int process_union_type(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + if (checkp(process_reserved(state, cache, die)) == RESERVED) + return 0; + + return check(__process_structure_type(state, cache, die, "union_type ", + ___process_structure_type, + match_all)); +} static int process_enumerator_type(struct state *state, struct die *cache, Dwarf_Die *die) diff --git a/scripts/gendwarfksyms/examples/reserved.c b/scripts/gendwarfksyms/examples/reserved.c new file mode 100644 index 000000000000..1e8de7ccd7d2 --- /dev/null +++ b/scripts/gendwarfksyms/examples/reserved.c @@ -0,0 +1,66 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2024 Google LLC + * + * Reserved data structure field example. See dwarf.c:process_reserved + * for details. + * + * $ gcc -g -c examples/reserved.c + * + * With --stable, only the reserved field placeholder is used for calculating + * symbol versions. + * + * $ echo exported0 | ./gendwarfksyms --stable --dump-dies reserved.o + * variable structure_type struct0 { + * member base_type int byte_size(4) encoding(5) data_member_location(0), + * member base_type long int byte_size(8) encoding(5) data_member_location(8), + * } byte_size(16) + * + * $ echo exported1 | ./gendwarfksyms --stable --dump-dies reserved.o + * variable structure_type struct1 { + * member base_type int byte_size(4) encoding(5) data_member_location(0), + * member base_type long int byte_size(8) encoding(5) data_member_location(8), + * } byte_size(16) + * + * $ echo exported2 | ./gendwarfksyms --stable --dump-dies reserved.o + * variable structure_type struct2 { + * member base_type int byte_size(4) encoding(5) data_member_location(0), + * member base_type long int byte_size(8) encoding(5) data_member_location(8), + * } byte_size(16) + */ + +struct struct0 { + int a; + union { + long __kabi_reserved_0; + struct { + int b; + int c; + }; + }; +}; + +struct struct1 { + int a; + union { + struct { + int b; + int c; + }; + long __kabi_reserved_1; + }; +}; + +struct struct2 { + int a; + union { + unsigned long b; + struct { + long __kabi_reserved_1; + }; + }; +}; + +struct struct0 exported0; +struct struct1 exported1; +struct struct2 exported2; diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h index 05b5c01b1c2a..963a07167892 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -220,12 +220,27 @@ extern void cache_clear_expanded(struct expansion_cache *ec); /* * dwarf.c */ + +/* See dwarf.c:process_reserved */ +#define RESERVED_PREFIX "__kabi_reserved" +#define RESERVED_PREFIX_LEN (sizeof(RESERVED_PREFIX) - 1) + struct expansion_state { bool expand; bool in_pointer_type; unsigned int ptr_expansion_depth; }; +enum reserved_status { + /* >0 to stop DIE processing */ + NOT_RESERVED = 1, + RESERVED +}; + +struct reserved_state { + int members; +}; + struct state { Dwfl_Module *mod; Dwarf *dbg; @@ -235,6 +250,9 @@ struct state { /* Structure expansion */ struct expansion_state expand; struct expansion_cache expansion_cache; + + /* Reserved members */ + struct reserved_state reserved; }; typedef int (*die_callback_t)(struct state *state, struct die *cache, From patchwork Thu Aug 15 17:39:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sami Tolvanen X-Patchwork-Id: 13765052 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 DB2EA1C0DC7 for ; Thu, 15 Aug 2024 17:39: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=1723743586; cv=none; b=bothdZg9mfnqtqbzvGTazJ8VN/zFnYZ0zcpPU/Onhrh7BcnEqFQ2qLcxpuNP7WJ1pPX82sDjkBsbAQ1ZtYD9ObK2E+T/fRYg5OwTVs7hxYJ/Iogd3c+093e3oOu0fwNOTOmRzk39Ou1SYhe+HVGQEwaf4aUMiUEOHifFRWhGJr0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743586; c=relaxed/simple; bh=u2NAoGPKAPKT+TpiqoFdPon2GiyhMXZdDU3h7zflpd4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=llSTwy3yUwIilU6pBkUChaDQyDCsV6hS+T4wut8tkUpGSTKuExqZkNyARu05vVA/COI2WgwwoDkuUnkcgsoLMvOgtjLv2VC3It0KYTgH//kPQ7mL+cbkeUGNLAm1NQrXmL0hMdxU5pxi7S3zb71LYQmFXcdlhNdQymbJ9hJQLPY= 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=vlQ+e9jU; 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="vlQ+e9jU" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6b052c5939cso7864067b3.1 for ; Thu, 15 Aug 2024 10:39:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723743584; x=1724348384; 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=rvkihQVxilci4ahNE1D8FzulqWknfQTNlLMa6I6x8hA=; b=vlQ+e9jUjZcOlUACbuVApWiv2hKkg+gCV9rYJJBOuMYl5PzhW4Z4uf6aaqNVol20iu 1xi/gaIuNtbwHL0hiXzWRLGbfxuwAbOx1kGWHS2ocDL2u+lTUOmowzYOfq14uaHoGW7V CRodc+WHbq9O0/q1KsvjTlBu+EspBPsu0FFFBkGIoUmbUc9WXN1sS+x8bX+JqShdwdqf mXjeswQOAwf57XDpfMQV+V923uLusqdIY6yA1YojOqk39DOTE+j0bjrh7iXx3W2NnMON H4eISkeXe0yFarXA0drBDsCupet+gy7adwGmLo2iglCKwDWtuqsdjnrsx0l5IUoBqsf/ 1DlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723743584; x=1724348384; 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=rvkihQVxilci4ahNE1D8FzulqWknfQTNlLMa6I6x8hA=; b=oWNgsUQDexJ+3VxRmLaSoxPOIh1akAVImU0JN9Gb4UoXHBcW89QdSXJvBro9X0aVGp sJ++MuvayJTGuwBpECNUd93TKZgdh6A5PTxDOYCOVis6tVZRr/2QKyIVc56W4UGMX1Wb 1uqcAvlQc1fwaUCXGWQ72/dOnAv3hCaDyRv0wBYMoecdAbPdqlN2vVN3t2kuWp3iG0tT GuAKDZXPVKdnX3eGVWoirFBHld5hbZmWDgBhRzG9uLdm6Om1J5LV1hKnkhQDNb5U8xwe T0Hg5x9mxIGrkUsDkXhtPBHAERZQ2qzXT819/WUU1qoZLllzAYNLQ/TtiNXRHjOoo22z 6PGQ== X-Forwarded-Encrypted: i=1; AJvYcCVe8fqu1N2pMh6CK7ptFWkHLdYJVnc5TEQtgZsWxgGesrjczj/ibVf0ZlprGLtBvkb8r5txTXPgFDdbhWZhJ3hqIWUla0GFHFXJrxVq X-Gm-Message-State: AOJu0Yzhqf0Lzvf0Xp3XPEla0Jb6WM7CNTdB1CzdAFsDeaCFM/jJEDrm umGciEe2A8RT7kCLUoaODvzqEoqwAaNgagLNg2IG4bWhKJ4mJbNRcn/PHb4BRSiuAhMG2xBuoxc 0lMe+1Uf/wezoBti5zvHxvGDU8A== X-Google-Smtp-Source: AGHT+IG9S2PSLwKc0udu8I+6MHxXANcYb3L8yRqmH7TB0UIAP0u33tVkU9SrPY57CXk2d2U4eRcDrYniR8MuW7MasLc= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a05:690c:6208:b0:68e:edf4:7b42 with SMTP id 00721157ae682-6b1d42694c8mr59377b3.2.1723743583755; Thu, 15 Aug 2024 10:39:43 -0700 (PDT) Date: Thu, 15 Aug 2024 17:39:21 +0000 In-Reply-To: <20240815173903.4172139-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240815173903.4172139-21-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=u2NAoGPKAPKT+TpiqoFdPon2GiyhMXZdDU3h7zflpd4=; b=owEB7QES/pANAwAKAUy19oSLvFbuAcsmYgBmvj06nYnAUNx8o66EBLX4cU2eInAJSpLP8qYpz sKtbF+ZNs+JAbMEAAEKAB0WIQQ1zPtjsoPW0663g5RMtfaEi7xW7gUCZr49OgAKCRBMtfaEi7xW 7h06DACM546WdYBdw7Q9mrV/mD1RWAUpswtCJqVQxJ1f5GKGao7eEcXupGQuIPnw5Ey1+gEthL/ zUx8fmz4IOOWvMPnn54qc/0OYW6/yOLHQwIOrDeJJf1hMUgHTEdAad87xgKJCve78B3TGIfh2nK 0yG7q7yEEOxwnfzUWqETFg86JLzNqDLEcF+Le7QPYIg1crSXxPSGj6Qiq+dIClVReWIE6CEXm6A jWmn1/KCkWAGpc279+B+yrNNeukRQIgxYLJ88G+cLlgO31F4vU6302m5gBkhobfa5+qVTq3iPBi l2Qpx22OOHSDBGGwUufoz7aw+kwon3jhDBACj7QQTb+EJlImWNzKGwUtNj/9Meh0Z3Xr4ZEiquw A0WEfBnHtAuLfLpMfn2gsfq/4/5XA4sjbgrBgGBx5xR+Oa1fZwoL0dsfPIBqAXw9LoxbFAMytxK plh5XkQf3fItnk4E4Zyj5I8eM3vUaUZ4Mr52o+5/5O182vI0Y8C1VVTNzQiI1HDbrRaK8= X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240815173903.4172139-38-samitolvanen@google.com> Subject: [PATCH v2 17/19] 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 , Wedson Almeida Filho , Gary Guo , Petr Pavlu , Neal Gompa , Hector Martin , Janne Grunau , Asahi Linux , 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 Aug 15 17:39: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: 13765053 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 BB6F71C0DF5 for ; Thu, 15 Aug 2024 17:39:46 +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=1723743588; cv=none; b=f4NUbtdv1rvknFqboSx5aEnj4tZQFquCV2zZYNpns7zHnaJeahsl15FGGcieOaqQQcyNhr/bEkAxMNQt2aqaHu8OKQ+vooWUIdPgaBMdAQqVT+ETXeMYouE4+2uIeFWancNEvW4CxySZ4NGqmrMsWE/WL8/0KQ7A7e4XcbnSXc0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743588; c=relaxed/simple; bh=JdHnM47y3DGHzd+8iOwDpRmp/jKwcxYBnVNDUzIM1Ts=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FEQTRkg6DA4ki4dbdz7q6xHxFO2W2P/lxLS1Grq5qNXdC1IGtMUjO2QiSD9NfrkLEM9iVOyNDFmWckDX1iEopsUPfGCmyelTBgs3Zn3bPMYSZGZQtr09GmrKoT7PVO1yvMEPulRmavDYWXPvlKsYXDNn25Dx2R/WM9TIazG+o6E= 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=ZM1lGGhX; 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="ZM1lGGhX" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e11703f1368so1327376276.1 for ; Thu, 15 Aug 2024 10:39:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723743586; x=1724348386; 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=Nzfrcr5/2Jg+dBktFb/j3BHPnQVbNwPzVAym41fmhTI=; b=ZM1lGGhXjiDPhgK4at+h2mr3e1tDaZuDEHY6ZxxARgVSpnrHK1cTG+x9yd2V+ANGD/ 1U3IZfMP720YfOFQRWr6cButoOk765Ba/O23/T+KeTyJHVxH+yM5M2o1cbB60AqwZLsU uk2FRvf2K7HzzOlcK3RQSu02JfWftOCqkX/IVCQN/UDEPprGRzc+C1wFk96bonWWls6r 0dAvuVwg0io3YD0RvI1WBaL70Oi2AF/FC2Gvu59u/OFBHXgsZBfWALkXzhZoIahf52pc h/+7/KfBaJyE+s5mgVygIieH/TOg9ORrVqUEQ5XKn3QP6ksR4nGJiieUCJpXgmI1ceVN MACw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723743586; x=1724348386; 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=Nzfrcr5/2Jg+dBktFb/j3BHPnQVbNwPzVAym41fmhTI=; b=sbQZqVBJxATIp3P++8Ptov5+mw4lPiNtUhI/51GN7bOe3vIX7ea+/oBqCGLKrJNvvE Kk+oboAmUMD5eoCLCwzfKOmJPAp/AGQN/7ltKXGJ4YHspm0LdbpYQy/5IlnWu4fQx6zc u5M//1LZ6yqDpc0ENtTrmf8FYQbqo7nPvsenPS9jIeY2WAeNLCOznE7t7wD1g/WqtM01 fztlpN/7kSZms2P5umIyBx2eGs4we2Wu1+N7Bs8kSaXD4MZtT89qulId7lkO/ENHSTer bg4H+VUnHYSuGAvJAWf9liF36pFWG8Q2n7jPEHgG4j+YHP7dxM+6h/33toKesQRpWXXL Mo8Q== X-Forwarded-Encrypted: i=1; AJvYcCW577Iryr5gITh6gs40kvVG2Bpv2m5jMZiFgc7ObLKPXt+dhFBe+tRselkzXRl6Am/DTJHAqatq7vVXHfYl6TGMUbC3Vmnx49h99Oy/ X-Gm-Message-State: AOJu0YyDeKbmfm+0Q97lwSXx1hJ2GStvpxyHDWvqgQmIYlCSf9vWJmo8 Rx9MUHf+fGgjfvYePmaM1B8ZAhNAkK/j9kA+MGnarY1XH/MWTnBaGjgrjIJZdi1uVcM9i0PkcNi mU9p4Nqy4Y90FbvWYltT4sYnTWA== X-Google-Smtp-Source: AGHT+IEyPYGVIGctM8flGqu9QR91NaZgOu+gmaEjFyDcEPukNhfYOFghBSH/3RMkvc9pPV1ROu/JBf6StL4PXETlzNk= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a25:aad0:0:b0:e0b:ab63:b9c8 with SMTP id 3f1490d57ef6-e1180fa06d5mr496276.11.1723743585775; Thu, 15 Aug 2024 10:39:45 -0700 (PDT) Date: Thu, 15 Aug 2024 17:39:22 +0000 In-Reply-To: <20240815173903.4172139-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240815173903.4172139-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=635; i=samitolvanen@google.com; h=from:subject; bh=JdHnM47y3DGHzd+8iOwDpRmp/jKwcxYBnVNDUzIM1Ts=; b=owEB7QES/pANAwAKAUy19oSLvFbuAcsmYgBmvj06AdeqULMg79EuNtCXP+0M0mvzmV7eGmEL1 TuO+ezyrdOJAbMEAAEKAB0WIQQ1zPtjsoPW0663g5RMtfaEi7xW7gUCZr49OgAKCRBMtfaEi7xW 7lvXC/9FcJ2/EUgwUrKKscdKY50Al5IkUmE5/+LjfwqoQUfzZ3RY4s6dV6KbAcj4/Bhr2Xb4dt/ 2e9OMOyAkxPtsaxq2meNZWjTCOs5B1S9dt4YHoHFuFH7zcjd6gWIMJI8k1mlaXiZlcaS4yy3/fU 3JI5uzSOu/9bidigVxQV1rgTfC5cAw8PKaMpTuiZ1mgPWAUrADAZMBpdO8sSULOKFzef1yk32ya 89z7CW/fL0vwVz+9RZktwxObxSFIvAgmq5UkMp/Q0PS/lsj6kCbKsje6YaKlpgZys5otmjCXA47 cwLMLAhG/Yr79l5URg1O0tiij11q9zeoKn+2tGUH/qOls7UpOgHzoGaqcBaQBhLm7heKDc4DG+Y ZT0AQdp9myWj0erPLfH9Slv3EbXdbYBX1fN1lQvOb2LGm0vExdj1vlXX2J5PPetMZ9f9eSKN21G bKZcAaEPBGqMik8HlMXvCBOIAs2VM0OkWTxp3tQE5pgq0BPWeb8KEp9M9H3mEzX8UeiQs= X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240815173903.4172139-39-samitolvanen@google.com> Subject: [PATCH v2 18/19] x86/asm-prototypes: Include From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Wedson Almeida Filho , Gary Guo , Petr Pavlu , Neal Gompa , Hector Martin , Janne Grunau , Asahi Linux , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen refers to struct pt_regs, make sure it's visible. Signed-off-by: Sami Tolvanen --- arch/x86/include/asm/asm-prototypes.h | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/x86/include/asm/asm-prototypes.h b/arch/x86/include/asm/asm-prototypes.h index 25466c4d2134..c82e68c8b7c2 100644 --- a/arch/x86/include/asm/asm-prototypes.h +++ b/arch/x86/include/asm/asm-prototypes.h @@ -1,4 +1,5 @@ /* SPDX-License-Identifier: GPL-2.0 */ +#include #include #include #include From patchwork Thu Aug 15 17:39: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: 13765054 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.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 258E01C2328 for ; Thu, 15 Aug 2024 17:39:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743590; cv=none; b=Oce8Dyd6Bp1rFOHFdUbyZ54dwIzMyuG6E9YBVFdTGgWzLi1fwmmZUc0VU+8ZPob3YU+gpOsvQ0BurQ2uza16ypBJo7Uex5y7YX4RVggheZhxce6N0AFT1gKBmvVXRLjW7WAfC29OZl1L3XGyQVY1mZh8m90Y1jsHIKgNOUM76LY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723743590; c=relaxed/simple; bh=mz7nffxN7wId2ePyNyx2gbUj67IvJCbAVk8B4r6F3JI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=g0NavwamnOdU3/54ys1Rax638Fn07k0TBkq0UB4/2TO4eI3LjcgEZ0yzgWvLT/PYXstGlzRQGjv9pkz6FfMc0XXWnXk2F3O95zT2zzySEJuR3C1TtgAJLQ9k54Z5xxclYKun6zyKoYqr8AZVPZPYcdo9gjmpKzEL8AxcdpI55k8= 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=LnEwPMPV; arc=none smtp.client-ip=209.85.215.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="LnEwPMPV" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-7b544c7f7b3so1025193a12.2 for ; Thu, 15 Aug 2024 10:39:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723743588; x=1724348388; 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=cHfKDENnTQm8ek88L2arGqWCp64m5+2eabNApwrdQWY=; b=LnEwPMPVbeoeSua6GvHxSbd3y5TvC2a1MGWzskd+CAB2jSmEQa8CFFFLIy6KQf+stt KPs5p0wata407FkfAeh+TrsfxbWSMeVI47oY0UtRDCZqEkGG6Q8vomsxLgPcEhS+jubq DP3IXpU2WOeva7q8t6nABbuE9Gfz9CnfTizVomTQxZeCV3kG8+C1O8BOP7R0PQgsNas8 Cgf1NePbfo6Gdc46iUvnj0rLIXoed2aQPAlwNYtSONobR4kxE/8lkvpbQqwJ9jvpguA8 LpNKXd25NKk0vS11OS8q5sM4XNjMrpkOtuRfyBXjVvcRctbXRpJREZ4butLsRUR1ktIY 3ZLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723743588; x=1724348388; 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=cHfKDENnTQm8ek88L2arGqWCp64m5+2eabNApwrdQWY=; b=Yg/80I3XYBX4lMsXXK7CZ03z1Qpp2ImdoZD7V9XHp7hq3LR+Q94GzY4yyTIEAezgsW XdVCvmveujJdflr1cFGBaWBPCpUkItU7fLGzrXtLCSqRzbMien+v7azfPnes+md77QVu +ISFaAaKwc08cP179m/n1N8juWBpdeAwY/G2JuEbJug8t9UVX15NxSWEJF5B74cj+g/K Xp6Agwqhf4iSbLi3DnQvl3oa3yZadWlIuqcohUZW+XQnJXDzzfnvJs8uIGvu41EPiCmr o6faPCA/2iRX69PpzCFExwGGw+a0y1aYUhU7nmirVGm5BF4tROiLcQpmURv1RFUxb1RO Vo0A== X-Forwarded-Encrypted: i=1; AJvYcCXDqRcn4QXMHlEcyn9XUS2fYKHqDJsOJhSXXIJwtWCuyM2BS6hu+tO+vOw3VHBAEbr9NI+dd7/5Gf+9lFgD6Ism2KVJ9YZwxup6DBKe X-Gm-Message-State: AOJu0YwGZBm0Bf/uJ7m5IPGreSUyNmAIKteLQtEbQjYQAOxncebEtoL/ nC/83FC0oUGua9Xyw//G2hA2+/zr4RIyUHcCQt7WluVWZMnwjzdooBgkT7A4pX6udANcWlcTThH +Dizw2I9ME09sKtIoc017IecsfQ== X-Google-Smtp-Source: AGHT+IFTAkXFaRwAV/SQwqCeTGEycib4FiTINhjKTaFPZbrPJmJ+KMbkdPWYs8+NjnMSf4VPl8XwcMRzylf5jVza6Gs= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a63:af1c:0:b0:75e:cf19:8f83 with SMTP id 41be03b00d2f7-7c9796d8a56mr125a12.3.1723743587293; Thu, 15 Aug 2024 10:39:47 -0700 (PDT) Date: Thu, 15 Aug 2024 17:39:23 +0000 In-Reply-To: <20240815173903.4172139-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240815173903.4172139-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=4761; i=samitolvanen@google.com; h=from:subject; bh=mz7nffxN7wId2ePyNyx2gbUj67IvJCbAVk8B4r6F3JI=; b=owEB7QES/pANAwAKAUy19oSLvFbuAcsmYgBmvj06CUaKOoKTE/x4zhmMUl5+xK4VKhS9egsRZ EV60/RxPFeJAbMEAAEKAB0WIQQ1zPtjsoPW0663g5RMtfaEi7xW7gUCZr49OgAKCRBMtfaEi7xW 7rzZDACSD3JRn+H/NSXRZE7PImLTyCeLXiFaUKPrqH9OX1i0VZRE+Ujk3Fwuv1EQvYxnkTXOGSQ 4pzHAXhRMgHLUw9Pj6WaFvL15HccicXnzBlDKBCRbchKw1xRa1hgd5BD1dOVLal2i2yGJ5wjWj0 w7hogl6gH6uVQcLpDQAhAGHCzMprB+Joyc/Yp+xwhcPTqCWeElf4fcBwKR8i+IpLf9hRgmG6p6y AqDez4vPKDD85j4xvlyhkuasvReQEsWWB3j/2swrrMVBprrGZnIIje8us1UinjDhg521N+mtV5X WWIOKRhrhCyv8kmHEKUOgoBp2zNSJEGIimSkz/3s7lrkIrlCdwmFsd3vgNEMbaS5mR/AkgDcqyv 5XaOmdjdfTVLCvqAPWx3Px2kXJ2M1bx1VAZNuzXWuQPsCaMOWd9yEpuyXnor1R7Aoaw9hG6nYHy l/aa9uwh9DOgnzHvAaeyx1TKhEx+sIF5Gh3O3D2TvLFWr5TNOv0agadESGCu/jUR5jhUs= X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240815173903.4172139-40-samitolvanen@google.com> Subject: [PATCH v2 19/19] 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 , Wedson Almeida Filho , Gary Guo , Petr Pavlu , Neal Gompa , Hector Martin , Janne Grunau , Asahi Linux , 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 | 34 ++++++++++++++++++++++++++++------ 3 files changed, 53 insertions(+), 8 deletions(-) diff --git a/kernel/module/Kconfig b/kernel/module/Kconfig index a506d4ac660f..80b2e9b0596b 100644 --- a/kernel/module/Kconfig +++ b/kernel/module/Kconfig @@ -168,13 +168,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 2fd0199662e9..52db4f1f37c4 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 diff --git a/scripts/Makefile.build b/scripts/Makefile.build index efacca63c897..985e4ba7b813 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -122,13 +122,22 @@ cmd_cpp_i_c = $(CPP) $(c_flags) -o $@ $< $(obj)/%.i: $(obj)/%.c FORCE $(call if_changed_dep,cpp_i_c) +gendwarfksyms := scripts/gendwarfksyms/gendwarfksyms +getexportsymbols = $(NM) $(1) | sed -n 's/.* __export_symbol_\(.*\)/$(2)/p' + genksyms = scripts/genksyms/genksyms \ $(if $(1), -T $(2)) \ $(if $(KBUILD_PRESERVE), -p) \ -r $(or $(wildcard $(2:.symtypes=.symref)), /dev/null) # These mirror gensymtypes_S and co below, keep them in synch. +ifdef CONFIG_GENDWARFKSYMS +cmd_gensymtypes_c = $(if $(skip_gendwarfksyms),, \ + $(call getexportsymbols,$(2:.symtypes=.o),\1) | \ + $(gendwarfksyms) $(2:.symtypes=.o) $(if $(1), --symtypes $(2))) +else cmd_gensymtypes_c = $(CPP) -D__GENKSYMS__ $(c_flags) $< | $(genksyms) +endif # CONFIG_GENDWARFKSYMS quiet_cmd_cc_symtypes_c = SYM $(quiet_modtag) $@ cmd_cc_symtypes_c = $(call cmd_gensymtypes_c,true,$@) >/dev/null @@ -324,14 +333,27 @@ $(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) \ + $(if $(1), --symtypes $(2)) +else +cmd_gensymtypes_S = \ + $(getasmexports) | \ + $(CPP) -D__GENKSYMS__ $(c_flags) -xc - | $(genksyms) +endif # CONFIG_GENDWARFKSYMS quiet_cmd_cc_symtypes_S = SYM $(quiet_modtag) $@ cmd_cc_symtypes_S = $(call cmd_gensymtypes_S,true,$@) >/dev/null