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 
1039     const DataExtractor* debug_str_data = dwarf2Data ? &dwarf2Data->get_debug_str_data() : NULL;
1040     if (verbose)
1041         s.Offset (*offset_ptr);
1042     else
1043         s.Printf ("            ");
1044     s.Indent(DW_AT_value_to_name(attr));
1045 
1046     if (show_form)
1047     {
1048         s.Printf( "[%s", DW_FORM_value_to_name(form));
1049     }
1050 
1051     DWARFFormValue form_value(form);
1052 
1053     if (!form_value.ExtractValue(debug_info_data, offset_ptr, cu))
1054         return;
1055 
1056     if (show_form)
1057     {
1058         if (form == DW_FORM_indirect)
1059         {
1060             s.Printf( " [%s]", DW_FORM_value_to_name(form_value.Form()));
1061         }
1062 
1063         s.PutCString("] ");
1064     }
1065 
1066     s.PutCString("( ");
1067 
1068     // Always dump form value if verbose is enabled
1069     if (verbose)
1070     {
1071         form_value.Dump(s, debug_str_data, cu);
1072     }
1073 
1074 
1075     // Check to see if we have any special attribute formatters
1076     switch (attr)
1077     {
1078     case DW_AT_stmt_list:
1079         if ( verbose ) s.PutCString(" ( ");
1080         s.Printf( "0x%8.8llx", form_value.Unsigned());
1081         if ( verbose ) s.PutCString(" )");
1082         break;
1083 
1084     case DW_AT_language:
1085         if ( verbose ) s.PutCString(" ( ");
1086         s.PutCString(DW_LANG_value_to_name(form_value.Unsigned()));
1087         if ( verbose ) s.PutCString(" )");
1088         break;
1089 
1090     case DW_AT_encoding:
1091         if ( verbose ) s.PutCString(" ( ");
1092         s.PutCString(DW_ATE_value_to_name(form_value.Unsigned()));
1093         if ( verbose ) s.PutCString(" )");
1094         break;
1095 
1096     case DW_AT_frame_base:
1097     case DW_AT_location:
1098     case DW_AT_data_member_location:
1099         {
1100             const uint8_t* blockData = form_value.BlockData();
1101             if (blockData)
1102             {
1103                 if (!verbose)
1104                     form_value.Dump(s, debug_str_data, cu);
1105 
1106                 // Location description is inlined in data in the form value
1107                 DataExtractor locationData(debug_info_data, (*offset_ptr) - form_value.Unsigned(), form_value.Unsigned());
1108                 if ( verbose ) s.PutCString(" ( ");
1109                 print_dwarf_expression (s, locationData, DWARFCompileUnit::GetAddressByteSize(cu), 4, false);
1110                 if ( verbose ) s.PutCString(" )");
1111             }
1112             else
1113             {
1114                 // We have a location list offset as the value that is
1115                 // the offset into the .debug_loc section that describes
1116                 // the value over it's lifetime
1117                 uint64_t debug_loc_offset = form_value.Unsigned();
1118                 if (dwarf2Data)
1119                 {
1120                     if ( !verbose )
1121                         form_value.Dump(s, debug_str_data, cu);
1122                     DWARFLocationList::Dump(s, cu, dwarf2Data->get_debug_loc_data(), debug_loc_offset);
1123                 }
1124                 else
1125                 {
1126                     if ( !verbose )
1127                         form_value.Dump(s, NULL, cu);
1128                 }
1129             }
1130         }
1131         break;
1132 
1133     case DW_AT_abstract_origin:
1134     case DW_AT_specification:
1135         {
1136             uint64_t abstract_die_offset = form_value.Reference(cu);
1137             form_value.Dump(s, debug_str_data, cu);
1138         //  *ostrm_ptr << HEX32 << abstract_die_offset << " ( ";
1139             if ( verbose ) s.PutCString(" ( ");
1140             GetName(dwarf2Data, cu, abstract_die_offset, s);
1141             if ( verbose ) s.PutCString(" )");
1142         }
1143         break;
1144 
1145     case DW_AT_type:
1146         {
1147             uint64_t type_die_offset = form_value.Reference(cu);
1148             if (!verbose)
1149                 form_value.Dump(s, debug_str_data, cu);
1150             s.PutCString(" ( ");
1151             AppendTypeName(dwarf2Data, cu, type_die_offset, s);
1152             s.PutCString(" )");
1153         }
1154         break;
1155 
1156     case DW_AT_ranges:
1157         {
1158             if ( !verbose )
1159                 form_value.Dump(s, debug_str_data, cu);
1160             uint32_t ranges_offset = form_value.Unsigned();
1161             dw_addr_t base_addr = cu ? cu->GetBaseAddress() : 0;
1162             if (dwarf2Data)
1163                 DWARFDebugRanges::Dump(s, dwarf2Data->get_debug_ranges_data(), &ranges_offset, base_addr);
1164         }
1165         break;
1166 
1167     default:
1168         if ( !verbose )
1169             form_value.Dump(s, debug_str_data, cu);
1170         break;
1171     }
1172 
1173     s.PutCString(" )\n");
1174 }
1175 
1176 //----------------------------------------------------------------------
1177 // Get all attribute values for a given DIE, including following any
1178 // specification or abstract origin attributes and including those in
1179 // the results. Any duplicate attributes will have the first instance
1180 // take precedence (this can happen for declaration attributes).
1181 //----------------------------------------------------------------------
1182 size_t
1183 DWARFDebugInfoEntry::GetAttributes
1184 (
1185     SymbolFileDWARF* dwarf2Data,
1186     const DWARFCompileUnit* cu,
1187     const uint8_t *fixed_form_sizes,
1188     DWARFDebugInfoEntry::Attributes& attributes,
1189     uint32_t curr_depth
1190 ) const
1191 {
1192     uint32_t offset;
1193     const DWARFAbbreviationDeclaration* abbrevDecl = GetAbbreviationDeclarationPtr(dwarf2Data, cu, offset);
1194 
1195     if (abbrevDecl)
1196     {
1197         const DataExtractor& debug_info_data = dwarf2Data->get_debug_info_data();
1198 
1199         if (fixed_form_sizes == NULL)
1200             fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize(cu->GetAddressByteSize());
1201 
1202         const uint32_t num_attributes = abbrevDecl->NumAttributes();
1203         uint32_t i;
1204         dw_attr_t attr;
1205         dw_form_t form;
1206         DWARFFormValue form_value;
1207         for (i=0; i<num_attributes; ++i)
1208         {
1209             abbrevDecl->GetAttrAndFormByIndexUnchecked (i, attr, form);
1210 
1211             // If we are tracking down DW_AT_specification or DW_AT_abstract_origin
1212             // attributes, the depth will be non-zero. We need to omit certain
1213             // attributes that don't make sense.
1214             switch (attr)
1215             {
1216             case DW_AT_sibling:
1217             case DW_AT_declaration:
1218                 if (curr_depth > 0)
1219                 {
1220                     // This attribute doesn't make sense when combined with
1221                     // the DIE that references this DIE. We know a DIE is
1222                     // referencing this DIE because curr_depth is not zero
1223                     break;
1224                 }
1225                 // Fall through...
1226             default:
1227                 attributes.Append(cu, offset, attr, form);
1228                 break;
1229             }
1230 
1231             if ((attr == DW_AT_specification) || (attr == DW_AT_abstract_origin))
1232             {
1233                 form_value.SetForm(form);
1234                 if (form_value.ExtractValue(debug_info_data, &offset, cu))
1235                 {
1236                     const DWARFDebugInfoEntry* die = NULL;
1237                     dw_offset_t die_offset = form_value.Reference(cu);
1238                     if (cu->ContainsDIEOffset(die_offset))
1239                     {
1240                         die = const_cast<DWARFCompileUnit*>(cu)->GetDIEPtr(die_offset);
1241                         if (die)
1242                             die->GetAttributes(dwarf2Data, cu, fixed_form_sizes, attributes, curr_depth + 1);
1243                     }
1244                     else
1245                     {
1246                         DWARFCompileUnitSP cu_sp_ptr;
1247                         die = const_cast<SymbolFileDWARF*>(dwarf2Data)->DebugInfo()->GetDIEPtr(die_offset, &cu_sp_ptr);
1248                         if (die)
1249                             die->GetAttributes(dwarf2Data, cu_sp_ptr.get(), fixed_form_sizes, attributes, curr_depth + 1);
1250                     }
1251                 }
1252             }
1253             else
1254             {
1255                 const uint8_t fixed_skip_size = fixed_form_sizes [form];
1256                 if (fixed_skip_size)
1257                     offset += fixed_skip_size;
1258                 else
1259                     DWARFFormValue::SkipValue(form, debug_info_data, &offset, cu);
1260             }
1261         }
1262     }
1263     else
1264     {
1265         attributes.Clear();
1266     }
1267     return attributes.Size();
1268 
1269 }
1270 
1271 //----------------------------------------------------------------------
1272 // GetAttributeValue
1273 //
1274 // Get the value of an attribute and return the .debug_info offset of the
1275 // attribute if it was properly extracted into form_value, or zero
1276 // if we fail since an offset of zero is invalid for an attribute (it
1277 // would be a compile unit header).
1278 //----------------------------------------------------------------------
1279 dw_offset_t
1280 DWARFDebugInfoEntry::GetAttributeValue
1281 (
1282     SymbolFileDWARF* dwarf2Data,
1283     const DWARFCompileUnit* cu,
1284     const dw_attr_t attr,
1285     DWARFFormValue& form_value,
1286     dw_offset_t* end_attr_offset_ptr
1287 ) const
1288 {
1289     uint32_t offset;
1290     const DWARFAbbreviationDeclaration* abbrevDecl = GetAbbreviationDeclarationPtr(dwarf2Data, cu, offset);
1291 
1292     if (abbrevDecl)
1293     {
1294         uint32_t attr_idx = abbrevDecl->FindAttributeIndex(attr);
1295 
1296         if (attr_idx != DW_INVALID_INDEX)
1297         {
1298             const DataExtractor& debug_info_data = dwarf2Data->get_debug_info_data();
1299 
1300             uint32_t idx=0;
1301             while (idx<attr_idx)
1302                 DWARFFormValue::SkipValue(abbrevDecl->GetFormByIndex(idx++), debug_info_data, &offset, cu);
1303 
1304             const dw_offset_t attr_offset = offset;
1305             form_value.SetForm(abbrevDecl->GetFormByIndex(idx));
1306             if (form_value.ExtractValue(debug_info_data, &offset, cu))
1307             {
1308                 if (end_attr_offset_ptr)
1309                     *end_attr_offset_ptr = offset;
1310                 return attr_offset;
1311             }
1312         }
1313     }
1314 
1315     return 0;
1316 }
1317 
1318 //----------------------------------------------------------------------
1319 // GetAttributeValueAsString
1320 //
1321 // Get the value of an attribute as a string return it. The resulting
1322 // pointer to the string data exists within the supplied SymbolFileDWARF
1323 // and will only be available as long as the SymbolFileDWARF is still around
1324 // and it's content doesn't change.
1325 //----------------------------------------------------------------------
1326 const char*
1327 DWARFDebugInfoEntry::GetAttributeValueAsString
1328 (
1329     SymbolFileDWARF* dwarf2Data,
1330     const DWARFCompileUnit* cu,
1331     const dw_attr_t attr,
1332     const char* fail_value) const
1333 {
1334     DWARFFormValue form_value;
1335     if (GetAttributeValue(dwarf2Data, cu, attr, form_value))
1336         return form_value.AsCString(&dwarf2Data->get_debug_str_data());
1337     return fail_value;
1338 }
1339 
1340 //----------------------------------------------------------------------
1341 // GetAttributeValueAsUnsigned
1342 //
1343 // Get the value of an attribute as unsigned and return it.
1344 //----------------------------------------------------------------------
1345 uint64_t
1346 DWARFDebugInfoEntry::GetAttributeValueAsUnsigned
1347 (
1348     SymbolFileDWARF* dwarf2Data,
1349     const DWARFCompileUnit* cu,
1350     const dw_attr_t attr,
1351     uint64_t fail_value
1352 ) const
1353 {
1354     DWARFFormValue form_value;
1355     if (GetAttributeValue(dwarf2Data, cu, attr, form_value))
1356         return form_value.Unsigned();
1357     return fail_value;
1358 }
1359 
1360 //----------------------------------------------------------------------
1361 // GetAttributeValueAsSigned
1362 //
1363 // Get the value of an attribute a signed value and return it.
1364 //----------------------------------------------------------------------
1365 int64_t
1366 DWARFDebugInfoEntry::GetAttributeValueAsSigned
1367 (
1368     SymbolFileDWARF* dwarf2Data,
1369     const DWARFCompileUnit* cu,
1370     const dw_attr_t attr,
1371     int64_t fail_value
1372 ) const
1373 {
1374     DWARFFormValue form_value;
1375     if (GetAttributeValue(dwarf2Data, cu, attr, form_value))
1376         return form_value.Signed();
1377     return fail_value;
1378 }
1379 
1380 //----------------------------------------------------------------------
1381 // GetAttributeValueAsReference
1382 //
1383 // Get the value of an attribute as reference and fix up and compile
1384 // unit relative offsets as needed.
1385 //----------------------------------------------------------------------
1386 uint64_t
1387 DWARFDebugInfoEntry::GetAttributeValueAsReference
1388 (
1389     SymbolFileDWARF* dwarf2Data,
1390     const DWARFCompileUnit* cu,
1391     const dw_attr_t attr,
1392     uint64_t fail_value
1393 ) const
1394 {
1395     DWARFFormValue form_value;
1396     if (GetAttributeValue(dwarf2Data, cu, attr, form_value))
1397         return form_value.Reference(cu);
1398     return fail_value;
1399 }
1400 
1401 //----------------------------------------------------------------------
1402 // GetAttributeValueAsLocation
1403 //
1404 // Get the value of an attribute as reference and fix up and compile
1405 // unit relative offsets as needed.
1406 //----------------------------------------------------------------------
1407 dw_offset_t
1408 DWARFDebugInfoEntry::GetAttributeValueAsLocation
1409 (
1410     SymbolFileDWARF* dwarf2Data,
1411     const DWARFCompileUnit* cu,
1412     const dw_attr_t attr,
1413     DataExtractor& location_data,
1414     uint32_t &block_size
1415 ) const
1416 {
1417     block_size = 0;
1418     DWARFFormValue form_value;
1419 
1420     // Empty out data in case we don't find anything
1421     location_data.Clear();
1422     dw_offset_t end_addr_offset = DW_INVALID_OFFSET;
1423     const dw_offset_t attr_offset = GetAttributeValue(dwarf2Data, cu, attr, form_value, &end_addr_offset);
1424     if (attr_offset)
1425     {
1426         const uint8_t* blockData = form_value.BlockData();
1427         if (blockData)
1428         {
1429             // We have an inlined location list in the .debug_info section
1430             const DataExtractor& debug_info = dwarf2Data->get_debug_info_data();
1431             dw_offset_t block_offset = blockData - debug_info.GetDataStart();
1432             block_size = (end_addr_offset - attr_offset) - form_value.Unsigned();
1433             location_data.SetData(debug_info, block_offset, block_size);
1434         }
1435         else
1436         {
1437             // We have a location list offset as the value that is
1438             // the offset into the .debug_loc section that describes
1439             // the value over it's lifetime
1440             dw_offset_t debug_loc_offset = form_value.Unsigned();
1441             if (dwarf2Data)
1442             {
1443                 assert(dwarf2Data->get_debug_loc_data().GetAddressByteSize() == cu->GetAddressByteSize());
1444                 return DWARFLocationList::Extract(dwarf2Data->get_debug_loc_data(), &debug_loc_offset, location_data);
1445             }
1446         }
1447     }
1448     return attr_offset;
1449 }
1450 
1451 //----------------------------------------------------------------------
1452 // GetName
1453 //
1454 // Get value of the DW_AT_name attribute and return it if one exists,
1455 // else return NULL.
1456 //----------------------------------------------------------------------
1457 const char*
1458 DWARFDebugInfoEntry::GetName
1459 (
1460     SymbolFileDWARF* dwarf2Data,
1461     const DWARFCompileUnit* cu
1462 ) const
1463 {
1464     DWARFFormValue form_value;
1465     if (GetAttributeValue(dwarf2Data, cu, DW_AT_name, form_value))
1466         return form_value.AsCString(&dwarf2Data->get_debug_str_data());
1467     return NULL;
1468 }
1469 
1470 
1471 //----------------------------------------------------------------------
1472 // GetMangledName
1473 //
1474 // Get value of the DW_AT_MIPS_linkage_name attribute and return it if
1475 // one exists, else return the value of the DW_AT_name attribute
1476 //----------------------------------------------------------------------
1477 const char*
1478 DWARFDebugInfoEntry::GetMangledName
1479 (
1480     SymbolFileDWARF* dwarf2Data,
1481     const DWARFCompileUnit* cu,
1482     bool substitute_name_allowed
1483 ) const
1484 {
1485     const char* name = NULL;
1486     DWARFFormValue form_value;
1487 
1488     if (GetAttributeValue(dwarf2Data, cu, DW_AT_MIPS_linkage_name, form_value))
1489         name = form_value.AsCString(&dwarf2Data->get_debug_str_data());
1490 
1491     if (substitute_name_allowed && name == NULL)
1492     {
1493         if (GetAttributeValue(dwarf2Data, cu, DW_AT_name, form_value))
1494             name = form_value.AsCString(&dwarf2Data->get_debug_str_data());
1495     }
1496     return name;
1497 }
1498 
1499 
1500 //----------------------------------------------------------------------
1501 // GetPubname
1502 //
1503 // Get value the name for a DIE as it should appear for a
1504 // .debug_pubnames or .debug_pubtypes section.
1505 //----------------------------------------------------------------------
1506 const char*
1507 DWARFDebugInfoEntry::GetPubname
1508 (
1509     SymbolFileDWARF* dwarf2Data,
1510     const DWARFCompileUnit* cu
1511 ) const
1512 {
1513     const char* name = NULL;
1514     if (!dwarf2Data)
1515         return name;
1516 
1517     DWARFFormValue form_value;
1518 
1519     if (GetAttributeValue(dwarf2Data, cu, DW_AT_MIPS_linkage_name, form_value))
1520         name = form_value.AsCString(&dwarf2Data->get_debug_str_data());
1521     else if (GetAttributeValue(dwarf2Data, cu, DW_AT_name, form_value))
1522         name = form_value.AsCString(&dwarf2Data->get_debug_str_data());
1523     else if (GetAttributeValue(dwarf2Data, cu, DW_AT_specification, form_value))
1524     {
1525         // The specification DIE may be in another compile unit so we need
1526         // to get a die and its compile unit.
1527         DWARFCompileUnitSP cu_sp_ptr;
1528         const DWARFDebugInfoEntry* die = const_cast<SymbolFileDWARF*>(dwarf2Data)->DebugInfo()->GetDIEPtr(form_value.Reference(cu), &cu_sp_ptr);
1529         if (die)
1530             return die->GetPubname(dwarf2Data, cu_sp_ptr.get());
1531     }
1532     return name;
1533 }
1534 
1535 
1536 //----------------------------------------------------------------------
1537 // GetName
1538 //
1539 // Get value of the DW_AT_name attribute for a debug information entry
1540 // that exists at offset "die_offset" and place that value into the
1541 // supplied stream object. If the DIE is a NULL object "NULL" is placed
1542 // into the stream, and if no DW_AT_name attribute exists for the DIE
1543 // then nothing is printed.
1544 //----------------------------------------------------------------------
1545 bool
1546 DWARFDebugInfoEntry::GetName
1547 (
1548     SymbolFileDWARF* dwarf2Data,
1549     const DWARFCompileUnit* cu,
1550     const uint32_t die_offset,
1551     Stream &s
1552 )
1553 {
1554     if (dwarf2Data == NULL)
1555     {
1556         s.PutCString("NULL");
1557         return false;
1558     }
1559 
1560     DWARFDebugInfoEntry die;
1561     uint32_t offset = die_offset;
1562     if (die.Extract(dwarf2Data, cu, &offset))
1563     {
1564         if (die.IsNULL())
1565         {
1566             s.PutCString("NULL");
1567             return true;
1568         }
1569         else
1570         {
1571             DWARFFormValue form_value;
1572             if (die.GetAttributeValue(dwarf2Data, cu, DW_AT_name, form_value))
1573             {
1574                 const char* name = form_value.AsCString(&dwarf2Data->get_debug_str_data());
1575                 if (name)
1576                 {
1577                     s.PutCString(name);
1578                     return true;
1579                 }
1580             }
1581         }
1582     }
1583     return false;
1584 }
1585 
1586 //----------------------------------------------------------------------
1587 // AppendTypeName
1588 //
1589 // Follows the type name definition down through all needed tags to
1590 // end up with a fully qualified type name and dump the results to
1591 // the supplied stream. This is used to show the name of types given
1592 // a type identifier.
1593 //----------------------------------------------------------------------
1594 bool
1595 DWARFDebugInfoEntry::AppendTypeName
1596 (
1597     SymbolFileDWARF* dwarf2Data,
1598     const DWARFCompileUnit* cu,
1599     const uint32_t die_offset,
1600     Stream &s
1601 )
1602 {
1603     if (dwarf2Data == NULL)
1604     {
1605         s.PutCString("NULL");
1606         return false;
1607     }
1608 
1609     DWARFDebugInfoEntry die;
1610     uint32_t offset = die_offset;
1611     if (die.Extract(dwarf2Data, cu, &offset))
1612     {
1613         if (die.IsNULL())
1614         {
1615             s.PutCString("NULL");
1616             return true;
1617         }
1618         else
1619         {
1620             const char* name = die.GetPubname(dwarf2Data, cu);
1621         //  if (die.GetAttributeValue(dwarf2Data, cu, DW_AT_name, form_value))
1622         //      name = form_value.AsCString(&dwarf2Data->get_debug_str_data());
1623             if (name)
1624                 s.PutCString(name);
1625             else
1626             {
1627                 bool result = true;
1628                 const DWARFAbbreviationDeclaration* abbrevDecl = die.GetAbbreviationDeclarationPtr(dwarf2Data, cu, offset);
1629 
1630                 if (abbrevDecl == NULL)
1631                     return false;
1632 
1633                 switch (abbrevDecl->Tag())
1634                 {
1635                 case DW_TAG_array_type:         break;  // print out a "[]" after printing the full type of the element below
1636                 case DW_TAG_base_type:          s.PutCString("base ");         break;
1637                 case DW_TAG_class_type:         s.PutCString("class ");            break;
1638                 case DW_TAG_const_type:         s.PutCString("const ");            break;
1639                 case DW_TAG_enumeration_type:   s.PutCString("enum ");         break;
1640                 case DW_TAG_file_type:          s.PutCString("file ");         break;
1641                 case DW_TAG_interface_type:     s.PutCString("interface ");        break;
1642                 case DW_TAG_packed_type:        s.PutCString("packed ");       break;
1643                 case DW_TAG_pointer_type:       break;  // print out a '*' after printing the full type below
1644                 case DW_TAG_ptr_to_member_type: break;  // print out a '*' after printing the full type below
1645                 case DW_TAG_reference_type:     break;  // print out a '&' after printing the full type below
1646                 case DW_TAG_restrict_type:      s.PutCString("restrict ");     break;
1647                 case DW_TAG_set_type:           s.PutCString("set ");          break;
1648                 case DW_TAG_shared_type:        s.PutCString("shared ");       break;
1649                 case DW_TAG_string_type:        s.PutCString("string ");       break;
1650                 case DW_TAG_structure_type:     s.PutCString("struct ");       break;
1651                 case DW_TAG_subrange_type:      s.PutCString("subrange ");     break;
1652                 case DW_TAG_subroutine_type:    s.PutCString("function ");     break;
1653                 case DW_TAG_thrown_type:        s.PutCString("thrown ");       break;
1654                 case DW_TAG_union_type:         s.PutCString("union ");            break;
1655                 case DW_TAG_unspecified_type:   s.PutCString("unspecified ");  break;
1656                 case DW_TAG_volatile_type:      s.PutCString("volatile ");     break;
1657                 default:
1658                     return false;
1659                 }
1660 
1661                 // Follow the DW_AT_type if possible
1662                 DWARFFormValue form_value;
1663                 if (die.GetAttributeValue(dwarf2Data, cu, DW_AT_type, form_value))
1664                 {
1665                     uint64_t next_die_offset = form_value.Reference(cu);
1666                     result = AppendTypeName(dwarf2Data, cu, next_die_offset, s);
1667                 }
1668 
1669                 switch (abbrevDecl->Tag())
1670                 {
1671                 case DW_TAG_array_type:         s.PutCString("[]");    break;
1672                 case DW_TAG_pointer_type:       s.PutChar('*');    break;
1673                 case DW_TAG_ptr_to_member_type: s.PutChar('*');    break;
1674                 case DW_TAG_reference_type:     s.PutChar('&');    break;
1675                 default:
1676                     break;
1677                 }
1678                 return result;
1679             }
1680         }
1681     }
1682     return false;
1683 }
1684 
1685 bool
1686 DWARFDebugInfoEntry::Contains (const DWARFDebugInfoEntry *die) const
1687 {
1688     if (die)
1689     {
1690         const dw_offset_t die_offset = die->GetOffset();
1691         if (die_offset > GetOffset())
1692         {
1693             const DWARFDebugInfoEntry *sibling = GetSibling();
1694             assert (sibling); // TODO: take this out
1695             if (sibling)
1696                 return die_offset < sibling->GetOffset();
1697         }
1698     }
1699     return false;
1700 }
1701 
1702 //----------------------------------------------------------------------
1703 // BuildAddressRangeTable
1704 //----------------------------------------------------------------------
1705 void
1706 DWARFDebugInfoEntry::BuildAddressRangeTable
1707 (
1708     SymbolFileDWARF* dwarf2Data,
1709     const DWARFCompileUnit* cu,
1710     DWARFDebugAranges* debug_aranges
1711 ) const
1712 {
1713     if (m_tag)
1714     {
1715         if (m_tag == DW_TAG_subprogram)
1716         {
1717             dw_addr_t hi_pc = DW_INVALID_ADDRESS;
1718             dw_addr_t lo_pc = GetAttributeValueAsUnsigned(dwarf2Data, cu, DW_AT_low_pc, DW_INVALID_ADDRESS);
1719             if (lo_pc != DW_INVALID_ADDRESS)
1720                 hi_pc = GetAttributeValueAsUnsigned(dwarf2Data, cu, DW_AT_high_pc, DW_INVALID_ADDRESS);
1721             if (hi_pc != DW_INVALID_ADDRESS)
1722             {
1723             /// 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);
1724                 debug_aranges->AppendRange (cu->GetOffset(), lo_pc, hi_pc);
1725             }
1726         }
1727 
1728 
1729         const DWARFDebugInfoEntry* child = GetFirstChild();
1730         while (child)
1731         {
1732             child->BuildAddressRangeTable(dwarf2Data, cu, debug_aranges);
1733             child = child->GetSibling();
1734         }
1735     }
1736 }
1737 
1738 //----------------------------------------------------------------------
1739 // BuildFunctionAddressRangeTable
1740 //
1741 // This function is very similar to the BuildAddressRangeTable function
1742 // except that the actual DIE offset for the function is placed in the
1743 // table instead of the compile unit offset (which is the way the
1744 // standard .debug_aranges section does it).
1745 //----------------------------------------------------------------------
1746 void
1747 DWARFDebugInfoEntry::BuildFunctionAddressRangeTable
1748 (
1749     SymbolFileDWARF* dwarf2Data,
1750     const DWARFCompileUnit* cu,
1751     DWARFDebugAranges* debug_aranges
1752 ) const
1753 {
1754     if (m_tag)
1755     {
1756         if (m_tag == DW_TAG_subprogram)
1757         {
1758             dw_addr_t hi_pc = DW_INVALID_ADDRESS;
1759             dw_addr_t lo_pc = GetAttributeValueAsUnsigned(dwarf2Data, cu, DW_AT_low_pc, DW_INVALID_ADDRESS);
1760             if (lo_pc != DW_INVALID_ADDRESS)
1761                 hi_pc = GetAttributeValueAsUnsigned(dwarf2Data, cu, DW_AT_high_pc, DW_INVALID_ADDRESS);
1762             if (hi_pc != DW_INVALID_ADDRESS)
1763             {
1764             //  printf("BuildAddressRangeTable() 0x%8.8x: [0x%16.16llx - 0x%16.16llx)\n", m_offset, lo_pc, hi_pc); // DEBUG ONLY
1765                 debug_aranges->AppendRange (GetOffset(), lo_pc, hi_pc);
1766             }
1767         }
1768 
1769         const DWARFDebugInfoEntry* child = GetFirstChild();
1770         while (child)
1771         {
1772             child->BuildFunctionAddressRangeTable(dwarf2Data, cu, debug_aranges);
1773             child = child->GetSibling();
1774         }
1775     }
1776 }
1777 
1778 void
1779 DWARFDebugInfoEntry::GetDeclContextDIEs (SymbolFileDWARF* dwarf2Data,
1780                                          DWARFCompileUnit* cu,
1781                                          DWARFDIECollection &decl_context_dies) const
1782 {
1783     const DWARFDebugInfoEntry *parent_decl_ctx_die = GetParentDeclContextDIE (dwarf2Data, cu);
1784     if (parent_decl_ctx_die && parent_decl_ctx_die != this)
1785     {
1786         decl_context_dies.Append(parent_decl_ctx_die);
1787         parent_decl_ctx_die->GetDeclContextDIEs (dwarf2Data, cu, decl_context_dies);
1788     }
1789 }
1790 
1791 void
1792 DWARFDebugInfoEntry::GetDWARFDeclContext (SymbolFileDWARF* dwarf2Data,
1793                                           DWARFCompileUnit* cu,
1794                                           DWARFDeclContext &dwarf_decl_ctx) const
1795 {
1796     const dw_tag_t tag = Tag();
1797     if (tag != DW_TAG_compile_unit)
1798     {
1799         dwarf_decl_ctx.AppendDeclContext(tag, GetName(dwarf2Data, cu));
1800         const DWARFDebugInfoEntry *parent_decl_ctx_die = GetParentDeclContextDIE (dwarf2Data, cu);
1801         if (parent_decl_ctx_die && parent_decl_ctx_die != this)
1802         {
1803             if (parent_decl_ctx_die->Tag() != DW_TAG_compile_unit)
1804                 parent_decl_ctx_die->GetDWARFDeclContext (dwarf2Data, cu, dwarf_decl_ctx);
1805         }
1806     }
1807 }
1808 
1809 
1810 bool
1811 DWARFDebugInfoEntry::MatchesDWARFDeclContext (SymbolFileDWARF* dwarf2Data,
1812                                               DWARFCompileUnit* cu,
1813                                               const DWARFDeclContext &dwarf_decl_ctx) const
1814 {
1815 
1816     DWARFDeclContext this_dwarf_decl_ctx;
1817     GetDWARFDeclContext (dwarf2Data, cu, this_dwarf_decl_ctx);
1818     return this_dwarf_decl_ctx == dwarf_decl_ctx;
1819 }
1820 
1821 const DWARFDebugInfoEntry *
1822 DWARFDebugInfoEntry::GetParentDeclContextDIE (SymbolFileDWARF* dwarf2Data,
1823 											  DWARFCompileUnit* cu) const
1824 {
1825 	DWARFDebugInfoEntry::Attributes attributes;
1826 	GetAttributes(dwarf2Data, cu, NULL, attributes);
1827 	return GetParentDeclContextDIE (dwarf2Data, cu, attributes);
1828 }
1829 
1830 const DWARFDebugInfoEntry *
1831 DWARFDebugInfoEntry::GetParentDeclContextDIE (SymbolFileDWARF* dwarf2Data,
1832 											  DWARFCompileUnit* cu,
1833 											  const DWARFDebugInfoEntry::Attributes& attributes) const
1834 {
1835 	const DWARFDebugInfoEntry * die = this;
1836 
1837 	while (die != NULL)
1838 	{
1839 		// If this is the original DIE that we are searching for a declaration
1840 		// for, then don't look in the cache as we don't want our own decl
1841 		// context to be our decl context...
1842 		if (die != this)
1843 		{
1844 			switch (die->Tag())
1845 			{
1846 				case DW_TAG_compile_unit:
1847 				case DW_TAG_namespace:
1848 				case DW_TAG_structure_type:
1849 				case DW_TAG_union_type:
1850 				case DW_TAG_class_type:
1851 					return die;
1852 
1853 				default:
1854 					break;
1855 			}
1856 		}
1857 
1858 		dw_offset_t die_offset;
1859 
1860 		die_offset = attributes.FormValueAsUnsigned(dwarf2Data, DW_AT_specification, DW_INVALID_OFFSET);
1861 		if (die_offset != DW_INVALID_OFFSET)
1862 		{
1863 			const DWARFDebugInfoEntry *spec_die = cu->GetDIEPtr (die_offset);
1864 			if (spec_die)
1865 			{
1866 				const DWARFDebugInfoEntry *spec_die_decl_ctx_die = spec_die->GetParentDeclContextDIE (dwarf2Data, cu);
1867 				if (spec_die_decl_ctx_die)
1868 					return spec_die_decl_ctx_die;
1869 			}
1870 		}
1871 
1872         die_offset = attributes.FormValueAsUnsigned(dwarf2Data, DW_AT_abstract_origin, DW_INVALID_OFFSET);
1873 		if (die_offset != DW_INVALID_OFFSET)
1874 		{
1875 			const DWARFDebugInfoEntry *abs_die = cu->GetDIEPtr (die_offset);
1876 			if (abs_die)
1877 			{
1878 				const DWARFDebugInfoEntry *abs_die_decl_ctx_die = abs_die->GetParentDeclContextDIE (dwarf2Data, cu);
1879 				if (abs_die_decl_ctx_die)
1880 					return abs_die_decl_ctx_die;
1881 			}
1882 		}
1883 
1884 		die = die->GetParent();
1885 	}
1886     return NULL;
1887 }
1888 
1889 
1890 const char *
1891 DWARFDebugInfoEntry::GetQualifiedName (SymbolFileDWARF* dwarf2Data,
1892 									   DWARFCompileUnit* cu,
1893 									   std::string &storage) const
1894 {
1895 	DWARFDebugInfoEntry::Attributes attributes;
1896 	GetAttributes(dwarf2Data, cu, NULL, attributes);
1897 	return GetQualifiedName (dwarf2Data, cu, attributes, storage);
1898 }
1899 
1900 const char*
1901 DWARFDebugInfoEntry::GetQualifiedName (SymbolFileDWARF* dwarf2Data,
1902 									   DWARFCompileUnit* cu,
1903 									   const DWARFDebugInfoEntry::Attributes& attributes,
1904 									   std::string &storage) const
1905 {
1906 
1907 	const char *name = GetName (dwarf2Data, cu);
1908 
1909 	if (name)
1910 	{
1911 		const DWARFDebugInfoEntry *parent_decl_ctx_die = GetParentDeclContextDIE (dwarf2Data, cu);
1912 		storage.clear();
1913 		// TODO: change this to get the correct decl context parent....
1914 		while (parent_decl_ctx_die)
1915 		{
1916 			const dw_tag_t parent_tag = parent_decl_ctx_die->Tag();
1917 			switch (parent_tag)
1918 			{
1919                 case DW_TAG_namespace:
1920 				{
1921 					const char *namespace_name = parent_decl_ctx_die->GetName (dwarf2Data, cu);
1922 					if (namespace_name)
1923 					{
1924 						storage.insert (0, "::");
1925 						storage.insert (0, namespace_name);
1926 					}
1927 					else
1928 					{
1929 						storage.insert (0, "(anonymous namespace)::");
1930 					}
1931 					parent_decl_ctx_die = parent_decl_ctx_die->GetParentDeclContextDIE(dwarf2Data, cu);
1932 				}
1933                     break;
1934 
1935                 case DW_TAG_class_type:
1936                 case DW_TAG_structure_type:
1937                 case DW_TAG_union_type:
1938 				{
1939 					const char *class_union_struct_name = parent_decl_ctx_die->GetName (dwarf2Data, cu);
1940 
1941 					if (class_union_struct_name)
1942 					{
1943 						storage.insert (0, "::");
1944 						storage.insert (0, class_union_struct_name);
1945 					}
1946 					parent_decl_ctx_die = parent_decl_ctx_die->GetParentDeclContextDIE(dwarf2Data, cu);
1947 				}
1948                     break;
1949 
1950                 default:
1951                     parent_decl_ctx_die = NULL;
1952                     break;
1953 			}
1954 		}
1955 
1956 		if (storage.empty())
1957 			storage.append ("::");
1958 
1959 		storage.append (name);
1960 	}
1961 	if (storage.empty())
1962 		return NULL;
1963 	return storage.c_str();
1964 }
1965 
1966 
1967 //----------------------------------------------------------------------
1968 // LookupAddress
1969 //----------------------------------------------------------------------
1970 bool
1971 DWARFDebugInfoEntry::LookupAddress
1972 (
1973     const dw_addr_t address,
1974     SymbolFileDWARF* dwarf2Data,
1975     const DWARFCompileUnit* cu,
1976     DWARFDebugInfoEntry** function_die,
1977     DWARFDebugInfoEntry** block_die
1978 )
1979 {
1980     bool found_address = false;
1981     if (m_tag)
1982     {
1983         bool check_children = false;
1984         bool match_addr_range = false;
1985     //  printf("0x%8.8x: %30s: address = 0x%8.8x - ", m_offset, DW_TAG_value_to_name(tag), address);
1986         switch (m_tag)
1987         {
1988         case DW_TAG_array_type                 : break;
1989         case DW_TAG_class_type                 : check_children = true; break;
1990         case DW_TAG_entry_point                : break;
1991         case DW_TAG_enumeration_type           : break;
1992         case DW_TAG_formal_parameter           : break;
1993         case DW_TAG_imported_declaration       : break;
1994         case DW_TAG_label                      : break;
1995         case DW_TAG_lexical_block              : check_children = true; match_addr_range = true; break;
1996         case DW_TAG_member                     : break;
1997         case DW_TAG_pointer_type               : break;
1998         case DW_TAG_reference_type             : break;
1999         case DW_TAG_compile_unit               : match_addr_range = true; break;
2000         case DW_TAG_string_type                : break;
2001         case DW_TAG_structure_type             : check_children = true; break;
2002         case DW_TAG_subroutine_type            : break;
2003         case DW_TAG_typedef                    : break;
2004         case DW_TAG_union_type                 : break;
2005         case DW_TAG_unspecified_parameters     : break;
2006         case DW_TAG_variant                    : break;
2007         case DW_TAG_common_block               : check_children = true; break;
2008         case DW_TAG_common_inclusion           : break;
2009         case DW_TAG_inheritance                : break;
2010         case DW_TAG_inlined_subroutine         : check_children = true; match_addr_range = true; break;
2011         case DW_TAG_module                     : match_addr_range = true; break;
2012         case DW_TAG_ptr_to_member_type         : break;
2013         case DW_TAG_set_type                   : break;
2014         case DW_TAG_subrange_type              : break;
2015         case DW_TAG_with_stmt                  : break;
2016         case DW_TAG_access_declaration         : break;
2017         case DW_TAG_base_type                  : break;
2018         case DW_TAG_catch_block                : match_addr_range = true; break;
2019         case DW_TAG_const_type                 : break;
2020         case DW_TAG_constant                   : break;
2021         case DW_TAG_enumerator                 : break;
2022         case DW_TAG_file_type                  : break;
2023         case DW_TAG_friend                     : break;
2024         case DW_TAG_namelist                   : break;
2025         case DW_TAG_namelist_item              : break;
2026         case DW_TAG_packed_type                : break;
2027         case DW_TAG_subprogram                 : match_addr_range = true; break;
2028         case DW_TAG_template_type_parameter    : break;
2029         case DW_TAG_template_value_parameter   : break;
2030         case DW_TAG_thrown_type                : break;
2031         case DW_TAG_try_block                  : match_addr_range = true; break;
2032         case DW_TAG_variant_part               : break;
2033         case DW_TAG_variable                   : break;
2034         case DW_TAG_volatile_type              : break;
2035         case DW_TAG_dwarf_procedure            : break;
2036         case DW_TAG_restrict_type              : break;
2037         case DW_TAG_interface_type             : break;
2038         case DW_TAG_namespace                  : check_children = true; break;
2039         case DW_TAG_imported_module            : break;
2040         case DW_TAG_unspecified_type           : break;
2041         case DW_TAG_partial_unit               : break;
2042         case DW_TAG_imported_unit              : break;
2043         case DW_TAG_shared_type                : break;
2044         default: break;
2045         }
2046 
2047         if (match_addr_range)
2048         {
2049             dw_addr_t lo_pc = GetAttributeValueAsUnsigned(dwarf2Data, cu, DW_AT_low_pc, DW_INVALID_ADDRESS);
2050             if (lo_pc != DW_INVALID_ADDRESS)
2051             {
2052                 dw_addr_t hi_pc = GetAttributeValueAsUnsigned(dwarf2Data, cu, DW_AT_high_pc, DW_INVALID_ADDRESS);
2053                 if (hi_pc != DW_INVALID_ADDRESS)
2054                 {
2055                     //  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);
2056                     if ((lo_pc <= address) && (address < hi_pc))
2057                     {
2058                         found_address = true;
2059                     //  puts("***MATCH***");
2060                         switch (m_tag)
2061                         {
2062                         case DW_TAG_compile_unit:       // File
2063                             check_children = ((function_die != NULL) || (block_die != NULL));
2064                             break;
2065 
2066                         case DW_TAG_subprogram:         // Function
2067                             if (function_die)
2068                                 *function_die = this;
2069                             check_children = (block_die != NULL);
2070                             break;
2071 
2072                         case DW_TAG_inlined_subroutine: // Inlined Function
2073                         case DW_TAG_lexical_block:      // Block { } in code
2074                             if (block_die)
2075                             {
2076                                 *block_die = this;
2077                                 check_children = true;
2078                             }
2079                             break;
2080 
2081                         default:
2082                             check_children = true;
2083                             break;
2084                         }
2085                     }
2086                 }
2087                 else
2088                 {   // compile units may not have a valid high/low pc when there
2089                     // are address gaps in subroutines so we must always search
2090                     // if there is no valid high and low PC
2091                     check_children = (m_tag == DW_TAG_compile_unit) && ((function_die != NULL) || (block_die != NULL));
2092                 }
2093             }
2094             else
2095             {
2096                 dw_offset_t debug_ranges_offset = GetAttributeValueAsUnsigned(dwarf2Data, cu, DW_AT_ranges, DW_INVALID_OFFSET);
2097                 if (debug_ranges_offset != DW_INVALID_OFFSET)
2098                 {
2099                     DWARFDebugRanges::RangeList ranges;
2100                     DWARFDebugRanges* debug_ranges = dwarf2Data->DebugRanges();
2101                     debug_ranges->FindRanges(debug_ranges_offset, ranges);
2102                     // All DW_AT_ranges are relative to the base address of the
2103                     // compile unit. We add the compile unit base address to make
2104                     // sure all the addresses are properly fixed up.
2105                     ranges.Slide (cu->GetBaseAddress());
2106                     if (ranges.FindEntryThatContains(address))
2107                     {
2108                         found_address = true;
2109                     //  puts("***MATCH***");
2110                         switch (m_tag)
2111                         {
2112                         case DW_TAG_compile_unit:       // File
2113                             check_children = ((function_die != NULL) || (block_die != NULL));
2114                             break;
2115 
2116                         case DW_TAG_subprogram:         // Function
2117                             if (function_die)
2118                                 *function_die = this;
2119                             check_children = (block_die != NULL);
2120                             break;
2121 
2122                         case DW_TAG_inlined_subroutine: // Inlined Function
2123                         case DW_TAG_lexical_block:      // Block { } in code
2124                             if (block_die)
2125                             {
2126                                 *block_die = this;
2127                                 check_children = true;
2128                             }
2129                             break;
2130 
2131                         default:
2132                             check_children = true;
2133                             break;
2134                         }
2135                     }
2136                     else
2137                     {
2138                         check_children = false;
2139                     }
2140                 }
2141             }
2142         }
2143 
2144 
2145         if (check_children)
2146         {
2147         //  printf("checking children\n");
2148             DWARFDebugInfoEntry* child = GetFirstChild();
2149             while (child)
2150             {
2151                 if (child->LookupAddress(address, dwarf2Data, cu, function_die, block_die))
2152                     return true;
2153                 child = child->GetSibling();
2154             }
2155         }
2156     }
2157     return found_address;
2158 }
2159 
2160 const DWARFAbbreviationDeclaration*
2161 DWARFDebugInfoEntry::GetAbbreviationDeclarationPtr (SymbolFileDWARF* dwarf2Data,
2162                                                     const DWARFCompileUnit *cu,
2163                                                     dw_offset_t &offset) const
2164 {
2165     if (dwarf2Data)
2166     {
2167         offset = GetOffset();
2168 
2169         const DWARFAbbreviationDeclaration* abbrev_decl = cu->GetAbbreviations()->GetAbbreviationDeclaration (m_abbr_idx);
2170         if (abbrev_decl)
2171         {
2172             // Make sure the abbreviation code still matches. If it doesn't and
2173             // the DWARF data was mmap'ed, the backing file might have been modified
2174             // which is bad news.
2175             const uint64_t abbrev_code = dwarf2Data->get_debug_info_data().GetULEB128 (&offset);
2176 
2177             if (abbrev_decl->Code() == abbrev_code)
2178                 return abbrev_decl;
2179 
2180             dwarf2Data->GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("0x%8.8x: the DWARF debug information has been modified (abbrev code was %u, and is now %u)",
2181                                                                                    GetOffset(),
2182                                                                                    (uint32_t)abbrev_decl->Code(),
2183                                                                                    (uint32_t)abbrev_code);
2184         }
2185     }
2186     offset = DW_INVALID_OFFSET;
2187     return NULL;
2188 }
2189 
2190 
2191 bool
2192 DWARFDebugInfoEntry::OffsetLessThan (const DWARFDebugInfoEntry& a, const DWARFDebugInfoEntry& b)
2193 {
2194     return a.GetOffset() < b.GetOffset();
2195 }
2196 
2197 void
2198 DWARFDebugInfoEntry::DumpDIECollection (Stream &strm, DWARFDebugInfoEntry::collection &die_collection)
2199 {
2200     DWARFDebugInfoEntry::const_iterator pos;
2201     DWARFDebugInfoEntry::const_iterator end = die_collection.end();
2202     strm.PutCString("\noffset    parent   sibling  child\n");
2203     strm.PutCString("--------  -------- -------- --------\n");
2204     for (pos = die_collection.begin(); pos != end; ++pos)
2205     {
2206         const DWARFDebugInfoEntry& die_ref = *pos;
2207         const DWARFDebugInfoEntry* p = die_ref.GetParent();
2208         const DWARFDebugInfoEntry* s = die_ref.GetSibling();
2209         const DWARFDebugInfoEntry* c = die_ref.GetFirstChild();
2210         strm.Printf("%.8x: %.8x %.8x %.8x 0x%4.4x %s%s\n",
2211                     die_ref.GetOffset(),
2212                     p ? p->GetOffset() : 0,
2213                     s ? s->GetOffset() : 0,
2214                     c ? c->GetOffset() : 0,
2215                     die_ref.Tag(),
2216                     DW_TAG_value_to_name(die_ref.Tag()),
2217                     die_ref.HasChildren() ? " *" : "");
2218     }
2219 }
2220 
2221 
2222