From nobody Mon Nov 25 05:25:12 2024 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 7A2671D86C0 for ; Thu, 21 Nov 2024 20:42:55 +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=1732221778; cv=none; b=o7Ui3CIUkHrT3Ss9BRAFmmTrY+/BRiOSPWIBoWsUIUbyp9/r0tLXo/xzJs/2FGIYjCidsnVOtARZt29cf7AsQYnz2mS/LXrrpZgzKaOkIH92daP7Z7+wH6witQN1q3VASMU8b5JmJ3CNrbUrqoBADFqFVvgJbPRyzrOtXCqRG2o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732221778; c=relaxed/simple; bh=+87wRWX8FfsTtq4xSYLpb+JNh3+dyVdlqbxELomxVag=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=E8aH1SzTJvsdgcU4seoW9bLTGhxI4YIBE7pqeUYtDrhjAYwG/xAwHG20+od9tEtoTbRsHpEpZMNNkK84hddVzZ62r/H80Gu569xO7S8mn2LbhDLP59iJa/kLvHJLrrIdzKeQ1SseZRCw80ne1NuA0o6PPH6t2I2gcbhrl13jVtw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Zpdv0lB6; arc=none smtp.client-ip=209.85.214.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="Zpdv0lB6" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2129bf203d9so5118165ad.0 for ; Thu, 21 Nov 2024 12:42:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732221775; x=1732826575; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=vSBdFEgbOrqYxqatdw90LLZcTNyeQtzBA1zNti6V7OI=; b=Zpdv0lB63wc2oP+RWDcxGY2fnK5fTJ06qJRWdi/iR2rfKVzFguRWPqYpC7aLNnG6Eg ZK8JI0GhqC4LR7B9bjb/XqTuwZp2IrdE09M9bL7X1r5xtgTdadAwZVW1BxEajs1vPtd1 IMueWMMi5iIP04esUv4tn2HQD0WO9g0q+MrA3Mqa2brpGgDXqYZMvlbeS8Kru6PAuRqC 6j89WhyBiPizXugXYgueSOMqmajZAO4duuOq/DWmnoZPgcwF8ZeRR0nNdEFzWBTNE7XB 4ZAEnKRr9++ne5pqAMqg+u1eF1rzFdb+ay1Z71O9eBGIbf9q0sV6mf6bvcUzfycjYmBE tREg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732221775; x=1732826575; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=vSBdFEgbOrqYxqatdw90LLZcTNyeQtzBA1zNti6V7OI=; b=mz7uhwBOqq2Wovn5GI1H6E8JniUjk60r/nskKLW2Wvaxj52C78sft8Pxdd/KRSO4HA bAtupbgR/xNsKXBtanXv3LJK1fC71Y5GNopl/0vbpqYkkJsLqelkKAZiNr7VbF3h7dOd IJgRLoRJyX4sk/2BPHn3xmEMaFOt+wWiZUKmH++QA3+o4YeQtEhDb3TMzvrSgX0vXfHb 3IeIvJPCGnrPM2ud9uLRg9aQc+xHKgSrnyV8/98fA7M58CrOPToHbAKARcR5QmzxboRU rXRwDVRYo3sTnV7E30gbvcU1WbJjoF+UzZS/ErbVrv2N4CaVw5Dd9e1Vhz1fOCr99x6x +m7w== X-Forwarded-Encrypted: i=1; AJvYcCW0JNFz+4p+FypPJY4yv6as2yHrCwhNtaSycnZNyvCh4LTBSYHaSQ1A/Gu8m0ZuTBphE/SJ3U1SYr28sy8=@vger.kernel.org X-Gm-Message-State: AOJu0YyfOxv+2mAUzFABnxscw78B6P8mo+3CtdO/q0RJ7UDWtc78mY9T JPgusYaAAZWrUrPGMofAS4SPd9RSpWeykS9htjYIVUwiCtSqh4UASsoiR0zLCIq9rLd1r8VREvx BWk8qGO4G71O25ur8ZhXKCSDXSA== X-Google-Smtp-Source: AGHT+IF59PpuAkqWOFNmN7S7Fz06WzIx7bGFa+nImvct3hmlYntcXhO6BBiDf9SqB9/zAUWFxuv9z14FKtFlbEY9VnQ= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a17:903:22c9:b0:20c:62e1:635d with SMTP id d9443c01a7336-2129f27f694mr2765ad.7.1732221774743; Thu, 21 Nov 2024 12:42:54 -0800 (PST) Date: Thu, 21 Nov 2024 20:42:32 +0000 In-Reply-To: <20241121204220.2378181-20-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241121204220.2378181-20-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=16900; i=samitolvanen@google.com; h=from:subject; bh=+87wRWX8FfsTtq4xSYLpb+JNh3+dyVdlqbxELomxVag=; b=owGbwMvMwCEWxa662nLh8irG02pJDOn2s41aN4dWrA/qzuKYedRgx5M482s669zMr8oJ/D7+c 9r6ubsXdpSyMIhxMMiKKbK0fF29dfd3p9RXn4skYOawMoEMYeDiFICJtPgyMqzv4rZ1vf/sQOsC vwllsx1K4sqkN7zgm6CyZomURsmchmcM/z3Ln5z533Rff5KHdNER1T32Vac478T1MOv/6X09q5R TnQMA X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241121204220.2378181-31-samitolvanen@google.com> Subject: [PATCH v6 11/18] gendwarfksyms: Add symtypes output From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 | 11 + scripts/gendwarfksyms/dwarf.c | 1 + scripts/gendwarfksyms/gendwarfksyms.c | 33 ++- scripts/gendwarfksyms/gendwarfksyms.h | 19 ++ scripts/gendwarfksyms/symbols.c | 4 +- scripts/gendwarfksyms/types.c | 359 ++++++++++++++++++++++++++ 7 files changed, 425 insertions(+), 3 deletions(-) create mode 100644 scripts/gendwarfksyms/types.c diff --git a/scripts/gendwarfksyms/Makefile b/scripts/gendwarfksyms/Makefile index c06145d84df8..6540282dc746 100644 --- a/scripts/gendwarfksyms/Makefile +++ b/scripts/gendwarfksyms/Makefile @@ -6,5 +6,6 @@ gendwarfksyms-objs +=3D cache.o gendwarfksyms-objs +=3D die.o gendwarfksyms-objs +=3D dwarf.o gendwarfksyms-objs +=3D symbols.o +gendwarfksyms-objs +=3D types.o =20 HOSTLDLIBS_gendwarfksyms :=3D -ldw -lelf diff --git a/scripts/gendwarfksyms/die.c b/scripts/gendwarfksyms/die.c index 2829387fd815..df1ca3a032bb 100644 --- a/scripts/gendwarfksyms/die.c +++ b/scripts/gendwarfksyms/die.c @@ -22,6 +22,7 @@ static inline unsigned int die_hash(uintptr_t addr, enum = die_state state) static void init_die(struct die *cd) { cd->state =3D DIE_INCOMPLETE; + cd->mapped =3D false; cd->fqn =3D NULL; cd->tag =3D -1; cd->addr =3D 0; @@ -83,6 +84,16 @@ static void reset_die(struct die *cd) init_die(cd); } =20 +void die_map_for_each(die_map_callback_t func, void *arg) +{ + struct hlist_node *tmp; + struct die *cd; + + hash_for_each_safe(die_map, cd, tmp, hash) { + func(cd, arg); + } +} + void die_map_free(void) { struct hlist_node *tmp; diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index cf6d2c2844dc..18e42dbbfa3c 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -744,6 +744,7 @@ static void process_symbol(struct state *state, Dwarf_D= ie *die, { debug("%s", state->sym->name); check(process_func(state, NULL, die)); + state->sym->state =3D SYMBOL_MAPPED; if (dump_dies) fputs("\n", stderr); } diff --git a/scripts/gendwarfksyms/gendwarfksyms.c b/scripts/gendwarfksyms/= gendwarfksyms.c index ed6d8c585268..76a38b733ad2 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.c +++ b/scripts/gendwarfksyms/gendwarfksyms.c @@ -21,6 +21,11 @@ int debug; int dump_dies; /* Print debugging information about die_map changes */ int dump_die_map; +/* Print out type strings (i.e. type_map) */ +int dump_types; +/* Write a symtypes file */ +int symtypes; +static const char *symtypes_file; =20 static void usage(void) { @@ -29,6 +34,8 @@ static void usage(void) " -d, --debug Print debugging information\n" " --dump-dies Dump DWARF DIE contents\n" " --dump-die-map Print debugging information about die_map c= hanges\n" + " --dump-types Dump type strings\n" + " -T, --symtypes file Write a symtypes file\n" " -h, --help Print this message\n" "\n", stderr); @@ -41,6 +48,7 @@ static int process_module(Dwfl_Module *mod, void **userda= ta, const char *name, Dwarf_Die cudie; Dwarf_CU *cu =3D NULL; Dwarf *dbg; + FILE *symfile =3D arg; int res; =20 debug("%s", name); @@ -60,6 +68,10 @@ static int process_module(Dwfl_Module *mod, void **userd= ata, const char *name, process_cu(&cudie); } while (cu); =20 + /* + * Use die_map to expand type strings and write them to `symfile`. + */ + generate_symtypes(symfile); die_map_free(); =20 return DWARF_CB_OK; @@ -72,22 +84,29 @@ static const Dwfl_Callbacks callbacks =3D { =20 int main(int argc, char **argv) { + FILE *symfile =3D NULL; unsigned int n; int opt; =20 struct option opts[] =3D { { "debug", 0, NULL, 'd' }, { "dump-dies", 0, &dump_dies, 1 }, { "dump-die-map", 0, &dump_die_map, 1 }, + { "dump-types", 0, &dump_types, 1 }, + { "symtypes", 1, NULL, 'T' }, { "help", 0, NULL, 'h' }, { 0, 0, NULL, 0 } }; =20 - while ((opt =3D getopt_long(argc, argv, "dh", opts, NULL)) !=3D EOF) { + while ((opt =3D getopt_long(argc, argv, "dT:h", opts, NULL)) !=3D EOF) { switch (opt) { case 0: break; case 'd': debug =3D 1; break; + case 'T': + symtypes =3D 1; + symtypes_file =3D optarg; + break; case 'h': usage(); return 0; @@ -107,6 +126,13 @@ int main(int argc, char **argv) =20 symbol_read_exports(stdin); =20 + if (symtypes_file) { + symfile =3D fopen(symtypes_file, "w"); + if (!symfile) + error("fopen failed for '%s': %s", symtypes_file, + strerror(errno)); + } + for (n =3D optind; n < argc; n++) { Dwfl *dwfl; int fd; @@ -129,12 +155,15 @@ int main(int argc, char **argv) =20 dwfl_report_end(dwfl, NULL, NULL); =20 - if (dwfl_getmodules(dwfl, &process_module, NULL, 0)) + if (dwfl_getmodules(dwfl, &process_module, symfile, 0)) error("dwfl_getmodules failed for '%s'", argv[n]); =20 dwfl_end(dwfl); } =20 + if (symfile) + check(fclose(symfile)); + symbol_free(); =20 return 0; diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/= gendwarfksyms.h index 10fb476a7b76..7b35043d28ff 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -27,6 +27,8 @@ extern int debug; extern int dump_dies; extern int dump_die_map; +extern int dump_types; +extern int symtypes; =20 /* * Output helpers @@ -95,6 +97,11 @@ static inline unsigned int addr_hash(uintptr_t addr) return hash_ptr((const void *)addr); } =20 +enum symbol_state { + SYMBOL_UNPROCESSED, + SYMBOL_MAPPED, +}; + struct symbol_addr { uint32_t section; Elf64_Addr address; @@ -105,6 +112,8 @@ struct symbol { struct symbol_addr addr; struct hlist_node addr_hash; struct hlist_node name_hash; + enum symbol_state state; + uintptr_t die_addr; }; =20 typedef void (*symbol_callback_t)(struct symbol *, void *arg); @@ -159,6 +168,7 @@ static inline const char *die_state_name(enum die_state= state) =20 struct die { enum die_state state; + bool mapped; char *fqn; int tag; uintptr_t addr; @@ -166,10 +176,13 @@ struct die { struct hlist_node hash; }; =20 +typedef void (*die_map_callback_t)(struct die *, void *arg); + int __die_map_get(uintptr_t addr, enum die_state state, struct die **res); struct die *die_map_get(Dwarf_Die *die, enum die_state state); void die_map_add_string(struct die *pd, const char *str); void die_map_add_linebreak(struct die *pd, int linebreak); +void die_map_for_each(die_map_callback_t func, void *arg); void die_map_add_die(struct die *pd, struct die *child); void die_map_free(void); =20 @@ -240,4 +253,10 @@ int process_die_container(struct state *state, struct = die *cache, =20 void process_cu(Dwarf_Die *cudie); =20 +/* + * types.c + */ + +void generate_symtypes(FILE *file); + #endif /* __GENDWARFKSYMS_H */ diff --git a/scripts/gendwarfksyms/symbols.c b/scripts/gendwarfksyms/symbol= s.c index e7343394248b..6ed0c4769e6f 100644 --- a/scripts/gendwarfksyms/symbols.c +++ b/scripts/gendwarfksyms/symbols.c @@ -92,6 +92,7 @@ void symbol_read_exports(FILE *file) sym =3D xcalloc(1, sizeof(struct symbol)); sym->name =3D name; sym->addr.section =3D SHN_UNDEF; + sym->state =3D SYMBOL_UNPROCESSED; =20 hash_add(symbol_names, &sym->name_hash, hash_str(sym->name)); ++nsym; @@ -107,7 +108,8 @@ static void get_symbol(struct symbol *sym, void *arg) { struct symbol **res =3D arg; =20 - *res =3D sym; + if (sym->state =3D=3D SYMBOL_UNPROCESSED) + *res =3D sym; } =20 struct symbol *symbol_get(const char *name) diff --git a/scripts/gendwarfksyms/types.c b/scripts/gendwarfksyms/types.c new file mode 100644 index 000000000000..57ef5cbcfd40 --- /dev/null +++ b/scripts/gendwarfksyms/types.c @@ -0,0 +1,359 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Google LLC + */ + +#include "gendwarfksyms.h" + +static struct cache expansion_cache; + +/* + * A simple linked list of shared or owned strings to avoid copying strings + * around when not necessary. + */ +struct type_list_entry { + const char *str; + void *owned; + struct list_head list; +}; + +static void type_list_free(struct list_head *list) +{ + struct type_list_entry *entry; + struct type_list_entry *tmp; + + list_for_each_entry_safe(entry, tmp, list, list) { + if (entry->owned) + free(entry->owned); + free(entry); + } + + INIT_LIST_HEAD(list); +} + +static int type_list_append(struct list_head *list, const char *s, void *o= wned) +{ + struct type_list_entry *entry; + + if (!s) + return 0; + + entry =3D xmalloc(sizeof(struct type_list_entry)); + entry->str =3D s; + entry->owned =3D owned; + list_add_tail(&entry->list, list); + + return strlen(entry->str); +} + +static void type_list_write(struct list_head *list, FILE *file) +{ + struct type_list_entry *entry; + + list_for_each_entry(entry, list, list) { + if (entry->str) + checkp(fputs(entry->str, file)); + } +} + +/* + * An expanded type string in symtypes format. + */ +struct type_expansion { + char *name; + size_t len; + struct list_head expanded; + struct hlist_node hash; +}; + +static void type_expansion_init(struct type_expansion *type) +{ + type->name =3D NULL; + type->len =3D 0; + INIT_LIST_HEAD(&type->expanded); +} + +static inline void type_expansion_free(struct type_expansion *type) +{ + free(type->name); + type->name =3D NULL; + type->len =3D 0; + type_list_free(&type->expanded); +} + +static void type_expansion_append(struct type_expansion *type, const char = *s, + void *owned) +{ + type->len +=3D type_list_append(&type->expanded, s, owned); +} + +/* + * type_map -- the longest expansions for each type. + * + * const char *name -> struct type_expansion * + */ +#define TYPE_HASH_BITS 16 +static HASHTABLE_DEFINE(type_map, 1 << TYPE_HASH_BITS); + +static int type_map_get(const char *name, struct type_expansion **res) +{ + struct type_expansion *e; + + hash_for_each_possible(type_map, e, hash, hash_str(name)) { + if (!strcmp(name, e->name)) { + *res =3D e; + return 0; + } + } + + return -1; +} + +static void type_map_add(const char *name, struct type_expansion *type) +{ + struct type_expansion *e; + + if (type_map_get(name, &e)) { + e =3D xmalloc(sizeof(struct type_expansion)); + type_expansion_init(e); + e->name =3D xstrdup(name); + + hash_add(type_map, &e->hash, hash_str(e->name)); + + if (dump_types) + debug("adding %s", e->name); + } else { + /* Use the longest available expansion */ + if (type->len <=3D e->len) + return; + + type_list_free(&e->expanded); + + if (dump_types) + debug("replacing %s", e->name); + } + + /* Take ownership of type->expanded */ + list_replace_init(&type->expanded, &e->expanded); + e->len =3D type->len; + + if (dump_types) { + checkp(fputs(e->name, stderr)); + checkp(fputs(" ", stderr)); + type_list_write(&e->expanded, stderr); + checkp(fputs("\n", stderr)); + } +} + +static void type_map_write(FILE *file) +{ + struct type_expansion *e; + struct hlist_node *tmp; + + if (!file) + return; + + hash_for_each_safe(type_map, e, tmp, hash) { + checkp(fputs(e->name, file)); + checkp(fputs(" ", file)); + type_list_write(&e->expanded, file); + checkp(fputs("\n", file)); + } +} + +static void type_map_free(void) +{ + struct type_expansion *e; + struct hlist_node *tmp; + + hash_for_each_safe(type_map, e, tmp, hash) { + type_expansion_free(e); + free(e); + } + + hash_init(type_map); +} + +/* + * Type reference format: #, where prefix: + * s -> structure + * u -> union + * e -> enum + * t -> typedef + * + * Names with spaces are additionally wrapped in single quotes. + */ +static char get_type_prefix(int tag) +{ + switch (tag) { + case DW_TAG_class_type: + case DW_TAG_structure_type: + return 's'; + case DW_TAG_union_type: + return 'u'; + case DW_TAG_enumeration_type: + return 'e'; + case DW_TAG_typedef_type: + return 't'; + default: + return 0; + } +} + +static char *get_type_name(struct die *cache) +{ + const char *quote; + char prefix; + char *name; + + if (cache->state =3D=3D DIE_INCOMPLETE) { + warn("found incomplete cache entry: %p", cache); + return NULL; + } + if (!cache->fqn || !*cache->fqn) + return NULL; + + prefix =3D get_type_prefix(cache->tag); + if (!prefix) + return NULL; + + /* Wrap names with spaces in single quotes */ + quote =3D strstr(cache->fqn, " ") ? "'" : ""; + + /* #\0 */ + if (asprintf(&name, "%c#%s%s%s", prefix, quote, cache->fqn, quote) < 0) + error("asprintf failed for '%s'", cache->fqn); + + return name; +} + +static void __type_expand(struct die *cache, struct type_expansion *type, + bool recursive); + +static void type_expand_child(struct die *cache, struct type_expansion *ty= pe, + bool recursive) +{ + struct type_expansion child; + char *name; + + name =3D get_type_name(cache); + if (!name) { + __type_expand(cache, type, recursive); + return; + } + + if (recursive && !__cache_was_expanded(&expansion_cache, cache->addr)) { + __cache_mark_expanded(&expansion_cache, cache->addr); + type_expansion_init(&child); + __type_expand(cache, &child, true); + type_map_add(name, &child); + type_expansion_free(&child); + } + + type_expansion_append(type, name, name); +} + +static void __type_expand(struct die *cache, struct type_expansion *type, + bool recursive) +{ + struct die_fragment *df; + struct die *child; + + list_for_each_entry(df, &cache->fragments, list) { + switch (df->type) { + case FRAGMENT_STRING: + type_expansion_append(type, df->data.str, NULL); + break; + case FRAGMENT_DIE: + /* Use a complete die_map expansion if available */ + if (__die_map_get(df->data.addr, DIE_COMPLETE, + &child) && + __die_map_get(df->data.addr, DIE_UNEXPANDED, + &child)) + error("unknown child: %" PRIxPTR, + df->data.addr); + + type_expand_child(child, type, recursive); + break; + case FRAGMENT_LINEBREAK: + /* + * Keep whitespace in the symtypes format, but avoid + * repeated spaces. + */ + if (list_is_last(&df->list, &cache->fragments) || + list_next_entry(df, list)->type !=3D + FRAGMENT_LINEBREAK) + type_expansion_append(type, " ", NULL); + break; + default: + error("empty die_fragment in %p", cache); + } + } +} + +static void type_expand(struct die *cache, struct type_expansion *type, + bool recursive) +{ + type_expansion_init(type); + __type_expand(cache, type, recursive); + cache_free(&expansion_cache); +} + +static void expand_type(struct die *cache, void *arg) +{ + struct type_expansion type; + char *name; + + if (cache->mapped) + return; + + cache->mapped =3D true; + + /* + * Skip unexpanded die_map entries if there's a complete + * expansion available for this DIE. + */ + if (cache->state =3D=3D DIE_UNEXPANDED && + !__die_map_get(cache->addr, DIE_COMPLETE, &cache)) { + if (cache->mapped) + return; + + cache->mapped =3D true; + } + + name =3D get_type_name(cache); + if (!name) + return; + + debug("%s", name); + type_expand(cache, &type, true); + type_map_add(name, &type); + + type_expansion_free(&type); + free(name); +} + +void generate_symtypes(FILE *file) +{ + cache_init(&expansion_cache); + + /* + * die_map processing: + * + * 1. die_map contains all types referenced in exported symbol + * signatures, but can contain duplicates just like the original + * DWARF, and some references may not be fully expanded depending + * on how far we processed the DIE tree for that specific symbol. + * + * For each die_map entry, find the longest available expansion, + * and add it to type_map. + */ + die_map_for_each(expand_type, NULL); + + /* + * 2. If a symtypes file is requested, write type_map contents to + * the file. + */ + type_map_write(file); + type_map_free(); +} --=20 2.47.0.371.ga323438b13-goog