Logo AND Algorithmique Numérique Distribuée

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