brintos

brintos / llvm-project-archived public Read only

0
0
Text · 7.4 KiB · db6170c Raw
209 lines · cpp
1//===- DWARFGdbIndex.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-exception6//7//===----------------------------------------------------------------------===//8 9#include "llvm/DebugInfo/DWARF/DWARFGdbIndex.h"10#include "llvm/ADT/SmallVector.h"11#include "llvm/ADT/StringRef.h"12#include "llvm/Support/DataExtractor.h"13#include "llvm/Support/Format.h"14#include "llvm/Support/FormatVariadic.h"15#include "llvm/Support/raw_ostream.h"16#include <cassert>17#include <cinttypes>18#include <cstdint>19#include <set>20 21using namespace llvm;22 23// .gdb_index section format reference:24// https://sourceware.org/gdb/onlinedocs/gdb/Index-Section-Format.html25 26void DWARFGdbIndex::dumpCUList(raw_ostream &OS) const {27  OS << format("\n  CU list offset = 0x%x, has %" PRId64 " entries:",28               CuListOffset, (uint64_t)CuList.size())29     << '\n';30  uint32_t I = 0;31  for (const CompUnitEntry &CU : CuList)32    OS << format("    %d: Offset = 0x%llx, Length = 0x%llx\n", I++, CU.Offset,33                 CU.Length);34}35 36void DWARFGdbIndex::dumpTUList(raw_ostream &OS) const {37  OS << formatv("\n  Types CU list offset = {0:x}, has {1} entries:\n",38                TuListOffset, TuList.size());39  uint32_t I = 0;40  for (const TypeUnitEntry &TU : TuList)41    OS << formatv("    {0}: offset = {1:x8}, type_offset = {2:x8}, "42                  "type_signature = {3:x16}\n",43                  I++, TU.Offset, TU.TypeOffset, TU.TypeSignature);44}45 46void DWARFGdbIndex::dumpAddressArea(raw_ostream &OS) const {47  OS << format("\n  Address area offset = 0x%x, has %" PRId64 " entries:",48               AddressAreaOffset, (uint64_t)AddressArea.size())49     << '\n';50  for (const AddressEntry &Addr : AddressArea)51    OS << format(52        "    Low/High address = [0x%llx, 0x%llx) (Size: 0x%llx), CU id = %d\n",53        Addr.LowAddress, Addr.HighAddress, Addr.HighAddress - Addr.LowAddress,54        Addr.CuIndex);55}56 57void DWARFGdbIndex::dumpSymbolTable(raw_ostream &OS) const {58  OS << format("\n  Symbol table offset = 0x%x, size = %" PRId6459               ", filled slots:",60               SymbolTableOffset, (uint64_t)SymbolTable.size())61     << '\n';62 63  const auto FindCuVectorId = [&](uint32_t VecOffset) {64    // Entries in ConstantPoolVectors are sorted by their offset in constant65    // pool, see how ConstantPoolVectors is populated in parseImpl.66    const auto *It =67        llvm::lower_bound(ConstantPoolVectors, VecOffset,68                          [](const auto &ConstantPoolEntry, uint32_t Offset) {69                            return ConstantPoolEntry.first < Offset;70                          });71    assert(It != ConstantPoolVectors.end() && It->first == VecOffset &&72           "Invalid symbol table");73    return It - ConstantPoolVectors.begin();74  };75 76  uint32_t I = -1;77  for (const SymTableEntry &E : SymbolTable) {78    ++I;79    if (!E.NameOffset && !E.VecOffset)80      continue;81 82    OS << format("    %d: Name offset = 0x%x, CU vector offset = 0x%x\n", I,83                 E.NameOffset, E.VecOffset);84 85    StringRef Name = ConstantPoolStrings.substr(86        ConstantPoolOffset - StringPoolOffset + E.NameOffset);87 88    const uint32_t CuVectorId = FindCuVectorId(E.VecOffset);89    OS << format("      String name: %s, CU vector index: %d\n", Name.data(),90                 CuVectorId);91  }92}93 94void DWARFGdbIndex::dumpConstantPool(raw_ostream &OS) const {95  OS << format("\n  Constant pool offset = 0x%x, has %" PRId64 " CU vectors:",96               ConstantPoolOffset, (uint64_t)ConstantPoolVectors.size());97  uint32_t I = 0;98  for (const auto &V : ConstantPoolVectors) {99    OS << format("\n    %d(0x%x): ", I++, V.first);100    for (uint32_t Val : V.second)101      OS << format("0x%x ", Val);102  }103  OS << '\n';104}105 106void DWARFGdbIndex::dump(raw_ostream &OS) {107  if (HasError) {108    OS << "\n<error parsing>\n";109    return;110  }111 112  if (HasContent) {113    OS << "  Version = " << Version << '\n';114    dumpCUList(OS);115    dumpTUList(OS);116    dumpAddressArea(OS);117    dumpSymbolTable(OS);118    dumpConstantPool(OS);119  }120}121 122bool DWARFGdbIndex::parseImpl(DataExtractor Data) {123  uint64_t Offset = 0;124 125  // Only version 7 and 8 are supported at this moment.126  Version = Data.getU32(&Offset);127  if (Version != 7 && Version != 8)128    return false;129 130  CuListOffset = Data.getU32(&Offset);131  TuListOffset = Data.getU32(&Offset);132  AddressAreaOffset = Data.getU32(&Offset);133  SymbolTableOffset = Data.getU32(&Offset);134  ConstantPoolOffset = Data.getU32(&Offset);135 136  if (Offset != CuListOffset)137    return false;138 139  uint32_t CuListSize = (TuListOffset - CuListOffset) / 16;140  CuList.reserve(CuListSize);141  for (uint32_t i = 0; i < CuListSize; ++i) {142    uint64_t CuOffset = Data.getU64(&Offset);143    uint64_t CuLength = Data.getU64(&Offset);144    CuList.push_back({CuOffset, CuLength});145  }146 147  // CU Types are no longer needed as DWARF skeleton type units never made it148  // into the standard.149  uint32_t TuListSize = (AddressAreaOffset - TuListOffset) / 24;150  TuList.resize(TuListSize);151  for (uint32_t I = 0; I < TuListSize; ++I) {152    uint64_t CuOffset = Data.getU64(&Offset);153    uint64_t TypeOffset = Data.getU64(&Offset);154    uint64_t Signature = Data.getU64(&Offset);155    TuList[I] = {CuOffset, TypeOffset, Signature};156  }157 158  uint32_t AddressAreaSize = (SymbolTableOffset - AddressAreaOffset) / 20;159  AddressArea.reserve(AddressAreaSize);160  for (uint32_t i = 0; i < AddressAreaSize; ++i) {161    uint64_t LowAddress = Data.getU64(&Offset);162    uint64_t HighAddress = Data.getU64(&Offset);163    uint32_t CuIndex = Data.getU32(&Offset);164    AddressArea.push_back({LowAddress, HighAddress, CuIndex});165  }166 167  // The symbol table. This is an open addressed hash table. The size of the168  // hash table is always a power of 2.169  // Each slot in the hash table consists of a pair of offset_type values. The170  // first value is the offset of the symbol's name in the constant pool. The171  // second value is the offset of the CU vector in the constant pool.172  // If both values are 0, then this slot in the hash table is empty. This is ok173  // because while 0 is a valid constant pool index, it cannot be a valid index174  // for both a string and a CU vector.175  uint32_t SymTableSize = (ConstantPoolOffset - SymbolTableOffset) / 8;176  SymbolTable.reserve(SymTableSize);177  std::set<uint32_t> CUOffsets;178  for (uint32_t i = 0; i < SymTableSize; ++i) {179    uint32_t NameOffset = Data.getU32(&Offset);180    uint32_t CuVecOffset = Data.getU32(&Offset);181    SymbolTable.push_back({NameOffset, CuVecOffset});182    if (NameOffset || CuVecOffset)183      CUOffsets.insert(CuVecOffset);184  }185 186  // The constant pool. CU vectors are stored first, followed by strings.187  // The first value is the number of CU indices in the vector. Each subsequent188  // value is the index and symbol attributes of a CU in the CU list.189  for (auto CUOffset : CUOffsets) {190    Offset = ConstantPoolOffset + CUOffset;191    ConstantPoolVectors.emplace_back(0, SmallVector<uint32_t, 0>());192    auto &Vec = ConstantPoolVectors.back();193    Vec.first = Offset - ConstantPoolOffset;194 195    uint32_t Num = Data.getU32(&Offset);196    for (uint32_t J = 0; J < Num; ++J)197      Vec.second.push_back(Data.getU32(&Offset));198  }199 200  ConstantPoolStrings = Data.getData().drop_front(Offset);201  StringPoolOffset = Offset;202  return true;203}204 205void DWARFGdbIndex::parse(DataExtractor Data) {206  HasContent = !Data.getData().empty();207  HasError = HasContent && !parseImpl(Data);208}209