Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Fix DW_OP_call_frame_cfa
[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 Get the DW_TAG_type of the DIE
78  *
79  *  \param die DIE
80  *  \return DW_TAG_type attribute as a new string (NULL if none)
81  */
82 static char* MC_dwarf_at_type(Dwarf_Die* die);
83
84 /** \brief Get the name of an attribute (DW_AT_*) from its code
85  *
86  *  \param attr attribute code (see the DWARF specification)
87  *  \return name of the attribute
88  */
89 const char* MC_dwarf_attrname(int attr) {
90   switch (attr) {
91 #include "mc_dwarf_attrnames.h"
92   default:
93     return "DW_AT_unknown";
94   }
95 }
96
97 /** \brief Get the name of a dwarf tag (DW_TAG_*) from its code
98  *
99  *  \param tag tag code (see the DWARF specification)
100  *  \return name of the tag
101  */
102 const char* MC_dwarf_tagname(int tag) {
103   switch (tag) {
104 #include "mc_dwarf_tagnames.h"
105   case DW_TAG_invalid:
106     return "DW_TAG_invalid";
107   default:
108     return "DW_TAG_unknown";
109   }
110 }
111
112 /** \brief A class of DWARF tags (DW_TAG_*)
113  */
114 typedef enum mc_tag_class {
115   mc_tag_unknown,
116   mc_tag_type,
117   mc_tag_subprogram,
118   mc_tag_variable,
119   mc_tag_scope,
120   mc_tag_namespace
121 } mc_tag_class;
122
123 static mc_tag_class MC_dwarf_tag_classify(int tag) {
124   switch (tag) {
125
126     case DW_TAG_array_type:
127     case DW_TAG_class_type:
128     case DW_TAG_enumeration_type:
129     case DW_TAG_typedef:
130     case DW_TAG_pointer_type:
131     case DW_TAG_reference_type:
132     case DW_TAG_rvalue_reference_type:
133     case DW_TAG_string_type:
134     case DW_TAG_structure_type:
135     case DW_TAG_subroutine_type:
136     case DW_TAG_union_type:
137     case DW_TAG_ptr_to_member_type:
138     case DW_TAG_set_type:
139     case DW_TAG_subrange_type:
140     case DW_TAG_base_type:
141     case DW_TAG_const_type:
142     case DW_TAG_file_type:
143     case DW_TAG_packed_type:
144     case DW_TAG_volatile_type:
145     case DW_TAG_restrict_type:
146     case DW_TAG_interface_type:
147     case DW_TAG_unspecified_type:
148     case DW_TAG_mutable_type:
149     case DW_TAG_shared_type:
150       return mc_tag_type;
151
152     case DW_TAG_subprogram:
153       return mc_tag_subprogram;
154
155     case DW_TAG_variable:
156     case DW_TAG_formal_parameter:
157       return mc_tag_variable;
158
159     case DW_TAG_lexical_block:
160     case DW_TAG_try_block:
161     case DW_TAG_catch_block:
162     case DW_TAG_inlined_subroutine:
163     case DW_TAG_with_stmt:
164       return mc_tag_scope;
165
166     case DW_TAG_namespace:
167       return mc_tag_namespace;
168
169     default:
170       return mc_tag_unknown;
171
172   }
173 }
174
175 #define MC_DW_CLASS_UNKNOWN 0
176 #define MC_DW_CLASS_ADDRESS 1   // Location in the address space of the program
177 #define MC_DW_CLASS_BLOCK 2     // Arbitrary block of bytes
178 #define MC_DW_CLASS_CONSTANT 3
179 #define MC_DW_CLASS_STRING 3    // String
180 #define MC_DW_CLASS_FLAG 4      // Boolean
181 #define MC_DW_CLASS_REFERENCE 5 // Reference to another DIE
182 #define MC_DW_CLASS_EXPRLOC 6   // DWARF expression/location description
183 #define MC_DW_CLASS_LINEPTR 7
184 #define MC_DW_CLASS_LOCLISTPTR 8
185 #define MC_DW_CLASS_MACPTR 9
186 #define MC_DW_CLASS_RANGELISTPTR 10
187
188 /** \brief Find the DWARF data class for a given DWARF data form
189  *
190  *  This mapping is defined in the DWARF spec.
191  *
192  *  \param form The form (values taken from the DWARF spec)
193  *  \return An internal representation for the corresponding class
194  * */
195 static int MC_dwarf_form_get_class(int form) {
196   switch(form) {
197   case DW_FORM_addr:
198     return MC_DW_CLASS_ADDRESS;
199   case DW_FORM_block2:
200   case DW_FORM_block4:
201   case DW_FORM_block:
202   case DW_FORM_block1:
203     return MC_DW_CLASS_BLOCK;
204   case DW_FORM_data1:
205   case DW_FORM_data2:
206   case DW_FORM_data4:
207   case DW_FORM_data8:
208   case DW_FORM_udata:
209   case DW_FORM_sdata:
210     return MC_DW_CLASS_CONSTANT;
211   case DW_FORM_string:
212   case DW_FORM_strp:
213     return MC_DW_CLASS_STRING;
214   case DW_FORM_ref_addr:
215   case DW_FORM_ref1:
216   case DW_FORM_ref2:
217   case DW_FORM_ref4:
218   case DW_FORM_ref8:
219   case DW_FORM_ref_udata:
220     return MC_DW_CLASS_REFERENCE;
221   case DW_FORM_flag:
222   case DW_FORM_flag_present:
223     return MC_DW_CLASS_FLAG;
224   case DW_FORM_exprloc:
225     return MC_DW_CLASS_EXPRLOC;
226   // TODO sec offset
227   // TODO indirect
228   default:
229     return MC_DW_CLASS_UNKNOWN;
230   }
231 }
232
233 /** \brief Get the name of the tag of a given DIE
234  *
235  *  \param die DIE
236  *  \return name of the tag of this DIE
237  */
238 static inline const char* MC_dwarf_die_tagname(Dwarf_Die* die) {
239   return MC_dwarf_tagname(dwarf_tag(die));
240 }
241
242 // ***** Attributes
243
244 /** \brief Get an attribute of a given DIE as a string
245  *
246  *  \param die       the DIE
247  *  \param attribute attribute
248  *  \return value of the given attribute of the given DIE
249  */
250 static const char* MC_dwarf_attr_integrate_string(Dwarf_Die* die, int attribute) {
251   Dwarf_Attribute attr;
252   if (!dwarf_attr_integrate(die, attribute, &attr)) {
253         return NULL;
254   } else {
255         return dwarf_formstring(&attr);
256   }
257 }
258
259 /** \brief Get the linkage name of a DIE.
260  *
261  *  Use either DW_AT_linkage_name or DW_AT_MIPS_linkage_name.
262  *  DW_AT_linkage_name is standardized since DWARF 4.
263  *  Before this version of DWARF, the MIPS extensions
264  *  DW_AT_MIPS_linkage_name is used (at least by GCC).
265  *
266  *  \param  the DIE
267  *  \return linkage name of the given DIE (or NULL)
268  * */
269 static const char* MC_dwarf_at_linkage_name(Dwarf_Die* die) {
270   const char* name = MC_dwarf_attr_integrate_string(die, DW_AT_linkage_name);
271   if (!name)
272     name = MC_dwarf_attr_integrate_string(die, DW_AT_MIPS_linkage_name);
273   return name;
274 }
275
276 static Dwarf_Off MC_dwarf_attr_dieoffset(Dwarf_Die* die, int attribute) {
277   Dwarf_Attribute attr;
278   if (dwarf_hasattr_integrate(die, attribute)) {
279     dwarf_attr_integrate(die, attribute, &attr);
280     Dwarf_Die subtype_die;
281     if (dwarf_formref_die(&attr, &subtype_die)==NULL) {
282       xbt_die("Could not find DIE");
283     }
284     return dwarf_dieoffset(&subtype_die);
285   }
286   else return 0;
287 }
288
289 static Dwarf_Off MC_dwarf_attr_integrate_dieoffset(Dwarf_Die* die, int attribute) {
290   Dwarf_Attribute attr;
291   if (dwarf_hasattr_integrate(die, attribute)) {
292     dwarf_attr_integrate(die, DW_AT_type, &attr);
293     Dwarf_Die subtype_die;
294     if (dwarf_formref_die(&attr, &subtype_die)==NULL) {
295       xbt_die("Could not find DIE");
296     }
297     return dwarf_dieoffset(&subtype_die);
298   }
299   else return 0;
300 }
301
302 /** \brief Find the type/subtype (DW_AT_type) for a DIE
303  *
304  *  \param dit the DIE
305  *  \return DW_AT_type reference as a global offset in hexadecimal (or NULL)
306  */
307 static char* MC_dwarf_at_type(Dwarf_Die* die) {
308   Dwarf_Off offset = MC_dwarf_attr_integrate_dieoffset(die, DW_AT_type);
309   return offset == 0 ? NULL : bprintf("%" PRIx64 , offset);
310 }
311
312 static uint64_t MC_dwarf_attr_integrate_addr(Dwarf_Die* die, int attribute) {
313   Dwarf_Attribute attr;
314   if(dwarf_attr_integrate(die, attribute, &attr)==NULL)
315     return 0;
316   Dwarf_Addr value;
317   if (dwarf_formaddr(&attr, &value) == 0)
318     return (uint64_t) value;
319   else
320     return 0;
321 }
322
323 static uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die* die, int attribute, uint64_t default_value) {
324   Dwarf_Attribute attr;
325   if (dwarf_attr_integrate(die, attribute, &attr)==NULL)
326     return default_value;
327   Dwarf_Word value;
328   return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr), &value) == 0 ? (uint64_t) value : default_value;
329 }
330
331 static bool MC_dwarf_attr_flag(Dwarf_Die* die, int attribute, bool integrate) {
332   Dwarf_Attribute attr;
333   if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
334                     : dwarf_attr(die, attribute, &attr))==0)
335     return false;
336
337   bool result;
338   if (dwarf_formflag(&attr, &result))
339     xbt_die("Unexpected form for attribute %s",
340       MC_dwarf_attrname(attribute));
341   return result;
342 }
343
344 /** \brief Find the default lower bound for a given language
345  *
346  *  The default lower bound of an array (when DW_TAG_lower_bound
347  *  is missing) depends on the language of the compilation unit.
348  *
349  *  \param lang Language of the compilation unit (values defined in the DWARF spec)
350  *  \return     Default lower bound of an array in this compilation unit
351  * */
352 static uint64_t MC_dwarf_default_lower_bound(int lang) {
353   switch(lang) {
354   case DW_LANG_C:
355   case DW_LANG_C89:
356   case DW_LANG_C99:
357   case DW_LANG_C_plus_plus:
358   case DW_LANG_D:
359   case DW_LANG_Java:
360   case DW_LANG_ObjC:
361   case DW_LANG_ObjC_plus_plus:
362   case DW_LANG_Python:
363   case DW_LANG_UPC:
364     return 0;
365   case DW_LANG_Ada83:
366   case DW_LANG_Ada95:
367   case DW_LANG_Fortran77:
368   case DW_LANG_Fortran90:
369   case DW_LANG_Fortran95:
370   case DW_LANG_Modula2:
371   case DW_LANG_Pascal83:
372   case DW_LANG_PL1:
373   case DW_LANG_Cobol74:
374   case DW_LANG_Cobol85:
375     return 1;
376   default:
377     xbt_die("No default DW_TAG_lower_bound for language %i and none given", lang);
378     return 0;
379   }
380 }
381
382 /** \brief Finds the number of elements in a DW_TAG_subrange_type or DW_TAG_enumeration_type DIE
383  *
384  *  \param die  the DIE
385  *  \param unit DIE of the compilation unit
386  *  \return     number of elements in the range
387  * */
388 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
389   xbt_assert(dwarf_tag(die)==DW_TAG_enumeration_type ||dwarf_tag(die)==DW_TAG_subrange_type,
390       "MC_dwarf_subrange_element_count called with DIE of type %s", MC_dwarf_die_tagname(die));
391
392   // Use DW_TAG_count if present:
393   if (dwarf_hasattr_integrate(die, DW_AT_count)) {
394     return MC_dwarf_attr_integrate_uint(die, DW_AT_count, 0);
395   }
396
397   // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
398
399   if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
400         // This is not really 0, but the code expects this (we do not know):
401     return 0;
402   }
403   uint64_t upper_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_upper_bound, -1);
404
405   uint64_t lower_bound = 0;
406   if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
407     lower_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_lower_bound, -1);
408   } else {
409         lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
410   }
411   return upper_bound - lower_bound + 1;
412 }
413
414 /** \brief Finds the number of elements in a array type (DW_TAG_array_type)
415  *
416  *  The compilation unit might be needed because the default lower
417  *  bound depends on the language of the compilation unit.
418  *
419  *  \param die the DIE of the DW_TAG_array_type
420  *  \param unit the DIE of the compilation unit
421  *  \return number of elements in this array type
422  * */
423 static uint64_t MC_dwarf_array_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
424   xbt_assert(dwarf_tag(die)==DW_TAG_array_type,
425     "MC_dwarf_array_element_count called with DIE of type %s", MC_dwarf_die_tagname(die));
426
427   int result = 1;
428   Dwarf_Die child;
429   int res;
430   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
431         int child_tag = dwarf_tag(&child);
432     if (child_tag==DW_TAG_subrange_type ||child_tag==DW_TAG_enumeration_type) {
433       result *= MC_dwarf_subrange_element_count(&child, unit);
434     }
435   }
436   return result;
437 }
438
439 // ***** dw_type_t
440
441 /** \brief Initialize the location of a member of a type
442  * (DW_AT_data_member_location of a DW_TAG_member).
443  *
444  *  \param  type   a type (struct, class)
445  *  \param  member the member of the type
446  *  \param  child  DIE of the member (DW_TAG_member)
447  */
448 static void MC_dwarf_fill_member_location(dw_type_t type, dw_type_t member, Dwarf_Die* child) {
449   if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
450     xbt_die("Can't groke DW_AT_data_bit_offset.");
451   }
452
453   if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
454     if (type->type != DW_TAG_union_type) {
455         xbt_die(
456           "Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%p>%s",
457           member->name, type->id, type->name);
458     } else {
459       return;
460     }
461   }
462
463   Dwarf_Attribute attr;
464   dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
465   int form = dwarf_whatform(&attr);
466   int klass = MC_dwarf_form_get_class(form);
467   switch (klass) {
468   case MC_DW_CLASS_EXPRLOC:
469   case MC_DW_CLASS_BLOCK:
470     // Location expression:
471     {
472       Dwarf_Op* expr;
473       size_t len;
474       if (dwarf_getlocation(&attr, &expr, &len)) {
475         xbt_die(
476           "Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%p>%s",
477           MC_dwarf_attr_integrate_string(child, DW_AT_name),
478           type->id, type->name);
479       }
480       if (len==1 && expr[0].atom == DW_OP_plus_uconst) {
481         member->offset =  expr[0].number;
482       } else {
483         mc_dwarf_expression_init(&member->location, len, expr);
484       }
485       break;
486     }
487   case MC_DW_CLASS_CONSTANT:
488     // Offset from the base address of the object:
489     {
490       Dwarf_Word offset;
491       if (!dwarf_formudata(&attr, &offset))
492         member->offset = offset;
493       else
494         xbt_die("Cannot get %s location <%p>%s",
495           MC_dwarf_attr_integrate_string(child, DW_AT_name),
496           type->id, type->name);
497       break;
498     }
499   case MC_DW_CLASS_LOCLISTPTR:
500     // Reference to a location list:
501     // TODO
502   case MC_DW_CLASS_REFERENCE:
503     // It's supposed to be possible in DWARF2 but I couldn't find its semantic
504     // in the spec.
505   default:
506     xbt_die(
507       "Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
508       klass, form);
509   }
510
511 }
512
513 static void dw_type_free_voidp(void *t){
514   dw_type_free((dw_type_t) * (void **) t);
515 }
516
517 /** \brief Populate the list of members of a type
518  *
519  *  \param info ELF object containing the type DIE
520  *  \param die  DIE of the type
521  *  \param unit DIE of the compilation unit containing the type DIE
522  *  \param type the type
523  */
524 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_type_t type) {
525   int res;
526   Dwarf_Die child;
527   xbt_assert(!type->members);
528   type->members = xbt_dynar_new(sizeof(dw_type_t), (void(*)(void*))dw_type_free_voidp);
529   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
530     int tag = dwarf_tag(&child);
531     if (tag==DW_TAG_member || tag==DW_TAG_inheritance) {
532
533       // Skip declarations:
534       if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
535         continue;
536
537       // Skip compile time constants:
538       if(dwarf_hasattr(&child, DW_AT_const_value))
539         continue;
540
541       // TODO, we should use another type (because is is not a type but a member)
542       dw_type_t member = xbt_new0(s_dw_type_t, 1);
543       member->type = tag;
544
545       // Global Offset:
546       member->id = (void *) dwarf_dieoffset(&child);
547
548       const char* name = MC_dwarf_attr_integrate_string(&child, DW_AT_name);
549       if(name)
550         member->name = xbt_strdup(name);
551       else
552         member->name = NULL;
553
554       member->byte_size = MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
555       member->element_count = -1;
556       member->dw_type_id = MC_dwarf_at_type(&child);
557       member->members = NULL;
558       member->is_pointer_type = 0;
559       member->offset = 0;
560
561       if(dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
562         xbt_die("Can't groke DW_AT_data_bit_offset.");
563       }
564
565       MC_dwarf_fill_member_location(type, member, &child);
566
567       if (!member->dw_type_id) {
568         xbt_die("Missing type for member %s of <%p>%s", member->name, type->id, type->name);
569       }
570
571       xbt_dynar_push(type->members, &member);
572     }
573   }
574 }
575
576 /** \brief Create a MC type object from a DIE
577  *
578  *  \param info current object info object
579  *  \param DIE (for a given type);
580  *  \param unit compilation unit of the current DIE
581  *  \return MC representation of the type
582  */
583 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) {
584
585   dw_type_t type = xbt_new0(s_dw_type_t, 1);
586   type->type = -1;
587   type->id = NULL;
588   type->name = NULL;
589   type->byte_size = 0;
590   type->element_count = -1;
591   type->dw_type_id = NULL;
592   type->members = NULL;
593   type->is_pointer_type = 0;
594   type->offset = 0;
595
596   type->type = dwarf_tag(die);
597
598   // Global Offset
599   type->id = (void *) dwarf_dieoffset(die);
600
601   const char* prefix = "";
602   switch (type->type) {
603   case DW_TAG_structure_type:
604     prefix = "struct ";
605     break;
606   case DW_TAG_union_type:
607     prefix = "union ";
608     break;
609   case DW_TAG_class_type:
610     prefix = "class ";
611     break;
612   default:
613     prefix = "";
614   }
615
616   const char* name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
617   if (name!=NULL) {
618     type->name = namespace ? bprintf("%s%s::%s", prefix, namespace, name) : bprintf("%s%s", prefix, name);
619   }
620
621   XBT_DEBUG("Processing type <%p>%s", type->id, type->name);
622
623   type->dw_type_id = MC_dwarf_at_type(die);
624
625   // Computation of the byte_size;
626   if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
627     type->byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0);
628   else if (type->type == DW_TAG_array_type || type->type==DW_TAG_structure_type || type->type==DW_TAG_class_type) {
629     Dwarf_Word size;
630     if (dwarf_aggregate_size(die, &size)==0) {
631       type->byte_size = size;
632     }
633   }
634
635   switch (type->type) {
636   case DW_TAG_array_type:
637         type->element_count = MC_dwarf_array_element_count(die, unit);
638         // TODO, handle DW_byte_stride and (not) DW_bit_stride
639         break;
640
641   case DW_TAG_pointer_type:
642   case DW_TAG_reference_type:
643   case DW_TAG_rvalue_reference_type:
644     type->is_pointer_type = 1;
645     break;
646
647   case DW_TAG_structure_type:
648   case DW_TAG_union_type:
649   case DW_TAG_class_type:
650           MC_dwarf_add_members(info, die, unit, type);
651           char* new_namespace = namespace == NULL ? xbt_strdup(type->name)
652             : bprintf("%s::%s", namespace, name);
653           MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
654           free(new_namespace);
655           break;
656   }
657
658   return type;
659 }
660
661 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) {
662   dw_type_t type = MC_dwarf_die_to_type(info, die, unit, frame, namespace);
663
664   char* key = bprintf("%" PRIx64, (uint64_t) type->id);
665   xbt_dict_set(info->types, key, type, NULL);
666   xbt_free(key);
667
668   if(type->name && type->byte_size!=0) {
669     xbt_dict_set(info->full_types_by_name, type->name, type, NULL);
670   }
671 }
672
673 static int mc_anonymous_variable_index = 0;
674
675 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) {
676   // Skip declarations:
677   if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
678     return NULL;
679
680   // Skip compile time constants:
681   if(dwarf_hasattr(die, DW_AT_const_value))
682     return NULL;
683
684   Dwarf_Attribute attr_location;
685   if (dwarf_attr(die, DW_AT_location, &attr_location)==NULL) {
686     // No location: do not add it ?
687     return NULL;
688   }
689
690   dw_variable_t variable = xbt_new0(s_dw_variable_t, 1);
691   variable->dwarf_offset = dwarf_dieoffset(die);
692   variable->global = frame == NULL; // Can be override base on DW_AT_location
693
694   const char* name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
695   variable->name = xbt_strdup(name);
696
697   variable->type_origin = MC_dwarf_at_type(die);
698
699   int form = dwarf_whatform(&attr_location);
700   int klass = form == DW_FORM_sec_offset ? MC_DW_CLASS_CONSTANT : MC_dwarf_form_get_class(form);
701   switch (klass) {
702   case MC_DW_CLASS_EXPRLOC:
703   case MC_DW_CLASS_BLOCK:
704     // Location expression:
705     {
706       Dwarf_Op* expr;
707       size_t len;
708       if (dwarf_getlocation(&attr_location, &expr, &len)) {
709         xbt_die(
710           "Could not read location expression in DW_AT_location of variable <%p>%s",
711           (void*) variable->dwarf_offset, variable->name);
712       }
713
714       if (len==1 && expr[0].atom == DW_OP_addr) {
715         variable->global = 1;
716         Dwarf_Off offset = expr[0].number;
717         // TODO, Why is this different base on the object?
718         Dwarf_Off base = strcmp(info->file_name, xbt_binary_name) !=0 ? (Dwarf_Off) info->start_exec : 0;
719         variable->address = (void*) (base + offset);
720       } else {
721         mc_dwarf_location_list_init_from_expression(&variable->locations, len, expr);
722       }
723
724       break;
725     }
726   case MC_DW_CLASS_LOCLISTPTR:
727   case MC_DW_CLASS_CONSTANT:
728     // Reference to location list:
729     mc_dwarf_location_list_init(&variable->locations, info, die, &attr_location);
730     break;
731   default:
732     xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location in <%p>%s",
733       form, form, klass, klass, (void*) variable->dwarf_offset, variable->name);
734   }
735
736   // Handle start_scope:
737   if (dwarf_hasattr(die, DW_AT_start_scope)) {
738     Dwarf_Attribute attr;
739     dwarf_attr(die, DW_AT_start_scope, &attr);
740     int form  = dwarf_whatform(&attr);
741     int klass = MC_dwarf_form_get_class(form);
742     switch(klass) {
743     case MC_DW_CLASS_CONSTANT:
744     {
745       Dwarf_Word value;
746       variable->start_scope = dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
747       break;
748     }
749     default:
750       xbt_die("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
751         form, klass, name==NULL ? "?" : name);
752     }
753   }
754
755   if(namespace && variable->global) {
756     char* old_name = variable->name;
757     variable->name = bprintf("%s::%s", namespace, old_name);
758     free(old_name);
759   }
760
761   // The current code needs a variable name,
762   // generate a fake one:
763   if(!variable->name) {
764     variable->name = bprintf("@anonymous#%i", mc_anonymous_variable_index++);
765   }
766
767   return variable;
768 }
769
770 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) {
771   dw_variable_t variable = MC_die_to_variable(info, die, unit, frame, namespace);
772   if(variable==NULL)
773       return;
774   MC_dwarf_register_variable(info, frame, variable);
775 }
776
777 static void mc_frame_free_voipd(dw_frame_t* p) {
778   mc_frame_free(*p);
779   *p = NULL;
780 }
781
782 static void MC_dwarf_handle_scope_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t parent_frame, const char* namespace) {
783   // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt
784   int tag = dwarf_tag(die);
785   mc_tag_class klass = MC_dwarf_tag_classify(tag);
786
787   // (Template) Subprogram declaration:
788   if(klass==mc_tag_subprogram && MC_dwarf_attr_flag(die, DW_AT_declaration, false))
789     return;
790
791   if(klass==mc_tag_scope)
792     xbt_assert(parent_frame, "No parent scope for this scope");
793
794   dw_frame_t frame = xbt_new0(s_dw_frame_t, 1);
795
796   frame->tag   = tag;
797   frame->id = dwarf_dieoffset(die);
798
799   if(klass==mc_tag_subprogram) {
800     const char* name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
801     frame->name = namespace ? bprintf("%s::%s", namespace, name) : xbt_strdup(name);
802   }
803
804   frame->abstract_origin_id = MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin);
805
806   // This is the base address for DWARF addresses.
807   // Relocated addresses are offset from this base address.
808   // See DWARF4 spec 7.5
809   void* base = info->flags & MC_OBJECT_INFO_EXECUTABLE ? 0 : MC_object_base_address(info);
810
811   // Variables are filled in the (recursive) call of MC_dwarf_handle_children:
812   frame->variables = xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
813   frame->high_pc = ((char*) base) + MC_dwarf_attr_integrate_addr(die, DW_AT_high_pc);
814   frame->low_pc = ((char*) base) + MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
815
816   if(klass==mc_tag_subprogram) {
817     Dwarf_Attribute attr_frame_base;
818     if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
819       mc_dwarf_location_list_init(&frame->frame_base, info, die, &attr_frame_base);
820   }
821
822   frame->scopes = xbt_dynar_new(sizeof(dw_frame_t), (void_f_pvoid_t) mc_frame_free_voipd);
823
824   // Register it:
825   if(klass==mc_tag_subprogram) {
826     char* key = bprintf("%" PRIx64, (uint64_t) frame->id);
827     xbt_dict_set(info->subprograms,  key, frame, NULL);
828     xbt_free(key);
829   } else if(klass==mc_tag_scope) {
830     xbt_dynar_push(parent_frame->scopes, &frame);
831   }
832
833   // Handle children:
834   MC_dwarf_handle_children(info, die, unit, frame, namespace);
835 }
836
837 static void mc_dwarf_handle_namespace_die(
838     mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
839   const char* name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
840   if(frame)
841     xbt_die("Unexpected namespace in a subprogram");
842   char* new_namespace = namespace == NULL ? xbt_strdup(name)
843     : bprintf("%s::%s", namespace, name);
844   MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
845   xbt_free(new_namespace);
846 }
847
848 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
849   // For each child DIE:
850   Dwarf_Die child;
851   int res;
852   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
853     MC_dwarf_handle_die(info, &child, unit, frame, namespace);
854   }
855 }
856
857 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
858   int tag = dwarf_tag(die);
859   mc_tag_class klass = MC_dwarf_tag_classify(tag);
860   switch (klass) {
861
862     // Type:
863     case mc_tag_type:
864       MC_dwarf_handle_type_die(info, die, unit, frame, namespace);
865       break;
866
867     // Subprogram or scope:
868     case mc_tag_subprogram:
869     case mc_tag_scope:
870       MC_dwarf_handle_scope_die(info, die, unit, frame, namespace);
871       return;
872
873     // Variable:
874     case mc_tag_variable:
875       MC_dwarf_handle_variable_die(info, die, unit, frame, namespace);
876       break;
877
878     case mc_tag_namespace:
879       mc_dwarf_handle_namespace_die(info, die, unit, frame, namespace);
880       break;
881
882     default:
883       break;
884
885   }
886 }
887
888 /** \brief Populate the debugging informations of the given ELF object
889  *
890  *  Read the DWARf information of the EFFL object and populate the
891  *  lists of types, variables, functions.
892  */
893 void MC_dwarf_get_variables(mc_object_info_t info) {
894   int fd = open(info->file_name, O_RDONLY);
895   if (fd<0) {
896     xbt_die("Could not open file %s", info->file_name);
897   }
898   Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
899   if (dwarf==NULL) {
900     xbt_die("Your program must be compiled with -g");
901   }
902
903   // For each compilation unit:
904   Dwarf_Off offset = 0;
905   Dwarf_Off next_offset = 0;
906   size_t length;
907   while (dwarf_nextcu (dwarf, offset, &next_offset, &length, NULL, NULL, NULL) == 0) {
908     Dwarf_Die unit_die;
909     if(dwarf_offdie(dwarf, offset+length, &unit_die)!=NULL) {
910
911       // For each child DIE:
912       Dwarf_Die child;
913       int res;
914       for (res=dwarf_child(&unit_die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
915         MC_dwarf_handle_die(info, &child, &unit_die, NULL, NULL);
916       }
917
918     }
919     offset = next_offset;
920   }
921
922   dwarf_end(dwarf);
923   close(fd);
924 }