1 //===-- DWARFUnit.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 "DWARFUnit.h"
11 
12 #include "lldb/Core/Module.h"
13 #include "lldb/Host/StringConvert.h"
14 #include "lldb/Symbol/CompileUnit.h"
15 #include "lldb/Symbol/LineTable.h"
16 #include "lldb/Symbol/ObjectFile.h"
17 #include "lldb/Utility/StreamString.h"
18 #include "lldb/Utility/Timer.h"
19 
20 #include "DWARFDIECollection.h"
21 #include "DWARFDebugAranges.h"
22 #include "DWARFDebugInfo.h"
23 #include "LogChannelDWARF.h"
24 #include "SymbolFileDWARFDebugMap.h"
25 #include "SymbolFileDWARFDwo.h"
26 
27 using namespace lldb;
28 using namespace lldb_private;
29 using namespace std;
30 
31 extern int g_verbose;
32 
33 DWARFUnit::DWARFUnit(SymbolFileDWARF *dwarf) : m_dwarf(dwarf) {}
34 
35 DWARFUnit::~DWARFUnit() {}
36 
37 //----------------------------------------------------------------------
38 // ParseCompileUnitDIEsIfNeeded
39 //
40 // Parses a compile unit and indexes its DIEs if it hasn't already been done.
41 //----------------------------------------------------------------------
42 size_t DWARFUnit::ExtractDIEsIfNeeded(bool cu_die_only) {
43   const size_t initial_die_array_size = m_die_array.size();
44   if ((cu_die_only && initial_die_array_size > 0) || initial_die_array_size > 1)
45     return 0; // Already parsed
46 
47   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
48   Timer scoped_timer(
49       func_cat, "%8.8x: DWARFUnit::ExtractDIEsIfNeeded( cu_die_only = %i )",
50       m_offset, cu_die_only);
51 
52   // Set the offset to that of the first DIE and calculate the start of the
53   // next compilation unit header.
54   lldb::offset_t offset = GetFirstDIEOffset();
55   lldb::offset_t next_cu_offset = GetNextCompileUnitOffset();
56 
57   DWARFDebugInfoEntry die;
58   // Keep a flat array of the DIE for binary lookup by DIE offset
59   if (!cu_die_only) {
60     Log *log(
61         LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | DWARF_LOG_LOOKUPS));
62     if (log) {
63       m_dwarf->GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
64           log,
65           "DWARFUnit::ExtractDIEsIfNeeded () for compile unit at "
66           ".debug_info[0x%8.8x]",
67           GetOffset());
68     }
69   }
70 
71   uint32_t depth = 0;
72   // We are in our compile unit, parse starting at the offset we were told to
73   // parse
74   const DWARFDataExtractor &data = GetData();
75   std::vector<uint32_t> die_index_stack;
76   die_index_stack.reserve(32);
77   die_index_stack.push_back(0);
78   bool prev_die_had_children = false;
79   DWARFFormValue::FixedFormSizes fixed_form_sizes =
80       DWARFFormValue::GetFixedFormSizesForAddressSize(GetAddressByteSize(),
81                                                       IsDWARF64());
82   while (offset < next_cu_offset &&
83          die.FastExtract(data, this, fixed_form_sizes, &offset)) {
84     //        if (log)
85     //            log->Printf("0x%8.8x: %*.*s%s%s",
86     //                        die.GetOffset(),
87     //                        depth * 2, depth * 2, "",
88     //                        DW_TAG_value_to_name (die.Tag()),
89     //                        die.HasChildren() ? " *" : "");
90 
91     const bool null_die = die.IsNULL();
92     if (depth == 0) {
93       if (initial_die_array_size == 0)
94         AddUnitDIE(die);
95       uint64_t base_addr = die.GetAttributeValueAsAddress(
96           m_dwarf, this, DW_AT_low_pc, LLDB_INVALID_ADDRESS);
97       if (base_addr == LLDB_INVALID_ADDRESS)
98         base_addr =
99             die.GetAttributeValueAsAddress(m_dwarf, this, DW_AT_entry_pc, 0);
100       SetBaseAddress(base_addr);
101       if (cu_die_only)
102         return 1;
103     } else {
104       if (null_die) {
105         if (prev_die_had_children) {
106           // This will only happen if a DIE says is has children but all it
107           // contains is a NULL tag. Since we are removing the NULL DIEs from
108           // the list (saves up to 25% in C++ code), we need a way to let the
109           // DIE know that it actually doesn't have children.
110           if (!m_die_array.empty())
111             m_die_array.back().SetEmptyChildren(true);
112         }
113       } else {
114         die.SetParentIndex(m_die_array.size() - die_index_stack[depth - 1]);
115 
116         if (die_index_stack.back())
117           m_die_array[die_index_stack.back()].SetSiblingIndex(
118               m_die_array.size() - die_index_stack.back());
119 
120         // Only push the DIE if it isn't a NULL DIE
121         m_die_array.push_back(die);
122       }
123     }
124 
125     if (null_die) {
126       // NULL DIE.
127       if (!die_index_stack.empty())
128         die_index_stack.pop_back();
129 
130       if (depth > 0)
131         --depth;
132       if (depth == 0)
133         break; // We are done with this compile unit!
134 
135       prev_die_had_children = false;
136     } else {
137       die_index_stack.back() = m_die_array.size() - 1;
138       // Normal DIE
139       const bool die_has_children = die.HasChildren();
140       if (die_has_children) {
141         die_index_stack.push_back(0);
142         ++depth;
143       }
144       prev_die_had_children = die_has_children;
145     }
146   }
147 
148   // Give a little bit of info if we encounter corrupt DWARF (our offset should
149   // always terminate at or before the start of the next compilation unit
150   // header).
151   if (offset > next_cu_offset) {
152     m_dwarf->GetObjectFile()->GetModule()->ReportWarning(
153         "DWARF compile unit extends beyond its bounds cu 0x%8.8x at "
154         "0x%8.8" PRIx64 "\n",
155         GetOffset(), offset);
156   }
157 
158   // Since std::vector objects will double their size, we really need to make a
159   // new array with the perfect size so we don't end up wasting space. So here
160   // we copy and swap to make sure we don't have any extra memory taken up.
161 
162   if (m_die_array.size() < m_die_array.capacity()) {
163     DWARFDebugInfoEntry::collection exact_size_die_array(m_die_array.begin(),
164                                                          m_die_array.end());
165     exact_size_die_array.swap(m_die_array);
166   }
167   Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
168   if (log && log->GetVerbose()) {
169     StreamString strm;
170     Dump(&strm);
171     if (m_die_array.empty())
172       strm.Printf("error: no DIE for compile unit");
173     else
174       m_die_array[0].Dump(m_dwarf, this, strm, UINT32_MAX);
175     log->PutString(strm.GetString());
176   }
177 
178   if (!m_dwo_symbol_file)
179     return m_die_array.size();
180 
181   DWARFUnit *dwo_cu = m_dwo_symbol_file->GetCompileUnit();
182   size_t dwo_die_count = dwo_cu->ExtractDIEsIfNeeded(cu_die_only);
183   return m_die_array.size() + dwo_die_count -
184          1; // We have 2 CU die, but we want to count it only as one
185 }
186 
187 void DWARFUnit::AddUnitDIE(DWARFDebugInfoEntry &die) {
188   assert(m_die_array.empty() && "Compile unit DIE already added");
189 
190   // The average bytes per DIE entry has been seen to be around 14-20 so lets
191   // pre-reserve half of that since we are now stripping the NULL tags.
192 
193   // Only reserve the memory if we are adding children of the main compile unit
194   // DIE. The compile unit DIE is always the first entry, so if our size is 1,
195   // then we are adding the first compile unit child DIE and should reserve the
196   // memory.
197   m_die_array.reserve(GetDebugInfoSize() / 24);
198   m_die_array.push_back(die);
199 
200   const DWARFDebugInfoEntry &cu_die = m_die_array.front();
201   std::unique_ptr<SymbolFileDWARFDwo> dwo_symbol_file =
202       m_dwarf->GetDwoSymbolFileForCompileUnit(*this, cu_die);
203   if (!dwo_symbol_file)
204     return;
205 
206   DWARFUnit *dwo_cu = dwo_symbol_file->GetCompileUnit();
207   if (!dwo_cu)
208     return; // Can't fetch the compile unit from the dwo file.
209 
210   DWARFBaseDIE dwo_cu_die = dwo_cu->GetUnitDIEOnly();
211   if (!dwo_cu_die.IsValid())
212     return; // Can't fetch the compile unit DIE from the dwo file.
213 
214   uint64_t main_dwo_id =
215       cu_die.GetAttributeValueAsUnsigned(m_dwarf, this, DW_AT_GNU_dwo_id, 0);
216   uint64_t sub_dwo_id =
217       dwo_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_dwo_id, 0);
218   if (main_dwo_id != sub_dwo_id)
219     return; // The 2 dwo ID isn't match. Don't use the dwo file as it belongs to
220   // a differectn compilation.
221 
222   m_dwo_symbol_file = std::move(dwo_symbol_file);
223 
224   dw_addr_t addr_base =
225       cu_die.GetAttributeValueAsUnsigned(m_dwarf, this, DW_AT_GNU_addr_base, 0);
226   dw_addr_t ranges_base = cu_die.GetAttributeValueAsUnsigned(
227       m_dwarf, this, DW_AT_GNU_ranges_base, 0);
228   dwo_cu->SetAddrBase(addr_base, ranges_base, m_offset);
229 }
230 
231 DWARFDIE DWARFUnit::LookupAddress(const dw_addr_t address) {
232   if (DIE()) {
233     const DWARFDebugAranges &func_aranges = GetFunctionAranges();
234 
235     // Re-check the aranges auto pointer contents in case it was created above
236     if (!func_aranges.IsEmpty())
237       return GetDIE(func_aranges.FindAddress(address));
238   }
239   return DWARFDIE();
240 }
241 
242 size_t DWARFUnit::AppendDIEsWithTag(const dw_tag_t tag,
243 				    DWARFDIECollection &dies,
244 				    uint32_t depth) const {
245   size_t old_size = dies.Size();
246   DWARFDebugInfoEntry::const_iterator pos;
247   DWARFDebugInfoEntry::const_iterator end = m_die_array.end();
248   for (pos = m_die_array.begin(); pos != end; ++pos) {
249     if (pos->Tag() == tag)
250       dies.Append(DWARFDIE(this, &(*pos)));
251   }
252 
253   // Return the number of DIEs added to the collection
254   return dies.Size() - old_size;
255 }
256 
257 
258 lldb::user_id_t DWARFUnit::GetID() const {
259   dw_offset_t local_id =
260       m_base_obj_offset != DW_INVALID_OFFSET ? m_base_obj_offset : m_offset;
261   if (m_dwarf)
262     return DIERef(local_id, local_id).GetUID(m_dwarf);
263   else
264     return local_id;
265 }
266 
267 dw_offset_t DWARFUnit::GetNextCompileUnitOffset() const {
268   return m_offset + GetLengthByteSize() + GetLength();
269 }
270 
271 size_t DWARFUnit::GetDebugInfoSize() const {
272   return GetLengthByteSize() + GetLength() - GetHeaderByteSize();
273 }
274 
275 const DWARFAbbreviationDeclarationSet *DWARFUnit::GetAbbreviations() const {
276   return m_abbrevs;
277 }
278 
279 dw_offset_t DWARFUnit::GetAbbrevOffset() const {
280   return m_abbrevs ? m_abbrevs->GetOffset() : DW_INVALID_OFFSET;
281 }
282 
283 void DWARFUnit::SetAddrBase(dw_addr_t addr_base,
284                             dw_addr_t ranges_base,
285                             dw_offset_t base_obj_offset) {
286   m_addr_base = addr_base;
287   m_ranges_base = ranges_base;
288   m_base_obj_offset = base_obj_offset;
289 }
290 
291 void DWARFUnit::ClearDIEs() {
292   if (m_die_array.size() > 1) {
293     // std::vectors never get any smaller when resized to a smaller size, or
294     // when clear() or erase() are called, the size will report that it is
295     // smaller, but the memory allocated remains intact (call capacity() to see
296     // this). So we need to create a temporary vector and swap the contents
297     // which will cause just the internal pointers to be swapped so that when
298     // "tmp_array" goes out of scope, it will destroy the contents.
299 
300     // Save at least the compile unit DIE
301     DWARFDebugInfoEntry::collection tmp_array;
302     m_die_array.swap(tmp_array);
303     m_die_array.push_back(tmp_array.front());
304   }
305 
306   if (m_dwo_symbol_file)
307     m_dwo_symbol_file->GetCompileUnit()->ClearDIEs();
308 }
309 
310 void DWARFUnit::BuildAddressRangeTable(SymbolFileDWARF *dwarf,
311                                        DWARFDebugAranges *debug_aranges) {
312   // This function is usually called if there in no .debug_aranges section in
313   // order to produce a compile unit level set of address ranges that is
314   // accurate.
315 
316   size_t num_debug_aranges = debug_aranges->GetNumRanges();
317 
318   // First get the compile unit DIE only and check if it has a DW_AT_ranges
319   const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
320 
321   const dw_offset_t cu_offset = GetOffset();
322   if (die) {
323     DWARFRangeList ranges;
324     const size_t num_ranges =
325         die->GetAttributeAddressRanges(dwarf, this, ranges, false);
326     if (num_ranges > 0) {
327       // This compile unit has DW_AT_ranges, assume this is correct if it is
328       // present since clang no longer makes .debug_aranges by default and it
329       // emits DW_AT_ranges for DW_TAG_compile_units. GCC also does this with
330       // recent GCC builds.
331       for (size_t i = 0; i < num_ranges; ++i) {
332         const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
333         debug_aranges->AppendRange(cu_offset, range.GetRangeBase(),
334                                    range.GetRangeEnd());
335       }
336 
337       return; // We got all of our ranges from the DW_AT_ranges attribute
338     }
339   }
340   // We don't have a DW_AT_ranges attribute, so we need to parse the DWARF
341 
342   // If the DIEs weren't parsed, then we don't want all dies for all compile
343   // units to stay loaded when they weren't needed. So we can end up parsing
344   // the DWARF and then throwing them all away to keep memory usage down.
345   const bool clear_dies = ExtractDIEsIfNeeded(false) > 1;
346 
347   die = DIEPtr();
348   if (die)
349     die->BuildAddressRangeTable(dwarf, this, debug_aranges);
350 
351   if (debug_aranges->GetNumRanges() == num_debug_aranges) {
352     // We got nothing from the functions, maybe we have a line tables only
353     // situation. Check the line tables and build the arange table from this.
354     SymbolContext sc;
355     sc.comp_unit = dwarf->GetCompUnitForDWARFCompUnit(this);
356     if (sc.comp_unit) {
357       SymbolFileDWARFDebugMap *debug_map_sym_file =
358           m_dwarf->GetDebugMapSymfile();
359       if (debug_map_sym_file == NULL) {
360         LineTable *line_table = sc.comp_unit->GetLineTable();
361 
362         if (line_table) {
363           LineTable::FileAddressRanges file_ranges;
364           const bool append = true;
365           const size_t num_ranges =
366               line_table->GetContiguousFileAddressRanges(file_ranges, append);
367           for (uint32_t idx = 0; idx < num_ranges; ++idx) {
368             const LineTable::FileAddressRanges::Entry &range =
369                 file_ranges.GetEntryRef(idx);
370             debug_aranges->AppendRange(cu_offset, range.GetRangeBase(),
371                                        range.GetRangeEnd());
372           }
373         }
374       } else
375         debug_map_sym_file->AddOSOARanges(dwarf, debug_aranges);
376     }
377   }
378 
379   if (debug_aranges->GetNumRanges() == num_debug_aranges) {
380     // We got nothing from the functions, maybe we have a line tables only
381     // situation. Check the line tables and build the arange table from this.
382     SymbolContext sc;
383     sc.comp_unit = dwarf->GetCompUnitForDWARFCompUnit(this);
384     if (sc.comp_unit) {
385       LineTable *line_table = sc.comp_unit->GetLineTable();
386 
387       if (line_table) {
388         LineTable::FileAddressRanges file_ranges;
389         const bool append = true;
390         const size_t num_ranges =
391             line_table->GetContiguousFileAddressRanges(file_ranges, append);
392         for (uint32_t idx = 0; idx < num_ranges; ++idx) {
393           const LineTable::FileAddressRanges::Entry &range =
394               file_ranges.GetEntryRef(idx);
395           debug_aranges->AppendRange(GetOffset(), range.GetRangeBase(),
396                                      range.GetRangeEnd());
397         }
398       }
399     }
400   }
401 
402   // Keep memory down by clearing DIEs if this generate function caused them to
403   // be parsed
404   if (clear_dies)
405     ClearDIEs();
406 }
407 
408 lldb::ByteOrder DWARFUnit::GetByteOrder() const {
409   return m_dwarf->GetObjectFile()->GetByteOrder();
410 }
411 
412 TypeSystem *DWARFUnit::GetTypeSystem() {
413   if (m_dwarf)
414     return m_dwarf->GetTypeSystemForLanguage(GetLanguageType());
415   else
416     return nullptr;
417 }
418 
419 DWARFFormValue::FixedFormSizes DWARFUnit::GetFixedFormSizes() {
420   return DWARFFormValue::GetFixedFormSizesForAddressSize(GetAddressByteSize(),
421                                                          IsDWARF64());
422 }
423 
424 void DWARFUnit::SetBaseAddress(dw_addr_t base_addr) { m_base_addr = base_addr; }
425 
426 bool DWARFUnit::HasDIEsParsed() const { return m_die_array.size() > 1; }
427 
428 //----------------------------------------------------------------------
429 // Compare function DWARFDebugAranges::Range structures
430 //----------------------------------------------------------------------
431 static bool CompareDIEOffset(const DWARFDebugInfoEntry &die,
432                              const dw_offset_t die_offset) {
433   return die.GetOffset() < die_offset;
434 }
435 
436 //----------------------------------------------------------------------
437 // GetDIE()
438 //
439 // Get the DIE (Debug Information Entry) with the specified offset by first
440 // checking if the DIE is contained within this compile unit and grabbing the
441 // DIE from this compile unit. Otherwise we grab the DIE from the DWARF file.
442 //----------------------------------------------------------------------
443 DWARFDIE
444 DWARFUnit::GetDIE(dw_offset_t die_offset) {
445   if (die_offset != DW_INVALID_OFFSET) {
446     if (GetDwoSymbolFile())
447       return GetDwoSymbolFile()->GetCompileUnit()->GetDIE(die_offset);
448 
449     if (ContainsDIEOffset(die_offset)) {
450       ExtractDIEsIfNeeded(false);
451       DWARFDebugInfoEntry::const_iterator end = m_die_array.cend();
452       DWARFDebugInfoEntry::const_iterator pos =
453           lower_bound(m_die_array.cbegin(), end, die_offset, CompareDIEOffset);
454       if (pos != end) {
455         if (die_offset == (*pos).GetOffset())
456           return DWARFDIE(this, &(*pos));
457       }
458     } else {
459       // Don't specify the compile unit offset as we don't know it because the
460       // DIE belongs to
461       // a different compile unit in the same symbol file.
462       return m_dwarf->DebugInfo()->GetDIEForDIEOffset(die_offset);
463     }
464   }
465   return DWARFDIE(); // Not found
466 }
467 
468 uint8_t DWARFUnit::GetAddressByteSize(const DWARFUnit *cu) {
469   if (cu)
470     return cu->GetAddressByteSize();
471   return DWARFUnit::GetDefaultAddressSize();
472 }
473 
474 bool DWARFUnit::IsDWARF64(const DWARFUnit *cu) {
475   if (cu)
476     return cu->IsDWARF64();
477   return false;
478 }
479 
480 uint8_t DWARFUnit::GetDefaultAddressSize() { return 4; }
481 
482 void *DWARFUnit::GetUserData() const { return m_user_data; }
483 
484 void DWARFUnit::SetUserData(void *d) {
485   m_user_data = d;
486   if (m_dwo_symbol_file)
487     m_dwo_symbol_file->GetCompileUnit()->SetUserData(d);
488 }
489 
490 bool DWARFUnit::Supports_DW_AT_APPLE_objc_complete_type() {
491   if (GetProducer() == eProducerLLVMGCC)
492     return false;
493   return true;
494 }
495 
496 bool DWARFUnit::DW_AT_decl_file_attributes_are_invalid() {
497   // llvm-gcc makes completely invalid decl file attributes and won't ever be
498   // fixed, so we need to know to ignore these.
499   return GetProducer() == eProducerLLVMGCC;
500 }
501 
502 bool DWARFUnit::Supports_unnamed_objc_bitfields() {
503   if (GetProducer() == eProducerClang) {
504     const uint32_t major_version = GetProducerVersionMajor();
505     if (major_version > 425 ||
506         (major_version == 425 && GetProducerVersionUpdate() >= 13))
507       return true;
508     else
509       return false;
510   }
511   return true; // Assume all other compilers didn't have incorrect ObjC bitfield
512                // info
513 }
514 
515 SymbolFileDWARF *DWARFUnit::GetSymbolFileDWARF() const { return m_dwarf; }
516 
517 void DWARFUnit::ParseProducerInfo() {
518   m_producer_version_major = UINT32_MAX;
519   m_producer_version_minor = UINT32_MAX;
520   m_producer_version_update = UINT32_MAX;
521 
522   const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
523   if (die) {
524 
525     const char *producer_cstr =
526         die->GetAttributeValueAsString(m_dwarf, this, DW_AT_producer, NULL);
527     if (producer_cstr) {
528       RegularExpression llvm_gcc_regex(
529           llvm::StringRef("^4\\.[012]\\.[01] \\(Based on Apple "
530                           "Inc\\. build [0-9]+\\) \\(LLVM build "
531                           "[\\.0-9]+\\)$"));
532       if (llvm_gcc_regex.Execute(llvm::StringRef(producer_cstr))) {
533         m_producer = eProducerLLVMGCC;
534       } else if (strstr(producer_cstr, "clang")) {
535         static RegularExpression g_clang_version_regex(
536             llvm::StringRef("clang-([0-9]+)\\.([0-9]+)\\.([0-9]+)"));
537         RegularExpression::Match regex_match(3);
538         if (g_clang_version_regex.Execute(llvm::StringRef(producer_cstr),
539                                           &regex_match)) {
540           std::string str;
541           if (regex_match.GetMatchAtIndex(producer_cstr, 1, str))
542             m_producer_version_major =
543                 StringConvert::ToUInt32(str.c_str(), UINT32_MAX, 10);
544           if (regex_match.GetMatchAtIndex(producer_cstr, 2, str))
545             m_producer_version_minor =
546                 StringConvert::ToUInt32(str.c_str(), UINT32_MAX, 10);
547           if (regex_match.GetMatchAtIndex(producer_cstr, 3, str))
548             m_producer_version_update =
549                 StringConvert::ToUInt32(str.c_str(), UINT32_MAX, 10);
550         }
551         m_producer = eProducerClang;
552       } else if (strstr(producer_cstr, "GNU"))
553         m_producer = eProducerGCC;
554     }
555   }
556   if (m_producer == eProducerInvalid)
557     m_producer = eProcucerOther;
558 }
559 
560 DWARFProducer DWARFUnit::GetProducer() {
561   if (m_producer == eProducerInvalid)
562     ParseProducerInfo();
563   return m_producer;
564 }
565 
566 uint32_t DWARFUnit::GetProducerVersionMajor() {
567   if (m_producer_version_major == 0)
568     ParseProducerInfo();
569   return m_producer_version_major;
570 }
571 
572 uint32_t DWARFUnit::GetProducerVersionMinor() {
573   if (m_producer_version_minor == 0)
574     ParseProducerInfo();
575   return m_producer_version_minor;
576 }
577 
578 uint32_t DWARFUnit::GetProducerVersionUpdate() {
579   if (m_producer_version_update == 0)
580     ParseProducerInfo();
581   return m_producer_version_update;
582 }
583 LanguageType DWARFUnit::LanguageTypeFromDWARF(uint64_t val) {
584   // Note: user languages between lo_user and hi_user must be handled
585   // explicitly here.
586   switch (val) {
587   case DW_LANG_Mips_Assembler:
588     return eLanguageTypeMipsAssembler;
589   case DW_LANG_GOOGLE_RenderScript:
590     return eLanguageTypeExtRenderScript;
591   default:
592     return static_cast<LanguageType>(val);
593   }
594 }
595 
596 LanguageType DWARFUnit::GetLanguageType() {
597   if (m_language_type != eLanguageTypeUnknown)
598     return m_language_type;
599 
600   const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
601   if (die)
602     m_language_type = LanguageTypeFromDWARF(
603         die->GetAttributeValueAsUnsigned(m_dwarf, this, DW_AT_language, 0));
604   return m_language_type;
605 }
606 
607 bool DWARFUnit::GetIsOptimized() {
608   if (m_is_optimized == eLazyBoolCalculate) {
609     const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
610     if (die) {
611       m_is_optimized = eLazyBoolNo;
612       if (die->GetAttributeValueAsUnsigned(m_dwarf, this, DW_AT_APPLE_optimized,
613                                            0) == 1) {
614         m_is_optimized = eLazyBoolYes;
615       }
616     }
617   }
618   return m_is_optimized == eLazyBoolYes;
619 }
620 
621 SymbolFileDWARFDwo *DWARFUnit::GetDwoSymbolFile() const {
622   return m_dwo_symbol_file.get();
623 }
624 
625 dw_offset_t DWARFUnit::GetBaseObjOffset() const { return m_base_obj_offset; }
626 
627 const DWARFDebugAranges &DWARFUnit::GetFunctionAranges() {
628   if (m_func_aranges_ap.get() == NULL) {
629     m_func_aranges_ap.reset(new DWARFDebugAranges());
630     Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_ARANGES));
631 
632     if (log) {
633       m_dwarf->GetObjectFile()->GetModule()->LogMessage(
634           log,
635           "DWARFUnit::GetFunctionAranges() for compile unit at "
636           ".debug_info[0x%8.8x]",
637           GetOffset());
638     }
639     const DWARFDebugInfoEntry *die = DIEPtr();
640     if (die)
641       die->BuildFunctionAddressRangeTable(m_dwarf, this,
642                                           m_func_aranges_ap.get());
643 
644     if (m_dwo_symbol_file) {
645       DWARFUnit *dwo_cu = m_dwo_symbol_file->GetCompileUnit();
646       const DWARFDebugInfoEntry *dwo_die = dwo_cu->DIEPtr();
647       if (dwo_die)
648         dwo_die->BuildFunctionAddressRangeTable(m_dwo_symbol_file.get(), dwo_cu,
649                                                 m_func_aranges_ap.get());
650     }
651 
652     const bool minimize = false;
653     m_func_aranges_ap->Sort(minimize);
654   }
655   return *m_func_aranges_ap.get();
656 }
657 
658