reworked attr_numeric() to report failures
[platform/upstream/ltrace.git] / dwarf_prototypes.c
1 /* Copyright Dima Kogan <dima@secretsauce.net>
2  *
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.
6  *
7  */
8 #include <stdio.h>
9 #include <elfutils/libdwfl.h>
10 #include <dwarf.h>
11 #include <stdlib.h>
12 #include <errno.h>
13 #include <string.h>
14
15 #include "config.h"
16 #include "prototype.h"
17 #include "type.h"
18 #include "param.h"
19 #include "dict.h"
20 #include "lens.h"
21 #include "lens_enum.h"
22 #include "value.h"
23 #include "expr.h"
24 #include "library.h"
25 #include "options.h"
26 #include "filter.h"
27
28
29 //#define DUMP_PROTOTYPES
30
31 #if 1
32 #define complain( die, format, ... )                                                    \
33         fprintf(stderr, "%s() die '%s' @ 0x%lx: " format "\n",          \
34                         __func__, dwarf_diename(die), dwarf_dieoffset(die),     \
35                         ##__VA_ARGS__ )
36 #else
37 #define complain( die, format, ... )
38 #endif
39
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;
44
45
46 static bool get_type(struct arg_type_info** info, Dwarf_Die* type_die);
47
48
49 #if 0
50 static bool _dump_dwarf_tree(Dwarf_Die* die, int indent)
51 {
52     while (1) {
53         fprintf(stderr, "%*sprocessing unit: 0x%02x/'%s'\n", indent*4, "",
54                dwarf_tag(die), dwarf_diename(die));
55
56         Dwarf_Die child;
57         if (dwarf_child(die, &child) == 0) {
58                         if (!_dump_dwarf_tree(&child, indent+1))
59                                 return false;
60         }
61
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
66     }
67
68     return true;
69 }
70
71 static bool dump_dwarf_tree(Dwarf_Die* die)
72 {
73     return _dump_dwarf_tree( die, 0 );
74 }
75 #endif
76
77 #ifdef DUMP_PROTOTYPES
78 static bool _dump_ltrace_tree(const struct arg_type_info* info, int indent)
79 {
80         if (indent > 7) {
81                 fprintf(stderr, "%*s%p ...\n", indent*4, "", (void*)info);
82                 return true;
83         }
84
85         if (info == NULL) {
86                 fprintf(stderr, "%*s%p NULL\n", indent*4, "", (void*)info);
87                 return true;
88         }
89
90         switch (info->type) {
91         case ARGTYPE_VOID:
92                 fprintf(stderr, "%*s%p void\n", indent*4, "", (void*)info);
93                 break;
94
95         case ARGTYPE_INT:
96         case ARGTYPE_UINT:
97         case ARGTYPE_LONG:
98         case ARGTYPE_ULONG:
99         case ARGTYPE_CHAR:
100         case ARGTYPE_SHORT:
101         case ARGTYPE_USHORT:
102         case ARGTYPE_FLOAT:
103         case ARGTYPE_DOUBLE:
104                 fprintf(stderr, "%*s%p base\n", indent*4, "", (void*)info);
105                 break;
106
107         case ARGTYPE_ARRAY:
108                 fprintf(stderr, "%*s%p array. elements not printed\n", indent*4, "",
109                                 (void*)info);
110                 break;
111
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 );
115                 break;
116
117         case ARGTYPE_STRUCT:
118                 fprintf(stderr, "%*s%p struct...\n", indent*4, "", (void*)info);
119                 struct struct_field
120                 {
121                         struct arg_type_info *info;
122                         int own_info;
123                 }* elements = (struct struct_field*)info->u.entries.data;
124                 unsigned int i;
125                 for(i=0; i<info->u.entries.size; i++)
126                         _dump_ltrace_tree( elements[i].info, indent+1 );
127                 break;
128
129         default:
130                 fprintf(stderr, "%*s%p unknown type\n", indent*4, "", (void*)info);
131                 return false;;
132         }
133
134         return true;
135 }
136
137 static bool dump_ltrace_tree(const struct arg_type_info* info)
138 {
139         return _dump_ltrace_tree( info, 0 );
140 }
141 #endif
142
143
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)
149 {
150         Dwarf_Attribute attr ;
151
152         union {
153                 Dwarf_Word              udata;
154                 Dwarf_Sword     sdata;
155                 Dwarf_Addr              addr;
156                 bool                    flag;
157         } u;
158
159         if (dwarf_attr(die, attr_name, &attr) == NULL)
160                 return false;
161
162         unsigned int form = dwarf_whatform(&attr);
163
164 #define PROCESS_NUMERIC(type)                                           \
165         if (dwarf_form ## type(&attr, &u.type) != 0)    \
166                 return false;                                                           \
167         *result = (uint64_t)u.type;                                             \
168         return true
169
170
171         switch (form) {
172         case DW_FORM_addr:
173                 PROCESS_NUMERIC(addr);
174
175         case DW_FORM_data1:
176         case DW_FORM_data2:
177         case DW_FORM_data4:
178         case DW_FORM_data8:
179         case DW_FORM_udata:
180                 PROCESS_NUMERIC(udata);
181
182         case DW_FORM_sdata:
183                 PROCESS_NUMERIC(sdata);
184
185         case DW_FORM_flag:
186                 PROCESS_NUMERIC(flag);
187
188         default:
189                 complain(die, "Unknown numeric form %d for attr_name: %d", form, attr_name);
190                 return false;
191         }
192 #undef PROCESS_NUMERIC
193 }
194
195 static enum arg_type get_base_type(Dwarf_Die* die)
196 {
197         int64_t encoding;
198         if( !get_die_numeric((uint64_t*)&encoding, die, DW_AT_encoding) )
199                 return ARGTYPE_VOID;
200
201         if (encoding == DW_ATE_void )
202                 return ARGTYPE_VOID;
203
204         if (encoding == DW_ATE_signed_char || encoding == DW_ATE_unsigned_char )
205                 return ARGTYPE_CHAR;
206
207                 uint64_t byte_size;
208                 if (!get_die_numeric(&byte_size, die, DW_AT_byte_size))
209                         return ARGTYPE_VOID;
210
211         if (encoding == DW_ATE_signed   ||
212                 encoding == DW_ATE_unsigned ||
213                 encoding == DW_ATE_boolean) {
214
215                 bool is_signed = (encoding == DW_ATE_signed);
216
217                 switch (byte_size) {
218                 case sizeof(char):
219                         return ARGTYPE_CHAR;
220
221                 case sizeof(short):
222                         return is_signed ? ARGTYPE_SHORT : ARGTYPE_USHORT;
223
224                 case sizeof(int):
225                         return is_signed ? ARGTYPE_INT : ARGTYPE_UINT;
226
227                 case sizeof(long):
228                         return is_signed ? ARGTYPE_LONG : ARGTYPE_ULONG;
229
230                 default:
231                         complain(die, "");
232                         exit(1);
233                 }
234         }
235
236         if (encoding == DW_ATE_float) {
237                 switch (byte_size) {
238                 case sizeof(float):
239                         return ARGTYPE_FLOAT;
240
241                 case sizeof(double):
242                         return ARGTYPE_DOUBLE;
243
244                 default:
245                         // things like long doubles. ltrace has no support yet, so I just
246                         // say "void"
247                         return ARGTYPE_VOID;
248                 }
249         }
250
251 #if 0
252         if (encoding == DW_ATE_complex_float) {
253                 switch (byte_size) {
254                 case 2*sizeof(float):
255                         return ARGTYPE_FLOAT;
256
257                 case 2*sizeof(double):
258                         return ARGTYPE_DOUBLE;
259
260                 default:
261                         // things like long doubles. ltrace has no support yet, so I just
262                         // say "void"
263                         return ARGTYPE_VOID;
264                 }
265         }
266 #endif
267
268         // Unknown encoding. I just say void
269         complain(die, "Unknown base type. Returning 'void'");
270         return ARGTYPE_VOID;
271 }
272
273 static bool get_type_die(Dwarf_Die* type_die, Dwarf_Die* die)
274 {
275         Dwarf_Attribute attr;
276         return
277                 dwarf_attr(die, DW_AT_type, &attr) != NULL &&
278                 dwarf_formref_die(&attr, type_die) != NULL;
279 }
280
281 static size_t dwarf_die_hash(const void* x)
282 {
283         return *(const Dwarf_Off*)x;
284 }
285 static int dwarf_die_eq(const void* a, const void* b)
286 {
287         return *(const Dwarf_Off*)a == *(const Dwarf_Off*)b;
288 }
289
290 static bool get_enum(struct arg_type_info* enum_info, Dwarf_Die* parent)
291 {
292         enum_info->type = ARGTYPE_INT;
293
294         struct enum_lens *lens = calloc(1, sizeof(struct enum_lens));
295         if (lens == NULL) {
296                 complain(parent, "alloc error");
297                 return false;
298         }
299         lens_init_enum(lens);
300         enum_info->lens = &lens->super;
301
302         Dwarf_Die die;
303         if (dwarf_child(parent, &die) != 0) {
304                 // empty enum. we're done
305                 return true;
306         }
307
308         while(1) {
309                 complain(&die, "enum element: 0x%02x/'%s'", dwarf_tag(&die),
310                                  dwarf_diename(&die));
311
312                 if (dwarf_tag(&die) != DW_TAG_enumerator) {
313                         complain(&die, "Enums can have ONLY DW_TAG_enumerator elements");
314                         return false;
315                 }
316
317                 if (!dwarf_hasattr(&die, DW_AT_const_value)) {
318                         complain(&die, "Enums MUST have DW_AT_const_value values");
319                         return false;
320                 }
321
322                 const char* key = dwarf_diename(&die);
323                 if (key == NULL) {
324                         complain(&die, "Enums must have a DW_AT_name key");
325                         return false;
326                 }
327                 const char* dupkey = strdup(key);
328                 if (dupkey == NULL) {
329                         complain(&die, "Couldn't duplicate enum key");
330                         return false;
331                 }
332
333                 struct value* value = calloc( 1, sizeof(struct value));
334                 if (value == NULL) {
335                         complain(&die, "Couldn't alloc enum value");
336                         return false;
337                 }
338
339                 value_init_detached(value, NULL, type_get_simple( ARGTYPE_INT ), 0);
340                 uint64_t enum_value;
341                 if (!get_die_numeric(&enum_value, &die, DW_AT_const_value)) {
342                         complain(&die, "Couldn't get enum value");
343                         return false;
344                 }
345
346                 value_set_word(value, (long)enum_value);
347
348                 if (lens_enum_add( lens, dupkey, 0, value, 0 )) {
349                         complain(&die, "Couldn't add enum element");
350                         return false;
351                 }
352
353                 int res = dwarf_siblingof(&die, &die);
354                 if (res == 0) continue;     /* sibling exists    */
355                 if (res < 0)  return false; /* error             */
356                 break;                      /* no sibling exists */
357         }
358
359         return true;
360 }
361
362 static bool get_array(struct arg_type_info* array_info, Dwarf_Die* parent)
363 {
364         Dwarf_Die type_die;
365         if (!get_type_die( &type_die, parent )) {
366                 complain( parent, "Array has unknown type" );
367                 return false;
368         }
369
370         struct arg_type_info* info;
371         if (!get_type( &info, &type_die )) {
372                 complain( parent, "Couldn't figure out array's type" );
373                 return false;
374         }
375
376         Dwarf_Die subrange;
377         if (dwarf_child(parent, &subrange) != 0) {
378                 complain(parent,
379                                  "Array must have a DW_TAG_subrange_type child, but has none");
380                 return false;
381         }
382
383         Dwarf_Die next_subrange;
384         if (dwarf_siblingof(&subrange, &next_subrange) <= 0) {
385                 complain(parent,
386                                  "Array must have exactly one DW_TAG_subrange_type child");
387                 return false;
388         }
389
390         if (dwarf_hasattr(&subrange, DW_AT_lower_bound)) {
391                 uint64_t lower_bound;
392                 if (!get_die_numeric(&lower_bound, &subrange, DW_AT_lower_bound)) {
393                         complain( parent, "Couldn't read lower bound");
394                         return false;
395                 }
396
397                 if (lower_bound != 0) {
398                         complain( parent,
399                                           "Array subrange has a nonzero lower bound. Don't know what to do");
400                         return false;
401                 }
402         }
403
404         uint64_t N;
405         if (!dwarf_hasattr(&subrange, DW_AT_upper_bound)) {
406                 // no upper bound is defined. This is probably a variable-width array,
407                 // and I don't know how long it is. Let's say 0 to be safe
408                 N = 0;
409         }
410         else
411         {
412                 if (!get_die_numeric(&N, &subrange, DW_AT_upper_bound)) {
413                         complain( parent, "Couldn't read upper bound");
414                         return false;
415                 }
416                 N++;
417         }
418
419         // I'm not checking the subrange type. It should be some sort of integer,
420         // and I don't know what it would mean for it to be something else
421
422         struct value* value = calloc( 1, sizeof(struct value));
423         if (value == NULL) {
424                 complain(&subrange, "Couldn't alloc length value");
425                 return false;
426         }
427         value_init_detached(value, NULL, type_get_simple( ARGTYPE_INT ), 0);
428         value_set_word(value, N );
429
430         struct expr_node* length = calloc( 1, sizeof(struct expr_node));
431         if (length == NULL) {
432                 complain(&subrange, "Couldn't alloc length expr");
433                 return false;
434         }
435         expr_init_const(length, value);
436
437         type_init_array(array_info, info, 0, length, 0 );
438
439         return true;
440 }
441
442 static bool get_structure(struct arg_type_info* struct_info, Dwarf_Die* parent)
443 {
444         type_init_struct(struct_info);
445
446         Dwarf_Die die;
447         if (dwarf_child(parent, &die) != 0) {
448                 // no elements; we're done
449                 return true;
450         }
451
452         while(1) {
453                 complain(&die, "member: 0x%02x", dwarf_tag(&die));
454
455                 if (dwarf_tag(&die) != DW_TAG_member) {
456                         complain(&die, "Structure can have ONLY DW_TAG_member");
457                         return false;
458                 }
459
460                 Dwarf_Die type_die;
461                 if (!get_type_die( &type_die, &die )) {
462                         complain( &die, "Couldn't get type of element");
463                         return false;
464                 }
465
466                 struct arg_type_info* member_info = NULL;
467                 if (!get_type( &member_info, &type_die )) {
468                         complain(&die, "Couldn't parse type from DWARF data");
469                         return false;
470                 }
471                 type_struct_add( struct_info, member_info, 0 );
472
473                 int res = dwarf_siblingof(&die, &die);
474                 if (res == 0) continue;     /* sibling exists    */
475                 if (res < 0)  return false; /* error             */
476                 break;                      /* no sibling exists */
477         }
478
479         return true;
480 }
481
482 // Reads the type in the die into the given structure
483 // Returns true on sucess
484 static bool get_type(struct arg_type_info** info, Dwarf_Die* type_die)
485 {
486         Dwarf_Off die_offset = dwarf_dieoffset(type_die);
487         struct arg_type_info** found_type = dict_find(&type_hash, &die_offset );
488         if (found_type != NULL) {
489                 *info = *found_type;
490                 complain(type_die, "Read pre-computed type: %p", *info);
491                 return true;
492         }
493
494         Dwarf_Die next_die;
495
496         switch (dwarf_tag(type_die)) {
497         case DW_TAG_base_type:
498                 *info = type_get_simple( get_base_type( type_die ));
499                 complain(type_die, "Storing base type: %p", *info);
500                 dict_insert( &type_hash, &die_offset, info );
501                 return true;
502
503         case DW_TAG_subroutine_type:
504         case DW_TAG_inlined_subroutine:
505                 // function pointers are stored as void*. If ltrace tries to dereference
506                 // these, it'll get a segfault
507                 *info = type_get_simple( ARGTYPE_VOID );
508                 complain(type_die, "Storing subroutine type: %p", *info);
509                 dict_insert( &type_hash, &die_offset, info );
510                 return true;
511
512         case DW_TAG_pointer_type:
513
514                 if (!get_type_die(&next_die, type_die )) {
515                         // the pointed-to type isn't defined, so I report a void*
516                         *info = type_get_simple( ARGTYPE_VOID );
517                         complain(type_die, "Storing void-pointer type: %p", *info);
518                         dict_insert( &type_hash, &die_offset, info );
519                         return true;
520                 }
521
522                 *info = calloc( 1, sizeof(struct arg_type_info));
523                 if (*info == NULL) {
524                         complain(type_die, "alloc error");
525                         return false;
526                 }
527                 type_init_pointer(*info, NULL, 0);
528
529                 complain(type_die, "Storing pointer type: %p", *info);
530                 dict_insert( &type_hash, &die_offset, info );
531                 return get_type( &(*info)->u.ptr_info.info, &next_die );
532
533         case DW_TAG_structure_type:
534                 *info = calloc( 1, sizeof(struct arg_type_info));
535                 if (*info == NULL) {
536                         complain(type_die, "alloc error");
537                         return false;
538                 }
539
540                 complain(type_die, "Storing struct type: %p", *info);
541                 dict_insert( &type_hash, &die_offset, info );
542                 return get_structure( *info, type_die );
543
544
545         case DW_TAG_typedef: ;
546         case DW_TAG_const_type: ;
547         case DW_TAG_volatile_type: ;
548                 // Various tags are simply pass-through, so I just keep going
549                 bool res = true;
550                 if (get_type_die(&next_die, type_die )) {
551                         complain(type_die, "Storing const/typedef type: %p", *info);
552                         res = get_type( info, &next_die );
553                 } else {
554                         // no type. Use 'void'. Normally I'd think this is bogus, but stdio
555                         // typedefs something to void
556                         *info = type_get_simple( ARGTYPE_VOID );
557                         complain(type_die, "Storing void type: %p", *info);
558                 }
559                 if (res )
560                         dict_insert( &type_hash, &die_offset, info );
561                 return res;
562
563         case DW_TAG_enumeration_type:
564                 // We have an enumeration. This has type "int", but has a particular
565                 // lens to handle the enum
566                 *info = calloc( 1, sizeof(struct arg_type_info));
567                 if (*info == NULL) {
568                         complain(type_die, "alloc error");
569                         return false;
570                 }
571
572                 complain(type_die, "Storing enum int: %p", *info);
573                 dict_insert( &type_hash, &die_offset, info );
574                 return get_enum( *info, type_die );
575
576         case DW_TAG_array_type:
577                 *info = calloc( 1, sizeof(struct arg_type_info));
578                 if (*info == NULL) {
579                         complain(type_die, "alloc error");
580                         return false;
581                 }
582
583                 complain(type_die, "Storing array: %p", *info);
584                 dict_insert( &type_hash, &die_offset, info );
585                 return get_array( *info, type_die );
586
587         case DW_TAG_union_type:
588                 *info = type_get_simple( ARGTYPE_VOID );
589                 complain(type_die, "Storing union-as-void type: %p", *info);
590                 return true;
591
592         default:
593                 complain(type_die, "Unknown type tag 0x%x", dwarf_tag(type_die));
594                 break;
595         }
596
597         return false;
598 }
599
600 static bool get_prototype(struct prototype* proto, Dwarf_Die* subroutine)
601 {
602         // First, look at the return type. This is stored in a DW_AT_type tag in the
603         // subroutine DIE. If there is no such tag, this function returns void
604         Dwarf_Die return_type_die;
605         if (!get_type_die(&return_type_die, subroutine )) {
606                 proto->return_info = type_get_simple( ARGTYPE_VOID );
607                 proto->own_return_info = 0;
608         } else {
609                 proto->return_info = calloc( 1, sizeof( struct arg_type_info ));
610                 if (proto->return_info == NULL) {
611                         complain(subroutine, "Couldn't alloc return type");
612                         return false;
613                 }
614                 proto->own_return_info = 0;
615
616                 if (!get_type( &proto->return_info, &return_type_die )) {
617                         complain(subroutine, "Couldn't get return type");
618                         return false;
619                 }
620         }
621
622
623         // Now look at the arguments
624         Dwarf_Die arg_die;
625         if (dwarf_child(subroutine, &arg_die) != 0) {
626                 // no args. We're done
627                 return true;
628         }
629
630         while(1) {
631                 if (dwarf_tag(&arg_die) != DW_TAG_formal_parameter )
632                         goto next_prototype_argument;
633
634                 complain(&arg_die, "arg: 0x%02x", dwarf_tag(&arg_die));
635
636                 Dwarf_Die type_die;
637                 if (!get_type_die(&type_die, &arg_die )) {
638                         complain(&arg_die, "Couldn't get the argument type die");
639                         return false;
640                 }
641
642                 struct arg_type_info* arg_type_info = NULL;
643                 if (!get_type( &arg_type_info, &type_die )) {
644                         complain(&arg_die, "Couldn't parse arg type from DWARF data");
645                         return false;
646                 }
647
648                 struct param param;
649                 param_init_type(&param, arg_type_info, 0);
650                 if (prototype_push_param(proto, &param) <0) {
651                         complain(&arg_die, "couldn't add argument to the prototype");
652                         return false;
653                 }
654
655 #ifdef DUMP_PROTOTYPES
656                 fprintf(stderr, "Adding argument:\n");
657                 dump_ltrace_tree(arg_type_info);
658 #endif
659
660         next_prototype_argument: ;
661                 int res = dwarf_siblingof(&arg_die, &arg_die);
662                 if (res == 0) continue;     /* sibling exists    */
663                 if (res < 0)  return false; /* error             */
664                 break;                      /* no sibling exists */
665         }
666
667         return true;
668 }
669
670 static bool process_die_compileunit(struct protolib* plib, struct library* lib,
671                                                                         Dwarf_Die* parent)
672 {
673         Dwarf_Die die;
674         if (dwarf_child(parent, &die) != 0) {
675                 // no child nodes, so nothing to do
676                 return true;
677         }
678
679         while (1) {
680                 if (dwarf_tag(&die) == DW_TAG_subprogram) {
681                         const char* function_name = dwarf_diename(&die);
682
683                         complain(&die, "subroutine_type: 0x%02x; function '%s'",
684                                          dwarf_tag(&die), function_name);
685
686                         struct prototype* proto =
687                                 protolib_lookup_prototype(plib, function_name, true );
688
689                         if (proto != NULL) {
690                                 complain(&die, "Prototype already exists. Skipping");
691                                 goto next_prototype;
692                         }
693
694                         if (!filter_matches_symbol(options.plt_filter,    function_name, lib) &&
695                                 !filter_matches_symbol(options.static_filter, function_name, lib) &&
696                                 !filter_matches_symbol(options.export_filter, function_name, lib)) {
697                                 complain(&die, "Prototype not requested by any filter");
698                                 goto next_prototype;
699                         }
700
701                         proto = malloc(sizeof(struct prototype));
702                         if (proto == NULL) {
703                                 complain(&die, "couldn't alloc prototype");
704                                 return false;
705                         }
706                         prototype_init( proto );
707
708                         if (!get_prototype(proto, &die )) {
709                                 complain(&die, "couldn't get prototype");
710                                 return false;
711                         }
712
713                         protolib_add_prototype(plib, function_name, 0, proto);
714                 }
715
716                 next_prototype:;
717                 int res = dwarf_siblingof(&die, &die);
718                 if (res == 0) continue;     /* sibling exists    */
719                 if (res < 0)  return false; /* error             */
720                 break;                      /* no sibling exists */
721         }
722
723         return true;
724 }
725
726 static bool import(struct protolib* plib, struct library* lib, Dwfl* dwfl)
727 {
728         dict_init(&type_hash, sizeof(Dwarf_Off), sizeof(struct arg_type_info*),
729                           dwarf_die_hash, dwarf_die_eq, NULL );
730
731         Dwarf_Addr bias;
732     Dwarf_Die* die = NULL;
733     while ((die = dwfl_nextcu(dwfl, die, &bias)) != NULL) {
734         if (dwarf_tag(die) == DW_TAG_compile_unit) {
735             if (!process_die_compileunit(plib, lib, die)) {
736                 complain(die, "Error reading compile unit");
737                                 exit(1);
738                                 return false;
739             }
740         } else {
741             complain(die, "DW_TAG_compile_unit expected");
742                         exit(1);
743             return false;
744         }
745     }
746
747         dict_destroy( &type_hash, NULL, NULL, NULL );
748         return true;
749 }
750
751 bool import_DWARF_prototypes(struct protolib* plib, struct library* lib,
752                                                          Dwfl *dwfl)
753 {
754         if (plib == NULL) {
755                 plib = protolib_cache_default(&g_protocache, lib->soname, 0);
756                 if (plib == NULL) {
757                         fprintf(stderr, "Error loading protolib %s: %s.\n",
758                                         lib->soname, strerror(errno));
759                 }
760         }
761
762         return import(plib, lib, dwfl);
763 }
764
765 /*
766 - I handle static functions now. Should I? Those do not have DW_AT_external==1
767
768 - should process existing prototypes to make sure they match
769
770 - what do function pointers look like? I'm doing void*
771
772 - unions
773
774 - all my *allocs leak
775
776 - protolib_lookup_prototype should look for imports?
777
778 */