Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
privatization : perform the initialization at process launch, not at MPI_Init time
[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 <%"PRIx64">%s",
461           member->name, (uint64_t) 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 <%"PRIx64">%s",
481           MC_dwarf_attr_integrate_string(child, DW_AT_name),
482           (uint64_t) 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 <%"PRIx64">%s",
499           MC_dwarf_attr_integrate_string(child, DW_AT_name),
500           (uint64_t) 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 = 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 <%"PRIx64">%s", member->name, (uint64_t) 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 = 0;
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 = 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   type->dw_type_id = MC_dwarf_at_type(die);
626
627   // Computation of the byte_size;
628   if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
629     type->byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0);
630   else if (type->type == DW_TAG_array_type || type->type==DW_TAG_structure_type || type->type==DW_TAG_class_type) {
631     Dwarf_Word size;
632     if (dwarf_aggregate_size(die, &size)==0) {
633       type->byte_size = size;
634     }
635   }
636
637   switch (type->type) {
638   case DW_TAG_array_type:
639         type->element_count = MC_dwarf_array_element_count(die, unit);
640         // TODO, handle DW_byte_stride and (not) DW_bit_stride
641         break;
642
643   case DW_TAG_pointer_type:
644   case DW_TAG_reference_type:
645   case DW_TAG_rvalue_reference_type:
646     type->is_pointer_type = 1;
647     break;
648
649   case DW_TAG_structure_type:
650   case DW_TAG_union_type:
651   case DW_TAG_class_type:
652           MC_dwarf_add_members(info, die, unit, type);
653           char* new_namespace = namespace == NULL ? xbt_strdup(type->name)
654             : bprintf("%s::%s", namespace, name);
655           MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
656           free(new_namespace);
657           break;
658   }
659
660   return type;
661 }
662
663 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) {
664   dw_type_t type = MC_dwarf_die_to_type(info, die, unit, frame, namespace);
665
666   char* key = bprintf("%" PRIx64, (uint64_t) type->id);
667   xbt_dict_set(info->types, key, type, NULL);
668   xbt_free(key);
669
670   if(type->name && type->byte_size!=0) {
671     xbt_dict_set(info->full_types_by_name, type->name, type, NULL);
672   }
673 }
674
675 static int mc_anonymous_variable_index = 0;
676
677 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) {
678   // Skip declarations:
679   if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
680     return NULL;
681
682   // Skip compile time constants:
683   if(dwarf_hasattr(die, DW_AT_const_value))
684     return NULL;
685
686   Dwarf_Attribute attr_location;
687   if (dwarf_attr(die, DW_AT_location, &attr_location)==NULL) {
688     // No location: do not add it ?
689     return NULL;
690   }
691
692   dw_variable_t variable = xbt_new0(s_dw_variable_t, 1);
693   variable->dwarf_offset = dwarf_dieoffset(die);
694   variable->global = frame == NULL; // Can be override base on DW_AT_location
695   variable->object_info = info;
696
697   const char* name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
698   variable->name = xbt_strdup(name);
699
700   variable->type_origin = MC_dwarf_at_type(die);
701
702   int form = dwarf_whatform(&attr_location);
703   int klass = form == DW_FORM_sec_offset ? MC_DW_CLASS_CONSTANT : MC_dwarf_form_get_class(form);
704   switch (klass) {
705   case MC_DW_CLASS_EXPRLOC:
706   case MC_DW_CLASS_BLOCK:
707     // Location expression:
708     {
709       Dwarf_Op* expr;
710       size_t len;
711       if (dwarf_getlocation(&attr_location, &expr, &len)) {
712         xbt_die(
713           "Could not read location expression in DW_AT_location of variable <%p>%s",
714           (void*) variable->dwarf_offset, variable->name);
715       }
716
717       if (len==1 && expr[0].atom == DW_OP_addr) {
718         variable->global = 1;
719         Dwarf_Off offset = expr[0].number;
720         Dwarf_Off base = (Dwarf_Off) MC_object_base_address(info);
721         variable->address = (void*) (base + offset);
722       } else {
723         mc_dwarf_location_list_init_from_expression(&variable->locations, len, expr);
724       }
725
726       break;
727     }
728   case MC_DW_CLASS_LOCLISTPTR:
729   case MC_DW_CLASS_CONSTANT:
730     // Reference to location list:
731     mc_dwarf_location_list_init(&variable->locations, info, die, &attr_location);
732     break;
733   default:
734     xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location in <%p>%s",
735       form, form, klass, klass, (void*) variable->dwarf_offset, variable->name);
736   }
737
738   // Handle start_scope:
739   if (dwarf_hasattr(die, DW_AT_start_scope)) {
740     Dwarf_Attribute attr;
741     dwarf_attr(die, DW_AT_start_scope, &attr);
742     int form  = dwarf_whatform(&attr);
743     int klass = MC_dwarf_form_get_class(form);
744     switch(klass) {
745     case MC_DW_CLASS_CONSTANT:
746     {
747       Dwarf_Word value;
748       variable->start_scope = dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
749       break;
750     }
751     case MC_DW_CLASS_RANGELISTPTR: // TODO
752     default:
753       xbt_die("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
754         form, klass, name==NULL ? "?" : name);
755     }
756   }
757
758   if(namespace && variable->global) {
759     char* old_name = variable->name;
760     variable->name = bprintf("%s::%s", namespace, old_name);
761     free(old_name);
762   }
763
764   // The current code needs a variable name,
765   // generate a fake one:
766   if(!variable->name) {
767     variable->name = bprintf("@anonymous#%i", mc_anonymous_variable_index++);
768   }
769
770   return variable;
771 }
772
773 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) {
774   dw_variable_t variable = MC_die_to_variable(info, die, unit, frame, namespace);
775   if(variable==NULL)
776       return;
777   MC_dwarf_register_variable(info, frame, variable);
778 }
779
780 static void mc_frame_free_voipd(dw_frame_t* p) {
781   mc_frame_free(*p);
782   *p = NULL;
783 }
784
785 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) {
786   // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt
787   int tag = dwarf_tag(die);
788   mc_tag_class klass = MC_dwarf_tag_classify(tag);
789
790   // (Template) Subprogram declaration:
791   if(klass==mc_tag_subprogram && MC_dwarf_attr_flag(die, DW_AT_declaration, false))
792     return;
793
794   if(klass==mc_tag_scope)
795     xbt_assert(parent_frame, "No parent scope for this scope");
796
797   dw_frame_t frame = xbt_new0(s_dw_frame_t, 1);
798
799   frame->tag   = tag;
800   frame->id = dwarf_dieoffset(die);
801   frame->object_info = info;
802
803   if(klass==mc_tag_subprogram) {
804     const char* name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
805     frame->name = namespace ? bprintf("%s::%s", namespace, name) : xbt_strdup(name);
806   }
807
808   frame->abstract_origin_id = MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin);
809
810   // This is the base address for DWARF addresses.
811   // Relocated addresses are offset from this base address.
812   // See DWARF4 spec 7.5
813   void* base = MC_object_base_address(info);
814
815   // Variables are filled in the (recursive) call of MC_dwarf_handle_children:
816   frame->variables = xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
817
818   // TODO, support DW_AT_ranges
819   uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
820   frame->low_pc = low_pc ? ((char*) base) + low_pc : 0;
821   if(low_pc) {
822     // DW_AT_high_pc:
823     Dwarf_Attribute attr;
824     if(!dwarf_attr_integrate(die, DW_AT_high_pc, &attr)) {
825       xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc");
826     }
827
828     Dwarf_Sword offset;
829     Dwarf_Addr high_pc;
830
831     switch(MC_dwarf_form_get_class(dwarf_whatform(&attr))) {
832
833     // DW_AT_high_pc if an offset from the low_pc:
834     case MC_DW_CLASS_CONSTANT:
835
836       if (dwarf_formsdata(&attr, &offset) !=0)
837         xbt_die("Could not read constant");
838       frame->high_pc = (void*) ((Dwarf_Off)frame->low_pc + offset);
839       break;
840
841     // DW_AT_high_pc is a relocatable address:
842     case MC_DW_CLASS_ADDRESS:
843       if (dwarf_formaddr(&attr, &high_pc) != 0)
844         xbt_die("Could not read address");
845       frame->high_pc = ((char*) base) + high_pc;
846       break;
847
848     default:
849       xbt_die("Unexpected class for DW_AT_high_pc");
850
851     }
852   }
853
854   if(klass==mc_tag_subprogram) {
855     Dwarf_Attribute attr_frame_base;
856     if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
857       mc_dwarf_location_list_init(&frame->frame_base, info, die, &attr_frame_base);
858   }
859
860   frame->scopes = xbt_dynar_new(sizeof(dw_frame_t), (void_f_pvoid_t) mc_frame_free_voipd);
861
862   // Register it:
863   if(klass==mc_tag_subprogram) {
864     char* key = bprintf("%" PRIx64, (uint64_t) frame->id);
865     xbt_dict_set(info->subprograms,  key, frame, NULL);
866     xbt_free(key);
867   } else if(klass==mc_tag_scope) {
868     xbt_dynar_push(parent_frame->scopes, &frame);
869   }
870
871   // Handle children:
872   MC_dwarf_handle_children(info, die, unit, frame, namespace);
873 }
874
875 static void mc_dwarf_handle_namespace_die(
876     mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
877   const char* name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
878   if(frame)
879     xbt_die("Unexpected namespace in a subprogram");
880   char* new_namespace = namespace == NULL ? xbt_strdup(name)
881     : bprintf("%s::%s", namespace, name);
882   MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
883   xbt_free(new_namespace);
884 }
885
886 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
887   // For each child DIE:
888   Dwarf_Die child;
889   int res;
890   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
891     MC_dwarf_handle_die(info, &child, unit, frame, namespace);
892   }
893 }
894
895 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
896   int tag = dwarf_tag(die);
897   mc_tag_class klass = MC_dwarf_tag_classify(tag);
898   switch (klass) {
899
900     // Type:
901     case mc_tag_type:
902       MC_dwarf_handle_type_die(info, die, unit, frame, namespace);
903       break;
904
905     // Subprogram or scope:
906     case mc_tag_subprogram:
907     case mc_tag_scope:
908       MC_dwarf_handle_scope_die(info, die, unit, frame, namespace);
909       return;
910
911     // Variable:
912     case mc_tag_variable:
913       MC_dwarf_handle_variable_die(info, die, unit, frame, namespace);
914       break;
915
916     case mc_tag_namespace:
917       mc_dwarf_handle_namespace_die(info, die, unit, frame, namespace);
918       break;
919
920     default:
921       break;
922
923   }
924 }
925
926 /** \brief Populate the debugging informations of the given ELF object
927  *
928  *  Read the DWARf information of the EFFL object and populate the
929  *  lists of types, variables, functions.
930  */
931 void MC_dwarf_get_variables(mc_object_info_t info) {
932   int fd = open(info->file_name, O_RDONLY);
933   if (fd<0) {
934     xbt_die("Could not open file %s", info->file_name);
935   }
936   Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
937   if (dwarf==NULL) {
938     xbt_die("Your program must be compiled with -g");
939   }
940
941   // For each compilation unit:
942   Dwarf_Off offset = 0;
943   Dwarf_Off next_offset = 0;
944   size_t length;
945   while (dwarf_nextcu (dwarf, offset, &next_offset, &length, NULL, NULL, NULL) == 0) {
946     Dwarf_Die unit_die;
947     if(dwarf_offdie(dwarf, offset+length, &unit_die)!=NULL) {
948
949       // For each child DIE:
950       Dwarf_Die child;
951       int res;
952       for (res=dwarf_child(&unit_die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
953         MC_dwarf_handle_die(info, &child, &unit_die, NULL, NULL);
954       }
955
956     }
957     offset = next_offset;
958   }
959
960   dwarf_end(dwarf);
961   close(fd);
962 }