1 //===-- DWARFDebugInfoEntry.cpp ---------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "DWARFDebugInfoEntry.h"
11 
12 #include <assert.h>
13 
14 #include <algorithm>
15 
16 #include "lldb/Core/Module.h"
17 #include "lldb/Core/Stream.h"
18 #include "lldb/Expression/DWARFExpression.h"
19 #include "lldb/Symbol/ObjectFile.h"
20 
21 #include "DWARFCompileUnit.h"
22 #include "SymbolFileDWARF.h"
23 #include "DWARFDebugAbbrev.h"
24 #include "DWARFDebugAranges.h"
25 #include "DWARFDebugInfo.h"
26 #include "DWARFDeclContext.h"
27 #include "DWARFDIECollection.h"
28 #include "DWARFFormValue.h"
29 #include "DWARFLocationDescription.h"
30 #include "DWARFLocationList.h"
31 #include "DWARFDebugRanges.h"
32 
33 using namespace lldb_private;
34 using namespace std;
35 extern int g_verbose;
36 
37 
38 
39 DWARFDebugInfoEntry::Attributes::Attributes() :
40     m_infos()
41 {
42 }
43 
44 DWARFDebugInfoEntry::Attributes::~Attributes()
45 {
46 }
47 
48 
49 uint32_t
50 DWARFDebugInfoEntry::Attributes::FindAttributeIndex(dw_attr_t attr) const
51 {
52     collection::const_iterator end = m_infos.end();
53     collection::const_iterator beg = m_infos.begin();
54     collection::const_iterator pos;
55     for (pos = beg; pos != end; ++pos)
56     {
57         if (pos->attr == attr)
58             return std::distance(beg, pos);
59     }
60     return UINT32_MAX;
61 }
62 
63 void
64 DWARFDebugInfoEntry::Attributes::Append(const DWARFCompileUnit *cu, dw_offset_t attr_die_offset, dw_attr_t attr, dw_form_t form)
65 {
66     Info info = { cu, attr_die_offset, attr, form };
67     m_infos.push_back(info);
68 }
69 
70 bool
71 DWARFDebugInfoEntry::Attributes::ContainsAttribute(dw_attr_t attr) const
72 {
73     return FindAttributeIndex(attr) != UINT32_MAX;
74 }
75 
76 bool
77 DWARFDebugInfoEntry::Attributes::RemoveAttribute(dw_attr_t attr)
78 {
79     uint32_t attr_index = FindAttributeIndex(attr);
80     if (attr_index != UINT32_MAX)
81     {
82         m_infos.erase(m_infos.begin() + attr_index);
83         return true;
84     }
85     return false;
86 }
87 
88 bool
89 DWARFDebugInfoEntry::Attributes::ExtractFormValueAtIndex (SymbolFileDWARF* dwarf2Data, uint32_t i, DWARFFormValue &form_value) const
90 {
91     form_value.SetForm(FormAtIndex(i));
92     dw_offset_t offset = DIEOffsetAtIndex(i);
93     return form_value.ExtractValue(dwarf2Data->get_debug_info_data(), &offset, CompileUnitAtIndex(i));
94 }
95 
96 uint64_t
97 DWARFDebugInfoEntry::Attributes::FormValueAsUnsigned (SymbolFileDWARF* dwarf2Data, dw_attr_t attr, uint64_t fail_value) const
98 {
99     const uint32_t attr_idx = FindAttributeIndex (attr);
100     if (attr_idx != UINT32_MAX)
101         return FormValueAsUnsignedAtIndex (dwarf2Data, attr_idx, fail_value);
102     return fail_value;
103 }
104 
105 uint64_t
106 DWARFDebugInfoEntry::Attributes::FormValueAsUnsignedAtIndex(SymbolFileDWARF* dwarf2Data, uint32_t i, uint64_t fail_value) const
107 {
108     DWARFFormValue form_value;
109     if (ExtractFormValueAtIndex(dwarf2Data, i, form_value))
110         return form_value.Reference(CompileUnitAtIndex(i));
111     return fail_value;
112 }
113 
114 
115 
116 bool
117 DWARFDebugInfoEntry::FastExtract
118 (
119     const DataExtractor& debug_info_data,
120     const DWARFCompileUnit* cu,
121     const uint8_t *fixed_form_sizes,
122     uint32_t* offset_ptr
123 )
124 {
125     m_offset = *offset_ptr;
126     m_parent_idx = 0;
127     m_sibling_idx = 0;
128     m_empty_children = false;
129     const uint64_t abbr_idx = debug_info_data.GetULEB128 (offset_ptr);
130     assert (abbr_idx < (1 << DIE_ABBR_IDX_BITSIZE));
131     m_abbr_idx = abbr_idx;
132 
133     //assert (fixed_form_sizes);  // For best performance this should be specified!
134 
135     if (m_abbr_idx)
136     {
137         uint32_t offset = *offset_ptr;
138 
139         const DWARFAbbreviationDeclaration *abbrevDecl = cu->GetAbbreviations()->GetAbbreviationDeclaration(m_abbr_idx);
140 
141         if (abbrevDecl == NULL)
142         {
143             cu->GetSymbolFileDWARF()->GetObjectFile()->GetModule()->ReportError ("{0x%8.8x}: invalid abbreviation code %u, please file a bug and attach the file at the start of this error message",
144                                                                                  m_offset,
145                                                                                  (unsigned)abbr_idx);
146             // WE can't parse anymore if the DWARF is borked...
147             *offset_ptr = UINT32_MAX;
148             return false;
149         }
150         m_tag = abbrevDecl->Tag();
151         m_has_children = abbrevDecl->HasChildren();
152         // Skip all data in the .debug_info for the attributes
153         const uint32_t numAttributes = abbrevDecl->NumAttributes();
154         register uint32_t i;
155         register dw_form_t form;
156         for (i=0; i<numAttributes; ++i)
157         {
158             form = abbrevDecl->GetFormByIndexUnchecked(i);
159 
160             const uint8_t fixed_skip_size = fixed_form_sizes [form];
161             if (fixed_skip_size)
162                 offset += fixed_skip_size;
163             else
164             {
165                 bool form_is_indirect = false;
166                 do
167                 {
168                     form_is_indirect = false;
169                     register uint32_t form_size = 0;
170                     switch (form)
171                     {
172                     // Blocks if inlined data that have a length field and the data bytes
173                     // inlined in the .debug_info
174                     case DW_FORM_exprloc     :
175                     case DW_FORM_block       : form_size = debug_info_data.GetULEB128 (&offset);      break;
176                     case DW_FORM_block1      : form_size = debug_info_data.GetU8_unchecked (&offset); break;
177                     case DW_FORM_block2      : form_size = debug_info_data.GetU16_unchecked (&offset);break;
178                     case DW_FORM_block4      : form_size = debug_info_data.GetU32_unchecked (&offset);break;
179 
180                     // Inlined NULL terminated C-strings
181                     case DW_FORM_string      :
182                         debug_info_data.GetCStr (&offset);
183                         break;
184 
185                     // Compile unit address sized values
186                     case DW_FORM_addr        :
187                     case DW_FORM_ref_addr    :
188                         form_size = cu->GetAddressByteSize();
189                         break;
190 
191                     // 0 sized form
192                     case DW_FORM_flag_present:
193                         form_size = 0;
194                         break;
195 
196                     // 1 byte values
197                     case DW_FORM_data1       :
198                     case DW_FORM_flag        :
199                     case DW_FORM_ref1        :
200                         form_size = 1;
201                         break;
202 
203                     // 2 byte values
204                     case DW_FORM_data2       :
205                     case DW_FORM_ref2        :
206                         form_size = 2;
207                         break;
208 
209                     // 4 byte values
210                     case DW_FORM_strp        :
211                     case DW_FORM_data4       :
212                     case DW_FORM_ref4        :
213                         form_size = 4;
214                         break;
215 
216                     // 8 byte values
217                     case DW_FORM_data8       :
218                     case DW_FORM_ref8        :
219                     case DW_FORM_ref_sig8    :
220                         form_size = 8;
221                         break;
222 
223                     // signed or unsigned LEB 128 values
224                     case DW_FORM_sdata       :
225                     case DW_FORM_udata       :
226                     case DW_FORM_ref_udata   :
227                         debug_info_data.Skip_LEB128 (&offset);
228                         break;
229 
230                     case DW_FORM_indirect    :
231                         form_is_indirect = true;
232                         form = debug_info_data.GetULEB128 (&offset);
233                         break;
234 
235                     case DW_FORM_sec_offset  :
236                         if (cu->GetAddressByteSize () == 4)
237                             debug_info_data.GetU32 (offset_ptr);
238                         else
239                             debug_info_data.GetU64 (offset_ptr);
240                         break;
241 
242                     default:
243                         *offset_ptr = m_offset;
244                         return false;
245                     }
246                     offset += form_size;
247 
248                 } while (form_is_indirect);
249             }
250         }
251         *offset_ptr = offset;
252         return true;
253     }
254     else
255     {
256         m_tag = 0;
257         m_has_children = false;
258         return true;    // NULL debug tag entry
259     }
260 
261     return false;
262 }
263 
264 //----------------------------------------------------------------------
265 // Extract
266 //
267 // Extract a debug info entry for a given compile unit from the
268 // .debug_info and .debug_abbrev data within the SymbolFileDWARF class
269 // starting at the given offset
270 //----------------------------------------------------------------------
271 bool
272 DWARFDebugInfoEntry::Extract
273 (
274     SymbolFileDWARF* dwarf2Data,
275     const DWARFCompileUnit* cu,
276     uint32_t* offset_ptr
277 )
278 {
279     const DataExtractor& debug_info_data = dwarf2Data->get_debug_info_data();
280 //    const DataExtractor& debug_str_data = dwarf2Data->get_debug_str_data();
281     const uint32_t cu_end_offset = cu->GetNextCompileUnitOffset();
282     const uint8_t cu_addr_size = cu->GetAddressByteSize();
283     uint32_t offset = *offset_ptr;
284 //  if (offset >= cu_end_offset)
285 //      Log::Error("DIE at offset 0x%8.8x is beyond the end of the current compile unit (0x%8.8x)", m_offset, cu_end_offset);
286     if ((offset < cu_end_offset) && debug_info_data.ValidOffset(offset))
287     {
288         m_offset = offset;
289 
290         const uint64_t abbr_idx = debug_info_data.GetULEB128(&offset);
291         assert (abbr_idx < (1 << DIE_ABBR_IDX_BITSIZE));
292         m_abbr_idx = abbr_idx;
293         if (abbr_idx)
294         {
295             const DWARFAbbreviationDeclaration *abbrevDecl = cu->GetAbbreviations()->GetAbbreviationDeclaration(abbr_idx);
296 
297             if (abbrevDecl)
298             {
299                 m_tag = abbrevDecl->Tag();
300                 m_has_children = abbrevDecl->HasChildren();
301 
302                 bool isCompileUnitTag = m_tag == DW_TAG_compile_unit;
303                 if (cu && isCompileUnitTag)
304                     ((DWARFCompileUnit*)cu)->SetBaseAddress(0);
305 
306                 // Skip all data in the .debug_info for the attributes
307                 const uint32_t numAttributes = abbrevDecl->NumAttributes();
308                 uint32_t i;
309                 dw_attr_t attr;
310                 dw_form_t form;
311                 for (i=0; i<numAttributes; ++i)
312                 {
313                     abbrevDecl->GetAttrAndFormByIndexUnchecked(i, attr, form);
314 
315                     if (isCompileUnitTag && ((attr == DW_AT_entry_pc) || (attr == DW_AT_low_pc)))
316                     {
317                         DWARFFormValue form_value(form);
318                         if (form_value.ExtractValue(debug_info_data, &offset, cu))
319                         {
320                             if (attr == DW_AT_low_pc || attr == DW_AT_entry_pc)
321                                 ((DWARFCompileUnit*)cu)->SetBaseAddress(form_value.Unsigned());
322                         }
323                     }
324                     else
325                     {
326                         bool form_is_indirect = false;
327                         do
328                         {
329                             form_is_indirect = false;
330                             register uint32_t form_size = 0;
331                             switch (form)
332                             {
333                             // Blocks if inlined data that have a length field and the data bytes
334                             // inlined in the .debug_info
335                             case DW_FORM_exprloc     :
336                             case DW_FORM_block       : form_size = debug_info_data.GetULEB128(&offset);  break;
337                             case DW_FORM_block1      : form_size = debug_info_data.GetU8(&offset);       break;
338                             case DW_FORM_block2      : form_size = debug_info_data.GetU16(&offset);      break;
339                             case DW_FORM_block4      : form_size = debug_info_data.GetU32(&offset);      break;
340 
341                             // Inlined NULL terminated C-strings
342                             case DW_FORM_string      : debug_info_data.GetCStr(&offset);                 break;
343 
344                             // Compile unit address sized values
345                             case DW_FORM_addr        :
346                             case DW_FORM_ref_addr    :
347                                 form_size = cu_addr_size;
348                                 break;
349 
350                             // 0 sized form
351                             case DW_FORM_flag_present:
352                                 form_size = 0;
353                                 break;
354 
355                             // 1 byte values
356                             case DW_FORM_data1       :
357                             case DW_FORM_flag        :
358                             case DW_FORM_ref1        :
359                                 form_size = 1;
360                                 break;
361 
362                             // 2 byte values
363                             case DW_FORM_data2       :
364                             case DW_FORM_ref2        :
365                                 form_size = 2;
366                                 break;
367 
368                             // 4 byte values
369                             case DW_FORM_strp        :
370                                 form_size = 4;
371                                 break;
372 
373                             case DW_FORM_data4       :
374                             case DW_FORM_ref4        :
375                                 form_size = 4;
376                                 break;
377 
378                             // 8 byte values
379                             case DW_FORM_data8       :
380                             case DW_FORM_ref8        :
381                             case DW_FORM_ref_sig8    :
382                                 form_size = 8;
383                                 break;
384 
385                             // signed or unsigned LEB 128 values
386                             case DW_FORM_sdata       :
387                             case DW_FORM_udata       :
388                             case DW_FORM_ref_udata   :
389                                 debug_info_data.Skip_LEB128(&offset);
390                                 break;
391 
392                             case DW_FORM_indirect    :
393                                 form = debug_info_data.GetULEB128(&offset);
394                                 form_is_indirect = true;
395                                 break;
396 
397                             case DW_FORM_sec_offset  :
398                                 if (cu->GetAddressByteSize () == 4)
399                                     debug_info_data.GetU32 (offset_ptr);
400                                 else
401                                     debug_info_data.GetU64 (offset_ptr);
402                                 break;
403 
404                             default:
405                                 *offset_ptr = offset;
406                                 return false;
407                             }
408 
409                             offset += form_size;
410                         } while (form_is_indirect);
411                     }
412                 }
413                 *offset_ptr = offset;
414                 return true;
415             }
416         }
417         else
418         {
419             m_tag = 0;
420             m_has_children = false;
421             *offset_ptr = offset;
422             return true;    // NULL debug tag entry
423         }
424     }
425 
426     return false;
427 }
428 
429 //----------------------------------------------------------------------
430 // DumpAncestry
431 //
432 // Dumps all of a debug information entries parents up until oldest and
433 // all of it's attributes to the specified stream.
434 //----------------------------------------------------------------------
435 void
436 DWARFDebugInfoEntry::DumpAncestry
437 (
438     SymbolFileDWARF* dwarf2Data,
439     const DWARFCompileUnit* cu,
440     const DWARFDebugInfoEntry* oldest,
441     Stream &s,
442     uint32_t recurse_depth
443 ) const
444 {
445     const DWARFDebugInfoEntry* parent = GetParent();
446     if (parent && parent != oldest)
447         parent->DumpAncestry(dwarf2Data, cu, oldest, s, 0);
448     Dump(dwarf2Data, cu, s, recurse_depth);
449 }
450 
451 //----------------------------------------------------------------------
452 // Compare two DIE by comparing all their attributes values, and
453 // following all DW_FORM_ref attributes and comparing their contents as
454 // well (except for DW_AT_sibling attributes.
455 //
456 //  DWARFDebugInfoEntry::CompareState compare_state;
457 //  int result = DWARFDebugInfoEntry::Compare(this, 0x00017ccb, 0x0001eb2b, compare_state, false, true);
458 //----------------------------------------------------------------------
459 //int
460 //DWARFDebugInfoEntry::Compare
461 //(
462 //    SymbolFileDWARF* dwarf2Data,
463 //    dw_offset_t a_die_offset,
464 //    dw_offset_t b_die_offset,
465 //    CompareState &compare_state,
466 //    bool compare_siblings,
467 //    bool compare_children
468 //)
469 //{
470 //    if (a_die_offset == b_die_offset)
471 //        return 0;
472 //
473 //    DWARFCompileUnitSP a_cu_sp;
474 //    DWARFCompileUnitSP b_cu_sp;
475 //    const DWARFDebugInfoEntry* a_die = dwarf2Data->DebugInfo()->GetDIEPtr(a_die_offset, &a_cu_sp);
476 //    const DWARFDebugInfoEntry* b_die = dwarf2Data->DebugInfo()->GetDIEPtr(b_die_offset, &b_cu_sp);
477 //
478 //    return Compare(dwarf2Data, a_cu_sp.get(), a_die, b_cu_sp.get(), b_die, compare_state, compare_siblings, compare_children);
479 //}
480 //
481 //int
482 //DWARFDebugInfoEntry::Compare
483 //(
484 //    SymbolFileDWARF* dwarf2Data,
485 //    DWARFCompileUnit* a_cu, const DWARFDebugInfoEntry* a_die,
486 //    DWARFCompileUnit* b_cu, const DWARFDebugInfoEntry* b_die,
487 //    CompareState &compare_state,
488 //    bool compare_siblings,
489 //    bool compare_children
490 //)
491 //{
492 //    if (a_die == b_die)
493 //        return 0;
494 //
495 //    if (!compare_state.AddTypePair(a_die->GetOffset(), b_die->GetOffset()))
496 //    {
497 //        // We are already comparing both of these types, so let
498 //        // compares complete for the real result
499 //        return 0;
500 //    }
501 //
502 //    //printf("DWARFDebugInfoEntry::Compare(0x%8.8x, 0x%8.8x)\n", a_die->GetOffset(), b_die->GetOffset());
503 //
504 //    // Do we have two valid DIEs?
505 //    if (a_die && b_die)
506 //    {
507 //        // Both DIE are valid
508 //        int result = 0;
509 //
510 //        const dw_tag_t a_tag = a_die->Tag();
511 //        const dw_tag_t b_tag = b_die->Tag();
512 //        if (a_tag == 0 && b_tag == 0)
513 //            return 0;
514 //
515 //        //printf("    comparing tags: %s and %s\n", DW_TAG_value_to_name(a_tag), DW_TAG_value_to_name(b_tag));
516 //
517 //        if (a_tag < b_tag)
518 //            return -1;
519 //        else if (a_tag > b_tag)
520 //            return 1;
521 //
522 //        DWARFDebugInfoEntry::Attributes a_attrs;
523 //        DWARFDebugInfoEntry::Attributes b_attrs;
524 //        size_t a_attr_count = a_die->GetAttributes(dwarf2Data, a_cu, a_attrs);
525 //        size_t b_attr_count = b_die->GetAttributes(dwarf2Data, b_cu, b_attrs);
526 //        if (a_attr_count != b_attr_count)
527 //        {
528 //            a_attrs.RemoveAttribute(DW_AT_sibling);
529 //            b_attrs.RemoveAttribute(DW_AT_sibling);
530 //        }
531 //
532 //        a_attr_count = a_attrs.Size();
533 //        b_attr_count = b_attrs.Size();
534 //
535 //        DWARFFormValue a_form_value;
536 //        DWARFFormValue b_form_value;
537 //
538 //        if (a_attr_count != b_attr_count)
539 //        {
540 //            uint32_t is_decl_index = a_attrs.FindAttributeIndex(DW_AT_declaration);
541 //            uint32_t a_name_index = UINT32_MAX;
542 //            uint32_t b_name_index = UINT32_MAX;
543 //            if (is_decl_index != UINT32_MAX)
544 //            {
545 //                if (a_attr_count == 2)
546 //                {
547 //                    a_name_index = a_attrs.FindAttributeIndex(DW_AT_name);
548 //                    b_name_index = b_attrs.FindAttributeIndex(DW_AT_name);
549 //                }
550 //            }
551 //            else
552 //            {
553 //                is_decl_index = b_attrs.FindAttributeIndex(DW_AT_declaration);
554 //                if (is_decl_index != UINT32_MAX && a_attr_count == 2)
555 //                {
556 //                    a_name_index = a_attrs.FindAttributeIndex(DW_AT_name);
557 //                    b_name_index = b_attrs.FindAttributeIndex(DW_AT_name);
558 //                }
559 //            }
560 //            if (a_name_index != UINT32_MAX && b_name_index != UINT32_MAX)
561 //            {
562 //                if (a_attrs.ExtractFormValueAtIndex(dwarf2Data, a_name_index, a_form_value) &&
563 //                    b_attrs.ExtractFormValueAtIndex(dwarf2Data, b_name_index, b_form_value))
564 //                {
565 //                    result = DWARFFormValue::Compare (a_form_value, b_form_value, a_cu, b_cu, &dwarf2Data->get_debug_str_data());
566 //                    if (result == 0)
567 //                    {
568 //                        a_attr_count = b_attr_count = 0;
569 //                        compare_children = false;
570 //                    }
571 //                }
572 //            }
573 //        }
574 //
575 //        if (a_attr_count < b_attr_count)
576 //            return -1;
577 //        if (a_attr_count > b_attr_count)
578 //            return 1;
579 //
580 //
581 //        // The number of attributes are the same...
582 //        if (a_attr_count > 0)
583 //        {
584 //            const DataExtractor* debug_str_data_ptr = &dwarf2Data->get_debug_str_data();
585 //
586 //            uint32_t i;
587 //            for (i=0; i<a_attr_count; ++i)
588 //            {
589 //                const dw_attr_t a_attr = a_attrs.AttributeAtIndex(i);
590 //                const dw_attr_t b_attr = b_attrs.AttributeAtIndex(i);
591 //                //printf("    comparing attributes\n\t\t0x%8.8x: %s %s\t\t0x%8.8x: %s %s\n",
592 //                //                a_attrs.DIEOffsetAtIndex(i), DW_FORM_value_to_name(a_attrs.FormAtIndex(i)), DW_AT_value_to_name(a_attr),
593 //                //                b_attrs.DIEOffsetAtIndex(i), DW_FORM_value_to_name(b_attrs.FormAtIndex(i)), DW_AT_value_to_name(b_attr));
594 //
595 //                if (a_attr < b_attr)
596 //                    return -1;
597 //                else if (a_attr > b_attr)
598 //                    return 1;
599 //
600 //                switch (a_attr)
601 //                {
602 //                // Since we call a form of GetAttributes which inlines the
603 //                // attributes from DW_AT_abstract_origin and DW_AT_specification
604 //                // we don't care if their values mismatch...
605 //                case DW_AT_abstract_origin:
606 //                case DW_AT_specification:
607 //                case DW_AT_sibling:
608 //                case DW_AT_containing_type:
609 //                    //printf("        action = IGNORE\n");
610 //                    result = 0;
611 //                    break;  // ignore
612 //
613 //                default:
614 //                    if (a_attrs.ExtractFormValueAtIndex(dwarf2Data, i, a_form_value) &&
615 //                        b_attrs.ExtractFormValueAtIndex(dwarf2Data, i, b_form_value))
616 //                        result = DWARFFormValue::Compare (a_form_value, b_form_value, a_cu, b_cu, debug_str_data_ptr);
617 //                    break;
618 //                }
619 //
620 //                //printf("\t  result = %i\n", result);
621 //
622 //                if (result != 0)
623 //                {
624 //                    // Attributes weren't equal, lets see if we care?
625 //                    switch (a_attr)
626 //                    {
627 //                    case DW_AT_decl_file:
628 //                        // TODO: add the ability to compare files in two different compile units
629 //                        if (a_cu == b_cu)
630 //                        {
631 //                            //printf("        action = RETURN RESULT\n");
632 //                            return result;  // Only return the compare results when the compile units are the same and the decl_file attributes can be compared
633 //                        }
634 //                        else
635 //                        {
636 //                            result = 0;
637 //                            //printf("        action = IGNORE\n");
638 //                        }
639 //                        break;
640 //
641 //                    default:
642 //                        switch (a_attrs.FormAtIndex(i))
643 //                        {
644 //                        case DW_FORM_ref1:
645 //                        case DW_FORM_ref2:
646 //                        case DW_FORM_ref4:
647 //                        case DW_FORM_ref8:
648 //                        case DW_FORM_ref_udata:
649 //                        case DW_FORM_ref_addr:
650 //                            //printf("    action = COMPARE DIEs 0x%8.8x 0x%8.8x\n", (dw_offset_t)a_form_value.Reference(a_cu), (dw_offset_t)b_form_value.Reference(b_cu));
651 //                            // These attribute values refer to other DIEs, so lets compare those instead of their DIE offsets...
652 //                            result = Compare(dwarf2Data, a_form_value.Reference(a_cu), b_form_value.Reference(b_cu), compare_state, false, true);
653 //                            if (result != 0)
654 //                                return result;
655 //                            break;
656 //
657 //                        default:
658 //                            // We do care that they were different, return this result...
659 //                            //printf("        action = RETURN RESULT\n");
660 //                            return result;
661 //                        }
662 //                    }
663 //                }
664 //            }
665 //        }
666 //        //printf("    SUCCESS\n\t\t0x%8.8x: %s\n\t\t0x%8.8x: %s\n", a_die->GetOffset(), DW_TAG_value_to_name(a_tag), b_die->GetOffset(), DW_TAG_value_to_name(b_tag));
667 //
668 //        if (compare_children)
669 //        {
670 //            bool a_has_children = a_die->HasChildren();
671 //            bool b_has_children = b_die->HasChildren();
672 //            if (a_has_children == b_has_children)
673 //            {
674 //                // Both either have kids or don't
675 //                if (a_has_children)
676 //                    result = Compare(   dwarf2Data,
677 //                                        a_cu, a_die->GetFirstChild(),
678 //                                        b_cu, b_die->GetFirstChild(),
679 //                                        compare_state, true, compare_children);
680 //                else
681 //                    result = 0;
682 //            }
683 //            else if (!a_has_children)
684 //                result = -1;    // A doesn't have kids, but B does
685 //            else
686 //                result = 1; // A has kids, but B doesn't
687 //        }
688 //
689 //        if (compare_siblings)
690 //        {
691 //            result = Compare(   dwarf2Data,
692 //                                a_cu, a_die->GetSibling(),
693 //                                b_cu, b_die->GetSibling(),
694 //                                compare_state, true, compare_children);
695 //        }
696 //
697 //        return result;
698 //    }
699 //
700 //    if (a_die == NULL)
701 //        return -1;  // a_die is NULL, yet b_die is non-NULL
702 //    else
703 //        return 1;   // a_die is non-NULL, yet b_die is NULL
704 //
705 //}
706 //
707 //
708 //int
709 //DWARFDebugInfoEntry::Compare
710 //(
711 //  SymbolFileDWARF* dwarf2Data,
712 //  const DWARFCompileUnit* cu_a,
713 //  const DWARFDebugInfoEntry* die_a,
714 //  const DWARFCompileUnit* cu_a,
715 //  const DWARFDebugInfoEntry* die_b,
716 //  CompareState &compare_state
717 //)
718 //{
719 //}
720 
721 //----------------------------------------------------------------------
722 // GetDIENamesAndRanges
723 //
724 // Gets the valid address ranges for a given DIE by looking for a
725 // DW_AT_low_pc/DW_AT_high_pc pair, DW_AT_entry_pc, or DW_AT_ranges
726 // attributes.
727 //----------------------------------------------------------------------
728 bool
729 DWARFDebugInfoEntry::GetDIENamesAndRanges
730 (
731     SymbolFileDWARF* dwarf2Data,
732     const DWARFCompileUnit* cu,
733     const char * &name,
734     const char * &mangled,
735     DWARFDebugRanges::RangeList& ranges,
736     int& decl_file,
737     int& decl_line,
738     int& decl_column,
739     int& call_file,
740     int& call_line,
741     int& call_column,
742     DWARFExpression *frame_base
743 ) const
744 {
745     if (dwarf2Data == NULL)
746         return false;
747 
748     dw_addr_t lo_pc = DW_INVALID_ADDRESS;
749     dw_addr_t hi_pc = DW_INVALID_ADDRESS;
750     std::vector<dw_offset_t> die_offsets;
751     bool set_frame_base_loclist_addr = false;
752 
753     dw_offset_t offset;
754     const DWARFAbbreviationDeclaration* abbrevDecl = GetAbbreviationDeclarationPtr(dwarf2Data, cu, offset);
755 
756     if (abbrevDecl)
757     {
758         const DataExtractor& debug_info_data = dwarf2Data->get_debug_info_data();
759 
760         if (!debug_info_data.ValidOffset(offset))
761             return false;
762 
763         const uint32_t numAttributes = abbrevDecl->NumAttributes();
764         uint32_t i;
765         dw_attr_t attr;
766         dw_form_t form;
767         for (i=0; i<numAttributes; ++i)
768         {
769             abbrevDecl->GetAttrAndFormByIndexUnchecked(i, attr, form);
770             DWARFFormValue form_value(form);
771             if (form_value.ExtractValue(debug_info_data, &offset, cu))
772             {
773                 switch (attr)
774                 {
775                 case DW_AT_low_pc:
776                 case DW_AT_entry_pc:
777                     lo_pc = form_value.Unsigned();
778                     break;
779 
780                 case DW_AT_high_pc:
781                     hi_pc = form_value.Unsigned();
782                     break;
783 
784                 case DW_AT_ranges:
785                     {
786                         const DWARFDebugRanges* debug_ranges = dwarf2Data->DebugRanges();
787                         debug_ranges->FindRanges(form_value.Unsigned(), ranges);
788                         // All DW_AT_ranges are relative to the base address of the
789                         // compile unit. We add the compile unit base address to make
790                         // sure all the addresses are properly fixed up.
791                         ranges.Slide(cu->GetBaseAddress());
792                     }
793                     break;
794 
795                 case DW_AT_name:
796                     if (name == NULL)
797                         name = form_value.AsCString(&dwarf2Data->get_debug_str_data());
798                     break;
799 
800                 case DW_AT_MIPS_linkage_name:
801                     if (mangled == NULL)
802                         mangled = form_value.AsCString(&dwarf2Data->get_debug_str_data());
803                     break;
804 
805                 case DW_AT_abstract_origin:
806                     die_offsets.push_back(form_value.Reference(cu));
807                     break;
808 
809                 case DW_AT_specification:
810                     die_offsets.push_back(form_value.Reference(cu));
811                     break;
812 
813                 case DW_AT_decl_file:
814                     if (decl_file == 0)
815                         decl_file = form_value.Unsigned();
816                     break;
817 
818                 case DW_AT_decl_line:
819                     if (decl_line == 0)
820                         decl_line = form_value.Unsigned();
821                     break;
822 
823                 case DW_AT_decl_column:
824                     if (decl_column == 0)
825                         decl_column = form_value.Unsigned();
826                     break;
827 
828                 case DW_AT_call_file:
829                     if (call_file == 0)
830                         call_file = form_value.Unsigned();
831                     break;
832 
833                 case DW_AT_call_line:
834                     if (call_line == 0)
835                         call_line = form_value.Unsigned();
836                     break;
837 
838                 case DW_AT_call_column:
839                     if (call_column == 0)
840                         call_column = form_value.Unsigned();
841                     break;
842 
843                 case DW_AT_frame_base:
844                     if (frame_base)
845                     {
846                         if (form_value.BlockData())
847                         {
848                             uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
849                             uint32_t block_length = form_value.Unsigned();
850                             frame_base->SetOpcodeData(debug_info_data, block_offset, block_length);
851                         }
852                         else
853                         {
854                             const DataExtractor &debug_loc_data = dwarf2Data->get_debug_loc_data();
855                             const dw_offset_t debug_loc_offset = form_value.Unsigned();
856 
857                             size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset);
858                             if (loc_list_length > 0)
859                             {
860                                 frame_base->SetOpcodeData(debug_loc_data, debug_loc_offset, loc_list_length);
861                                 if (lo_pc != DW_INVALID_ADDRESS)
862                                 {
863                                     assert (lo_pc >= cu->GetBaseAddress());
864                                     frame_base->SetLocationListSlide(lo_pc - cu->GetBaseAddress());
865                                 }
866                                 else
867                                 {
868                                     set_frame_base_loclist_addr = true;
869                                 }
870                             }
871                         }
872                     }
873                     break;
874 
875                 default:
876                     break;
877                 }
878             }
879         }
880     }
881 
882     if (ranges.IsEmpty())
883     {
884         if (lo_pc != DW_INVALID_ADDRESS)
885         {
886             if (hi_pc != DW_INVALID_ADDRESS && hi_pc > lo_pc)
887                 ranges.Append(DWARFDebugRanges::Range (lo_pc, hi_pc - lo_pc));
888             else
889                 ranges.Append(DWARFDebugRanges::Range (lo_pc, 0));
890         }
891     }
892 
893     if (set_frame_base_loclist_addr)
894     {
895         dw_addr_t lowest_range_pc = ranges.GetMinRangeBase(0);
896         assert (lowest_range_pc >= cu->GetBaseAddress());
897         frame_base->SetLocationListSlide (lowest_range_pc - cu->GetBaseAddress());
898     }
899 
900     if (ranges.IsEmpty() || name == NULL || mangled == NULL)
901     {
902         std::vector<dw_offset_t>::const_iterator pos;
903         std::vector<dw_offset_t>::const_iterator end = die_offsets.end();
904         for (pos = die_offsets.begin(); pos != end; ++pos)
905         {
906             DWARFCompileUnitSP cu_sp_ptr;
907             const DWARFDebugInfoEntry* die = NULL;
908             dw_offset_t die_offset = *pos;
909             if (die_offset != DW_INVALID_OFFSET)
910             {
911                 die = dwarf2Data->DebugInfo()->GetDIEPtr(die_offset, &cu_sp_ptr);
912                 if (die)
913                     die->GetDIENamesAndRanges(dwarf2Data, cu_sp_ptr.get(), name, mangled, ranges, decl_file, decl_line, decl_column, call_file, call_line, call_column);
914             }
915         }
916     }
917     return !ranges.IsEmpty();
918 }
919 
920 //----------------------------------------------------------------------
921 // Dump
922 //
923 // Dumps a debug information entry and all of it's attributes to the
924 // specified stream.
925 //----------------------------------------------------------------------
926 void
927 DWARFDebugInfoEntry::Dump
928 (
929     SymbolFileDWARF* dwarf2Data,
930     const DWARFCompileUnit* cu,
931     Stream &s,
932     uint32_t recurse_depth
933 ) const
934 {
935     const DataExtractor& debug_info_data = dwarf2Data->get_debug_info_data();
936     uint32_t offset = m_offset;
937 
938     if (debug_info_data.ValidOffset(offset))
939     {
940         dw_uleb128_t abbrCode = debug_info_data.GetULEB128(&offset);
941 
942         s.Printf("\n0x%8.8x: ", m_offset);
943         s.Indent();
944         if (abbrCode != m_abbr_idx)
945         {
946             s.Printf( "error: DWARF has been modified\n");
947         }
948         else if (abbrCode)
949         {
950             const DWARFAbbreviationDeclaration* abbrevDecl = cu->GetAbbreviations()->GetAbbreviationDeclaration (abbrCode);
951 
952             if (abbrevDecl)
953             {
954                 s.PutCString(DW_TAG_value_to_name(abbrevDecl->Tag()));
955                 s.Printf( " [%u] %c\n", abbrCode, abbrevDecl->HasChildren() ? '*':' ');
956 
957                 // Dump all data in the .debug_info for the attributes
958                 const uint32_t numAttributes = abbrevDecl->NumAttributes();
959                 uint32_t i;
960                 dw_attr_t attr;
961                 dw_form_t form;
962                 for (i=0; i<numAttributes; ++i)
963                 {
964                     abbrevDecl->GetAttrAndFormByIndexUnchecked(i, attr, form);
965 
966                     DumpAttribute(dwarf2Data, cu, debug_info_data, &offset, s, attr, form);
967                 }
968 
969                 const DWARFDebugInfoEntry* child = GetFirstChild();
970                 if (recurse_depth > 0 && child)
971                 {
972                     s.IndentMore();
973 
974                     while (child)
975                     {
976                         child->Dump(dwarf2Data, cu, s, recurse_depth-1);
977                         child = child->GetSibling();
978                     }
979                     s.IndentLess();
980                 }
981             }
982             else
983                 s.Printf( "Abbreviation code note found in 'debug_abbrev' class for code: %u\n", abbrCode);
984         }
985         else
986         {
987             s.Printf( "NULL\n");
988         }
989     }
990 }
991 
992 void
993 DWARFDebugInfoEntry::DumpLocation
994 (
995     SymbolFileDWARF* dwarf2Data,
996     DWARFCompileUnit* cu,
997     Stream &s
998 ) const
999 {
1000     const DWARFDebugInfoEntry *cu_die = cu->GetCompileUnitDIEOnly();
1001     const char *cu_name = NULL;
1002     if (cu_die != NULL)
1003         cu_name = cu_die->GetName (dwarf2Data, cu);
1004     const char *obj_file_name = NULL;
1005     ObjectFile *obj_file = dwarf2Data->GetObjectFile();
1006     if (obj_file)
1007         obj_file_name = obj_file->GetFileSpec().GetFilename().AsCString();
1008     const char *die_name = GetName (dwarf2Data, cu);
1009     s.Printf ("0x%8.8x/0x%8.8x: %-30s (from %s in %s)",
1010               cu->GetOffset(),
1011               GetOffset(),
1012               die_name ? die_name : "",
1013               cu_name ? cu_name : "<NULL>",
1014               obj_file_name ? obj_file_name : "<NULL>");
1015 }
1016 
1017 //----------------------------------------------------------------------
1018 // DumpAttribute
1019 //
1020 // Dumps a debug information entry attribute along with it's form. Any
1021 // special display of attributes is done (disassemble location lists,
1022 // show enumeration values for attributes, etc).
1023 //----------------------------------------------------------------------
1024 void
1025 DWARFDebugInfoEntry::DumpAttribute
1026 (
1027     SymbolFileDWARF* dwarf2Data,
1028     const DWARFCompileUnit* cu,
1029     const DataExtractor& debug_info_data,
1030     uint32_t* offset_ptr,
1031     Stream &s,
1032     dw_attr_t attr,
1033     dw_form_t form
1034 )
1035 {
1036     bool verbose    = s.GetVerbose();
1037     bool show_form  = s.GetFlags().Test(DWARFDebugInfo::eDumpFlag_ShowForm);
1038     const DataExtractor* debug_str_data = dwarf2Data ? &dwarf2Data->get_debug_str_data() : NULL;
1039     if (verbose)
1040         s.Offset (*offset_ptr);
1041     else
1042         s.Printf ("            ");
1043     s.Indent(DW_AT_value_to_name(attr));
1044 
1045     if (show_form)
1046     {
1047         s.Printf( "[%s", DW_FORM_value_to_name(form));
1048     }
1049 
1050     DWARFFormValue form_value(form);
1051 
1052     if (!form_value.ExtractValue(debug_info_data, offset_ptr, cu))
1053         return;
1054 
1055     if (show_form)
1056     {
1057         if (form == DW_FORM_indirect)
1058         {
1059             s.Printf( " [%s]", DW_FORM_value_to_name(form_value.Form()));
1060         }
1061 
1062         s.PutCString("] ");
1063     }
1064 
1065     s.PutCString("( ");
1066 
1067     // Always dump form value if verbose is enabled
1068     if (verbose)
1069     {
1070         form_value.Dump(s, debug_str_data, cu);
1071     }
1072 
1073 
1074     // Check to see if we have any special attribute formatters
1075     switch (attr)
1076     {
1077     case DW_AT_stmt_list:
1078         if ( verbose ) s.PutCString(" ( ");
1079         s.Printf( "0x%8.8llx", form_value.Unsigned());
1080         if ( verbose ) s.PutCString(" )");
1081         break;
1082 
1083     case DW_AT_language:
1084         if ( verbose ) s.PutCString(" ( ");
1085         s.PutCString(DW_LANG_value_to_name(form_value.Unsigned()));
1086         if ( verbose ) s.PutCString(" )");
1087         break;
1088 
1089     case DW_AT_encoding:
1090         if ( verbose ) s.PutCString(" ( ");
1091         s.PutCString(DW_ATE_value_to_name(form_value.Unsigned()));
1092         if ( verbose ) s.PutCString(" )");
1093         break;
1094 
1095     case DW_AT_frame_base:
1096     case DW_AT_location:
1097     case DW_AT_data_member_location:
1098         {
1099             const uint8_t* blockData = form_value.BlockData();
1100             if (blockData)
1101             {
1102                 if (!verbose)
1103                     form_value.Dump(s, debug_str_data, cu);
1104 
1105                 // Location description is inlined in data in the form value
1106                 DataExtractor locationData(debug_info_data, (*offset_ptr) - form_value.Unsigned(), form_value.Unsigned());
1107                 if ( verbose ) s.PutCString(" ( ");
1108                 print_dwarf_expression (s, locationData, DWARFCompileUnit::GetAddressByteSize(cu), 4, false);
1109                 if ( verbose ) s.PutCString(" )");
1110             }
1111             else
1112             {
1113                 // We have a location list offset as the value that is
1114                 // the offset into the .debug_loc section that describes
1115                 // the value over it's lifetime
1116                 uint64_t debug_loc_offset = form_value.Unsigned();
1117                 if (dwarf2Data)
1118                 {
1119                     if ( !verbose )
1120                         form_value.Dump(s, debug_str_data, cu);
1121                     DWARFLocationList::Dump(s, cu, dwarf2Data->get_debug_loc_data(), debug_loc_offset);
1122                 }
1123                 else
1124                 {
1125                     if ( !verbose )
1126                         form_value.Dump(s, NULL, cu);
1127                 }
1128             }
1129         }
1130         break;
1131 
1132     case DW_AT_abstract_origin:
1133     case DW_AT_specification:
1134         {
1135             uint64_t abstract_die_offset = form_value.Reference(cu);
1136             form_value.Dump(s, debug_str_data, cu);
1137         //  *ostrm_ptr << HEX32 << abstract_die_offset << " ( ";
1138             if ( verbose ) s.PutCString(" ( ");
1139             GetName(dwarf2Data, cu, abstract_die_offset, s);
1140             if ( verbose ) s.PutCString(" )");
1141         }
1142         break;
1143 
1144     case DW_AT_type:
1145         {
1146             uint64_t type_die_offset = form_value.Reference(cu);
1147             if (!verbose)
1148                 form_value.Dump(s, debug_str_data, cu);
1149             s.PutCString(" ( ");
1150             AppendTypeName(dwarf2Data, cu, type_die_offset, s);
1151             s.PutCString(" )");
1152         }
1153         break;
1154 
1155     case DW_AT_ranges:
1156         {
1157             if ( !verbose )
1158                 form_value.Dump(s, debug_str_data, cu);
1159             uint32_t ranges_offset = form_value.Unsigned();
1160             dw_addr_t base_addr = cu ? cu->GetBaseAddress() : 0;
1161             DWARFDebugRanges::Dump(s, dwarf2Data->get_debug_ranges_data(), &ranges_offset, base_addr);
1162         }
1163         break;
1164 
1165     default:
1166         if ( !verbose )
1167             form_value.Dump(s, debug_str_data, cu);
1168         break;
1169     }
1170 
1171     s.PutCString(" )\n");
1172 }
1173 
1174 //----------------------------------------------------------------------
1175 // Get all attribute values for a given DIE, including following any
1176 // specification or abstract origin attributes and including those in
1177 // the results. Any duplicate attributes will have the first instance
1178 // take precedence (this can happen for declaration attributes).
1179 //----------------------------------------------------------------------
1180 size_t
1181 DWARFDebugInfoEntry::GetAttributes
1182 (
1183     SymbolFileDWARF* dwarf2Data,
1184     const DWARFCompileUnit* cu,
1185     const uint8_t *fixed_form_sizes,
1186     DWARFDebugInfoEntry::Attributes& attributes,
1187     uint32_t curr_depth
1188 ) const
1189 {
1190     uint32_t offset;
1191     const DWARFAbbreviationDeclaration* abbrevDecl = GetAbbreviationDeclarationPtr(dwarf2Data, cu, offset);
1192 
1193     if (abbrevDecl)
1194     {
1195         const DataExtractor& debug_info_data = dwarf2Data->get_debug_info_data();
1196 
1197         if (fixed_form_sizes == NULL)
1198             fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize(cu->GetAddressByteSize());
1199 
1200         const uint32_t num_attributes = abbrevDecl->NumAttributes();
1201         uint32_t i;
1202         dw_attr_t attr;
1203         dw_form_t form;
1204         DWARFFormValue form_value;
1205         for (i=0; i<num_attributes; ++i)
1206         {
1207             abbrevDecl->GetAttrAndFormByIndexUnchecked (i, attr, form);
1208 
1209             // If we are tracking down DW_AT_specification or DW_AT_abstract_origin
1210             // attributes, the depth will be non-zero. We need to omit certain
1211             // attributes that don't make sense.
1212             switch (attr)
1213             {
1214             case DW_AT_sibling:
1215             case DW_AT_declaration:
1216                 if (curr_depth > 0)
1217                 {
1218                     // This attribute doesn't make sense when combined with
1219                     // the DIE that references this DIE. We know a DIE is
1220                     // referencing this DIE because curr_depth is not zero
1221                     break;
1222                 }
1223                 // Fall through...
1224             default:
1225                 attributes.Append(cu, offset, attr, form);
1226                 break;
1227             }
1228 
1229             if ((attr == DW_AT_specification) || (attr == DW_AT_abstract_origin))
1230             {
1231                 form_value.SetForm(form);
1232                 if (form_value.ExtractValue(debug_info_data, &offset, cu))
1233                 {
1234                     const DWARFDebugInfoEntry* die = NULL;
1235                     dw_offset_t die_offset = form_value.Reference(cu);
1236                     if (cu->ContainsDIEOffset(die_offset))
1237                     {
1238                         die = const_cast<DWARFCompileUnit*>(cu)->GetDIEPtr(die_offset);
1239                         if (die)
1240                             die->GetAttributes(dwarf2Data, cu, fixed_form_sizes, attributes, curr_depth + 1);
1241                     }
1242                     else
1243                     {
1244                         DWARFCompileUnitSP cu_sp_ptr;
1245                         die = const_cast<SymbolFileDWARF*>(dwarf2Data)->DebugInfo()->GetDIEPtr(die_offset, &cu_sp_ptr);
1246                         if (die)
1247                             die->GetAttributes(dwarf2Data, cu_sp_ptr.get(), fixed_form_sizes, attributes, curr_depth + 1);
1248                     }
1249                 }
1250             }
1251             else
1252             {
1253                 const uint8_t fixed_skip_size = fixed_form_sizes [form];
1254                 if (fixed_skip_size)
1255                     offset += fixed_skip_size;
1256                 else
1257                     DWARFFormValue::SkipValue(form, debug_info_data, &offset, cu);
1258             }
1259         }
1260     }
1261     else
1262     {
1263         attributes.Clear();
1264     }
1265     return attributes.Size();
1266 
1267 }
1268 
1269 //----------------------------------------------------------------------
1270 // GetAttributeValue
1271 //
1272 // Get the value of an attribute and return the .debug_info offset of the
1273 // attribute if it was properly extracted into form_value, or zero
1274 // if we fail since an offset of zero is invalid for an attribute (it
1275 // would be a compile unit header).
1276 //----------------------------------------------------------------------
1277 dw_offset_t
1278 DWARFDebugInfoEntry::GetAttributeValue
1279 (
1280     SymbolFileDWARF* dwarf2Data,
1281     const DWARFCompileUnit* cu,
1282     const dw_attr_t attr,
1283     DWARFFormValue& form_value,
1284     dw_offset_t* end_attr_offset_ptr
1285 ) const
1286 {
1287     uint32_t offset;
1288     const DWARFAbbreviationDeclaration* abbrevDecl = GetAbbreviationDeclarationPtr(dwarf2Data, cu, offset);
1289 
1290     if (abbrevDecl)
1291     {
1292         uint32_t attr_idx = abbrevDecl->FindAttributeIndex(attr);
1293 
1294         if (attr_idx != DW_INVALID_INDEX)
1295         {
1296             const DataExtractor& debug_info_data = dwarf2Data->get_debug_info_data();
1297 
1298             uint32_t idx=0;
1299             while (idx<attr_idx)
1300                 DWARFFormValue::SkipValue(abbrevDecl->GetFormByIndex(idx++), debug_info_data, &offset, cu);
1301 
1302             const dw_offset_t attr_offset = offset;
1303             form_value.SetForm(abbrevDecl->GetFormByIndex(idx));
1304             if (form_value.ExtractValue(debug_info_data, &offset, cu))
1305             {
1306                 if (end_attr_offset_ptr)
1307                     *end_attr_offset_ptr = offset;
1308                 return attr_offset;
1309             }
1310         }
1311     }
1312 
1313     return 0;
1314 }
1315 
1316 //----------------------------------------------------------------------
1317 // GetAttributeValueAsString
1318 //
1319 // Get the value of an attribute as a string return it. The resulting
1320 // pointer to the string data exists within the supplied SymbolFileDWARF
1321 // and will only be available as long as the SymbolFileDWARF is still around
1322 // and it's content doesn't change.
1323 //----------------------------------------------------------------------
1324 const char*
1325 DWARFDebugInfoEntry::GetAttributeValueAsString
1326 (
1327     SymbolFileDWARF* dwarf2Data,
1328     const DWARFCompileUnit* cu,
1329     const dw_attr_t attr,
1330     const char* fail_value) const
1331 {
1332     DWARFFormValue form_value;
1333     if (GetAttributeValue(dwarf2Data, cu, attr, form_value))
1334         return form_value.AsCString(&dwarf2Data->get_debug_str_data());
1335     return fail_value;
1336 }
1337 
1338 //----------------------------------------------------------------------
1339 // GetAttributeValueAsUnsigned
1340 //
1341 // Get the value of an attribute as unsigned and return it.
1342 //----------------------------------------------------------------------
1343 uint64_t
1344 DWARFDebugInfoEntry::GetAttributeValueAsUnsigned
1345 (
1346     SymbolFileDWARF* dwarf2Data,
1347     const DWARFCompileUnit* cu,
1348     const dw_attr_t attr,
1349     uint64_t fail_value
1350 ) const
1351 {
1352     DWARFFormValue form_value;
1353     if (GetAttributeValue(dwarf2Data, cu, attr, form_value))
1354         return form_value.Unsigned();
1355     return fail_value;
1356 }
1357 
1358 //----------------------------------------------------------------------
1359 // GetAttributeValueAsSigned
1360 //
1361 // Get the value of an attribute a signed value and return it.
1362 //----------------------------------------------------------------------
1363 int64_t
1364 DWARFDebugInfoEntry::GetAttributeValueAsSigned
1365 (
1366     SymbolFileDWARF* dwarf2Data,
1367     const DWARFCompileUnit* cu,
1368     const dw_attr_t attr,
1369     int64_t fail_value
1370 ) const
1371 {
1372     DWARFFormValue form_value;
1373     if (GetAttributeValue(dwarf2Data, cu, attr, form_value))
1374         return form_value.Signed();
1375     return fail_value;
1376 }
1377 
1378 //----------------------------------------------------------------------
1379 // GetAttributeValueAsReference
1380 //
1381 // Get the value of an attribute as reference and fix up and compile
1382 // unit relative offsets as needed.
1383 //----------------------------------------------------------------------
1384 uint64_t
1385 DWARFDebugInfoEntry::GetAttributeValueAsReference
1386 (
1387     SymbolFileDWARF* dwarf2Data,
1388     const DWARFCompileUnit* cu,
1389     const dw_attr_t attr,
1390     uint64_t fail_value
1391 ) const
1392 {
1393     DWARFFormValue form_value;
1394     if (GetAttributeValue(dwarf2Data, cu, attr, form_value))
1395         return form_value.Reference(cu);
1396     return fail_value;
1397 }
1398 
1399 //----------------------------------------------------------------------
1400 // GetAttributeValueAsLocation
1401 //
1402 // Get the value of an attribute as reference and fix up and compile
1403 // unit relative offsets as needed.
1404 //----------------------------------------------------------------------
1405 dw_offset_t
1406 DWARFDebugInfoEntry::GetAttributeValueAsLocation
1407 (
1408     SymbolFileDWARF* dwarf2Data,
1409     const DWARFCompileUnit* cu,
1410     const dw_attr_t attr,
1411     DataExtractor& location_data,
1412     uint32_t &block_size
1413 ) const
1414 {
1415     block_size = 0;
1416     DWARFFormValue form_value;
1417 
1418     // Empty out data in case we don't find anything
1419     location_data.Clear();
1420     dw_offset_t end_addr_offset = DW_INVALID_OFFSET;
1421     const dw_offset_t attr_offset = GetAttributeValue(dwarf2Data, cu, attr, form_value, &end_addr_offset);
1422     if (attr_offset)
1423     {
1424         const uint8_t* blockData = form_value.BlockData();
1425         if (blockData)
1426         {
1427             // We have an inlined location list in the .debug_info section
1428             const DataExtractor& debug_info = dwarf2Data->get_debug_info_data();
1429             dw_offset_t block_offset = blockData - debug_info.GetDataStart();
1430             block_size = (end_addr_offset - attr_offset) - form_value.Unsigned();
1431             location_data.SetData(debug_info, block_offset, block_size);
1432         }
1433         else
1434         {
1435             // We have a location list offset as the value that is
1436             // the offset into the .debug_loc section that describes
1437             // the value over it's lifetime
1438             dw_offset_t debug_loc_offset = form_value.Unsigned();
1439             if (dwarf2Data)
1440             {
1441                 assert(dwarf2Data->get_debug_loc_data().GetAddressByteSize() == cu->GetAddressByteSize());
1442                 return DWARFLocationList::Extract(dwarf2Data->get_debug_loc_data(), &debug_loc_offset, location_data);
1443             }
1444         }
1445     }
1446     return attr_offset;
1447 }
1448 
1449 //----------------------------------------------------------------------
1450 // GetName
1451 //
1452 // Get value of the DW_AT_name attribute and return it if one exists,
1453 // else return NULL.
1454 //----------------------------------------------------------------------
1455 const char*
1456 DWARFDebugInfoEntry::GetName
1457 (
1458     SymbolFileDWARF* dwarf2Data,
1459     const DWARFCompileUnit* cu
1460 ) const
1461 {
1462     DWARFFormValue form_value;
1463     if (GetAttributeValue(dwarf2Data, cu, DW_AT_name, form_value))
1464         return form_value.AsCString(&dwarf2Data->get_debug_str_data());
1465     return NULL;
1466 }
1467 
1468 
1469 //----------------------------------------------------------------------
1470 // GetMangledName
1471 //
1472 // Get value of the DW_AT_MIPS_linkage_name attribute and return it if
1473 // one exists, else return the value of the DW_AT_name attribute
1474 //----------------------------------------------------------------------
1475 const char*
1476 DWARFDebugInfoEntry::GetMangledName
1477 (
1478     SymbolFileDWARF* dwarf2Data,
1479     const DWARFCompileUnit* cu,
1480     bool substitute_name_allowed
1481 ) const
1482 {
1483     const char* name = NULL;
1484     DWARFFormValue form_value;
1485 
1486     if (GetAttributeValue(dwarf2Data, cu, DW_AT_MIPS_linkage_name, form_value))
1487         name = form_value.AsCString(&dwarf2Data->get_debug_str_data());
1488 
1489     if (substitute_name_allowed && name == NULL)
1490     {
1491         if (GetAttributeValue(dwarf2Data, cu, DW_AT_name, form_value))
1492             name = form_value.AsCString(&dwarf2Data->get_debug_str_data());
1493     }
1494     return name;
1495 }
1496 
1497 
1498 //----------------------------------------------------------------------
1499 // GetPubname
1500 //
1501 // Get value the name for a DIE as it should appear for a
1502 // .debug_pubnames or .debug_pubtypes section.
1503 //----------------------------------------------------------------------
1504 const char*
1505 DWARFDebugInfoEntry::GetPubname
1506 (
1507     SymbolFileDWARF* dwarf2Data,
1508     const DWARFCompileUnit* cu
1509 ) const
1510 {
1511     const char* name = NULL;
1512     DWARFFormValue form_value;
1513 
1514     if (GetAttributeValue(dwarf2Data, cu, DW_AT_MIPS_linkage_name, form_value))
1515         name = form_value.AsCString(&dwarf2Data->get_debug_str_data());
1516     else if (GetAttributeValue(dwarf2Data, cu, DW_AT_name, form_value))
1517         name = form_value.AsCString(&dwarf2Data->get_debug_str_data());
1518     else if (GetAttributeValue(dwarf2Data, cu, DW_AT_specification, form_value))
1519     {
1520         // The specification DIE may be in another compile unit so we need
1521         // to get a die and its compile unit.
1522         DWARFCompileUnitSP cu_sp_ptr;
1523         const DWARFDebugInfoEntry* die = const_cast<SymbolFileDWARF*>(dwarf2Data)->DebugInfo()->GetDIEPtr(form_value.Reference(cu), &cu_sp_ptr);
1524         if (die)
1525             return die->GetPubname(dwarf2Data, cu_sp_ptr.get());
1526     }
1527     return name;
1528 }
1529 
1530 
1531 //----------------------------------------------------------------------
1532 // GetName
1533 //
1534 // Get value of the DW_AT_name attribute for a debug information entry
1535 // that exists at offset "die_offset" and place that value into the
1536 // supplied stream object. If the DIE is a NULL object "NULL" is placed
1537 // into the stream, and if no DW_AT_name attribute exists for the DIE
1538 // then nothing is printed.
1539 //----------------------------------------------------------------------
1540 bool
1541 DWARFDebugInfoEntry::GetName
1542 (
1543     SymbolFileDWARF* dwarf2Data,
1544     const DWARFCompileUnit* cu,
1545     const uint32_t die_offset,
1546     Stream &s
1547 )
1548 {
1549     DWARFDebugInfoEntry die;
1550     uint32_t offset = die_offset;
1551     if (die.Extract(dwarf2Data, cu, &offset))
1552     {
1553         if (die.IsNULL())
1554         {
1555             s.PutCString("NULL");
1556             return true;
1557         }
1558         else
1559         {
1560             DWARFFormValue form_value;
1561             if (die.GetAttributeValue(dwarf2Data, cu, DW_AT_name, form_value))
1562             {
1563                 const char* name = form_value.AsCString(&dwarf2Data->get_debug_str_data());
1564                 if (name)
1565                 {
1566                     s.PutCString(name);
1567                     return true;
1568                 }
1569             }
1570         }
1571     }
1572     return false;
1573 }
1574 
1575 //----------------------------------------------------------------------
1576 // AppendTypeName
1577 //
1578 // Follows the type name definition down through all needed tags to
1579 // end up with a fully qualified type name and dump the results to
1580 // the supplied stream. This is used to show the name of types given
1581 // a type identifier.
1582 //----------------------------------------------------------------------
1583 bool
1584 DWARFDebugInfoEntry::AppendTypeName
1585 (
1586     SymbolFileDWARF* dwarf2Data,
1587     const DWARFCompileUnit* cu,
1588     const uint32_t die_offset,
1589     Stream &s
1590 )
1591 {
1592     DWARFDebugInfoEntry die;
1593     uint32_t offset = die_offset;
1594     if (die.Extract(dwarf2Data, cu, &offset))
1595     {
1596         if (die.IsNULL())
1597         {
1598             s.PutCString("NULL");
1599             return true;
1600         }
1601         else
1602         {
1603             const char* name = die.GetPubname(dwarf2Data, cu);
1604         //  if (die.GetAttributeValue(dwarf2Data, cu, DW_AT_name, form_value))
1605         //      name = form_value.AsCString(&dwarf2Data->get_debug_str_data());
1606             if (name)
1607                 s.PutCString(name);
1608             else
1609             {
1610                 bool result = true;
1611                 const DWARFAbbreviationDeclaration* abbrevDecl = die.GetAbbreviationDeclarationPtr(dwarf2Data, cu, offset);
1612 
1613                 switch (abbrevDecl->Tag())
1614                 {
1615                 case DW_TAG_array_type:         break;  // print out a "[]" after printing the full type of the element below
1616                 case DW_TAG_base_type:          s.PutCString("base ");         break;
1617                 case DW_TAG_class_type:         s.PutCString("class ");            break;
1618                 case DW_TAG_const_type:         s.PutCString("const ");            break;
1619                 case DW_TAG_enumeration_type:   s.PutCString("enum ");         break;
1620                 case DW_TAG_file_type:          s.PutCString("file ");         break;
1621                 case DW_TAG_interface_type:     s.PutCString("interface ");        break;
1622                 case DW_TAG_packed_type:        s.PutCString("packed ");       break;
1623                 case DW_TAG_pointer_type:       break;  // print out a '*' after printing the full type below
1624                 case DW_TAG_ptr_to_member_type: break;  // print out a '*' after printing the full type below
1625                 case DW_TAG_reference_type:     break;  // print out a '&' after printing the full type below
1626                 case DW_TAG_restrict_type:      s.PutCString("restrict ");     break;
1627                 case DW_TAG_set_type:           s.PutCString("set ");          break;
1628                 case DW_TAG_shared_type:        s.PutCString("shared ");       break;
1629                 case DW_TAG_string_type:        s.PutCString("string ");       break;
1630                 case DW_TAG_structure_type:     s.PutCString("struct ");       break;
1631                 case DW_TAG_subrange_type:      s.PutCString("subrange ");     break;
1632                 case DW_TAG_subroutine_type:    s.PutCString("function ");     break;
1633                 case DW_TAG_thrown_type:        s.PutCString("thrown ");       break;
1634                 case DW_TAG_union_type:         s.PutCString("union ");            break;
1635                 case DW_TAG_unspecified_type:   s.PutCString("unspecified ");  break;
1636                 case DW_TAG_volatile_type:      s.PutCString("volatile ");     break;
1637                 default:
1638                     return false;
1639                 }
1640 
1641                 // Follow the DW_AT_type if possible
1642                 DWARFFormValue form_value;
1643                 if (die.GetAttributeValue(dwarf2Data, cu, DW_AT_type, form_value))
1644                 {
1645                     uint64_t next_die_offset = form_value.Reference(cu);
1646                     result = AppendTypeName(dwarf2Data, cu, next_die_offset, s);
1647                 }
1648 
1649                 switch (abbrevDecl->Tag())
1650                 {
1651                 case DW_TAG_array_type:         s.PutCString("[]");    break;
1652                 case DW_TAG_pointer_type:       s.PutChar('*');    break;
1653                 case DW_TAG_ptr_to_member_type: s.PutChar('*');    break;
1654                 case DW_TAG_reference_type:     s.PutChar('&');    break;
1655                 default:
1656                     break;
1657                 }
1658                 return result;
1659             }
1660         }
1661     }
1662     return false;
1663 }
1664 
1665 bool
1666 DWARFDebugInfoEntry::Contains (const DWARFDebugInfoEntry *die) const
1667 {
1668     if (die)
1669     {
1670         const dw_offset_t die_offset = die->GetOffset();
1671         if (die_offset > GetOffset())
1672         {
1673             const DWARFDebugInfoEntry *sibling = GetSibling();
1674             assert (sibling); // TODO: take this out
1675             if (sibling)
1676                 return die_offset < sibling->GetOffset();
1677         }
1678     }
1679     return false;
1680 }
1681 
1682 //----------------------------------------------------------------------
1683 // BuildAddressRangeTable
1684 //----------------------------------------------------------------------
1685 void
1686 DWARFDebugInfoEntry::BuildAddressRangeTable
1687 (
1688     SymbolFileDWARF* dwarf2Data,
1689     const DWARFCompileUnit* cu,
1690     DWARFDebugAranges* debug_aranges
1691 ) const
1692 {
1693     if (m_tag)
1694     {
1695         if (m_tag == DW_TAG_subprogram)
1696         {
1697             dw_addr_t hi_pc = DW_INVALID_ADDRESS;
1698             dw_addr_t lo_pc = GetAttributeValueAsUnsigned(dwarf2Data, cu, DW_AT_low_pc, DW_INVALID_ADDRESS);
1699             if (lo_pc != DW_INVALID_ADDRESS)
1700                 hi_pc = GetAttributeValueAsUnsigned(dwarf2Data, cu, DW_AT_high_pc, DW_INVALID_ADDRESS);
1701             if (hi_pc != DW_INVALID_ADDRESS)
1702             {
1703             /// printf("BuildAddressRangeTable() 0x%8.8x: %30s: [0x%8.8x - 0x%8.8x)\n", m_offset, DW_TAG_value_to_name(tag), lo_pc, hi_pc);
1704                 debug_aranges->AppendRange (cu->GetOffset(), lo_pc, hi_pc);
1705             }
1706         }
1707 
1708 
1709         const DWARFDebugInfoEntry* child = GetFirstChild();
1710         while (child)
1711         {
1712             child->BuildAddressRangeTable(dwarf2Data, cu, debug_aranges);
1713             child = child->GetSibling();
1714         }
1715     }
1716 }
1717 
1718 //----------------------------------------------------------------------
1719 // BuildFunctionAddressRangeTable
1720 //
1721 // This function is very similar to the BuildAddressRangeTable function
1722 // except that the actual DIE offset for the function is placed in the
1723 // table instead of the compile unit offset (which is the way the
1724 // standard .debug_aranges section does it).
1725 //----------------------------------------------------------------------
1726 void
1727 DWARFDebugInfoEntry::BuildFunctionAddressRangeTable
1728 (
1729     SymbolFileDWARF* dwarf2Data,
1730     const DWARFCompileUnit* cu,
1731     DWARFDebugAranges* debug_aranges
1732 ) const
1733 {
1734     if (m_tag)
1735     {
1736         if (m_tag == DW_TAG_subprogram)
1737         {
1738             dw_addr_t hi_pc = DW_INVALID_ADDRESS;
1739             dw_addr_t lo_pc = GetAttributeValueAsUnsigned(dwarf2Data, cu, DW_AT_low_pc, DW_INVALID_ADDRESS);
1740             if (lo_pc != DW_INVALID_ADDRESS)
1741                 hi_pc = GetAttributeValueAsUnsigned(dwarf2Data, cu, DW_AT_high_pc, DW_INVALID_ADDRESS);
1742             if (hi_pc != DW_INVALID_ADDRESS)
1743             {
1744             //  printf("BuildAddressRangeTable() 0x%8.8x: [0x%16.16llx - 0x%16.16llx)\n", m_offset, lo_pc, hi_pc); // DEBUG ONLY
1745                 debug_aranges->AppendRange (GetOffset(), lo_pc, hi_pc);
1746             }
1747         }
1748 
1749         const DWARFDebugInfoEntry* child = GetFirstChild();
1750         while (child)
1751         {
1752             child->BuildFunctionAddressRangeTable(dwarf2Data, cu, debug_aranges);
1753             child = child->GetSibling();
1754         }
1755     }
1756 }
1757 
1758 void
1759 DWARFDebugInfoEntry::GetDeclContextDIEs (SymbolFileDWARF* dwarf2Data,
1760                                          DWARFCompileUnit* cu,
1761                                          DWARFDIECollection &decl_context_dies) const
1762 {
1763     const DWARFDebugInfoEntry *parent_decl_ctx_die = GetParentDeclContextDIE (dwarf2Data, cu);
1764     if (parent_decl_ctx_die && parent_decl_ctx_die != this)
1765     {
1766         decl_context_dies.Append(parent_decl_ctx_die);
1767         parent_decl_ctx_die->GetDeclContextDIEs (dwarf2Data, cu, decl_context_dies);
1768     }
1769 }
1770 
1771 void
1772 DWARFDebugInfoEntry::GetDWARFDeclContext (SymbolFileDWARF* dwarf2Data,
1773                                           DWARFCompileUnit* cu,
1774                                           DWARFDeclContext &dwarf_decl_ctx) const
1775 {
1776     const dw_tag_t tag = Tag();
1777     if (tag != DW_TAG_compile_unit)
1778     {
1779         dwarf_decl_ctx.AppendDeclContext(tag, GetName(dwarf2Data, cu));
1780         const DWARFDebugInfoEntry *parent_decl_ctx_die = GetParentDeclContextDIE (dwarf2Data, cu);
1781         if (parent_decl_ctx_die && parent_decl_ctx_die != this)
1782         {
1783             if (parent_decl_ctx_die->Tag() != DW_TAG_compile_unit)
1784                 parent_decl_ctx_die->GetDWARFDeclContext (dwarf2Data, cu, dwarf_decl_ctx);
1785         }
1786     }
1787 }
1788 
1789 
1790 bool
1791 DWARFDebugInfoEntry::MatchesDWARFDeclContext (SymbolFileDWARF* dwarf2Data,
1792                                               DWARFCompileUnit* cu,
1793                                               const DWARFDeclContext &dwarf_decl_ctx) const
1794 {
1795 
1796     DWARFDeclContext this_dwarf_decl_ctx;
1797     GetDWARFDeclContext (dwarf2Data, cu, this_dwarf_decl_ctx);
1798     return this_dwarf_decl_ctx == dwarf_decl_ctx;
1799 }
1800 
1801 const DWARFDebugInfoEntry *
1802 DWARFDebugInfoEntry::GetParentDeclContextDIE (SymbolFileDWARF* dwarf2Data,
1803 											  DWARFCompileUnit* cu) const
1804 {
1805 	DWARFDebugInfoEntry::Attributes attributes;
1806 	GetAttributes(dwarf2Data, cu, NULL, attributes);
1807 	return GetParentDeclContextDIE (dwarf2Data, cu, attributes);
1808 }
1809 
1810 const DWARFDebugInfoEntry *
1811 DWARFDebugInfoEntry::GetParentDeclContextDIE (SymbolFileDWARF* dwarf2Data,
1812 											  DWARFCompileUnit* cu,
1813 											  const DWARFDebugInfoEntry::Attributes& attributes) const
1814 {
1815 	const DWARFDebugInfoEntry * die = this;
1816 
1817 	while (die != NULL)
1818 	{
1819 		// If this is the original DIE that we are searching for a declaration
1820 		// for, then don't look in the cache as we don't want our own decl
1821 		// context to be our decl context...
1822 		if (die != this)
1823 		{
1824 			switch (die->Tag())
1825 			{
1826 				case DW_TAG_compile_unit:
1827 				case DW_TAG_namespace:
1828 				case DW_TAG_structure_type:
1829 				case DW_TAG_union_type:
1830 				case DW_TAG_class_type:
1831 					return die;
1832 
1833 				default:
1834 					break;
1835 			}
1836 		}
1837 
1838 		dw_offset_t die_offset;
1839 
1840 		die_offset = attributes.FormValueAsUnsigned(dwarf2Data, DW_AT_specification, DW_INVALID_OFFSET);
1841 		if (die_offset != DW_INVALID_OFFSET)
1842 		{
1843 			const DWARFDebugInfoEntry *spec_die = cu->GetDIEPtr (die_offset);
1844 			if (spec_die)
1845 			{
1846 				const DWARFDebugInfoEntry *spec_die_decl_ctx_die = spec_die->GetParentDeclContextDIE (dwarf2Data, cu);
1847 				if (spec_die_decl_ctx_die)
1848 					return spec_die_decl_ctx_die;
1849 			}
1850 		}
1851 
1852         die_offset = attributes.FormValueAsUnsigned(dwarf2Data, DW_AT_abstract_origin, DW_INVALID_OFFSET);
1853 		if (die_offset != DW_INVALID_OFFSET)
1854 		{
1855 			const DWARFDebugInfoEntry *abs_die = cu->GetDIEPtr (die_offset);
1856 			if (abs_die)
1857 			{
1858 				const DWARFDebugInfoEntry *abs_die_decl_ctx_die = abs_die->GetParentDeclContextDIE (dwarf2Data, cu);
1859 				if (abs_die_decl_ctx_die)
1860 					return abs_die_decl_ctx_die;
1861 			}
1862 		}
1863 
1864 		die = die->GetParent();
1865 	}
1866     return NULL;
1867 }
1868 
1869 
1870 const char *
1871 DWARFDebugInfoEntry::GetQualifiedName (SymbolFileDWARF* dwarf2Data,
1872 									   DWARFCompileUnit* cu,
1873 									   std::string &storage) const
1874 {
1875 	DWARFDebugInfoEntry::Attributes attributes;
1876 	GetAttributes(dwarf2Data, cu, NULL, attributes);
1877 	return GetQualifiedName (dwarf2Data, cu, attributes, storage);
1878 }
1879 
1880 const char*
1881 DWARFDebugInfoEntry::GetQualifiedName (SymbolFileDWARF* dwarf2Data,
1882 									   DWARFCompileUnit* cu,
1883 									   const DWARFDebugInfoEntry::Attributes& attributes,
1884 									   std::string &storage) const
1885 {
1886 
1887 	const char *name = GetName (dwarf2Data, cu);
1888 
1889 	if (name)
1890 	{
1891 		const DWARFDebugInfoEntry *parent_decl_ctx_die = GetParentDeclContextDIE (dwarf2Data, cu);
1892 		storage.clear();
1893 		// TODO: change this to get the correct decl context parent....
1894 		while (parent_decl_ctx_die)
1895 		{
1896 			const dw_tag_t parent_tag = parent_decl_ctx_die->Tag();
1897 			switch (parent_tag)
1898 			{
1899                 case DW_TAG_namespace:
1900 				{
1901 					const char *namespace_name = parent_decl_ctx_die->GetName (dwarf2Data, cu);
1902 					if (namespace_name)
1903 					{
1904 						storage.insert (0, "::");
1905 						storage.insert (0, namespace_name);
1906 					}
1907 					else
1908 					{
1909 						storage.insert (0, "(anonymous namespace)::");
1910 					}
1911 					parent_decl_ctx_die = parent_decl_ctx_die->GetParentDeclContextDIE(dwarf2Data, cu);
1912 				}
1913                     break;
1914 
1915                 case DW_TAG_class_type:
1916                 case DW_TAG_structure_type:
1917                 case DW_TAG_union_type:
1918 				{
1919 					const char *class_union_struct_name = parent_decl_ctx_die->GetName (dwarf2Data, cu);
1920 
1921 					if (class_union_struct_name)
1922 					{
1923 						storage.insert (0, "::");
1924 						storage.insert (0, class_union_struct_name);
1925 					}
1926 					parent_decl_ctx_die = parent_decl_ctx_die->GetParentDeclContextDIE(dwarf2Data, cu);
1927 				}
1928                     break;
1929 
1930                 default:
1931                     parent_decl_ctx_die = NULL;
1932                     break;
1933 			}
1934 		}
1935 
1936 		if (storage.empty())
1937 			storage.append ("::");
1938 
1939 		storage.append (name);
1940 	}
1941 	if (storage.empty())
1942 		return NULL;
1943 	return storage.c_str();
1944 }
1945 
1946 
1947 //----------------------------------------------------------------------
1948 // LookupAddress
1949 //----------------------------------------------------------------------
1950 bool
1951 DWARFDebugInfoEntry::LookupAddress
1952 (
1953     const dw_addr_t address,
1954     SymbolFileDWARF* dwarf2Data,
1955     const DWARFCompileUnit* cu,
1956     DWARFDebugInfoEntry** function_die,
1957     DWARFDebugInfoEntry** block_die
1958 )
1959 {
1960     bool found_address = false;
1961     if (m_tag)
1962     {
1963         bool check_children = false;
1964         bool match_addr_range = false;
1965     //  printf("0x%8.8x: %30s: address = 0x%8.8x - ", m_offset, DW_TAG_value_to_name(tag), address);
1966         switch (m_tag)
1967         {
1968         case DW_TAG_array_type                 : break;
1969         case DW_TAG_class_type                 : check_children = true; break;
1970         case DW_TAG_entry_point                : break;
1971         case DW_TAG_enumeration_type           : break;
1972         case DW_TAG_formal_parameter           : break;
1973         case DW_TAG_imported_declaration       : break;
1974         case DW_TAG_label                      : break;
1975         case DW_TAG_lexical_block              : check_children = true; match_addr_range = true; break;
1976         case DW_TAG_member                     : break;
1977         case DW_TAG_pointer_type               : break;
1978         case DW_TAG_reference_type             : break;
1979         case DW_TAG_compile_unit               : match_addr_range = true; break;
1980         case DW_TAG_string_type                : break;
1981         case DW_TAG_structure_type             : check_children = true; break;
1982         case DW_TAG_subroutine_type            : break;
1983         case DW_TAG_typedef                    : break;
1984         case DW_TAG_union_type                 : break;
1985         case DW_TAG_unspecified_parameters     : break;
1986         case DW_TAG_variant                    : break;
1987         case DW_TAG_common_block               : check_children = true; break;
1988         case DW_TAG_common_inclusion           : break;
1989         case DW_TAG_inheritance                : break;
1990         case DW_TAG_inlined_subroutine         : check_children = true; match_addr_range = true; break;
1991         case DW_TAG_module                     : match_addr_range = true; break;
1992         case DW_TAG_ptr_to_member_type         : break;
1993         case DW_TAG_set_type                   : break;
1994         case DW_TAG_subrange_type              : break;
1995         case DW_TAG_with_stmt                  : break;
1996         case DW_TAG_access_declaration         : break;
1997         case DW_TAG_base_type                  : break;
1998         case DW_TAG_catch_block                : match_addr_range = true; break;
1999         case DW_TAG_const_type                 : break;
2000         case DW_TAG_constant                   : break;
2001         case DW_TAG_enumerator                 : break;
2002         case DW_TAG_file_type                  : break;
2003         case DW_TAG_friend                     : break;
2004         case DW_TAG_namelist                   : break;
2005         case DW_TAG_namelist_item              : break;
2006         case DW_TAG_packed_type                : break;
2007         case DW_TAG_subprogram                 : match_addr_range = true; break;
2008         case DW_TAG_template_type_parameter    : break;
2009         case DW_TAG_template_value_parameter   : break;
2010         case DW_TAG_thrown_type                : break;
2011         case DW_TAG_try_block                  : match_addr_range = true; break;
2012         case DW_TAG_variant_part               : break;
2013         case DW_TAG_variable                   : break;
2014         case DW_TAG_volatile_type              : break;
2015         case DW_TAG_dwarf_procedure            : break;
2016         case DW_TAG_restrict_type              : break;
2017         case DW_TAG_interface_type             : break;
2018         case DW_TAG_namespace                  : check_children = true; break;
2019         case DW_TAG_imported_module            : break;
2020         case DW_TAG_unspecified_type           : break;
2021         case DW_TAG_partial_unit               : break;
2022         case DW_TAG_imported_unit              : break;
2023         case DW_TAG_shared_type                : break;
2024         default: break;
2025         }
2026 
2027         if (match_addr_range)
2028         {
2029             dw_addr_t lo_pc = GetAttributeValueAsUnsigned(dwarf2Data, cu, DW_AT_low_pc, DW_INVALID_ADDRESS);
2030             if (lo_pc != DW_INVALID_ADDRESS)
2031             {
2032                 dw_addr_t hi_pc = GetAttributeValueAsUnsigned(dwarf2Data, cu, DW_AT_high_pc, DW_INVALID_ADDRESS);
2033                 if (hi_pc != DW_INVALID_ADDRESS)
2034                 {
2035                     //  printf("\n0x%8.8x: %30s: address = 0x%8.8x  [0x%8.8x - 0x%8.8x) ", m_offset, DW_TAG_value_to_name(tag), address, lo_pc, hi_pc);
2036                     if ((lo_pc <= address) && (address < hi_pc))
2037                     {
2038                         found_address = true;
2039                     //  puts("***MATCH***");
2040                         switch (m_tag)
2041                         {
2042                         case DW_TAG_compile_unit:       // File
2043                             check_children = ((function_die != NULL) || (block_die != NULL));
2044                             break;
2045 
2046                         case DW_TAG_subprogram:         // Function
2047                             if (function_die)
2048                                 *function_die = this;
2049                             check_children = (block_die != NULL);
2050                             break;
2051 
2052                         case DW_TAG_inlined_subroutine: // Inlined Function
2053                         case DW_TAG_lexical_block:      // Block { } in code
2054                             if (block_die)
2055                             {
2056                                 *block_die = this;
2057                                 check_children = true;
2058                             }
2059                             break;
2060 
2061                         default:
2062                             check_children = true;
2063                             break;
2064                         }
2065                     }
2066                 }
2067                 else
2068                 {   // compile units may not have a valid high/low pc when there
2069                     // are address gaps in subroutines so we must always search
2070                     // if there is no valid high and low PC
2071                     check_children = (m_tag == DW_TAG_compile_unit) && ((function_die != NULL) || (block_die != NULL));
2072                 }
2073             }
2074             else
2075             {
2076                 dw_offset_t debug_ranges_offset = GetAttributeValueAsUnsigned(dwarf2Data, cu, DW_AT_ranges, DW_INVALID_OFFSET);
2077                 if (debug_ranges_offset != DW_INVALID_OFFSET)
2078                 {
2079                     DWARFDebugRanges::RangeList ranges;
2080                     DWARFDebugRanges* debug_ranges = dwarf2Data->DebugRanges();
2081                     debug_ranges->FindRanges(debug_ranges_offset, ranges);
2082                     // All DW_AT_ranges are relative to the base address of the
2083                     // compile unit. We add the compile unit base address to make
2084                     // sure all the addresses are properly fixed up.
2085                     ranges.Slide (cu->GetBaseAddress());
2086                     if (ranges.FindEntryThatContains(address))
2087                     {
2088                         found_address = true;
2089                     //  puts("***MATCH***");
2090                         switch (m_tag)
2091                         {
2092                         case DW_TAG_compile_unit:       // File
2093                             check_children = ((function_die != NULL) || (block_die != NULL));
2094                             break;
2095 
2096                         case DW_TAG_subprogram:         // Function
2097                             if (function_die)
2098                                 *function_die = this;
2099                             check_children = (block_die != NULL);
2100                             break;
2101 
2102                         case DW_TAG_inlined_subroutine: // Inlined Function
2103                         case DW_TAG_lexical_block:      // Block { } in code
2104                             if (block_die)
2105                             {
2106                                 *block_die = this;
2107                                 check_children = true;
2108                             }
2109                             break;
2110 
2111                         default:
2112                             check_children = true;
2113                             break;
2114                         }
2115                     }
2116                     else
2117                     {
2118                         check_children = false;
2119                     }
2120                 }
2121             }
2122         }
2123 
2124 
2125         if (check_children)
2126         {
2127         //  printf("checking children\n");
2128             DWARFDebugInfoEntry* child = GetFirstChild();
2129             while (child)
2130             {
2131                 if (child->LookupAddress(address, dwarf2Data, cu, function_die, block_die))
2132                     return true;
2133                 child = child->GetSibling();
2134             }
2135         }
2136     }
2137     return found_address;
2138 }
2139 
2140 const DWARFAbbreviationDeclaration*
2141 DWARFDebugInfoEntry::GetAbbreviationDeclarationPtr (SymbolFileDWARF* dwarf2Data,
2142                                                     const DWARFCompileUnit *cu,
2143                                                     dw_offset_t &offset) const
2144 {
2145     offset = GetOffset();
2146 
2147     const DWARFAbbreviationDeclaration* abbrev_decl = cu->GetAbbreviations()->GetAbbreviationDeclaration (m_abbr_idx);
2148     if (abbrev_decl)
2149     {
2150         // Make sure the abbreviation code still matches. If it doesn't and
2151         // the DWARF data was mmap'ed, the backing file might have been modified
2152         // which is bad news.
2153         const uint64_t abbrev_code = dwarf2Data->get_debug_info_data().GetULEB128 (&offset);
2154 
2155         if (abbrev_decl->Code() == abbrev_code)
2156             return abbrev_decl;
2157 
2158         dwarf2Data->GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("0x%8.8x: the DWARF debug information has been modified (abbrev code was %u, and is now %u)",
2159                                                                                GetOffset(),
2160                                                                                (uint32_t)abbrev_decl->Code(),
2161                                                                                (uint32_t)abbrev_code);
2162     }
2163     offset = DW_INVALID_OFFSET;
2164     return NULL;
2165 }
2166 
2167 
2168 bool
2169 DWARFDebugInfoEntry::OffsetLessThan (const DWARFDebugInfoEntry& a, const DWARFDebugInfoEntry& b)
2170 {
2171     return a.GetOffset() < b.GetOffset();
2172 }
2173 
2174 void
2175 DWARFDebugInfoEntry::DumpDIECollection (Stream &strm, DWARFDebugInfoEntry::collection &die_collection)
2176 {
2177     DWARFDebugInfoEntry::const_iterator pos;
2178     DWARFDebugInfoEntry::const_iterator end = die_collection.end();
2179     strm.PutCString("\noffset    parent   sibling  child\n");
2180     strm.PutCString("--------  -------- -------- --------\n");
2181     for (pos = die_collection.begin(); pos != end; ++pos)
2182     {
2183         const DWARFDebugInfoEntry& die_ref = *pos;
2184         const DWARFDebugInfoEntry* p = die_ref.GetParent();
2185         const DWARFDebugInfoEntry* s = die_ref.GetSibling();
2186         const DWARFDebugInfoEntry* c = die_ref.GetFirstChild();
2187         strm.Printf("%.8x: %.8x %.8x %.8x 0x%4.4x %s%s\n",
2188                     die_ref.GetOffset(),
2189                     p ? p->GetOffset() : 0,
2190                     s ? s->GetOffset() : 0,
2191                     c ? c->GetOffset() : 0,
2192                     die_ref.Tag(),
2193                     DW_TAG_value_to_name(die_ref.Tag()),
2194                     die_ref.HasChildren() ? " *" : "");
2195     }
2196 }
2197 
2198 
2199