LCOV - code coverage report
Current view: top level - lib/DebugInfo/DWARF - DWARFGdbIndex.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 87 89 97.8 %
Date: 2018-07-13 00:08:38 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- DWARFGdbIndex.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/DWARFGdbIndex.h"
      11             : #include "llvm/ADT/SmallVector.h"
      12             : #include "llvm/ADT/StringRef.h"
      13             : #include "llvm/Support/Format.h"
      14             : #include "llvm/Support/raw_ostream.h"
      15             : #include <algorithm>
      16             : #include <cassert>
      17             : #include <cinttypes>
      18             : #include <cstdint>
      19             : #include <utility>
      20             : 
      21             : using namespace llvm;
      22             : 
      23             : // .gdb_index section format reference:
      24             : // https://sourceware.org/gdb/onlinedocs/gdb/Index-Section-Format.html
      25             : 
      26           8 : void DWARFGdbIndex::dumpCUList(raw_ostream &OS) const {
      27          16 :   OS << format("\n  CU list offset = 0x%x, has %" PRId64 " entries:",
      28           8 :                CuListOffset, (uint64_t)CuList.size())
      29             :      << '\n';
      30             :   uint32_t I = 0;
      31          30 :   for (const CompUnitEntry &CU : CuList)
      32          22 :     OS << format("    %d: Offset = 0x%llx, Length = 0x%llx\n", I++, CU.Offset,
      33             :                  CU.Length);
      34           8 : }
      35             : 
      36           8 : void DWARFGdbIndex::dumpAddressArea(raw_ostream &OS) const {
      37          16 :   OS << format("\n  Address area offset = 0x%x, has %" PRId64 " entries:",
      38           8 :                AddressAreaOffset, (uint64_t)AddressArea.size())
      39             :      << '\n';
      40          30 :   for (const AddressEntry &Addr : AddressArea)
      41          22 :     OS << format(
      42             :         "    Low/High address = [0x%llx, 0x%llx) (Size: 0x%llx), CU id = %d\n",
      43          11 :         Addr.LowAddress, Addr.HighAddress, Addr.HighAddress - Addr.LowAddress,
      44             :         Addr.CuIndex);
      45           8 : }
      46             : 
      47           8 : void DWARFGdbIndex::dumpSymbolTable(raw_ostream &OS) const {
      48          16 :   OS << format("\n  Symbol table offset = 0x%x, size = %" PRId64
      49             :                ", filled slots:",
      50           8 :                SymbolTableOffset, (uint64_t)SymbolTable.size())
      51             :      << '\n';
      52             :   uint32_t I = -1;
      53       16392 :   for (const SymTableEntry &E : SymbolTable) {
      54        8192 :     ++I;
      55        8192 :     if (!E.NameOffset && !E.VecOffset)
      56             :       continue;
      57             : 
      58          20 :     OS << format("    %d: Name offset = 0x%x, CU vector offset = 0x%x\n", I,
      59             :                  E.NameOffset, E.VecOffset);
      60             : 
      61             :     StringRef Name = ConstantPoolStrings.substr(
      62          10 :         ConstantPoolOffset - StringPoolOffset + E.NameOffset);
      63             : 
      64             :     auto CuVector = std::find_if(
      65             :         ConstantPoolVectors.begin(), ConstantPoolVectors.end(),
      66             :         [&](const std::pair<uint32_t, SmallVector<uint32_t, 0>> &V) {
      67          19 :           return V.first == E.VecOffset;
      68             :         });
      69             :     assert(CuVector != ConstantPoolVectors.end() && "Invalid symbol table");
      70          10 :     uint32_t CuVectorId = CuVector - ConstantPoolVectors.begin();
      71          10 :     OS << format("      String name: %s, CU vector index: %d\n", Name.data(),
      72             :                  CuVectorId);
      73             :   }
      74           8 : }
      75             : 
      76           8 : void DWARFGdbIndex::dumpConstantPool(raw_ostream &OS) const {
      77          16 :   OS << format("\n  Constant pool offset = 0x%x, has %" PRId64 " CU vectors:",
      78             :                ConstantPoolOffset, (uint64_t)ConstantPoolVectors.size());
      79             :   uint32_t I = 0;
      80          28 :   for (const auto &V : ConstantPoolVectors) {
      81          20 :     OS << format("\n    %d(0x%x): ", I++, V.first);
      82          36 :     for (uint32_t Val : V.second)
      83          13 :       OS << format("0x%x ", Val);
      84             :   }
      85             :   OS << '\n';
      86           8 : }
      87             : 
      88           8 : void DWARFGdbIndex::dump(raw_ostream &OS) {
      89           8 :   if (HasError) {
      90           0 :     OS << "\n<error parsing>\n";
      91           0 :     return;
      92             :   }
      93             : 
      94           8 :   if (HasContent) {
      95           8 :     OS << "  Version = " << Version << '\n';
      96           8 :     dumpCUList(OS);
      97           8 :     dumpAddressArea(OS);
      98           8 :     dumpSymbolTable(OS);
      99           8 :     dumpConstantPool(OS);
     100             :   }
     101             : }
     102             : 
     103           8 : bool DWARFGdbIndex::parseImpl(DataExtractor Data) {
     104           8 :   uint32_t Offset = 0;
     105             : 
     106             :   // Only version 7 is supported at this moment.
     107           8 :   Version = Data.getU32(&Offset);
     108           8 :   if (Version != 7)
     109             :     return false;
     110             : 
     111           8 :   CuListOffset = Data.getU32(&Offset);
     112           8 :   uint32_t CuTypesOffset = Data.getU32(&Offset);
     113           8 :   AddressAreaOffset = Data.getU32(&Offset);
     114           8 :   SymbolTableOffset = Data.getU32(&Offset);
     115           8 :   ConstantPoolOffset = Data.getU32(&Offset);
     116             : 
     117           8 :   if (Offset != CuListOffset)
     118             :     return false;
     119             : 
     120           8 :   uint32_t CuListSize = (CuTypesOffset - CuListOffset) / 16;
     121           8 :   CuList.reserve(CuListSize);
     122          30 :   for (uint32_t i = 0; i < CuListSize; ++i) {
     123          11 :     uint64_t CuOffset = Data.getU64(&Offset);
     124          11 :     uint64_t CuLength = Data.getU64(&Offset);
     125          11 :     CuList.push_back({CuOffset, CuLength});
     126             :   }
     127             : 
     128             :   // CU Types are no longer needed as DWARF skeleton type units never made it
     129             :   // into the standard.
     130           8 :   uint32_t CuTypesListSize = (AddressAreaOffset - CuTypesOffset) / 24;
     131           8 :   if (CuTypesListSize != 0)
     132             :     return false;
     133             : 
     134           8 :   uint32_t AddressAreaSize = (SymbolTableOffset - AddressAreaOffset) / 20;
     135           8 :   AddressArea.reserve(AddressAreaSize);
     136          30 :   for (uint32_t i = 0; i < AddressAreaSize; ++i) {
     137          11 :     uint64_t LowAddress = Data.getU64(&Offset);
     138          11 :     uint64_t HighAddress = Data.getU64(&Offset);
     139          11 :     uint32_t CuIndex = Data.getU32(&Offset);
     140          11 :     AddressArea.push_back({LowAddress, HighAddress, CuIndex});
     141             :   }
     142             : 
     143             :   // The symbol table. This is an open addressed hash table. The size of the
     144             :   // hash table is always a power of 2.
     145             :   // Each slot in the hash table consists of a pair of offset_type values. The
     146             :   // first value is the offset of the symbol's name in the constant pool. The
     147             :   // second value is the offset of the CU vector in the constant pool.
     148             :   // If both values are 0, then this slot in the hash table is empty. This is ok
     149             :   // because while 0 is a valid constant pool index, it cannot be a valid index
     150             :   // for both a string and a CU vector.
     151           8 :   uint32_t SymTableSize = (ConstantPoolOffset - SymbolTableOffset) / 8;
     152           8 :   SymbolTable.reserve(SymTableSize);
     153             :   uint32_t CuVectorsTotal = 0;
     154       16392 :   for (uint32_t i = 0; i < SymTableSize; ++i) {
     155        8192 :     uint32_t NameOffset = Data.getU32(&Offset);
     156        8192 :     uint32_t CuVecOffset = Data.getU32(&Offset);
     157        8192 :     SymbolTable.push_back({NameOffset, CuVecOffset});
     158        8192 :     if (NameOffset || CuVecOffset)
     159          10 :       ++CuVectorsTotal;
     160             :   }
     161             : 
     162             :   // The constant pool. CU vectors are stored first, followed by strings.
     163             :   // The first value is the number of CU indices in the vector. Each subsequent
     164             :   // value is the index and symbol attributes of a CU in the CU list.
     165          28 :   for (uint32_t i = 0; i < CuVectorsTotal; ++i) {
     166          30 :     ConstantPoolVectors.emplace_back(0, SmallVector<uint32_t, 0>());
     167          10 :     auto &Vec = ConstantPoolVectors.back();
     168          10 :     Vec.first = Offset - ConstantPoolOffset;
     169             : 
     170          10 :     uint32_t Num = Data.getU32(&Offset);
     171          36 :     for (uint32_t j = 0; j < Num; ++j)
     172          13 :       Vec.second.push_back(Data.getU32(&Offset));
     173             :   }
     174             : 
     175          16 :   ConstantPoolStrings = Data.getData().drop_front(Offset);
     176           8 :   StringPoolOffset = Offset;
     177           8 :   return true;
     178             : }
     179             : 
     180           8 : void DWARFGdbIndex::parse(DataExtractor Data) {
     181           8 :   HasContent = !Data.getData().empty();
     182           8 :   HasError = HasContent && !parseImpl(Data);
     183           8 : }

Generated by: LCOV version 1.13