LCOV - code coverage report
Current view: top level - lib/DebugInfo/DWARF - DWARFAcceleratorTable.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 100 105 95.2 %
Date: 2017-09-14 15:23:50 Functions: 9 9 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- DWARFAcceleratorTable.cpp ------------------------------------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : 
      10             : #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
      11             : 
      12             : #include "llvm/ADT/SmallVector.h"
      13             : #include "llvm/BinaryFormat/Dwarf.h"
      14             : #include "llvm/DebugInfo/DWARF/DWARFContext.h"
      15             : #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
      16             : #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
      17             : #include "llvm/Support/Compiler.h"
      18             : #include "llvm/Support/Format.h"
      19             : #include "llvm/Support/raw_ostream.h"
      20             : #include <cstddef>
      21             : #include <cstdint>
      22             : #include <utility>
      23             : 
      24             : using namespace llvm;
      25             : 
      26         673 : bool DWARFAcceleratorTable::extract() {
      27         673 :   uint32_t Offset = 0;
      28             : 
      29             :   // Check that we can at least read the header.
      30        1346 :   if (!AccelSection.isValidOffset(offsetof(Header, HeaderDataLength)+4))
      31             :     return false;
      32             : 
      33         149 :   Hdr.Magic = AccelSection.getU32(&Offset);
      34         149 :   Hdr.Version = AccelSection.getU16(&Offset);
      35         149 :   Hdr.HashFunction = AccelSection.getU16(&Offset);
      36         149 :   Hdr.NumBuckets = AccelSection.getU32(&Offset);
      37         149 :   Hdr.NumHashes = AccelSection.getU32(&Offset);
      38         149 :   Hdr.HeaderDataLength = AccelSection.getU32(&Offset);
      39             : 
      40             :   // Check that we can read all the hashes and offsets from the
      41             :   // section (see SourceLevelDebugging.rst for the structure of the index).
      42         447 :   if (!AccelSection.isValidOffset(sizeof(Hdr) + Hdr.HeaderDataLength +
      43         149 :                                   Hdr.NumBuckets*4 + Hdr.NumHashes*8))
      44             :     return false;
      45             : 
      46          81 :   HdrData.DIEOffsetBase = AccelSection.getU32(&Offset);
      47          81 :   uint32_t NumAtoms = AccelSection.getU32(&Offset);
      48             : 
      49         233 :   for (unsigned i = 0; i < NumAtoms; ++i) {
      50         152 :     uint16_t AtomType = AccelSection.getU16(&Offset);
      51         152 :     auto AtomForm = static_cast<dwarf::Form>(AccelSection.getU16(&Offset));
      52         304 :     HdrData.Atoms.push_back(std::make_pair(AtomType, AtomForm));
      53             :   }
      54             : 
      55             :   return true;
      56             : }
      57             : 
      58           7 : uint32_t DWARFAcceleratorTable::getNumBuckets() { return Hdr.NumBuckets; }
      59           7 : uint32_t DWARFAcceleratorTable::getNumHashes() { return Hdr.NumHashes; }
      60          15 : uint32_t DWARFAcceleratorTable::getSizeHdr() { return sizeof(Hdr); }
      61           7 : uint32_t DWARFAcceleratorTable::getHeaderDataLength() {
      62           7 :   return Hdr.HeaderDataLength;
      63             : }
      64             : 
      65             : ArrayRef<std::pair<DWARFAcceleratorTable::HeaderData::AtomType,
      66             :                    DWARFAcceleratorTable::HeaderData::Form>>
      67          84 : DWARFAcceleratorTable::getAtomsDesc() {
      68         168 :   return HdrData.Atoms;
      69             : }
      70             : 
      71           6 : bool DWARFAcceleratorTable::validateForms() {
      72          21 :   for (auto Atom : getAtomsDesc()) {
      73          20 :     DWARFFormValue FormValue(Atom.second);
      74             :     switch (Atom.first) {
      75          10 :     case dwarf::DW_ATOM_die_offset:
      76             :     case dwarf::DW_ATOM_die_tag:
      77             :     case dwarf::DW_ATOM_type_flags:
      78          12 :       if ((!FormValue.isFormClass(DWARFFormValue::FC_Constant) &&
      79          19 :            !FormValue.isFormClass(DWARFFormValue::FC_Flag)) ||
      80           9 :           FormValue.getForm() == dwarf::DW_FORM_sdata)
      81           1 :         return false;
      82             :     default:
      83             :       break;
      84             :     }
      85             :   }
      86             :   return true;
      87             : }
      88             : 
      89             : std::pair<uint32_t, dwarf::Tag>
      90          71 : DWARFAcceleratorTable::readAtoms(uint32_t &HashDataOffset) {
      91          71 :   uint32_t DieOffset = dwarf::DW_INVALID_OFFSET;
      92          71 :   dwarf::Tag DieTag = dwarf::DW_TAG_null;
      93             : 
      94         227 :   for (auto Atom : getAtomsDesc()) {
      95         170 :     DWARFFormValue FormValue(Atom.second);
      96          85 :     FormValue.extractValue(AccelSection, &HashDataOffset, NULL);
      97          85 :     switch (Atom.first) {
      98          71 :     case dwarf::DW_ATOM_die_offset:
      99         213 :       DieOffset = *FormValue.getAsUnsignedConstant();
     100          71 :       break;
     101           7 :     case dwarf::DW_ATOM_die_tag:
     102          21 :       DieTag = (dwarf::Tag)*FormValue.getAsUnsignedConstant();
     103           7 :       break;
     104             :     default:
     105             :       break;
     106             :     }
     107             :   }
     108          71 :   return {DieOffset, DieTag};
     109             : }
     110             : 
     111          74 : LLVM_DUMP_METHOD void DWARFAcceleratorTable::dump(raw_ostream &OS) const {
     112             :   // Dump the header.
     113         296 :   OS << "Magic = " << format("0x%08x", Hdr.Magic) << '\n'
     114         296 :      << "Version = " << format("0x%04x", Hdr.Version) << '\n'
     115         296 :      << "Hash function = " << format("0x%08x", Hdr.HashFunction) << '\n'
     116         222 :      << "Bucket count = " << Hdr.NumBuckets << '\n'
     117         222 :      << "Hashes count = " << Hdr.NumHashes << '\n'
     118         222 :      << "HeaderData length = " << Hdr.HeaderDataLength << '\n'
     119         222 :      << "DIE offset base = " << HdrData.DIEOffsetBase << '\n'
     120         222 :      << "Number of atoms = " << HdrData.Atoms.size() << '\n';
     121             : 
     122          74 :   unsigned i = 0;
     123         148 :   SmallVector<DWARFFormValue, 3> AtomForms;
     124         364 :   for (const auto &Atom: HdrData.Atoms) {
     125         142 :     OS << format("Atom[%d] Type: ", i++);
     126         142 :     auto TypeString = dwarf::AtomTypeString(Atom.first);
     127         142 :     if (!TypeString.empty())
     128         142 :       OS << TypeString;
     129             :     else
     130           0 :       OS << format("DW_ATOM_Unknown_0x%x", Atom.first);
     131         142 :     OS << " Form: ";
     132         142 :     auto FormString = dwarf::FormEncodingString(Atom.second);
     133             :     if (!FormString.empty())
     134         142 :       OS << FormString;
     135             :     else
     136           0 :       OS << format("DW_FORM_Unknown_0x%x", Atom.second);
     137         142 :     OS << '\n';
     138         284 :     AtomForms.push_back(DWARFFormValue(Atom.second));
     139             :   }
     140             : 
     141             :   // Now go through the actual tables and dump them.
     142          74 :   uint32_t Offset = sizeof(Hdr) + Hdr.HeaderDataLength;
     143          74 :   unsigned HashesBase = Offset + Hdr.NumBuckets * 4;
     144          74 :   unsigned OffsetsBase = HashesBase + Hdr.NumHashes * 4;
     145             : 
     146         330 :   for (unsigned Bucket = 0; Bucket < Hdr.NumBuckets; ++Bucket) {
     147         256 :     unsigned Index = AccelSection.getU32(&Offset);
     148             : 
     149         512 :     OS << format("Bucket[%d]\n", Bucket);
     150         323 :     if (Index == UINT32_MAX) {
     151          67 :       OS << "  EMPTY\n";
     152          67 :       continue;
     153             :     }
     154             : 
     155         803 :     for (unsigned HashIdx = Index; HashIdx < Hdr.NumHashes; ++HashIdx) {
     156         422 :       unsigned HashOffset = HashesBase + HashIdx*4;
     157         422 :       unsigned OffsetsOffset = OffsetsBase + HashIdx*4;
     158         422 :       uint32_t Hash = AccelSection.getU32(&HashOffset);
     159             : 
     160         422 :       if (Hash % Hdr.NumBuckets != Bucket)
     161             :         break;
     162             : 
     163         307 :       unsigned DataOffset = AccelSection.getU32(&OffsetsOffset);
     164         614 :       OS << format("  Hash = 0x%08x Offset = 0x%08x\n", Hash, DataOffset);
     165         614 :       if (!AccelSection.isValidOffset(DataOffset)) {
     166           0 :         OS << "    Invalid section offset\n";
     167           0 :         continue;
     168             :       }
     169        1553 :       while (AccelSection.isValidOffsetForDataOfSize(DataOffset, 4)) {
     170         620 :         unsigned StringOffset = AccelSection.getRelocatedValue(4, &DataOffset);
     171         620 :         if (!StringOffset)
     172             :           break;
     173         626 :         OS << format("    Name: %08x \"%s\"\n", StringOffset,
     174         313 :                      StringSection.getCStr(&StringOffset));
     175         313 :         unsigned NumData = AccelSection.getU32(&DataOffset);
     176         687 :         for (unsigned Data = 0; Data < NumData; ++Data) {
     177         748 :           OS << format("    Data[%d] => ", Data);
     178         374 :           unsigned i = 0;
     179        1716 :           for (auto &Atom : AtomForms) {
     180        1188 :             OS << format("{Atom[%d]: ", i++);
     181         594 :             if (Atom.extractValue(AccelSection, &DataOffset, nullptr))
     182        1188 :               Atom.dump(OS);
     183             :             else
     184           0 :               OS << "Error extracting the value";
     185         594 :             OS << "} ";
     186             :           }
     187         374 :           OS << '\n';
     188             :         }
     189             :       }
     190             :     }
     191             :   }
     192          74 : }

Generated by: LCOV version 1.13