1 //===- DWARFDebugLine.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 "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/BinaryFormat/Dwarf.h"
15 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
16 #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
17 #include "llvm/Support/Format.h"
18 #include "llvm/Support/Path.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include <algorithm>
21 #include <cassert>
22 #include <cinttypes>
23 #include <cstdint>
24 #include <cstdio>
25 #include <utility>
26 
27 using namespace llvm;
28 using namespace dwarf;
29 
30 using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind;
31 
32 namespace {
33 
34 struct ContentDescriptor {
35   dwarf::LineNumberEntryFormat Type;
36   dwarf::Form Form;
37 };
38 
39 using ContentDescriptors = SmallVector<ContentDescriptor, 4>;
40 
41 } // end anonmyous namespace
42 
43 DWARFDebugLine::Prologue::Prologue() { clear(); }
44 
45 void DWARFDebugLine::Prologue::clear() {
46   TotalLength = PrologueLength = 0;
47   SegSelectorSize = 0;
48   MinInstLength = MaxOpsPerInst = DefaultIsStmt = LineBase = LineRange = 0;
49   OpcodeBase = 0;
50   FormParams = DWARFFormParams({0, 0, DWARF32});
51   HasMD5 = false;
52   StandardOpcodeLengths.clear();
53   IncludeDirectories.clear();
54   FileNames.clear();
55 }
56 
57 void DWARFDebugLine::Prologue::dump(raw_ostream &OS) const {
58   OS << "Line table prologue:\n"
59      << format("    total_length: 0x%8.8" PRIx64 "\n", TotalLength)
60      << format("         version: %u\n", getVersion());
61   if (getVersion() >= 5)
62     OS << format("    address_size: %u\n", getAddressSize())
63        << format(" seg_select_size: %u\n", SegSelectorSize);
64   OS << format(" prologue_length: 0x%8.8" PRIx64 "\n", PrologueLength)
65      << format(" min_inst_length: %u\n", MinInstLength)
66      << format(getVersion() >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst)
67      << format(" default_is_stmt: %u\n", DefaultIsStmt)
68      << format("       line_base: %i\n", LineBase)
69      << format("      line_range: %u\n", LineRange)
70      << format("     opcode_base: %u\n", OpcodeBase);
71 
72   for (uint32_t I = 0; I != StandardOpcodeLengths.size(); ++I)
73     OS << format("standard_opcode_lengths[%s] = %u\n",
74                  LNStandardString(I + 1).data(), StandardOpcodeLengths[I]);
75 
76   if (!IncludeDirectories.empty())
77     for (uint32_t I = 0; I != IncludeDirectories.size(); ++I)
78       OS << format("include_directories[%3u] = '", I + 1)
79          << IncludeDirectories[I] << "'\n";
80 
81   if (!FileNames.empty()) {
82     if (HasMD5)
83       OS << "                Dir  MD5 Checksum                     File Name\n"
84          << "                ---- -------------------------------- -----------"
85             "---------------\n";
86     else
87       OS << "                Dir  Mod Time   File Len   File Name\n"
88          << "                ---- ---------- ---------- -----------"
89             "----------------\n";
90     for (uint32_t I = 0; I != FileNames.size(); ++I) {
91       const FileNameEntry &FileEntry = FileNames[I];
92       OS << format("file_names[%3u] %4" PRIu64 " ", I + 1, FileEntry.DirIdx);
93       if (HasMD5)
94         OS << FileEntry.Checksum.digest();
95       else
96         OS << format("0x%8.8" PRIx64 " 0x%8.8" PRIx64, FileEntry.ModTime,
97                      FileEntry.Length);
98       OS << ' ' << FileEntry.Name << '\n';
99     }
100   }
101 }
102 
103 // Parse v2-v4 directory and file tables.
104 static void
105 parseV2DirFileTables(const DWARFDataExtractor &DebugLineData,
106                      uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
107                      std::vector<StringRef> &IncludeDirectories,
108                      std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
109   while (*OffsetPtr < EndPrologueOffset) {
110     StringRef S = DebugLineData.getCStrRef(OffsetPtr);
111     if (S.empty())
112       break;
113     IncludeDirectories.push_back(S);
114   }
115 
116   while (*OffsetPtr < EndPrologueOffset) {
117     StringRef Name = DebugLineData.getCStrRef(OffsetPtr);
118     if (Name.empty())
119       break;
120     DWARFDebugLine::FileNameEntry FileEntry;
121     FileEntry.Name = Name;
122     FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
123     FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
124     FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
125     FileNames.push_back(FileEntry);
126   }
127 }
128 
129 // Parse v5 directory/file entry content descriptions.
130 // Returns the descriptors, or an empty vector if we did not find a path or
131 // ran off the end of the prologue.
132 static ContentDescriptors
133 parseV5EntryFormat(const DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr,
134                    uint64_t EndPrologueOffset, bool *HasMD5) {
135   ContentDescriptors Descriptors;
136   int FormatCount = DebugLineData.getU8(OffsetPtr);
137   bool HasPath = false;
138   for (int I = 0; I != FormatCount; ++I) {
139     if (*OffsetPtr >= EndPrologueOffset)
140       return ContentDescriptors();
141     ContentDescriptor Descriptor;
142     Descriptor.Type =
143       dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr));
144     Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr));
145     if (Descriptor.Type == dwarf::DW_LNCT_path)
146       HasPath = true;
147     else if (Descriptor.Type == dwarf::DW_LNCT_MD5 && HasMD5)
148       *HasMD5 = true;
149     Descriptors.push_back(Descriptor);
150   }
151   return HasPath ? Descriptors : ContentDescriptors();
152 }
153 
154 static bool
155 parseV5DirFileTables(const DWARFDataExtractor &DebugLineData,
156                      uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
157                      const DWARFFormParams &FormParams, const DWARFUnit *U,
158                      bool &HasMD5, std::vector<StringRef> &IncludeDirectories,
159                      std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
160   // Get the directory entry description.
161   ContentDescriptors DirDescriptors =
162       parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset, nullptr);
163   if (DirDescriptors.empty())
164     return false;
165 
166   // Get the directory entries, according to the format described above.
167   int DirEntryCount = DebugLineData.getU8(OffsetPtr);
168   for (int I = 0; I != DirEntryCount; ++I) {
169     if (*OffsetPtr >= EndPrologueOffset)
170       return false;
171     for (auto Descriptor : DirDescriptors) {
172       DWARFFormValue Value(Descriptor.Form);
173       switch (Descriptor.Type) {
174       case DW_LNCT_path:
175         if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, U))
176           return false;
177         IncludeDirectories.push_back(Value.getAsCString().getValue());
178         break;
179       default:
180         if (!Value.skipValue(DebugLineData, OffsetPtr, FormParams))
181           return false;
182       }
183     }
184   }
185 
186   // Get the file entry description.
187   ContentDescriptors FileDescriptors =
188       parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset, &HasMD5);
189   if (FileDescriptors.empty())
190     return false;
191 
192   // Get the file entries, according to the format described above.
193   int FileEntryCount = DebugLineData.getU8(OffsetPtr);
194   for (int I = 0; I != FileEntryCount; ++I) {
195     if (*OffsetPtr >= EndPrologueOffset)
196       return false;
197     DWARFDebugLine::FileNameEntry FileEntry;
198     for (auto Descriptor : FileDescriptors) {
199       DWARFFormValue Value(Descriptor.Form);
200       if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, U))
201         return false;
202       switch (Descriptor.Type) {
203       case DW_LNCT_path:
204         FileEntry.Name = Value.getAsCString().getValue();
205         break;
206       case DW_LNCT_directory_index:
207         FileEntry.DirIdx = Value.getAsUnsignedConstant().getValue();
208         break;
209       case DW_LNCT_timestamp:
210         FileEntry.ModTime = Value.getAsUnsignedConstant().getValue();
211         break;
212       case DW_LNCT_size:
213         FileEntry.Length = Value.getAsUnsignedConstant().getValue();
214         break;
215       case DW_LNCT_MD5:
216         assert(Value.getAsBlock().getValue().size() == 16);
217         std::uninitialized_copy_n(Value.getAsBlock().getValue().begin(), 16,
218                                   FileEntry.Checksum.Bytes.begin());
219         break;
220       default:
221         break;
222       }
223     }
224     FileNames.push_back(FileEntry);
225   }
226   return true;
227 }
228 
229 bool DWARFDebugLine::Prologue::parse(const DWARFDataExtractor &DebugLineData,
230                                      uint32_t *OffsetPtr, const DWARFUnit *U) {
231   const uint64_t PrologueOffset = *OffsetPtr;
232 
233   clear();
234   TotalLength = DebugLineData.getU32(OffsetPtr);
235   if (TotalLength == UINT32_MAX) {
236     FormParams.Format = dwarf::DWARF64;
237     TotalLength = DebugLineData.getU64(OffsetPtr);
238   } else if (TotalLength >= 0xffffff00) {
239     return false;
240   }
241   FormParams.Version = DebugLineData.getU16(OffsetPtr);
242   if (getVersion() < 2)
243     return false;
244 
245   if (getVersion() >= 5) {
246     FormParams.AddrSize = DebugLineData.getU8(OffsetPtr);
247     assert((DebugLineData.getAddressSize() == 0 ||
248             DebugLineData.getAddressSize() == getAddressSize()) &&
249            "Line table header and data extractor disagree");
250     SegSelectorSize = DebugLineData.getU8(OffsetPtr);
251   }
252 
253   PrologueLength = DebugLineData.getUnsigned(OffsetPtr, sizeofPrologueLength());
254   const uint64_t EndPrologueOffset = PrologueLength + *OffsetPtr;
255   MinInstLength = DebugLineData.getU8(OffsetPtr);
256   if (getVersion() >= 4)
257     MaxOpsPerInst = DebugLineData.getU8(OffsetPtr);
258   DefaultIsStmt = DebugLineData.getU8(OffsetPtr);
259   LineBase = DebugLineData.getU8(OffsetPtr);
260   LineRange = DebugLineData.getU8(OffsetPtr);
261   OpcodeBase = DebugLineData.getU8(OffsetPtr);
262 
263   StandardOpcodeLengths.reserve(OpcodeBase - 1);
264   for (uint32_t I = 1; I < OpcodeBase; ++I) {
265     uint8_t OpLen = DebugLineData.getU8(OffsetPtr);
266     StandardOpcodeLengths.push_back(OpLen);
267   }
268 
269   if (getVersion() >= 5) {
270     if (!parseV5DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
271                               FormParams, U, HasMD5, IncludeDirectories,
272                               FileNames)) {
273       fprintf(stderr,
274               "warning: parsing line table prologue at 0x%8.8" PRIx64
275               " found an invalid directory or file table description at"
276               " 0x%8.8" PRIx64 "\n", PrologueOffset, (uint64_t)*OffsetPtr);
277       return false;
278     }
279   } else
280     parseV2DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
281                          IncludeDirectories, FileNames);
282 
283   if (*OffsetPtr != EndPrologueOffset) {
284     fprintf(stderr,
285             "warning: parsing line table prologue at 0x%8.8" PRIx64
286             " should have ended at 0x%8.8" PRIx64
287             " but it ended at 0x%8.8" PRIx64 "\n",
288             PrologueOffset, EndPrologueOffset, (uint64_t)*OffsetPtr);
289     return false;
290   }
291   return true;
292 }
293 
294 DWARFDebugLine::Row::Row(bool DefaultIsStmt) { reset(DefaultIsStmt); }
295 
296 void DWARFDebugLine::Row::postAppend() {
297   BasicBlock = false;
298   PrologueEnd = false;
299   EpilogueBegin = false;
300 }
301 
302 void DWARFDebugLine::Row::reset(bool DefaultIsStmt) {
303   Address = 0;
304   Line = 1;
305   Column = 0;
306   File = 1;
307   Isa = 0;
308   Discriminator = 0;
309   IsStmt = DefaultIsStmt;
310   BasicBlock = false;
311   EndSequence = false;
312   PrologueEnd = false;
313   EpilogueBegin = false;
314 }
315 
316 void DWARFDebugLine::Row::dumpTableHeader(raw_ostream &OS) {
317   OS << "Address            Line   Column File   ISA Discriminator Flags\n"
318      << "------------------ ------ ------ ------ --- ------------- "
319         "-------------\n";
320 }
321 
322 void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
323   OS << format("0x%16.16" PRIx64 " %6u %6u", Address, Line, Column)
324      << format(" %6u %3u %13u ", File, Isa, Discriminator)
325      << (IsStmt ? " is_stmt" : "") << (BasicBlock ? " basic_block" : "")
326      << (PrologueEnd ? " prologue_end" : "")
327      << (EpilogueBegin ? " epilogue_begin" : "")
328      << (EndSequence ? " end_sequence" : "") << '\n';
329 }
330 
331 DWARFDebugLine::Sequence::Sequence() { reset(); }
332 
333 void DWARFDebugLine::Sequence::reset() {
334   LowPC = 0;
335   HighPC = 0;
336   FirstRowIndex = 0;
337   LastRowIndex = 0;
338   Empty = true;
339 }
340 
341 DWARFDebugLine::LineTable::LineTable() { clear(); }
342 
343 void DWARFDebugLine::LineTable::dump(raw_ostream &OS) const {
344   Prologue.dump(OS);
345   OS << '\n';
346 
347   if (!Rows.empty()) {
348     Row::dumpTableHeader(OS);
349     for (const Row &R : Rows) {
350       R.dump(OS);
351     }
352   }
353 }
354 
355 void DWARFDebugLine::LineTable::clear() {
356   Prologue.clear();
357   Rows.clear();
358   Sequences.clear();
359 }
360 
361 DWARFDebugLine::ParsingState::ParsingState(struct LineTable *LT)
362     : LineTable(LT) {
363   resetRowAndSequence();
364 }
365 
366 void DWARFDebugLine::ParsingState::resetRowAndSequence() {
367   Row.reset(LineTable->Prologue.DefaultIsStmt);
368   Sequence.reset();
369 }
370 
371 void DWARFDebugLine::ParsingState::appendRowToMatrix(uint32_t Offset) {
372   if (Sequence.Empty) {
373     // Record the beginning of instruction sequence.
374     Sequence.Empty = false;
375     Sequence.LowPC = Row.Address;
376     Sequence.FirstRowIndex = RowNumber;
377   }
378   ++RowNumber;
379   LineTable->appendRow(Row);
380   if (Row.EndSequence) {
381     // Record the end of instruction sequence.
382     Sequence.HighPC = Row.Address;
383     Sequence.LastRowIndex = RowNumber;
384     if (Sequence.isValid())
385       LineTable->appendSequence(Sequence);
386     Sequence.reset();
387   }
388   Row.postAppend();
389 }
390 
391 const DWARFDebugLine::LineTable *
392 DWARFDebugLine::getLineTable(uint32_t Offset) const {
393   LineTableConstIter Pos = LineTableMap.find(Offset);
394   if (Pos != LineTableMap.end())
395     return &Pos->second;
396   return nullptr;
397 }
398 
399 const DWARFDebugLine::LineTable *
400 DWARFDebugLine::getOrParseLineTable(DWARFDataExtractor &DebugLineData,
401                                     uint32_t Offset, const DWARFUnit *U) {
402   std::pair<LineTableIter, bool> Pos =
403       LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable()));
404   LineTable *LT = &Pos.first->second;
405   if (Pos.second) {
406     if (!LT->parse(DebugLineData, &Offset, U))
407       return nullptr;
408   }
409   return LT;
410 }
411 
412 bool DWARFDebugLine::LineTable::parse(DWARFDataExtractor &DebugLineData,
413                                       uint32_t *OffsetPtr, const DWARFUnit *U,
414                                       raw_ostream *OS) {
415   const uint32_t DebugLineOffset = *OffsetPtr;
416 
417   clear();
418 
419   if (!Prologue.parse(DebugLineData, OffsetPtr, U)) {
420     // Restore our offset and return false to indicate failure!
421     *OffsetPtr = DebugLineOffset;
422     return false;
423   }
424 
425   if (OS)
426     Prologue.dump(*OS);
427 
428   const uint32_t EndOffset =
429       DebugLineOffset + Prologue.TotalLength + Prologue.sizeofTotalLength();
430 
431   // See if we should tell the data extractor the address size.
432   if (DebugLineData.getAddressSize() == 0)
433     DebugLineData.setAddressSize(Prologue.getAddressSize());
434   else
435     assert(Prologue.getAddressSize() == 0 ||
436            Prologue.getAddressSize() == DebugLineData.getAddressSize());
437 
438   ParsingState State(this);
439 
440   while (*OffsetPtr < EndOffset) {
441     if (OS)
442       *OS << format("0x%08.08" PRIx32 ": ", *OffsetPtr);
443 
444     uint8_t Opcode = DebugLineData.getU8(OffsetPtr);
445 
446     if (OS)
447       *OS << format("%02.02" PRIx8 " ", Opcode);
448 
449     if (Opcode == 0) {
450       // Extended Opcodes always start with a zero opcode followed by
451       // a uleb128 length so you can skip ones you don't know about
452       uint64_t Len = DebugLineData.getULEB128(OffsetPtr);
453       uint32_t ExtOffset = *OffsetPtr;
454 
455       // Tolerate zero-length; assume length is correct and soldier on.
456       if (Len == 0) {
457         if (OS)
458           *OS << "Badly formed extended line op (length 0)\n";
459         continue;
460       }
461 
462       uint8_t SubOpcode = DebugLineData.getU8(OffsetPtr);
463       if (OS)
464         *OS << LNExtendedString(SubOpcode);
465       switch (SubOpcode) {
466       case DW_LNE_end_sequence:
467         // Set the end_sequence register of the state machine to true and
468         // append a row to the matrix using the current values of the
469         // state-machine registers. Then reset the registers to the initial
470         // values specified above. Every statement program sequence must end
471         // with a DW_LNE_end_sequence instruction which creates a row whose
472         // address is that of the byte after the last target machine instruction
473         // of the sequence.
474         State.Row.EndSequence = true;
475         State.appendRowToMatrix(*OffsetPtr);
476         if (OS) {
477           *OS << "\n";
478           OS->indent(12);
479           State.Row.dump(*OS);
480         }
481         State.resetRowAndSequence();
482         break;
483 
484       case DW_LNE_set_address:
485         // Takes a single relocatable address as an operand. The size of the
486         // operand is the size appropriate to hold an address on the target
487         // machine. Set the address register to the value given by the
488         // relocatable address. All of the other statement program opcodes
489         // that affect the address register add a delta to it. This instruction
490         // stores a relocatable value into it instead.
491         //
492         // Make sure the extractor knows the address size.  If not, infer it
493         // from the size of the operand.
494         if (DebugLineData.getAddressSize() == 0)
495           DebugLineData.setAddressSize(Len - 1);
496         else
497           assert(DebugLineData.getAddressSize() == Len - 1);
498         State.Row.Address = DebugLineData.getRelocatedAddress(OffsetPtr);
499         if (OS)
500           *OS << format(" (0x%16.16" PRIx64 ")", State.Row.Address);
501         break;
502 
503       case DW_LNE_define_file:
504         // Takes 4 arguments. The first is a null terminated string containing
505         // a source file name. The second is an unsigned LEB128 number
506         // representing the directory index of the directory in which the file
507         // was found. The third is an unsigned LEB128 number representing the
508         // time of last modification of the file. The fourth is an unsigned
509         // LEB128 number representing the length in bytes of the file. The time
510         // and length fields may contain LEB128(0) if the information is not
511         // available.
512         //
513         // The directory index represents an entry in the include_directories
514         // section of the statement program prologue. The index is LEB128(0)
515         // if the file was found in the current directory of the compilation,
516         // LEB128(1) if it was found in the first directory in the
517         // include_directories section, and so on. The directory index is
518         // ignored for file names that represent full path names.
519         //
520         // The files are numbered, starting at 1, in the order in which they
521         // appear; the names in the prologue come before names defined by
522         // the DW_LNE_define_file instruction. These numbers are used in the
523         // the file register of the state machine.
524         {
525           FileNameEntry FileEntry;
526           FileEntry.Name = DebugLineData.getCStr(OffsetPtr);
527           FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
528           FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
529           FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
530           Prologue.FileNames.push_back(FileEntry);
531           if (OS)
532             *OS << " (" << FileEntry.Name.str()
533                 << ", dir=" << FileEntry.DirIdx << ", mod_time="
534                 << format("(0x%16.16" PRIx64 ")", FileEntry.ModTime)
535                 << ", length=" << FileEntry.Length << ")";
536         }
537         break;
538 
539       case DW_LNE_set_discriminator:
540         State.Row.Discriminator = DebugLineData.getULEB128(OffsetPtr);
541         if (OS)
542           *OS << " (" << State.Row.Discriminator << ")";
543         break;
544 
545       default:
546         if (OS)
547           *OS << format("Unrecognized extended op 0x%02.02" PRIx8, SubOpcode)
548               << format(" length %" PRIx64, Len);
549         // Len doesn't include the zero opcode byte or the length itself, but
550         // it does include the sub_opcode, so we have to adjust for that.
551         (*OffsetPtr) += Len - 1;
552         break;
553       }
554       // Make sure the stated and parsed lengths are the same.
555       // Otherwise we have an unparseable line-number program.
556       if (*OffsetPtr - ExtOffset != Len) {
557         fprintf(stderr, "Unexpected line op length at offset 0x%8.8" PRIx32
558                 " expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx32 "\n",
559                 ExtOffset, Len, *OffsetPtr - ExtOffset);
560         // Skip the rest of the line-number program.
561         *OffsetPtr = EndOffset;
562         return false;
563       }
564     } else if (Opcode < Prologue.OpcodeBase) {
565       if (OS)
566         *OS << LNStandardString(Opcode);
567       switch (Opcode) {
568       // Standard Opcodes
569       case DW_LNS_copy:
570         // Takes no arguments. Append a row to the matrix using the
571         // current values of the state-machine registers. Then set
572         // the basic_block register to false.
573         State.appendRowToMatrix(*OffsetPtr);
574         if (OS) {
575           *OS << "\n";
576           OS->indent(12);
577           State.Row.dump(*OS);
578           *OS << "\n";
579         }
580         break;
581 
582       case DW_LNS_advance_pc:
583         // Takes a single unsigned LEB128 operand, multiplies it by the
584         // min_inst_length field of the prologue, and adds the
585         // result to the address register of the state machine.
586         {
587           uint64_t AddrOffset =
588               DebugLineData.getULEB128(OffsetPtr) * Prologue.MinInstLength;
589           State.Row.Address += AddrOffset;
590           if (OS)
591             *OS << " (" << AddrOffset << ")";
592         }
593         break;
594 
595       case DW_LNS_advance_line:
596         // Takes a single signed LEB128 operand and adds that value to
597         // the line register of the state machine.
598         State.Row.Line += DebugLineData.getSLEB128(OffsetPtr);
599         if (OS)
600           *OS << " (" << State.Row.Line << ")";
601         break;
602 
603       case DW_LNS_set_file:
604         // Takes a single unsigned LEB128 operand and stores it in the file
605         // register of the state machine.
606         State.Row.File = DebugLineData.getULEB128(OffsetPtr);
607         if (OS)
608           *OS << " (" << State.Row.File << ")";
609         break;
610 
611       case DW_LNS_set_column:
612         // Takes a single unsigned LEB128 operand and stores it in the
613         // column register of the state machine.
614         State.Row.Column = DebugLineData.getULEB128(OffsetPtr);
615         if (OS)
616           *OS << " (" << State.Row.Column << ")";
617         break;
618 
619       case DW_LNS_negate_stmt:
620         // Takes no arguments. Set the is_stmt register of the state
621         // machine to the logical negation of its current value.
622         State.Row.IsStmt = !State.Row.IsStmt;
623         break;
624 
625       case DW_LNS_set_basic_block:
626         // Takes no arguments. Set the basic_block register of the
627         // state machine to true
628         State.Row.BasicBlock = true;
629         break;
630 
631       case DW_LNS_const_add_pc:
632         // Takes no arguments. Add to the address register of the state
633         // machine the address increment value corresponding to special
634         // opcode 255. The motivation for DW_LNS_const_add_pc is this:
635         // when the statement program needs to advance the address by a
636         // small amount, it can use a single special opcode, which occupies
637         // a single byte. When it needs to advance the address by up to
638         // twice the range of the last special opcode, it can use
639         // DW_LNS_const_add_pc followed by a special opcode, for a total
640         // of two bytes. Only if it needs to advance the address by more
641         // than twice that range will it need to use both DW_LNS_advance_pc
642         // and a special opcode, requiring three or more bytes.
643         {
644           uint8_t AdjustOpcode = 255 - Prologue.OpcodeBase;
645           uint64_t AddrOffset =
646               (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
647           State.Row.Address += AddrOffset;
648           if (OS)
649             *OS
650                 << format(" (0x%16.16" PRIx64 ")", AddrOffset);
651         }
652         break;
653 
654       case DW_LNS_fixed_advance_pc:
655         // Takes a single uhalf operand. Add to the address register of
656         // the state machine the value of the (unencoded) operand. This
657         // is the only extended opcode that takes an argument that is not
658         // a variable length number. The motivation for DW_LNS_fixed_advance_pc
659         // is this: existing assemblers cannot emit DW_LNS_advance_pc or
660         // special opcodes because they cannot encode LEB128 numbers or
661         // judge when the computation of a special opcode overflows and
662         // requires the use of DW_LNS_advance_pc. Such assemblers, however,
663         // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
664         {
665           uint16_t PCOffset = DebugLineData.getU16(OffsetPtr);
666           State.Row.Address += PCOffset;
667           if (OS)
668             *OS
669                 << format(" (0x%16.16" PRIx64 ")", PCOffset);
670         }
671         break;
672 
673       case DW_LNS_set_prologue_end:
674         // Takes no arguments. Set the prologue_end register of the
675         // state machine to true
676         State.Row.PrologueEnd = true;
677         break;
678 
679       case DW_LNS_set_epilogue_begin:
680         // Takes no arguments. Set the basic_block register of the
681         // state machine to true
682         State.Row.EpilogueBegin = true;
683         break;
684 
685       case DW_LNS_set_isa:
686         // Takes a single unsigned LEB128 operand and stores it in the
687         // column register of the state machine.
688         State.Row.Isa = DebugLineData.getULEB128(OffsetPtr);
689         if (OS)
690           *OS << " (" << State.Row.Isa << ")";
691         break;
692 
693       default:
694         // Handle any unknown standard opcodes here. We know the lengths
695         // of such opcodes because they are specified in the prologue
696         // as a multiple of LEB128 operands for each opcode.
697         {
698           assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size());
699           uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1];
700           for (uint8_t I = 0; I < OpcodeLength; ++I) {
701             uint64_t Value = DebugLineData.getULEB128(OffsetPtr);
702             if (OS)
703               *OS << format("Skipping ULEB128 value: 0x%16.16" PRIx64 ")\n",
704                             Value);
705           }
706         }
707         break;
708       }
709     } else {
710       // Special Opcodes
711 
712       // A special opcode value is chosen based on the amount that needs
713       // to be added to the line and address registers. The maximum line
714       // increment for a special opcode is the value of the line_base
715       // field in the header, plus the value of the line_range field,
716       // minus 1 (line base + line range - 1). If the desired line
717       // increment is greater than the maximum line increment, a standard
718       // opcode must be used instead of a special opcode. The "address
719       // advance" is calculated by dividing the desired address increment
720       // by the minimum_instruction_length field from the header. The
721       // special opcode is then calculated using the following formula:
722       //
723       //  opcode = (desired line increment - line_base) +
724       //           (line_range * address advance) + opcode_base
725       //
726       // If the resulting opcode is greater than 255, a standard opcode
727       // must be used instead.
728       //
729       // To decode a special opcode, subtract the opcode_base from the
730       // opcode itself to give the adjusted opcode. The amount to
731       // increment the address register is the result of the adjusted
732       // opcode divided by the line_range multiplied by the
733       // minimum_instruction_length field from the header. That is:
734       //
735       //  address increment = (adjusted opcode / line_range) *
736       //                      minimum_instruction_length
737       //
738       // The amount to increment the line register is the line_base plus
739       // the result of the adjusted opcode modulo the line_range. That is:
740       //
741       // line increment = line_base + (adjusted opcode % line_range)
742 
743       uint8_t AdjustOpcode = Opcode - Prologue.OpcodeBase;
744       uint64_t AddrOffset =
745           (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
746       int32_t LineOffset =
747           Prologue.LineBase + (AdjustOpcode % Prologue.LineRange);
748       State.Row.Line += LineOffset;
749       State.Row.Address += AddrOffset;
750 
751       if (OS) {
752         *OS << "address += " << ((uint32_t)AdjustOpcode)
753             << ",  line += " << LineOffset << "\n";
754         OS->indent(12);
755         State.Row.dump(*OS);
756       }
757 
758       State.appendRowToMatrix(*OffsetPtr);
759       // Reset discriminator to 0.
760       State.Row.Discriminator = 0;
761     }
762     if(OS)
763       *OS << "\n";
764   }
765 
766   if (!State.Sequence.Empty) {
767     fprintf(stderr, "warning: last sequence in debug line table is not"
768                     "terminated!\n");
769   }
770 
771   // Sort all sequences so that address lookup will work faster.
772   if (!Sequences.empty()) {
773     std::sort(Sequences.begin(), Sequences.end(), Sequence::orderByLowPC);
774     // Note: actually, instruction address ranges of sequences should not
775     // overlap (in shared objects and executables). If they do, the address
776     // lookup would still work, though, but result would be ambiguous.
777     // We don't report warning in this case. For example,
778     // sometimes .so compiled from multiple object files contains a few
779     // rudimentary sequences for address ranges [0x0, 0xsomething).
780   }
781 
782   return EndOffset;
783 }
784 
785 uint32_t
786 DWARFDebugLine::LineTable::findRowInSeq(const DWARFDebugLine::Sequence &Seq,
787                                         uint64_t Address) const {
788   if (!Seq.containsPC(Address))
789     return UnknownRowIndex;
790   // Search for instruction address in the rows describing the sequence.
791   // Rows are stored in a vector, so we may use arithmetical operations with
792   // iterators.
793   DWARFDebugLine::Row Row;
794   Row.Address = Address;
795   RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex;
796   RowIter LastRow = Rows.begin() + Seq.LastRowIndex;
797   LineTable::RowIter RowPos = std::lower_bound(
798       FirstRow, LastRow, Row, DWARFDebugLine::Row::orderByAddress);
799   if (RowPos == LastRow) {
800     return Seq.LastRowIndex - 1;
801   }
802   uint32_t Index = Seq.FirstRowIndex + (RowPos - FirstRow);
803   if (RowPos->Address > Address) {
804     if (RowPos == FirstRow)
805       return UnknownRowIndex;
806     else
807       Index--;
808   }
809   return Index;
810 }
811 
812 uint32_t DWARFDebugLine::LineTable::lookupAddress(uint64_t Address) const {
813   if (Sequences.empty())
814     return UnknownRowIndex;
815   // First, find an instruction sequence containing the given address.
816   DWARFDebugLine::Sequence Sequence;
817   Sequence.LowPC = Address;
818   SequenceIter FirstSeq = Sequences.begin();
819   SequenceIter LastSeq = Sequences.end();
820   SequenceIter SeqPos = std::lower_bound(
821       FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
822   DWARFDebugLine::Sequence FoundSeq;
823   if (SeqPos == LastSeq) {
824     FoundSeq = Sequences.back();
825   } else if (SeqPos->LowPC == Address) {
826     FoundSeq = *SeqPos;
827   } else {
828     if (SeqPos == FirstSeq)
829       return UnknownRowIndex;
830     FoundSeq = *(SeqPos - 1);
831   }
832   return findRowInSeq(FoundSeq, Address);
833 }
834 
835 bool DWARFDebugLine::LineTable::lookupAddressRange(
836     uint64_t Address, uint64_t Size, std::vector<uint32_t> &Result) const {
837   if (Sequences.empty())
838     return false;
839   uint64_t EndAddr = Address + Size;
840   // First, find an instruction sequence containing the given address.
841   DWARFDebugLine::Sequence Sequence;
842   Sequence.LowPC = Address;
843   SequenceIter FirstSeq = Sequences.begin();
844   SequenceIter LastSeq = Sequences.end();
845   SequenceIter SeqPos = std::lower_bound(
846       FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
847   if (SeqPos == LastSeq || SeqPos->LowPC != Address) {
848     if (SeqPos == FirstSeq)
849       return false;
850     SeqPos--;
851   }
852   if (!SeqPos->containsPC(Address))
853     return false;
854 
855   SequenceIter StartPos = SeqPos;
856 
857   // Add the rows from the first sequence to the vector, starting with the
858   // index we just calculated
859 
860   while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) {
861     const DWARFDebugLine::Sequence &CurSeq = *SeqPos;
862     // For the first sequence, we need to find which row in the sequence is the
863     // first in our range.
864     uint32_t FirstRowIndex = CurSeq.FirstRowIndex;
865     if (SeqPos == StartPos)
866       FirstRowIndex = findRowInSeq(CurSeq, Address);
867 
868     // Figure out the last row in the range.
869     uint32_t LastRowIndex = findRowInSeq(CurSeq, EndAddr - 1);
870     if (LastRowIndex == UnknownRowIndex)
871       LastRowIndex = CurSeq.LastRowIndex - 1;
872 
873     assert(FirstRowIndex != UnknownRowIndex);
874     assert(LastRowIndex != UnknownRowIndex);
875 
876     for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) {
877       Result.push_back(I);
878     }
879 
880     ++SeqPos;
881   }
882 
883   return true;
884 }
885 
886 bool DWARFDebugLine::LineTable::hasFileAtIndex(uint64_t FileIndex) const {
887   return FileIndex != 0 && FileIndex <= Prologue.FileNames.size();
888 }
889 
890 bool DWARFDebugLine::LineTable::getFileNameByIndex(uint64_t FileIndex,
891                                                    const char *CompDir,
892                                                    FileLineInfoKind Kind,
893                                                    std::string &Result) const {
894   if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
895     return false;
896   const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
897   StringRef FileName = Entry.Name;
898   if (Kind != FileLineInfoKind::AbsoluteFilePath ||
899       sys::path::is_absolute(FileName)) {
900     Result = FileName;
901     return true;
902   }
903 
904   SmallString<16> FilePath;
905   uint64_t IncludeDirIndex = Entry.DirIdx;
906   StringRef IncludeDir;
907   // Be defensive about the contents of Entry.
908   if (IncludeDirIndex > 0 &&
909       IncludeDirIndex <= Prologue.IncludeDirectories.size())
910     IncludeDir = Prologue.IncludeDirectories[IncludeDirIndex - 1];
911 
912   // We may still need to append compilation directory of compile unit.
913   // We know that FileName is not absolute, the only way to have an
914   // absolute path at this point would be if IncludeDir is absolute.
915   if (CompDir && Kind == FileLineInfoKind::AbsoluteFilePath &&
916       sys::path::is_relative(IncludeDir))
917     sys::path::append(FilePath, CompDir);
918 
919   // sys::path::append skips empty strings.
920   sys::path::append(FilePath, IncludeDir, FileName);
921   Result = FilePath.str();
922   return true;
923 }
924 
925 bool DWARFDebugLine::LineTable::getFileLineInfoForAddress(
926     uint64_t Address, const char *CompDir, FileLineInfoKind Kind,
927     DILineInfo &Result) const {
928   // Get the index of row we're looking for in the line table.
929   uint32_t RowIndex = lookupAddress(Address);
930   if (RowIndex == -1U)
931     return false;
932   // Take file number and line/column from the row.
933   const auto &Row = Rows[RowIndex];
934   if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName))
935     return false;
936   Result.Line = Row.Line;
937   Result.Column = Row.Column;
938   Result.Discriminator = Row.Discriminator;
939   return true;
940 }
941