LCOV - code coverage report
Current view: top level - lib/DebugInfo/DWARF - DWARFGdbIndex.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 99 101 98.0 %
Date: 2017-09-14 15:23:50 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           7 : void DWARFGdbIndex::dumpCUList(raw_ostream &OS) const {
      27          21 :   OS << format("\n  CU list offset = 0x%x, has %" PRId64 " entries:",
      28          21 :                CuListOffset, (uint64_t)CuList.size())
      29           7 :      << '\n';
      30           7 :   uint32_t I = 0;
      31          30 :   for (const CompUnitEntry &CU : CuList)
      32          18 :     OS << format("    %d: Offset = 0x%llx, Length = 0x%llx\n", I++, CU.Offset,
      33             :                  CU.Length);
      34           7 : }
      35             : 
      36           7 : void DWARFGdbIndex::dumpAddressArea(raw_ostream &OS) const {
      37          21 :   OS << format("\n  Address area offset = 0x%x, has %" PRId64 " entries:",
      38          21 :                AddressAreaOffset, (uint64_t)AddressArea.size())
      39           7 :      << '\n';
      40          30 :   for (const AddressEntry &Addr : AddressArea)
      41          27 :     OS << format(
      42             :         "    Low/High address = [0x%llx, 0x%llx) (Size: 0x%llx), CU id = %d\n",
      43           9 :         Addr.LowAddress, Addr.HighAddress, Addr.HighAddress - Addr.LowAddress,
      44             :         Addr.CuIndex);
      45           7 : }
      46             : 
      47           7 : void DWARFGdbIndex::dumpSymbolTable(raw_ostream &OS) const {
      48          21 :   OS << format("\n  Symbol table offset = 0x%x, size = %" PRId64
      49             :                ", filled slots:",
      50          21 :                SymbolTableOffset, (uint64_t)SymbolTable.size())
      51           7 :      << '\n';
      52           7 :   uint32_t I = -1;
      53        7189 :   for (const SymTableEntry &E : SymbolTable) {
      54        7168 :     ++I;
      55        7168 :     if (!E.NameOffset && !E.VecOffset)
      56        7161 :       continue;
      57             : 
      58          14 :     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          14 :         ConstantPoolOffset - StringPoolOffset + E.NameOffset);
      63             : 
      64          21 :     auto CuVector = std::find_if(
      65             :         ConstantPoolVectors.begin(), ConstantPoolVectors.end(),
      66             :         [&](const std::pair<uint32_t, SmallVector<uint32_t, 0>> &V) {
      67          13 :           return V.first == E.VecOffset;
      68           7 :         });
      69             :     assert(CuVector != ConstantPoolVectors.end() && "Invalid symbol table");
      70           7 :     uint32_t CuVectorId = CuVector - ConstantPoolVectors.begin();
      71          14 :     OS << format("      String name: %s, CU vector index: %d\n", Name.data(),
      72             :                  CuVectorId);
      73             :   }
      74           7 : }
      75             : 
      76           7 : void DWARFGdbIndex::dumpConstantPool(raw_ostream &OS) const {
      77          14 :   OS << format("\n  Constant pool offset = 0x%x, has %" PRId64 " CU vectors:",
      78          14 :                ConstantPoolOffset, (uint64_t)ConstantPoolVectors.size());
      79           7 :   uint32_t I = 0;
      80          28 :   for (const auto &V : ConstantPoolVectors) {
      81          14 :     OS << format("\n    %d(0x%x): ", I++, V.first);
      82          30 :     for (uint32_t Val : V.second)
      83          18 :       OS << format("0x%x ", Val);
      84             :   }
      85           7 :   OS << '\n';
      86           7 : }
      87             : 
      88           7 : void DWARFGdbIndex::dump(raw_ostream &OS) {
      89           7 :   if (HasError) {
      90           0 :     OS << "\n<error parsing>\n";
      91           0 :     return;
      92             :   }
      93             : 
      94           7 :   if (HasContent) {
      95          21 :     OS << "  Version = " << Version << '\n';
      96           7 :     dumpCUList(OS);
      97           7 :     dumpAddressArea(OS);
      98           7 :     dumpSymbolTable(OS);
      99           7 :     dumpConstantPool(OS);
     100             :   }
     101             : }
     102             : 
     103           7 : bool DWARFGdbIndex::parseImpl(DataExtractor Data) {
     104           7 :   uint32_t Offset = 0;
     105             : 
     106             :   // Only version 7 is supported at this moment.
     107           7 :   Version = Data.getU32(&Offset);
     108           7 :   if (Version != 7)
     109             :     return false;
     110             : 
     111           7 :   CuListOffset = Data.getU32(&Offset);
     112           7 :   uint32_t CuTypesOffset = Data.getU32(&Offset);
     113           7 :   AddressAreaOffset = Data.getU32(&Offset);
     114           7 :   SymbolTableOffset = Data.getU32(&Offset);
     115           7 :   ConstantPoolOffset = Data.getU32(&Offset);
     116             : 
     117           7 :   if (Offset != CuListOffset)
     118             :     return false;
     119             : 
     120           7 :   uint32_t CuListSize = (CuTypesOffset - CuListOffset) / 16;
     121           7 :   CuList.reserve(CuListSize);
     122          16 :   for (uint32_t i = 0; i < CuListSize; ++i) {
     123           9 :     uint64_t CuOffset = Data.getU64(&Offset);
     124           9 :     uint64_t CuLength = Data.getU64(&Offset);
     125           9 :     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           7 :   uint32_t CuTypesListSize = (AddressAreaOffset - CuTypesOffset) / 24;
     131           7 :   if (CuTypesListSize != 0)
     132             :     return false;
     133             : 
     134           7 :   uint32_t AddressAreaSize = (SymbolTableOffset - AddressAreaOffset) / 20;
     135           7 :   AddressArea.reserve(AddressAreaSize);
     136          16 :   for (uint32_t i = 0; i < AddressAreaSize; ++i) {
     137           9 :     uint64_t LowAddress = Data.getU64(&Offset);
     138           9 :     uint64_t HighAddress = Data.getU64(&Offset);
     139           9 :     uint32_t CuIndex = Data.getU32(&Offset);
     140           9 :     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           7 :   uint32_t SymTableSize = (ConstantPoolOffset - SymbolTableOffset) / 8;
     152           7 :   SymbolTable.reserve(SymTableSize);
     153           7 :   uint32_t CuVectorsTotal = 0;
     154        7175 :   for (uint32_t i = 0; i < SymTableSize; ++i) {
     155        7168 :     uint32_t NameOffset = Data.getU32(&Offset);
     156        7168 :     uint32_t CuVecOffset = Data.getU32(&Offset);
     157        7168 :     SymbolTable.push_back({NameOffset, CuVecOffset});
     158        7168 :     if (NameOffset || CuVecOffset)
     159           7 :       ++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          21 :   for (uint32_t i = 0; i < CuVectorsTotal; ++i) {
     166          21 :     ConstantPoolVectors.emplace_back(0, SmallVector<uint32_t, 0>());
     167          14 :     auto &Vec = ConstantPoolVectors.back();
     168           7 :     Vec.first = Offset - ConstantPoolOffset;
     169             : 
     170           7 :     uint32_t Num = Data.getU32(&Offset);
     171          16 :     for (uint32_t j = 0; j < Num; ++j)
     172           9 :       Vec.second.push_back(Data.getU32(&Offset));
     173             :   }
     174             : 
     175          14 :   ConstantPoolStrings = Data.getData().drop_front(Offset);
     176           7 :   StringPoolOffset = Offset;
     177           7 :   return true;
     178             : }
     179             : 
     180           7 : void DWARFGdbIndex::parse(DataExtractor Data) {
     181           7 :   HasContent = !Data.getData().empty();
     182           7 :   HasError = HasContent && !parseImpl(Data);
     183           7 : }

Generated by: LCOV version 1.13