1 //===-- LVELFReader.cpp ---------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This implements the LVELFReader class.
10 // It supports ELF and Mach-O formats.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/DebugInfo/LogicalView/Readers/LVELFReader.h"
15 #include "llvm/DebugInfo/DIContext.h"
16 #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
17 #include "llvm/DebugInfo/DWARF/DWARFExpression.h"
18 #include "llvm/DebugInfo/LogicalView/Core/LVLine.h"
19 #include "llvm/DebugInfo/LogicalView/Core/LVScope.h"
20 #include "llvm/DebugInfo/LogicalView/Core/LVSymbol.h"
21 #include "llvm/DebugInfo/LogicalView/Core/LVType.h"
22 #include "llvm/Object/Error.h"
23 #include "llvm/Object/MachO.h"
24 #include "llvm/Support/FormatVariadic.h"
25 
26 using namespace llvm;
27 using namespace llvm::object;
28 using namespace llvm::logicalview;
29 
30 #define DEBUG_TYPE "ElfReader"
31 
32 LVElement *LVELFReader::createElement(dwarf::Tag Tag) {
33   CurrentScope = nullptr;
34   CurrentSymbol = nullptr;
35   CurrentType = nullptr;
36   CurrentRanges.clear();
37 
38   if (!options().getPrintSymbols()) {
39     switch (Tag) {
40     // As the command line options did not specify a request to print
41     // logical symbols (--print=symbols or --print=all or --print=elements),
42     // skip its creation.
43     case dwarf::DW_TAG_formal_parameter:
44     case dwarf::DW_TAG_unspecified_parameters:
45     case dwarf::DW_TAG_member:
46     case dwarf::DW_TAG_variable:
47     case dwarf::DW_TAG_inheritance:
48     case dwarf::DW_TAG_constant:
49     case dwarf::DW_TAG_call_site_parameter:
50     case dwarf::DW_TAG_GNU_call_site_parameter:
51       return nullptr;
52     default:
53       break;
54     }
55   }
56 
57   switch (Tag) {
58   // Types.
59   case dwarf::DW_TAG_base_type:
60     CurrentType = createType();
61     CurrentType->setIsBase();
62     if (options().getAttributeBase())
63       CurrentType->setIncludeInPrint();
64     return CurrentType;
65   case dwarf::DW_TAG_const_type:
66     CurrentType = createType();
67     CurrentType->setIsConst();
68     CurrentType->setName("const");
69     return CurrentType;
70   case dwarf::DW_TAG_enumerator:
71     CurrentType = createTypeEnumerator();
72     return CurrentType;
73   case dwarf::DW_TAG_imported_declaration:
74     CurrentType = createTypeImport();
75     CurrentType->setIsImportDeclaration();
76     return CurrentType;
77   case dwarf::DW_TAG_imported_module:
78     CurrentType = createTypeImport();
79     CurrentType->setIsImportModule();
80     return CurrentType;
81   case dwarf::DW_TAG_pointer_type:
82     CurrentType = createType();
83     CurrentType->setIsPointer();
84     CurrentType->setName("*");
85     return CurrentType;
86   case dwarf::DW_TAG_ptr_to_member_type:
87     CurrentType = createType();
88     CurrentType->setIsPointerMember();
89     CurrentType->setName("*");
90     return CurrentType;
91   case dwarf::DW_TAG_reference_type:
92     CurrentType = createType();
93     CurrentType->setIsReference();
94     CurrentType->setName("&");
95     return CurrentType;
96   case dwarf::DW_TAG_restrict_type:
97     CurrentType = createType();
98     CurrentType->setIsRestrict();
99     CurrentType->setName("restrict");
100     return CurrentType;
101   case dwarf::DW_TAG_rvalue_reference_type:
102     CurrentType = createType();
103     CurrentType->setIsRvalueReference();
104     CurrentType->setName("&&");
105     return CurrentType;
106   case dwarf::DW_TAG_subrange_type:
107     CurrentType = createTypeSubrange();
108     return CurrentType;
109   case dwarf::DW_TAG_template_value_parameter:
110     CurrentType = createTypeParam();
111     CurrentType->setIsTemplateValueParam();
112     return CurrentType;
113   case dwarf::DW_TAG_template_type_parameter:
114     CurrentType = createTypeParam();
115     CurrentType->setIsTemplateTypeParam();
116     return CurrentType;
117   case dwarf::DW_TAG_GNU_template_template_param:
118     CurrentType = createTypeParam();
119     CurrentType->setIsTemplateTemplateParam();
120     return CurrentType;
121   case dwarf::DW_TAG_typedef:
122     CurrentType = createTypeDefinition();
123     return CurrentType;
124   case dwarf::DW_TAG_unspecified_type:
125     CurrentType = createType();
126     CurrentType->setIsUnspecified();
127     return CurrentType;
128   case dwarf::DW_TAG_volatile_type:
129     CurrentType = createType();
130     CurrentType->setIsVolatile();
131     CurrentType->setName("volatile");
132     return CurrentType;
133 
134   // Symbols.
135   case dwarf::DW_TAG_formal_parameter:
136     CurrentSymbol = createSymbol();
137     CurrentSymbol->setIsParameter();
138     return CurrentSymbol;
139   case dwarf::DW_TAG_unspecified_parameters:
140     CurrentSymbol = createSymbol();
141     CurrentSymbol->setIsUnspecified();
142     CurrentSymbol->setName("...");
143     return CurrentSymbol;
144   case dwarf::DW_TAG_member:
145     CurrentSymbol = createSymbol();
146     CurrentSymbol->setIsMember();
147     return CurrentSymbol;
148   case dwarf::DW_TAG_variable:
149     CurrentSymbol = createSymbol();
150     CurrentSymbol->setIsVariable();
151     return CurrentSymbol;
152   case dwarf::DW_TAG_inheritance:
153     CurrentSymbol = createSymbol();
154     CurrentSymbol->setIsInheritance();
155     return CurrentSymbol;
156   case dwarf::DW_TAG_call_site_parameter:
157   case dwarf::DW_TAG_GNU_call_site_parameter:
158     CurrentSymbol = createSymbol();
159     CurrentSymbol->setIsCallSiteParameter();
160     return CurrentSymbol;
161   case dwarf::DW_TAG_constant:
162     CurrentSymbol = createSymbol();
163     CurrentSymbol->setIsConstant();
164     return CurrentSymbol;
165 
166   // Scopes.
167   case dwarf::DW_TAG_catch_block:
168     CurrentScope = createScope();
169     CurrentScope->setIsCatchBlock();
170     return CurrentScope;
171   case dwarf::DW_TAG_lexical_block:
172     CurrentScope = createScope();
173     CurrentScope->setIsLexicalBlock();
174     return CurrentScope;
175   case dwarf::DW_TAG_try_block:
176     CurrentScope = createScope();
177     CurrentScope->setIsTryBlock();
178     return CurrentScope;
179   case dwarf::DW_TAG_compile_unit:
180   case dwarf::DW_TAG_skeleton_unit:
181     CurrentScope = createScopeCompileUnit();
182     CompileUnit = static_cast<LVScopeCompileUnit *>(CurrentScope);
183     return CurrentScope;
184   case dwarf::DW_TAG_inlined_subroutine:
185     CurrentScope = createScopeFunctionInlined();
186     return CurrentScope;
187   case dwarf::DW_TAG_namespace:
188     CurrentScope = createScopeNamespace();
189     return CurrentScope;
190   case dwarf::DW_TAG_template_alias:
191     CurrentScope = createScopeAlias();
192     return CurrentScope;
193   case dwarf::DW_TAG_array_type:
194     CurrentScope = createScopeArray();
195     return CurrentScope;
196   case dwarf::DW_TAG_call_site:
197   case dwarf::DW_TAG_GNU_call_site:
198     CurrentScope = createScopeFunction();
199     CurrentScope->setIsCallSite();
200     return CurrentScope;
201   case dwarf::DW_TAG_entry_point:
202     CurrentScope = createScopeFunction();
203     CurrentScope->setIsEntryPoint();
204     return CurrentScope;
205   case dwarf::DW_TAG_subprogram:
206     CurrentScope = createScopeFunction();
207     CurrentScope->setIsSubprogram();
208     return CurrentScope;
209   case dwarf::DW_TAG_subroutine_type:
210     CurrentScope = createScopeFunctionType();
211     return CurrentScope;
212   case dwarf::DW_TAG_label:
213     CurrentScope = createScopeFunction();
214     CurrentScope->setIsLabel();
215     return CurrentScope;
216   case dwarf::DW_TAG_class_type:
217     CurrentScope = createScopeAggregate();
218     CurrentScope->setIsClass();
219     return CurrentScope;
220   case dwarf::DW_TAG_structure_type:
221     CurrentScope = createScopeAggregate();
222     CurrentScope->setIsStructure();
223     return CurrentScope;
224   case dwarf::DW_TAG_union_type:
225     CurrentScope = createScopeAggregate();
226     CurrentScope->setIsUnion();
227     return CurrentScope;
228   case dwarf::DW_TAG_enumeration_type:
229     CurrentScope = createScopeEnumeration();
230     return CurrentScope;
231   case dwarf::DW_TAG_GNU_formal_parameter_pack:
232     CurrentScope = createScopeFormalPack();
233     return CurrentScope;
234   case dwarf::DW_TAG_GNU_template_parameter_pack:
235     CurrentScope = createScopeTemplatePack();
236     return CurrentScope;
237   default:
238     // Collect TAGs not implemented.
239     if (options().getInternalTag() && Tag)
240       CompileUnit->addDebugTag(Tag, CurrentOffset);
241     break;
242   }
243   return nullptr;
244 }
245 
246 void LVELFReader::processOneAttribute(const DWARFDie &Die, LVOffset *OffsetPtr,
247                                       const AttributeSpec &AttrSpec) {
248   uint64_t OffsetOnEntry = *OffsetPtr;
249   DWARFUnit *U = Die.getDwarfUnit();
250   const DWARFFormValue &FormValue =
251       DWARFFormValue::createFromUnit(AttrSpec.Form, U, OffsetPtr);
252 
253   // We are processing .debug_info section, implicit_const attribute
254   // values are not really stored here, but in .debug_abbrev section.
255   auto GetAsUnsignedConstant = [&]() -> int64_t {
256     return AttrSpec.isImplicitConst() ? AttrSpec.getImplicitConstValue()
257                                       : *FormValue.getAsUnsignedConstant();
258   };
259 
260   auto GetFlag = [](const DWARFFormValue &FormValue) -> bool {
261     return FormValue.isFormClass(DWARFFormValue::FC_Flag);
262   };
263 
264   auto GetBoundValue = [](const DWARFFormValue &FormValue) -> int64_t {
265     switch (FormValue.getForm()) {
266     case dwarf::DW_FORM_ref_addr:
267     case dwarf::DW_FORM_ref1:
268     case dwarf::DW_FORM_ref2:
269     case dwarf::DW_FORM_ref4:
270     case dwarf::DW_FORM_ref8:
271     case dwarf::DW_FORM_ref_udata:
272     case dwarf::DW_FORM_ref_sig8:
273       return *FormValue.getAsReferenceUVal();
274     case dwarf::DW_FORM_data1:
275     case dwarf::DW_FORM_flag:
276     case dwarf::DW_FORM_data2:
277     case dwarf::DW_FORM_data4:
278     case dwarf::DW_FORM_data8:
279     case dwarf::DW_FORM_udata:
280     case dwarf::DW_FORM_ref_sup4:
281     case dwarf::DW_FORM_ref_sup8:
282       return *FormValue.getAsUnsignedConstant();
283     case dwarf::DW_FORM_sdata:
284       return *FormValue.getAsSignedConstant();
285     default:
286       return 0;
287     }
288   };
289 
290   LLVM_DEBUG({
291     dbgs() << "     " << hexValue(OffsetOnEntry)
292            << formatv(" {0}", AttrSpec.Attr) << "\n";
293   });
294 
295   switch (AttrSpec.Attr) {
296   case dwarf::DW_AT_accessibility:
297     CurrentElement->setAccessibilityCode(*FormValue.getAsUnsignedConstant());
298     break;
299   case dwarf::DW_AT_artificial:
300     CurrentElement->setIsArtificial();
301     break;
302   case dwarf::DW_AT_bit_size:
303     CurrentElement->setBitSize(*FormValue.getAsUnsignedConstant());
304     break;
305   case dwarf::DW_AT_call_file:
306     CurrentElement->setCallFilenameIndex(GetAsUnsignedConstant());
307     break;
308   case dwarf::DW_AT_call_line:
309     CurrentElement->setCallLineNumber(IncrementFileIndex
310                                           ? GetAsUnsignedConstant() + 1
311                                           : GetAsUnsignedConstant());
312     break;
313   case dwarf::DW_AT_comp_dir:
314     CompileUnit->setCompilationDirectory(dwarf::toStringRef(FormValue));
315     break;
316   case dwarf::DW_AT_const_value:
317     if (FormValue.isFormClass(DWARFFormValue::FC_Block)) {
318       ArrayRef<uint8_t> Expr = *FormValue.getAsBlock();
319       // Store the expression as a hexadecimal string.
320       CurrentElement->setValue(
321           llvm::toHex(llvm::toStringRef(Expr), /*LowerCase=*/true));
322     } else if (FormValue.isFormClass(DWARFFormValue::FC_Constant)) {
323       // In the case of negative values, generate the string representation
324       // for a positive value prefixed with the negative sign.
325       if (FormValue.getForm() == dwarf::DW_FORM_sdata) {
326         std::stringstream Stream;
327         int64_t Value = *FormValue.getAsSignedConstant();
328         if (Value < 0) {
329           Stream << "-";
330           Value = std::abs(Value);
331         }
332         Stream << hexString(Value, 2);
333         CurrentElement->setValue(Stream.str());
334       } else
335         CurrentElement->setValue(
336             hexString(*FormValue.getAsUnsignedConstant(), 2));
337     } else
338       CurrentElement->setValue(dwarf::toStringRef(FormValue));
339     break;
340   case dwarf::DW_AT_count:
341     CurrentElement->setCount(*FormValue.getAsUnsignedConstant());
342     break;
343   case dwarf::DW_AT_decl_line:
344     CurrentElement->setLineNumber(GetAsUnsignedConstant());
345     break;
346   case dwarf::DW_AT_decl_file:
347     CurrentElement->setFilenameIndex(IncrementFileIndex
348                                          ? GetAsUnsignedConstant() + 1
349                                          : GetAsUnsignedConstant());
350     break;
351   case dwarf::DW_AT_enum_class:
352     if (GetFlag(FormValue))
353       CurrentElement->setIsEnumClass();
354     break;
355   case dwarf::DW_AT_external:
356     if (GetFlag(FormValue))
357       CurrentElement->setIsExternal();
358     break;
359   case dwarf::DW_AT_GNU_discriminator:
360     CurrentElement->setDiscriminator(*FormValue.getAsUnsignedConstant());
361     break;
362   case dwarf::DW_AT_inline:
363     CurrentElement->setInlineCode(*FormValue.getAsUnsignedConstant());
364     break;
365   case dwarf::DW_AT_lower_bound:
366     CurrentElement->setLowerBound(GetBoundValue(FormValue));
367     break;
368   case dwarf::DW_AT_name:
369     CurrentElement->setName(dwarf::toStringRef(FormValue));
370     break;
371   case dwarf::DW_AT_linkage_name:
372   case dwarf::DW_AT_MIPS_linkage_name:
373     CurrentElement->setLinkageName(dwarf::toStringRef(FormValue));
374     break;
375   case dwarf::DW_AT_producer:
376     if (options().getAttributeProducer())
377       CurrentElement->setProducer(dwarf::toStringRef(FormValue));
378     break;
379   case dwarf::DW_AT_upper_bound:
380     CurrentElement->setUpperBound(GetBoundValue(FormValue));
381     break;
382   case dwarf::DW_AT_virtuality:
383     CurrentElement->setVirtualityCode(*FormValue.getAsUnsignedConstant());
384     break;
385 
386   case dwarf::DW_AT_abstract_origin:
387   case dwarf::DW_AT_call_origin:
388   case dwarf::DW_AT_extension:
389   case dwarf::DW_AT_import:
390   case dwarf::DW_AT_specification:
391   case dwarf::DW_AT_type:
392     updateReference(AttrSpec.Attr, FormValue);
393     break;
394 
395   case dwarf::DW_AT_low_pc:
396     if (options().getGeneralCollectRanges()) {
397       FoundLowPC = true;
398       // For toolchains that support the removal of unused code, the linker
399       // marks functions that have been removed, by setting the value for the
400       // low_pc to the max address.
401       if (std::optional<uint64_t> Value = FormValue.getAsAddress()) {
402         CurrentLowPC = *Value;
403       } else {
404         uint64_t UValue = FormValue.getRawUValue();
405         if (U->getAddrOffsetSectionItem(UValue)) {
406           CurrentLowPC = *FormValue.getAsAddress();
407         } else {
408           FoundLowPC = false;
409           // We are dealing with an index into the .debug_addr section.
410           LLVM_DEBUG({
411             dbgs() << format("indexed (%8.8x) address = ", (uint32_t)UValue);
412           });
413         }
414       }
415       if (FoundLowPC) {
416         if (CurrentLowPC == MaxAddress)
417           CurrentElement->setIsDiscarded();
418         if (CurrentElement->isCompileUnit())
419           setCUBaseAddress(CurrentLowPC);
420       }
421     }
422     break;
423 
424   case dwarf::DW_AT_high_pc:
425     if (options().getGeneralCollectRanges()) {
426       FoundHighPC = true;
427       if (std::optional<uint64_t> Address = FormValue.getAsAddress())
428         // High PC is an address.
429         CurrentHighPC = *Address;
430       if (std::optional<uint64_t> Offset = FormValue.getAsUnsignedConstant())
431         // High PC is an offset from LowPC.
432         CurrentHighPC = CurrentLowPC + *Offset;
433       // Store the real upper limit for the address range.
434       if (UpdateHighAddress && CurrentHighPC > 0)
435         --CurrentHighPC;
436       if (CurrentElement->isCompileUnit())
437         setCUHighAddress(CurrentHighPC);
438     }
439     break;
440 
441   case dwarf::DW_AT_ranges:
442     if (RangesDataAvailable && options().getGeneralCollectRanges()) {
443       auto GetRanges = [](const DWARFFormValue &FormValue,
444                           DWARFUnit *U) -> Expected<DWARFAddressRangesVector> {
445         if (FormValue.getForm() == dwarf::DW_FORM_rnglistx)
446           return U->findRnglistFromIndex(*FormValue.getAsSectionOffset());
447         return U->findRnglistFromOffset(*FormValue.getAsSectionOffset());
448       };
449       Expected<DWARFAddressRangesVector> RangesOrError =
450           GetRanges(FormValue, U);
451       if (!RangesOrError) {
452         LLVM_DEBUG({
453           std::string TheError(toString(RangesOrError.takeError()));
454           dbgs() << format("error decoding address ranges = ",
455                            TheError.c_str());
456         });
457         consumeError(RangesOrError.takeError());
458         break;
459       }
460       // The address ranges are absolute. There is no need to add any addend.
461       DWARFAddressRangesVector Ranges = RangesOrError.get();
462       for (DWARFAddressRange &Range : Ranges) {
463         // This seems to be a tombstone for empty ranges.
464         if (Range.LowPC == Range.HighPC)
465           continue;
466         // Store the real upper limit for the address range.
467         if (UpdateHighAddress && Range.HighPC > 0)
468           --Range.HighPC;
469         // Add the pair of addresses.
470         CurrentScope->addObject(Range.LowPC, Range.HighPC);
471         // If the scope is the CU, do not update the ranges set.
472         if (!CurrentElement->isCompileUnit())
473           CurrentRanges.emplace_back(Range.LowPC, Range.HighPC);
474       }
475     }
476     break;
477 
478   // Get the location list for the symbol.
479   case dwarf::DW_AT_data_member_location:
480     if (options().getAttributeAnyLocation())
481       processLocationMember(AttrSpec.Attr, FormValue, Die, OffsetOnEntry);
482     break;
483 
484   // Get the location list for the symbol.
485   case dwarf::DW_AT_location:
486   case dwarf::DW_AT_string_length:
487   case dwarf::DW_AT_use_location:
488     if (options().getAttributeAnyLocation() && CurrentSymbol)
489       processLocationList(AttrSpec.Attr, FormValue, Die, OffsetOnEntry);
490     break;
491 
492   case dwarf::DW_AT_call_data_value:
493   case dwarf::DW_AT_call_value:
494   case dwarf::DW_AT_GNU_call_site_data_value:
495   case dwarf::DW_AT_GNU_call_site_value:
496     if (options().getAttributeAnyLocation() && CurrentSymbol)
497       processLocationList(AttrSpec.Attr, FormValue, Die, OffsetOnEntry,
498                           /*CallSiteLocation=*/true);
499     break;
500 
501   default:
502     break;
503   }
504 }
505 
506 LVScope *LVELFReader::processOneDie(const DWARFDie &InputDIE, LVScope *Parent,
507                                     DWARFDie &SkeletonDie) {
508   // If the input DIE corresponds to the compile unit, it can be:
509   // a) Simple DWARF: a standard DIE. Ignore the skeleton DIE (is empty).
510   // b) Split DWARF: the DIE for the split DWARF. The skeleton is the DIE
511   //    for the skeleton DWARF. Process both DIEs.
512   const DWARFDie &DIE = SkeletonDie.isValid() ? SkeletonDie : InputDIE;
513   DWARFDataExtractor DebugInfoData =
514       DIE.getDwarfUnit()->getDebugInfoExtractor();
515   LVOffset Offset = DIE.getOffset();
516 
517   // Reset values for the current DIE.
518   CurrentLowPC = 0;
519   CurrentHighPC = 0;
520   CurrentOffset = Offset;
521   CurrentEndOffset = 0;
522   FoundLowPC = false;
523   FoundHighPC = false;
524 
525   // Process supported attributes.
526   if (DebugInfoData.isValidOffset(Offset)) {
527 
528     LLVM_DEBUG({
529       dbgs() << "DIE: " << hexValue(Offset) << formatv(" {0}", DIE.getTag())
530              << "\n";
531     });
532 
533     // Create the logical view element for the current DIE.
534     dwarf::Tag Tag = DIE.getTag();
535     CurrentElement = createElement(Tag);
536     if (!CurrentElement)
537       return CurrentScope;
538 
539     CurrentElement->setTag(Tag);
540     CurrentElement->setOffset(Offset);
541 
542     if (options().getAttributeAnySource() && CurrentElement->isCompileUnit())
543       addCompileUnitOffset(Offset,
544                            static_cast<LVScopeCompileUnit *>(CurrentElement));
545 
546     // Insert the newly created element into the element symbol table. If the
547     // element is in the list, it means there are previously created elements
548     // referencing this element.
549     if (ElementTable.find(Offset) == ElementTable.end()) {
550       // No previous references to this offset.
551       ElementTable.emplace(std::piecewise_construct,
552                            std::forward_as_tuple(Offset),
553                            std::forward_as_tuple(CurrentElement));
554     } else {
555       // There are previous references to this element. We need to update the
556       // element and all the references pointing to this element.
557       LVElementEntry &Reference = ElementTable[Offset];
558       Reference.Element = CurrentElement;
559       // Traverse the element set and update the elements (backtracking).
560       for (LVElement *Target : Reference.References)
561         Target->setReference(CurrentElement);
562       for (LVElement *Target : Reference.Types)
563         Target->setType(CurrentElement);
564       // Clear the pending elements.
565       Reference.References.clear();
566       Reference.Types.clear();
567     }
568 
569     // Add the current element to its parent as there are attributes
570     // (locations) that require the scope level.
571     if (CurrentScope)
572       Parent->addElement(CurrentScope);
573     else if (CurrentSymbol)
574       Parent->addElement(CurrentSymbol);
575     else if (CurrentType)
576       Parent->addElement(CurrentType);
577 
578     // Process the attributes for the given DIE.
579     auto ProcessAttributes = [&](const DWARFDie &TheDIE,
580                                  DWARFDataExtractor &DebugData) {
581       CurrentEndOffset = Offset;
582       uint32_t abbrCode = DebugData.getULEB128(&CurrentEndOffset);
583       if (abbrCode) {
584         if (const DWARFAbbreviationDeclaration *AbbrevDecl =
585                 TheDIE.getAbbreviationDeclarationPtr())
586           if (AbbrevDecl)
587             for (const DWARFAbbreviationDeclaration::AttributeSpec &AttrSpec :
588                  AbbrevDecl->attributes())
589               processOneAttribute(TheDIE, &CurrentEndOffset, AttrSpec);
590       }
591     };
592 
593     ProcessAttributes(DIE, DebugInfoData);
594 
595     // If the input DIE is for a compile unit, process its attributes in
596     // the case of split DWARF, to override any common attribute values.
597     if (SkeletonDie.isValid()) {
598       DWARFDataExtractor DebugInfoData =
599           InputDIE.getDwarfUnit()->getDebugInfoExtractor();
600       LVOffset Offset = InputDIE.getOffset();
601       if (DebugInfoData.isValidOffset(Offset))
602         ProcessAttributes(InputDIE, DebugInfoData);
603     }
604   }
605 
606   if (CurrentScope) {
607     if (CurrentScope->getCanHaveRanges()) {
608       // If the scope has ranges, they are already added to the scope.
609       // Add any collected LowPC/HighPC values.
610       bool IsCompileUnit = CurrentScope->getIsCompileUnit();
611       if (FoundLowPC && FoundHighPC) {
612         CurrentScope->addObject(CurrentLowPC, CurrentHighPC);
613         if (!IsCompileUnit) {
614           // If the scope is a function, add it to the public names.
615           if ((options().getAttributePublics() ||
616                options().getPrintAnyLine()) &&
617               CurrentScope->getIsFunction() &&
618               !CurrentScope->getIsInlinedFunction())
619             CompileUnit->addPublicName(CurrentScope, CurrentLowPC,
620                                        CurrentHighPC);
621         }
622       }
623 
624       // Look for scopes with ranges and no linkage name information that
625       // are referencing another scopes via DW_AT_specification. They are
626       // possible candidates for a comdat scope.
627       if (CurrentScope->getHasRanges() &&
628           !CurrentScope->getLinkageNameIndex() &&
629           CurrentScope->getHasReferenceSpecification()) {
630         // Get the linkage name in order to search for a possible comdat.
631         std::optional<DWARFFormValue> LinkageDIE =
632             DIE.findRecursively(dwarf::DW_AT_linkage_name);
633         if (LinkageDIE.has_value()) {
634           StringRef Name(dwarf::toStringRef(LinkageDIE));
635           if (!Name.empty())
636             CurrentScope->setLinkageName(Name);
637         }
638       }
639 
640       // If the current scope is in the 'LinkageNames' table, update its
641       // logical scope. For other scopes, always we will assume the default
642       // ".text" section index.
643       LVSectionIndex SectionIndex = updateSymbolTable(CurrentScope);
644       if (CurrentScope->getIsComdat())
645         CompileUnit->setHasComdatScopes();
646 
647       // Update section index contained ranges.
648       if (SectionIndex) {
649         if (!CurrentRanges.empty()) {
650           for (LVAddressRange &Range : CurrentRanges)
651             addSectionRange(SectionIndex, CurrentScope, Range.first,
652                             Range.second);
653           CurrentRanges.clear();
654         }
655         // If the scope is the CU, do not update the ranges set.
656         if (FoundLowPC && FoundHighPC && !IsCompileUnit) {
657           addSectionRange(SectionIndex, CurrentScope, CurrentLowPC,
658                           CurrentHighPC);
659         }
660       }
661     }
662     // Mark member functions.
663     if (Parent->getIsAggregate())
664       CurrentScope->setIsMember();
665   }
666 
667   // Keep track of symbols with locations.
668   if (options().getAttributeAnyLocation() && CurrentSymbol &&
669       CurrentSymbol->getHasLocation())
670     SymbolsWithLocations.push_back(CurrentSymbol);
671 
672   // If we have template parameters, mark the parent as template.
673   if (CurrentType && CurrentType->getIsTemplateParam())
674     Parent->setIsTemplate();
675 
676   return CurrentScope;
677 }
678 
679 void LVELFReader::traverseDieAndChildren(DWARFDie &DIE, LVScope *Parent,
680                                          DWARFDie &SkeletonDie) {
681   // Process the current DIE.
682   LVScope *Scope = processOneDie(DIE, Parent, SkeletonDie);
683   if (Scope) {
684     LVOffset Lower = DIE.getOffset();
685     LVOffset Upper = CurrentEndOffset;
686     DWARFDie DummyDie;
687     // Traverse the children chain.
688     DWARFDie Child = DIE.getFirstChild();
689     while (Child) {
690       traverseDieAndChildren(Child, Scope, DummyDie);
691       Upper = Child.getOffset();
692       Child = Child.getSibling();
693     }
694     // Calculate contributions to the debug info section.
695     if (options().getPrintSizes() && Upper)
696       CompileUnit->addSize(Scope, Lower, Upper);
697   }
698 }
699 
700 void LVELFReader::processLocationGaps() {
701   if (options().getAttributeAnyLocation())
702     for (LVSymbol *Symbol : SymbolsWithLocations)
703       Symbol->fillLocationGaps();
704 }
705 
706 void LVELFReader::createLineAndFileRecords(
707     const DWARFDebugLine::LineTable *Lines) {
708   if (!Lines)
709     return;
710 
711   // Get the source filenames.
712   if (!Lines->Prologue.FileNames.empty())
713     for (const DWARFDebugLine::FileNameEntry &Entry :
714          Lines->Prologue.FileNames) {
715       std::string Directory;
716       if (Lines->getDirectoryForEntry(Entry, Directory))
717         Directory = transformPath(Directory);
718       if (Directory.empty())
719         Directory = std::string(CompileUnit->getCompilationDirectory());
720       std::string File = transformPath(dwarf::toStringRef(Entry.Name));
721       std::string String;
722       raw_string_ostream(String) << Directory << "/" << File;
723       CompileUnit->addFilename(String);
724     }
725 
726   // In DWARF5 the file indexes start at 0;
727   bool IncrementIndex = Lines->Prologue.getVersion() >= 5;
728 
729   // Get the source lines if requested by command line option.
730   if (options().getPrintLines() && Lines->Rows.size())
731     for (const DWARFDebugLine::Row &Row : Lines->Rows) {
732       // Here we collect logical debug lines in CULines. Later on,
733       // the 'processLines()' function will move each created logical line
734       // to its enclosing logical scope, using the debug ranges information
735       // and they will be released when its scope parent is deleted.
736       LVLineDebug *Line = createLineDebug();
737       CULines.push_back(Line);
738       Line->setAddress(Row.Address.Address);
739       Line->setFilename(
740           CompileUnit->getFilename(IncrementIndex ? Row.File + 1 : Row.File));
741       Line->setLineNumber(Row.Line);
742       if (Row.Discriminator)
743         Line->setDiscriminator(Row.Discriminator);
744       if (Row.IsStmt)
745         Line->setIsNewStatement();
746       if (Row.BasicBlock)
747         Line->setIsBasicBlock();
748       if (Row.EndSequence)
749         Line->setIsEndSequence();
750       if (Row.EpilogueBegin)
751         Line->setIsEpilogueBegin();
752       if (Row.PrologueEnd)
753         Line->setIsPrologueEnd();
754       LLVM_DEBUG({
755         dbgs() << "Address: " << hexValue(Line->getAddress())
756                << " Line: " << Line->lineNumberAsString(/*ShowZero=*/true)
757                << "\n";
758       });
759     }
760 }
761 
762 std::string LVELFReader::getRegisterName(LVSmall Opcode,
763                                          ArrayRef<uint64_t> Operands) {
764   // The 'prettyPrintRegisterOp' function uses the DWARFUnit to support
765   // DW_OP_regval_type. At this point we are operating on a logical view
766   // item, with no access to the underlying DWARF data used by LLVM.
767   // We do not support DW_OP_regval_type here.
768   if (Opcode == dwarf::DW_OP_regval_type)
769     return {};
770 
771   std::string string;
772   raw_string_ostream Stream(string);
773   DIDumpOptions DumpOpts;
774   auto *MCRegInfo = MRI.get();
775   auto GetRegName = [&MCRegInfo](uint64_t DwarfRegNum, bool IsEH) -> StringRef {
776     if (!MCRegInfo)
777       return {};
778     if (std::optional<unsigned> LLVMRegNum =
779             MCRegInfo->getLLVMRegNum(DwarfRegNum, IsEH))
780       if (const char *RegName = MCRegInfo->getName(*LLVMRegNum))
781         return StringRef(RegName);
782     return {};
783   };
784   DumpOpts.GetNameForDWARFReg = GetRegName;
785   DWARFExpression::prettyPrintRegisterOp(/*U=*/nullptr, Stream, DumpOpts,
786                                          Opcode, Operands);
787   return Stream.str();
788 }
789 
790 Error LVELFReader::createScopes() {
791   LLVM_DEBUG({
792     W.startLine() << "\n";
793     W.printString("File", Obj.getFileName().str());
794     W.printString("Format", FileFormatName);
795   });
796 
797   if (Error Err = LVReader::createScopes())
798     return Err;
799 
800   // As the DwarfContext object is valid only during the scopes creation,
801   // we need to create our own Target information, to be used during the
802   // logical view printing, in the case of instructions being requested.
803   std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(Obj);
804   if (!DwarfContext)
805     return createStringError(errc::invalid_argument,
806                              "Could not create DWARF information: %s",
807                              getFilename().str().c_str());
808 
809   if (Error Err = loadTargetInfo(Obj))
810     return Err;
811 
812   // Create a mapping for virtual addresses.
813   mapVirtualAddress(Obj);
814 
815   // Select the correct compile unit range, depending if we are dealing with
816   // a standard or split DWARF object.
817   DWARFContext::compile_unit_range CompileUnits =
818       DwarfContext->getNumCompileUnits() ? DwarfContext->compile_units()
819                                          : DwarfContext->dwo_compile_units();
820   for (const std::unique_ptr<DWARFUnit> &CU : CompileUnits) {
821 
822     // Deduction of index used for the line records.
823     //
824     // For the following test case: test.cpp
825     //  void foo(void ParamPtr) { }
826 
827     // Both GCC and Clang generate DWARF-5 .debug_line layout.
828 
829     // * GCC (GNU C++17 11.3.0) - All DW_AT_decl_file use index 1.
830     //
831     //   .debug_info:
832     //     format = DWARF32, version = 0x0005
833     //     DW_TAG_compile_unit
834     //       DW_AT_name	("test.cpp")
835     //       DW_TAG_subprogram ("foo")
836     //         DW_AT_decl_file (1)
837     //         DW_TAG_formal_parameter ("ParamPtr")
838     //           DW_AT_decl_file (1)
839     //   .debug_line:
840     //     Line table prologue: format (DWARF32), version (5)
841     //     include_directories[0] = "..."
842     //     file_names[0]: name ("test.cpp"), dir_index (0)
843     //     file_names[1]: name ("test.cpp"), dir_index (0)
844 
845     // * Clang (14.0.6) - All DW_AT_decl_file use index 0.
846     //
847     //   .debug_info:
848     //     format = DWARF32, version = 0x0005
849     //     DW_AT_producer	("clang version 14.0.6")
850     //     DW_AT_name	("test.cpp")
851     //
852     //     DW_TAG_subprogram ("foo")
853     //       DW_AT_decl_file (0)
854     //       DW_TAG_formal_parameter ("ParamPtr")
855     //         DW_AT_decl_file (0)
856     //   .debug_line:
857     //     Line table prologue: format (DWARF32), version (5)
858     //     include_directories[0] = "..."
859     //     file_names[0]: name ("test.cpp"), dir_index (0)
860 
861     // From DWARFDebugLine::getFileNameByIndex documentation:
862     //   In Dwarf 4, the files are 1-indexed.
863     //   In Dwarf 5, the files are 0-indexed.
864     // Additional discussions here:
865     // https://www.mail-archive.com/dwarf-discuss@lists.dwarfstd.org/msg00883.html
866 
867     // The ELF Reader is expecting the files are 1-indexed, so using
868     // the .debug_line header information decide if the indexed require
869     // an internal adjustment.
870 
871     // For the case of GCC (DWARF5), if the entries[0] and [1] are the
872     // same, do not perform any adjustment.
873     auto DeduceIncrementFileIndex = [&]() -> bool {
874       if (CU->getVersion() < 5)
875         // DWARF-4 or earlier -> Don't increment index.
876         return false;
877 
878       if (const DWARFDebugLine::LineTable *LT =
879               CU->getContext().getLineTableForUnit(CU.get())) {
880         // Check if there are at least 2 entries and if they are the same.
881         if (LT->hasFileAtIndex(0) && LT->hasFileAtIndex(1)) {
882           const DWARFDebugLine::FileNameEntry &EntryZero =
883               LT->Prologue.getFileNameEntry(0);
884           const DWARFDebugLine::FileNameEntry &EntryOne =
885               LT->Prologue.getFileNameEntry(1);
886           // Check directory indexes.
887           if (EntryZero.DirIdx != EntryOne.DirIdx)
888             // DWARF-5 -> Increment index.
889             return true;
890           // Check filename.
891           std::string FileZero;
892           std::string FileOne;
893           StringRef None;
894           LT->getFileNameByIndex(
895               0, None, DILineInfoSpecifier::FileLineInfoKind::RawValue,
896               FileZero);
897           LT->getFileNameByIndex(
898               1, None, DILineInfoSpecifier::FileLineInfoKind::RawValue,
899               FileOne);
900           return FileZero.compare(FileOne);
901         }
902       }
903 
904       // DWARF-5 -> Increment index.
905       return true;
906     };
907     // The ELF reader expects the indexes as 1-indexed.
908     IncrementFileIndex = DeduceIncrementFileIndex();
909 
910     DWARFDie UnitDie = CU->getUnitDIE();
911     SmallString<16> DWOAlternativeLocation;
912     if (UnitDie) {
913       std::optional<const char *> DWOFileName =
914           CU->getVersion() >= 5
915               ? dwarf::toString(UnitDie.find(dwarf::DW_AT_dwo_name))
916               : dwarf::toString(UnitDie.find(dwarf::DW_AT_GNU_dwo_name));
917       StringRef From(DWOFileName.value_or(""));
918       DWOAlternativeLocation = createAlternativePath(From);
919     }
920 
921     // The current CU can be a normal compile unit (standard) or a skeleton
922     // compile unit (split). For both cases, the returned die, will be used
923     // to create the logical scopes.
924     DWARFDie CUDie = CU->getNonSkeletonUnitDIE(
925         /*ExtractUnitDIEOnly=*/false,
926         /*DWOAlternativeLocation=*/DWOAlternativeLocation);
927     if (!CUDie.isValid())
928       continue;
929 
930     // The current unit corresponds to the .dwo file. We need to get the
931     // skeleton unit and query for any ranges that will enclose any ranges
932     // in the non-skeleton unit.
933     DWARFDie DummyDie;
934     DWARFDie SkeletonDie =
935         CUDie.getDwarfUnit()->isDWOUnit() ? CU->getUnitDIE(false) : DummyDie;
936     // Disable the ranges processing if we have just a single .dwo object,
937     // as any DW_AT_ranges will access not available range information.
938     RangesDataAvailable =
939         (!CUDie.getDwarfUnit()->isDWOUnit() ||
940          (SkeletonDie.isValid() ? !SkeletonDie.getDwarfUnit()->isDWOUnit()
941                                 : true));
942 
943     traverseDieAndChildren(CUDie, Root, SkeletonDie);
944 
945     createLineAndFileRecords(DwarfContext->getLineTableForUnit(CU.get()));
946     if (Error Err = createInstructions())
947       return Err;
948 
949     // Process the compilation unit, as there are cases where enclosed
950     // functions have the same ranges values. Insert the compilation unit
951     // ranges at the end, to allow enclosing ranges to be first in the list.
952     LVSectionIndex SectionIndex = getSectionIndex(CompileUnit);
953     addSectionRange(SectionIndex, CompileUnit);
954     LVRange *ScopesWithRanges = getSectionRanges(SectionIndex);
955     ScopesWithRanges->sort();
956 
957     processLines(&CULines, SectionIndex);
958     processLocationGaps();
959 
960     // These are per compile unit.
961     ScopesWithRanges->clear();
962     SymbolsWithLocations.clear();
963     CULines.clear();
964   }
965 
966   return Error::success();
967 }
968 
969 // Get the location information for the associated attribute.
970 void LVELFReader::processLocationList(dwarf::Attribute Attr,
971                                       const DWARFFormValue &FormValue,
972                                       const DWARFDie &Die,
973                                       uint64_t OffsetOnEntry,
974                                       bool CallSiteLocation) {
975 
976   auto ProcessLocationExpression = [&](const DWARFExpression &Expression) {
977     for (const DWARFExpression::Operation &Op : Expression)
978       CurrentSymbol->addLocationOperands(Op.getCode(), Op.getRawOperands());
979   };
980 
981   DWARFUnit *U = Die.getDwarfUnit();
982   DWARFContext &DwarfContext = U->getContext();
983   bool IsLittleEndian = DwarfContext.isLittleEndian();
984   if (FormValue.isFormClass(DWARFFormValue::FC_Block) ||
985       (DWARFAttribute::mayHaveLocationExpr(Attr) &&
986        FormValue.isFormClass(DWARFFormValue::FC_Exprloc))) {
987     ArrayRef<uint8_t> Expr = *FormValue.getAsBlock();
988     DataExtractor Data(StringRef((const char *)Expr.data(), Expr.size()),
989                        IsLittleEndian, 0);
990     DWARFExpression Expression(Data, U->getAddressByteSize(),
991                                U->getFormParams().Format);
992 
993     // Add location and operation entries.
994     CurrentSymbol->addLocation(Attr, /*LowPC=*/0, /*HighPC=*/-1,
995                                /*SectionOffset=*/0, OffsetOnEntry,
996                                CallSiteLocation);
997     ProcessLocationExpression(Expression);
998     return;
999   }
1000 
1001   if (DWARFAttribute::mayHaveLocationList(Attr) &&
1002       FormValue.isFormClass(DWARFFormValue::FC_SectionOffset)) {
1003     uint64_t Offset = *FormValue.getAsSectionOffset();
1004     if (FormValue.getForm() == dwarf::DW_FORM_loclistx) {
1005       std::optional<uint64_t> LoclistOffset = U->getLoclistOffset(Offset);
1006       if (!LoclistOffset)
1007         return;
1008       Offset = *LoclistOffset;
1009     }
1010     uint64_t BaseAddr = 0;
1011     if (std::optional<SectionedAddress> BA = U->getBaseAddress())
1012       BaseAddr = BA->Address;
1013     LVAddress LowPC = 0;
1014     LVAddress HighPC = 0;
1015 
1016     auto ProcessLocationEntry = [&](const DWARFLocationEntry &Entry) {
1017       if (Entry.Kind == dwarf::DW_LLE_base_address) {
1018         BaseAddr = Entry.Value0;
1019         return;
1020       }
1021       if (Entry.Kind == dwarf::DW_LLE_offset_pair) {
1022         LowPC = BaseAddr + Entry.Value0;
1023         HighPC = BaseAddr + Entry.Value1;
1024         DWARFAddressRange Range{LowPC, HighPC, Entry.SectionIndex};
1025         if (Range.SectionIndex == SectionedAddress::UndefSection)
1026           Range.SectionIndex = Entry.SectionIndex;
1027         DWARFLocationExpression Loc{Range, Entry.Loc};
1028         DWARFDataExtractor Data(Loc.Expr, IsLittleEndian,
1029                                 U->getAddressByteSize());
1030         DWARFExpression Expression(Data, U->getAddressByteSize());
1031 
1032         // Store the real upper limit for the address range.
1033         if (UpdateHighAddress && HighPC > 0)
1034           --HighPC;
1035         // Add location and operation entries.
1036         CurrentSymbol->addLocation(Attr, LowPC, HighPC, Offset, OffsetOnEntry,
1037                                    CallSiteLocation);
1038         ProcessLocationExpression(Expression);
1039       }
1040     };
1041     Error E = U->getLocationTable().visitLocationList(
1042         &Offset, [&](const DWARFLocationEntry &E) {
1043           ProcessLocationEntry(E);
1044           return true;
1045         });
1046     if (E)
1047       consumeError(std::move(E));
1048   }
1049 }
1050 
1051 void LVELFReader::processLocationMember(dwarf::Attribute Attr,
1052                                         const DWARFFormValue &FormValue,
1053                                         const DWARFDie &Die,
1054                                         uint64_t OffsetOnEntry) {
1055   // Check if the value is an integer constant.
1056   if (FormValue.isFormClass(DWARFFormValue::FC_Constant))
1057     // Add a record to hold a constant as location.
1058     CurrentSymbol->addLocationConstant(Attr, *FormValue.getAsUnsignedConstant(),
1059                                        OffsetOnEntry);
1060   else
1061     // This is a a location description, or a reference to one.
1062     processLocationList(Attr, FormValue, Die, OffsetOnEntry);
1063 }
1064 
1065 // Update the current element with the reference.
1066 void LVELFReader::updateReference(dwarf::Attribute Attr,
1067                                   const DWARFFormValue &FormValue) {
1068   // FIXME: We are assuming that at most one Reference (DW_AT_specification,
1069   // DW_AT_abstract_origin, ...) and at most one Type (DW_AT_import, DW_AT_type)
1070   // appear in any single DIE, but this may not be true.
1071   uint64_t Reference = *FormValue.getAsReference();
1072   // Get target for the given reference, if already created.
1073   LVElement *Target = getElementForOffset(
1074       Reference, CurrentElement,
1075       /*IsType=*/Attr == dwarf::DW_AT_import || Attr == dwarf::DW_AT_type);
1076   // Check if we are dealing with cross CU references.
1077   if (FormValue.getForm() == dwarf::DW_FORM_ref_addr) {
1078     if (Target) {
1079       // The global reference is ready. Mark it as global.
1080       Target->setIsGlobalReference();
1081       // Remove global reference from the unseen list.
1082       removeGlobalOffset(Reference);
1083     } else
1084       // Record the unseen cross CU reference.
1085       addGlobalOffset(Reference);
1086   }
1087 
1088   // At this point, 'Target' can be null, in the case of the target element
1089   // not being seen. But the correct bit is set, to indicate that the target
1090   // is being referenced by (abstract_origin, extension, specification) or
1091   // (import, type).
1092   // We must differentiate between the kind of reference. This is needed to
1093   // complete inlined function instances with dropped abstract references,
1094   // in order to facilitate a logical comparison.
1095   switch (Attr) {
1096   case dwarf::DW_AT_abstract_origin:
1097   case dwarf::DW_AT_call_origin:
1098     CurrentElement->setReference(Target);
1099     CurrentElement->setHasReferenceAbstract();
1100     break;
1101   case dwarf::DW_AT_extension:
1102     CurrentElement->setReference(Target);
1103     CurrentElement->setHasReferenceExtension();
1104     break;
1105   case dwarf::DW_AT_specification:
1106     CurrentElement->setReference(Target);
1107     CurrentElement->setHasReferenceSpecification();
1108     break;
1109   case dwarf::DW_AT_import:
1110   case dwarf::DW_AT_type:
1111     CurrentElement->setType(Target);
1112     break;
1113   default:
1114     break;
1115   }
1116 }
1117 
1118 // Get an element given the DIE offset.
1119 LVElement *LVELFReader::getElementForOffset(LVOffset Offset, LVElement *Element,
1120                                             bool IsType) {
1121   auto Iter = ElementTable.try_emplace(Offset).first;
1122   // Update the element and all the references pointing to this element.
1123   LVElementEntry &Entry = Iter->second;
1124   if (!Entry.Element) {
1125     if (IsType)
1126       Entry.Types.insert(Element);
1127     else
1128       Entry.References.insert(Element);
1129   }
1130   return Entry.Element;
1131 }
1132 
1133 Error LVELFReader::loadTargetInfo(const ObjectFile &Obj) {
1134   // Detect the architecture from the object file. We usually don't need OS
1135   // info to lookup a target and create register info.
1136   Triple TT;
1137   TT.setArch(Triple::ArchType(Obj.getArch()));
1138   TT.setVendor(Triple::UnknownVendor);
1139   TT.setOS(Triple::UnknownOS);
1140 
1141   // Features to be passed to target/subtarget
1142   Expected<SubtargetFeatures> Features = Obj.getFeatures();
1143   SubtargetFeatures FeaturesValue;
1144   if (!Features) {
1145     consumeError(Features.takeError());
1146     FeaturesValue = SubtargetFeatures();
1147   }
1148   FeaturesValue = *Features;
1149   return loadGenericTargetInfo(TT.str(), FeaturesValue.getString());
1150 }
1151 
1152 void LVELFReader::mapRangeAddress(const ObjectFile &Obj) {
1153   for (auto Iter = Obj.symbol_begin(); Iter != Obj.symbol_end(); ++Iter) {
1154     const SymbolRef &Symbol = *Iter;
1155 
1156     Expected<SymbolRef::Type> TypeOrErr = Symbol.getType();
1157     if (!TypeOrErr) {
1158       consumeError(TypeOrErr.takeError());
1159       continue;
1160     }
1161 
1162     // Process only symbols that represent a function.
1163     SymbolRef::Type Type = *TypeOrErr;
1164     if (Type != SymbolRef::ST_Function)
1165       continue;
1166 
1167     // In the case of a Mach-O STAB symbol, get its section only if
1168     // the STAB symbol's section field refers to a valid section index.
1169     // Otherwise the symbol may error trying to load a section that
1170     // does not exist.
1171     const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(&Obj);
1172     bool IsSTAB = false;
1173     if (MachO) {
1174       DataRefImpl SymDRI = Symbol.getRawDataRefImpl();
1175       uint8_t NType =
1176           (MachO->is64Bit() ? MachO->getSymbol64TableEntry(SymDRI).n_type
1177                             : MachO->getSymbolTableEntry(SymDRI).n_type);
1178       if (NType & MachO::N_STAB)
1179         IsSTAB = true;
1180     }
1181 
1182     Expected<section_iterator> IterOrErr = Symbol.getSection();
1183     if (!IterOrErr) {
1184       consumeError(IterOrErr.takeError());
1185       continue;
1186     }
1187     section_iterator Section = IsSTAB ? Obj.section_end() : *IterOrErr;
1188     if (Section == Obj.section_end())
1189       continue;
1190 
1191     // Get the symbol value.
1192     Expected<uint64_t> AddressOrErr = Symbol.getAddress();
1193     if (!AddressOrErr) {
1194       consumeError(AddressOrErr.takeError());
1195       continue;
1196     }
1197     uint64_t Address = *AddressOrErr;
1198 
1199     // Get symbol name.
1200     StringRef Name;
1201     Expected<StringRef> NameOrErr = Symbol.getName();
1202     if (!NameOrErr) {
1203       consumeError(NameOrErr.takeError());
1204       continue;
1205     }
1206     Name = *NameOrErr;
1207 
1208     // Check if the symbol is Comdat.
1209     Expected<uint32_t> FlagsOrErr = Symbol.getFlags();
1210     if (!FlagsOrErr) {
1211       consumeError(FlagsOrErr.takeError());
1212       continue;
1213     }
1214     uint32_t Flags = *FlagsOrErr;
1215 
1216     // Mark the symbol as 'comdat' in any of the following cases:
1217     // - Symbol has the SF_Weak flag or
1218     // - Symbol section index different from the DotTextSectionIndex.
1219     LVSectionIndex SectionIndex = Section->getIndex();
1220     bool IsComdat =
1221         (Flags & SymbolRef::SF_Weak) || (SectionIndex != DotTextSectionIndex);
1222 
1223     // Record the symbol name (linkage) and its loading address.
1224     addToSymbolTable(Name, Address, SectionIndex, IsComdat);
1225   }
1226 }
1227 
1228 void LVELFReader::sortScopes() { Root->sort(); }
1229 
1230 void LVELFReader::print(raw_ostream &OS) const {
1231   OS << "LVType\n";
1232   LLVM_DEBUG(dbgs() << "CreateReaders\n");
1233 }
1234