1 /* Copyright Dima Kogan <dima@secretsauce.net>
3 * This program is free software; you can redistribute it and/or modify it under
4 * the terms of version 2 of the GNU General Public License as published by the
5 * Free Software Foundation.
9 #include <elfutils/libdwfl.h>
16 #include "prototype.h"
21 #include "lens_enum.h"
29 //#define DUMP_PROTOTYPES
32 #define complain( die, format, ... ) \
33 fprintf(stderr, "%s() die '%s' @ 0x%lx: " format "\n", \
34 __func__, dwarf_diename(die), dwarf_dieoffset(die), \
37 #define complain( die, format, ... )
40 // A map from DIE addresses (Dwarf_Off) to type structures (struct
41 // arg_type_info*). This is created and filled in at the start of each import,
42 // and deleted when the import is complete
43 static struct dict type_hash;
46 static bool get_type(struct arg_type_info** info, Dwarf_Die* type_die);
50 static bool _dump_dwarf_tree(Dwarf_Die* die, int indent)
53 fprintf(stderr, "%*sprocessing unit: 0x%02x/'%s'\n", indent*4, "",
54 dwarf_tag(die), dwarf_diename(die));
57 if (dwarf_child(die, &child) == 0) {
58 if (!_dump_dwarf_tree(&child, indent+1))
62 int res = dwarf_siblingof(die, die);
63 if (res == 0 ) continue; // sibling exists
64 if (res < 0 ) return false; // error
65 break; // no sibling exists
71 static bool dump_dwarf_tree(Dwarf_Die* die)
73 return _dump_dwarf_tree( die, 0 );
77 #ifdef DUMP_PROTOTYPES
78 static bool _dump_ltrace_tree(const struct arg_type_info* info, int indent)
81 fprintf(stderr, "%*s%p ...\n", indent*4, "", (void*)info);
86 fprintf(stderr, "%*s%p NULL\n", indent*4, "", (void*)info);
92 fprintf(stderr, "%*s%p void\n", indent*4, "", (void*)info);
104 fprintf(stderr, "%*s%p base\n", indent*4, "", (void*)info);
108 fprintf(stderr, "%*s%p array. elements not printed\n", indent*4, "",
112 case ARGTYPE_POINTER:
113 fprintf(stderr, "%*s%p pointer to...\n", indent*4, "", (void*)info);
114 _dump_ltrace_tree( info->u.ptr_info.info, indent+1 );
118 fprintf(stderr, "%*s%p struct...\n", indent*4, "", (void*)info);
121 struct arg_type_info *info;
123 }* elements = (struct struct_field*)info->u.entries.data;
125 for(i=0; i<info->u.entries.size; i++)
126 _dump_ltrace_tree( elements[i].info, indent+1 );
130 fprintf(stderr, "%*s%p unknown type\n", indent*4, "", (void*)info);
137 static bool dump_ltrace_tree(const struct arg_type_info* info)
139 return _dump_ltrace_tree( info, 0 );
144 // pulls a numerical value out of a particular attribute in a die. Returns true
145 // if successful. The result is returned in *result. Note that this is cast to
146 // (uint64_t), regardless of the actual type of the input
147 static bool get_die_numeric(uint64_t* result,
148 Dwarf_Die *die, unsigned int attr_name)
150 Dwarf_Attribute attr ;
159 if (dwarf_attr(die, attr_name, &attr) == NULL)
162 unsigned int form = dwarf_whatform(&attr);
164 #define PROCESS_NUMERIC(type) \
165 if (dwarf_form ## type(&attr, &u.type) != 0) \
167 *result = (uint64_t)u.type; \
173 PROCESS_NUMERIC(addr);
180 PROCESS_NUMERIC(udata);
183 PROCESS_NUMERIC(sdata);
186 PROCESS_NUMERIC(flag);
189 complain(die, "Unknown numeric form %d for attr_name: %d", form, attr_name);
192 #undef PROCESS_NUMERIC
195 static bool get_integer_base_type( enum arg_type* type, int byte_size, bool is_signed )
199 *type = ARGTYPE_CHAR;
203 *type = is_signed ? ARGTYPE_SHORT : ARGTYPE_USHORT;
207 *type = is_signed ? ARGTYPE_INT : ARGTYPE_UINT;
211 *type = is_signed ? ARGTYPE_LONG : ARGTYPE_ULONG;
219 static enum arg_type get_base_type(Dwarf_Die* die)
222 if( !get_die_numeric((uint64_t*)&encoding, die, DW_AT_encoding) )
225 if (encoding == DW_ATE_void )
228 if (encoding == DW_ATE_signed_char || encoding == DW_ATE_unsigned_char )
232 if (!get_die_numeric(&byte_size, die, DW_AT_byte_size))
235 if (encoding == DW_ATE_signed ||
236 encoding == DW_ATE_unsigned ||
237 encoding == DW_ATE_boolean) {
239 bool is_signed = (encoding == DW_ATE_signed);
242 if(!get_integer_base_type(&type, (int)byte_size, is_signed)) {
243 complain(die, "Unknown integer base type. Using 'void'");
249 if (encoding == DW_ATE_float) {
252 return ARGTYPE_FLOAT;
255 return ARGTYPE_DOUBLE;
258 // things like long doubles. ltrace has no support yet, so I just
265 if (encoding == DW_ATE_complex_float) {
267 case 2*sizeof(float):
268 return ARGTYPE_FLOAT;
270 case 2*sizeof(double):
271 return ARGTYPE_DOUBLE;
274 // things like long doubles. ltrace has no support yet, so I just
281 // Unknown encoding. I just say void
282 complain(die, "Unknown base type. Returning 'void'");
286 static bool get_type_die(Dwarf_Die* type_die, Dwarf_Die* die)
288 Dwarf_Attribute attr;
290 dwarf_attr(die, DW_AT_type, &attr) != NULL &&
291 dwarf_formref_die(&attr, type_die) != NULL;
294 static size_t dwarf_die_hash(const void* x)
296 return *(const Dwarf_Off*)x;
298 static int dwarf_die_eq(const void* a, const void* b)
300 return *(const Dwarf_Off*)a == *(const Dwarf_Off*)b;
303 static bool get_enum(struct arg_type_info* enum_info, Dwarf_Die* parent)
306 if (!get_die_numeric(&byte_size, parent, DW_AT_byte_size)) {
307 // No byte size given, assume 'int'
308 enum_info->type = ARGTYPE_INT;
310 if(!get_integer_base_type(&enum_info->type, (int)byte_size, true)) {
311 complain(parent, "Unknown integer base type. Using 'int'");
312 enum_info->type = ARGTYPE_INT;
316 struct enum_lens *lens = calloc(1, sizeof(struct enum_lens));
318 complain(parent, "alloc error");
321 lens_init_enum(lens);
322 enum_info->lens = &lens->super;
325 if (dwarf_child(parent, &die) != 0) {
326 // empty enum. we're done
331 complain(&die, "enum element: 0x%02x/'%s'", dwarf_tag(&die),
332 dwarf_diename(&die));
334 if (dwarf_tag(&die) != DW_TAG_enumerator) {
335 complain(&die, "Enums can have ONLY DW_TAG_enumerator elements");
339 if (!dwarf_hasattr(&die, DW_AT_const_value)) {
340 complain(&die, "Enums MUST have DW_AT_const_value values");
344 const char* key = dwarf_diename(&die);
346 complain(&die, "Enums must have a DW_AT_name key");
349 const char* dupkey = strdup(key);
350 if (dupkey == NULL) {
351 complain(&die, "Couldn't duplicate enum key");
355 struct value* value = calloc( 1, sizeof(struct value));
357 complain(&die, "Couldn't alloc enum value");
361 value_init_detached(value, NULL, type_get_simple( enum_info->type ), 0);
363 if (!get_die_numeric(&enum_value, &die, DW_AT_const_value)) {
364 complain(&die, "Couldn't get enum value");
368 value_set_word(value, (long)enum_value);
370 if (lens_enum_add( lens, dupkey, 0, value, 0 )) {
371 complain(&die, "Couldn't add enum element");
375 int res = dwarf_siblingof(&die, &die);
376 if (res == 0) continue; /* sibling exists */
377 if (res < 0) return false; /* error */
378 break; /* no sibling exists */
384 static bool get_array(struct arg_type_info* array_info, Dwarf_Die* parent)
387 if (!get_type_die( &type_die, parent )) {
388 complain( parent, "Array has unknown type" );
392 struct arg_type_info* info;
393 if (!get_type( &info, &type_die )) {
394 complain( parent, "Couldn't figure out array's type" );
399 if (dwarf_child(parent, &subrange) != 0) {
401 "Array must have a DW_TAG_subrange_type child, but has none");
405 Dwarf_Die next_subrange;
406 if (dwarf_siblingof(&subrange, &next_subrange) <= 0) {
408 "Array must have exactly one DW_TAG_subrange_type child");
412 if (dwarf_hasattr(&subrange, DW_AT_lower_bound)) {
413 uint64_t lower_bound;
414 if (!get_die_numeric(&lower_bound, &subrange, DW_AT_lower_bound)) {
415 complain( parent, "Couldn't read lower bound");
419 if (lower_bound != 0) {
421 "Array subrange has a nonzero lower bound. Don't know what to do");
427 if (!dwarf_hasattr(&subrange, DW_AT_upper_bound)) {
428 // no upper bound is defined. This is probably a variable-width array,
429 // and I don't know how long it is. Let's say 0 to be safe
434 if (!get_die_numeric(&N, &subrange, DW_AT_upper_bound)) {
435 complain( parent, "Couldn't read upper bound");
441 // I'm not checking the subrange type. It should be some sort of integer,
442 // and I don't know what it would mean for it to be something else
444 struct value* value = calloc( 1, sizeof(struct value));
446 complain(&subrange, "Couldn't alloc length value");
449 value_init_detached(value, NULL, type_get_simple( ARGTYPE_INT ), 0);
450 value_set_word(value, N );
452 struct expr_node* length = calloc( 1, sizeof(struct expr_node));
453 if (length == NULL) {
454 complain(&subrange, "Couldn't alloc length expr");
457 expr_init_const(length, value);
459 type_init_array(array_info, info, 0, length, 0 );
464 static bool get_structure(struct arg_type_info* struct_info, Dwarf_Die* parent)
466 type_init_struct(struct_info);
469 if (dwarf_child(parent, &die) != 0) {
470 // no elements; we're done
475 complain(&die, "member: 0x%02x", dwarf_tag(&die));
477 if (dwarf_tag(&die) != DW_TAG_member) {
478 complain(&die, "Structure can have ONLY DW_TAG_member");
483 if (!get_type_die( &type_die, &die )) {
484 complain( &die, "Couldn't get type of element");
488 struct arg_type_info* member_info = NULL;
489 if (!get_type( &member_info, &type_die )) {
490 complain(&die, "Couldn't parse type from DWARF data");
493 type_struct_add( struct_info, member_info, 0 );
495 int res = dwarf_siblingof(&die, &die);
496 if (res == 0) continue; /* sibling exists */
497 if (res < 0) return false; /* error */
498 break; /* no sibling exists */
504 // Reads the type in the die into the given structure
505 // Returns true on sucess
506 static bool get_type(struct arg_type_info** info, Dwarf_Die* type_die)
508 Dwarf_Off die_offset = dwarf_dieoffset(type_die);
509 struct arg_type_info** found_type = dict_find(&type_hash, &die_offset );
510 if (found_type != NULL) {
512 complain(type_die, "Read pre-computed type: %p", *info);
518 switch (dwarf_tag(type_die)) {
519 case DW_TAG_base_type:
520 *info = type_get_simple( get_base_type( type_die ));
521 complain(type_die, "Storing base type: %p", *info);
522 dict_insert( &type_hash, &die_offset, info );
525 case DW_TAG_subroutine_type:
526 case DW_TAG_inlined_subroutine:
527 // function pointers are stored as void*. If ltrace tries to dereference
528 // these, it'll get a segfault
529 *info = type_get_simple( ARGTYPE_VOID );
530 complain(type_die, "Storing subroutine type: %p", *info);
531 dict_insert( &type_hash, &die_offset, info );
534 case DW_TAG_pointer_type:
536 if (!get_type_die(&next_die, type_die )) {
537 // the pointed-to type isn't defined, so I report a void*
538 *info = type_get_simple( ARGTYPE_VOID );
539 complain(type_die, "Storing void-pointer type: %p", *info);
540 dict_insert( &type_hash, &die_offset, info );
544 *info = calloc( 1, sizeof(struct arg_type_info));
546 complain(type_die, "alloc error");
549 type_init_pointer(*info, NULL, 0);
551 complain(type_die, "Storing pointer type: %p", *info);
552 dict_insert( &type_hash, &die_offset, info );
553 return get_type( &(*info)->u.ptr_info.info, &next_die );
555 case DW_TAG_structure_type:
556 *info = calloc( 1, sizeof(struct arg_type_info));
558 complain(type_die, "alloc error");
562 complain(type_die, "Storing struct type: %p", *info);
563 dict_insert( &type_hash, &die_offset, info );
564 return get_structure( *info, type_die );
567 case DW_TAG_typedef: ;
568 case DW_TAG_const_type: ;
569 case DW_TAG_volatile_type: ;
570 // Various tags are simply pass-through, so I just keep going
572 if (get_type_die(&next_die, type_die )) {
573 complain(type_die, "Storing const/typedef type: %p", *info);
574 res = get_type( info, &next_die );
576 // no type. Use 'void'. Normally I'd think this is bogus, but stdio
577 // typedefs something to void
578 *info = type_get_simple( ARGTYPE_VOID );
579 complain(type_die, "Storing void type: %p", *info);
582 dict_insert( &type_hash, &die_offset, info );
585 case DW_TAG_enumeration_type:
586 // We have an enumeration. This has type "int", but has a particular
587 // lens to handle the enum
588 *info = calloc( 1, sizeof(struct arg_type_info));
590 complain(type_die, "alloc error");
594 complain(type_die, "Storing enum int: %p", *info);
595 dict_insert( &type_hash, &die_offset, info );
596 return get_enum( *info, type_die );
598 case DW_TAG_array_type:
599 *info = calloc( 1, sizeof(struct arg_type_info));
601 complain(type_die, "alloc error");
605 complain(type_die, "Storing array: %p", *info);
606 dict_insert( &type_hash, &die_offset, info );
607 return get_array( *info, type_die );
609 case DW_TAG_union_type:
610 *info = type_get_simple( ARGTYPE_VOID );
611 complain(type_die, "Storing union-as-void type: %p", *info);
615 complain(type_die, "Unknown type tag 0x%x", dwarf_tag(type_die));
622 static bool get_prototype(struct prototype* proto, Dwarf_Die* subroutine)
624 // First, look at the return type. This is stored in a DW_AT_type tag in the
625 // subroutine DIE. If there is no such tag, this function returns void
626 Dwarf_Die return_type_die;
627 if (!get_type_die(&return_type_die, subroutine )) {
628 proto->return_info = type_get_simple( ARGTYPE_VOID );
629 proto->own_return_info = 0;
631 proto->return_info = calloc( 1, sizeof( struct arg_type_info ));
632 if (proto->return_info == NULL) {
633 complain(subroutine, "Couldn't alloc return type");
636 proto->own_return_info = 0;
638 if (!get_type( &proto->return_info, &return_type_die )) {
639 complain(subroutine, "Couldn't get return type");
645 // Now look at the arguments
647 if (dwarf_child(subroutine, &arg_die) != 0) {
648 // no args. We're done
653 if (dwarf_tag(&arg_die) != DW_TAG_formal_parameter )
654 goto next_prototype_argument;
656 complain(&arg_die, "arg: 0x%02x", dwarf_tag(&arg_die));
659 if (!get_type_die(&type_die, &arg_die )) {
660 complain(&arg_die, "Couldn't get the argument type die");
664 struct arg_type_info* arg_type_info = NULL;
665 if (!get_type( &arg_type_info, &type_die )) {
666 complain(&arg_die, "Couldn't parse arg type from DWARF data");
671 param_init_type(¶m, arg_type_info, 0);
672 if (prototype_push_param(proto, ¶m) <0) {
673 complain(&arg_die, "couldn't add argument to the prototype");
677 #ifdef DUMP_PROTOTYPES
678 fprintf(stderr, "Adding argument:\n");
679 dump_ltrace_tree(arg_type_info);
682 next_prototype_argument: ;
683 int res = dwarf_siblingof(&arg_die, &arg_die);
684 if (res == 0) continue; /* sibling exists */
685 if (res < 0) return false; /* error */
686 break; /* no sibling exists */
692 static bool process_die_compileunit(struct protolib* plib, struct library* lib,
696 if (dwarf_child(parent, &die) != 0) {
697 // no child nodes, so nothing to do
702 if (dwarf_tag(&die) == DW_TAG_subprogram) {
703 const char* function_name = dwarf_diename(&die);
705 complain(&die, "subroutine_type: 0x%02x; function '%s'",
706 dwarf_tag(&die), function_name);
708 struct prototype* proto =
709 protolib_lookup_prototype(plib, function_name, true );
712 complain(&die, "Prototype already exists. Skipping");
716 if (!filter_matches_symbol(options.plt_filter, function_name, lib) &&
717 !filter_matches_symbol(options.static_filter, function_name, lib) &&
718 !filter_matches_symbol(options.export_filter, function_name, lib)) {
719 complain(&die, "Prototype not requested by any filter");
723 proto = malloc(sizeof(struct prototype));
725 complain(&die, "couldn't alloc prototype");
728 prototype_init( proto );
730 if (!get_prototype(proto, &die )) {
731 complain(&die, "couldn't get prototype");
735 protolib_add_prototype(plib, function_name, 0, proto);
739 int res = dwarf_siblingof(&die, &die);
740 if (res == 0) continue; /* sibling exists */
741 if (res < 0) return false; /* error */
742 break; /* no sibling exists */
748 static bool import(struct protolib* plib, struct library* lib, Dwfl* dwfl)
750 dict_init(&type_hash, sizeof(Dwarf_Off), sizeof(struct arg_type_info*),
751 dwarf_die_hash, dwarf_die_eq, NULL );
754 Dwarf_Die* die = NULL;
755 while ((die = dwfl_nextcu(dwfl, die, &bias)) != NULL) {
756 if (dwarf_tag(die) == DW_TAG_compile_unit) {
757 if (!process_die_compileunit(plib, lib, die)) {
758 complain(die, "Error reading compile unit");
763 complain(die, "DW_TAG_compile_unit expected");
769 dict_destroy( &type_hash, NULL, NULL, NULL );
773 bool import_DWARF_prototypes(struct protolib* plib, struct library* lib,
777 plib = protolib_cache_default(&g_protocache, lib->soname, 0);
779 fprintf(stderr, "Error loading protolib %s: %s.\n",
780 lib->soname, strerror(errno));
784 return import(plib, lib, dwfl);
788 - I handle static functions now. Should I? Those do not have DW_AT_external==1
790 - should process existing prototypes to make sure they match
792 - what do function pointers look like? I'm doing void*
796 - all my *allocs leak
798 - protolib_lookup_prototype should look for imports?