Move getBaseSymbol somewhere the COFF writer can use.
[oota-llvm.git] / lib / DebugInfo / DWARFDebugInfoEntry.cpp
1 //===-- DWARFDebugInfoEntry.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 "DWARFDebugInfoEntry.h"
11 #include "DWARFCompileUnit.h"
12 #include "DWARFContext.h"
13 #include "DWARFDebugAbbrev.h"
14 #include "llvm/DebugInfo/DWARFFormValue.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/Dwarf.h"
17 #include "llvm/Support/Format.h"
18 #include "llvm/Support/raw_ostream.h"
19 using namespace llvm;
20 using namespace dwarf;
21
22 void DWARFDebugInfoEntryMinimal::dump(raw_ostream &OS, const DWARFUnit *u,
23                                       unsigned recurseDepth,
24                                       unsigned indent) const {
25   DataExtractor debug_info_data = u->getDebugInfoExtractor();
26   uint32_t offset = Offset;
27
28   if (debug_info_data.isValidOffset(offset)) {
29     uint32_t abbrCode = debug_info_data.getULEB128(&offset);
30
31     OS << format("\n0x%8.8x: ", Offset);
32     if (abbrCode) {
33       if (AbbrevDecl) {
34         const char *tagString = TagString(getTag());
35         if (tagString)
36           OS.indent(indent) << tagString;
37         else
38           OS.indent(indent) << format("DW_TAG_Unknown_%x", getTag());
39         OS << format(" [%u] %c\n", abbrCode,
40                      AbbrevDecl->hasChildren() ? '*' : ' ');
41
42         // Dump all data in the DIE for the attributes.
43         for (const auto &AttrSpec : AbbrevDecl->attributes()) {
44           dumpAttribute(OS, u, &offset, AttrSpec.Attr, AttrSpec.Form, indent);
45         }
46
47         const DWARFDebugInfoEntryMinimal *child = getFirstChild();
48         if (recurseDepth > 0 && child) {
49           while (child) {
50             child->dump(OS, u, recurseDepth-1, indent+2);
51             child = child->getSibling();
52           }
53         }
54       } else {
55         OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
56            << abbrCode << '\n';
57       }
58     } else {
59       OS.indent(indent) << "NULL\n";
60     }
61   }
62 }
63
64 void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS,
65                                                const DWARFUnit *u,
66                                                uint32_t *offset_ptr,
67                                                uint16_t attr, uint16_t form,
68                                                unsigned indent) const {
69   OS << "            ";
70   OS.indent(indent+2);
71   const char *attrString = AttributeString(attr);
72   if (attrString)
73     OS << attrString;
74   else
75     OS << format("DW_AT_Unknown_%x", attr);
76   const char *formString = FormEncodingString(form);
77   if (formString)
78     OS << " [" << formString << ']';
79   else
80     OS << format(" [DW_FORM_Unknown_%x]", form);
81
82   DWARFFormValue formValue(form);
83
84   if (!formValue.extractValue(u->getDebugInfoExtractor(), offset_ptr, u))
85     return;
86
87   OS << "\t(";
88   formValue.dump(OS, u);
89   OS << ")\n";
90 }
91
92 bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFUnit *U,
93                                              uint32_t *OffsetPtr) {
94   Offset = *OffsetPtr;
95   DataExtractor DebugInfoData = U->getDebugInfoExtractor();
96   uint32_t UEndOffset = U->getNextUnitOffset();
97   if (Offset >= UEndOffset || !DebugInfoData.isValidOffset(Offset))
98     return false;
99   uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
100   if (0 == AbbrCode) {
101     // NULL debug tag entry.
102     AbbrevDecl = nullptr;
103     return true;
104   }
105   AbbrevDecl = U->getAbbreviations()->getAbbreviationDeclaration(AbbrCode);
106   if (nullptr == AbbrevDecl) {
107     // Restore the original offset.
108     *OffsetPtr = Offset;
109     return false;
110   }
111   ArrayRef<uint8_t> FixedFormSizes = DWARFFormValue::getFixedFormSizes(
112       U->getAddressByteSize(), U->getVersion());
113   assert(FixedFormSizes.size() > 0);
114
115   // Skip all data in the .debug_info for the attributes
116   for (const auto &AttrSpec : AbbrevDecl->attributes()) {
117     uint16_t Form = AttrSpec.Form;
118
119     uint8_t FixedFormSize =
120         (Form < FixedFormSizes.size()) ? FixedFormSizes[Form] : 0;
121     if (FixedFormSize)
122       *OffsetPtr += FixedFormSize;
123     else if (!DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, U)) {
124       // Restore the original offset.
125       *OffsetPtr = Offset;
126       return false;
127     }
128   }
129   return true;
130 }
131
132 bool DWARFDebugInfoEntryMinimal::isSubprogramDIE() const {
133   return getTag() == DW_TAG_subprogram;
134 }
135
136 bool DWARFDebugInfoEntryMinimal::isSubroutineDIE() const {
137   uint32_t Tag = getTag();
138   return Tag == DW_TAG_subprogram ||
139          Tag == DW_TAG_inlined_subroutine;
140 }
141
142 bool DWARFDebugInfoEntryMinimal::getAttributeValue(
143     const DWARFUnit *U, const uint16_t Attr, DWARFFormValue &FormValue) const {
144   if (!AbbrevDecl)
145     return false;
146
147   uint32_t AttrIdx = AbbrevDecl->findAttributeIndex(Attr);
148   if (AttrIdx == -1U)
149     return false;
150
151   DataExtractor DebugInfoData = U->getDebugInfoExtractor();
152   uint32_t DebugInfoOffset = getOffset();
153
154   // Skip the abbreviation code so we are at the data for the attributes
155   DebugInfoData.getULEB128(&DebugInfoOffset);
156
157   // Skip preceding attribute values.
158   for (uint32_t i = 0; i < AttrIdx; ++i) {
159     DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(i),
160                               DebugInfoData, &DebugInfoOffset, U);
161   }
162
163   FormValue = DWARFFormValue(AbbrevDecl->getFormByIndex(AttrIdx));
164   return FormValue.extractValue(DebugInfoData, &DebugInfoOffset, U);
165 }
166
167 const char *DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
168     const DWARFUnit *U, const uint16_t Attr, const char *FailValue) const {
169   DWARFFormValue FormValue;
170   if (!getAttributeValue(U, Attr, FormValue))
171     return FailValue;
172   Optional<const char *> Result = FormValue.getAsCString(U);
173   return Result.hasValue() ? Result.getValue() : FailValue;
174 }
175
176 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsAddress(
177     const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
178   DWARFFormValue FormValue;
179   if (!getAttributeValue(U, Attr, FormValue))
180     return FailValue;
181   Optional<uint64_t> Result = FormValue.getAsAddress(U);
182   return Result.hasValue() ? Result.getValue() : FailValue;
183 }
184
185 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsignedConstant(
186     const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
187   DWARFFormValue FormValue;
188   if (!getAttributeValue(U, Attr, FormValue))
189     return FailValue;
190   Optional<uint64_t> Result = FormValue.getAsUnsignedConstant();
191   return Result.hasValue() ? Result.getValue() : FailValue;
192 }
193
194 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
195     const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
196   DWARFFormValue FormValue;
197   if (!getAttributeValue(U, Attr, FormValue))
198     return FailValue;
199   Optional<uint64_t> Result = FormValue.getAsReference(U);
200   return Result.hasValue() ? Result.getValue() : FailValue;
201 }
202
203 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsSectionOffset(
204     const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
205   DWARFFormValue FormValue;
206   if (!getAttributeValue(U, Attr, FormValue))
207     return FailValue;
208   Optional<uint64_t> Result = FormValue.getAsSectionOffset();
209   return Result.hasValue() ? Result.getValue() : FailValue;
210 }
211
212 bool DWARFDebugInfoEntryMinimal::getLowAndHighPC(const DWARFUnit *U,
213                                                  uint64_t &LowPC,
214                                                  uint64_t &HighPC) const {
215   LowPC = getAttributeValueAsAddress(U, DW_AT_low_pc, -1ULL);
216   if (LowPC == -1ULL)
217     return false;
218   HighPC = getAttributeValueAsAddress(U, DW_AT_high_pc, -1ULL);
219   if (HighPC == -1ULL) {
220     // Since DWARF4, DW_AT_high_pc may also be of class constant, in which case
221     // it represents function size.
222     HighPC = getAttributeValueAsUnsignedConstant(U, DW_AT_high_pc, -1ULL);
223     if (HighPC != -1ULL)
224       HighPC += LowPC;
225   }
226   return (HighPC != -1ULL);
227 }
228
229 DWARFAddressRangesVector
230 DWARFDebugInfoEntryMinimal::getAddressRanges(const DWARFUnit *U) const {
231   if (isNULL())
232     return DWARFAddressRangesVector();
233   // Single range specified by low/high PC.
234   uint64_t LowPC, HighPC;
235   if (getLowAndHighPC(U, LowPC, HighPC)) {
236     return DWARFAddressRangesVector(1, std::make_pair(LowPC, HighPC));
237   }
238   // Multiple ranges from .debug_ranges section.
239   uint32_t RangesOffset =
240       getAttributeValueAsSectionOffset(U, DW_AT_ranges, -1U);
241   if (RangesOffset != -1U) {
242     DWARFDebugRangeList RangeList;
243     if (U->extractRangeList(RangesOffset, RangeList))
244       return RangeList.getAbsoluteRanges(U->getBaseAddress());
245   }
246   return DWARFAddressRangesVector();
247 }
248
249 void DWARFDebugInfoEntryMinimal::collectChildrenAddressRanges(
250     const DWARFUnit *U, DWARFAddressRangesVector& Ranges) const {
251   if (isNULL())
252     return;
253   if (isSubprogramDIE()) {
254     const auto &DIERanges = getAddressRanges(U);
255     Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
256   }
257
258   const DWARFDebugInfoEntryMinimal *Child = getFirstChild();
259   while (Child) {
260     Child->collectChildrenAddressRanges(U, Ranges);
261     Child = Child->getSibling();
262   }
263 }
264
265 bool DWARFDebugInfoEntryMinimal::addressRangeContainsAddress(
266     const DWARFUnit *U, const uint64_t Address) const {
267   for (const auto& R : getAddressRanges(U)) {
268     if (R.first <= Address && Address < R.second)
269       return true;
270   }
271   return false;
272 }
273
274 const char *
275 DWARFDebugInfoEntryMinimal::getSubroutineName(const DWARFUnit *U) const {
276   if (!isSubroutineDIE())
277     return nullptr;
278   // Try to get mangled name if possible.
279   if (const char *name =
280       getAttributeValueAsString(U, DW_AT_MIPS_linkage_name, nullptr))
281     return name;
282   if (const char *name = getAttributeValueAsString(U, DW_AT_linkage_name,
283                                                    nullptr))
284     return name;
285   if (const char *name = getAttributeValueAsString(U, DW_AT_name, nullptr))
286     return name;
287   // Try to get name from specification DIE.
288   uint32_t spec_ref =
289       getAttributeValueAsReference(U, DW_AT_specification, -1U);
290   if (spec_ref != -1U) {
291     DWARFDebugInfoEntryMinimal spec_die;
292     if (spec_die.extractFast(U, &spec_ref)) {
293       if (const char *name = spec_die.getSubroutineName(U))
294         return name;
295     }
296   }
297   // Try to get name from abstract origin DIE.
298   uint32_t abs_origin_ref =
299       getAttributeValueAsReference(U, DW_AT_abstract_origin, -1U);
300   if (abs_origin_ref != -1U) {
301     DWARFDebugInfoEntryMinimal abs_origin_die;
302     if (abs_origin_die.extractFast(U, &abs_origin_ref)) {
303       if (const char *name = abs_origin_die.getSubroutineName(U))
304         return name;
305     }
306   }
307   return nullptr;
308 }
309
310 void DWARFDebugInfoEntryMinimal::getCallerFrame(const DWARFUnit *U,
311                                                 uint32_t &CallFile,
312                                                 uint32_t &CallLine,
313                                                 uint32_t &CallColumn) const {
314   CallFile = getAttributeValueAsUnsignedConstant(U, DW_AT_call_file, 0);
315   CallLine = getAttributeValueAsUnsignedConstant(U, DW_AT_call_line, 0);
316   CallColumn = getAttributeValueAsUnsignedConstant(U, DW_AT_call_column, 0);
317 }
318
319 DWARFDebugInfoEntryInlinedChain
320 DWARFDebugInfoEntryMinimal::getInlinedChainForAddress(
321     const DWARFUnit *U, const uint64_t Address) const {
322   DWARFDebugInfoEntryInlinedChain InlinedChain;
323   InlinedChain.U = U;
324   if (isNULL())
325     return InlinedChain;
326   for (const DWARFDebugInfoEntryMinimal *DIE = this; DIE; ) {
327     // Append current DIE to inlined chain only if it has correct tag
328     // (e.g. it is not a lexical block).
329     if (DIE->isSubroutineDIE()) {
330       InlinedChain.DIEs.push_back(*DIE);
331     }
332     // Try to get child which also contains provided address.
333     const DWARFDebugInfoEntryMinimal *Child = DIE->getFirstChild();
334     while (Child) {
335       if (Child->addressRangeContainsAddress(U, Address)) {
336         // Assume there is only one such child.
337         break;
338       }
339       Child = Child->getSibling();
340     }
341     DIE = Child;
342   }
343   // Reverse the obtained chain to make the root of inlined chain last.
344   std::reverse(InlinedChain.DIEs.begin(), InlinedChain.DIEs.end());
345   return InlinedChain;
346 }