1 /* Most of this is Copyright Dima Kogan <dima@secretsauce.net>
3 * Pieces of this were taken from dwarf_prototypes.c in the dwarves project.
4 * Those are Copyright (C) 2008 Arnaldo Carvalho de Melo <acme@redhat.com>.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of version 2 of the GNU General Public License as published by the
8 * Free Software Foundation.
12 #include <elfutils/libdwfl.h>
19 #include "prototype.h"
24 #include "lens_enum.h"
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 getType( struct arg_type_info** info, Dwarf_Die* type_die);
50 static bool _dump_dwarf_tree(Dwarf_Die* die, int indent)
54 printf("%*sprocessing unit: 0x%02x/'%s'\n", indent*4, "",
55 dwarf_tag(die), dwarf_diename(die) );
58 if (dwarf_child(die, &child) == 0)
60 if( !_dump_dwarf_tree(&child, indent+1) )
64 int res = dwarf_siblingof(die, die);
65 if( res == 0 ) continue; // sibling exists
66 if( res < 0 ) return false; // error
67 break; // no sibling exists
73 static bool dump_dwarf_tree(Dwarf_Die* die)
75 return _dump_dwarf_tree( die, 0 );
78 static bool _dump_ltrace_tree( const struct arg_type_info* info, int indent )
82 printf("%*s%p ...\n", indent*4, "", (void*)info);
88 printf("%*s%p NULL\n", indent*4, "", (void*)info);
95 printf("%*s%p void\n", indent*4, "", (void*)info);
107 printf("%*s%p base\n", indent*4, "", (void*)info);
111 printf("%*s%p array. elements not printed\n", indent*4, "", (void*)info);
114 case ARGTYPE_POINTER:
115 printf("%*s%p pointer to...\n", indent*4, "", (void*)info);
116 _dump_ltrace_tree( info->u.ptr_info.info, indent+1 );
120 printf("%*s%p struct...\n", indent*4, "", (void*)info);
123 struct arg_type_info *info;
125 }* elements = (struct struct_field*)info->u.entries.data;
127 for(i=0; i<info->u.entries.size; i++)
128 _dump_ltrace_tree( elements[i].info, indent+1 );
132 printf("%*s%p unknown type\n", indent*4, "", (void*)info);
139 static bool dump_ltrace_tree( const struct arg_type_info* info )
141 return _dump_ltrace_tree( info, 0 );
147 static uint64_t attr_numeric(Dwarf_Die *die, uint32_t name)
149 Dwarf_Attribute attr;
152 if (dwarf_attr(die, name, &attr) == NULL)
155 form = dwarf_whatform(&attr);
160 if (dwarf_formaddr(&attr, &addr) == 0)
169 case DW_FORM_udata: {
171 if (dwarf_formudata(&attr, &value) == 0)
176 case DW_FORM_flag_present: {
178 if (dwarf_formflag(&attr, &value) == 0)
183 complain(die, "DW_AT_<0x%x>=0x%x", name, form);
190 static enum arg_type getBaseType( Dwarf_Die* die )
192 int encoding = attr_numeric(die, DW_AT_encoding);
194 if( encoding == DW_ATE_void )
197 if( encoding == DW_ATE_signed_char || encoding == DW_ATE_unsigned_char )
200 if( encoding == DW_ATE_signed ||
201 encoding == DW_ATE_unsigned )
203 bool is_signed = (encoding == DW_ATE_signed);
204 switch( attr_numeric(die, DW_AT_byte_size) )
210 return is_signed ? ARGTYPE_SHORT : ARGTYPE_USHORT;
213 return is_signed ? ARGTYPE_INT : ARGTYPE_UINT;
216 return is_signed ? ARGTYPE_LONG : ARGTYPE_ULONG;
224 if( encoding == DW_ATE_float )
226 switch( attr_numeric(die, DW_AT_byte_size) )
229 return ARGTYPE_FLOAT;
232 return ARGTYPE_DOUBLE;
245 static bool getTypeDie( Dwarf_Die* type_die, Dwarf_Die* die )
247 Dwarf_Attribute attr;
249 dwarf_attr(die, DW_AT_type, &attr) != NULL &&
250 dwarf_formref_die(&attr, type_die) != NULL;
253 static size_t dwarf_die_hash(const void* x)
255 return *(const Dwarf_Off*)x;
257 static int dwarf_die_eq(const void* a, const void* b)
259 return *(const Dwarf_Off*)a == *(const Dwarf_Off*)b;
262 static bool getEnum(struct arg_type_info* enum_info, Dwarf_Die* parent)
264 enum_info->type = ARGTYPE_INT;
266 struct enum_lens *lens = calloc(1, sizeof(struct enum_lens));
269 complain(parent, "alloc error");
272 lens_init_enum(lens);
273 enum_info->lens = &lens->super;
276 if( dwarf_child(parent, &die) != 0 )
278 // empty enum. we're done
283 complain(&die, "enum element: 0x%02x/'%s'", dwarf_tag(&die), dwarf_diename(&die) );
285 if( dwarf_tag(&die) != DW_TAG_enumerator )
287 complain(&die, "Enums can have ONLY DW_TAG_enumerator elements");
291 if( !dwarf_hasattr(&die, DW_AT_const_value) )
293 complain(&die, "Enums MUST have DW_AT_const_value values");
297 const char* key = dwarf_diename(&die);
300 complain(&die, "Enums must have a DW_AT_name key");
303 const char* dupkey = strdup(key);
306 complain(&die, "Couldn't duplicate enum key");
310 struct value* value = calloc( 1, sizeof(struct value) );
313 complain(&die, "Couldn't alloc enum value");
317 value_init_detached(value, NULL, type_get_simple( ARGTYPE_INT ), 0);
318 value_set_word(value, attr_numeric(&die, DW_AT_const_value) );
320 if( lens_enum_add( lens, dupkey, 0, value, 0 ) )
322 complain(&die, "Couldn't add enum element");
326 int res = dwarf_siblingof(&die, &die);
327 if( res == 0 ) continue; /* sibling exists */
328 if( res < 0 ) return false; /* error */
329 break; /* no sibling exists */
335 static bool getArray(struct arg_type_info* array_info, Dwarf_Die* parent)
338 if( !getTypeDie( &type_die, parent ) )
340 complain( parent, "Array has unknown type" );
344 struct arg_type_info* info;
345 if( !getType( &info, &type_die ) )
347 complain( parent, "Couldn't figure out array's type" );
352 if( dwarf_child(parent, &subrange) != 0 )
354 complain( parent, "Array must have a DW_TAG_subrange_type child, but has none" );
358 Dwarf_Die next_subrange;
359 if( dwarf_siblingof(&subrange, &next_subrange) <= 0 )
361 complain( parent, "Array must have exactly one DW_TAG_subrange_type child" );
365 if( !dwarf_hasattr(&subrange, DW_AT_upper_bound) )
367 complain( parent, "Array subrange must have a DW_AT_upper_bound");
371 if( dwarf_hasattr(&subrange, DW_AT_lower_bound) )
373 if( attr_numeric(&subrange, DW_AT_lower_bound) != 0 )
375 complain( parent, "Array subrange has a nonzero lower bound. Don't know what to do");
380 // I'm not checking the subrange type. It should be some sort of integer,
381 // and I don't know what it would mean for it to be something else
383 struct value* value = calloc( 1, sizeof(struct value) );
386 complain(&subrange, "Couldn't alloc length value");
389 value_init_detached(value, NULL, type_get_simple( ARGTYPE_INT ), 0);
390 value_set_word(value, attr_numeric(&subrange, DW_AT_upper_bound)+1 );
392 struct expr_node* length = calloc( 1, sizeof(struct expr_node) );
395 complain(&subrange, "Couldn't alloc length expr");
398 expr_init_const(length, value);
400 type_init_array(array_info, info, 0, length, 0 );
405 static bool getStructure(struct arg_type_info* struct_info, Dwarf_Die* parent)
407 type_init_struct(struct_info);
410 if( dwarf_child(parent, &die) != 0 )
412 // no elements; we're done
417 fprintf(stderr, "member: 0x%02x/'%s'\n", dwarf_tag(&die), dwarf_diename(&die) );
419 if( dwarf_tag(&die) != DW_TAG_member )
421 complain(&die, "Structure can have ONLY DW_TAG_member");
426 if( !getTypeDie( &type_die, &die ) )
428 complain( &die, "Couldn't get type of element");
432 struct arg_type_info* member_info = NULL;
433 if( !getType( &member_info, &type_die ) )
435 complain(&die, "Couldn't parse type from DWARF data");
438 type_struct_add( struct_info, member_info, 0 );
440 int res = dwarf_siblingof(&die, &die);
441 if( res == 0 ) continue; /* sibling exists */
442 if( res < 0 ) return false; /* error */
443 break; /* no sibling exists */
449 // Reads the type in the die into the given structure
450 // Returns true on sucess
451 static bool getType( struct arg_type_info** info, Dwarf_Die* type_die)
453 Dwarf_Off die_offset = dwarf_dieoffset(type_die);
454 struct arg_type_info** found_type = dict_find(&type_hash, &die_offset );
455 if(found_type != NULL)
458 complain(type_die, "Read pre-computed type: %p", *info);
464 switch( dwarf_tag(type_die) )
466 case DW_TAG_base_type:
467 *info = type_get_simple( getBaseType( type_die ) );
468 complain(type_die, "Storing base type: %p", *info);
469 dict_insert( &type_hash, &die_offset, info );
472 case DW_TAG_subroutine_type:
473 case DW_TAG_inlined_subroutine:
474 // function pointers are stored as void*. If ltrace tries to dereference
475 // these, it'll get a segfault
476 *info = type_get_simple( ARGTYPE_VOID );
477 complain(type_die, "Storing subroutine type: %p", *info);
478 dict_insert( &type_hash, &die_offset, info );
481 case DW_TAG_pointer_type:
483 if( !getTypeDie(&next_die, type_die ) )
485 // the pointed-to type isn't defined, so I report a void*
486 *info = type_get_simple( ARGTYPE_VOID );
487 complain(type_die, "Storing void-pointer type: %p", *info);
488 dict_insert( &type_hash, &die_offset, info );
492 *info = calloc( 1, sizeof(struct arg_type_info) );
495 complain(type_die, "alloc error");
498 type_init_pointer(*info, NULL, 0);
500 complain(type_die, "Storing pointer type: %p", *info);
501 dict_insert( &type_hash, &die_offset, info );
502 return getType( &(*info)->u.ptr_info.info, &next_die );
504 case DW_TAG_structure_type:
505 *info = calloc( 1, sizeof(struct arg_type_info) );
508 complain(type_die, "alloc error");
512 complain(type_die, "Storing struct type: %p", *info);
513 dict_insert( &type_hash, &die_offset, info );
514 return getStructure( *info, type_die );
517 case DW_TAG_typedef: ;
518 case DW_TAG_const_type: ;
519 case DW_TAG_volatile_type: ;
520 // Various tags are simply pass-through, so I just keep going
522 if( getTypeDie(&next_die, type_die ) )
524 complain(type_die, "Storing const/typedef type: %p", *info);
525 res = getType( info, &next_die );
529 // no type. Use 'void'. Normally I'd think this is bogus, but stdio
530 // typedefs something to void
531 *info = type_get_simple( ARGTYPE_VOID );
532 complain(type_die, "Storing void type: %p", *info);
535 dict_insert( &type_hash, &die_offset, info );
538 case DW_TAG_enumeration_type:
539 // We have an enumeration. This has type "int", but has a particular
540 // lens to handle the enum
541 *info = calloc( 1, sizeof(struct arg_type_info) );
544 complain(type_die, "alloc error");
548 complain(type_die, "Storing enum int: %p", *info);
549 dict_insert( &type_hash, &die_offset, info );
550 return getEnum( *info, type_die );
552 case DW_TAG_array_type:
553 *info = calloc( 1, sizeof(struct arg_type_info) );
556 complain(type_die, "alloc error");
560 complain(type_die, "Storing array: %p", *info);
561 dict_insert( &type_hash, &die_offset, info );
562 return getArray( *info, type_die );
565 complain(type_die, "Unknown type tag 0x%x", dwarf_tag(type_die));
572 static bool getPrototype(struct prototype* proto, Dwarf_Die* subroutine)
574 // First, look at the return type. This is stored in a DW_AT_type tag in the
575 // subroutine DIE. If there is no such tag, this function returns void
576 Dwarf_Die return_type_die;
577 if( !getTypeDie(&return_type_die, subroutine ) )
579 proto->return_info = type_get_simple( ARGTYPE_VOID );
580 proto->own_return_info = 0;
584 proto->return_info = calloc( 1, sizeof( struct arg_type_info ) );
585 if( proto->return_info == NULL )
587 complain(subroutine, "Couldn't alloc return type");
590 proto->own_return_info = 0;
592 if( !getType( &proto->return_info, &return_type_die ) )
594 complain(subroutine, "Couldn't get return type");
600 // Now look at the arguments
602 if( dwarf_child(subroutine, &arg_die) != 0 )
604 // no args. We're done
609 if( dwarf_tag(&arg_die) != DW_TAG_formal_parameter )
610 goto next_prototype_argument;
612 complain(&arg_die, "arg: 0x%02x", dwarf_tag(&arg_die));
615 if( !getTypeDie(&type_die, &arg_die ) )
617 complain(&arg_die, "Couldn't get the argument type die");
621 struct arg_type_info* arg_type_info = NULL;
622 if( !getType( &arg_type_info, &type_die ) )
624 complain(&arg_die, "Couldn't parse arg type from DWARF data");
629 param_init_type(¶m, arg_type_info, 0);
630 if( prototype_push_param(proto, ¶m) <0 )
632 complain(&arg_die, "couldn't add argument to the prototype");
636 next_prototype_argument: ;
637 int res = dwarf_siblingof(&arg_die, &arg_die);
638 if( res == 0 ) continue; /* sibling exists */
639 if( res < 0 ) return false; /* error */
640 break; /* no sibling exists */
646 static bool process_die_compileunit(struct protolib* plib, struct library* lib, Dwarf_Die* parent)
649 if( dwarf_child(parent, &die) != 0 )
654 if( dwarf_tag(&die) == DW_TAG_subprogram )
656 const char* function_name = dwarf_diename(&die);
658 complain(&die, "subroutine_type: 0x%02x; function '%s'", dwarf_tag(&die), function_name);
660 struct prototype* proto =
661 protolib_lookup_prototype(plib, function_name, true );
665 complain(&die, "Prototype already exists. Skipping");
669 if( !filter_matches_symbol(options.plt_filter, function_name, lib) &&
670 !filter_matches_symbol(options.static_filter, function_name, lib) &&
671 !filter_matches_symbol(options.export_filter, function_name, lib) )
673 complain(&die, "Prototype not requested by any filter");
677 proto = malloc(sizeof(struct prototype));
680 complain(&die, "couldn't alloc prototype");
683 prototype_init( proto );
685 if( !getPrototype(proto, &die ) )
687 complain(&die, "couldn't get prototype");
691 protolib_add_prototype(plib, function_name, 0, proto);
695 int res = dwarf_siblingof(&die, &die);
696 if( res == 0 ) continue; /* sibling exists */
697 if( res < 0 ) return false; /* error */
698 break; /* no sibling exists */
704 static bool import( struct protolib* plib, struct library* lib, Dwfl* dwfl )
706 dict_init(&type_hash, sizeof(Dwarf_Off), sizeof(struct arg_type_info*),
707 dwarf_die_hash, dwarf_die_eq, NULL );
710 Dwarf_Die* die = NULL;
711 while( (die = dwfl_nextcu(dwfl, die, &bias)) != NULL )
713 if( dwarf_tag(die) == DW_TAG_compile_unit )
715 if( !process_die_compileunit(plib, lib, die) )
717 complain(die, "Error reading compile unit");
724 complain(die, "DW_TAG_compile_unit expected");
730 dict_destroy( &type_hash, NULL, NULL, NULL );
734 bool import_DWARF_prototypes( struct protolib* plib, struct library* lib,
739 plib = protolib_cache_default(&g_protocache, lib->soname, 0);
742 fprintf(stderr, "Error loading protolib %s: %s.\n",
743 lib->soname, strerror(errno));
747 return import(plib, lib, dwfl);
751 - I handle static functions now. Should I? Those do not have DW_AT_external==1
753 - should process existing prototypes to make sure they match
755 - what do function pointers look like? I'm doing void*
759 - all my *allocs leak
761 - protolib_lookup_prototype should look for imports?