Logo AND Algorithmique Numérique Distribuée

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