Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
972d3e96078cc7d1edc784bec1d286fb58871842
[simgrid.git] / src / mc / mc_dwarf.c
1 /* Copyright (c) 2008-2013. The SimGrid Team.
2  * All rights reserved.                                                     */
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <stdlib.h>
7 #include <dwarf.h>
8 #include <elfutils/libdw.h>
9 #include <inttypes.h>
10
11 #include <simgrid_config.h>
12 #include <xbt/log.h>
13 #include <xbt/sysdep.h>
14
15 #include "mc_private.h"
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dwarf, mc, "DWARF processing");
18
19 /** \brief The default DW_TAG_lower_bound for a given DW_AT_language.
20  *
21  *  The default for a given language is defined in the DWARF spec.
22  *
23  *  \param language consant as defined by the DWARf spec
24  */
25 static uint64_t MC_dwarf_default_lower_bound(int lang);
26
27 /** \brief Computes the the element_count of a DW_TAG_enumeration_type DIE
28  *
29  * This is the number of elements in a given array dimension.
30  *
31  * A reference of the compilation unit (DW_TAG_compile_unit) is
32  * needed because the default lower bound (when there is no DW_AT_lower_bound)
33  * depends of the language of the compilation unit (DW_AT_language).
34  *
35  * \param die  DIE for the DW_TAG_enumeration_type or DW_TAG_subrange_type
36  * \param unit DIE of the DW_TAG_compile_unit
37  */
38 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die* die, Dwarf_Die* unit);
39
40 /** \brief Computes the number of elements of a given DW_TAG_array_type.
41  *
42  * \param die DIE for the DW_TAG_array_type
43  */
44 static uint64_t MC_dwarf_array_element_count(Dwarf_Die* die, Dwarf_Die* unit);
45
46 /** \brief Process a DIE
47  *
48  *  \param info the resulting object fot the library/binary file (output)
49  *  \param die  the current DIE
50  *  \param unit the DIE of the compile unit of the current DIE
51  *  \param frame containg frame if any
52  */
53 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace);
54
55 /** \brief Process a type DIE
56  */
57 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace);
58
59 /** \brief Calls MC_dwarf_handle_die on all childrend of the given die
60  *
61  *  \param info the resulting object fot the library/binary file (output)
62  *  \param die  the current DIE
63  *  \param unit the DIE of the compile unit of the current DIE
64  *  \param frame containg frame if any
65  */
66 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace);
67
68 /** \brief Handle a variable (DW_TAG_variable or other)
69  *
70  *  \param info the resulting object fot the library/binary file (output)
71  *  \param die  the current DIE
72  *  \param unit the DIE of the compile unit of the current DIE
73  *  \param frame containg frame if any
74  */
75 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace);
76
77 /** \brief Convert a libdw DWARF expression into a MC representation of the location
78  *
79  *  \param expr array of DWARf operations
80  *  \param len  number of elements
81  *  \return a new MC expression
82  */
83 static dw_location_t MC_dwarf_get_expression(Dwarf_Op* expr,  size_t len);
84
85 /** \brief Get the DW_TAG_type of the DIE
86  *
87  *  \param die DIE
88  *  \return DW_TAG_type attribute as a new string (NULL if none)
89  */
90 static char* MC_dwarf_at_type(Dwarf_Die* die);
91
92 /** \brief Get the name of an attribute (DW_AT_*) from its code
93  *
94  *  \param attr attribute code (see the DWARF specification)
95  *  \return name of the attribute
96  */
97 const char* MC_dwarf_attrname(int attr) {
98   switch (attr) {
99 #include "mc_dwarf_attrnames.h"
100   default:
101     return "DW_AT_unknown";
102   }
103 }
104
105 /** \brief Get the name of a dwarf tag (DW_TAG_*) from its code
106  *
107  *  \param tag tag code (see the DWARF specification)
108  *  \return name of the tag
109  */
110 const char* MC_dwarf_tagname(int tag) {
111   switch (tag) {
112 #include "mc_dwarf_tagnames.h"
113   case DW_TAG_invalid:
114     return "DW_TAG_invalid";
115   default:
116     return "DW_TAG_unknown";
117   }
118 }
119
120 /** \brief A class of DWARF tags (DW_TAG_*)
121  */
122 typedef enum mc_tag_class {
123   mc_tag_unknown,
124   mc_tag_type,
125   mc_tag_subprogram,
126   mc_tag_variable,
127   mc_tag_scope,
128   mc_tag_namespace
129 } mc_tag_class;
130
131 static mc_tag_class MC_dwarf_tag_classify(int tag) {
132   switch (tag) {
133
134     case DW_TAG_array_type:
135     case DW_TAG_class_type:
136     case DW_TAG_enumeration_type:
137     case DW_TAG_typedef:
138     case DW_TAG_pointer_type:
139     case DW_TAG_reference_type:
140     case DW_TAG_rvalue_reference_type:
141     case DW_TAG_string_type:
142     case DW_TAG_structure_type:
143     case DW_TAG_subroutine_type:
144     case DW_TAG_union_type:
145     case DW_TAG_ptr_to_member_type:
146     case DW_TAG_set_type:
147     case DW_TAG_subrange_type:
148     case DW_TAG_base_type:
149     case DW_TAG_const_type:
150     case DW_TAG_file_type:
151     case DW_TAG_packed_type:
152     case DW_TAG_volatile_type:
153     case DW_TAG_restrict_type:
154     case DW_TAG_interface_type:
155     case DW_TAG_unspecified_type:
156     case DW_TAG_mutable_type:
157     case DW_TAG_shared_type:
158       return mc_tag_type;
159
160     case DW_TAG_subprogram:
161       return mc_tag_subprogram;
162
163     case DW_TAG_variable:
164     case DW_TAG_formal_parameter:
165       return mc_tag_variable;
166
167     case DW_TAG_lexical_block:
168     case DW_TAG_try_block:
169     case DW_TAG_inlined_subroutine:
170       return mc_tag_scope;
171
172     case DW_TAG_namespace:
173       return mc_tag_namespace;
174
175     default:
176       return mc_tag_unknown;
177
178   }
179 }
180
181 #define MC_DW_CLASS_UNKNOWN 0
182 #define MC_DW_CLASS_ADDRESS 1   // Location in the address space of the program
183 #define MC_DW_CLASS_BLOCK 2     // Arbitrary block of bytes
184 #define MC_DW_CLASS_CONSTANT 3
185 #define MC_DW_CLASS_STRING 3    // String
186 #define MC_DW_CLASS_FLAG 4      // Boolean
187 #define MC_DW_CLASS_REFERENCE 5 // Reference to another DIE
188 #define MC_DW_CLASS_EXPRLOC 6   // DWARF expression/location description
189 #define MC_DW_CLASS_LINEPTR 7
190 #define MC_DW_CLASS_LOCLISTPTR 8
191 #define MC_DW_CLASS_MACPTR 9
192 #define MC_DW_CLASS_RANGELISTPTR 10
193
194 static int MC_dwarf_form_get_class(int form) {
195   switch(form) {
196   case DW_FORM_addr:
197     return MC_DW_CLASS_ADDRESS;
198   case DW_FORM_block2:
199   case DW_FORM_block4:
200   case DW_FORM_block:
201   case DW_FORM_block1:
202     return MC_DW_CLASS_BLOCK;
203   case DW_FORM_data1:
204   case DW_FORM_data2:
205   case DW_FORM_data4:
206   case DW_FORM_data8:
207   case DW_FORM_udata:
208   case DW_FORM_sdata:
209     return MC_DW_CLASS_CONSTANT;
210   case DW_FORM_string:
211   case DW_FORM_strp:
212     return MC_DW_CLASS_STRING;
213   case DW_FORM_ref_addr:
214   case DW_FORM_ref1:
215   case DW_FORM_ref2:
216   case DW_FORM_ref4:
217   case DW_FORM_ref8:
218   case DW_FORM_ref_udata:
219     return MC_DW_CLASS_REFERENCE;
220   case DW_FORM_flag:
221   case DW_FORM_flag_present:
222     return MC_DW_CLASS_FLAG;
223   case DW_FORM_exprloc:
224     return MC_DW_CLASS_EXPRLOC;
225   // TODO sec offset
226   // TODO indirect
227   default:
228     return MC_DW_CLASS_UNKNOWN;
229   }
230 }
231
232 /** \brief Get the name of the tag of a given DIE
233  *
234  *  \param die DIE
235  *  \return name of the tag of this DIE
236  */
237 static inline const char* MC_dwarf_die_tagname(Dwarf_Die* die) {
238   return MC_dwarf_tagname(dwarf_tag(die));
239 }
240
241 // ***** Attributes
242
243 /** \brief Get an attribute of a given DIE as a string
244  *
245  *  \param the DIE
246  *  \param attribute attribute
247  *  \return value of the given attribute of the given DIE
248  */
249 static const char* MC_dwarf_attr_string(Dwarf_Die* die, int attribute) {
250   Dwarf_Attribute attr;
251   if (!dwarf_attr_integrate(die, attribute, &attr)) {
252         return NULL;
253   } else {
254         return dwarf_formstring(&attr);
255   }
256 }
257
258 /** \brief Get the linkage name of a DIE.
259  *
260  *  Use either DW_AT_linkage_name or DW_AR_MIPS_linkage_name.
261  *
262  *  \param DIE
263  *  \return linkage name of the given DIE (or NULL)
264  * */
265 static const char* MC_dwarf_at_linkage_name(Dwarf_Die* die) {
266   const char* name = MC_dwarf_attr_string(die, DW_AT_linkage_name);
267   if (!name)
268     name = MC_dwarf_attr_string(die, DW_AT_MIPS_linkage_name);
269   return name;
270 }
271
272 /** \brief Create a location list from a given attribute
273  *
274  *  \param die the DIE
275  *  \param attr the attribute
276  *  \return MC specific representation of the location list represented by the given attribute
277  *  of the given die
278  */
279 static dw_location_t MC_dwarf_get_location_list(mc_object_info_t info, Dwarf_Die* die, Dwarf_Attribute* attr) {
280
281   dw_location_t location = xbt_new0(s_dw_location_t, 1);
282   location->type = e_dw_loclist;
283   xbt_dynar_t loclist = xbt_dynar_new(sizeof(dw_location_entry_t), NULL);
284   location->location.loclist = loclist;
285
286   ptrdiff_t offset = 0;
287   Dwarf_Addr base, start, end;
288   Dwarf_Op *expr;
289   size_t len;
290
291   while (1) {
292
293     offset = dwarf_getlocations(attr, offset, &base, &start, &end, &expr, &len);
294     if (offset==0)
295       return location;
296     else if (offset==-1)
297       xbt_die("Error while loading location list");
298
299     dw_location_entry_t new_entry = xbt_new0(s_dw_location_entry_t, 1);
300
301     void* base = info->flags & MC_OBJECT_INFO_EXECUTABLE ? 0 : MC_object_base_address(info);
302
303     new_entry->lowpc = (char*) base + start;
304     new_entry->highpc = (char*) base + end;
305     new_entry->location = MC_dwarf_get_expression(expr, len);
306
307     xbt_dynar_push(loclist, &new_entry);
308
309   }
310 }
311
312 /** \brief Get the location expression or location list from an attribute
313  *
314  *  Processes direct expressions as well as location lists.
315  *
316  *  \param die the DIE
317  *  \param attr the attribute
318  *  \return MC specific representation of the location represented by the given attribute
319  *  of the given die
320  */
321 static dw_location_t MC_dwarf_get_location(mc_object_info_t info, Dwarf_Die* die, Dwarf_Attribute* attr) {
322   int form = dwarf_whatform(attr);
323   switch (form) {
324
325   // The attribute is an DWARF location expression:
326   case DW_FORM_exprloc:
327   case DW_FORM_block1: // not in the spec
328   case DW_FORM_block2:
329   case DW_FORM_block4:
330   case DW_FORM_block:
331     {
332       Dwarf_Op* expr;
333       size_t len;
334       if (dwarf_getlocation(attr, &expr, &len))
335         xbt_die("Could not read location expression");
336       return MC_dwarf_get_expression(expr, len);
337     }
338
339   // The attribute is a reference to a location list entry:
340   case DW_FORM_sec_offset:
341   case DW_FORM_data1:
342   case DW_FORM_data2:
343   case DW_FORM_data4:
344   case DW_FORM_data8:
345     {
346       return MC_dwarf_get_location_list(info, die, attr);
347     }
348     break;
349
350   default:
351     xbt_die("Unexpected form %i list for location in attribute %s of <%p>%s",
352       form,
353       MC_dwarf_attrname(attr->code),
354       (void*) dwarf_dieoffset(die),
355       MC_dwarf_attr_string(die, DW_AT_name));
356     return NULL;
357   }
358 }
359
360 /** \brief Get the location expression or location list from an attribute
361  *
362  *  Processes direct expressions as well as location lists.
363  *
364  *  \param die the DIE
365  *  \param attribute the attribute code
366  *  \return MC specific representation of the location represented by the given attribute
367  *  of the given die
368  */
369 static dw_location_t MC_dwarf_at_location(mc_object_info_t info, Dwarf_Die* die, int attribute) {
370   if(!dwarf_hasattr_integrate(die, attribute))
371     return xbt_new0(s_dw_location_t, 1);
372
373   Dwarf_Attribute attr;
374   dwarf_attr_integrate(die, attribute, &attr);
375   return MC_dwarf_get_location(info, die, &attr);
376 }
377
378 static char* MC_dwarf_at_type(Dwarf_Die* die) {
379   Dwarf_Attribute attr;
380   if (dwarf_hasattr_integrate(die, DW_AT_type)) {
381         dwarf_attr_integrate(die, DW_AT_type, &attr);
382         Dwarf_Die subtype_die;
383         if (dwarf_formref_die(&attr, &subtype_die)==NULL) {
384           xbt_die("Could not find DIE for type");
385         }
386         Dwarf_Off subtype_global_offset = dwarf_dieoffset(&subtype_die);
387     return bprintf("%" PRIx64 , subtype_global_offset);
388   }
389   else return NULL;
390 }
391
392 static uint64_t MC_dwarf_attr_addr(Dwarf_Die* die, int attribute) {
393   Dwarf_Attribute attr;
394   if(dwarf_attr_integrate(die, attribute, &attr)==NULL)
395     return 0;
396   Dwarf_Addr value;
397   if (dwarf_formaddr(&attr, &value) == 0)
398     return (uint64_t) value;
399   else
400     return 0;
401 }
402
403 static uint64_t MC_dwarf_attr_uint(Dwarf_Die* die, int attribute, uint64_t default_value) {
404   Dwarf_Attribute attr;
405   if (dwarf_attr_integrate(die, attribute, &attr)==NULL)
406     return default_value;
407   Dwarf_Word value;
408   return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr), &value) == 0 ? (uint64_t) value : default_value;
409 }
410
411 static bool MC_dwarf_attr_flag(Dwarf_Die* die, int attribute, int integrate) {
412   Dwarf_Attribute attr;
413   if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
414                     : dwarf_attr(die, attribute, &attr))==0)
415     return false;
416
417   bool result;
418   if (dwarf_formflag(&attr, &result))
419     xbt_die("Unexpected form for attribute %s",
420       MC_dwarf_attrname(attribute));
421   return result;
422 }
423
424 static uint64_t MC_dwarf_default_lower_bound(int lang) {
425   switch(lang) {
426   case DW_LANG_C:
427   case DW_LANG_C89:
428   case DW_LANG_C99:
429   case DW_LANG_C_plus_plus:
430   case DW_LANG_D:
431   case DW_LANG_Java:
432   case DW_LANG_ObjC:
433   case DW_LANG_ObjC_plus_plus:
434   case DW_LANG_Python:
435   case DW_LANG_UPC:
436     return 0;
437   case DW_LANG_Ada83:
438   case DW_LANG_Ada95:
439   case DW_LANG_Fortran77:
440   case DW_LANG_Fortran90:
441   case DW_LANG_Fortran95:
442   case DW_LANG_Modula2:
443   case DW_LANG_Pascal83:
444   case DW_LANG_PL1:
445   case DW_LANG_Cobol74:
446   case DW_LANG_Cobol85:
447     return 1;
448   default:
449     xbt_die("No default MT_TAG_lower_bound for language %i and none given", lang);
450     return 0;
451   }
452 }
453
454 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
455   xbt_assert(dwarf_tag(die)==DW_TAG_enumeration_type ||dwarf_tag(die)==DW_TAG_subrange_type,
456       "MC_dwarf_subrange_element_count called with DIE of type %s", MC_dwarf_die_tagname(die));
457
458   // Use DW_TAG_count if present:
459   if (dwarf_hasattr_integrate(die, DW_AT_count)) {
460     return MC_dwarf_attr_uint(die, DW_AT_count, 0);
461   }
462
463   // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
464
465   if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
466         // This is not really 0, but the code expects this (we do not know):
467     return 0;
468   }
469   uint64_t upper_bound = MC_dwarf_attr_uint(die, DW_AT_upper_bound, -1);
470
471   uint64_t lower_bound = 0;
472   if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
473     lower_bound = MC_dwarf_attr_uint(die, DW_AT_lower_bound, -1);
474   } else {
475         lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
476   }
477   return upper_bound - lower_bound + 1;
478 }
479
480 static uint64_t MC_dwarf_array_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
481   xbt_assert(dwarf_tag(die)==DW_TAG_array_type,
482     "MC_dwarf_array_element_count called with DIE of type %s", MC_dwarf_die_tagname(die));
483
484   int result = 1;
485   Dwarf_Die child;
486   int res;
487   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
488         int child_tag = dwarf_tag(&child);
489     if (child_tag==DW_TAG_subrange_type ||child_tag==DW_TAG_enumeration_type) {
490       result *= MC_dwarf_subrange_element_count(&child, unit);
491     }
492   }
493   return result;
494 }
495
496 // ***** Location
497
498 Dwarf_Off MC_dwarf_resolve_location(unw_cursor_t* c, dw_location_t location, void* frame_pointer_address) {
499   unw_word_t res;
500   switch (location->type){
501   case e_dw_compose:
502     if (xbt_dynar_length(location->location.compose) > 1){
503       return 0; /* TODO : location list with optimizations enabled */
504     }
505     dw_location_t location_entry = xbt_dynar_get_as(location->location.compose, 0, dw_location_t);
506     switch (location_entry->type){
507     case e_dw_bregister_op:
508       unw_get_reg(c, location_entry->location.breg_op.reg, &res);
509       return (Dwarf_Off) ((long)res + location_entry->location.breg_op.offset);
510       break;
511     case e_dw_fbregister_op:
512       if (frame_pointer_address != NULL)
513         return (Dwarf_Off)((char *)frame_pointer_address + location_entry->location.fbreg_op);
514       else
515         return 0;
516     default:
517       return 0; /* FIXME : implement other cases (with optimizations enabled) */
518     }
519     break;
520     default:
521       return 0;
522   }
523 }
524
525 // ***** dw_type_t
526
527 static void MC_dwarf_fill_member_location(dw_type_t type, dw_type_t member, Dwarf_Die* child) {
528   if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
529     xbt_die("Can't groke DW_AT_data_bit_offset.");
530   }
531
532   if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
533     if (type->type != DW_TAG_union_type) {
534         xbt_die(
535           "Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%p>%s",
536           member->name, type->id, type->name);
537     } else {
538       return;
539     }
540   }
541
542   Dwarf_Attribute attr;
543   dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
544   int form = dwarf_whatform(&attr);
545   int klass = MC_dwarf_form_get_class(form);
546   switch (klass) {
547   case MC_DW_CLASS_EXPRLOC:
548   case MC_DW_CLASS_BLOCK:
549     // Location expression:
550     {
551       Dwarf_Op* expr;
552       size_t len;
553       if (dwarf_getlocation(&attr, &expr, &len)) {
554         xbt_die(
555           "Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%p>%s",
556           MC_dwarf_attr_string(child, DW_AT_name),
557           type->id, type->name);
558       }
559       if (len==1 && expr[0].atom == DW_OP_plus_uconst) {
560         member->offset =  expr[0].number;
561       } else {
562         xbt_die("Can't groke this location expression yet.");
563       }
564       break;
565     }
566   case MC_DW_CLASS_CONSTANT:
567     // Offset from the base address of the object:
568     {
569       Dwarf_Word offset;
570       if (!dwarf_formudata(&attr, &offset))
571         member->offset = offset;
572       else
573         xbt_die("Cannot get %s location <%p>%s",
574           MC_dwarf_attr_string(child, DW_AT_name),
575           type->id, type->name);
576       break;
577     }
578   case MC_DW_CLASS_LOCLISTPTR:
579     // Reference to a location list:
580     // TODO
581   case MC_DW_CLASS_REFERENCE:
582     // It's supposed to be possible in DWARF2 but I couldn't find its semantic
583     // in the spec.
584   default:
585     xbt_die(
586       "Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
587       klass, form);
588   }
589
590 }
591
592 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_type_t type) {
593   int res;
594   Dwarf_Die child;
595   xbt_assert(!type->members);
596   type->members = xbt_dynar_new(sizeof(dw_type_t), (void(*)(void*))dw_type_free);
597   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
598     if (dwarf_tag(&child)==DW_TAG_member) {
599
600       // Skip declarations:
601       if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
602         continue;
603
604       // Skip compile time constants:
605       if(dwarf_hasattr(&child, DW_AT_const_value))
606         continue;
607
608       // TODO, we should use another type (because is is not a type but a member)
609       dw_type_t member = xbt_new0(s_dw_type_t, 1);
610       member->type = -1;
611       member->id = NULL;
612
613       const char* name = MC_dwarf_attr_string(&child, DW_AT_name);
614       if(name)
615         member->name = xbt_strdup(name);
616       else
617         member->name = NULL;
618
619       member->byte_size = MC_dwarf_attr_uint(&child, DW_AT_byte_size, 0);
620       member->element_count = -1;
621       member->dw_type_id = MC_dwarf_at_type(&child);
622       member->members = NULL;
623       member->is_pointer_type = 0;
624       member->offset = 0;
625
626       if(dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
627         xbt_die("Can't groke DW_AT_data_bit_offset.");
628       }
629
630       MC_dwarf_fill_member_location(type, member, &child);
631
632       if (!member->dw_type_id) {
633         xbt_die("Missing type for member %s of <%p>%s", member->name, type->id, type->name);
634       }
635
636       xbt_dynar_push(type->members, &member);
637     }
638   }
639 }
640
641 /** \brief Create a MC type object from a DIE
642  *
643  *  \param info current object info object
644  *  \param DIE (for a given type);
645  *  \param unit compilation unit of the current DIE
646  *  \return MC representation of the type
647  */
648 static dw_type_t MC_dwarf_die_to_type(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
649
650   dw_type_t type = xbt_new0(s_dw_type_t, 1);
651   type->type = -1;
652   type->id = NULL;
653   type->name = NULL;
654   type->byte_size = 0;
655   type->element_count = -1;
656   type->dw_type_id = NULL;
657   type->members = NULL;
658   type->is_pointer_type = 0;
659   type->offset = 0;
660
661   type->type = dwarf_tag(die);
662
663   // Global Offset
664   type->id = (void *) dwarf_dieoffset(die);
665
666   const char* name = MC_dwarf_attr_string(die, DW_AT_name);
667   if (name!=NULL) {
668         type->name = xbt_strdup(name);
669   }
670
671   XBT_DEBUG("Processing type <%p>%s", type->id, type->name);
672
673   type->dw_type_id = MC_dwarf_at_type(die);
674
675   // Computation of the byte_size;
676   if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
677     type->byte_size = MC_dwarf_attr_uint(die, DW_AT_byte_size, 0);
678   else if (type->type == DW_TAG_array_type || type->type==DW_TAG_structure_type || type->type==DW_TAG_class_type) {
679     Dwarf_Word size;
680     if (dwarf_aggregate_size(die, &size)==0) {
681       type->byte_size = size;
682     }
683   }
684
685   switch (type->type) {
686   case DW_TAG_array_type:
687         type->element_count = MC_dwarf_array_element_count(die, unit);
688         // TODO, handle DW_byte_stride and (not) DW_bit_stride
689         break;
690
691   case DW_TAG_pointer_type:
692   case DW_TAG_reference_type:
693   case DW_TAG_rvalue_reference_type:
694     type->is_pointer_type = 1;
695     break;
696
697   case DW_TAG_structure_type:
698   case DW_TAG_union_type:
699   case DW_TAG_class_type:
700           MC_dwarf_add_members(info, die, unit, type);
701           char* new_namespace = namespace == NULL ? xbt_strdup(type->name)
702             : bprintf("%s::%s", namespace, type->name);
703           MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
704           free(new_namespace);
705           break;
706   }
707
708   return type;
709 }
710
711 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
712   dw_type_t type = MC_dwarf_die_to_type(info, die, unit, frame, namespace);
713
714   char* key = bprintf("%" PRIx64, (uint64_t) type->id);
715   xbt_dict_set(info->types, key, type, NULL);
716
717   if(type->name && type->byte_size!=0) {
718     xbt_dict_set(info->types_by_name, type->name, type, NULL);
719   }
720 }
721
722 /** \brief Convert libdw location expresion elment into native one (or NULL in some cases) */
723 static dw_location_t MC_dwarf_get_expression_element(Dwarf_Op* op) {
724   dw_location_t element = xbt_new0(s_dw_location_t, 1);
725   uint8_t atom = op->atom;
726   if (atom >= DW_OP_reg0 && atom<= DW_OP_reg31) {
727     element->type = e_dw_register;
728     element->location.reg = atom - DW_OP_reg0;
729   }
730   else if (atom >= DW_OP_breg0 && atom<= DW_OP_breg31) {
731     element->type = e_dw_bregister_op;
732     element->location.reg = atom - DW_OP_breg0;
733     element->location.breg_op.offset = op->number;
734   }
735   else if (atom >= DW_OP_lit0 && atom<= DW_OP_lit31) {
736     element->type = e_dw_lit;
737     element->location.reg = atom - DW_OP_lit0;
738   }
739   else switch (atom) {
740   case DW_OP_fbreg:
741     element->type = e_dw_fbregister_op;
742     element->location.fbreg_op = op->number;
743     break;
744   case DW_OP_piece:
745     element->type = e_dw_piece;
746     element->location.piece = op->number;
747     break;
748   case DW_OP_plus_uconst:
749     element->type = e_dw_plus_uconst;
750     element->location.plus_uconst = op->number;
751     break;
752   case DW_OP_abs:
753     element->type = e_dw_arithmetic;
754     element->location.arithmetic = xbt_strdup("abs");
755     break;
756   case DW_OP_and:
757     element->type = e_dw_arithmetic;
758     element->location.arithmetic = xbt_strdup("and");
759     break;
760   case DW_OP_div:
761     element->type = e_dw_arithmetic;
762     element->location.arithmetic = xbt_strdup("div");
763     break;
764   case DW_OP_minus:
765     element->type = e_dw_arithmetic;
766     element->location.arithmetic = xbt_strdup("minus");
767     break;
768   case DW_OP_mod:
769     element->type = e_dw_arithmetic;
770     element->location.arithmetic = xbt_strdup("mod");
771     break;
772   case DW_OP_mul:
773     element->type = e_dw_arithmetic;
774     element->location.arithmetic = xbt_strdup("mul");
775     break;
776   case DW_OP_neg:
777     element->type = e_dw_arithmetic;
778     element->location.arithmetic = xbt_strdup("neg");
779     break;
780   case DW_OP_not:
781     element->type = e_dw_arithmetic;
782     element->location.arithmetic = xbt_strdup("not");
783     break;
784   case DW_OP_or:
785     element->type = e_dw_arithmetic;
786     element->location.arithmetic = xbt_strdup("or");
787     break;
788   case DW_OP_plus:
789     element->type = e_dw_arithmetic;
790     element->location.arithmetic = xbt_strdup("plus");
791     break;
792
793   case DW_OP_stack_value:
794     // Why nothing here?
795     xbt_free(element);
796     return NULL;
797
798   case DW_OP_deref_size:
799     element->type = e_dw_deref;
800     element->location.deref_size =  (unsigned int short) op->number;
801     break;
802   case DW_OP_deref:
803     element->type = e_dw_deref;
804     element->location.deref_size = sizeof(void *);
805     break;
806   case DW_OP_constu:
807     element->type = e_dw_uconstant;
808     element->location.uconstant.bytes = 1;
809     element->location.uconstant.value = (unsigned long int) op->number;
810     break;
811   case DW_OP_consts:
812     element->type = e_dw_sconstant;
813     element->location.uconstant.bytes = 1;
814     element->location.uconstant.value = (unsigned long int) op->number;
815     break;
816
817   case DW_OP_const1u:
818     element->type = e_dw_uconstant;
819     element->location.uconstant.bytes = 1;
820     element->location.uconstant.value = (unsigned long int) op->number;
821     break;
822   case DW_OP_const2u:
823     element->type = e_dw_uconstant;
824     element->location.uconstant.bytes = 2;
825     element->location.uconstant.value = (unsigned long int) op->number;
826     break;
827   case DW_OP_const4u:
828     element->type = e_dw_uconstant;
829     element->location.uconstant.bytes = 4;
830     element->location.uconstant.value = (unsigned long int) op->number;
831     break;
832   case DW_OP_const8u:
833     element->type = e_dw_uconstant;
834     element->location.uconstant.bytes = 8;
835     element->location.uconstant.value = (unsigned long int) op->number;
836     break;
837
838   case DW_OP_const1s:
839     element->type = e_dw_sconstant;
840     element->location.uconstant.bytes = 1;
841     element->location.uconstant.value = (unsigned long int) op->number;
842     break;
843   case DW_OP_const2s:
844     element->type = e_dw_sconstant;
845     element->location.uconstant.bytes = 2;
846     element->location.uconstant.value = (unsigned long int) op->number;
847     break;
848   case DW_OP_const4s:
849     element->type = e_dw_sconstant;
850     element->location.uconstant.bytes = 4;
851     element->location.uconstant.value = (unsigned long int) op->number;
852     break;
853   case DW_OP_const8s:
854     element->type = e_dw_sconstant;
855     element->location.uconstant.bytes = 8;
856     element->location.uconstant.value = (unsigned long int) op->number;
857     break;
858   default:
859     element->type = e_dw_unsupported;
860     break;
861   }
862   return element;
863 }
864
865 /** \brief Convert libdw location expresion into native one */
866 static dw_location_t MC_dwarf_get_expression(Dwarf_Op* expr,  size_t len) {
867   dw_location_t loc = xbt_new0(s_dw_location_t, 1);
868   loc->type = e_dw_compose;
869   loc->location.compose = xbt_dynar_new(sizeof(dw_location_t), NULL);
870
871   int i;
872   for (i=0; i!=len; ++i) {
873     dw_location_t element =  MC_dwarf_get_expression_element(expr+i);
874     if (element)
875       xbt_dynar_push(loc->location.compose, &element);
876   }
877
878   return loc;
879 }
880
881 static int mc_anonymous_variable_index = 0;
882
883 static dw_variable_t MC_die_to_variable(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
884   // Skip declarations:
885   if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
886     return NULL;
887
888   // Skip compile time constants:
889   if(dwarf_hasattr(die, DW_AT_const_value))
890     return NULL;
891
892   Dwarf_Attribute attr_location;
893   if (dwarf_attr(die, DW_AT_location, &attr_location)==NULL) {
894     // No location: do not add it ?
895     return NULL;
896   }
897
898   dw_variable_t variable = xbt_new0(s_dw_variable_t, 1);
899   variable->dwarf_offset = dwarf_dieoffset(die);
900   variable->global = frame == NULL; // Can be override base on DW_AT_location
901
902   const char* name = MC_dwarf_attr_string(die, DW_AT_name);
903   variable->name = xbt_strdup(name);
904
905   variable->type_origin = MC_dwarf_at_type(die);
906
907   int form = dwarf_whatform(&attr_location);
908   int klass = form == DW_FORM_sec_offset ? MC_DW_CLASS_CONSTANT : MC_dwarf_form_get_class(form);
909   switch (klass) {
910   case MC_DW_CLASS_EXPRLOC:
911   case MC_DW_CLASS_BLOCK:
912     // Location expression:
913     {
914       Dwarf_Op* expr;
915       size_t len;
916       if (dwarf_getlocation(&attr_location, &expr, &len)) {
917         xbt_die(
918           "Could not read location expression in DW_AT_location of variable <%p>%s",
919           (void*) variable->dwarf_offset, variable->name);
920       }
921
922       if (len==1 && expr[0].atom == DW_OP_addr) {
923         variable->global = 1;
924         Dwarf_Off offset = expr[0].number;
925         // TODO, Why is this different base on the object?
926         Dwarf_Off base = strcmp(info->file_name, xbt_binary_name) !=0 ? (Dwarf_Off) info->start_exec : 0;
927         variable->address = (void*) (base + offset);
928       } else {
929         mc_dwarf_location_list_init_from_expression(&variable->locations, len, expr);
930       }
931
932       break;
933     }
934   case MC_DW_CLASS_LOCLISTPTR:
935   case MC_DW_CLASS_CONSTANT:
936     // Reference to location list:
937     mc_dwarf_location_list_init(&variable->locations, info, die, &attr_location);
938     break;
939   default:
940     xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location in <%p>%s",
941       form, form, klass, klass, (void*) variable->dwarf_offset, variable->name);
942   }
943
944   // Handle start_scope:
945   if (dwarf_hasattr(die, DW_AT_start_scope)) {
946     Dwarf_Attribute attr;
947     dwarf_attr(die, DW_AT_start_scope, &attr);
948     int form  = dwarf_whatform(&attr);
949     int klass = MC_dwarf_form_get_class(form);
950     switch(klass) {
951     case MC_DW_CLASS_CONSTANT:
952     {
953       Dwarf_Word value;
954       variable->start_scope = dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
955       break;
956     }
957     default:
958       xbt_die("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
959         form, klass, name==NULL ? "?" : name);
960     }
961   }
962
963   if(namespace && variable->global) {
964     char* old_name = variable->name;
965     variable->name = bprintf("%s::%s", namespace, old_name);
966     free(old_name);
967   }
968
969   // The current code needs a variable name,
970   // generate a fake one:
971   if(!variable->name) {
972     variable->name = bprintf("@anonymous#%i", mc_anonymous_variable_index++);
973   }
974
975   return variable;
976 }
977
978 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
979   dw_variable_t variable = MC_die_to_variable(info, die, unit, frame, namespace);
980   if(variable==NULL)
981       return;
982   MC_dwarf_register_variable(info, frame, variable);
983 }
984
985 static void MC_dwarf_handle_subprogram_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t parent_frame, const char* namespace) {
986
987   // (Template) Subprogram declaration:
988   if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
989     return;
990
991   // Abstract inline instance (DW_AT_inline != DW_INL_not_inlined):
992   if (dwarf_func_inline(die))
993     return;
994
995   // This is probably not a concrete instance:
996   // DWARF2/3 and DWARF4 do not agree on the meaning of DW_INL_not_inlined.
997   // For DWARF2/3, the subprogram is abstract.
998   // For DARF4, the subprogram is not abstract.
999   if(!dwarf_hasattr_integrate(die, DW_AT_low_pc))
1000     return;
1001
1002   dw_frame_t frame = xbt_new0(s_dw_frame_t, 1);
1003
1004   frame->start = dwarf_dieoffset(die);
1005
1006   const char* name = MC_dwarf_attr_string(die, DW_AT_name);
1007   frame->name = namespace ? bprintf("%s::%s", namespace, name) : xbt_strdup(name);
1008
1009   // This is the base address for DWARF addresses.
1010   // Relocated addresses are offset from this base address.
1011   // See DWARF4 spec 7.5
1012   void* base = info->flags & MC_OBJECT_INFO_EXECUTABLE ? 0 : MC_object_base_address(info);
1013
1014   // Variables are filled in the (recursive) call of MC_dwarf_handle_children:
1015   frame->variables = xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
1016   frame->high_pc = ((char*) base) + MC_dwarf_attr_addr(die, DW_AT_high_pc);
1017   frame->low_pc = ((char*) base) + MC_dwarf_attr_addr(die, DW_AT_low_pc);
1018
1019   if(frame->high_pc==0 || frame->low_pc==0)
1020     xbt_die("Could not resolve highpc/lowpc");
1021
1022   Dwarf_Attribute attr_frame_base;
1023   if (!dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
1024     xbt_die("Coult not find DW_AT_frame_base for subprogram %s %p", frame->name, frame->start);
1025   mc_dwarf_location_list_init(&frame->frame_base, info, die, &attr_frame_base);
1026
1027   frame->end = -1; // This one is now useless:
1028
1029   // Register it:
1030   xbt_dynar_push(info->subprograms, &frame);
1031
1032   // Handle children:
1033   MC_dwarf_handle_children(info, die, unit, frame, namespace);
1034 }
1035
1036 static void mc_dwarf_handle_namespace_die(
1037     mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
1038   const char* name = MC_dwarf_attr_string(die, DW_AT_name);
1039   if(frame)
1040     xbt_die("Unexpected namespace in a subprogram");
1041   char* new_namespace = namespace == NULL ? xbt_strdup(name)
1042     : bprintf("%s::%s", namespace, name);
1043   MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
1044 }
1045
1046 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
1047   Dwarf_Die child;
1048   int res;
1049   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
1050     MC_dwarf_handle_die(info, &child, unit, frame, namespace);
1051   }
1052 }
1053
1054 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
1055   int tag = dwarf_tag(die);
1056   mc_tag_class klass = MC_dwarf_tag_classify(tag);
1057   switch (klass) {
1058
1059     // Type:
1060     case mc_tag_type:
1061       MC_dwarf_handle_type_die(info, die, unit, frame, namespace);
1062       break;
1063
1064     // Program:
1065     case mc_tag_subprogram:
1066       MC_dwarf_handle_subprogram_die(info, die, unit, frame, namespace);
1067       return;
1068
1069     // Variable:
1070     case mc_tag_variable:
1071       MC_dwarf_handle_variable_die(info, die, unit, frame, namespace);
1072       break;
1073
1074     // Scope:
1075     case mc_tag_scope:
1076       // TODO
1077       break;
1078
1079     case mc_tag_namespace:
1080       mc_dwarf_handle_namespace_die(info, die, unit, frame, namespace);
1081       break;
1082
1083     default:
1084       break;
1085
1086   }
1087 }
1088
1089 void MC_dwarf_get_variables(mc_object_info_t info) {
1090   int fd = open(info->file_name, O_RDONLY);
1091   if (fd<0) {
1092     xbt_die("Could not open file %s", info->file_name);
1093   }
1094   Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
1095   if (dwarf==NULL) {
1096     xbt_die("Your program must be compiled with -g");
1097   }
1098
1099   Dwarf_Off offset = 0;
1100   Dwarf_Off next_offset = 0;
1101   size_t length;
1102   while (dwarf_nextcu (dwarf, offset, &next_offset, &length, NULL, NULL, NULL) == 0) {
1103     Dwarf_Die unit_die;
1104
1105     if(dwarf_offdie(dwarf, offset+length, &unit_die)!=NULL) {
1106       Dwarf_Die child;
1107       int res;
1108       for (res=dwarf_child(&unit_die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
1109         MC_dwarf_handle_die(info, &child, &unit_die, NULL, NULL);
1110       }
1111     }
1112     offset = next_offset;
1113   }
1114
1115   dwarf_end(dwarf);
1116   close(fd);
1117 }