1 //===-- DWARFCompileUnit.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 "DWARFCompileUnit.h"
11 
12 #include "lldb/Core/Mangled.h"
13 #include "lldb/Core/Module.h"
14 #include "lldb/Core/Stream.h"
15 #include "lldb/Core/Timer.h"
16 #include "lldb/Symbol/ObjectFile.h"
17 #include "lldb/Target/ObjCLanguageRuntime.h"
18 
19 #include "DWARFDebugAbbrev.h"
20 #include "DWARFDebugAranges.h"
21 #include "DWARFDebugInfo.h"
22 #include "DWARFDIECollection.h"
23 #include "DWARFFormValue.h"
24 #include "LogChannelDWARF.h"
25 #include "NameToDIE.h"
26 #include "SymbolFileDWARF.h"
27 
28 using namespace lldb;
29 using namespace lldb_private;
30 using namespace std;
31 
32 
33 extern int g_verbose;
34 
35 DWARFCompileUnit::DWARFCompileUnit(SymbolFileDWARF* dwarf2Data) :
36     m_dwarf2Data    (dwarf2Data),
37     m_abbrevs       (NULL),
38     m_user_data     (NULL),
39     m_die_array     (),
40     m_func_aranges_ap (),
41     m_base_addr     (0),
42     m_offset        (DW_INVALID_OFFSET),
43     m_length        (0),
44     m_version       (0),
45     m_addr_size     (DWARFCompileUnit::GetDefaultAddressSize()),
46     m_producer      (eProducerInvalid),
47     m_producer_version_major (0),
48     m_producer_version_minor (0),
49     m_producer_version_update (0)
50 {
51 }
52 
53 void
54 DWARFCompileUnit::Clear()
55 {
56     m_offset        = DW_INVALID_OFFSET;
57     m_length        = 0;
58     m_version       = 0;
59     m_abbrevs       = NULL;
60     m_addr_size     = DWARFCompileUnit::GetDefaultAddressSize();
61     m_base_addr     = 0;
62     m_die_array.clear();
63     m_func_aranges_ap.reset();
64     m_user_data     = NULL;
65     m_producer      = eProducerInvalid;
66 }
67 
68 bool
69 DWARFCompileUnit::Extract(const DataExtractor &debug_info, uint32_t* offset_ptr)
70 {
71     Clear();
72 
73     m_offset = *offset_ptr;
74 
75     if (debug_info.ValidOffset(*offset_ptr))
76     {
77         dw_offset_t abbr_offset;
78         const DWARFDebugAbbrev *abbr = m_dwarf2Data->DebugAbbrev();
79         m_length        = debug_info.GetU32(offset_ptr);
80         m_version       = debug_info.GetU16(offset_ptr);
81         abbr_offset     = debug_info.GetU32(offset_ptr);
82         m_addr_size     = debug_info.GetU8 (offset_ptr);
83 
84         bool length_OK = debug_info.ValidOffset(GetNextCompileUnitOffset()-1);
85         bool version_OK = SymbolFileDWARF::SupportedVersion(m_version);
86         bool abbr_offset_OK = m_dwarf2Data->get_debug_abbrev_data().ValidOffset(abbr_offset);
87         bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8));
88 
89         if (length_OK && version_OK && addr_size_OK && abbr_offset_OK && abbr != NULL)
90         {
91             m_abbrevs = abbr->GetAbbreviationDeclarationSet(abbr_offset);
92             return true;
93         }
94 
95         // reset the offset to where we tried to parse from if anything went wrong
96         *offset_ptr = m_offset;
97     }
98 
99     return false;
100 }
101 
102 
103 dw_offset_t
104 DWARFCompileUnit::Extract(dw_offset_t offset, const DataExtractor& debug_info_data, const DWARFAbbreviationDeclarationSet* abbrevs)
105 {
106     Clear();
107 
108     m_offset = offset;
109 
110     if (debug_info_data.ValidOffset(offset))
111     {
112         m_length        = debug_info_data.GetU32(&offset);
113         m_version       = debug_info_data.GetU16(&offset);
114         bool abbrevs_OK = debug_info_data.GetU32(&offset) == abbrevs->GetOffset();
115         m_abbrevs       = abbrevs;
116         m_addr_size     = debug_info_data.GetU8 (&offset);
117 
118         bool version_OK = SymbolFileDWARF::SupportedVersion(m_version);
119         bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8));
120 
121         if (version_OK && addr_size_OK && abbrevs_OK && debug_info_data.ValidOffset(offset))
122             return offset;
123     }
124     return DW_INVALID_OFFSET;
125 }
126 
127 void
128 DWARFCompileUnit::ClearDIEs(bool keep_compile_unit_die)
129 {
130     if (m_die_array.size() > 1)
131     {
132         // std::vectors never get any smaller when resized to a smaller size,
133         // or when clear() or erase() are called, the size will report that it
134         // is smaller, but the memory allocated remains intact (call capacity()
135         // to see this). So we need to create a temporary vector and swap the
136         // contents which will cause just the internal pointers to be swapped
137         // so that when "tmp_array" goes out of scope, it will destroy the
138         // contents.
139 
140         // Save at least the compile unit DIE
141         DWARFDebugInfoEntry::collection tmp_array;
142         m_die_array.swap(tmp_array);
143         if (keep_compile_unit_die)
144             m_die_array.push_back(tmp_array.front());
145     }
146 }
147 
148 //----------------------------------------------------------------------
149 // ParseCompileUnitDIEsIfNeeded
150 //
151 // Parses a compile unit and indexes its DIEs if it hasn't already been
152 // done.
153 //----------------------------------------------------------------------
154 size_t
155 DWARFCompileUnit::ExtractDIEsIfNeeded (bool cu_die_only)
156 {
157     const size_t initial_die_array_size = m_die_array.size();
158     if ((cu_die_only && initial_die_array_size > 0) || initial_die_array_size > 1)
159         return 0; // Already parsed
160 
161     Timer scoped_timer (__PRETTY_FUNCTION__,
162                         "%8.8x: DWARFCompileUnit::ExtractDIEsIfNeeded( cu_die_only = %i )",
163                         m_offset,
164                         cu_die_only);
165 
166     // Set the offset to that of the first DIE and calculate the start of the
167     // next compilation unit header.
168     uint32_t offset = GetFirstDIEOffset();
169     uint32_t next_cu_offset = GetNextCompileUnitOffset();
170 
171     DWARFDebugInfoEntry die;
172         // Keep a flat array of the DIE for binary lookup by DIE offset
173     if (!cu_die_only)
174     {
175         LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | DWARF_LOG_LOOKUPS));
176         if (log)
177         {
178             m_dwarf2Data->GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log.get(),
179                                                                                     "DWARFCompileUnit::ExtractDIEsIfNeeded () for compile unit at .debug_info[0x%8.8x]",
180                                                                                     GetOffset());
181         }
182     }
183 
184     uint32_t depth = 0;
185     // We are in our compile unit, parse starting at the offset
186     // we were told to parse
187     const DataExtractor& debug_info_data = m_dwarf2Data->get_debug_info_data();
188     std::vector<uint32_t> die_index_stack;
189     die_index_stack.reserve(32);
190     die_index_stack.push_back(0);
191     bool prev_die_had_children = false;
192     const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (GetAddressByteSize());
193     while (offset < next_cu_offset &&
194            die.FastExtract (debug_info_data, this, fixed_form_sizes, &offset))
195     {
196 //        if (log)
197 //            log->Printf("0x%8.8x: %*.*s%s%s",
198 //                        die.GetOffset(),
199 //                        depth * 2, depth * 2, "",
200 //                        DW_TAG_value_to_name (die.Tag()),
201 //                        die.HasChildren() ? " *" : "");
202 
203         const bool null_die = die.IsNULL();
204         if (depth == 0)
205         {
206             uint64_t base_addr = die.GetAttributeValueAsUnsigned(m_dwarf2Data, this, DW_AT_low_pc, LLDB_INVALID_ADDRESS);
207             if (base_addr == LLDB_INVALID_ADDRESS)
208                 base_addr = die.GetAttributeValueAsUnsigned(m_dwarf2Data, this, DW_AT_entry_pc, 0);
209             SetBaseAddress (base_addr);
210             if (initial_die_array_size == 0)
211                 AddDIE (die);
212             if (cu_die_only)
213                 return 1;
214         }
215         else
216         {
217             if (null_die)
218             {
219                 if (prev_die_had_children)
220                 {
221                     // This will only happen if a DIE says is has children
222                     // but all it contains is a NULL tag. Since we are removing
223                     // the NULL DIEs from the list (saves up to 25% in C++ code),
224                     // we need a way to let the DIE know that it actually doesn't
225                     // have children.
226                     if (!m_die_array.empty())
227                         m_die_array.back().SetEmptyChildren(true);
228                 }
229             }
230             else
231             {
232                 die.SetParentIndex(m_die_array.size() - die_index_stack[depth-1]);
233 
234                 if (die_index_stack.back())
235                     m_die_array[die_index_stack.back()].SetSiblingIndex(m_die_array.size()-die_index_stack.back());
236 
237                 // Only push the DIE if it isn't a NULL DIE
238                     m_die_array.push_back(die);
239             }
240         }
241 
242         if (null_die)
243         {
244             // NULL DIE.
245             if (!die_index_stack.empty())
246                 die_index_stack.pop_back();
247 
248             if (depth > 0)
249                 --depth;
250             if (depth == 0)
251                 break;  // We are done with this compile unit!
252 
253             prev_die_had_children = false;
254         }
255         else
256         {
257             die_index_stack.back() = m_die_array.size() - 1;
258             // Normal DIE
259             const bool die_has_children = die.HasChildren();
260             if (die_has_children)
261             {
262                 die_index_stack.push_back(0);
263                 ++depth;
264             }
265             prev_die_had_children = die_has_children;
266         }
267     }
268 
269     // Give a little bit of info if we encounter corrupt DWARF (our offset
270     // should always terminate at or before the start of the next compilation
271     // unit header).
272     if (offset > next_cu_offset)
273     {
274         m_dwarf2Data->GetObjectFile()->GetModule()->ReportWarning ("DWARF compile unit extends beyond its bounds cu 0x%8.8x at 0x%8.8x\n",
275                                                                    GetOffset(),
276                                                                    offset);
277     }
278 
279     // Since std::vector objects will double their size, we really need to
280     // make a new array with the perfect size so we don't end up wasting
281     // space. So here we copy and swap to make sure we don't have any extra
282     // memory taken up.
283 
284     if (m_die_array.size () < m_die_array.capacity())
285     {
286         DWARFDebugInfoEntry::collection exact_size_die_array (m_die_array.begin(), m_die_array.end());
287         exact_size_die_array.swap (m_die_array);
288     }
289     LogSP log (LogChannelDWARF::GetLogIfAll (DWARF_LOG_DEBUG_INFO | DWARF_LOG_VERBOSE));
290     if (log)
291     {
292         StreamString strm;
293         DWARFDebugInfoEntry::DumpDIECollection (strm, m_die_array);
294         log->PutCString (strm.GetString().c_str());
295     }
296 
297     return m_die_array.size();
298 }
299 
300 
301 dw_offset_t
302 DWARFCompileUnit::GetAbbrevOffset() const
303 {
304     return m_abbrevs ? m_abbrevs->GetOffset() : DW_INVALID_OFFSET;
305 }
306 
307 
308 
309 bool
310 DWARFCompileUnit::Verify(Stream *s) const
311 {
312     const DataExtractor& debug_info = m_dwarf2Data->get_debug_info_data();
313     bool valid_offset = debug_info.ValidOffset(m_offset);
314     bool length_OK = debug_info.ValidOffset(GetNextCompileUnitOffset()-1);
315     bool version_OK = SymbolFileDWARF::SupportedVersion(m_version);
316     bool abbr_offset_OK = m_dwarf2Data->get_debug_abbrev_data().ValidOffset(GetAbbrevOffset());
317     bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8));
318     bool verbose = s->GetVerbose();
319     if (valid_offset && length_OK && version_OK && addr_size_OK && abbr_offset_OK)
320     {
321         if (verbose)
322             s->Printf("    0x%8.8x: OK\n", m_offset);
323         return true;
324     }
325     else
326     {
327         s->Printf("    0x%8.8x: ", m_offset);
328 
329         m_dwarf2Data->get_debug_info_data().Dump (s, m_offset, lldb::eFormatHex, 1, Size(), 32, LLDB_INVALID_ADDRESS, 0, 0);
330         s->EOL();
331         if (valid_offset)
332         {
333             if (!length_OK)
334                 s->Printf("        The length (0x%8.8x) for this compile unit is too large for the .debug_info provided.\n", m_length);
335             if (!version_OK)
336                 s->Printf("        The 16 bit compile unit header version is not supported.\n");
337             if (!abbr_offset_OK)
338                 s->Printf("        The offset into the .debug_abbrev section (0x%8.8x) is not valid.\n", GetAbbrevOffset());
339             if (!addr_size_OK)
340                 s->Printf("        The address size is unsupported: 0x%2.2x\n", m_addr_size);
341         }
342         else
343             s->Printf("        The start offset of the compile unit header in the .debug_info is invalid.\n");
344     }
345     return false;
346 }
347 
348 
349 void
350 DWARFCompileUnit::Dump(Stream *s) const
351 {
352     s->Printf("0x%8.8x: Compile Unit: length = 0x%8.8x, version = 0x%4.4x, abbr_offset = 0x%8.8x, addr_size = 0x%2.2x (next CU at {0x%8.8x})\n",
353                 m_offset, m_length, m_version, GetAbbrevOffset(), m_addr_size, GetNextCompileUnitOffset());
354 }
355 
356 
357 static uint8_t g_default_addr_size = 4;
358 
359 uint8_t
360 DWARFCompileUnit::GetAddressByteSize(const DWARFCompileUnit* cu)
361 {
362     if (cu)
363         return cu->GetAddressByteSize();
364     return DWARFCompileUnit::GetDefaultAddressSize();
365 }
366 
367 uint8_t
368 DWARFCompileUnit::GetDefaultAddressSize()
369 {
370     return g_default_addr_size;
371 }
372 
373 void
374 DWARFCompileUnit::SetDefaultAddressSize(uint8_t addr_size)
375 {
376     g_default_addr_size = addr_size;
377 }
378 
379 void
380 DWARFCompileUnit::BuildAddressRangeTable (SymbolFileDWARF* dwarf2Data,
381                                           DWARFDebugAranges* debug_aranges,
382                                           bool clear_dies_if_already_not_parsed)
383 {
384     // This function is usually called if there in no .debug_aranges section
385     // in order to produce a compile unit level set of address ranges that
386     // is accurate. If the DIEs weren't parsed, then we don't want all dies for
387     // all compile units to stay loaded when they weren't needed. So we can end
388     // up parsing the DWARF and then throwing them all away to keep memory usage
389     // down.
390     const bool clear_dies = ExtractDIEsIfNeeded (false) > 1;
391 
392     const DWARFDebugInfoEntry* die = DIE();
393     if (die)
394         die->BuildAddressRangeTable(dwarf2Data, this, debug_aranges);
395 
396     // Keep memory down by clearing DIEs if this generate function
397     // caused them to be parsed
398     if (clear_dies)
399         ClearDIEs (true);
400 
401 }
402 
403 
404 const DWARFDebugAranges &
405 DWARFCompileUnit::GetFunctionAranges ()
406 {
407     if (m_func_aranges_ap.get() == NULL)
408     {
409         m_func_aranges_ap.reset (new DWARFDebugAranges());
410         LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_ARANGES));
411 
412         if (log)
413         {
414             m_dwarf2Data->GetObjectFile()->GetModule()->LogMessage (log.get(),
415                                                                     "DWARFCompileUnit::GetFunctionAranges() for compile unit at .debug_info[0x%8.8x]",
416                                                                     GetOffset());
417         }
418         const DWARFDebugInfoEntry* die = DIE();
419         if (die)
420             die->BuildFunctionAddressRangeTable (m_dwarf2Data, this, m_func_aranges_ap.get());
421         const bool minimize = false;
422         m_func_aranges_ap->Sort(minimize);
423     }
424     return *m_func_aranges_ap.get();
425 }
426 
427 bool
428 DWARFCompileUnit::LookupAddress
429 (
430     const dw_addr_t address,
431     DWARFDebugInfoEntry** function_die_handle,
432     DWARFDebugInfoEntry** block_die_handle
433 )
434 {
435     bool success = false;
436 
437     if (function_die_handle != NULL && DIE())
438     {
439 
440         const DWARFDebugAranges &func_aranges = GetFunctionAranges ();
441 
442         // Re-check the aranges auto pointer contents in case it was created above
443         if (!func_aranges.IsEmpty())
444         {
445             *function_die_handle = GetDIEPtr(func_aranges.FindAddress(address));
446             if (*function_die_handle != NULL)
447             {
448                 success = true;
449                 if (block_die_handle != NULL)
450                 {
451                     DWARFDebugInfoEntry* child = (*function_die_handle)->GetFirstChild();
452                     while (child)
453                     {
454                         if (child->LookupAddress(address, m_dwarf2Data, this, NULL, block_die_handle))
455                             break;
456                         child = child->GetSibling();
457                     }
458                 }
459             }
460         }
461     }
462     return success;
463 }
464 
465 //----------------------------------------------------------------------
466 // Compare function DWARFDebugAranges::Range structures
467 //----------------------------------------------------------------------
468 static bool CompareDIEOffset (const DWARFDebugInfoEntry& die1, const DWARFDebugInfoEntry& die2)
469 {
470     return die1.GetOffset() < die2.GetOffset();
471 }
472 
473 //----------------------------------------------------------------------
474 // GetDIEPtr()
475 //
476 // Get the DIE (Debug Information Entry) with the specified offset.
477 //----------------------------------------------------------------------
478 DWARFDebugInfoEntry*
479 DWARFCompileUnit::GetDIEPtr(dw_offset_t die_offset)
480 {
481     if (die_offset != DW_INVALID_OFFSET)
482     {
483         ExtractDIEsIfNeeded (false);
484         DWARFDebugInfoEntry compare_die;
485         compare_die.SetOffset(die_offset);
486         DWARFDebugInfoEntry::iterator end = m_die_array.end();
487         DWARFDebugInfoEntry::iterator pos = lower_bound(m_die_array.begin(), end, compare_die, CompareDIEOffset);
488         if (pos != end)
489         {
490             if (die_offset == (*pos).GetOffset())
491                 return &(*pos);
492         }
493     }
494     return NULL;    // Not found in any compile units
495 }
496 
497 //----------------------------------------------------------------------
498 // GetDIEPtrContainingOffset()
499 //
500 // Get the DIE (Debug Information Entry) that contains the specified
501 // .debug_info offset.
502 //----------------------------------------------------------------------
503 const DWARFDebugInfoEntry*
504 DWARFCompileUnit::GetDIEPtrContainingOffset(dw_offset_t die_offset)
505 {
506     if (die_offset != DW_INVALID_OFFSET)
507     {
508         ExtractDIEsIfNeeded (false);
509         DWARFDebugInfoEntry compare_die;
510         compare_die.SetOffset(die_offset);
511         DWARFDebugInfoEntry::iterator end = m_die_array.end();
512         DWARFDebugInfoEntry::iterator pos = lower_bound(m_die_array.begin(), end, compare_die, CompareDIEOffset);
513         if (pos != end)
514         {
515             if (die_offset >= (*pos).GetOffset())
516             {
517                 DWARFDebugInfoEntry::iterator next = pos + 1;
518                 if (next != end)
519                 {
520                     if (die_offset < (*next).GetOffset())
521                         return &(*pos);
522                 }
523             }
524         }
525     }
526     return NULL;    // Not found in any compile units
527 }
528 
529 
530 
531 size_t
532 DWARFCompileUnit::AppendDIEsWithTag (const dw_tag_t tag, DWARFDIECollection& dies, uint32_t depth) const
533 {
534     size_t old_size = dies.Size();
535     DWARFDebugInfoEntry::const_iterator pos;
536     DWARFDebugInfoEntry::const_iterator end = m_die_array.end();
537     for (pos = m_die_array.begin(); pos != end; ++pos)
538     {
539         if (pos->Tag() == tag)
540             dies.Append (&(*pos));
541     }
542 
543     // Return the number of DIEs added to the collection
544     return dies.Size() - old_size;
545 }
546 
547 //void
548 //DWARFCompileUnit::AddGlobalDIEByIndex (uint32_t die_idx)
549 //{
550 //    m_global_die_indexes.push_back (die_idx);
551 //}
552 //
553 //
554 //void
555 //DWARFCompileUnit::AddGlobal (const DWARFDebugInfoEntry* die)
556 //{
557 //    // Indexes to all file level global and static variables
558 //    m_global_die_indexes;
559 //
560 //    if (m_die_array.empty())
561 //        return;
562 //
563 //    const DWARFDebugInfoEntry* first_die = &m_die_array[0];
564 //    const DWARFDebugInfoEntry* end = first_die + m_die_array.size();
565 //    if (first_die <= die && die < end)
566 //        m_global_die_indexes.push_back (die - first_die);
567 //}
568 
569 
570 void
571 DWARFCompileUnit::Index (const uint32_t cu_idx,
572                          NameToDIE& func_basenames,
573                          NameToDIE& func_fullnames,
574                          NameToDIE& func_methods,
575                          NameToDIE& func_selectors,
576                          NameToDIE& objc_class_selectors,
577                          NameToDIE& globals,
578                          NameToDIE& types,
579                          NameToDIE& namespaces)
580 {
581     const DataExtractor* debug_str = &m_dwarf2Data->get_debug_str_data();
582 
583     const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (GetAddressByteSize());
584 
585     LogSP log (LogChannelDWARF::GetLogIfAll (DWARF_LOG_LOOKUPS));
586 
587     if (log)
588     {
589         m_dwarf2Data->GetObjectFile()->GetModule()->LogMessage (log.get(),
590                                                                 "DWARFCompileUnit::Index() for compile unit at .debug_info[0x%8.8x]",
591                                                                 GetOffset());
592     }
593 
594     DWARFDebugInfoEntry::const_iterator pos;
595     DWARFDebugInfoEntry::const_iterator begin = m_die_array.begin();
596     DWARFDebugInfoEntry::const_iterator end = m_die_array.end();
597     for (pos = begin; pos != end; ++pos)
598     {
599         const DWARFDebugInfoEntry &die = *pos;
600 
601         const dw_tag_t tag = die.Tag();
602 
603         switch (tag)
604         {
605         case DW_TAG_subprogram:
606         case DW_TAG_inlined_subroutine:
607         case DW_TAG_base_type:
608         case DW_TAG_class_type:
609         case DW_TAG_constant:
610         case DW_TAG_enumeration_type:
611         case DW_TAG_string_type:
612         case DW_TAG_subroutine_type:
613         case DW_TAG_structure_type:
614         case DW_TAG_union_type:
615         case DW_TAG_typedef:
616         case DW_TAG_namespace:
617         case DW_TAG_variable:
618         case DW_TAG_unspecified_type:
619             break;
620 
621         default:
622             continue;
623         }
624 
625         DWARFDebugInfoEntry::Attributes attributes;
626         const char *name = NULL;
627         const char *mangled_cstr = NULL;
628         bool is_declaration = false;
629         //bool is_artificial = false;
630         bool has_address = false;
631         bool has_location = false;
632         bool is_global_or_static_variable = false;
633 
634         dw_offset_t specification_die_offset = DW_INVALID_OFFSET;
635         const size_t num_attributes = die.GetAttributes(m_dwarf2Data, this, fixed_form_sizes, attributes);
636         if (num_attributes > 0)
637         {
638             for (uint32_t i=0; i<num_attributes; ++i)
639             {
640                 dw_attr_t attr = attributes.AttributeAtIndex(i);
641                 DWARFFormValue form_value;
642                 switch (attr)
643                 {
644                 case DW_AT_name:
645                     if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value))
646                         name = form_value.AsCString(debug_str);
647                     break;
648 
649                 case DW_AT_declaration:
650                     if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value))
651                         is_declaration = form_value.Unsigned() != 0;
652                     break;
653 
654 //                case DW_AT_artificial:
655 //                    if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value))
656 //                        is_artificial = form_value.Unsigned() != 0;
657 //                    break;
658 
659                 case DW_AT_MIPS_linkage_name:
660                     if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value))
661                         mangled_cstr = form_value.AsCString(debug_str);
662                     break;
663 
664                 case DW_AT_low_pc:
665                 case DW_AT_high_pc:
666                 case DW_AT_ranges:
667                     has_address = true;
668                     break;
669 
670                 case DW_AT_entry_pc:
671                     has_address = true;
672                     break;
673 
674                 case DW_AT_location:
675                     has_location = true;
676                     if (tag == DW_TAG_variable)
677                     {
678                         const DWARFDebugInfoEntry* parent_die = die.GetParent();
679                         while ( parent_die != NULL )
680                         {
681                             switch (parent_die->Tag())
682                             {
683                             case DW_TAG_subprogram:
684                             case DW_TAG_lexical_block:
685                             case DW_TAG_inlined_subroutine:
686                                 // Even if this is a function level static, we don't add it. We could theoretically
687                                 // add these if we wanted to by introspecting into the DW_AT_location and seeing
688                                 // if the location describes a hard coded address, but we dont want the performance
689                                 // penalty of that right now.
690                                 is_global_or_static_variable = false;
691 //                              if (attributes.ExtractFormValueAtIndex(dwarf2Data, i, form_value))
692 //                              {
693 //                                  // If we have valid block data, then we have location expression bytes
694 //                                  // that are fixed (not a location list).
695 //                                  const uint8_t *block_data = form_value.BlockData();
696 //                                  if (block_data)
697 //                                  {
698 //                                      uint32_t block_length = form_value.Unsigned();
699 //                                      if (block_length == 1 + attributes.CompileUnitAtIndex(i)->GetAddressByteSize())
700 //                                      {
701 //                                          if (block_data[0] == DW_OP_addr)
702 //                                              add_die = true;
703 //                                      }
704 //                                  }
705 //                              }
706                                 parent_die = NULL;  // Terminate the while loop.
707                                 break;
708 
709                             case DW_TAG_compile_unit:
710                                 is_global_or_static_variable = true;
711                                 parent_die = NULL;  // Terminate the while loop.
712                                 break;
713 
714                             default:
715                                 parent_die = parent_die->GetParent();   // Keep going in the while loop.
716                                 break;
717                             }
718                         }
719                     }
720                     break;
721 
722                 case DW_AT_specification:
723                     if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value))
724                         specification_die_offset = form_value.Reference(this);
725                     break;
726                 }
727             }
728         }
729 
730         switch (tag)
731         {
732         case DW_TAG_subprogram:
733             if (has_address)
734             {
735                 if (name)
736                 {
737                     // Note, this check is also done in ParseMethodName, but since this is a hot loop, we do the
738                     // simple inlined check outside the call.
739                     if (ObjCLanguageRuntime::IsPossibleObjCMethodName(name))
740                     {
741                         ConstString objc_class_name;
742                         ConstString objc_selector_name;
743                         ConstString objc_fullname_no_category_name;
744                         ConstString objc_class_name_no_category;
745                         if (ObjCLanguageRuntime::ParseMethodName (name,
746                                                                   &objc_class_name,
747                                                                   &objc_selector_name,
748                                                                   &objc_fullname_no_category_name,
749                                                                   &objc_class_name_no_category))
750                         {
751                             func_fullnames.Insert (ConstString(name), die.GetOffset());
752                             if (objc_class_name)
753                                 objc_class_selectors.Insert(objc_class_name, die.GetOffset());
754                             if (objc_class_name_no_category)
755                                 objc_class_selectors.Insert(objc_class_name_no_category, die.GetOffset());
756                             if (objc_selector_name)
757                                 func_selectors.Insert (objc_selector_name, die.GetOffset());
758                             if (objc_fullname_no_category_name)
759                                 func_fullnames.Insert (objc_fullname_no_category_name, die.GetOffset());
760                         }
761                     }
762                     // If we have a mangled name, then the DW_AT_name attribute
763                     // is usually the method name without the class or any parameters
764                     const DWARFDebugInfoEntry *parent = die.GetParent();
765                     bool is_method = false;
766                     if (parent)
767                     {
768                         dw_tag_t parent_tag = parent->Tag();
769                         if (parent_tag == DW_TAG_class_type || parent_tag == DW_TAG_structure_type)
770                         {
771                             is_method = true;
772                         }
773                         else
774                         {
775                             if (specification_die_offset != DW_INVALID_OFFSET)
776                             {
777                                 const DWARFDebugInfoEntry *specification_die = m_dwarf2Data->DebugInfo()->GetDIEPtr (specification_die_offset, NULL);
778                                 if (specification_die)
779                                 {
780                                     parent = specification_die->GetParent();
781                                     if (parent)
782                                     {
783                                         parent_tag = parent->Tag();
784 
785                                         if (parent_tag == DW_TAG_class_type || parent_tag == DW_TAG_structure_type)
786                                             is_method = true;
787                                     }
788                                 }
789                             }
790                         }
791                     }
792 
793 
794                     if (is_method)
795                         func_methods.Insert (ConstString(name), die.GetOffset());
796                     else
797                         func_basenames.Insert (ConstString(name), die.GetOffset());
798                 }
799                 if (mangled_cstr)
800                 {
801                     // Make sure our mangled name isn't the same string table entry
802                     // as our name. If it starts with '_', then it is ok, else compare
803                     // the string to make sure it isn't the same and we don't end up
804                     // with duplicate entries
805                     if (name != mangled_cstr && ((mangled_cstr[0] == '_') || (name && ::strcmp(name, mangled_cstr) != 0)))
806                     {
807                         Mangled mangled (ConstString(mangled_cstr), true);
808                         func_fullnames.Insert (mangled.GetMangledName(), die.GetOffset());
809                         if (mangled.GetDemangledName())
810                             func_fullnames.Insert (mangled.GetDemangledName(), die.GetOffset());
811                     }
812                 }
813             }
814             break;
815 
816         case DW_TAG_inlined_subroutine:
817             if (has_address)
818             {
819                 if (name)
820                     func_basenames.Insert (ConstString(name), die.GetOffset());
821                 if (mangled_cstr)
822                 {
823                     // Make sure our mangled name isn't the same string table entry
824                     // as our name. If it starts with '_', then it is ok, else compare
825                     // the string to make sure it isn't the same and we don't end up
826                     // with duplicate entries
827                     if (name != mangled_cstr && ((mangled_cstr[0] == '_') || (::strcmp(name, mangled_cstr) != 0)))
828                     {
829                         Mangled mangled (ConstString(mangled_cstr), true);
830                         func_fullnames.Insert (mangled.GetMangledName(), die.GetOffset());
831                         if (mangled.GetDemangledName())
832                             func_fullnames.Insert (mangled.GetDemangledName(), die.GetOffset());
833                     }
834                 }
835             }
836             break;
837 
838         case DW_TAG_base_type:
839         case DW_TAG_class_type:
840         case DW_TAG_constant:
841         case DW_TAG_enumeration_type:
842         case DW_TAG_string_type:
843         case DW_TAG_subroutine_type:
844         case DW_TAG_structure_type:
845         case DW_TAG_union_type:
846         case DW_TAG_typedef:
847         case DW_TAG_unspecified_type:
848             if (name && is_declaration == false)
849             {
850                 types.Insert (ConstString(name), die.GetOffset());
851             }
852             break;
853 
854         case DW_TAG_namespace:
855             if (name)
856                 namespaces.Insert (ConstString(name), die.GetOffset());
857             break;
858 
859         case DW_TAG_variable:
860             if (name && has_location && is_global_or_static_variable)
861             {
862                 globals.Insert (ConstString(name), die.GetOffset());
863                 // Be sure to include variables by their mangled and demangled
864                 // names if they have any since a variable can have a basename
865                 // "i", a mangled named "_ZN12_GLOBAL__N_11iE" and a demangled
866                 // mangled name "(anonymous namespace)::i"...
867 
868                 // Make sure our mangled name isn't the same string table entry
869                 // as our name. If it starts with '_', then it is ok, else compare
870                 // the string to make sure it isn't the same and we don't end up
871                 // with duplicate entries
872                 if (mangled_cstr && name != mangled_cstr && ((mangled_cstr[0] == '_') || (::strcmp(name, mangled_cstr) != 0)))
873                 {
874                     Mangled mangled (ConstString(mangled_cstr), true);
875                     globals.Insert (mangled.GetMangledName(), die.GetOffset());
876                     if (mangled.GetDemangledName())
877                         globals.Insert (mangled.GetDemangledName(), die.GetOffset());
878                 }
879             }
880             break;
881 
882         default:
883             continue;
884         }
885     }
886 }
887 
888 bool
889 DWARFCompileUnit::Supports_unnamed_objc_bitfields ()
890 {
891     if (GetProducer() == eProducerClang)
892     {
893         if (GetProducerVersionMajor() >= 425 && GetProducerVersionUpdate() >= 13)
894             return true;
895         else
896             return false;
897     }
898     return true; // Assume all other compilers didn't have incorrect ObjC bitfield info
899 }
900 
901 bool
902 DWARFCompileUnit::Supports_DW_AT_APPLE_objc_complete_type ()
903 {
904     if (GetProducer() == eProducerLLVMGCC)
905         return false;
906     return true;
907 }
908 
909 bool
910 DWARFCompileUnit::DW_AT_decl_file_attributes_are_invalid()
911 {
912     // llvm-gcc makes completely invalid decl file attributes and won't ever
913     // be fixed, so we need to know to ignore these.
914     return GetProducer() == eProducerLLVMGCC;
915 }
916 
917 void
918 DWARFCompileUnit::ParseProducerInfo ()
919 {
920     m_producer_version_major = UINT32_MAX;
921     m_producer_version_minor = UINT32_MAX;
922     m_producer_version_update = UINT32_MAX;
923 
924     const DWARFDebugInfoEntry *die = GetCompileUnitDIEOnly();
925     if (die)
926     {
927 
928         const char *producer_cstr = die->GetAttributeValueAsString(m_dwarf2Data, this, DW_AT_producer, NULL);
929         if (producer_cstr)
930         {
931             RegularExpression llvm_gcc_regex("^4\\.[012]\\.[01] \\(Based on Apple Inc\\. build [0-9]+\\) \\(LLVM build [\\.0-9]+\\)$");
932             if (llvm_gcc_regex.Execute (producer_cstr))
933             {
934                 m_producer = eProducerLLVMGCC;
935             }
936             else if (strstr(producer_cstr, "clang"))
937             {
938                 RegularExpression clang_regex("clang-([0-9]+)\\.([0-9]+)\\.([0-9]+)");
939                 if (clang_regex.Execute (producer_cstr, 3))
940                 {
941                     std::string str;
942                     if (clang_regex.GetMatchAtIndex (producer_cstr, 1, str))
943                         m_producer_version_major = Args::StringToUInt32(str.c_str(), UINT32_MAX, 10);
944                     if (clang_regex.GetMatchAtIndex (producer_cstr, 2, str))
945                         m_producer_version_minor = Args::StringToUInt32(str.c_str(), UINT32_MAX, 10);
946                     if (clang_regex.GetMatchAtIndex (producer_cstr, 3, str))
947                         m_producer_version_update = Args::StringToUInt32(str.c_str(), UINT32_MAX, 10);
948                 }
949                 m_producer = eProducerClang;
950             }
951             else if (strstr(producer_cstr, "GNU"))
952                 m_producer = eProducerGCC;
953         }
954     }
955     if (m_producer == eProducerInvalid)
956         m_producer = eProcucerOther;
957 }
958 
959 DWARFCompileUnit::Producer
960 DWARFCompileUnit::GetProducer ()
961 {
962     if (m_producer == eProducerInvalid)
963         ParseProducerInfo ();
964     return m_producer;
965 }
966 
967 
968 uint32_t
969 DWARFCompileUnit::GetProducerVersionMajor()
970 {
971     if (m_producer_version_major == 0)
972         ParseProducerInfo ();
973     return m_producer_version_major;
974 }
975 
976 uint32_t
977 DWARFCompileUnit::GetProducerVersionMinor()
978 {
979     if (m_producer_version_minor == 0)
980         ParseProducerInfo ();
981     return m_producer_version_minor;
982 }
983 
984 uint32_t
985 DWARFCompileUnit::GetProducerVersionUpdate()
986 {
987     if (m_producer_version_update == 0)
988         ParseProducerInfo ();
989     return m_producer_version_update;
990 }
991 
992