Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame^] | 1 | //===-- DWARFCompileUnit.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 "DWARFCompileUnit.h" |
| 11 | #include "DWARFContext.h" |
| 12 | #include "DWARFFormValue.h" |
| 13 | #include "llvm/Support/Dwarf.h" |
| 14 | #include "llvm/Support/Format.h" |
| 15 | #include "llvm/Support/raw_ostream.h" |
| 16 | using namespace llvm; |
| 17 | using namespace dwarf; |
| 18 | |
| 19 | DataExtractor DWARFCompileUnit::getDebugInfoExtractor() const { |
| 20 | return DataExtractor(Context.getInfoSection(), |
| 21 | Context.isLittleEndian(), getAddressByteSize()); |
| 22 | } |
| 23 | |
| 24 | bool DWARFCompileUnit::extract(DataExtractor debug_info, uint32_t *offset_ptr) { |
| 25 | clear(); |
| 26 | |
| 27 | Offset = *offset_ptr; |
| 28 | |
| 29 | if (debug_info.isValidOffset(*offset_ptr)) { |
| 30 | uint64_t abbrOffset; |
| 31 | const DWARFDebugAbbrev *abbr = Context.getDebugAbbrev(); |
| 32 | Length = debug_info.getU32(offset_ptr); |
| 33 | Version = debug_info.getU16(offset_ptr); |
| 34 | abbrOffset = debug_info.getU32(offset_ptr); |
| 35 | AddrSize = debug_info.getU8(offset_ptr); |
| 36 | |
| 37 | bool lengthOK = debug_info.isValidOffset(getNextCompileUnitOffset()-1); |
| 38 | bool versionOK = DWARFContext::isSupportedVersion(Version); |
| 39 | bool abbrOffsetOK = Context.getAbbrevSection().size() > abbrOffset; |
| 40 | bool addrSizeOK = AddrSize == 4 || AddrSize == 8; |
| 41 | |
| 42 | if (lengthOK && versionOK && addrSizeOK && abbrOffsetOK && abbr != NULL) { |
| 43 | Abbrevs = abbr->getAbbreviationDeclarationSet(abbrOffset); |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | // reset the offset to where we tried to parse from if anything went wrong |
| 48 | *offset_ptr = Offset; |
| 49 | } |
| 50 | |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | uint32_t |
| 55 | DWARFCompileUnit::extract(uint32_t offset, DataExtractor debug_info_data, |
| 56 | const DWARFAbbreviationDeclarationSet *abbrevs) { |
| 57 | clear(); |
| 58 | |
| 59 | Offset = offset; |
| 60 | |
| 61 | if (debug_info_data.isValidOffset(offset)) { |
| 62 | Length = debug_info_data.getU32(&offset); |
| 63 | Version = debug_info_data.getU16(&offset); |
| 64 | bool abbrevsOK = debug_info_data.getU32(&offset) == abbrevs->getOffset(); |
| 65 | Abbrevs = abbrevs; |
| 66 | AddrSize = debug_info_data.getU8 (&offset); |
| 67 | |
| 68 | bool versionOK = DWARFContext::isSupportedVersion(Version); |
| 69 | bool addrSizeOK = AddrSize == 4 || AddrSize == 8; |
| 70 | |
| 71 | if (versionOK && addrSizeOK && abbrevsOK && |
| 72 | debug_info_data.isValidOffset(offset)) |
| 73 | return offset; |
| 74 | } |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | void DWARFCompileUnit::clear() { |
| 79 | Offset = 0; |
| 80 | Length = 0; |
| 81 | Version = 0; |
| 82 | Abbrevs = 0; |
| 83 | AddrSize = 0; |
| 84 | BaseAddr = 0; |
| 85 | DieArray.clear(); |
| 86 | } |
| 87 | |
| 88 | void DWARFCompileUnit::dump(raw_ostream &OS) { |
| 89 | OS << format("0x%08x", Offset) << ": Compile Unit:" |
| 90 | << " length = " << format("0x%08x", Length) |
| 91 | << " version = " << format("0x%04x", Version) |
| 92 | << " abbr_offset = " << format("0x%04x", Abbrevs->getOffset()) |
| 93 | << " addr_size = " << format("0x%02x", AddrSize) |
| 94 | << " (next CU at " << format("0x%08x", getNextCompileUnitOffset()) |
| 95 | << ")\n"; |
| 96 | |
| 97 | extractDIEsIfNeeded(false); |
| 98 | for (unsigned i = 0, e = DieArray.size(); i != e; ++i) |
| 99 | DieArray[i].dump(OS, this, 10); |
| 100 | } |
| 101 | |
| 102 | void DWARFCompileUnit::setDIERelations() { |
| 103 | if (DieArray.empty()) |
| 104 | return; |
| 105 | DWARFDebugInfoEntryMinimal *die_array_begin = &DieArray.front(); |
| 106 | DWARFDebugInfoEntryMinimal *die_array_end = &DieArray.back(); |
| 107 | DWARFDebugInfoEntryMinimal *curr_die; |
| 108 | // We purposely are skipping the last element in the array in the loop below |
| 109 | // so that we can always have a valid next item |
| 110 | for (curr_die = die_array_begin; curr_die < die_array_end; ++curr_die) { |
| 111 | // Since our loop doesn't include the last element, we can always |
| 112 | // safely access the next die in the array. |
| 113 | DWARFDebugInfoEntryMinimal *next_die = curr_die + 1; |
| 114 | |
| 115 | const DWARFAbbreviationDeclaration *curr_die_abbrev = |
| 116 | curr_die->getAbbreviationDeclarationPtr(); |
| 117 | |
| 118 | if (curr_die_abbrev) { |
| 119 | // Normal DIE |
| 120 | if (curr_die_abbrev->hasChildren()) |
| 121 | next_die->setParent(curr_die); |
| 122 | else |
| 123 | curr_die->setSibling(next_die); |
| 124 | } else { |
| 125 | // NULL DIE that terminates a sibling chain |
| 126 | DWARFDebugInfoEntryMinimal *parent = curr_die->getParent(); |
| 127 | if (parent) |
| 128 | parent->setSibling(next_die); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // Since we skipped the last element, we need to fix it up! |
| 133 | if (die_array_begin < die_array_end) |
| 134 | curr_die->setParent(die_array_begin); |
| 135 | } |
| 136 | |
| 137 | size_t DWARFCompileUnit::extractDIEsIfNeeded(bool cu_die_only) { |
| 138 | const size_t initial_die_array_size = DieArray.size(); |
| 139 | if ((cu_die_only && initial_die_array_size > 0) || |
| 140 | initial_die_array_size > 1) |
| 141 | return 0; // Already parsed |
| 142 | |
| 143 | // Set the offset to that of the first DIE and calculate the start of the |
| 144 | // next compilation unit header. |
| 145 | uint32_t offset = getFirstDIEOffset(); |
| 146 | uint32_t next_cu_offset = getNextCompileUnitOffset(); |
| 147 | |
| 148 | DWARFDebugInfoEntryMinimal die; |
| 149 | // Keep a flat array of the DIE for binary lookup by DIE offset |
| 150 | uint32_t depth = 0; |
| 151 | // We are in our compile unit, parse starting at the offset |
| 152 | // we were told to parse |
| 153 | |
| 154 | const uint8_t *fixed_form_sizes = |
| 155 | DWARFFormValue::getFixedFormSizesForAddressSize(getAddressByteSize()); |
| 156 | |
| 157 | while (offset < next_cu_offset && |
| 158 | die.extractFast(this, fixed_form_sizes, &offset)) { |
| 159 | |
| 160 | if (depth == 0) { |
| 161 | uint64_t base_addr = |
| 162 | die.getAttributeValueAsUnsigned(this, DW_AT_low_pc, -1U); |
| 163 | if (base_addr == -1U) |
| 164 | base_addr = die.getAttributeValueAsUnsigned(this, DW_AT_entry_pc, 0); |
| 165 | setBaseAddress(base_addr); |
| 166 | } |
| 167 | |
| 168 | if (cu_die_only) { |
| 169 | addDIE(die); |
| 170 | return 1; |
| 171 | } |
| 172 | else if (depth == 0 && initial_die_array_size == 1) { |
| 173 | // Don't append the CU die as we already did that |
| 174 | } else { |
| 175 | addDIE (die); |
| 176 | } |
| 177 | |
| 178 | const DWARFAbbreviationDeclaration *abbrDecl = |
| 179 | die.getAbbreviationDeclarationPtr(); |
| 180 | if (abbrDecl) { |
| 181 | // Normal DIE |
| 182 | if (abbrDecl->hasChildren()) |
| 183 | ++depth; |
| 184 | } else { |
| 185 | // NULL DIE. |
| 186 | if (depth > 0) |
| 187 | --depth; |
| 188 | if (depth == 0) |
| 189 | break; // We are done with this compile unit! |
| 190 | } |
| 191 | |
| 192 | } |
| 193 | |
| 194 | // Give a little bit of info if we encounter corrupt DWARF (our offset |
| 195 | // should always terminate at or before the start of the next compilation |
| 196 | // unit header). |
| 197 | if (offset > next_cu_offset) { |
| 198 | fprintf (stderr, "warning: DWARF compile unit extends beyond its bounds cu 0x%8.8x at 0x%8.8x'\n", getOffset(), offset); |
| 199 | } |
| 200 | |
| 201 | setDIERelations(); |
| 202 | return DieArray.size(); |
| 203 | } |