Skip to content

Commit 3669dfd

Browse files
committed
gendwarfksyms: Expand structure types
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 <unnamed> { 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 <samitolvanen@google.com>
1 parent 226d748 commit 3669dfd

File tree

2 files changed

+132
-2
lines changed

2 files changed

+132
-2
lines changed

scripts/gendwarfksyms/dwarf.c

Lines changed: 127 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,13 @@ static int process_fqn(struct state *state, struct die *cache, Dwarf_Die *die)
206206
return 0; \
207207
}
208208

209+
DEFINE_PROCESS_UDATA_ATTRIBUTE(accessibility)
209210
DEFINE_PROCESS_UDATA_ATTRIBUTE(alignment)
211+
DEFINE_PROCESS_UDATA_ATTRIBUTE(bit_size)
210212
DEFINE_PROCESS_UDATA_ATTRIBUTE(byte_size)
211213
DEFINE_PROCESS_UDATA_ATTRIBUTE(encoding)
214+
DEFINE_PROCESS_UDATA_ATTRIBUTE(data_bit_offset)
215+
DEFINE_PROCESS_UDATA_ATTRIBUTE(data_member_location)
212216

213217
/* Match functions -- die_match_callback_t */
214218
#define DEFINE_MATCH(type) \
@@ -217,8 +221,11 @@ DEFINE_PROCESS_UDATA_ATTRIBUTE(encoding)
217221
return dwarf_tag(die) == DW_TAG_##type##_type; \
218222
}
219223

224+
DEFINE_MATCH(enumerator)
220225
DEFINE_MATCH(formal_parameter)
226+
DEFINE_MATCH(member)
221227
DEFINE_MATCH(subrange)
228+
DEFINE_MATCH(variant)
222229

223230
bool match_all(Dwarf_Die *die)
224231
{
@@ -263,6 +270,10 @@ static int __process_list_type(struct state *state, struct die *cache,
263270
{
264271
check(process(state, cache, type));
265272
check(process_type_attr(state, cache, die));
273+
check(process_accessibility_attr(state, cache, die));
274+
check(process_bit_size_attr(state, cache, die));
275+
check(process_data_bit_offset_attr(state, cache, die));
276+
check(process_data_member_location_attr(state, cache, die));
266277
check(process(state, cache, ","));
267278
return check(process_linebreak(cache, 0));
268279
}
@@ -275,6 +286,7 @@ static int __process_list_type(struct state *state, struct die *cache,
275286
}
276287

277288
DEFINE_PROCESS_LIST_TYPE(formal_parameter)
289+
DEFINE_PROCESS_LIST_TYPE(member)
278290

279291
/* Container types with DW_AT_type */
280292
static int __process_type(struct state *state, struct die *cache,
@@ -307,6 +319,7 @@ DEFINE_PROCESS_TYPE(reference)
307319
DEFINE_PROCESS_TYPE(restrict)
308320
DEFINE_PROCESS_TYPE(rvalue_reference)
309321
DEFINE_PROCESS_TYPE(shared)
322+
DEFINE_PROCESS_TYPE(template_type_parameter)
310323
DEFINE_PROCESS_TYPE(volatile)
311324
DEFINE_PROCESS_TYPE(typedef)
312325

@@ -361,6 +374,108 @@ static int process_subroutine_type(struct state *state, struct die *cache,
361374
return check(__process_subroutine_type(state, cache, die,
362375
"subroutine_type"));
363376
}
377+
static int process_variant_type(struct state *state, struct die *cache,
378+
Dwarf_Die *die)
379+
{
380+
return check(process_die_container(state, cache, die, process_type,
381+
match_member_type));
382+
}
383+
384+
static int process_variant_part_type(struct state *state, struct die *cache,
385+
Dwarf_Die *die)
386+
{
387+
check(process(state, cache, "variant_part {"));
388+
check(process_linebreak(cache, 1));
389+
check(process_die_container(state, cache, die, process_type,
390+
match_variant_type));
391+
check(process_linebreak(cache, -1));
392+
check(process(state, cache, "},"));
393+
return check(process_linebreak(cache, 0));
394+
}
395+
396+
static int ___process_structure_type(struct state *state, struct die *cache,
397+
Dwarf_Die *die)
398+
{
399+
switch (dwarf_tag(die)) {
400+
case DW_TAG_member:
401+
case DW_TAG_variant_part:
402+
return check(process_type(state, cache, die));
403+
case DW_TAG_class_type:
404+
case DW_TAG_enumeration_type:
405+
case DW_TAG_structure_type:
406+
case DW_TAG_template_type_parameter:
407+
case DW_TAG_union_type:
408+
check(process_type(state, cache, die));
409+
check(process(state, cache, ","));
410+
return check(process_linebreak(cache, 0));
411+
case DW_TAG_subprogram:
412+
return 0; /* Skip member functions */
413+
default:
414+
error("unexpected structure_type child: %x", dwarf_tag(die));
415+
return -1;
416+
}
417+
}
418+
419+
static int __process_structure_type(struct state *state, struct die *cache,
420+
Dwarf_Die *die, const char *type,
421+
die_callback_t process_func,
422+
die_match_callback_t match_func)
423+
{
424+
check(process(state, cache, type));
425+
check(process_fqn(state, cache, die));
426+
check(process(state, cache, " {"));
427+
check(process_linebreak(cache, 1));
428+
429+
check(process_die_container(state, cache, die, process_func,
430+
match_func));
431+
432+
check(process_linebreak(cache, -1));
433+
check(process(state, cache, "}"));
434+
435+
check(process_byte_size_attr(state, cache, die));
436+
check(process_alignment_attr(state, cache, die));
437+
438+
return 0;
439+
}
440+
441+
#define DEFINE_PROCESS_STRUCTURE_TYPE(structure) \
442+
static int process_##structure##_type( \
443+
struct state *state, struct die *cache, Dwarf_Die *die) \
444+
{ \
445+
return check(__process_structure_type( \
446+
state, cache, die, #structure "_type ", \
447+
___process_structure_type, match_all)); \
448+
}
449+
450+
DEFINE_PROCESS_STRUCTURE_TYPE(class)
451+
DEFINE_PROCESS_STRUCTURE_TYPE(structure)
452+
DEFINE_PROCESS_STRUCTURE_TYPE(union)
453+
454+
static int process_enumerator_type(struct state *state, struct die *cache,
455+
Dwarf_Die *die)
456+
{
457+
Dwarf_Word value;
458+
459+
check(process(state, cache, "enumerator "));
460+
check(process_fqn(state, cache, die));
461+
462+
if (get_udata_attr(die, DW_AT_const_value, &value)) {
463+
check(process(state, cache, " = "));
464+
check(process_fmt(state, cache, "%" PRIu64, value));
465+
}
466+
467+
check(process(state, cache, ","));
468+
return check(process_linebreak(cache, 0));
469+
}
470+
471+
static int process_enumeration_type(struct state *state, struct die *cache,
472+
Dwarf_Die *die)
473+
{
474+
return check(__process_structure_type(state, cache, die,
475+
"enumeration_type ", process_type,
476+
match_enumerator_type));
477+
}
478+
364479
static int process_base_type(struct state *state, struct die *cache,
365480
Dwarf_Die *die)
366481
{
@@ -438,17 +553,27 @@ static int process_type(struct state *state, struct die *parent, Dwarf_Die *die)
438553
PROCESS_TYPE(rvalue_reference)
439554
PROCESS_TYPE(shared)
440555
PROCESS_TYPE(volatile)
556+
/* Container types */
557+
PROCESS_TYPE(class)
558+
PROCESS_TYPE(structure)
559+
PROCESS_TYPE(union)
560+
PROCESS_TYPE(enumeration)
441561
/* Subtypes */
562+
PROCESS_TYPE(enumerator)
442563
PROCESS_TYPE(formal_parameter)
564+
PROCESS_TYPE(member)
443565
PROCESS_TYPE(subrange)
566+
PROCESS_TYPE(template_type_parameter)
567+
PROCESS_TYPE(variant)
568+
PROCESS_TYPE(variant_part)
444569
/* Other types */
445570
PROCESS_TYPE(array)
446571
PROCESS_TYPE(base)
447572
PROCESS_TYPE(subroutine)
448573
PROCESS_TYPE(typedef)
449574
default:
450-
debug("unimplemented type: %x", tag);
451-
break;
575+
error("unexpected type: %x", tag);
576+
return -1;
452577
}
453578

454579
/* Update cache state and append to the parent (if any) */

scripts/gendwarfksyms/gendwarfksyms.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,13 @@ extern bool debug;
5959
#define checkp(expr) __check(expr, __res < 0, __res)
6060

6161
/* Consistent aliases (DW_TAG_<type>_type) for DWARF tags */
62+
#define DW_TAG_enumerator_type DW_TAG_enumerator
6263
#define DW_TAG_formal_parameter_type DW_TAG_formal_parameter
64+
#define DW_TAG_member_type DW_TAG_member
65+
#define DW_TAG_template_type_parameter_type DW_TAG_template_type_parameter
6366
#define DW_TAG_typedef_type DW_TAG_typedef
67+
#define DW_TAG_variant_part_type DW_TAG_variant_part
68+
#define DW_TAG_variant_type DW_TAG_variant
6469

6570
/*
6671
* symbols.c

0 commit comments

Comments
 (0)