Logo AND Algorithmique Numérique Distribuée

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