Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
871e55b60c9b344f5f5e1c095bbe512c66a85a12
[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, const char* namespace);
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, dw_frame_t frame, const char* namespace);
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, const char* namespace);
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, const char* namespace);
76
77 /** \brief Get the DW_TAG_type of the DIE
78  *
79  *  \param die DIE
80  *  \return DW_TAG_type attribute as a new string (NULL if none)
81  */
82 static char* MC_dwarf_at_type(Dwarf_Die* die);
83
84 /** \brief Get the name of an attribute (DW_AT_*) from its code
85  *
86  *  \param attr attribute code (see the DWARF specification)
87  *  \return name of the attribute
88  */
89 const char* MC_dwarf_attrname(int attr) {
90   switch (attr) {
91 #include "mc_dwarf_attrnames.h"
92   default:
93     return "DW_AT_unknown";
94   }
95 }
96
97 /** \brief Get the name of a dwarf tag (DW_TAG_*) from its code
98  *
99  *  \param tag tag code (see the DWARF specification)
100  *  \return name of the tag
101  */
102 const char* MC_dwarf_tagname(int tag) {
103   switch (tag) {
104 #include "mc_dwarf_tagnames.h"
105   case DW_TAG_invalid:
106     return "DW_TAG_invalid";
107   default:
108     return "DW_TAG_unknown";
109   }
110 }
111
112 /** \brief A class of DWARF tags (DW_TAG_*)
113  */
114 typedef enum mc_tag_class {
115   mc_tag_unknown,
116   mc_tag_type,
117   mc_tag_subprogram,
118   mc_tag_variable,
119   mc_tag_scope,
120   mc_tag_namespace
121 } mc_tag_class;
122
123 static mc_tag_class MC_dwarf_tag_classify(int tag) {
124   switch (tag) {
125
126     case DW_TAG_array_type:
127     case DW_TAG_class_type:
128     case DW_TAG_enumeration_type:
129     case DW_TAG_typedef:
130     case DW_TAG_pointer_type:
131     case DW_TAG_reference_type:
132     case DW_TAG_rvalue_reference_type:
133     case DW_TAG_string_type:
134     case DW_TAG_structure_type:
135     case DW_TAG_subroutine_type:
136     case DW_TAG_union_type:
137     case DW_TAG_ptr_to_member_type:
138     case DW_TAG_set_type:
139     case DW_TAG_subrange_type:
140     case DW_TAG_base_type:
141     case DW_TAG_const_type:
142     case DW_TAG_file_type:
143     case DW_TAG_packed_type:
144     case DW_TAG_volatile_type:
145     case DW_TAG_restrict_type:
146     case DW_TAG_interface_type:
147     case DW_TAG_unspecified_type:
148     case DW_TAG_mutable_type:
149     case DW_TAG_shared_type:
150       return mc_tag_type;
151
152     case DW_TAG_subprogram:
153       return mc_tag_subprogram;
154
155     case DW_TAG_variable:
156     case DW_TAG_formal_parameter:
157       return mc_tag_variable;
158
159     case DW_TAG_lexical_block:
160     case DW_TAG_try_block:
161     case DW_TAG_inlined_subroutine:
162       return mc_tag_scope;
163
164     case DW_TAG_namespace:
165       return mc_tag_namespace;
166
167     default:
168       return mc_tag_unknown;
169
170   }
171 }
172
173 #define MC_DW_CLASS_UNKNOWN 0
174 #define MC_DW_CLASS_ADDRESS 1   // Location in the address space of the program
175 #define MC_DW_CLASS_BLOCK 2     // Arbitrary block of bytes
176 #define MC_DW_CLASS_CONSTANT 3
177 #define MC_DW_CLASS_STRING 3    // String
178 #define MC_DW_CLASS_FLAG 4      // Boolean
179 #define MC_DW_CLASS_REFERENCE 5 // Reference to another DIE
180 #define MC_DW_CLASS_EXPRLOC 6   // DWARF expression/location description
181 #define MC_DW_CLASS_LINEPTR 7
182 #define MC_DW_CLASS_LOCLISTPTR 8
183 #define MC_DW_CLASS_MACPTR 9
184 #define MC_DW_CLASS_RANGELISTPTR 10
185
186 static int MC_dwarf_form_get_class(int form) {
187   switch(form) {
188   case DW_FORM_addr:
189     return MC_DW_CLASS_ADDRESS;
190   case DW_FORM_block2:
191   case DW_FORM_block4:
192   case DW_FORM_block:
193   case DW_FORM_block1:
194     return MC_DW_CLASS_BLOCK;
195   case DW_FORM_data1:
196   case DW_FORM_data2:
197   case DW_FORM_data4:
198   case DW_FORM_data8:
199   case DW_FORM_udata:
200   case DW_FORM_sdata:
201     return MC_DW_CLASS_CONSTANT;
202   case DW_FORM_string:
203   case DW_FORM_strp:
204     return MC_DW_CLASS_STRING;
205   case DW_FORM_ref_addr:
206   case DW_FORM_ref1:
207   case DW_FORM_ref2:
208   case DW_FORM_ref4:
209   case DW_FORM_ref8:
210   case DW_FORM_ref_udata:
211     return MC_DW_CLASS_REFERENCE;
212   case DW_FORM_flag:
213   case DW_FORM_flag_present:
214     return MC_DW_CLASS_FLAG;
215   case DW_FORM_exprloc:
216     return MC_DW_CLASS_EXPRLOC;
217   // TODO sec offset
218   // TODO indirect
219   default:
220     return MC_DW_CLASS_UNKNOWN;
221   }
222 }
223
224 /** \brief Get the name of the tag of a given DIE
225  *
226  *  \param die DIE
227  *  \return name of the tag of this DIE
228  */
229 static inline const char* MC_dwarf_die_tagname(Dwarf_Die* die) {
230   return MC_dwarf_tagname(dwarf_tag(die));
231 }
232
233 // ***** Attributes
234
235 /** \brief Get an attribute of a given DIE as a string
236  *
237  *  \param the DIE
238  *  \param attribute attribute
239  *  \return value of the given attribute of the given DIE
240  */
241 static const char* MC_dwarf_attr_string(Dwarf_Die* die, int attribute) {
242   Dwarf_Attribute attr;
243   if (!dwarf_attr_integrate(die, attribute, &attr)) {
244         return NULL;
245   } else {
246         return dwarf_formstring(&attr);
247   }
248 }
249
250 /** \brief Get the linkage name of a DIE.
251  *
252  *  Use either DW_AT_linkage_name or DW_AR_MIPS_linkage_name.
253  *
254  *  \param DIE
255  *  \return linkage name of the given DIE (or NULL)
256  * */
257 static const char* MC_dwarf_at_linkage_name(Dwarf_Die* die) {
258   const char* name = MC_dwarf_attr_string(die, DW_AT_linkage_name);
259   if (!name)
260     name = MC_dwarf_attr_string(die, DW_AT_MIPS_linkage_name);
261   return name;
262 }
263
264 static char* MC_dwarf_at_type(Dwarf_Die* die) {
265   Dwarf_Attribute attr;
266   if (dwarf_hasattr_integrate(die, DW_AT_type)) {
267         dwarf_attr_integrate(die, DW_AT_type, &attr);
268         Dwarf_Die subtype_die;
269         if (dwarf_formref_die(&attr, &subtype_die)==NULL) {
270           xbt_die("Could not find DIE for type");
271         }
272         Dwarf_Off subtype_global_offset = dwarf_dieoffset(&subtype_die);
273     return bprintf("%" PRIx64 , subtype_global_offset);
274   }
275   else return NULL;
276 }
277
278 static uint64_t MC_dwarf_attr_addr(Dwarf_Die* die, int attribute) {
279   Dwarf_Attribute attr;
280   if(dwarf_attr_integrate(die, attribute, &attr)==NULL)
281     return 0;
282   Dwarf_Addr value;
283   if (dwarf_formaddr(&attr, &value) == 0)
284     return (uint64_t) value;
285   else
286     return 0;
287 }
288
289 static uint64_t MC_dwarf_attr_uint(Dwarf_Die* die, int attribute, uint64_t default_value) {
290   Dwarf_Attribute attr;
291   if (dwarf_attr_integrate(die, attribute, &attr)==NULL)
292     return default_value;
293   Dwarf_Word value;
294   return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr), &value) == 0 ? (uint64_t) value : default_value;
295 }
296
297 static bool MC_dwarf_attr_flag(Dwarf_Die* die, int attribute, int integrate) {
298   Dwarf_Attribute attr;
299   if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
300                     : dwarf_attr(die, attribute, &attr))==0)
301     return false;
302
303   bool result;
304   if (dwarf_formflag(&attr, &result))
305     xbt_die("Unexpected form for attribute %s",
306       MC_dwarf_attrname(attribute));
307   return result;
308 }
309
310 static uint64_t MC_dwarf_default_lower_bound(int lang) {
311   switch(lang) {
312   case DW_LANG_C:
313   case DW_LANG_C89:
314   case DW_LANG_C99:
315   case DW_LANG_C_plus_plus:
316   case DW_LANG_D:
317   case DW_LANG_Java:
318   case DW_LANG_ObjC:
319   case DW_LANG_ObjC_plus_plus:
320   case DW_LANG_Python:
321   case DW_LANG_UPC:
322     return 0;
323   case DW_LANG_Ada83:
324   case DW_LANG_Ada95:
325   case DW_LANG_Fortran77:
326   case DW_LANG_Fortran90:
327   case DW_LANG_Fortran95:
328   case DW_LANG_Modula2:
329   case DW_LANG_Pascal83:
330   case DW_LANG_PL1:
331   case DW_LANG_Cobol74:
332   case DW_LANG_Cobol85:
333     return 1;
334   default:
335     xbt_die("No default MT_TAG_lower_bound for language %i and none given", lang);
336     return 0;
337   }
338 }
339
340 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
341   xbt_assert(dwarf_tag(die)==DW_TAG_enumeration_type ||dwarf_tag(die)==DW_TAG_subrange_type,
342       "MC_dwarf_subrange_element_count called with DIE of type %s", MC_dwarf_die_tagname(die));
343
344   // Use DW_TAG_count if present:
345   if (dwarf_hasattr_integrate(die, DW_AT_count)) {
346     return MC_dwarf_attr_uint(die, DW_AT_count, 0);
347   }
348
349   // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
350
351   if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
352         // This is not really 0, but the code expects this (we do not know):
353     return 0;
354   }
355   uint64_t upper_bound = MC_dwarf_attr_uint(die, DW_AT_upper_bound, -1);
356
357   uint64_t lower_bound = 0;
358   if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
359     lower_bound = MC_dwarf_attr_uint(die, DW_AT_lower_bound, -1);
360   } else {
361         lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
362   }
363   return upper_bound - lower_bound + 1;
364 }
365
366 static uint64_t MC_dwarf_array_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
367   xbt_assert(dwarf_tag(die)==DW_TAG_array_type,
368     "MC_dwarf_array_element_count called with DIE of type %s", MC_dwarf_die_tagname(die));
369
370   int result = 1;
371   Dwarf_Die child;
372   int res;
373   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
374         int child_tag = dwarf_tag(&child);
375     if (child_tag==DW_TAG_subrange_type ||child_tag==DW_TAG_enumeration_type) {
376       result *= MC_dwarf_subrange_element_count(&child, unit);
377     }
378   }
379   return result;
380 }
381
382 // ***** dw_type_t
383
384 static void MC_dwarf_fill_member_location(dw_type_t type, dw_type_t member, Dwarf_Die* child) {
385   if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
386     xbt_die("Can't groke DW_AT_data_bit_offset.");
387   }
388
389   if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
390     if (type->type != DW_TAG_union_type) {
391         xbt_die(
392           "Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%p>%s",
393           member->name, type->id, type->name);
394     } else {
395       return;
396     }
397   }
398
399   Dwarf_Attribute attr;
400   dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
401   int form = dwarf_whatform(&attr);
402   int klass = MC_dwarf_form_get_class(form);
403   switch (klass) {
404   case MC_DW_CLASS_EXPRLOC:
405   case MC_DW_CLASS_BLOCK:
406     // Location expression:
407     {
408       Dwarf_Op* expr;
409       size_t len;
410       if (dwarf_getlocation(&attr, &expr, &len)) {
411         xbt_die(
412           "Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%p>%s",
413           MC_dwarf_attr_string(child, DW_AT_name),
414           type->id, type->name);
415       }
416       if (len==1 && expr[0].atom == DW_OP_plus_uconst) {
417         member->offset =  expr[0].number;
418       } else {
419         mc_dwarf_expression_init(&member->location, len, expr);
420       }
421       break;
422     }
423   case MC_DW_CLASS_CONSTANT:
424     // Offset from the base address of the object:
425     {
426       Dwarf_Word offset;
427       if (!dwarf_formudata(&attr, &offset))
428         member->offset = offset;
429       else
430         xbt_die("Cannot get %s location <%p>%s",
431           MC_dwarf_attr_string(child, DW_AT_name),
432           type->id, type->name);
433       break;
434     }
435   case MC_DW_CLASS_LOCLISTPTR:
436     // Reference to a location list:
437     // TODO
438   case MC_DW_CLASS_REFERENCE:
439     // It's supposed to be possible in DWARF2 but I couldn't find its semantic
440     // in the spec.
441   default:
442     xbt_die(
443       "Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
444       klass, form);
445   }
446
447 }
448
449 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_type_t type) {
450   int res;
451   Dwarf_Die child;
452   xbt_assert(!type->members);
453   type->members = xbt_dynar_new(sizeof(dw_type_t), (void(*)(void*))dw_type_free);
454   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
455     int tag = dwarf_tag(&child);
456     if (tag==DW_TAG_member || tag==DW_TAG_inheritance) {
457
458       // Skip declarations:
459       if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
460         continue;
461
462       // Skip compile time constants:
463       if(dwarf_hasattr(&child, DW_AT_const_value))
464         continue;
465
466       // TODO, we should use another type (because is is not a type but a member)
467       dw_type_t member = xbt_new0(s_dw_type_t, 1);
468       member->type = tag;
469
470       // Global Offset:
471       member->id = (void *) dwarf_dieoffset(&child);
472
473       const char* name = MC_dwarf_attr_string(&child, DW_AT_name);
474       if(name)
475         member->name = xbt_strdup(name);
476       else
477         member->name = NULL;
478
479       member->byte_size = MC_dwarf_attr_uint(&child, DW_AT_byte_size, 0);
480       member->element_count = -1;
481       member->dw_type_id = MC_dwarf_at_type(&child);
482       member->members = NULL;
483       member->is_pointer_type = 0;
484       member->offset = 0;
485
486       if(dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
487         xbt_die("Can't groke DW_AT_data_bit_offset.");
488       }
489
490       MC_dwarf_fill_member_location(type, member, &child);
491
492       if (!member->dw_type_id) {
493         xbt_die("Missing type for member %s of <%p>%s", member->name, type->id, type->name);
494       }
495
496       xbt_dynar_push(type->members, &member);
497     }
498   }
499 }
500
501 /** \brief Create a MC type object from a DIE
502  *
503  *  \param info current object info object
504  *  \param DIE (for a given type);
505  *  \param unit compilation unit of the current DIE
506  *  \return MC representation of the type
507  */
508 static dw_type_t MC_dwarf_die_to_type(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
509
510   dw_type_t type = xbt_new0(s_dw_type_t, 1);
511   type->type = -1;
512   type->id = NULL;
513   type->name = NULL;
514   type->byte_size = 0;
515   type->element_count = -1;
516   type->dw_type_id = NULL;
517   type->members = NULL;
518   type->is_pointer_type = 0;
519   type->offset = 0;
520
521   type->type = dwarf_tag(die);
522
523   // Global Offset
524   type->id = (void *) dwarf_dieoffset(die);
525
526   const char* prefix = "";
527   switch (type->type) {
528   case DW_TAG_structure_type:
529     prefix = "struct ";
530     break;
531   case DW_TAG_union_type:
532     prefix = "union ";
533     break;
534   case DW_TAG_class_type:
535     prefix = "class ";
536     break;
537   default:
538     prefix = "";
539   }
540
541   const char* name = MC_dwarf_attr_string(die, DW_AT_name);
542   if (name!=NULL) {
543     type->name = namespace ? bprintf("%s%s::%s", prefix, namespace, name) : bprintf("%s%s", prefix, name);
544   }
545
546   XBT_DEBUG("Processing type <%p>%s", type->id, type->name);
547
548   type->dw_type_id = MC_dwarf_at_type(die);
549
550   // Computation of the byte_size;
551   if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
552     type->byte_size = MC_dwarf_attr_uint(die, DW_AT_byte_size, 0);
553   else if (type->type == DW_TAG_array_type || type->type==DW_TAG_structure_type || type->type==DW_TAG_class_type) {
554     Dwarf_Word size;
555     if (dwarf_aggregate_size(die, &size)==0) {
556       type->byte_size = size;
557     }
558   }
559
560   switch (type->type) {
561   case DW_TAG_array_type:
562         type->element_count = MC_dwarf_array_element_count(die, unit);
563         // TODO, handle DW_byte_stride and (not) DW_bit_stride
564         break;
565
566   case DW_TAG_pointer_type:
567   case DW_TAG_reference_type:
568   case DW_TAG_rvalue_reference_type:
569     type->is_pointer_type = 1;
570     break;
571
572   case DW_TAG_structure_type:
573   case DW_TAG_union_type:
574   case DW_TAG_class_type:
575           MC_dwarf_add_members(info, die, unit, type);
576           char* new_namespace = namespace == NULL ? xbt_strdup(type->name)
577             : bprintf("%s::%s", namespace, name);
578           MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
579           free(new_namespace);
580           break;
581   }
582
583   return type;
584 }
585
586 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
587   dw_type_t type = MC_dwarf_die_to_type(info, die, unit, frame, namespace);
588
589   char* key = bprintf("%" PRIx64, (uint64_t) type->id);
590   xbt_dict_set(info->types, key, type, NULL);
591
592   if(type->name && type->byte_size!=0) {
593     xbt_dict_set(info->full_types_by_name, type->name, type, NULL);
594   }
595 }
596
597 static int mc_anonymous_variable_index = 0;
598
599 static dw_variable_t MC_die_to_variable(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
600   // Skip declarations:
601   if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
602     return NULL;
603
604   // Skip compile time constants:
605   if(dwarf_hasattr(die, DW_AT_const_value))
606     return NULL;
607
608   Dwarf_Attribute attr_location;
609   if (dwarf_attr(die, DW_AT_location, &attr_location)==NULL) {
610     // No location: do not add it ?
611     return NULL;
612   }
613
614   dw_variable_t variable = xbt_new0(s_dw_variable_t, 1);
615   variable->dwarf_offset = dwarf_dieoffset(die);
616   variable->global = frame == NULL; // Can be override base on DW_AT_location
617
618   const char* name = MC_dwarf_attr_string(die, DW_AT_name);
619   variable->name = xbt_strdup(name);
620
621   variable->type_origin = MC_dwarf_at_type(die);
622
623   int form = dwarf_whatform(&attr_location);
624   int klass = form == DW_FORM_sec_offset ? MC_DW_CLASS_CONSTANT : MC_dwarf_form_get_class(form);
625   switch (klass) {
626   case MC_DW_CLASS_EXPRLOC:
627   case MC_DW_CLASS_BLOCK:
628     // Location expression:
629     {
630       Dwarf_Op* expr;
631       size_t len;
632       if (dwarf_getlocation(&attr_location, &expr, &len)) {
633         xbt_die(
634           "Could not read location expression in DW_AT_location of variable <%p>%s",
635           (void*) variable->dwarf_offset, variable->name);
636       }
637
638       if (len==1 && expr[0].atom == DW_OP_addr) {
639         variable->global = 1;
640         Dwarf_Off offset = expr[0].number;
641         // TODO, Why is this different base on the object?
642         Dwarf_Off base = strcmp(info->file_name, xbt_binary_name) !=0 ? (Dwarf_Off) info->start_exec : 0;
643         variable->address = (void*) (base + offset);
644       } else {
645         mc_dwarf_location_list_init_from_expression(&variable->locations, len, expr);
646       }
647
648       break;
649     }
650   case MC_DW_CLASS_LOCLISTPTR:
651   case MC_DW_CLASS_CONSTANT:
652     // Reference to location list:
653     mc_dwarf_location_list_init(&variable->locations, info, die, &attr_location);
654     break;
655   default:
656     xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location in <%p>%s",
657       form, form, klass, klass, (void*) variable->dwarf_offset, variable->name);
658   }
659
660   // Handle start_scope:
661   if (dwarf_hasattr(die, DW_AT_start_scope)) {
662     Dwarf_Attribute attr;
663     dwarf_attr(die, DW_AT_start_scope, &attr);
664     int form  = dwarf_whatform(&attr);
665     int klass = MC_dwarf_form_get_class(form);
666     switch(klass) {
667     case MC_DW_CLASS_CONSTANT:
668     {
669       Dwarf_Word value;
670       variable->start_scope = dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
671       break;
672     }
673     default:
674       xbt_die("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
675         form, klass, name==NULL ? "?" : name);
676     }
677   }
678
679   if(namespace && variable->global) {
680     char* old_name = variable->name;
681     variable->name = bprintf("%s::%s", namespace, old_name);
682     free(old_name);
683   }
684
685   // The current code needs a variable name,
686   // generate a fake one:
687   if(!variable->name) {
688     variable->name = bprintf("@anonymous#%i", mc_anonymous_variable_index++);
689   }
690
691   return variable;
692 }
693
694 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
695   dw_variable_t variable = MC_die_to_variable(info, die, unit, frame, namespace);
696   if(variable==NULL)
697       return;
698   MC_dwarf_register_variable(info, frame, variable);
699 }
700
701 static void MC_dwarf_handle_subprogram_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t parent_frame, const char* namespace) {
702
703   // (Template) Subprogram declaration:
704   if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
705     return;
706
707   // Abstract inline instance (DW_AT_inline != DW_INL_not_inlined):
708   if (dwarf_func_inline(die))
709     return;
710
711   // This is probably not a concrete instance:
712   // DWARF2/3 and DWARF4 do not agree on the meaning of DW_INL_not_inlined.
713   // For DWARF2/3, the subprogram is abstract.
714   // For DARF4, the subprogram is not abstract.
715   if(!dwarf_hasattr_integrate(die, DW_AT_low_pc))
716     return;
717
718   dw_frame_t frame = xbt_new0(s_dw_frame_t, 1);
719
720   frame->start = dwarf_dieoffset(die);
721
722   const char* name = MC_dwarf_attr_string(die, DW_AT_name);
723   frame->name = namespace ? bprintf("%s::%s", namespace, name) : xbt_strdup(name);
724
725   // This is the base address for DWARF addresses.
726   // Relocated addresses are offset from this base address.
727   // See DWARF4 spec 7.5
728   void* base = info->flags & MC_OBJECT_INFO_EXECUTABLE ? 0 : MC_object_base_address(info);
729
730   // Variables are filled in the (recursive) call of MC_dwarf_handle_children:
731   frame->variables = xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
732   frame->high_pc = ((char*) base) + MC_dwarf_attr_addr(die, DW_AT_high_pc);
733   frame->low_pc = ((char*) base) + MC_dwarf_attr_addr(die, DW_AT_low_pc);
734
735   if(frame->high_pc==0 || frame->low_pc==0)
736     xbt_die("Could not resolve highpc/lowpc");
737
738   Dwarf_Attribute attr_frame_base;
739   if (!dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
740     xbt_die("Coult not find DW_AT_frame_base for subprogram %s 0x%lx", frame->name, frame->start);
741   mc_dwarf_location_list_init(&frame->frame_base, info, die, &attr_frame_base);
742
743   frame->end = -1; // This one is now useless:
744
745   // Register it:
746   xbt_dynar_push(info->subprograms, &frame);
747
748   // Handle children:
749   MC_dwarf_handle_children(info, die, unit, frame, namespace);
750 }
751
752 static void mc_dwarf_handle_namespace_die(
753     mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
754   const char* name = MC_dwarf_attr_string(die, DW_AT_name);
755   if(frame)
756     xbt_die("Unexpected namespace in a subprogram");
757   char* new_namespace = namespace == NULL ? xbt_strdup(name)
758     : bprintf("%s::%s", namespace, name);
759   MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
760 }
761
762 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
763   Dwarf_Die child;
764   int res;
765   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
766     MC_dwarf_handle_die(info, &child, unit, frame, namespace);
767   }
768 }
769
770 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
771   int tag = dwarf_tag(die);
772   mc_tag_class klass = MC_dwarf_tag_classify(tag);
773   switch (klass) {
774
775     // Type:
776     case mc_tag_type:
777       MC_dwarf_handle_type_die(info, die, unit, frame, namespace);
778       break;
779
780     // Program:
781     case mc_tag_subprogram:
782       MC_dwarf_handle_subprogram_die(info, die, unit, frame, namespace);
783       return;
784
785     // Variable:
786     case mc_tag_variable:
787       MC_dwarf_handle_variable_die(info, die, unit, frame, namespace);
788       break;
789
790     // Scope:
791     case mc_tag_scope:
792       // TODO
793       break;
794
795     case mc_tag_namespace:
796       mc_dwarf_handle_namespace_die(info, die, unit, frame, namespace);
797       break;
798
799     default:
800       break;
801
802   }
803 }
804
805 void MC_dwarf_get_variables(mc_object_info_t info) {
806   int fd = open(info->file_name, O_RDONLY);
807   if (fd<0) {
808     xbt_die("Could not open file %s", info->file_name);
809   }
810   Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
811   if (dwarf==NULL) {
812     xbt_die("Your program must be compiled with -g");
813   }
814
815   Dwarf_Off offset = 0;
816   Dwarf_Off next_offset = 0;
817   size_t length;
818   while (dwarf_nextcu (dwarf, offset, &next_offset, &length, NULL, NULL, NULL) == 0) {
819     Dwarf_Die unit_die;
820
821     if(dwarf_offdie(dwarf, offset+length, &unit_die)!=NULL) {
822       Dwarf_Die child;
823       int res;
824       for (res=dwarf_child(&unit_die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
825         MC_dwarf_handle_die(info, &child, &unit_die, NULL, NULL);
826       }
827     }
828     offset = next_offset;
829   }
830
831   dwarf_end(dwarf);
832   close(fd);
833 }