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