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 DUMP_PROTOTYPES
35 #define complain( die, format, ... ) \
36 fprintf(stderr, "%s() die '%s' @ 0x%lx: " format "\n", \
37 __func__, dwarf_diename(die), dwarf_dieoffset(die), \
40 #define complain( die, format, ... )
43 // A map from DIE addresses (Dwarf_Off) to type structures (struct
44 // arg_type_info*). This is created and filled in at the start of each import,
45 // and deleted when the import is complete
46 static struct dict type_hash;
49 static bool get_type(struct arg_type_info** info, Dwarf_Die* type_die);
53 static bool _dump_dwarf_tree(Dwarf_Die* die, int indent)
56 fprintf(stderr, "%*sprocessing unit: 0x%02x/'%s'\n", indent*4, "",
57 dwarf_tag(die), dwarf_diename(die));
60 if (dwarf_child(die, &child) == 0) {
61 if (!_dump_dwarf_tree(&child, indent+1))
65 int res = dwarf_siblingof(die, die);
66 if (res == 0 ) continue; // sibling exists
67 if (res < 0 ) return false; // error
68 break; // no sibling exists
74 static bool dump_dwarf_tree(Dwarf_Die* die)
76 return _dump_dwarf_tree( die, 0 );
80 #ifdef DUMP_PROTOTYPES
81 static bool _dump_ltrace_tree(const struct arg_type_info* info, int indent)
84 fprintf(stderr, "%*s%p ...\n", indent*4, "", (void*)info);
89 fprintf(stderr, "%*s%p NULL\n", indent*4, "", (void*)info);
95 fprintf(stderr, "%*s%p void\n", indent*4, "", (void*)info);
107 fprintf(stderr, "%*s%p base\n", indent*4, "", (void*)info);
111 fprintf(stderr, "%*s%p array. elements not printed\n", indent*4, "",
115 case ARGTYPE_POINTER:
116 fprintf(stderr, "%*s%p pointer to...\n", indent*4, "", (void*)info);
117 _dump_ltrace_tree( info->u.ptr_info.info, indent+1 );
121 fprintf(stderr, "%*s%p struct...\n", indent*4, "", (void*)info);
124 struct arg_type_info *info;
126 }* elements = (struct struct_field*)info->u.entries.data;
128 for(i=0; i<info->u.entries.size; i++)
129 _dump_ltrace_tree( elements[i].info, indent+1 );
133 fprintf(stderr, "%*s%p unknown type\n", indent*4, "", (void*)info);
140 static bool dump_ltrace_tree(const struct arg_type_info* info)
142 return _dump_ltrace_tree( info, 0 );
148 static uint64_t attr_numeric(Dwarf_Die *die, uint32_t name)
150 Dwarf_Attribute attr;
153 if (dwarf_attr(die, name, &attr) == NULL)
156 form = dwarf_whatform(&attr);
161 if (dwarf_formaddr(&attr, &addr) == 0)
170 case DW_FORM_udata: {
172 if (dwarf_formudata(&attr, &value) == 0)
177 case DW_FORM_flag_present: {
179 if (dwarf_formflag(&attr, &value) == 0)
184 complain(die, "DW_AT_<0x%x>=0x%x", name, form);
191 static enum arg_type get_base_type(Dwarf_Die* die)
193 int encoding = attr_numeric(die, DW_AT_encoding);
195 if (encoding == DW_ATE_void )
198 if (encoding == DW_ATE_signed_char || encoding == DW_ATE_unsigned_char )
201 if (encoding == DW_ATE_signed ||
202 encoding == DW_ATE_unsigned ||
203 encoding == DW_ATE_boolean) {
204 bool is_signed = (encoding == DW_ATE_signed);
205 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) {
225 switch (attr_numeric(die, DW_AT_byte_size)) {
227 return ARGTYPE_FLOAT;
230 return ARGTYPE_DOUBLE;
233 // things like long doubles. ltrace has no support yet, so I just
239 // Unknown encoding. I just say void
240 complain(die, "Unknown base type. Returning 'void'");
244 static bool get_type_die(Dwarf_Die* type_die, Dwarf_Die* die)
246 Dwarf_Attribute attr;
248 dwarf_attr(die, DW_AT_type, &attr) != NULL &&
249 dwarf_formref_die(&attr, type_die) != NULL;
252 static size_t dwarf_die_hash(const void* x)
254 return *(const Dwarf_Off*)x;
256 static int dwarf_die_eq(const void* a, const void* b)
258 return *(const Dwarf_Off*)a == *(const Dwarf_Off*)b;
261 static bool get_enum(struct arg_type_info* enum_info, Dwarf_Die* parent)
263 enum_info->type = ARGTYPE_INT;
265 struct enum_lens *lens = calloc(1, sizeof(struct enum_lens));
267 complain(parent, "alloc error");
270 lens_init_enum(lens);
271 enum_info->lens = &lens->super;
274 if (dwarf_child(parent, &die) != 0) {
275 // empty enum. we're done
280 complain(&die, "enum element: 0x%02x/'%s'", dwarf_tag(&die),
281 dwarf_diename(&die));
283 if (dwarf_tag(&die) != DW_TAG_enumerator) {
284 complain(&die, "Enums can have ONLY DW_TAG_enumerator elements");
288 if (!dwarf_hasattr(&die, DW_AT_const_value)) {
289 complain(&die, "Enums MUST have DW_AT_const_value values");
293 const char* key = dwarf_diename(&die);
295 complain(&die, "Enums must have a DW_AT_name key");
298 const char* dupkey = strdup(key);
299 if (dupkey == NULL) {
300 complain(&die, "Couldn't duplicate enum key");
304 struct value* value = calloc( 1, sizeof(struct value));
306 complain(&die, "Couldn't alloc enum value");
310 value_init_detached(value, NULL, type_get_simple( ARGTYPE_INT ), 0);
311 value_set_word(value, attr_numeric(&die, DW_AT_const_value));
313 if (lens_enum_add( lens, dupkey, 0, value, 0 )) {
314 complain(&die, "Couldn't add enum element");
318 int res = dwarf_siblingof(&die, &die);
319 if (res == 0) continue; /* sibling exists */
320 if (res < 0) return false; /* error */
321 break; /* no sibling exists */
327 static bool get_array(struct arg_type_info* array_info, Dwarf_Die* parent)
330 if (!get_type_die( &type_die, parent )) {
331 complain( parent, "Array has unknown type" );
335 struct arg_type_info* info;
336 if (!get_type( &info, &type_die )) {
337 complain( parent, "Couldn't figure out array's type" );
342 if (dwarf_child(parent, &subrange) != 0) {
344 "Array must have a DW_TAG_subrange_type child, but has none");
348 Dwarf_Die next_subrange;
349 if (dwarf_siblingof(&subrange, &next_subrange) <= 0) {
351 "Array must have exactly one DW_TAG_subrange_type child");
355 if (dwarf_hasattr(&subrange, DW_AT_lower_bound)) {
356 if (attr_numeric(&subrange, DW_AT_lower_bound) != 0) {
358 "Array subrange has a nonzero lower bound. Don't know what to do");
364 if (!dwarf_hasattr(&subrange, DW_AT_upper_bound)) {
365 // no upper bound is defined. This is probably a variable-width array,
366 // and I don't know how long it is. Let's say 0 to be safe
370 N = attr_numeric(&subrange, DW_AT_upper_bound)+1;
372 // I'm not checking the subrange type. It should be some sort of integer,
373 // and I don't know what it would mean for it to be something else
375 struct value* value = calloc( 1, sizeof(struct value));
377 complain(&subrange, "Couldn't alloc length value");
380 value_init_detached(value, NULL, type_get_simple( ARGTYPE_INT ), 0);
381 value_set_word(value, N );
383 struct expr_node* length = calloc( 1, sizeof(struct expr_node));
384 if (length == NULL) {
385 complain(&subrange, "Couldn't alloc length expr");
388 expr_init_const(length, value);
390 type_init_array(array_info, info, 0, length, 0 );
395 static bool get_structure(struct arg_type_info* struct_info, Dwarf_Die* parent)
397 type_init_struct(struct_info);
400 if (dwarf_child(parent, &die) != 0) {
401 // no elements; we're done
406 complain(&die, "member: 0x%02x", dwarf_tag(&die));
408 if (dwarf_tag(&die) != DW_TAG_member) {
409 complain(&die, "Structure can have ONLY DW_TAG_member");
414 if (!get_type_die( &type_die, &die )) {
415 complain( &die, "Couldn't get type of element");
419 struct arg_type_info* member_info = NULL;
420 if (!get_type( &member_info, &type_die )) {
421 complain(&die, "Couldn't parse type from DWARF data");
424 type_struct_add( struct_info, member_info, 0 );
426 int res = dwarf_siblingof(&die, &die);
427 if (res == 0) continue; /* sibling exists */
428 if (res < 0) return false; /* error */
429 break; /* no sibling exists */
435 // Reads the type in the die into the given structure
436 // Returns true on sucess
437 static bool get_type(struct arg_type_info** info, Dwarf_Die* type_die)
439 Dwarf_Off die_offset = dwarf_dieoffset(type_die);
440 struct arg_type_info** found_type = dict_find(&type_hash, &die_offset );
441 if (found_type != NULL) {
443 complain(type_die, "Read pre-computed type: %p", *info);
449 switch (dwarf_tag(type_die)) {
450 case DW_TAG_base_type:
451 *info = type_get_simple( get_base_type( type_die ));
452 complain(type_die, "Storing base type: %p", *info);
453 dict_insert( &type_hash, &die_offset, info );
456 case DW_TAG_subroutine_type:
457 case DW_TAG_inlined_subroutine:
458 // function pointers are stored as void*. If ltrace tries to dereference
459 // these, it'll get a segfault
460 *info = type_get_simple( ARGTYPE_VOID );
461 complain(type_die, "Storing subroutine type: %p", *info);
462 dict_insert( &type_hash, &die_offset, info );
465 case DW_TAG_pointer_type:
467 if (!get_type_die(&next_die, type_die )) {
468 // the pointed-to type isn't defined, so I report a void*
469 *info = type_get_simple( ARGTYPE_VOID );
470 complain(type_die, "Storing void-pointer type: %p", *info);
471 dict_insert( &type_hash, &die_offset, info );
475 *info = calloc( 1, sizeof(struct arg_type_info));
477 complain(type_die, "alloc error");
480 type_init_pointer(*info, NULL, 0);
482 complain(type_die, "Storing pointer type: %p", *info);
483 dict_insert( &type_hash, &die_offset, info );
484 return get_type( &(*info)->u.ptr_info.info, &next_die );
486 case DW_TAG_structure_type:
487 *info = calloc( 1, sizeof(struct arg_type_info));
489 complain(type_die, "alloc error");
493 complain(type_die, "Storing struct type: %p", *info);
494 dict_insert( &type_hash, &die_offset, info );
495 return get_structure( *info, type_die );
498 case DW_TAG_typedef: ;
499 case DW_TAG_const_type: ;
500 case DW_TAG_volatile_type: ;
501 // Various tags are simply pass-through, so I just keep going
503 if (get_type_die(&next_die, type_die )) {
504 complain(type_die, "Storing const/typedef type: %p", *info);
505 res = get_type( info, &next_die );
507 // no type. Use 'void'. Normally I'd think this is bogus, but stdio
508 // typedefs something to void
509 *info = type_get_simple( ARGTYPE_VOID );
510 complain(type_die, "Storing void type: %p", *info);
513 dict_insert( &type_hash, &die_offset, info );
516 case DW_TAG_enumeration_type:
517 // We have an enumeration. This has type "int", but has a particular
518 // lens to handle the enum
519 *info = calloc( 1, sizeof(struct arg_type_info));
521 complain(type_die, "alloc error");
525 complain(type_die, "Storing enum int: %p", *info);
526 dict_insert( &type_hash, &die_offset, info );
527 return get_enum( *info, type_die );
529 case DW_TAG_array_type:
530 *info = calloc( 1, sizeof(struct arg_type_info));
532 complain(type_die, "alloc error");
536 complain(type_die, "Storing array: %p", *info);
537 dict_insert( &type_hash, &die_offset, info );
538 return get_array( *info, type_die );
540 case DW_TAG_union_type:
541 *info = type_get_simple( ARGTYPE_VOID );
542 complain(type_die, "Storing union-as-void type: %p", *info);
546 complain(type_die, "Unknown type tag 0x%x", dwarf_tag(type_die));
553 static bool get_prototype(struct prototype* proto, Dwarf_Die* subroutine)
555 // First, look at the return type. This is stored in a DW_AT_type tag in the
556 // subroutine DIE. If there is no such tag, this function returns void
557 Dwarf_Die return_type_die;
558 if (!get_type_die(&return_type_die, subroutine )) {
559 proto->return_info = type_get_simple( ARGTYPE_VOID );
560 proto->own_return_info = 0;
562 proto->return_info = calloc( 1, sizeof( struct arg_type_info ));
563 if (proto->return_info == NULL) {
564 complain(subroutine, "Couldn't alloc return type");
567 proto->own_return_info = 0;
569 if (!get_type( &proto->return_info, &return_type_die )) {
570 complain(subroutine, "Couldn't get return type");
576 // Now look at the arguments
578 if (dwarf_child(subroutine, &arg_die) != 0) {
579 // no args. We're done
584 if (dwarf_tag(&arg_die) != DW_TAG_formal_parameter )
585 goto next_prototype_argument;
587 complain(&arg_die, "arg: 0x%02x", dwarf_tag(&arg_die));
590 if (!get_type_die(&type_die, &arg_die )) {
591 complain(&arg_die, "Couldn't get the argument type die");
595 struct arg_type_info* arg_type_info = NULL;
596 if (!get_type( &arg_type_info, &type_die )) {
597 complain(&arg_die, "Couldn't parse arg type from DWARF data");
602 param_init_type(¶m, arg_type_info, 0);
603 if (prototype_push_param(proto, ¶m) <0) {
604 complain(&arg_die, "couldn't add argument to the prototype");
608 #ifdef DUMP_PROTOTYPES
609 fprintf(stderr, "Adding argument:\n");
610 dump_ltrace_tree(arg_type_info);
613 next_prototype_argument: ;
614 int res = dwarf_siblingof(&arg_die, &arg_die);
615 if (res == 0) continue; /* sibling exists */
616 if (res < 0) return false; /* error */
617 break; /* no sibling exists */
623 static bool process_die_compileunit(struct protolib* plib, struct library* lib,
627 if (dwarf_child(parent, &die) != 0) {
628 // no child nodes, so nothing to do
633 if (dwarf_tag(&die) == DW_TAG_subprogram) {
634 const char* function_name = dwarf_diename(&die);
636 complain(&die, "subroutine_type: 0x%02x; function '%s'",
637 dwarf_tag(&die), function_name);
639 struct prototype* proto =
640 protolib_lookup_prototype(plib, function_name, true );
643 complain(&die, "Prototype already exists. Skipping");
647 if (!filter_matches_symbol(options.plt_filter, function_name, lib) &&
648 !filter_matches_symbol(options.static_filter, function_name, lib) &&
649 !filter_matches_symbol(options.export_filter, function_name, lib)) {
650 complain(&die, "Prototype not requested by any filter");
654 proto = malloc(sizeof(struct prototype));
656 complain(&die, "couldn't alloc prototype");
659 prototype_init( proto );
661 if (!get_prototype(proto, &die )) {
662 complain(&die, "couldn't get prototype");
666 protolib_add_prototype(plib, function_name, 0, proto);
670 int res = dwarf_siblingof(&die, &die);
671 if (res == 0) continue; /* sibling exists */
672 if (res < 0) return false; /* error */
673 break; /* no sibling exists */
679 static bool import(struct protolib* plib, struct library* lib, Dwfl* dwfl)
681 dict_init(&type_hash, sizeof(Dwarf_Off), sizeof(struct arg_type_info*),
682 dwarf_die_hash, dwarf_die_eq, NULL );
685 Dwarf_Die* die = NULL;
686 while ((die = dwfl_nextcu(dwfl, die, &bias)) != NULL) {
687 if (dwarf_tag(die) == DW_TAG_compile_unit) {
688 if (!process_die_compileunit(plib, lib, die)) {
689 complain(die, "Error reading compile unit");
694 complain(die, "DW_TAG_compile_unit expected");
700 dict_destroy( &type_hash, NULL, NULL, NULL );
704 bool import_DWARF_prototypes(struct protolib* plib, struct library* lib,
708 plib = protolib_cache_default(&g_protocache, lib->soname, 0);
710 fprintf(stderr, "Error loading protolib %s: %s.\n",
711 lib->soname, strerror(errno));
715 return import(plib, lib, dwfl);
719 - I handle static functions now. Should I? Those do not have DW_AT_external==1
721 - should process existing prototypes to make sure they match
723 - what do function pointers look like? I'm doing void*
727 - all my *allocs leak
729 - protolib_lookup_prototype should look for imports?