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 = new LVType(); 61 CurrentType->setIsBase(); 62 if (options().getAttributeBase()) 63 CurrentType->setIncludeInPrint(); 64 return CurrentType; 65 case dwarf::DW_TAG_const_type: 66 CurrentType = new LVType(); 67 CurrentType->setIsConst(); 68 CurrentType->setName("const"); 69 return CurrentType; 70 case dwarf::DW_TAG_enumerator: 71 CurrentType = new LVTypeEnumerator(); 72 return CurrentType; 73 case dwarf::DW_TAG_imported_declaration: 74 CurrentType = new LVTypeImport(); 75 CurrentType->setIsImportDeclaration(); 76 return CurrentType; 77 case dwarf::DW_TAG_imported_module: 78 CurrentType = new LVTypeImport(); 79 CurrentType->setIsImportModule(); 80 return CurrentType; 81 case dwarf::DW_TAG_pointer_type: 82 CurrentType = new LVType(); 83 CurrentType->setIsPointer(); 84 CurrentType->setName("*"); 85 return CurrentType; 86 case dwarf::DW_TAG_ptr_to_member_type: 87 CurrentType = new LVType(); 88 CurrentType->setIsPointerMember(); 89 CurrentType->setName("*"); 90 return CurrentType; 91 case dwarf::DW_TAG_reference_type: 92 CurrentType = new LVType(); 93 CurrentType->setIsReference(); 94 CurrentType->setName("&"); 95 return CurrentType; 96 case dwarf::DW_TAG_restrict_type: 97 CurrentType = new LVType(); 98 CurrentType->setIsRestrict(); 99 CurrentType->setName("restrict"); 100 return CurrentType; 101 case dwarf::DW_TAG_rvalue_reference_type: 102 CurrentType = new LVType(); 103 CurrentType->setIsRvalueReference(); 104 CurrentType->setName("&&"); 105 return CurrentType; 106 case dwarf::DW_TAG_subrange_type: 107 CurrentType = new LVTypeSubrange(); 108 return CurrentType; 109 case dwarf::DW_TAG_template_value_parameter: 110 CurrentType = new LVTypeParam(); 111 CurrentType->setIsTemplateValueParam(); 112 return CurrentType; 113 case dwarf::DW_TAG_template_type_parameter: 114 CurrentType = new LVTypeParam(); 115 CurrentType->setIsTemplateTypeParam(); 116 return CurrentType; 117 case dwarf::DW_TAG_GNU_template_template_param: 118 CurrentType = new LVTypeParam(); 119 CurrentType->setIsTemplateTemplateParam(); 120 return CurrentType; 121 case dwarf::DW_TAG_typedef: 122 CurrentType = new LVTypeDefinition(); 123 return CurrentType; 124 case dwarf::DW_TAG_unspecified_type: 125 CurrentType = new LVType(); 126 CurrentType->setIsUnspecified(); 127 return CurrentType; 128 case dwarf::DW_TAG_volatile_type: 129 CurrentType = new LVType(); 130 CurrentType->setIsVolatile(); 131 CurrentType->setName("volatile"); 132 return CurrentType; 133 134 // Symbols. 135 case dwarf::DW_TAG_formal_parameter: 136 CurrentSymbol = new LVSymbol(); 137 CurrentSymbol->setIsParameter(); 138 return CurrentSymbol; 139 case dwarf::DW_TAG_unspecified_parameters: 140 CurrentSymbol = new LVSymbol(); 141 CurrentSymbol->setIsUnspecified(); 142 CurrentSymbol->setName("..."); 143 return CurrentSymbol; 144 case dwarf::DW_TAG_member: 145 CurrentSymbol = new LVSymbol(); 146 CurrentSymbol->setIsMember(); 147 return CurrentSymbol; 148 case dwarf::DW_TAG_variable: 149 CurrentSymbol = new LVSymbol(); 150 CurrentSymbol->setIsVariable(); 151 return CurrentSymbol; 152 case dwarf::DW_TAG_inheritance: 153 CurrentSymbol = new LVSymbol(); 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 = new LVSymbol(); 159 CurrentSymbol->setIsCallSiteParameter(); 160 return CurrentSymbol; 161 case dwarf::DW_TAG_constant: 162 CurrentSymbol = new LVSymbol(); 163 CurrentSymbol->setIsConstant(); 164 return CurrentSymbol; 165 166 // Scopes. 167 case dwarf::DW_TAG_catch_block: 168 CurrentScope = new LVScope(); 169 CurrentScope->setIsCatchBlock(); 170 return CurrentScope; 171 case dwarf::DW_TAG_lexical_block: 172 CurrentScope = new LVScope(); 173 CurrentScope->setIsLexicalBlock(); 174 return CurrentScope; 175 case dwarf::DW_TAG_try_block: 176 CurrentScope = new LVScope(); 177 CurrentScope->setIsTryBlock(); 178 return CurrentScope; 179 case dwarf::DW_TAG_compile_unit: 180 case dwarf::DW_TAG_skeleton_unit: 181 CurrentScope = new LVScopeCompileUnit(); 182 CompileUnit = static_cast<LVScopeCompileUnit *>(CurrentScope); 183 return CurrentScope; 184 case dwarf::DW_TAG_inlined_subroutine: 185 CurrentScope = new LVScopeFunctionInlined(); 186 return CurrentScope; 187 case dwarf::DW_TAG_namespace: 188 CurrentScope = new LVScopeNamespace(); 189 return CurrentScope; 190 case dwarf::DW_TAG_template_alias: 191 CurrentScope = new LVScopeAlias(); 192 return CurrentScope; 193 case dwarf::DW_TAG_array_type: 194 CurrentScope = new LVScopeArray(); 195 return CurrentScope; 196 case dwarf::DW_TAG_call_site: 197 case dwarf::DW_TAG_GNU_call_site: 198 CurrentScope = new LVScopeFunction(); 199 CurrentScope->setIsCallSite(); 200 return CurrentScope; 201 case dwarf::DW_TAG_entry_point: 202 CurrentScope = new LVScopeFunction(); 203 CurrentScope->setIsEntryPoint(); 204 return CurrentScope; 205 case dwarf::DW_TAG_subprogram: 206 CurrentScope = new LVScopeFunction(); 207 CurrentScope->setIsSubprogram(); 208 return CurrentScope; 209 case dwarf::DW_TAG_subroutine_type: 210 CurrentScope = new LVScopeFunctionType(); 211 return CurrentScope; 212 case dwarf::DW_TAG_label: 213 CurrentScope = new LVScopeFunction(); 214 CurrentScope->setIsLabel(); 215 return CurrentScope; 216 case dwarf::DW_TAG_class_type: 217 CurrentScope = new LVScopeAggregate(); 218 CurrentScope->setIsClass(); 219 return CurrentScope; 220 case dwarf::DW_TAG_structure_type: 221 CurrentScope = new LVScopeAggregate(); 222 CurrentScope->setIsStructure(); 223 return CurrentScope; 224 case dwarf::DW_TAG_union_type: 225 CurrentScope = new LVScopeAggregate(); 226 CurrentScope->setIsUnion(); 227 return CurrentScope; 228 case dwarf::DW_TAG_enumeration_type: 229 CurrentScope = new LVScopeEnumeration(); 230 return CurrentScope; 231 case dwarf::DW_TAG_GNU_formal_parameter_pack: 232 CurrentScope = new LVScopeFormalPack(); 233 return CurrentScope; 234 case dwarf::DW_TAG_GNU_template_parameter_pack: 235 CurrentScope = new LVScopeTemplatePack(); 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( 552 std::piecewise_construct, std::forward_as_tuple(Offset), 553 std::forward_as_tuple(CurrentElement, LVElementSet())); 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.first = CurrentElement; 559 // Traverse the element set and update the elements (backtracking). 560 // Using the bit associated with 'type' or 'reference' allows us to set 561 // the correct target. 562 for (LVElement *Target : Reference.second) 563 Target->getHasReference() ? Target->setReference(CurrentElement) 564 : Target->setType(CurrentElement); 565 // Clear the pending elements. 566 Reference.second.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 = new LVLineDebug(); 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, uint64_t Operands[2]) { 763 // The 'prettyPrintRegisterOp' function uses the DWARFUnit to support 764 // DW_OP_regval_type. At this point we are operating on a logical view 765 // item, with no access to the underlying DWARF data used by LLVM. 766 // We do not support DW_OP_regval_type here. 767 if (Opcode == dwarf::DW_OP_regval_type) 768 return {}; 769 770 std::string string; 771 raw_string_ostream Stream(string); 772 DIDumpOptions DumpOpts; 773 auto *MCRegInfo = MRI.get(); 774 auto GetRegName = [&MCRegInfo](uint64_t DwarfRegNum, bool IsEH) -> StringRef { 775 if (!MCRegInfo) 776 return {}; 777 if (std::optional<unsigned> LLVMRegNum = 778 MCRegInfo->getLLVMRegNum(DwarfRegNum, IsEH)) 779 if (const char *RegName = MCRegInfo->getName(*LLVMRegNum)) 780 return StringRef(RegName); 781 return {}; 782 }; 783 DumpOpts.GetNameForDWARFReg = GetRegName; 784 DWARFExpression::prettyPrintRegisterOp(/*U=*/nullptr, Stream, DumpOpts, 785 Opcode, Operands); 786 return Stream.str(); 787 } 788 789 Error LVELFReader::createScopes() { 790 LLVM_DEBUG({ 791 W.startLine() << "\n"; 792 W.printString("File", Obj.getFileName().str()); 793 W.printString("Format", FileFormatName); 794 }); 795 796 if (Error Err = LVReader::createScopes()) 797 return Err; 798 799 // As the DwarfContext object is valid only during the scopes creation, 800 // we need to create our own Target information, to be used during the 801 // logical view printing, in the case of instructions being requested. 802 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(Obj); 803 if (!DwarfContext) 804 return createStringError(errc::invalid_argument, 805 "Could not create DWARF information: %s", 806 getFilename().str().c_str()); 807 808 if (Error Err = loadTargetInfo(Obj)) 809 return Err; 810 811 // Create a mapping for virtual addresses. 812 mapVirtualAddress(Obj); 813 814 // Select the correct compile unit range, depending if we are dealing with 815 // a standard or split DWARF object. 816 DWARFContext::compile_unit_range CompileUnits = 817 DwarfContext->getNumCompileUnits() ? DwarfContext->compile_units() 818 : DwarfContext->dwo_compile_units(); 819 for (const std::unique_ptr<DWARFUnit> &CU : CompileUnits) { 820 821 // Deduction of index used for the line records. 822 // 823 // For the following test case: test.cpp 824 // void foo(void ParamPtr) { } 825 826 // Both GCC and Clang generate DWARF-5 .debug_line layout. 827 828 // * GCC (GNU C++17 11.3.0) - All DW_AT_decl_file use index 1. 829 // 830 // .debug_info: 831 // format = DWARF32, version = 0x0005 832 // DW_TAG_compile_unit 833 // DW_AT_name ("test.cpp") 834 // DW_TAG_subprogram ("foo") 835 // DW_AT_decl_file (1) 836 // DW_TAG_formal_parameter ("ParamPtr") 837 // DW_AT_decl_file (1) 838 // .debug_line: 839 // Line table prologue: format (DWARF32), version (5) 840 // include_directories[0] = "..." 841 // file_names[0]: name ("test.cpp"), dir_index (0) 842 // file_names[1]: name ("test.cpp"), dir_index (0) 843 844 // * Clang (14.0.6) - All DW_AT_decl_file use index 0. 845 // 846 // .debug_info: 847 // format = DWARF32, version = 0x0005 848 // DW_AT_producer ("clang version 14.0.6") 849 // DW_AT_name ("test.cpp") 850 // 851 // DW_TAG_subprogram ("foo") 852 // DW_AT_decl_file (0) 853 // DW_TAG_formal_parameter ("ParamPtr") 854 // DW_AT_decl_file (0) 855 // .debug_line: 856 // Line table prologue: format (DWARF32), version (5) 857 // include_directories[0] = "..." 858 // file_names[0]: name ("test.cpp"), dir_index (0) 859 860 // From DWARFDebugLine::getFileNameByIndex documentation: 861 // In Dwarf 4, the files are 1-indexed. 862 // In Dwarf 5, the files are 0-indexed. 863 // Additional discussions here: 864 // https://www.mail-archive.com/dwarf-discuss@lists.dwarfstd.org/msg00883.html 865 866 // The ELF Reader is expecting the files are 1-indexed, so using 867 // the .debug_line header information decide if the indexed require 868 // an internal adjustment. 869 870 // For the case of GCC (DWARF5), if the entries[0] and [1] are the 871 // same, do not perform any adjustment. 872 auto DeduceIncrementFileIndex = [&]() -> bool { 873 if (CU->getVersion() < 5) 874 // DWARF-4 or earlier -> Don't increment index. 875 return false; 876 877 if (const DWARFDebugLine::LineTable *LT = 878 CU->getContext().getLineTableForUnit(CU.get())) { 879 // Check if there are at least 2 entries and if they are the same. 880 if (LT->hasFileAtIndex(0) && LT->hasFileAtIndex(1)) { 881 const DWARFDebugLine::FileNameEntry &EntryZero = 882 LT->Prologue.getFileNameEntry(0); 883 const DWARFDebugLine::FileNameEntry &EntryOne = 884 LT->Prologue.getFileNameEntry(1); 885 // Check directory indexes. 886 if (EntryZero.DirIdx != EntryOne.DirIdx) 887 // DWARF-5 -> Increment index. 888 return true; 889 // Check filename. 890 std::string FileZero; 891 std::string FileOne; 892 StringRef None; 893 LT->getFileNameByIndex( 894 0, None, DILineInfoSpecifier::FileLineInfoKind::RawValue, 895 FileZero); 896 LT->getFileNameByIndex( 897 1, None, DILineInfoSpecifier::FileLineInfoKind::RawValue, 898 FileOne); 899 return FileZero.compare(FileOne); 900 } 901 } 902 903 // DWARF-5 -> Increment index. 904 return true; 905 }; 906 // The ELF reader expects the indexes as 1-indexed. 907 IncrementFileIndex = DeduceIncrementFileIndex(); 908 909 DWARFDie UnitDie = CU->getUnitDIE(); 910 SmallString<16> DWOAlternativeLocation; 911 if (UnitDie) { 912 std::optional<const char *> DWOFileName = 913 CU->getVersion() >= 5 914 ? dwarf::toString(UnitDie.find(dwarf::DW_AT_dwo_name)) 915 : dwarf::toString(UnitDie.find(dwarf::DW_AT_GNU_dwo_name)); 916 StringRef From(DWOFileName.value_or("")); 917 DWOAlternativeLocation = createAlternativePath(From); 918 } 919 920 // The current CU can be a normal compile unit (standard) or a skeleton 921 // compile unit (split). For both cases, the returned die, will be used 922 // to create the logical scopes. 923 DWARFDie CUDie = CU->getNonSkeletonUnitDIE( 924 /*ExtractUnitDIEOnly=*/false, 925 /*DWOAlternativeLocation=*/DWOAlternativeLocation); 926 if (!CUDie.isValid()) 927 continue; 928 929 // The current unit corresponds to the .dwo file. We need to get the 930 // skeleton unit and query for any ranges that will enclose any ranges 931 // in the non-skeleton unit. 932 DWARFDie DummyDie; 933 DWARFDie SkeletonDie = 934 CUDie.getDwarfUnit()->isDWOUnit() ? CU->getUnitDIE(false) : DummyDie; 935 // Disable the ranges processing if we have just a single .dwo object, 936 // as any DW_AT_ranges will access not available range information. 937 RangesDataAvailable = 938 (!CUDie.getDwarfUnit()->isDWOUnit() || 939 (SkeletonDie.isValid() ? !SkeletonDie.getDwarfUnit()->isDWOUnit() 940 : true)); 941 942 traverseDieAndChildren(CUDie, Root, SkeletonDie); 943 944 createLineAndFileRecords(DwarfContext->getLineTableForUnit(CU.get())); 945 if (Error Err = createInstructions()) 946 return Err; 947 948 // Process the compilation unit, as there are cases where enclosed 949 // functions have the same ranges values. Insert the compilation unit 950 // ranges at the end, to allow enclosing ranges to be first in the list. 951 LVSectionIndex SectionIndex = getSectionIndex(CompileUnit); 952 addSectionRange(SectionIndex, CompileUnit); 953 LVRange *ScopesWithRanges = getSectionRanges(SectionIndex); 954 ScopesWithRanges->sort(); 955 956 processLines(&CULines, SectionIndex); 957 processLocationGaps(); 958 959 // These are per compile unit. 960 ScopesWithRanges->clear(); 961 SymbolsWithLocations.clear(); 962 CULines.clear(); 963 } 964 965 return Error::success(); 966 } 967 968 // Get the location information for the associated attribute. 969 void LVELFReader::processLocationList(dwarf::Attribute Attr, 970 const DWARFFormValue &FormValue, 971 const DWARFDie &Die, 972 uint64_t OffsetOnEntry, 973 bool CallSiteLocation) { 974 975 auto ProcessLocationExpression = [&](const DWARFExpression &Expression) { 976 // DW_OP_const_type is variable-length and has 3 977 // operands. DWARFExpression thus far only supports 2. 978 uint64_t Operands[2] = {0}; 979 for (const DWARFExpression::Operation &Op : Expression) { 980 DWARFExpression::Operation::Description Description = Op.getDescription(); 981 for (unsigned Operand = 0; Operand < 2; ++Operand) { 982 if (Description.Op[Operand] == DWARFExpression::Operation::SizeNA) 983 break; 984 Operands[Operand] = Op.getRawOperand(Operand); 985 } 986 CurrentSymbol->addLocationOperands(Op.getCode(), Operands[0], 987 Operands[1]); 988 } 989 }; 990 991 DWARFUnit *U = Die.getDwarfUnit(); 992 DWARFContext &DwarfContext = U->getContext(); 993 bool IsLittleEndian = DwarfContext.isLittleEndian(); 994 if (FormValue.isFormClass(DWARFFormValue::FC_Block) || 995 (DWARFAttribute::mayHaveLocationExpr(Attr) && 996 FormValue.isFormClass(DWARFFormValue::FC_Exprloc))) { 997 ArrayRef<uint8_t> Expr = *FormValue.getAsBlock(); 998 DataExtractor Data(StringRef((const char *)Expr.data(), Expr.size()), 999 IsLittleEndian, 0); 1000 DWARFExpression Expression(Data, U->getAddressByteSize(), 1001 U->getFormParams().Format); 1002 1003 // Add location and operation entries. 1004 CurrentSymbol->addLocation(Attr, /*LowPC=*/0, /*HighPC=*/-1, 1005 /*SectionOffset=*/0, OffsetOnEntry, 1006 CallSiteLocation); 1007 ProcessLocationExpression(Expression); 1008 return; 1009 } 1010 1011 if (DWARFAttribute::mayHaveLocationList(Attr) && 1012 FormValue.isFormClass(DWARFFormValue::FC_SectionOffset)) { 1013 uint64_t Offset = *FormValue.getAsSectionOffset(); 1014 if (FormValue.getForm() == dwarf::DW_FORM_loclistx) { 1015 std::optional<uint64_t> LoclistOffset = U->getLoclistOffset(Offset); 1016 if (!LoclistOffset) 1017 return; 1018 Offset = *LoclistOffset; 1019 } 1020 uint64_t BaseAddr = 0; 1021 if (std::optional<SectionedAddress> BA = U->getBaseAddress()) 1022 BaseAddr = BA->Address; 1023 LVAddress LowPC = 0; 1024 LVAddress HighPC = 0; 1025 1026 auto ProcessLocationEntry = [&](const DWARFLocationEntry &Entry) { 1027 if (Entry.Kind == dwarf::DW_LLE_base_address) { 1028 BaseAddr = Entry.Value0; 1029 return; 1030 } 1031 if (Entry.Kind == dwarf::DW_LLE_offset_pair) { 1032 LowPC = BaseAddr + Entry.Value0; 1033 HighPC = BaseAddr + Entry.Value1; 1034 DWARFAddressRange Range{LowPC, HighPC, Entry.SectionIndex}; 1035 if (Range.SectionIndex == SectionedAddress::UndefSection) 1036 Range.SectionIndex = Entry.SectionIndex; 1037 DWARFLocationExpression Loc{Range, Entry.Loc}; 1038 DWARFDataExtractor Data(Loc.Expr, IsLittleEndian, 1039 U->getAddressByteSize()); 1040 DWARFExpression Expression(Data, U->getAddressByteSize()); 1041 1042 // Store the real upper limit for the address range. 1043 if (UpdateHighAddress && HighPC > 0) 1044 --HighPC; 1045 // Add location and operation entries. 1046 CurrentSymbol->addLocation(Attr, LowPC, HighPC, Offset, OffsetOnEntry, 1047 CallSiteLocation); 1048 ProcessLocationExpression(Expression); 1049 } 1050 }; 1051 Error E = U->getLocationTable().visitLocationList( 1052 &Offset, [&](const DWARFLocationEntry &E) { 1053 ProcessLocationEntry(E); 1054 return true; 1055 }); 1056 if (E) 1057 consumeError(std::move(E)); 1058 } 1059 } 1060 1061 void LVELFReader::processLocationMember(dwarf::Attribute Attr, 1062 const DWARFFormValue &FormValue, 1063 const DWARFDie &Die, 1064 uint64_t OffsetOnEntry) { 1065 // Check if the value is an integer constant. 1066 if (FormValue.isFormClass(DWARFFormValue::FC_Constant)) 1067 // Add a record to hold a constant as location. 1068 CurrentSymbol->addLocationConstant(Attr, *FormValue.getAsUnsignedConstant(), 1069 OffsetOnEntry); 1070 else 1071 // This is a a location description, or a reference to one. 1072 processLocationList(Attr, FormValue, Die, OffsetOnEntry); 1073 } 1074 1075 // Update the current element with the reference. 1076 void LVELFReader::updateReference(dwarf::Attribute Attr, 1077 const DWARFFormValue &FormValue) { 1078 // We are assuming that DW_AT_specification, DW_AT_abstract_origin, 1079 // DW_AT_type and DW_AT_extension do not appear at the same time 1080 // in the same DIE. 1081 uint64_t Reference = *FormValue.getAsReference(); 1082 // Get target for the given reference, if already created. 1083 LVElement *Target = getElementForOffset(Reference, CurrentElement); 1084 // Check if we are dealing with cross CU references. 1085 if (FormValue.getForm() == dwarf::DW_FORM_ref_addr) { 1086 if (Target) { 1087 // The global reference is ready. Mark it as global. 1088 Target->setIsGlobalReference(); 1089 // Remove global reference from the unseen list. 1090 removeGlobalOffset(Reference); 1091 } else 1092 // Record the unseen cross CU reference. 1093 addGlobalOffset(Reference); 1094 } 1095 1096 // At this point, 'Target' can be null, in the case of the target element 1097 // not being seen. But the correct bit is set, to indicate that the target 1098 // is being referenced by (abstract_origin, extension, specification) or 1099 // (import, type). 1100 // We must differentiate between the kind of reference. This is needed to 1101 // complete inlined function instances with dropped abstract references, 1102 // in order to facilitate a logical comparison. 1103 switch (Attr) { 1104 case dwarf::DW_AT_abstract_origin: 1105 case dwarf::DW_AT_call_origin: 1106 CurrentElement->setReference(Target); 1107 CurrentElement->setHasReferenceAbstract(); 1108 break; 1109 case dwarf::DW_AT_extension: 1110 CurrentElement->setReference(Target); 1111 CurrentElement->setHasReferenceExtension(); 1112 break; 1113 case dwarf::DW_AT_specification: 1114 CurrentElement->setReference(Target); 1115 CurrentElement->setHasReferenceSpecification(); 1116 break; 1117 case dwarf::DW_AT_import: 1118 case dwarf::DW_AT_type: 1119 CurrentElement->setType(Target); 1120 break; 1121 default: 1122 break; 1123 } 1124 } 1125 1126 // Get an element given the DIE offset. 1127 LVElement *LVELFReader::getElementForOffset(LVOffset Offset, 1128 LVElement *Element) { 1129 LVElement *Target = nullptr; 1130 // Search offset in the cross references. 1131 LVElementReference::iterator Iter = ElementTable.find(Offset); 1132 if (Iter == ElementTable.end()) 1133 // Reference to an unseen element. 1134 ElementTable.emplace(std::piecewise_construct, 1135 std::forward_as_tuple(Offset), 1136 std::forward_as_tuple(nullptr, LVElementSet{Element})); 1137 else { 1138 // There are previous references to this element. We need to update the 1139 // element and all the references pointing to this element. 1140 LVElementEntry &Reference = Iter->second; 1141 Target = Reference.first; 1142 if (!Target) 1143 // Add the element to the set. 1144 Reference.second.insert(Element); 1145 } 1146 return Target; 1147 } 1148 1149 Error LVELFReader::loadTargetInfo(const ObjectFile &Obj) { 1150 // Detect the architecture from the object file. We usually don't need OS 1151 // info to lookup a target and create register info. 1152 Triple TT; 1153 TT.setArch(Triple::ArchType(Obj.getArch())); 1154 TT.setVendor(Triple::UnknownVendor); 1155 TT.setOS(Triple::UnknownOS); 1156 1157 // Features to be passed to target/subtarget 1158 Expected<SubtargetFeatures> Features = Obj.getFeatures(); 1159 SubtargetFeatures FeaturesValue; 1160 if (!Features) { 1161 consumeError(Features.takeError()); 1162 FeaturesValue = SubtargetFeatures(); 1163 } 1164 FeaturesValue = *Features; 1165 return loadGenericTargetInfo(TT.str(), FeaturesValue.getString()); 1166 } 1167 1168 void LVELFReader::mapRangeAddress(const ObjectFile &Obj) { 1169 for (auto Iter = Obj.symbol_begin(); Iter != Obj.symbol_end(); ++Iter) { 1170 const SymbolRef &Symbol = *Iter; 1171 1172 Expected<SymbolRef::Type> TypeOrErr = Symbol.getType(); 1173 if (!TypeOrErr) { 1174 consumeError(TypeOrErr.takeError()); 1175 continue; 1176 } 1177 1178 // Process only symbols that represent a function. 1179 SymbolRef::Type Type = *TypeOrErr; 1180 if (Type != SymbolRef::ST_Function) 1181 continue; 1182 1183 // In the case of a Mach-O STAB symbol, get its section only if 1184 // the STAB symbol's section field refers to a valid section index. 1185 // Otherwise the symbol may error trying to load a section that 1186 // does not exist. 1187 const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(&Obj); 1188 bool IsSTAB = false; 1189 if (MachO) { 1190 DataRefImpl SymDRI = Symbol.getRawDataRefImpl(); 1191 uint8_t NType = 1192 (MachO->is64Bit() ? MachO->getSymbol64TableEntry(SymDRI).n_type 1193 : MachO->getSymbolTableEntry(SymDRI).n_type); 1194 if (NType & MachO::N_STAB) 1195 IsSTAB = true; 1196 } 1197 1198 Expected<section_iterator> IterOrErr = Symbol.getSection(); 1199 if (!IterOrErr) { 1200 consumeError(IterOrErr.takeError()); 1201 continue; 1202 } 1203 section_iterator Section = IsSTAB ? Obj.section_end() : *IterOrErr; 1204 if (Section == Obj.section_end()) 1205 continue; 1206 1207 // Get the symbol value. 1208 Expected<uint64_t> AddressOrErr = Symbol.getAddress(); 1209 if (!AddressOrErr) { 1210 consumeError(AddressOrErr.takeError()); 1211 continue; 1212 } 1213 uint64_t Address = *AddressOrErr; 1214 1215 // Get symbol name. 1216 StringRef Name; 1217 Expected<StringRef> NameOrErr = Symbol.getName(); 1218 if (!NameOrErr) { 1219 consumeError(NameOrErr.takeError()); 1220 continue; 1221 } 1222 Name = *NameOrErr; 1223 1224 // Check if the symbol is Comdat. 1225 Expected<uint32_t> FlagsOrErr = Symbol.getFlags(); 1226 if (!FlagsOrErr) { 1227 consumeError(FlagsOrErr.takeError()); 1228 continue; 1229 } 1230 uint32_t Flags = *FlagsOrErr; 1231 1232 // Mark the symbol as 'comdat' in any of the following cases: 1233 // - Symbol has the SF_Weak flag or 1234 // - Symbol section index different from the DotTextSectionIndex. 1235 LVSectionIndex SectionIndex = Section->getIndex(); 1236 bool IsComdat = 1237 (Flags & SymbolRef::SF_Weak) || (SectionIndex != DotTextSectionIndex); 1238 1239 // Record the symbol name (linkage) and its loading address. 1240 addToSymbolTable(Name, Address, SectionIndex, IsComdat); 1241 } 1242 } 1243 1244 void LVELFReader::sortScopes() { Root->sort(); } 1245 1246 void LVELFReader::print(raw_ostream &OS) const { 1247 OS << "LVType\n"; 1248 LLVM_DEBUG(dbgs() << "CreateReaders\n"); 1249 } 1250