File Index
Files and directories
Sign in
FILE

dwarf.c

File preview with syntax highlighting.

linux-6.19.14/scripts/gendwarfksyms/dwarf.c C
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Copyright (C) 2024 Google LLC
   4 */
   5
   6#define _GNU_SOURCE
   7#include <assert.h>
   8#include <inttypes.h>
   9#include <stdarg.h>
  10#include "gendwarfksyms.h"
  11
  12/* See get_union_kabi_status */
  13#define KABI_PREFIX "__kabi_"
  14#define KABI_PREFIX_LEN (sizeof(KABI_PREFIX) - 1)
  15#define KABI_RESERVED_PREFIX "reserved"
  16#define KABI_RESERVED_PREFIX_LEN (sizeof(KABI_RESERVED_PREFIX) - 1)
  17#define KABI_RENAMED_PREFIX "renamed"
  18#define KABI_RENAMED_PREFIX_LEN (sizeof(KABI_RENAMED_PREFIX) - 1)
  19#define KABI_IGNORED_PREFIX "ignored"
  20#define KABI_IGNORED_PREFIX_LEN (sizeof(KABI_IGNORED_PREFIX) - 1)
  21
  22static inline bool is_kabi_prefix(const char *name)
  23{
  24	return name && !strncmp(name, KABI_PREFIX, KABI_PREFIX_LEN);
  25}
  26
  27enum kabi_status {
  28	/* >0 to stop DIE processing */
  29	KABI_NORMAL = 1,
  30	KABI_RESERVED,
  31	KABI_IGNORED,
  32};
  33
  34static bool do_linebreak;
  35static int indentation_level;
  36
  37/* Line breaks and indentation for pretty-printing */
  38static void process_linebreak(struct die *cache, int n)
  39{
  40	indentation_level += n;
  41	do_linebreak = true;
  42	die_map_add_linebreak(cache, n);
  43}
  44
  45#define DEFINE_GET_ATTR(attr, type)                                    \
  46	static bool get_##attr##_attr(Dwarf_Die *die, unsigned int id, \
  47				      type *value)                     \
  48	{                                                              \
  49		Dwarf_Attribute da;                                    \
  50		return dwarf_attr(die, id, &da) &&                     \
  51		       !dwarf_form##attr(&da, value);                  \
  52	}
  53
  54DEFINE_GET_ATTR(flag, bool)
  55DEFINE_GET_ATTR(udata, Dwarf_Word)
  56
  57static bool get_ref_die_attr(Dwarf_Die *die, unsigned int id, Dwarf_Die *value)
  58{
  59	Dwarf_Attribute da;
  60
  61	/* dwarf_formref_die returns a pointer instead of an error value. */
  62	return dwarf_attr(die, id, &da) && dwarf_formref_die(&da, value);
  63}
  64
  65#define DEFINE_GET_STRING_ATTR(attr)                         \
  66	static const char *get_##attr##_attr(Dwarf_Die *die) \
  67	{                                                    \
  68		Dwarf_Attribute da;                          \
  69		if (dwarf_attr(die, DW_AT_##attr, &da))      \
  70			return dwarf_formstring(&da);        \
  71		return NULL;                                 \
  72	}
  73
  74DEFINE_GET_STRING_ATTR(name)
  75DEFINE_GET_STRING_ATTR(linkage_name)
  76
  77static const char *get_symbol_name(Dwarf_Die *die)
  78{
  79	const char *name;
  80
  81	/* rustc uses DW_AT_linkage_name for exported symbols */
  82	name = get_linkage_name_attr(die);
  83	if (!name)
  84		name = get_name_attr(die);
  85
  86	return name;
  87}
  88
  89static bool match_export_symbol(struct state *state, Dwarf_Die *die)
  90{
  91	Dwarf_Die *source = die;
  92	Dwarf_Die origin;
  93
  94	/* If the DIE has an abstract origin, use it for type information. */
  95	if (get_ref_die_attr(die, DW_AT_abstract_origin, &origin))
  96		source = &origin;
  97
  98	state->sym = symbol_get(get_symbol_name(die));
  99
 100	/* Look up using the origin name if there are no matches. */
 101	if (!state->sym && source != die)
 102		state->sym = symbol_get(get_symbol_name(source));
 103
 104	state->die = *source;
 105	return !!state->sym;
 106}
 107
 108/* DW_AT_decl_file -> struct srcfile */
 109static struct cache srcfile_cache;
 110
 111static bool is_definition_private(Dwarf_Die *die)
 112{
 113	Dwarf_Word filenum;
 114	Dwarf_Files *files;
 115	Dwarf_Die cudie;
 116	const char *s;
 117	int res;
 118
 119	/*
 120	 * Definitions in .c files cannot change the public ABI,
 121	 * so consider them private.
 122	 */
 123	if (!get_udata_attr(die, DW_AT_decl_file, &filenum))
 124		return false;
 125
 126	res = cache_get(&srcfile_cache, filenum);
 127	if (res >= 0)
 128		return !!res;
 129
 130	if (!dwarf_cu_die(die->cu, &cudie, NULL, NULL, NULL, NULL, NULL, NULL))
 131		error("dwarf_cu_die failed: '%s'", dwarf_errmsg(-1));
 132
 133	if (dwarf_getsrcfiles(&cudie, &files, NULL))
 134		error("dwarf_getsrcfiles failed: '%s'", dwarf_errmsg(-1));
 135
 136	s = dwarf_filesrc(files, filenum, NULL, NULL);
 137	if (!s)
 138		error("dwarf_filesrc failed: '%s'", dwarf_errmsg(-1));
 139
 140	s = strrchr(s, '.');
 141	res = s && !strcmp(s, ".c");
 142	cache_set(&srcfile_cache, filenum, res);
 143
 144	return !!res;
 145}
 146
 147static bool is_kabi_definition(struct die *cache, Dwarf_Die *die)
 148{
 149	bool value;
 150
 151	if (get_flag_attr(die, DW_AT_declaration, &value) && value)
 152		return false;
 153
 154	if (kabi_is_declonly(cache->fqn))
 155		return false;
 156
 157	return !is_definition_private(die);
 158}
 159
 160/*
 161 * Type string processing
 162 */
 163static void process(struct die *cache, const char *s)
 164{
 165	s = s ?: "<null>";
 166
 167	if (dump_dies && do_linebreak) {
 168		fputs("\n", stderr);
 169		for (int i = 0; i < indentation_level; i++)
 170			fputs("  ", stderr);
 171		do_linebreak = false;
 172	}
 173	if (dump_dies)
 174		fputs(s, stderr);
 175
 176	if (cache)
 177		die_debug_r("cache %p string '%s'", cache, s);
 178	die_map_add_string(cache, s);
 179}
 180
 181#define MAX_FMT_BUFFER_SIZE 128
 182
 183static void process_fmt(struct die *cache, const char *fmt, ...)
 184{
 185	char buf[MAX_FMT_BUFFER_SIZE];
 186	va_list args;
 187
 188	va_start(args, fmt);
 189
 190	if (checkp(vsnprintf(buf, sizeof(buf), fmt, args)) >= sizeof(buf))
 191		error("vsnprintf overflow: increase MAX_FMT_BUFFER_SIZE");
 192
 193	process(cache, buf);
 194	va_end(args);
 195}
 196
 197static void update_fqn(struct die *cache, Dwarf_Die *die)
 198{
 199	struct die *fqn;
 200
 201	if (!cache->fqn) {
 202		if (!__die_map_get((uintptr_t)die->addr, DIE_FQN, &fqn) &&
 203		    *fqn->fqn)
 204			cache->fqn = xstrdup(fqn->fqn);
 205		else
 206			cache->fqn = "";
 207	}
 208}
 209
 210static void process_fqn(struct die *cache, Dwarf_Die *die)
 211{
 212	update_fqn(cache, die);
 213	if (*cache->fqn)
 214		process(cache, " ");
 215	process(cache, cache->fqn);
 216}
 217
 218#define DEFINE_PROCESS_UDATA_ATTRIBUTE(attribute)                          \
 219	static void process_##attribute##_attr(struct die *cache,          \
 220					       Dwarf_Die *die)             \
 221	{                                                                  \
 222		Dwarf_Word value;                                          \
 223		if (get_udata_attr(die, DW_AT_##attribute, &value))        \
 224			process_fmt(cache, " " #attribute "(%" PRIu64 ")", \
 225				    value);                                \
 226	}
 227
 228DEFINE_PROCESS_UDATA_ATTRIBUTE(accessibility)
 229DEFINE_PROCESS_UDATA_ATTRIBUTE(alignment)
 230DEFINE_PROCESS_UDATA_ATTRIBUTE(bit_size)
 231DEFINE_PROCESS_UDATA_ATTRIBUTE(encoding)
 232DEFINE_PROCESS_UDATA_ATTRIBUTE(data_bit_offset)
 233DEFINE_PROCESS_UDATA_ATTRIBUTE(data_member_location)
 234DEFINE_PROCESS_UDATA_ATTRIBUTE(discr_value)
 235
 236static void process_byte_size_attr(struct die *cache, Dwarf_Die *die)
 237{
 238	Dwarf_Word value;
 239	unsigned long override;
 240
 241	if (get_udata_attr(die, DW_AT_byte_size, &value)) {
 242		if (stable && kabi_get_byte_size(cache->fqn, &override))
 243			value = override;
 244
 245		process_fmt(cache, " byte_size(%" PRIu64 ")", value);
 246	}
 247}
 248
 249/* Match functions -- die_match_callback_t */
 250#define DEFINE_MATCH(type)                                     \
 251	static bool match_##type##_type(Dwarf_Die *die)        \
 252	{                                                      \
 253		return dwarf_tag(die) == DW_TAG_##type##_type; \
 254	}
 255
 256DEFINE_MATCH(enumerator)
 257DEFINE_MATCH(formal_parameter)
 258DEFINE_MATCH(member)
 259DEFINE_MATCH(subrange)
 260
 261bool match_all(Dwarf_Die *die)
 262{
 263	return true;
 264}
 265
 266int process_die_container(struct state *state, struct die *cache,
 267			  Dwarf_Die *die, die_callback_t func,
 268			  die_match_callback_t match)
 269{
 270	Dwarf_Die current;
 271	int res;
 272
 273	/* Track the first item in lists. */
 274	if (state)
 275		state->first_list_item = true;
 276
 277	res = checkp(dwarf_child(die, &current));
 278	while (!res) {
 279		if (match(&current)) {
 280			/* <0 = error, 0 = continue, >0 = stop */
 281			res = checkp(func(state, cache, &current));
 282			if (res)
 283				goto out;
 284		}
 285
 286		res = checkp(dwarf_siblingof(&current, &current));
 287	}
 288
 289	res = 0;
 290out:
 291	if (state)
 292		state->first_list_item = false;
 293
 294	return res;
 295}
 296
 297static int process_type(struct state *state, struct die *parent,
 298			Dwarf_Die *die);
 299
 300static void process_type_attr(struct state *state, struct die *cache,
 301			      Dwarf_Die *die)
 302{
 303	Dwarf_Die type;
 304
 305	if (get_ref_die_attr(die, DW_AT_type, &type)) {
 306		check(process_type(state, cache, &type));
 307		return;
 308	}
 309
 310	/* Compilers can omit DW_AT_type -- print out 'void' to clarify */
 311	process(cache, "base_type void");
 312}
 313
 314static void process_list_comma(struct state *state, struct die *cache)
 315{
 316	if (state->first_list_item) {
 317		state->first_list_item = false;
 318	} else {
 319		process(cache, " ,");
 320		process_linebreak(cache, 0);
 321	}
 322}
 323
 324/* Comma-separated with DW_AT_type */
 325static void __process_list_type(struct state *state, struct die *cache,
 326				Dwarf_Die *die, const char *type)
 327{
 328	const char *name = get_name_attr(die);
 329
 330	if (stable) {
 331		if (is_kabi_prefix(name))
 332			name = NULL;
 333		state->kabi.orig_name = NULL;
 334	}
 335
 336	process_list_comma(state, cache);
 337	process(cache, type);
 338	process_type_attr(state, cache, die);
 339
 340	if (stable && state->kabi.orig_name)
 341		name = state->kabi.orig_name;
 342	if (name) {
 343		process(cache, " ");
 344		process(cache, name);
 345	}
 346
 347	process_accessibility_attr(cache, die);
 348	process_bit_size_attr(cache, die);
 349	process_data_bit_offset_attr(cache, die);
 350	process_data_member_location_attr(cache, die);
 351}
 352
 353#define DEFINE_PROCESS_LIST_TYPE(type)                                       \
 354	static void process_##type##_type(struct state *state,               \
 355					  struct die *cache, Dwarf_Die *die) \
 356	{                                                                    \
 357		__process_list_type(state, cache, die, #type " ");           \
 358	}
 359
 360DEFINE_PROCESS_LIST_TYPE(formal_parameter)
 361DEFINE_PROCESS_LIST_TYPE(member)
 362
 363/* Container types with DW_AT_type */
 364static void __process_type(struct state *state, struct die *cache,
 365			   Dwarf_Die *die, const char *type)
 366{
 367	process(cache, type);
 368	process_fqn(cache, die);
 369	process(cache, " {");
 370	process_linebreak(cache, 1);
 371	process_type_attr(state, cache, die);
 372	process_linebreak(cache, -1);
 373	process(cache, "}");
 374	process_byte_size_attr(cache, die);
 375	process_alignment_attr(cache, die);
 376}
 377
 378#define DEFINE_PROCESS_TYPE(type)                                            \
 379	static void process_##type##_type(struct state *state,               \
 380					  struct die *cache, Dwarf_Die *die) \
 381	{                                                                    \
 382		__process_type(state, cache, die, #type "_type");            \
 383	}
 384
 385DEFINE_PROCESS_TYPE(atomic)
 386DEFINE_PROCESS_TYPE(const)
 387DEFINE_PROCESS_TYPE(immutable)
 388DEFINE_PROCESS_TYPE(packed)
 389DEFINE_PROCESS_TYPE(pointer)
 390DEFINE_PROCESS_TYPE(reference)
 391DEFINE_PROCESS_TYPE(restrict)
 392DEFINE_PROCESS_TYPE(rvalue_reference)
 393DEFINE_PROCESS_TYPE(shared)
 394DEFINE_PROCESS_TYPE(template_type_parameter)
 395DEFINE_PROCESS_TYPE(volatile)
 396DEFINE_PROCESS_TYPE(typedef)
 397
 398static void process_subrange_type(struct state *state, struct die *cache,
 399				  Dwarf_Die *die)
 400{
 401	Dwarf_Word count = 0;
 402
 403	if (get_udata_attr(die, DW_AT_count, &count))
 404		process_fmt(cache, "[%" PRIu64 "]", count);
 405	else if (get_udata_attr(die, DW_AT_upper_bound, &count))
 406		process_fmt(cache, "[%" PRIu64 "]", count + 1);
 407	else
 408		process(cache, "[]");
 409}
 410
 411static void process_array_type(struct state *state, struct die *cache,
 412			       Dwarf_Die *die)
 413{
 414	process(cache, "array_type");
 415	/* Array size */
 416	check(process_die_container(state, cache, die, process_type,
 417				    match_subrange_type));
 418	process(cache, " {");
 419	process_linebreak(cache, 1);
 420	process_type_attr(state, cache, die);
 421	process_linebreak(cache, -1);
 422	process(cache, "}");
 423}
 424
 425static void __process_subroutine_type(struct state *state, struct die *cache,
 426				      Dwarf_Die *die, const char *type)
 427{
 428	process(cache, type);
 429	process(cache, " (");
 430	process_linebreak(cache, 1);
 431	/* Parameters */
 432	check(process_die_container(state, cache, die, process_type,
 433				    match_formal_parameter_type));
 434	process_linebreak(cache, -1);
 435	process(cache, ")");
 436	process_linebreak(cache, 0);
 437	/* Return type */
 438	process(cache, "-> ");
 439	process_type_attr(state, cache, die);
 440}
 441
 442static void process_subroutine_type(struct state *state, struct die *cache,
 443				    Dwarf_Die *die)
 444{
 445	__process_subroutine_type(state, cache, die, "subroutine_type");
 446}
 447
 448static void process_variant_type(struct state *state, struct die *cache,
 449				 Dwarf_Die *die)
 450{
 451	process_list_comma(state, cache);
 452	process(cache, "variant {");
 453	process_linebreak(cache, 1);
 454	check(process_die_container(state, cache, die, process_type,
 455				    match_member_type));
 456	process_linebreak(cache, -1);
 457	process(cache, "}");
 458	process_discr_value_attr(cache, die);
 459}
 460
 461static void process_variant_part_type(struct state *state, struct die *cache,
 462				      Dwarf_Die *die)
 463{
 464	process_list_comma(state, cache);
 465	process(cache, "variant_part {");
 466	process_linebreak(cache, 1);
 467	check(process_die_container(state, cache, die, process_type,
 468				    match_all));
 469	process_linebreak(cache, -1);
 470	process(cache, "}");
 471}
 472
 473static int get_kabi_status(Dwarf_Die *die, const char **suffix)
 474{
 475	const char *name = get_name_attr(die);
 476
 477	if (suffix)
 478		*suffix = NULL;
 479
 480	if (is_kabi_prefix(name)) {
 481		name += KABI_PREFIX_LEN;
 482
 483		if (!strncmp(name, KABI_RESERVED_PREFIX,
 484			     KABI_RESERVED_PREFIX_LEN))
 485			return KABI_RESERVED;
 486		if (!strncmp(name, KABI_IGNORED_PREFIX,
 487			     KABI_IGNORED_PREFIX_LEN))
 488			return KABI_IGNORED;
 489
 490		if (!strncmp(name, KABI_RENAMED_PREFIX,
 491			     KABI_RENAMED_PREFIX_LEN)) {
 492			if (suffix) {
 493				name += KABI_RENAMED_PREFIX_LEN;
 494				*suffix = name;
 495			}
 496			return KABI_RESERVED;
 497		}
 498	}
 499
 500	return KABI_NORMAL;
 501}
 502
 503static int check_struct_member_kabi_status(struct state *state,
 504					   struct die *__unused, Dwarf_Die *die)
 505{
 506	int res;
 507
 508	assert(dwarf_tag(die) == DW_TAG_member_type);
 509
 510	/*
 511	 * If the union member is a struct, expect the __kabi field to
 512	 * be the first member of the structure, i.e..:
 513	 *
 514	 * union {
 515	 * 	type new_member;
 516	 * 	struct {
 517	 * 		type __kabi_field;
 518	 * 	}
 519	 * };
 520	 */
 521	res = get_kabi_status(die, &state->kabi.orig_name);
 522
 523	if (res == KABI_RESERVED &&
 524	    !get_ref_die_attr(die, DW_AT_type, &state->kabi.placeholder))
 525		error("structure member missing a type?");
 526
 527	return res;
 528}
 529
 530static int check_union_member_kabi_status(struct state *state,
 531					  struct die *__unused, Dwarf_Die *die)
 532{
 533	Dwarf_Die type;
 534	int res;
 535
 536	assert(dwarf_tag(die) == DW_TAG_member_type);
 537
 538	if (!get_ref_die_attr(die, DW_AT_type, &type))
 539		error("union member missing a type?");
 540
 541	/*
 542	 * We expect a union with two members. Check if either of them
 543	 * has a __kabi name prefix, i.e.:
 544	 *
 545	 * union {
 546	 * 	...
 547	 * 	type memberN; // <- type, N = {0,1}
 548	 *	...
 549	 * };
 550	 *
 551	 * The member can also be a structure type, in which case we'll
 552	 * check the first structure member.
 553	 *
 554	 * In any case, stop processing after we've seen two members.
 555	 */
 556	res = get_kabi_status(die, &state->kabi.orig_name);
 557
 558	if (res == KABI_RESERVED)
 559		state->kabi.placeholder = type;
 560	if (res != KABI_NORMAL)
 561		return res;
 562
 563	if (dwarf_tag(&type) == DW_TAG_structure_type)
 564		res = checkp(process_die_container(
 565			state, NULL, &type, check_struct_member_kabi_status,
 566			match_member_type));
 567
 568	if (res <= KABI_NORMAL && ++state->kabi.members < 2)
 569		return 0; /* Continue */
 570
 571	return res;
 572}
 573
 574static int get_union_kabi_status(Dwarf_Die *die, Dwarf_Die *placeholder,
 575				 const char **orig_name)
 576{
 577	struct state state;
 578	int res;
 579
 580	if (!stable)
 581		return KABI_NORMAL;
 582
 583	/*
 584	 * To maintain a stable kABI, distributions may choose to reserve
 585	 * space in structs for later use by adding placeholder members,
 586	 * for example:
 587	 *
 588	 * struct s {
 589	 * 	u32 a;
 590	 *	// an 8-byte placeholder for future use
 591	 * 	u64 __kabi_reserved_0;
 592	 * };
 593	 *
 594	 * When the reserved member is taken into use, the type change
 595	 * would normally cause the symbol version to change as well, but
 596	 * if the replacement uses the following convention, gendwarfksyms
 597	 * continues to use the placeholder type for versioning instead,
 598	 * thus maintaining the same symbol version:
 599	 *
 600	 * struct s {
 601	 * 	u32 a;
 602	 *	union {
 603	 * 		// placeholder replaced with a new member `b`
 604	 * 		struct t b;
 605	 * 		struct {
 606	 * 			// the placeholder type that is still
 607	 *			// used for versioning
 608	 * 			u64 __kabi_reserved_0;
 609	 * 		};
 610	 * 	};
 611	 * };
 612	 *
 613	 * I.e., as long as the replaced member is in a union, and the
 614	 * placeholder has a __kabi_reserved name prefix, we'll continue
 615	 * to use the placeholder type (here u64) for version calculation
 616	 * instead of the union type.
 617	 *
 618	 * It's also possible to ignore new members from versioning if
 619	 * they've been added to alignment holes, for example, by
 620	 * including them in a union with another member that uses the
 621	 * __kabi_ignored name prefix:
 622	 *
 623	 * struct s {
 624	 * 	u32 a;
 625	 *	// an alignment hole is used to add `n`
 626	 * 	union {
 627	 * 		u32 n;
 628	 *		// hide the entire union member from versioning
 629	 * 		u8 __kabi_ignored_0;
 630	 * 	};
 631	 * 	u64 b;
 632	 * };
 633	 *
 634	 * Note that the user of this feature is responsible for ensuring
 635	 * that the structure actually remains ABI compatible.
 636	 */
 637	memset(&state.kabi, 0, sizeof(state.kabi));
 638
 639	res = checkp(process_die_container(&state, NULL, die,
 640					   check_union_member_kabi_status,
 641					   match_member_type));
 642
 643	if (res == KABI_RESERVED) {
 644		if (placeholder)
 645			*placeholder = state.kabi.placeholder;
 646		if (orig_name)
 647			*orig_name = state.kabi.orig_name;
 648	}
 649
 650	return res;
 651}
 652
 653static bool is_kabi_ignored(Dwarf_Die *die)
 654{
 655	Dwarf_Die type;
 656
 657	if (!stable)
 658		return false;
 659
 660	if (!get_ref_die_attr(die, DW_AT_type, &type))
 661		error("member missing a type?");
 662
 663	return dwarf_tag(&type) == DW_TAG_union_type &&
 664	       checkp(get_union_kabi_status(&type, NULL, NULL)) == KABI_IGNORED;
 665}
 666
 667static int ___process_structure_type(struct state *state, struct die *cache,
 668				     Dwarf_Die *die)
 669{
 670	switch (dwarf_tag(die)) {
 671	case DW_TAG_member:
 672		if (is_kabi_ignored(die))
 673			return 0;
 674		return check(process_type(state, cache, die));
 675	case DW_TAG_variant_part:
 676		return check(process_type(state, cache, die));
 677	case DW_TAG_class_type:
 678	case DW_TAG_enumeration_type:
 679	case DW_TAG_structure_type:
 680	case DW_TAG_template_type_parameter:
 681	case DW_TAG_union_type:
 682	case DW_TAG_subprogram:
 683		/* Skip non-member types, including member functions */
 684		return 0;
 685	default:
 686		error("unexpected structure_type child: %x", dwarf_tag(die));
 687	}
 688}
 689
 690static void __process_structure_type(struct state *state, struct die *cache,
 691				     Dwarf_Die *die, const char *type,
 692				     die_callback_t process_func,
 693				     die_match_callback_t match_func)
 694{
 695	bool expand;
 696
 697	process(cache, type);
 698	process_fqn(cache, die);
 699	process(cache, " {");
 700	process_linebreak(cache, 1);
 701
 702	expand = state->expand.expand && is_kabi_definition(cache, die);
 703
 704	if (expand) {
 705		state->expand.current_fqn = cache->fqn;
 706		check(process_die_container(state, cache, die, process_func,
 707					    match_func));
 708	}
 709
 710	process_linebreak(cache, -1);
 711	process(cache, "}");
 712
 713	if (expand) {
 714		process_byte_size_attr(cache, die);
 715		process_alignment_attr(cache, die);
 716	}
 717}
 718
 719#define DEFINE_PROCESS_STRUCTURE_TYPE(structure)                        \
 720	static void process_##structure##_type(                         \
 721		struct state *state, struct die *cache, Dwarf_Die *die) \
 722	{                                                               \
 723		__process_structure_type(state, cache, die,             \
 724					 #structure "_type",            \
 725					 ___process_structure_type,     \
 726					 match_all);                    \
 727	}
 728
 729DEFINE_PROCESS_STRUCTURE_TYPE(class)
 730DEFINE_PROCESS_STRUCTURE_TYPE(structure)
 731
 732static void process_union_type(struct state *state, struct die *cache,
 733			       Dwarf_Die *die)
 734{
 735	Dwarf_Die placeholder;
 736
 737	int res = checkp(get_union_kabi_status(die, &placeholder,
 738					       &state->kabi.orig_name));
 739
 740	if (res == KABI_RESERVED)
 741		check(process_type(state, cache, &placeholder));
 742	if (res > KABI_NORMAL)
 743		return;
 744
 745	__process_structure_type(state, cache, die, "union_type",
 746				 ___process_structure_type, match_all);
 747}
 748
 749static void process_enumerator_type(struct state *state, struct die *cache,
 750				    Dwarf_Die *die)
 751{
 752	bool overridden = false;
 753	unsigned long override;
 754	Dwarf_Word value;
 755
 756	if (stable) {
 757		/* Get the fqn before we process anything */
 758		update_fqn(cache, die);
 759
 760		if (kabi_is_enumerator_ignored(state->expand.current_fqn,
 761					       cache->fqn))
 762			return;
 763
 764		overridden = kabi_get_enumerator_value(
 765			state->expand.current_fqn, cache->fqn, &override);
 766		value = override;
 767	}
 768
 769	process_list_comma(state, cache);
 770	process(cache, "enumerator");
 771	process_fqn(cache, die);
 772
 773	if (overridden || get_udata_attr(die, DW_AT_const_value, &value)) {
 774		process(cache, " = ");
 775		process_fmt(cache, "%" PRIu64, value);
 776	}
 777}
 778
 779static void process_enumeration_type(struct state *state, struct die *cache,
 780				     Dwarf_Die *die)
 781{
 782	__process_structure_type(state, cache, die, "enumeration_type",
 783				 process_type, match_enumerator_type);
 784}
 785
 786static void process_base_type(struct state *state, struct die *cache,
 787			      Dwarf_Die *die)
 788{
 789	process(cache, "base_type");
 790	process_fqn(cache, die);
 791	process_byte_size_attr(cache, die);
 792	process_encoding_attr(cache, die);
 793	process_alignment_attr(cache, die);
 794}
 795
 796static void process_unspecified_type(struct state *state, struct die *cache,
 797				     Dwarf_Die *die)
 798{
 799	/*
 800	 * These can be emitted for stand-alone assembly code, which means we
 801	 * might run into them in vmlinux.o.
 802	 */
 803	process(cache, "unspecified_type");
 804}
 805
 806static void process_cached(struct state *state, struct die *cache,
 807			   Dwarf_Die *die)
 808{
 809	struct die_fragment *df;
 810	Dwarf_Die child;
 811
 812	list_for_each_entry(df, &cache->fragments, list) {
 813		switch (df->type) {
 814		case FRAGMENT_STRING:
 815			die_debug_b("cache %p STRING '%s'", cache,
 816				    df->data.str);
 817			process(NULL, df->data.str);
 818			break;
 819		case FRAGMENT_LINEBREAK:
 820			process_linebreak(NULL, df->data.linebreak);
 821			break;
 822		case FRAGMENT_DIE:
 823			if (!dwarf_die_addr_die(dwarf_cu_getdwarf(die->cu),
 824						(void *)df->data.addr, &child))
 825				error("dwarf_die_addr_die failed");
 826			die_debug_b("cache %p DIE addr %" PRIxPTR " tag %x",
 827				    cache, df->data.addr, dwarf_tag(&child));
 828			check(process_type(state, NULL, &child));
 829			break;
 830		default:
 831			error("empty die_fragment");
 832		}
 833	}
 834}
 835
 836static void state_init(struct state *state)
 837{
 838	state->expand.expand = true;
 839	state->expand.current_fqn = NULL;
 840	cache_init(&state->expansion_cache);
 841}
 842
 843static void expansion_state_restore(struct expansion_state *state,
 844				    struct expansion_state *saved)
 845{
 846	state->expand = saved->expand;
 847	state->current_fqn = saved->current_fqn;
 848}
 849
 850static void expansion_state_save(struct expansion_state *state,
 851				 struct expansion_state *saved)
 852{
 853	expansion_state_restore(saved, state);
 854}
 855
 856static bool is_expanded_type(int tag)
 857{
 858	return tag == DW_TAG_class_type || tag == DW_TAG_structure_type ||
 859	       tag == DW_TAG_union_type || tag == DW_TAG_enumeration_type;
 860}
 861
 862#define PROCESS_TYPE(type)                                \
 863	case DW_TAG_##type##_type:                        \
 864		process_##type##_type(state, cache, die); \
 865		break;
 866
 867static int process_type(struct state *state, struct die *parent, Dwarf_Die *die)
 868{
 869	enum die_state want_state = DIE_COMPLETE;
 870	struct die *cache;
 871	struct expansion_state saved;
 872	int tag = dwarf_tag(die);
 873
 874	expansion_state_save(&state->expand, &saved);
 875
 876	/*
 877	 * Structures and enumeration types are expanded only once per
 878	 * exported symbol. This is sufficient for detecting ABI changes
 879	 * within the structure.
 880	 */
 881	if (is_expanded_type(tag)) {
 882		if (cache_was_expanded(&state->expansion_cache, die->addr))
 883			state->expand.expand = false;
 884
 885		if (state->expand.expand)
 886			cache_mark_expanded(&state->expansion_cache, die->addr);
 887		else
 888			want_state = DIE_UNEXPANDED;
 889	}
 890
 891	/*
 892	 * If we have want_state already cached, use it instead of walking
 893	 * through DWARF.
 894	 */
 895	cache = die_map_get(die, want_state);
 896
 897	if (cache->state == want_state) {
 898		die_debug_g("cached addr %p tag %x -- %s", die->addr, tag,
 899			    die_state_name(cache->state));
 900
 901		process_cached(state, cache, die);
 902		die_map_add_die(parent, cache);
 903
 904		expansion_state_restore(&state->expand, &saved);
 905		return 0;
 906	}
 907
 908	die_debug_g("addr %p tag %x -- %s -> %s", die->addr, tag,
 909		    die_state_name(cache->state), die_state_name(want_state));
 910
 911	switch (tag) {
 912	/* Type modifiers */
 913	PROCESS_TYPE(atomic)
 914	PROCESS_TYPE(const)
 915	PROCESS_TYPE(immutable)
 916	PROCESS_TYPE(packed)
 917	PROCESS_TYPE(pointer)
 918	PROCESS_TYPE(reference)
 919	PROCESS_TYPE(restrict)
 920	PROCESS_TYPE(rvalue_reference)
 921	PROCESS_TYPE(shared)
 922	PROCESS_TYPE(volatile)
 923	/* Container types */
 924	PROCESS_TYPE(class)
 925	PROCESS_TYPE(structure)
 926	PROCESS_TYPE(union)
 927	PROCESS_TYPE(enumeration)
 928	/* Subtypes */
 929	PROCESS_TYPE(enumerator)
 930	PROCESS_TYPE(formal_parameter)
 931	PROCESS_TYPE(member)
 932	PROCESS_TYPE(subrange)
 933	PROCESS_TYPE(template_type_parameter)
 934	PROCESS_TYPE(variant)
 935	PROCESS_TYPE(variant_part)
 936	/* Other types */
 937	PROCESS_TYPE(array)
 938	PROCESS_TYPE(base)
 939	PROCESS_TYPE(subroutine)
 940	PROCESS_TYPE(typedef)
 941	PROCESS_TYPE(unspecified)
 942	default:
 943		error("unexpected type: %x", tag);
 944	}
 945
 946	die_debug_r("parent %p cache %p die addr %p tag %x", parent, cache,
 947		    die->addr, tag);
 948
 949	/* Update cache state and append to the parent (if any) */
 950	cache->tag = tag;
 951	cache->state = want_state;
 952	die_map_add_die(parent, cache);
 953
 954	expansion_state_restore(&state->expand, &saved);
 955	return 0;
 956}
 957
 958/*
 959 * Exported symbol processing
 960 */
 961static struct die *get_symbol_cache(struct state *state, Dwarf_Die *die)
 962{
 963	struct die *cache;
 964
 965	cache = die_map_get(die, DIE_SYMBOL);
 966
 967	if (cache->state != DIE_INCOMPLETE)
 968		return NULL; /* We already processed a symbol for this DIE */
 969
 970	cache->tag = dwarf_tag(die);
 971	return cache;
 972}
 973
 974static void process_symbol(struct state *state, Dwarf_Die *die,
 975			   die_callback_t process_func)
 976{
 977	struct die *cache;
 978
 979	symbol_set_die(state->sym, die);
 980
 981	cache = get_symbol_cache(state, die);
 982	if (!cache)
 983		return;
 984
 985	debug("%s", state->sym->name);
 986	check(process_func(state, cache, die));
 987	cache->state = DIE_SYMBOL;
 988	if (dump_dies)
 989		fputs("\n", stderr);
 990}
 991
 992static int __process_subprogram(struct state *state, struct die *cache,
 993				Dwarf_Die *die)
 994{
 995	__process_subroutine_type(state, cache, die, "subprogram");
 996	return 0;
 997}
 998
 999static void process_subprogram(struct state *state, Dwarf_Die *die)
1000{
1001	process_symbol(state, die, __process_subprogram);
1002}
1003
1004static int __process_variable(struct state *state, struct die *cache,
1005			      Dwarf_Die *die)
1006{
1007	process(cache, "variable ");
1008	process_type_attr(state, cache, die);
1009	return 0;
1010}
1011
1012static void process_variable(struct state *state, Dwarf_Die *die)
1013{
1014	process_symbol(state, die, __process_variable);
1015}
1016
1017static void save_symbol_ptr(struct state *state)
1018{
1019	Dwarf_Die ptr_type;
1020	Dwarf_Die type;
1021
1022	if (!get_ref_die_attr(&state->die, DW_AT_type, &ptr_type) ||
1023	    dwarf_tag(&ptr_type) != DW_TAG_pointer_type)
1024		error("%s must be a pointer type!",
1025		      get_symbol_name(&state->die));
1026
1027	if (!get_ref_die_attr(&ptr_type, DW_AT_type, &type))
1028		error("%s pointer missing a type attribute?",
1029		      get_symbol_name(&state->die));
1030
1031	/*
1032	 * Save the symbol pointer DIE in case the actual symbol is
1033	 * missing from the DWARF. Clang, for example, intentionally
1034	 * omits external symbols from the debugging information.
1035	 */
1036	if (dwarf_tag(&type) == DW_TAG_subroutine_type)
1037		symbol_set_ptr(state->sym, &type);
1038	else
1039		symbol_set_ptr(state->sym, &ptr_type);
1040}
1041
1042static int process_exported_symbols(struct state *unused, struct die *cache,
1043				    Dwarf_Die *die)
1044{
1045	int tag = dwarf_tag(die);
1046
1047	switch (tag) {
1048	/* Possible containers of exported symbols */
1049	case DW_TAG_namespace:
1050	case DW_TAG_class_type:
1051	case DW_TAG_structure_type:
1052		return check(process_die_container(
1053			NULL, cache, die, process_exported_symbols, match_all));
1054
1055	/* Possible exported symbols */
1056	case DW_TAG_subprogram:
1057	case DW_TAG_variable: {
1058		struct state state;
1059
1060		if (!match_export_symbol(&state, die))
1061			return 0;
1062
1063		state_init(&state);
1064
1065		if (is_symbol_ptr(get_symbol_name(&state.die)))
1066			save_symbol_ptr(&state);
1067		else if (tag == DW_TAG_subprogram)
1068			process_subprogram(&state, &state.die);
1069		else
1070			process_variable(&state, &state.die);
1071
1072		cache_free(&state.expansion_cache);
1073		return 0;
1074	}
1075	default:
1076		return 0;
1077	}
1078}
1079
1080static void process_symbol_ptr(struct symbol *sym, void *arg)
1081{
1082	struct state state;
1083	Dwarf *dwarf = arg;
1084
1085	if (sym->state != SYMBOL_UNPROCESSED || !sym->ptr_die_addr)
1086		return;
1087
1088	debug("%s", sym->name);
1089	state_init(&state);
1090	state.sym = sym;
1091
1092	if (!dwarf_die_addr_die(dwarf, (void *)sym->ptr_die_addr, &state.die))
1093		error("dwarf_die_addr_die failed for symbol ptr: '%s'",
1094		      sym->name);
1095
1096	if (dwarf_tag(&state.die) == DW_TAG_subroutine_type)
1097		process_subprogram(&state, &state.die);
1098	else
1099		process_variable(&state, &state.die);
1100
1101	cache_free(&state.expansion_cache);
1102}
1103
1104static int resolve_fqns(struct state *parent, struct die *unused,
1105			Dwarf_Die *die)
1106{
1107	struct state state;
1108	struct die *cache;
1109	const char *name;
1110	bool use_prefix;
1111	char *prefix = NULL;
1112	char *fqn = "";
1113	int tag;
1114
1115	if (!__die_map_get((uintptr_t)die->addr, DIE_FQN, &cache))
1116		return 0;
1117
1118	tag = dwarf_tag(die);
1119
1120	/*
1121	 * Only namespaces and structures need to pass a prefix to the next
1122	 * scope.
1123	 */
1124	use_prefix = tag == DW_TAG_namespace || tag == DW_TAG_class_type ||
1125		     tag == DW_TAG_structure_type;
1126
1127	state.expand.current_fqn = NULL;
1128	name = get_name_attr(die);
1129
1130	if (parent && parent->expand.current_fqn && (use_prefix || name)) {
1131		/*
1132		 * The fqn for the current DIE, and if needed, a prefix for the
1133		 * next scope.
1134		 */
1135		if (asprintf(&prefix, "%s::%s", parent->expand.current_fqn,
1136			     name ? name : "<anonymous>") < 0)
1137			error("asprintf failed");
1138
1139		if (use_prefix)
1140			state.expand.current_fqn = prefix;
1141
1142		/*
1143		 * Use fqn only if the DIE has a name. Otherwise fqn will
1144		 * remain empty.
1145		 */
1146		if (name) {
1147			fqn = prefix;
1148			/* prefix will be freed by die_map. */
1149			prefix = NULL;
1150		}
1151	} else if (name) {
1152		/* No prefix from the previous scope. Use only the name. */
1153		fqn = xstrdup(name);
1154
1155		if (use_prefix)
1156			state.expand.current_fqn = fqn;
1157	}
1158
1159	/* If the DIE has a non-empty name, cache it. */
1160	if (*fqn) {
1161		cache = die_map_get(die, DIE_FQN);
1162		/* Move ownership of fqn to die_map. */
1163		cache->fqn = fqn;
1164		cache->state = DIE_FQN;
1165	}
1166
1167	check(process_die_container(&state, NULL, die, resolve_fqns,
1168				    match_all));
1169
1170	free(prefix);
1171	return 0;
1172}
1173
1174void process_cu(Dwarf_Die *cudie)
1175{
1176	check(process_die_container(NULL, NULL, cudie, resolve_fqns,
1177				    match_all));
1178
1179	check(process_die_container(NULL, NULL, cudie, process_exported_symbols,
1180				    match_all));
1181
1182	symbol_for_each(process_symbol_ptr, dwarf_cu_getdwarf(cudie->cu));
1183
1184	cache_free(&srcfile_cache);
1185}
~/ Read-only file preview.