Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
34a511d1285ded22766a663ee2c9b1dca8a4326a
[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 /** \brief Find the type/subtype (DW_AT_type) for a DIE
277  *
278  *  \param dit the DIE
279  *  \return DW_AT_type reference as a global offset in hexadecimal (or NULL)
280  */
281 static char* MC_dwarf_at_type(Dwarf_Die* die) {
282   Dwarf_Attribute attr;
283   if (dwarf_hasattr_integrate(die, DW_AT_type)) {
284         dwarf_attr_integrate(die, DW_AT_type, &attr);
285         Dwarf_Die subtype_die;
286         if (dwarf_formref_die(&attr, &subtype_die)==NULL) {
287           xbt_die("Could not find DIE for type");
288         }
289         Dwarf_Off subtype_global_offset = dwarf_dieoffset(&subtype_die);
290     return bprintf("%" PRIx64 , subtype_global_offset);
291   }
292   else return NULL;
293 }
294
295 static uint64_t MC_dwarf_attr_integrate_addr(Dwarf_Die* die, int attribute) {
296   Dwarf_Attribute attr;
297   if(dwarf_attr_integrate(die, attribute, &attr)==NULL)
298     return 0;
299   Dwarf_Addr value;
300   if (dwarf_formaddr(&attr, &value) == 0)
301     return (uint64_t) value;
302   else
303     return 0;
304 }
305
306 static uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die* die, int attribute, uint64_t default_value) {
307   Dwarf_Attribute attr;
308   if (dwarf_attr_integrate(die, attribute, &attr)==NULL)
309     return default_value;
310   Dwarf_Word value;
311   return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr), &value) == 0 ? (uint64_t) value : default_value;
312 }
313
314 static bool MC_dwarf_attr_flag(Dwarf_Die* die, int attribute, bool integrate) {
315   Dwarf_Attribute attr;
316   if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
317                     : dwarf_attr(die, attribute, &attr))==0)
318     return false;
319
320   bool result;
321   if (dwarf_formflag(&attr, &result))
322     xbt_die("Unexpected form for attribute %s",
323       MC_dwarf_attrname(attribute));
324   return result;
325 }
326
327 /** \brief Find the default lower bound for a given language
328  *
329  *  The default lower bound of an array (when DW_TAG_lower_bound
330  *  is missing) depends on the language of the compilation unit.
331  *
332  *  \param lang Language of the compilation unit (values defined in the DWARF spec)
333  *  \return     Default lower bound of an array in this compilation unit
334  * */
335 static uint64_t MC_dwarf_default_lower_bound(int lang) {
336   switch(lang) {
337   case DW_LANG_C:
338   case DW_LANG_C89:
339   case DW_LANG_C99:
340   case DW_LANG_C_plus_plus:
341   case DW_LANG_D:
342   case DW_LANG_Java:
343   case DW_LANG_ObjC:
344   case DW_LANG_ObjC_plus_plus:
345   case DW_LANG_Python:
346   case DW_LANG_UPC:
347     return 0;
348   case DW_LANG_Ada83:
349   case DW_LANG_Ada95:
350   case DW_LANG_Fortran77:
351   case DW_LANG_Fortran90:
352   case DW_LANG_Fortran95:
353   case DW_LANG_Modula2:
354   case DW_LANG_Pascal83:
355   case DW_LANG_PL1:
356   case DW_LANG_Cobol74:
357   case DW_LANG_Cobol85:
358     return 1;
359   default:
360     xbt_die("No default DW_TAG_lower_bound for language %i and none given", lang);
361     return 0;
362   }
363 }
364
365 /** \brief Finds the number of elements in a DW_TAG_subrange_type or DW_TAG_enumeration_type DIE
366  *
367  *  \param die  the DIE
368  *  \param unit DIE of the compilation unit
369  *  \return     number of elements in the range
370  * */
371 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
372   xbt_assert(dwarf_tag(die)==DW_TAG_enumeration_type ||dwarf_tag(die)==DW_TAG_subrange_type,
373       "MC_dwarf_subrange_element_count called with DIE of type %s", MC_dwarf_die_tagname(die));
374
375   // Use DW_TAG_count if present:
376   if (dwarf_hasattr_integrate(die, DW_AT_count)) {
377     return MC_dwarf_attr_integrate_uint(die, DW_AT_count, 0);
378   }
379
380   // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
381
382   if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
383         // This is not really 0, but the code expects this (we do not know):
384     return 0;
385   }
386   uint64_t upper_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_upper_bound, -1);
387
388   uint64_t lower_bound = 0;
389   if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
390     lower_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_lower_bound, -1);
391   } else {
392         lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
393   }
394   return upper_bound - lower_bound + 1;
395 }
396
397 /** \brief Finds the number of elements in a array type (DW_TAG_array_type)
398  *
399  *  The compilation unit might be needed because the default lower
400  *  bound depends on the language of the compilation unit.
401  *
402  *  \param die the DIE of the DW_TAG_array_type
403  *  \param unit the DIE of the compilation unit
404  *  \return number of elements in this array type
405  * */
406 static uint64_t MC_dwarf_array_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
407   xbt_assert(dwarf_tag(die)==DW_TAG_array_type,
408     "MC_dwarf_array_element_count called with DIE of type %s", MC_dwarf_die_tagname(die));
409
410   int result = 1;
411   Dwarf_Die child;
412   int res;
413   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
414         int child_tag = dwarf_tag(&child);
415     if (child_tag==DW_TAG_subrange_type ||child_tag==DW_TAG_enumeration_type) {
416       result *= MC_dwarf_subrange_element_count(&child, unit);
417     }
418   }
419   return result;
420 }
421
422 // ***** dw_type_t
423
424 /** \brief Initialize the location of a member of a type
425  * (DW_AT_data_member_location of a DW_TAG_member).
426  *
427  *  \param  type   a type (struct, class)
428  *  \param  member the member of the type
429  *  \param  child  DIE of the member (DW_TAG_member)
430  */
431 static void MC_dwarf_fill_member_location(dw_type_t type, dw_type_t member, Dwarf_Die* child) {
432   if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
433     xbt_die("Can't groke DW_AT_data_bit_offset.");
434   }
435
436   if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
437     if (type->type != DW_TAG_union_type) {
438         xbt_die(
439           "Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%p>%s",
440           member->name, type->id, type->name);
441     } else {
442       return;
443     }
444   }
445
446   Dwarf_Attribute attr;
447   dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
448   int form = dwarf_whatform(&attr);
449   int klass = MC_dwarf_form_get_class(form);
450   switch (klass) {
451   case MC_DW_CLASS_EXPRLOC:
452   case MC_DW_CLASS_BLOCK:
453     // Location expression:
454     {
455       Dwarf_Op* expr;
456       size_t len;
457       if (dwarf_getlocation(&attr, &expr, &len)) {
458         xbt_die(
459           "Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%p>%s",
460           MC_dwarf_attr_integrate_string(child, DW_AT_name),
461           type->id, type->name);
462       }
463       if (len==1 && expr[0].atom == DW_OP_plus_uconst) {
464         member->offset =  expr[0].number;
465       } else {
466         mc_dwarf_expression_init(&member->location, len, expr);
467       }
468       break;
469     }
470   case MC_DW_CLASS_CONSTANT:
471     // Offset from the base address of the object:
472     {
473       Dwarf_Word offset;
474       if (!dwarf_formudata(&attr, &offset))
475         member->offset = offset;
476       else
477         xbt_die("Cannot get %s location <%p>%s",
478           MC_dwarf_attr_integrate_string(child, DW_AT_name),
479           type->id, type->name);
480       break;
481     }
482   case MC_DW_CLASS_LOCLISTPTR:
483     // Reference to a location list:
484     // TODO
485   case MC_DW_CLASS_REFERENCE:
486     // It's supposed to be possible in DWARF2 but I couldn't find its semantic
487     // in the spec.
488   default:
489     xbt_die(
490       "Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
491       klass, form);
492   }
493
494 }
495
496 static void dw_type_free_voidp(void *t){
497   dw_type_free((dw_type_t) * (void **) t);
498 }
499
500 /** \brief Populate the list of members of a type
501  *
502  *  \param info ELF object containing the type DIE
503  *  \param die  DIE of the type
504  *  \param unit DIE of the compilation unit containing the type DIE
505  *  \param type the type
506  */
507 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_type_t type) {
508   int res;
509   Dwarf_Die child;
510   xbt_assert(!type->members);
511   type->members = xbt_dynar_new(sizeof(dw_type_t), (void(*)(void*))dw_type_free_voidp);
512   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
513     int tag = dwarf_tag(&child);
514     if (tag==DW_TAG_member || tag==DW_TAG_inheritance) {
515
516       // Skip declarations:
517       if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
518         continue;
519
520       // Skip compile time constants:
521       if(dwarf_hasattr(&child, DW_AT_const_value))
522         continue;
523
524       // TODO, we should use another type (because is is not a type but a member)
525       dw_type_t member = xbt_new0(s_dw_type_t, 1);
526       member->type = tag;
527
528       // Global Offset:
529       member->id = (void *) dwarf_dieoffset(&child);
530
531       const char* name = MC_dwarf_attr_integrate_string(&child, DW_AT_name);
532       if(name)
533         member->name = xbt_strdup(name);
534       else
535         member->name = NULL;
536
537       member->byte_size = MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
538       member->element_count = -1;
539       member->dw_type_id = MC_dwarf_at_type(&child);
540       member->members = NULL;
541       member->is_pointer_type = 0;
542       member->offset = 0;
543
544       if(dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
545         xbt_die("Can't groke DW_AT_data_bit_offset.");
546       }
547
548       MC_dwarf_fill_member_location(type, member, &child);
549
550       if (!member->dw_type_id) {
551         xbt_die("Missing type for member %s of <%p>%s", member->name, type->id, type->name);
552       }
553
554       xbt_dynar_push(type->members, &member);
555     }
556   }
557 }
558
559 /** \brief Create a MC type object from a DIE
560  *
561  *  \param info current object info object
562  *  \param DIE (for a given type);
563  *  \param unit compilation unit of the current DIE
564  *  \return MC representation of the type
565  */
566 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) {
567
568   dw_type_t type = xbt_new0(s_dw_type_t, 1);
569   type->type = -1;
570   type->id = NULL;
571   type->name = NULL;
572   type->byte_size = 0;
573   type->element_count = -1;
574   type->dw_type_id = NULL;
575   type->members = NULL;
576   type->is_pointer_type = 0;
577   type->offset = 0;
578
579   type->type = dwarf_tag(die);
580
581   // Global Offset
582   type->id = (void *) dwarf_dieoffset(die);
583
584   const char* prefix = "";
585   switch (type->type) {
586   case DW_TAG_structure_type:
587     prefix = "struct ";
588     break;
589   case DW_TAG_union_type:
590     prefix = "union ";
591     break;
592   case DW_TAG_class_type:
593     prefix = "class ";
594     break;
595   default:
596     prefix = "";
597   }
598
599   const char* name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
600   if (name!=NULL) {
601     type->name = namespace ? bprintf("%s%s::%s", prefix, namespace, name) : bprintf("%s%s", prefix, name);
602   }
603
604   XBT_DEBUG("Processing type <%p>%s", type->id, type->name);
605
606   type->dw_type_id = MC_dwarf_at_type(die);
607
608   // Computation of the byte_size;
609   if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
610     type->byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0);
611   else if (type->type == DW_TAG_array_type || type->type==DW_TAG_structure_type || type->type==DW_TAG_class_type) {
612     Dwarf_Word size;
613     if (dwarf_aggregate_size(die, &size)==0) {
614       type->byte_size = size;
615     }
616   }
617
618   switch (type->type) {
619   case DW_TAG_array_type:
620         type->element_count = MC_dwarf_array_element_count(die, unit);
621         // TODO, handle DW_byte_stride and (not) DW_bit_stride
622         break;
623
624   case DW_TAG_pointer_type:
625   case DW_TAG_reference_type:
626   case DW_TAG_rvalue_reference_type:
627     type->is_pointer_type = 1;
628     break;
629
630   case DW_TAG_structure_type:
631   case DW_TAG_union_type:
632   case DW_TAG_class_type:
633           MC_dwarf_add_members(info, die, unit, type);
634           char* new_namespace = namespace == NULL ? xbt_strdup(type->name)
635             : bprintf("%s::%s", namespace, name);
636           MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
637           free(new_namespace);
638           break;
639   }
640
641   return type;
642 }
643
644 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) {
645   dw_type_t type = MC_dwarf_die_to_type(info, die, unit, frame, namespace);
646
647   char* key = bprintf("%" PRIx64, (uint64_t) type->id);
648   xbt_dict_set(info->types, key, type, NULL);
649   xbt_free(key);
650
651   if(type->name && type->byte_size!=0) {
652     xbt_dict_set(info->full_types_by_name, type->name, type, NULL);
653   }
654 }
655
656 static int mc_anonymous_variable_index = 0;
657
658 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) {
659   // Skip declarations:
660   if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
661     return NULL;
662
663   // Skip compile time constants:
664   if(dwarf_hasattr(die, DW_AT_const_value))
665     return NULL;
666
667   Dwarf_Attribute attr_location;
668   if (dwarf_attr(die, DW_AT_location, &attr_location)==NULL) {
669     // No location: do not add it ?
670     return NULL;
671   }
672
673   dw_variable_t variable = xbt_new0(s_dw_variable_t, 1);
674   variable->dwarf_offset = dwarf_dieoffset(die);
675   variable->global = frame == NULL; // Can be override base on DW_AT_location
676
677   const char* name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
678   variable->name = xbt_strdup(name);
679
680   variable->type_origin = MC_dwarf_at_type(die);
681
682   int form = dwarf_whatform(&attr_location);
683   int klass = form == DW_FORM_sec_offset ? MC_DW_CLASS_CONSTANT : MC_dwarf_form_get_class(form);
684   switch (klass) {
685   case MC_DW_CLASS_EXPRLOC:
686   case MC_DW_CLASS_BLOCK:
687     // Location expression:
688     {
689       Dwarf_Op* expr;
690       size_t len;
691       if (dwarf_getlocation(&attr_location, &expr, &len)) {
692         xbt_die(
693           "Could not read location expression in DW_AT_location of variable <%p>%s",
694           (void*) variable->dwarf_offset, variable->name);
695       }
696
697       if (len==1 && expr[0].atom == DW_OP_addr) {
698         variable->global = 1;
699         Dwarf_Off offset = expr[0].number;
700         // TODO, Why is this different base on the object?
701         Dwarf_Off base = strcmp(info->file_name, xbt_binary_name) !=0 ? (Dwarf_Off) info->start_exec : 0;
702         variable->address = (void*) (base + offset);
703       } else {
704         mc_dwarf_location_list_init_from_expression(&variable->locations, len, expr);
705       }
706
707       break;
708     }
709   case MC_DW_CLASS_LOCLISTPTR:
710   case MC_DW_CLASS_CONSTANT:
711     // Reference to location list:
712     mc_dwarf_location_list_init(&variable->locations, info, die, &attr_location);
713     break;
714   default:
715     xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location in <%p>%s",
716       form, form, klass, klass, (void*) variable->dwarf_offset, variable->name);
717   }
718
719   // Handle start_scope:
720   if (dwarf_hasattr(die, DW_AT_start_scope)) {
721     Dwarf_Attribute attr;
722     dwarf_attr(die, DW_AT_start_scope, &attr);
723     int form  = dwarf_whatform(&attr);
724     int klass = MC_dwarf_form_get_class(form);
725     switch(klass) {
726     case MC_DW_CLASS_CONSTANT:
727     {
728       Dwarf_Word value;
729       variable->start_scope = dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
730       break;
731     }
732     default:
733       xbt_die("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
734         form, klass, name==NULL ? "?" : name);
735     }
736   }
737
738   if(namespace && variable->global) {
739     char* old_name = variable->name;
740     variable->name = bprintf("%s::%s", namespace, old_name);
741     free(old_name);
742   }
743
744   // The current code needs a variable name,
745   // generate a fake one:
746   if(!variable->name) {
747     variable->name = bprintf("@anonymous#%i", mc_anonymous_variable_index++);
748   }
749
750   return variable;
751 }
752
753 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) {
754   dw_variable_t variable = MC_die_to_variable(info, die, unit, frame, namespace);
755   if(variable==NULL)
756       return;
757   MC_dwarf_register_variable(info, frame, variable);
758 }
759
760 static void mc_frame_free_voipd(dw_frame_t* p) {
761   mc_frame_free(*p);
762   *p = NULL;
763 }
764
765 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) {
766   // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt
767   int tag = dwarf_tag(die);
768   mc_tag_class klass = MC_dwarf_tag_classify(tag);
769
770   // (Template) Subprogram declaration:
771   if(klass==mc_tag_subprogram && MC_dwarf_attr_flag(die, DW_AT_declaration, false))
772     return;
773
774   if(klass==mc_tag_scope)
775     xbt_assert(parent_frame, "No parent scope for this scope");
776
777   dw_frame_t frame = xbt_new0(s_dw_frame_t, 1);
778
779   frame->tag   = tag;
780   frame->start = dwarf_dieoffset(die);
781
782   const char* name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
783   if(name)
784     frame->name = namespace ? bprintf("%s::%s", namespace, name) : xbt_strdup(name);
785
786   // This is the base address for DWARF addresses.
787   // Relocated addresses are offset from this base address.
788   // See DWARF4 spec 7.5
789   void* base = info->flags & MC_OBJECT_INFO_EXECUTABLE ? 0 : MC_object_base_address(info);
790
791   // Variables are filled in the (recursive) call of MC_dwarf_handle_children:
792   frame->variables = xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
793   frame->high_pc = ((char*) base) + MC_dwarf_attr_integrate_addr(die, DW_AT_high_pc);
794   frame->low_pc = ((char*) base) + MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
795
796   if(klass==mc_tag_subprogram) {
797     Dwarf_Attribute attr_frame_base;
798     if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
799       mc_dwarf_location_list_init(&frame->frame_base, info, die, &attr_frame_base);
800   }
801
802   frame->scopes = xbt_dynar_new(sizeof(dw_frame_t), (void_f_pvoid_t) mc_frame_free_voipd);
803
804   // Register it:
805   if(klass==mc_tag_subprogram) {
806     char* key = bprintf("%" PRIx64, (uint64_t) frame->start);
807     xbt_dict_set(info->subprograms,  key, frame, NULL);
808     xbt_free(key);
809   } else if(klass==mc_tag_scope) {
810     xbt_dynar_push(parent_frame->scopes, &frame);
811   }
812
813   // Handle children:
814   MC_dwarf_handle_children(info, die, unit, frame, namespace);
815 }
816
817 static void mc_dwarf_handle_namespace_die(
818     mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
819   const char* name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
820   if(frame)
821     xbt_die("Unexpected namespace in a subprogram");
822   char* new_namespace = namespace == NULL ? xbt_strdup(name)
823     : bprintf("%s::%s", namespace, name);
824   MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
825   xbt_free(new_namespace);
826 }
827
828 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
829   // For each child DIE:
830   Dwarf_Die child;
831   int res;
832   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
833     MC_dwarf_handle_die(info, &child, unit, frame, namespace);
834   }
835 }
836
837 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
838   int tag = dwarf_tag(die);
839   mc_tag_class klass = MC_dwarf_tag_classify(tag);
840   switch (klass) {
841
842     // Type:
843     case mc_tag_type:
844       MC_dwarf_handle_type_die(info, die, unit, frame, namespace);
845       break;
846
847     // Subprogram or scope:
848     case mc_tag_subprogram:
849     case mc_tag_scope:
850       MC_dwarf_handle_scope_die(info, die, unit, frame, namespace);
851       return;
852
853     // Variable:
854     case mc_tag_variable:
855       MC_dwarf_handle_variable_die(info, die, unit, frame, namespace);
856       break;
857
858     case mc_tag_namespace:
859       mc_dwarf_handle_namespace_die(info, die, unit, frame, namespace);
860       break;
861
862     default:
863       break;
864
865   }
866 }
867
868 /** \brief Populate the debugging informations of the given ELF object
869  *
870  *  Read the DWARf information of the EFFL object and populate the
871  *  lists of types, variables, functions.
872  */
873 void MC_dwarf_get_variables(mc_object_info_t info) {
874   int fd = open(info->file_name, O_RDONLY);
875   if (fd<0) {
876     xbt_die("Could not open file %s", info->file_name);
877   }
878   Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
879   if (dwarf==NULL) {
880     xbt_die("Your program must be compiled with -g");
881   }
882
883   // For each compilation unit:
884   Dwarf_Off offset = 0;
885   Dwarf_Off next_offset = 0;
886   size_t length;
887   while (dwarf_nextcu (dwarf, offset, &next_offset, &length, NULL, NULL, NULL) == 0) {
888     Dwarf_Die unit_die;
889     if(dwarf_offdie(dwarf, offset+length, &unit_die)!=NULL) {
890
891       // For each child DIE:
892       Dwarf_Die child;
893       int res;
894       for (res=dwarf_child(&unit_die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
895         MC_dwarf_handle_die(info, &child, &unit_die, NULL, NULL);
896       }
897
898     }
899     offset = next_offset;
900   }
901
902   dwarf_end(dwarf);
903   close(fd);
904 }