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 #define NEXT_SIBLING(die) \
41 int res = dwarf_siblingof(die, die); \
42 if (res == 0) continue; /* sibling exists */ \
43 if (res < 0) return false; /* error */ \
44 break /* no sibling exists */
46 // A map from DIE addresses (Dwarf_Off) to type structures (struct
47 // arg_type_info*). This is created and filled in at the start of each import,
48 // and deleted when the import is complete
49 static struct dict type_hash;
52 static bool get_type(struct arg_type_info** info, Dwarf_Die* type_die, struct protolib* plib);
56 static bool _dump_dwarf_tree(Dwarf_Die* die, int indent)
59 fprintf(stderr, "%*sprocessing unit: 0x%02x/'%s'\n", indent*4, "",
60 dwarf_tag(die), dwarf_diename(die));
63 if (dwarf_child(die, &child) == 0) {
64 if (!_dump_dwarf_tree(&child, indent+1))
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);
147 // pulls a numerical value out of a particular attribute in a die. Returns true
148 // if successful. The result is returned in *result. Note that this is cast to
149 // (uint64_t), regardless of the actual type of the input
150 static bool get_die_numeric(uint64_t* result,
151 Dwarf_Die *die, unsigned int attr_name)
153 Dwarf_Attribute attr ;
162 if (dwarf_attr(die, attr_name, &attr) == NULL)
165 unsigned int form = dwarf_whatform(&attr);
167 #define PROCESS_NUMERIC(type) \
168 if (dwarf_form ## type(&attr, &u.type) != 0) \
170 *result = (uint64_t)u.type; \
176 PROCESS_NUMERIC(addr);
183 PROCESS_NUMERIC(udata);
186 PROCESS_NUMERIC(sdata);
189 PROCESS_NUMERIC(flag);
192 complain(die, "Unknown numeric form %d for attr_name: %d", form, attr_name);
195 #undef PROCESS_NUMERIC
198 static bool get_integer_base_type(enum arg_type* type, int byte_size, bool is_signed)
202 *type = ARGTYPE_CHAR;
206 *type = is_signed ? ARGTYPE_SHORT : ARGTYPE_USHORT;
210 *type = is_signed ? ARGTYPE_INT : ARGTYPE_UINT;
214 *type = is_signed ? ARGTYPE_LONG : ARGTYPE_ULONG;
222 static enum arg_type get_base_type(Dwarf_Die* die)
225 if(!get_die_numeric((uint64_t*)&encoding, die, DW_AT_encoding))
228 if (encoding == DW_ATE_void)
231 if (encoding == DW_ATE_signed_char || encoding == DW_ATE_unsigned_char)
235 if (!get_die_numeric(&byte_size, die, DW_AT_byte_size))
238 if (encoding == DW_ATE_signed ||
239 encoding == DW_ATE_unsigned ||
240 encoding == DW_ATE_boolean) {
242 bool is_signed = (encoding == DW_ATE_signed);
245 if(!get_integer_base_type(&type, (int)byte_size, is_signed)) {
246 complain(die, "Unknown integer base type. Using 'void'");
252 if (encoding == DW_ATE_float) {
255 return ARGTYPE_FLOAT;
258 return ARGTYPE_DOUBLE;
261 // things like long doubles. ltrace has no support yet, so I just
268 if (encoding == DW_ATE_complex_float) {
270 case 2*sizeof(float):
271 return ARGTYPE_FLOAT;
273 case 2*sizeof(double):
274 return ARGTYPE_DOUBLE;
277 // things like long doubles. ltrace has no support yet, so I just
284 // Unknown encoding. I just say void
285 complain(die, "Unknown base type. Returning 'void'");
289 static bool get_type_die(Dwarf_Die* type_die, Dwarf_Die* die)
291 Dwarf_Attribute attr;
293 dwarf_attr(die, DW_AT_type, &attr) != NULL &&
294 dwarf_formref_die(&attr, type_die) != NULL;
297 static size_t dwarf_die_hash(const void* x)
299 return *(const Dwarf_Off*)x;
301 static int dwarf_die_eq(const void* a, const void* b)
303 return *(const Dwarf_Off*)a == *(const Dwarf_Off*)b;
306 static bool get_enum(struct arg_type_info* enum_info, Dwarf_Die* parent)
309 if (!get_die_numeric(&byte_size, parent, DW_AT_byte_size)) {
310 // No byte size given, assume 'int'
311 enum_info->type = ARGTYPE_INT;
313 if(!get_integer_base_type(&enum_info->type, (int)byte_size, true)) {
314 complain(parent, "Unknown integer base type. Using 'int'");
315 enum_info->type = ARGTYPE_INT;
319 struct enum_lens *lens = calloc(1, sizeof(struct enum_lens));
321 complain(parent, "alloc error");
324 lens_init_enum(lens);
325 enum_info->lens = &lens->super;
328 if (dwarf_child(parent, &die) != 0) {
329 // empty enum. we're done
334 complain(&die, "enum element: 0x%02x/'%s'", dwarf_tag(&die),
335 dwarf_diename(&die));
337 if (dwarf_tag(&die) != DW_TAG_enumerator) {
338 complain(&die, "Enums can have ONLY DW_TAG_enumerator elements");
342 if (!dwarf_hasattr(&die, DW_AT_const_value)) {
343 complain(&die, "Enums MUST have DW_AT_const_value values");
347 const char* key = dwarf_diename(&die);
349 complain(&die, "Enums must have a DW_AT_name key");
352 const char* dupkey = strdup(key);
353 if (dupkey == NULL) {
354 complain(&die, "Couldn't duplicate enum key");
358 struct value* value = calloc(1, sizeof(struct value));
360 complain(&die, "Couldn't alloc enum value");
364 value_init_detached(value, NULL, type_get_simple(enum_info->type), 0);
366 if (!get_die_numeric(&enum_value, &die, DW_AT_const_value)) {
367 complain(&die, "Couldn't get enum value");
371 value_set_word(value, (long)enum_value);
373 if (lens_enum_add(lens, dupkey, 0, value, 0)) {
374 complain(&die, "Couldn't add enum element");
384 static bool get_array(struct arg_type_info* array_info, Dwarf_Die* parent, struct protolib* plib)
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, plib)) {
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, struct protolib* plib)
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, plib)) {
490 complain(&die, "Couldn't parse type from DWARF data");
493 type_struct_add(struct_info, member_info, 0);
501 // Reads the type in the die into the given structure
502 // Returns true on sucess
503 static bool get_type(struct arg_type_info** info, Dwarf_Die* type_die, struct protolib* plib)
505 Dwarf_Off die_offset = dwarf_dieoffset(type_die);
506 struct arg_type_info** found_type = dict_find(&type_hash, &die_offset);
507 if (found_type != NULL) {
509 complain(type_die, "Read pre-computed type: %p", *info);
513 const char* type_name = dwarf_diename(type_die);
514 if (type_name != NULL) {
516 struct named_type* already_defined_type =
517 protolib_lookup_type(plib, type_name, false);
519 if (already_defined_type != NULL) {
521 "Type '%s' defined in a .conf file. Using that instead of DWARF",
523 *info = already_defined_type->info;
530 switch (dwarf_tag(type_die)) {
531 case DW_TAG_base_type:
532 *info = type_get_simple(get_base_type(type_die));
533 complain(type_die, "Storing base type: %p", *info);
534 dict_insert(&type_hash, &die_offset, info);
537 case DW_TAG_subroutine_type:
538 case DW_TAG_inlined_subroutine:
539 // function pointers are stored as void*. If ltrace tries to dereference
540 // these, it'll get a segfault
541 *info = type_get_simple(ARGTYPE_VOID);
542 complain(type_die, "Storing subroutine type: %p", *info);
543 dict_insert(&type_hash, &die_offset, info);
546 case DW_TAG_pointer_type:
548 if (!get_type_die(&next_die, type_die)) {
549 // the pointed-to type isn't defined, so I report a void*
550 *info = type_get_simple(ARGTYPE_VOID);
551 complain(type_die, "Storing void-pointer type: %p", *info);
552 dict_insert(&type_hash, &die_offset, info);
556 *info = calloc(1, sizeof(struct arg_type_info));
558 complain(type_die, "alloc error");
561 type_init_pointer(*info, NULL, 0);
563 complain(type_die, "Storing pointer type: %p", *info);
564 dict_insert(&type_hash, &die_offset, info);
565 return get_type(&(*info)->u.ptr_info.info, &next_die, plib);
567 case DW_TAG_structure_type:
568 *info = calloc(1, sizeof(struct arg_type_info));
570 complain(type_die, "alloc error");
574 complain(type_die, "Storing struct type: %p", *info);
575 dict_insert(&type_hash, &die_offset, info);
576 return get_structure(*info, type_die, plib);
580 case DW_TAG_const_type:
581 case DW_TAG_volatile_type: {
582 // Various tags are simply pass-through, so I just keep going
584 if (get_type_die(&next_die, type_die)) {
585 complain(type_die, "Storing const/typedef type: %p", *info);
586 res = get_type(info, &next_die, plib);
588 // no type. Use 'void'. Normally I'd think this is bogus, but stdio
589 // typedefs something to void
590 *info = type_get_simple(ARGTYPE_VOID);
591 complain(type_die, "Storing void type: %p", *info);
594 dict_insert(&type_hash, &die_offset, info);
598 case DW_TAG_enumeration_type:
599 // We have an enumeration. This has type "int", but has a particular
600 // lens to handle the enum
601 *info = calloc(1, sizeof(struct arg_type_info));
603 complain(type_die, "alloc error");
607 complain(type_die, "Storing enum int: %p", *info);
608 dict_insert(&type_hash, &die_offset, info);
609 return get_enum(*info, type_die);
611 case DW_TAG_array_type:
612 *info = calloc(1, sizeof(struct arg_type_info));
614 complain(type_die, "alloc error");
618 complain(type_die, "Storing array: %p", *info);
619 dict_insert(&type_hash, &die_offset, info);
620 return get_array(*info, type_die, plib);
622 case DW_TAG_union_type:
623 *info = type_get_simple(ARGTYPE_VOID);
624 complain(type_die, "Storing union-as-void type: %p", *info);
628 complain(type_die, "Unknown type tag 0x%x", dwarf_tag(type_die));
635 static bool get_prototype(struct prototype* proto, Dwarf_Die* subroutine, struct protolib* plib)
637 // First, look at the return type. This is stored in a DW_AT_type tag in the
638 // subroutine DIE. If there is no such tag, this function returns void
639 Dwarf_Die return_type_die;
640 if (!get_type_die(&return_type_die, subroutine)) {
641 proto->return_info = type_get_simple(ARGTYPE_VOID);
642 proto->own_return_info = 0;
644 proto->return_info = calloc(1, sizeof(struct arg_type_info));
645 if (proto->return_info == NULL) {
646 complain(subroutine, "Couldn't alloc return type");
649 proto->own_return_info = 0;
651 if (!get_type(&proto->return_info, &return_type_die, plib)) {
652 complain(subroutine, "Couldn't get return type");
658 // Now look at the arguments
660 if (dwarf_child(subroutine, &arg_die) != 0) {
661 // no args. We're done
666 if (dwarf_tag(&arg_die) == DW_TAG_formal_parameter) {
668 complain(&arg_die, "arg: 0x%02x", dwarf_tag(&arg_die));
671 if (!get_type_die(&type_die, &arg_die)) {
672 complain(&arg_die, "Couldn't get the argument type die");
676 struct arg_type_info* arg_type_info = NULL;
677 if (!get_type(&arg_type_info, &type_die, plib)) {
678 complain(&arg_die, "Couldn't parse arg type from DWARF data");
683 param_init_type(¶m, arg_type_info, 0);
684 if (prototype_push_param(proto, ¶m) <0) {
685 complain(&arg_die, "couldn't add argument to the prototype");
689 #ifdef DUMP_PROTOTYPES
690 fprintf(stderr, "Adding argument:\n");
691 dump_ltrace_tree(arg_type_info);
695 NEXT_SIBLING(&arg_die);
701 static bool import_subprogram(struct protolib* plib, struct library* lib,
704 // I use the linkage function name if there is one, otherwise the
706 const char* function_name = NULL;
707 Dwarf_Attribute attr;
708 if (dwarf_attr(die, DW_AT_linkage_name, &attr) != NULL)
709 function_name = dwarf_formstring(&attr);
710 if (function_name == NULL)
711 function_name = dwarf_diename(die);
712 if (function_name == NULL) {
713 complain(die, "Function has no name. Not importing");
718 complain(die, "subroutine_type: 0x%02x; function '%s'",
719 dwarf_tag(die), function_name);
721 struct prototype* proto =
722 protolib_lookup_prototype(plib, function_name, false);
725 complain(die, "Prototype already exists. Skipping");
729 if (!filter_matches_symbol(options.plt_filter, function_name, lib) &&
730 !filter_matches_symbol(options.static_filter, function_name, lib) &&
731 !filter_matches_symbol(options.export_filter, function_name, lib)) {
732 complain(die, "Prototype not requested by any filter");
736 proto = malloc(sizeof(struct prototype));
738 complain(die, "couldn't alloc prototype");
741 prototype_init(proto);
743 if (!get_prototype(proto, die, plib)) {
744 complain(die, "couldn't get prototype");
748 protolib_add_prototype(plib, function_name, 0, proto);
752 static bool process_die_compileunit(struct protolib* plib, struct library* lib,
756 if (dwarf_child(parent, &die) != 0) {
757 // no child nodes, so nothing to do
762 if (dwarf_tag(&die) == DW_TAG_subprogram)
763 if(!import_subprogram(plib, lib, &die))
772 static bool import(struct protolib* plib, struct library* lib, Dwfl* dwfl)
774 dict_init(&type_hash, sizeof(Dwarf_Off), sizeof(struct arg_type_info*),
775 dwarf_die_hash, dwarf_die_eq, NULL);
778 Dwarf_Die* die = NULL;
779 while ((die = dwfl_nextcu(dwfl, die, &bias)) != NULL) {
780 if (dwarf_tag(die) == DW_TAG_compile_unit) {
781 if (!process_die_compileunit(plib, lib, die)) {
782 complain(die, "Error reading compile unit");
787 complain(die, "DW_TAG_compile_unit expected");
793 dict_destroy(&type_hash, NULL, NULL, NULL);
797 bool import_DWARF_prototypes(struct library* lib)
799 struct protolib* plib = lib->protolib;
800 Dwfl* dwfl = lib->dwfl;
803 plib = protolib_cache_default(&g_protocache, lib->soname, 0);
805 fprintf(stderr, "Error loading protolib %s: %s.\n",
806 lib->soname, strerror(errno));
810 if (import(plib, lib, dwfl)) {
811 lib->protolib = plib;
818 - I handle static functions now. Should I? Those do not have DW_AT_external==1
820 - should process existing prototypes to make sure they match
822 - what do function pointers look like? I'm doing void*
826 - all my *allocs leak