Logo AND Algorithmique Numérique Distribuée

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