summaryrefslogtreecommitdiff
path: root/lib/DebugInfo/DWARFAbbreviationDeclaration.cpp
blob: c3e570e14ccd93dbb0445e1b757552fda074a1ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//===-- DWARFAbbreviationDeclaration.cpp ----------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "DWARFAbbreviationDeclaration.h"
#include "llvm/Support/Dwarf.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
using namespace dwarf;

void DWARFAbbreviationDeclaration::clear() {
  Code = 0;
  Tag = 0;
  HasChildren = false;
  AttributeSpecs.clear();
}

DWARFAbbreviationDeclaration::DWARFAbbreviationDeclaration() {
  clear();
}

bool
DWARFAbbreviationDeclaration::extract(DataExtractor Data, uint32_t* OffsetPtr) {
  clear();
  Code = Data.getULEB128(OffsetPtr);
  if (Code == 0) {
    return false;
  }
  Tag = Data.getULEB128(OffsetPtr);
  uint8_t ChildrenByte = Data.getU8(OffsetPtr);
  HasChildren = (ChildrenByte == DW_CHILDREN_yes);

  while (true) {
    uint32_t CurOffset = *OffsetPtr;
    uint16_t Attr = Data.getULEB128(OffsetPtr);
    if (CurOffset == *OffsetPtr) {
      clear();
      return false;
    }
    CurOffset = *OffsetPtr;
    uint16_t Form = Data.getULEB128(OffsetPtr);
    if (CurOffset == *OffsetPtr) {
      clear();
      return false;
    }
    if (Attr == 0 && Form == 0)
      break;
    AttributeSpecs.push_back(AttributeSpec(Attr, Form));
  }

  if (Tag == 0) {
    clear();
    return false;
  }
  return true;
}

void DWARFAbbreviationDeclaration::dump(raw_ostream &OS) const {
  const char *tagString = TagString(getTag());
  OS << '[' << getCode() << "] ";
  if (tagString)
    OS << tagString;
  else
    OS << format("DW_TAG_Unknown_%x", getTag());
  OS << "\tDW_CHILDREN_" << (hasChildren() ? "yes" : "no") << '\n';
  for (const AttributeSpec &Spec : AttributeSpecs) {
    OS << '\t';
    const char *attrString = AttributeString(Spec.Attr);
    if (attrString)
      OS << attrString;
    else
      OS << format("DW_AT_Unknown_%x", Spec.Attr);
    OS << '\t';
    const char *formString = FormEncodingString(Spec.Form);
    if (formString)
      OS << formString;
    else
      OS << format("DW_FORM_Unknown_%x", Spec.Form);
    OS << '\n';
  }
  OS << '\n';
}

uint32_t
DWARFAbbreviationDeclaration::findAttributeIndex(uint16_t attr) const {
  for (uint32_t i = 0, e = AttributeSpecs.size(); i != e; ++i) {
    if (AttributeSpecs[i].Attr == attr)
      return i;
  }
  return -1U;
}