1 //===- DWARFVerifier.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 "llvm/DebugInfo/DWARF/DWARFVerifier.h"
11 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
12 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
13 #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
14 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
15 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
16 #include "llvm/DebugInfo/DWARF/DWARFSection.h"
17 #include "llvm/Support/raw_ostream.h"
23 using namespace dwarf;
24 using namespace object;
26 void DWARFVerifier::verifyDebugInfoAttribute(DWARFDie &Die,
27 DWARFAttribute &AttrValue) {
28 const auto Attr = AttrValue.Attr;
31 // Make sure the offset in the DW_AT_ranges attribute is valid.
32 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
33 if (*SectionOffset >= DCtx.getRangeSection().Data.size()) {
35 OS << "error: DW_AT_ranges offset is beyond .debug_ranges "
42 OS << "error: DIE has invalid DW_AT_ranges encoding:\n";
48 // Make sure the offset in the DW_AT_stmt_list attribute is valid.
49 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
50 if (*SectionOffset >= DCtx.getLineSection().Data.size()) {
52 OS << "error: DW_AT_stmt_list offset is beyond .debug_line "
54 << format("0x%08" PRIx32, *SectionOffset) << "\n";
60 OS << "error: DIE has invalid DW_AT_stmt_list encoding:\n";
71 void DWARFVerifier::verifyDebugInfoForm(DWARFDie &Die,
72 DWARFAttribute &AttrValue) {
73 const auto Form = AttrValue.Value.getForm();
79 case DW_FORM_ref_udata: {
80 // Verify all CU relative references are valid CU offsets.
81 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
84 auto DieCU = Die.getDwarfUnit();
85 auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset();
86 auto CUOffset = AttrValue.Value.getRawUValue();
87 if (CUOffset >= CUSize) {
89 OS << "error: " << FormEncodingString(Form) << " CU offset "
90 << format("0x%08" PRIx32, CUOffset)
91 << " is invalid (must be less than CU size of "
92 << format("0x%08" PRIx32, CUSize) << "):\n";
96 // Valid reference, but we will verify it points to an actual
98 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
103 case DW_FORM_ref_addr: {
104 // Verify all absolute DIE references have valid offsets in the
105 // .debug_info section.
106 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
109 if (*RefVal >= DCtx.getInfoSection().Data.size()) {
110 ++NumDebugInfoErrors;
111 OS << "error: DW_FORM_ref_addr offset beyond .debug_info "
116 // Valid reference, but we will verify it points to an actual
118 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
124 auto SecOffset = AttrValue.Value.getAsSectionOffset();
125 assert(SecOffset); // DW_FORM_strp is a section offset.
126 if (SecOffset && *SecOffset >= DCtx.getStringSection().size()) {
127 ++NumDebugInfoErrors;
128 OS << "error: DW_FORM_strp offset beyond .debug_str bounds:\n";
139 void DWARFVerifier::veifyDebugInfoReferences() {
140 // Take all references and make sure they point to an actual DIE by
141 // getting the DIE by offset and emitting an error
142 OS << "Verifying .debug_info references...\n";
143 for (auto Pair : ReferenceToDIEOffsets) {
144 auto Die = DCtx.getDIEForOffset(Pair.first);
147 ++NumDebugInfoErrors;
148 OS << "error: invalid DIE reference " << format("0x%08" PRIx64, Pair.first)
149 << ". Offset is in between DIEs:\n";
150 for (auto Offset : Pair.second) {
151 auto ReferencingDie = DCtx.getDIEForOffset(Offset);
152 ReferencingDie.dump(OS, 0);
159 bool DWARFVerifier::handleDebugInfo() {
160 NumDebugInfoErrors = 0;
161 OS << "Verifying .debug_info...\n";
162 for (const auto &CU : DCtx.compile_units()) {
163 unsigned NumDies = CU->getNumDIEs();
164 for (unsigned I = 0; I < NumDies; ++I) {
165 auto Die = CU->getDIEAtIndex(I);
166 const auto Tag = Die.getTag();
167 if (Tag == DW_TAG_null)
169 for (auto AttrValue : Die.attributes()) {
170 verifyDebugInfoAttribute(Die, AttrValue);
171 verifyDebugInfoForm(Die, AttrValue);
175 veifyDebugInfoReferences();
176 return NumDebugInfoErrors == 0;
179 void DWARFVerifier::verifyDebugLineStmtOffsets() {
180 std::map<uint64_t, DWARFDie> StmtListToDie;
181 for (const auto &CU : DCtx.compile_units()) {
182 auto Die = CU->getUnitDIE();
183 // Get the attribute value as a section offset. No need to produce an
184 // error here if the encoding isn't correct because we validate this in
185 // the .debug_info verifier.
186 auto StmtSectionOffset = toSectionOffset(Die.find(DW_AT_stmt_list));
187 if (!StmtSectionOffset)
189 const uint32_t LineTableOffset = *StmtSectionOffset;
190 auto LineTable = DCtx.getLineTableForUnit(CU.get());
191 if (LineTableOffset < DCtx.getLineSection().Data.size()) {
193 ++NumDebugLineErrors;
194 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
195 << "] was not able to be parsed for CU:\n";
201 // Make sure we don't get a valid line table back if the offset is wrong.
202 assert(LineTable == nullptr);
203 // Skip this line table as it isn't valid. No need to create an error
204 // here because we validate this in the .debug_info verifier.
207 auto Iter = StmtListToDie.find(LineTableOffset);
208 if (Iter != StmtListToDie.end()) {
209 ++NumDebugLineErrors;
210 OS << "error: two compile unit DIEs, "
211 << format("0x%08" PRIx32, Iter->second.getOffset()) << " and "
212 << format("0x%08" PRIx32, Die.getOffset())
213 << ", have the same DW_AT_stmt_list section offset:\n";
214 Iter->second.dump(OS, 0);
217 // Already verified this line table before, no need to do it again.
220 StmtListToDie[LineTableOffset] = Die;
224 void DWARFVerifier::verifyDebugLineRows() {
225 for (const auto &CU : DCtx.compile_units()) {
226 auto Die = CU->getUnitDIE();
227 auto LineTable = DCtx.getLineTableForUnit(CU.get());
228 // If there is no line table we will have created an error in the
229 // .debug_info verifier or in verifyDebugLineStmtOffsets().
232 uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size();
233 uint64_t PrevAddress = 0;
234 uint32_t RowIndex = 0;
235 for (const auto &Row : LineTable->Rows) {
236 if (Row.Address < PrevAddress) {
237 ++NumDebugLineErrors;
238 OS << "error: .debug_line["
239 << format("0x%08" PRIx32,
240 *toSectionOffset(Die.find(DW_AT_stmt_list)))
241 << "] row[" << RowIndex
242 << "] decreases in address from previous row:\n";
244 DWARFDebugLine::Row::dumpTableHeader(OS);
246 LineTable->Rows[RowIndex - 1].dump(OS);
251 if (Row.File > MaxFileIndex) {
252 ++NumDebugLineErrors;
253 OS << "error: .debug_line["
254 << format("0x%08" PRIx32,
255 *toSectionOffset(Die.find(DW_AT_stmt_list)))
256 << "][" << RowIndex << "] has invalid file index " << Row.File
257 << " (valid values are [1," << MaxFileIndex << "]):\n";
258 DWARFDebugLine::Row::dumpTableHeader(OS);
265 PrevAddress = Row.Address;
271 bool DWARFVerifier::handleDebugLine() {
272 NumDebugLineErrors = 0;
273 OS << "Verifying .debug_line...\n";
274 verifyDebugLineStmtOffsets();
275 verifyDebugLineRows();
276 return NumDebugLineErrors == 0;