Logo AND Algorithmique Numérique Distribuée

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