Logo AND Algorithmique Numérique Distribuée

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