Logo AND Algorithmique Numérique Distribuée

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