2017-02-28 07:43:14 +08:00
|
|
|
//===- DWARFDebugAbbrev.cpp -----------------------------------------------===//
|
2011-09-14 03:42:23 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-01-31 02:07:45 +08:00
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
|
2011-09-14 03:42:23 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-02-28 07:43:14 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cinttypes>
|
|
|
|
#include <cstdint>
|
|
|
|
|
2011-09-14 03:42:23 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-26 05:10:56 +08:00
|
|
|
DWARFAbbreviationDeclarationSet::DWARFAbbreviationDeclarationSet() {
|
2011-09-14 03:42:23 +08:00
|
|
|
clear();
|
2014-04-26 05:10:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DWARFAbbreviationDeclarationSet::clear() {
|
|
|
|
Offset = 0;
|
|
|
|
FirstAbbrCode = 0;
|
|
|
|
Decls.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DWARFAbbreviationDeclarationSet::extract(DataExtractor Data,
|
|
|
|
uint32_t *OffsetPtr) {
|
|
|
|
clear();
|
|
|
|
const uint32_t BeginOffset = *OffsetPtr;
|
|
|
|
Offset = BeginOffset;
|
|
|
|
DWARFAbbreviationDeclaration AbbrDecl;
|
|
|
|
uint32_t PrevAbbrCode = 0;
|
|
|
|
while (AbbrDecl.extract(Data, OffsetPtr)) {
|
|
|
|
if (FirstAbbrCode == 0) {
|
|
|
|
FirstAbbrCode = AbbrDecl.getCode();
|
2011-09-14 03:42:23 +08:00
|
|
|
} else {
|
2014-04-26 05:10:56 +08:00
|
|
|
if (PrevAbbrCode + 1 != AbbrDecl.getCode()) {
|
|
|
|
// Codes are not consecutive, can't do O(1) lookups.
|
|
|
|
FirstAbbrCode = UINT32_MAX;
|
|
|
|
}
|
2011-09-14 03:42:23 +08:00
|
|
|
}
|
2014-04-26 05:10:56 +08:00
|
|
|
PrevAbbrCode = AbbrDecl.getCode();
|
2014-10-05 00:55:56 +08:00
|
|
|
Decls.push_back(std::move(AbbrDecl));
|
2011-09-14 03:42:23 +08:00
|
|
|
}
|
2014-04-26 05:10:56 +08:00
|
|
|
return BeginOffset != *OffsetPtr;
|
2011-09-14 03:42:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DWARFAbbreviationDeclarationSet::dump(raw_ostream &OS) const {
|
2014-03-13 15:52:54 +08:00
|
|
|
for (const auto &Decl : Decls)
|
|
|
|
Decl.dump(OS);
|
2011-09-14 03:42:23 +08:00
|
|
|
}
|
|
|
|
|
2014-04-26 05:10:56 +08:00
|
|
|
const DWARFAbbreviationDeclaration *
|
|
|
|
DWARFAbbreviationDeclarationSet::getAbbreviationDeclaration(
|
|
|
|
uint32_t AbbrCode) const {
|
|
|
|
if (FirstAbbrCode == UINT32_MAX) {
|
2014-03-13 15:52:54 +08:00
|
|
|
for (const auto &Decl : Decls) {
|
2014-04-26 05:10:56 +08:00
|
|
|
if (Decl.getCode() == AbbrCode)
|
2014-03-13 15:52:54 +08:00
|
|
|
return &Decl;
|
2011-09-14 03:42:23 +08:00
|
|
|
}
|
2014-04-26 05:10:56 +08:00
|
|
|
return nullptr;
|
2011-09-14 03:42:23 +08:00
|
|
|
}
|
2014-04-26 05:10:56 +08:00
|
|
|
if (AbbrCode < FirstAbbrCode || AbbrCode >= FirstAbbrCode + Decls.size())
|
|
|
|
return nullptr;
|
|
|
|
return &Decls[AbbrCode - FirstAbbrCode];
|
2011-09-14 03:42:23 +08:00
|
|
|
}
|
|
|
|
|
2014-04-26 05:10:56 +08:00
|
|
|
DWARFDebugAbbrev::DWARFDebugAbbrev() {
|
|
|
|
clear();
|
|
|
|
}
|
2011-09-14 03:42:23 +08:00
|
|
|
|
2014-04-26 05:10:56 +08:00
|
|
|
void DWARFDebugAbbrev::clear() {
|
|
|
|
AbbrDeclSets.clear();
|
|
|
|
PrevAbbrOffsetPos = AbbrDeclSets.end();
|
|
|
|
}
|
2011-09-14 03:42:23 +08:00
|
|
|
|
2014-04-26 05:10:56 +08:00
|
|
|
void DWARFDebugAbbrev::extract(DataExtractor Data) {
|
|
|
|
clear();
|
2011-09-14 03:42:23 +08:00
|
|
|
|
2014-04-26 05:10:56 +08:00
|
|
|
uint32_t Offset = 0;
|
|
|
|
DWARFAbbreviationDeclarationSet AbbrDecls;
|
|
|
|
while (Data.isValidOffset(Offset)) {
|
|
|
|
uint32_t CUAbbrOffset = Offset;
|
|
|
|
if (!AbbrDecls.extract(Data, &Offset))
|
2011-09-14 03:42:23 +08:00
|
|
|
break;
|
2014-10-05 00:55:56 +08:00
|
|
|
AbbrDeclSets[CUAbbrOffset] = std::move(AbbrDecls);
|
2011-09-14 03:42:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DWARFDebugAbbrev::dump(raw_ostream &OS) const {
|
2014-04-26 05:10:56 +08:00
|
|
|
if (AbbrDeclSets.empty()) {
|
2011-09-14 03:42:23 +08:00
|
|
|
OS << "< EMPTY >\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-26 05:10:56 +08:00
|
|
|
for (const auto &I : AbbrDeclSets) {
|
2014-03-13 15:52:54 +08:00
|
|
|
OS << format("Abbrev table for offset: 0x%8.8" PRIx64 "\n", I.first);
|
|
|
|
I.second.dump(OS);
|
2011-09-14 03:42:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const DWARFAbbreviationDeclarationSet*
|
2014-04-25 06:41:09 +08:00
|
|
|
DWARFDebugAbbrev::getAbbreviationDeclarationSet(uint64_t CUAbbrOffset) const {
|
2014-04-26 05:10:56 +08:00
|
|
|
const auto End = AbbrDeclSets.end();
|
2014-04-25 06:41:09 +08:00
|
|
|
if (PrevAbbrOffsetPos != End && PrevAbbrOffsetPos->first == CUAbbrOffset) {
|
2011-09-14 05:47:32 +08:00
|
|
|
return &(PrevAbbrOffsetPos->second);
|
2011-09-14 03:42:23 +08:00
|
|
|
}
|
|
|
|
|
2014-04-26 05:10:56 +08:00
|
|
|
const auto Pos = AbbrDeclSets.find(CUAbbrOffset);
|
2014-04-25 06:41:09 +08:00
|
|
|
if (Pos != End) {
|
|
|
|
PrevAbbrOffsetPos = Pos;
|
|
|
|
return &(Pos->second);
|
|
|
|
}
|
|
|
|
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2011-09-14 03:42:23 +08:00
|
|
|
}
|