1 //===-- DWARFDebugInfoEntry.cpp -------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "DWARFDebugInfoEntry.h"
11 #include "DWARFCompileUnit.h"
12 #include "DWARFContext.h"
13 #include "DWARFDebugAbbrev.h"
14 #include "DWARFFormValue.h"
15 #include "llvm/Support/Dwarf.h"
16 #include "llvm/Support/Format.h"
17 #include "llvm/Support/raw_ostream.h"
19 using namespace dwarf;
21 void DWARFDebugInfoEntryMinimal::dump(raw_ostream &OS,
22 const DWARFCompileUnit *cu,
23 unsigned recurseDepth,
24 unsigned indent) const {
25 DataExtractor debug_info_data = cu->getDebugInfoExtractor();
26 uint32_t offset = Offset;
28 if (debug_info_data.isValidOffset(offset)) {
29 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
31 OS << format("\n0x%8.8x: ", Offset);
34 const char *tagString = TagString(getTag());
36 OS.indent(indent) << tagString;
38 OS.indent(indent) << format("DW_TAG_Unknown_%x", getTag());
39 OS << format(" [%u] %c\n", abbrCode,
40 AbbrevDecl->hasChildren() ? '*' : ' ');
42 // Dump all data in the .debug_info for the attributes
43 const uint32_t numAttributes = AbbrevDecl->getNumAttributes();
44 for (uint32_t i = 0; i != numAttributes; ++i) {
45 uint16_t attr = AbbrevDecl->getAttrByIndex(i);
46 uint16_t form = AbbrevDecl->getFormByIndex(i);
47 dumpAttribute(OS, cu, &offset, attr, form, indent);
50 const DWARFDebugInfoEntryMinimal *child = getFirstChild();
51 if (recurseDepth > 0 && child) {
53 child->dump(OS, cu, recurseDepth-1, indent+2);
54 child = child->getSibling();
58 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
62 OS.indent(indent) << "NULL\n";
67 void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS,
68 const DWARFCompileUnit *cu,
72 unsigned indent) const {
73 OS << format("0x%8.8x: ", *offset_ptr);
75 const char *attrString = AttributeString(attr);
79 OS << format("DW_AT_Unknown_%x", attr);
80 const char *formString = FormEncodingString(form);
82 OS << " [" << formString << ']';
84 OS << format(" [DW_FORM_Unknown_%x]", form);
86 DWARFFormValue formValue(form);
88 if (!formValue.extractValue(cu->getDebugInfoExtractor(), offset_ptr, cu))
92 formValue.dump(OS, cu);
96 bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFCompileUnit *cu,
97 const uint8_t *fixed_form_sizes,
98 uint32_t *offset_ptr) {
101 DataExtractor debug_info_data = cu->getDebugInfoExtractor();
102 uint64_t abbrCode = debug_info_data.getULEB128(offset_ptr);
104 assert(fixed_form_sizes); // For best performance this should be specified!
107 uint32_t offset = *offset_ptr;
109 AbbrevDecl = cu->getAbbreviations()->getAbbreviationDeclaration(abbrCode);
111 // Skip all data in the .debug_info for the attributes
112 const uint32_t numAttributes = AbbrevDecl->getNumAttributes();
115 for (i=0; i<numAttributes; ++i) {
116 form = AbbrevDecl->getFormByIndex(i);
118 const uint8_t fixed_skip_size = fixed_form_sizes[form];
120 offset += fixed_skip_size;
122 bool form_is_indirect = false;
124 form_is_indirect = false;
125 uint32_t form_size = 0;
127 // Blocks if inlined data that have a length field and the data bytes
128 // inlined in the .debug_info.
129 case DW_FORM_exprloc:
131 form_size = debug_info_data.getULEB128(&offset);
134 form_size = debug_info_data.getU8(&offset);
137 form_size = debug_info_data.getU16(&offset);
140 form_size = debug_info_data.getU32(&offset);
143 // Inlined NULL terminated C-strings
145 debug_info_data.getCStr(&offset);
148 // Compile unit address sized values
150 case DW_FORM_ref_addr:
151 form_size = cu->getAddressByteSize();
155 case DW_FORM_flag_present:
182 case DW_FORM_ref_sig8:
186 // signed or unsigned LEB 128 values
189 case DW_FORM_ref_udata:
190 debug_info_data.getULEB128(&offset);
193 case DW_FORM_indirect:
194 form_is_indirect = true;
195 form = debug_info_data.getULEB128(&offset);
198 case DW_FORM_sec_offset:
199 if (cu->getAddressByteSize() == 4)
200 debug_info_data.getU32(offset_ptr);
202 debug_info_data.getU64(offset_ptr);
206 *offset_ptr = Offset;
211 } while (form_is_indirect);
214 *offset_ptr = offset;
218 return true; // NULL debug tag entry
223 DWARFDebugInfoEntryMinimal::extract(const DWARFCompileUnit *cu,
224 uint32_t *offset_ptr) {
225 DataExtractor debug_info_data = cu->getDebugInfoExtractor();
226 const uint32_t cu_end_offset = cu->getNextCompileUnitOffset();
227 const uint8_t cu_addr_size = cu->getAddressByteSize();
228 uint32_t offset = *offset_ptr;
229 if ((offset < cu_end_offset) && debug_info_data.isValidOffset(offset)) {
232 uint64_t abbrCode = debug_info_data.getULEB128(&offset);
235 AbbrevDecl = cu->getAbbreviations()->getAbbreviationDeclaration(abbrCode);
238 uint16_t tag = AbbrevDecl->getTag();
240 bool isCompileUnitTag = tag == DW_TAG_compile_unit;
241 if(cu && isCompileUnitTag)
242 const_cast<DWARFCompileUnit*>(cu)->setBaseAddress(0);
244 // Skip all data in the .debug_info for the attributes
245 const uint32_t numAttributes = AbbrevDecl->getNumAttributes();
246 for (uint32_t i = 0; i != numAttributes; ++i) {
247 uint16_t attr = AbbrevDecl->getAttrByIndex(i);
248 uint16_t form = AbbrevDecl->getFormByIndex(i);
250 if (isCompileUnitTag &&
251 ((attr == DW_AT_entry_pc) || (attr == DW_AT_low_pc))) {
252 DWARFFormValue form_value(form);
253 if (form_value.extractValue(debug_info_data, &offset, cu)) {
254 if (attr == DW_AT_low_pc || attr == DW_AT_entry_pc)
255 const_cast<DWARFCompileUnit*>(cu)
256 ->setBaseAddress(form_value.getUnsigned());
259 bool form_is_indirect = false;
261 form_is_indirect = false;
262 register uint32_t form_size = 0;
264 // Blocks if inlined data that have a length field and the data
265 // bytes // inlined in the .debug_info
266 case DW_FORM_exprloc:
268 form_size = debug_info_data.getULEB128(&offset);
271 form_size = debug_info_data.getU8(&offset);
274 form_size = debug_info_data.getU16(&offset);
277 form_size = debug_info_data.getU32(&offset);
280 // Inlined NULL terminated C-strings
282 debug_info_data.getCStr(&offset);
285 // Compile unit address sized values
287 case DW_FORM_ref_addr:
288 form_size = cu_addr_size;
292 case DW_FORM_flag_present:
322 case DW_FORM_ref_sig8:
326 // signed or unsigned LEB 128 values
329 case DW_FORM_ref_udata:
330 debug_info_data.getULEB128(&offset);
333 case DW_FORM_indirect:
334 form = debug_info_data.getULEB128(&offset);
335 form_is_indirect = true;
338 case DW_FORM_sec_offset:
339 if (cu->getAddressByteSize() == 4)
340 debug_info_data.getU32(offset_ptr);
342 debug_info_data.getU64(offset_ptr);
346 *offset_ptr = offset;
351 } while (form_is_indirect);
354 *offset_ptr = offset;
359 *offset_ptr = offset;
360 return true; // NULL debug tag entry
368 DWARFDebugInfoEntryMinimal::getAttributeValue(const DWARFCompileUnit *cu,
370 DWARFFormValue &form_value,
371 uint32_t *end_attr_offset_ptr)
374 uint32_t attr_idx = AbbrevDecl->findAttributeIndex(attr);
376 if (attr_idx != -1U) {
377 uint32_t offset = getOffset();
379 DataExtractor debug_info_data = cu->getDebugInfoExtractor();
381 // Skip the abbreviation code so we are at the data for the attributes
382 debug_info_data.getULEB128(&offset);
385 while (idx < attr_idx)
386 DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(idx++),
387 debug_info_data, &offset, cu);
389 const uint32_t attr_offset = offset;
390 form_value = DWARFFormValue(AbbrevDecl->getFormByIndex(idx));
391 if (form_value.extractValue(debug_info_data, &offset, cu)) {
392 if (end_attr_offset_ptr)
393 *end_attr_offset_ptr = offset;
403 DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
404 const DWARFCompileUnit* cu,
406 const char* fail_value) const {
407 DWARFFormValue form_value;
408 if (getAttributeValue(cu, attr, form_value)) {
409 DataExtractor stringExtractor(cu->getContext().getStringSection(),
411 return form_value.getAsCString(&stringExtractor);
417 DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsigned(
418 const DWARFCompileUnit* cu,
420 uint64_t fail_value) const {
421 DWARFFormValue form_value;
422 if (getAttributeValue(cu, attr, form_value))
423 return form_value.getUnsigned();
428 DWARFDebugInfoEntryMinimal::getAttributeValueAsSigned(
429 const DWARFCompileUnit* cu,
431 int64_t fail_value) const {
432 DWARFFormValue form_value;
433 if (getAttributeValue(cu, attr, form_value))
434 return form_value.getSigned();
439 DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
440 const DWARFCompileUnit* cu,
442 uint64_t fail_value) const {
443 DWARFFormValue form_value;
444 if (getAttributeValue(cu, attr, form_value))
445 return form_value.getReference(cu);
450 DWARFDebugInfoEntryMinimal::buildAddressRangeTable(const DWARFCompileUnit *cu,
451 DWARFDebugAranges *debug_aranges)
454 uint16_t tag = AbbrevDecl->getTag();
455 if (tag == DW_TAG_subprogram) {
456 uint64_t hi_pc = -1ULL;
457 uint64_t lo_pc = getAttributeValueAsUnsigned(cu, DW_AT_low_pc, -1ULL);
459 hi_pc = getAttributeValueAsUnsigned(cu, DW_AT_high_pc, -1ULL);
461 debug_aranges->appendRange(cu->getOffset(), lo_pc, hi_pc);
464 const DWARFDebugInfoEntryMinimal *child = getFirstChild();
466 child->buildAddressRangeTable(cu, debug_aranges);
467 child = child->getSibling();
473 DWARFDebugInfoEntryMinimal::addressRangeContainsAddress(
474 const DWARFCompileUnit *cu, const uint64_t address) const {
475 if (!isNULL() && getTag() == DW_TAG_subprogram) {
476 uint64_t hi_pc = -1ULL;
477 uint64_t lo_pc = getAttributeValueAsUnsigned(cu, DW_AT_low_pc, -1ULL);
479 hi_pc = getAttributeValueAsUnsigned(cu, DW_AT_high_pc, -1ULL);
480 if (hi_pc != -1ULL) {
481 return (lo_pc <= address && address < hi_pc);
488 DWARFDebugInfoEntryMinimal::getSubprogramName(
489 const DWARFCompileUnit *cu) const {
490 if (isNULL() || getTag() != DW_TAG_subprogram)
492 // Try to get mangled name if possible.
493 if (const char *name =
494 getAttributeValueAsString(cu, DW_AT_MIPS_linkage_name, 0))
496 if (const char *name = getAttributeValueAsString(cu, DW_AT_linkage_name, 0))
498 if (const char *name = getAttributeValueAsString(cu, DW_AT_name, 0))
500 // Try to get name from specification DIE.
502 getAttributeValueAsReference(cu, DW_AT_specification, -1U);
503 if (spec_ref != -1U) {
504 DWARFDebugInfoEntryMinimal spec_die;
505 if (spec_die.extract(cu, &spec_ref)) {
506 if (const char *name = spec_die.getSubprogramName(cu))
510 // Try to get name from abstract origin DIE.
511 uint32_t abs_origin_ref =
512 getAttributeValueAsReference(cu, DW_AT_abstract_origin, -1U);
513 if (abs_origin_ref != -1U) {
514 DWARFDebugInfoEntryMinimal abs_origin_die;
515 if (abs_origin_die.extract(cu, &abs_origin_ref)) {
516 if (const char *name = abs_origin_die.getSubprogramName(cu))