xref: /aosp_15_r20/external/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- DWARFDebugLine.cpp ------------------------------------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker 
10*9880d681SAndroid Build Coastguard Worker #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
11*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Dwarf.h"
12*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Format.h"
13*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Path.h"
14*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
15*9880d681SAndroid Build Coastguard Worker #include <algorithm>
16*9880d681SAndroid Build Coastguard Worker using namespace llvm;
17*9880d681SAndroid Build Coastguard Worker using namespace dwarf;
18*9880d681SAndroid Build Coastguard Worker typedef DILineInfoSpecifier::FileLineInfoKind FileLineInfoKind;
19*9880d681SAndroid Build Coastguard Worker 
Prologue()20*9880d681SAndroid Build Coastguard Worker DWARFDebugLine::Prologue::Prologue() { clear(); }
21*9880d681SAndroid Build Coastguard Worker 
clear()22*9880d681SAndroid Build Coastguard Worker void DWARFDebugLine::Prologue::clear() {
23*9880d681SAndroid Build Coastguard Worker   TotalLength = Version = PrologueLength = 0;
24*9880d681SAndroid Build Coastguard Worker   MinInstLength = MaxOpsPerInst = DefaultIsStmt = LineBase = LineRange = 0;
25*9880d681SAndroid Build Coastguard Worker   OpcodeBase = 0;
26*9880d681SAndroid Build Coastguard Worker   IsDWARF64 = false;
27*9880d681SAndroid Build Coastguard Worker   StandardOpcodeLengths.clear();
28*9880d681SAndroid Build Coastguard Worker   IncludeDirectories.clear();
29*9880d681SAndroid Build Coastguard Worker   FileNames.clear();
30*9880d681SAndroid Build Coastguard Worker }
31*9880d681SAndroid Build Coastguard Worker 
dump(raw_ostream & OS) const32*9880d681SAndroid Build Coastguard Worker void DWARFDebugLine::Prologue::dump(raw_ostream &OS) const {
33*9880d681SAndroid Build Coastguard Worker   OS << "Line table prologue:\n"
34*9880d681SAndroid Build Coastguard Worker      << format("    total_length: 0x%8.8" PRIx64 "\n", TotalLength)
35*9880d681SAndroid Build Coastguard Worker      << format("         version: %u\n", Version)
36*9880d681SAndroid Build Coastguard Worker      << format(" prologue_length: 0x%8.8" PRIx64 "\n", PrologueLength)
37*9880d681SAndroid Build Coastguard Worker      << format(" min_inst_length: %u\n", MinInstLength)
38*9880d681SAndroid Build Coastguard Worker      << format(Version >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst)
39*9880d681SAndroid Build Coastguard Worker      << format(" default_is_stmt: %u\n", DefaultIsStmt)
40*9880d681SAndroid Build Coastguard Worker      << format("       line_base: %i\n", LineBase)
41*9880d681SAndroid Build Coastguard Worker      << format("      line_range: %u\n", LineRange)
42*9880d681SAndroid Build Coastguard Worker      << format("     opcode_base: %u\n", OpcodeBase);
43*9880d681SAndroid Build Coastguard Worker 
44*9880d681SAndroid Build Coastguard Worker   for (uint32_t i = 0; i < StandardOpcodeLengths.size(); ++i)
45*9880d681SAndroid Build Coastguard Worker     OS << format("standard_opcode_lengths[%s] = %u\n", LNStandardString(i + 1),
46*9880d681SAndroid Build Coastguard Worker                  StandardOpcodeLengths[i]);
47*9880d681SAndroid Build Coastguard Worker 
48*9880d681SAndroid Build Coastguard Worker   if (!IncludeDirectories.empty())
49*9880d681SAndroid Build Coastguard Worker     for (uint32_t i = 0; i < IncludeDirectories.size(); ++i)
50*9880d681SAndroid Build Coastguard Worker       OS << format("include_directories[%3u] = '", i + 1)
51*9880d681SAndroid Build Coastguard Worker          << IncludeDirectories[i] << "'\n";
52*9880d681SAndroid Build Coastguard Worker 
53*9880d681SAndroid Build Coastguard Worker   if (!FileNames.empty()) {
54*9880d681SAndroid Build Coastguard Worker     OS << "                Dir  Mod Time   File Len   File Name\n"
55*9880d681SAndroid Build Coastguard Worker        << "                ---- ---------- ---------- -----------"
56*9880d681SAndroid Build Coastguard Worker           "----------------\n";
57*9880d681SAndroid Build Coastguard Worker     for (uint32_t i = 0; i < FileNames.size(); ++i) {
58*9880d681SAndroid Build Coastguard Worker       const FileNameEntry &fileEntry = FileNames[i];
59*9880d681SAndroid Build Coastguard Worker       OS << format("file_names[%3u] %4" PRIu64 " ", i + 1, fileEntry.DirIdx)
60*9880d681SAndroid Build Coastguard Worker          << format("0x%8.8" PRIx64 " 0x%8.8" PRIx64 " ", fileEntry.ModTime,
61*9880d681SAndroid Build Coastguard Worker                    fileEntry.Length)
62*9880d681SAndroid Build Coastguard Worker          << fileEntry.Name << '\n';
63*9880d681SAndroid Build Coastguard Worker     }
64*9880d681SAndroid Build Coastguard Worker   }
65*9880d681SAndroid Build Coastguard Worker }
66*9880d681SAndroid Build Coastguard Worker 
parse(DataExtractor debug_line_data,uint32_t * offset_ptr)67*9880d681SAndroid Build Coastguard Worker bool DWARFDebugLine::Prologue::parse(DataExtractor debug_line_data,
68*9880d681SAndroid Build Coastguard Worker                                      uint32_t *offset_ptr) {
69*9880d681SAndroid Build Coastguard Worker   const uint64_t prologue_offset = *offset_ptr;
70*9880d681SAndroid Build Coastguard Worker 
71*9880d681SAndroid Build Coastguard Worker   clear();
72*9880d681SAndroid Build Coastguard Worker   TotalLength = debug_line_data.getU32(offset_ptr);
73*9880d681SAndroid Build Coastguard Worker   if (TotalLength == UINT32_MAX) {
74*9880d681SAndroid Build Coastguard Worker     IsDWARF64 = true;
75*9880d681SAndroid Build Coastguard Worker     TotalLength = debug_line_data.getU64(offset_ptr);
76*9880d681SAndroid Build Coastguard Worker   } else if (TotalLength > 0xffffff00) {
77*9880d681SAndroid Build Coastguard Worker     return false;
78*9880d681SAndroid Build Coastguard Worker   }
79*9880d681SAndroid Build Coastguard Worker   Version = debug_line_data.getU16(offset_ptr);
80*9880d681SAndroid Build Coastguard Worker   if (Version < 2)
81*9880d681SAndroid Build Coastguard Worker     return false;
82*9880d681SAndroid Build Coastguard Worker 
83*9880d681SAndroid Build Coastguard Worker   PrologueLength =
84*9880d681SAndroid Build Coastguard Worker       debug_line_data.getUnsigned(offset_ptr, sizeofPrologueLength());
85*9880d681SAndroid Build Coastguard Worker   const uint64_t end_prologue_offset = PrologueLength + *offset_ptr;
86*9880d681SAndroid Build Coastguard Worker   MinInstLength = debug_line_data.getU8(offset_ptr);
87*9880d681SAndroid Build Coastguard Worker   if (Version >= 4)
88*9880d681SAndroid Build Coastguard Worker     MaxOpsPerInst = debug_line_data.getU8(offset_ptr);
89*9880d681SAndroid Build Coastguard Worker   DefaultIsStmt = debug_line_data.getU8(offset_ptr);
90*9880d681SAndroid Build Coastguard Worker   LineBase = debug_line_data.getU8(offset_ptr);
91*9880d681SAndroid Build Coastguard Worker   LineRange = debug_line_data.getU8(offset_ptr);
92*9880d681SAndroid Build Coastguard Worker   OpcodeBase = debug_line_data.getU8(offset_ptr);
93*9880d681SAndroid Build Coastguard Worker 
94*9880d681SAndroid Build Coastguard Worker   StandardOpcodeLengths.reserve(OpcodeBase - 1);
95*9880d681SAndroid Build Coastguard Worker   for (uint32_t i = 1; i < OpcodeBase; ++i) {
96*9880d681SAndroid Build Coastguard Worker     uint8_t op_len = debug_line_data.getU8(offset_ptr);
97*9880d681SAndroid Build Coastguard Worker     StandardOpcodeLengths.push_back(op_len);
98*9880d681SAndroid Build Coastguard Worker   }
99*9880d681SAndroid Build Coastguard Worker 
100*9880d681SAndroid Build Coastguard Worker   while (*offset_ptr < end_prologue_offset) {
101*9880d681SAndroid Build Coastguard Worker     const char *s = debug_line_data.getCStr(offset_ptr);
102*9880d681SAndroid Build Coastguard Worker     if (s && s[0])
103*9880d681SAndroid Build Coastguard Worker       IncludeDirectories.push_back(s);
104*9880d681SAndroid Build Coastguard Worker     else
105*9880d681SAndroid Build Coastguard Worker       break;
106*9880d681SAndroid Build Coastguard Worker   }
107*9880d681SAndroid Build Coastguard Worker 
108*9880d681SAndroid Build Coastguard Worker   while (*offset_ptr < end_prologue_offset) {
109*9880d681SAndroid Build Coastguard Worker     const char *name = debug_line_data.getCStr(offset_ptr);
110*9880d681SAndroid Build Coastguard Worker     if (name && name[0]) {
111*9880d681SAndroid Build Coastguard Worker       FileNameEntry fileEntry;
112*9880d681SAndroid Build Coastguard Worker       fileEntry.Name = name;
113*9880d681SAndroid Build Coastguard Worker       fileEntry.DirIdx = debug_line_data.getULEB128(offset_ptr);
114*9880d681SAndroid Build Coastguard Worker       fileEntry.ModTime = debug_line_data.getULEB128(offset_ptr);
115*9880d681SAndroid Build Coastguard Worker       fileEntry.Length = debug_line_data.getULEB128(offset_ptr);
116*9880d681SAndroid Build Coastguard Worker       FileNames.push_back(fileEntry);
117*9880d681SAndroid Build Coastguard Worker     } else {
118*9880d681SAndroid Build Coastguard Worker       break;
119*9880d681SAndroid Build Coastguard Worker     }
120*9880d681SAndroid Build Coastguard Worker   }
121*9880d681SAndroid Build Coastguard Worker 
122*9880d681SAndroid Build Coastguard Worker   if (*offset_ptr != end_prologue_offset) {
123*9880d681SAndroid Build Coastguard Worker     fprintf(stderr, "warning: parsing line table prologue at 0x%8.8" PRIx64
124*9880d681SAndroid Build Coastguard Worker                     " should have ended at 0x%8.8" PRIx64
125*9880d681SAndroid Build Coastguard Worker                     " but it ended at 0x%8.8" PRIx64 "\n",
126*9880d681SAndroid Build Coastguard Worker             prologue_offset, end_prologue_offset, (uint64_t)*offset_ptr);
127*9880d681SAndroid Build Coastguard Worker     return false;
128*9880d681SAndroid Build Coastguard Worker   }
129*9880d681SAndroid Build Coastguard Worker   return true;
130*9880d681SAndroid Build Coastguard Worker }
131*9880d681SAndroid Build Coastguard Worker 
Row(bool default_is_stmt)132*9880d681SAndroid Build Coastguard Worker DWARFDebugLine::Row::Row(bool default_is_stmt) { reset(default_is_stmt); }
133*9880d681SAndroid Build Coastguard Worker 
postAppend()134*9880d681SAndroid Build Coastguard Worker void DWARFDebugLine::Row::postAppend() {
135*9880d681SAndroid Build Coastguard Worker   BasicBlock = false;
136*9880d681SAndroid Build Coastguard Worker   PrologueEnd = false;
137*9880d681SAndroid Build Coastguard Worker   EpilogueBegin = false;
138*9880d681SAndroid Build Coastguard Worker }
139*9880d681SAndroid Build Coastguard Worker 
reset(bool default_is_stmt)140*9880d681SAndroid Build Coastguard Worker void DWARFDebugLine::Row::reset(bool default_is_stmt) {
141*9880d681SAndroid Build Coastguard Worker   Address = 0;
142*9880d681SAndroid Build Coastguard Worker   Line = 1;
143*9880d681SAndroid Build Coastguard Worker   Column = 0;
144*9880d681SAndroid Build Coastguard Worker   File = 1;
145*9880d681SAndroid Build Coastguard Worker   Isa = 0;
146*9880d681SAndroid Build Coastguard Worker   Discriminator = 0;
147*9880d681SAndroid Build Coastguard Worker   IsStmt = default_is_stmt;
148*9880d681SAndroid Build Coastguard Worker   BasicBlock = false;
149*9880d681SAndroid Build Coastguard Worker   EndSequence = false;
150*9880d681SAndroid Build Coastguard Worker   PrologueEnd = false;
151*9880d681SAndroid Build Coastguard Worker   EpilogueBegin = false;
152*9880d681SAndroid Build Coastguard Worker }
153*9880d681SAndroid Build Coastguard Worker 
dump(raw_ostream & OS) const154*9880d681SAndroid Build Coastguard Worker void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
155*9880d681SAndroid Build Coastguard Worker   OS << format("0x%16.16" PRIx64 " %6u %6u", Address, Line, Column)
156*9880d681SAndroid Build Coastguard Worker      << format(" %6u %3u %13u ", File, Isa, Discriminator)
157*9880d681SAndroid Build Coastguard Worker      << (IsStmt ? " is_stmt" : "") << (BasicBlock ? " basic_block" : "")
158*9880d681SAndroid Build Coastguard Worker      << (PrologueEnd ? " prologue_end" : "")
159*9880d681SAndroid Build Coastguard Worker      << (EpilogueBegin ? " epilogue_begin" : "")
160*9880d681SAndroid Build Coastguard Worker      << (EndSequence ? " end_sequence" : "") << '\n';
161*9880d681SAndroid Build Coastguard Worker }
162*9880d681SAndroid Build Coastguard Worker 
Sequence()163*9880d681SAndroid Build Coastguard Worker DWARFDebugLine::Sequence::Sequence() { reset(); }
164*9880d681SAndroid Build Coastguard Worker 
reset()165*9880d681SAndroid Build Coastguard Worker void DWARFDebugLine::Sequence::reset() {
166*9880d681SAndroid Build Coastguard Worker   LowPC = 0;
167*9880d681SAndroid Build Coastguard Worker   HighPC = 0;
168*9880d681SAndroid Build Coastguard Worker   FirstRowIndex = 0;
169*9880d681SAndroid Build Coastguard Worker   LastRowIndex = 0;
170*9880d681SAndroid Build Coastguard Worker   Empty = true;
171*9880d681SAndroid Build Coastguard Worker }
172*9880d681SAndroid Build Coastguard Worker 
LineTable()173*9880d681SAndroid Build Coastguard Worker DWARFDebugLine::LineTable::LineTable() { clear(); }
174*9880d681SAndroid Build Coastguard Worker 
dump(raw_ostream & OS) const175*9880d681SAndroid Build Coastguard Worker void DWARFDebugLine::LineTable::dump(raw_ostream &OS) const {
176*9880d681SAndroid Build Coastguard Worker   Prologue.dump(OS);
177*9880d681SAndroid Build Coastguard Worker   OS << '\n';
178*9880d681SAndroid Build Coastguard Worker 
179*9880d681SAndroid Build Coastguard Worker   if (!Rows.empty()) {
180*9880d681SAndroid Build Coastguard Worker     OS << "Address            Line   Column File   ISA Discriminator Flags\n"
181*9880d681SAndroid Build Coastguard Worker        << "------------------ ------ ------ ------ --- ------------- "
182*9880d681SAndroid Build Coastguard Worker           "-------------\n";
183*9880d681SAndroid Build Coastguard Worker     for (const Row &R : Rows) {
184*9880d681SAndroid Build Coastguard Worker       R.dump(OS);
185*9880d681SAndroid Build Coastguard Worker     }
186*9880d681SAndroid Build Coastguard Worker   }
187*9880d681SAndroid Build Coastguard Worker }
188*9880d681SAndroid Build Coastguard Worker 
clear()189*9880d681SAndroid Build Coastguard Worker void DWARFDebugLine::LineTable::clear() {
190*9880d681SAndroid Build Coastguard Worker   Prologue.clear();
191*9880d681SAndroid Build Coastguard Worker   Rows.clear();
192*9880d681SAndroid Build Coastguard Worker   Sequences.clear();
193*9880d681SAndroid Build Coastguard Worker }
194*9880d681SAndroid Build Coastguard Worker 
ParsingState(struct LineTable * LT)195*9880d681SAndroid Build Coastguard Worker DWARFDebugLine::ParsingState::ParsingState(struct LineTable *LT)
196*9880d681SAndroid Build Coastguard Worker     : LineTable(LT), RowNumber(0) {
197*9880d681SAndroid Build Coastguard Worker   resetRowAndSequence();
198*9880d681SAndroid Build Coastguard Worker }
199*9880d681SAndroid Build Coastguard Worker 
resetRowAndSequence()200*9880d681SAndroid Build Coastguard Worker void DWARFDebugLine::ParsingState::resetRowAndSequence() {
201*9880d681SAndroid Build Coastguard Worker   Row.reset(LineTable->Prologue.DefaultIsStmt);
202*9880d681SAndroid Build Coastguard Worker   Sequence.reset();
203*9880d681SAndroid Build Coastguard Worker }
204*9880d681SAndroid Build Coastguard Worker 
appendRowToMatrix(uint32_t offset)205*9880d681SAndroid Build Coastguard Worker void DWARFDebugLine::ParsingState::appendRowToMatrix(uint32_t offset) {
206*9880d681SAndroid Build Coastguard Worker   if (Sequence.Empty) {
207*9880d681SAndroid Build Coastguard Worker     // Record the beginning of instruction sequence.
208*9880d681SAndroid Build Coastguard Worker     Sequence.Empty = false;
209*9880d681SAndroid Build Coastguard Worker     Sequence.LowPC = Row.Address;
210*9880d681SAndroid Build Coastguard Worker     Sequence.FirstRowIndex = RowNumber;
211*9880d681SAndroid Build Coastguard Worker   }
212*9880d681SAndroid Build Coastguard Worker   ++RowNumber;
213*9880d681SAndroid Build Coastguard Worker   LineTable->appendRow(Row);
214*9880d681SAndroid Build Coastguard Worker   if (Row.EndSequence) {
215*9880d681SAndroid Build Coastguard Worker     // Record the end of instruction sequence.
216*9880d681SAndroid Build Coastguard Worker     Sequence.HighPC = Row.Address;
217*9880d681SAndroid Build Coastguard Worker     Sequence.LastRowIndex = RowNumber;
218*9880d681SAndroid Build Coastguard Worker     if (Sequence.isValid())
219*9880d681SAndroid Build Coastguard Worker       LineTable->appendSequence(Sequence);
220*9880d681SAndroid Build Coastguard Worker     Sequence.reset();
221*9880d681SAndroid Build Coastguard Worker   }
222*9880d681SAndroid Build Coastguard Worker   Row.postAppend();
223*9880d681SAndroid Build Coastguard Worker }
224*9880d681SAndroid Build Coastguard Worker 
225*9880d681SAndroid Build Coastguard Worker const DWARFDebugLine::LineTable *
getLineTable(uint32_t offset) const226*9880d681SAndroid Build Coastguard Worker DWARFDebugLine::getLineTable(uint32_t offset) const {
227*9880d681SAndroid Build Coastguard Worker   LineTableConstIter pos = LineTableMap.find(offset);
228*9880d681SAndroid Build Coastguard Worker   if (pos != LineTableMap.end())
229*9880d681SAndroid Build Coastguard Worker     return &pos->second;
230*9880d681SAndroid Build Coastguard Worker   return nullptr;
231*9880d681SAndroid Build Coastguard Worker }
232*9880d681SAndroid Build Coastguard Worker 
233*9880d681SAndroid Build Coastguard Worker const DWARFDebugLine::LineTable *
getOrParseLineTable(DataExtractor debug_line_data,uint32_t offset)234*9880d681SAndroid Build Coastguard Worker DWARFDebugLine::getOrParseLineTable(DataExtractor debug_line_data,
235*9880d681SAndroid Build Coastguard Worker                                     uint32_t offset) {
236*9880d681SAndroid Build Coastguard Worker   std::pair<LineTableIter, bool> pos =
237*9880d681SAndroid Build Coastguard Worker       LineTableMap.insert(LineTableMapTy::value_type(offset, LineTable()));
238*9880d681SAndroid Build Coastguard Worker   LineTable *LT = &pos.first->second;
239*9880d681SAndroid Build Coastguard Worker   if (pos.second) {
240*9880d681SAndroid Build Coastguard Worker     if (!LT->parse(debug_line_data, RelocMap, &offset))
241*9880d681SAndroid Build Coastguard Worker       return nullptr;
242*9880d681SAndroid Build Coastguard Worker   }
243*9880d681SAndroid Build Coastguard Worker   return LT;
244*9880d681SAndroid Build Coastguard Worker }
245*9880d681SAndroid Build Coastguard Worker 
parse(DataExtractor debug_line_data,const RelocAddrMap * RMap,uint32_t * offset_ptr)246*9880d681SAndroid Build Coastguard Worker bool DWARFDebugLine::LineTable::parse(DataExtractor debug_line_data,
247*9880d681SAndroid Build Coastguard Worker                                       const RelocAddrMap *RMap,
248*9880d681SAndroid Build Coastguard Worker                                       uint32_t *offset_ptr) {
249*9880d681SAndroid Build Coastguard Worker   const uint32_t debug_line_offset = *offset_ptr;
250*9880d681SAndroid Build Coastguard Worker 
251*9880d681SAndroid Build Coastguard Worker   clear();
252*9880d681SAndroid Build Coastguard Worker 
253*9880d681SAndroid Build Coastguard Worker   if (!Prologue.parse(debug_line_data, offset_ptr)) {
254*9880d681SAndroid Build Coastguard Worker     // Restore our offset and return false to indicate failure!
255*9880d681SAndroid Build Coastguard Worker     *offset_ptr = debug_line_offset;
256*9880d681SAndroid Build Coastguard Worker     return false;
257*9880d681SAndroid Build Coastguard Worker   }
258*9880d681SAndroid Build Coastguard Worker 
259*9880d681SAndroid Build Coastguard Worker   const uint32_t end_offset =
260*9880d681SAndroid Build Coastguard Worker       debug_line_offset + Prologue.TotalLength + Prologue.sizeofTotalLength();
261*9880d681SAndroid Build Coastguard Worker 
262*9880d681SAndroid Build Coastguard Worker   ParsingState State(this);
263*9880d681SAndroid Build Coastguard Worker 
264*9880d681SAndroid Build Coastguard Worker   while (*offset_ptr < end_offset) {
265*9880d681SAndroid Build Coastguard Worker     uint8_t opcode = debug_line_data.getU8(offset_ptr);
266*9880d681SAndroid Build Coastguard Worker 
267*9880d681SAndroid Build Coastguard Worker     if (opcode == 0) {
268*9880d681SAndroid Build Coastguard Worker       // Extended Opcodes always start with a zero opcode followed by
269*9880d681SAndroid Build Coastguard Worker       // a uleb128 length so you can skip ones you don't know about
270*9880d681SAndroid Build Coastguard Worker       uint32_t ext_offset = *offset_ptr;
271*9880d681SAndroid Build Coastguard Worker       uint64_t len = debug_line_data.getULEB128(offset_ptr);
272*9880d681SAndroid Build Coastguard Worker       uint32_t arg_size = len - (*offset_ptr - ext_offset);
273*9880d681SAndroid Build Coastguard Worker 
274*9880d681SAndroid Build Coastguard Worker       uint8_t sub_opcode = debug_line_data.getU8(offset_ptr);
275*9880d681SAndroid Build Coastguard Worker       switch (sub_opcode) {
276*9880d681SAndroid Build Coastguard Worker       case DW_LNE_end_sequence:
277*9880d681SAndroid Build Coastguard Worker         // Set the end_sequence register of the state machine to true and
278*9880d681SAndroid Build Coastguard Worker         // append a row to the matrix using the current values of the
279*9880d681SAndroid Build Coastguard Worker         // state-machine registers. Then reset the registers to the initial
280*9880d681SAndroid Build Coastguard Worker         // values specified above. Every statement program sequence must end
281*9880d681SAndroid Build Coastguard Worker         // with a DW_LNE_end_sequence instruction which creates a row whose
282*9880d681SAndroid Build Coastguard Worker         // address is that of the byte after the last target machine instruction
283*9880d681SAndroid Build Coastguard Worker         // of the sequence.
284*9880d681SAndroid Build Coastguard Worker         State.Row.EndSequence = true;
285*9880d681SAndroid Build Coastguard Worker         State.appendRowToMatrix(*offset_ptr);
286*9880d681SAndroid Build Coastguard Worker         State.resetRowAndSequence();
287*9880d681SAndroid Build Coastguard Worker         break;
288*9880d681SAndroid Build Coastguard Worker 
289*9880d681SAndroid Build Coastguard Worker       case DW_LNE_set_address:
290*9880d681SAndroid Build Coastguard Worker         // Takes a single relocatable address as an operand. The size of the
291*9880d681SAndroid Build Coastguard Worker         // operand is the size appropriate to hold an address on the target
292*9880d681SAndroid Build Coastguard Worker         // machine. Set the address register to the value given by the
293*9880d681SAndroid Build Coastguard Worker         // relocatable address. All of the other statement program opcodes
294*9880d681SAndroid Build Coastguard Worker         // that affect the address register add a delta to it. This instruction
295*9880d681SAndroid Build Coastguard Worker         // stores a relocatable value into it instead.
296*9880d681SAndroid Build Coastguard Worker         {
297*9880d681SAndroid Build Coastguard Worker           // If this address is in our relocation map, apply the relocation.
298*9880d681SAndroid Build Coastguard Worker           RelocAddrMap::const_iterator AI = RMap->find(*offset_ptr);
299*9880d681SAndroid Build Coastguard Worker           if (AI != RMap->end()) {
300*9880d681SAndroid Build Coastguard Worker             const std::pair<uint8_t, int64_t> &R = AI->second;
301*9880d681SAndroid Build Coastguard Worker             State.Row.Address =
302*9880d681SAndroid Build Coastguard Worker                 debug_line_data.getAddress(offset_ptr) + R.second;
303*9880d681SAndroid Build Coastguard Worker           } else
304*9880d681SAndroid Build Coastguard Worker             State.Row.Address = debug_line_data.getAddress(offset_ptr);
305*9880d681SAndroid Build Coastguard Worker         }
306*9880d681SAndroid Build Coastguard Worker         break;
307*9880d681SAndroid Build Coastguard Worker 
308*9880d681SAndroid Build Coastguard Worker       case DW_LNE_define_file:
309*9880d681SAndroid Build Coastguard Worker         // Takes 4 arguments. The first is a null terminated string containing
310*9880d681SAndroid Build Coastguard Worker         // a source file name. The second is an unsigned LEB128 number
311*9880d681SAndroid Build Coastguard Worker         // representing the directory index of the directory in which the file
312*9880d681SAndroid Build Coastguard Worker         // was found. The third is an unsigned LEB128 number representing the
313*9880d681SAndroid Build Coastguard Worker         // time of last modification of the file. The fourth is an unsigned
314*9880d681SAndroid Build Coastguard Worker         // LEB128 number representing the length in bytes of the file. The time
315*9880d681SAndroid Build Coastguard Worker         // and length fields may contain LEB128(0) if the information is not
316*9880d681SAndroid Build Coastguard Worker         // available.
317*9880d681SAndroid Build Coastguard Worker         //
318*9880d681SAndroid Build Coastguard Worker         // The directory index represents an entry in the include_directories
319*9880d681SAndroid Build Coastguard Worker         // section of the statement program prologue. The index is LEB128(0)
320*9880d681SAndroid Build Coastguard Worker         // if the file was found in the current directory of the compilation,
321*9880d681SAndroid Build Coastguard Worker         // LEB128(1) if it was found in the first directory in the
322*9880d681SAndroid Build Coastguard Worker         // include_directories section, and so on. The directory index is
323*9880d681SAndroid Build Coastguard Worker         // ignored for file names that represent full path names.
324*9880d681SAndroid Build Coastguard Worker         //
325*9880d681SAndroid Build Coastguard Worker         // The files are numbered, starting at 1, in the order in which they
326*9880d681SAndroid Build Coastguard Worker         // appear; the names in the prologue come before names defined by
327*9880d681SAndroid Build Coastguard Worker         // the DW_LNE_define_file instruction. These numbers are used in the
328*9880d681SAndroid Build Coastguard Worker         // the file register of the state machine.
329*9880d681SAndroid Build Coastguard Worker         {
330*9880d681SAndroid Build Coastguard Worker           FileNameEntry fileEntry;
331*9880d681SAndroid Build Coastguard Worker           fileEntry.Name = debug_line_data.getCStr(offset_ptr);
332*9880d681SAndroid Build Coastguard Worker           fileEntry.DirIdx = debug_line_data.getULEB128(offset_ptr);
333*9880d681SAndroid Build Coastguard Worker           fileEntry.ModTime = debug_line_data.getULEB128(offset_ptr);
334*9880d681SAndroid Build Coastguard Worker           fileEntry.Length = debug_line_data.getULEB128(offset_ptr);
335*9880d681SAndroid Build Coastguard Worker           Prologue.FileNames.push_back(fileEntry);
336*9880d681SAndroid Build Coastguard Worker         }
337*9880d681SAndroid Build Coastguard Worker         break;
338*9880d681SAndroid Build Coastguard Worker 
339*9880d681SAndroid Build Coastguard Worker       case DW_LNE_set_discriminator:
340*9880d681SAndroid Build Coastguard Worker         State.Row.Discriminator = debug_line_data.getULEB128(offset_ptr);
341*9880d681SAndroid Build Coastguard Worker         break;
342*9880d681SAndroid Build Coastguard Worker 
343*9880d681SAndroid Build Coastguard Worker       default:
344*9880d681SAndroid Build Coastguard Worker         // Length doesn't include the zero opcode byte or the length itself, but
345*9880d681SAndroid Build Coastguard Worker         // it does include the sub_opcode, so we have to adjust for that below
346*9880d681SAndroid Build Coastguard Worker         (*offset_ptr) += arg_size;
347*9880d681SAndroid Build Coastguard Worker         break;
348*9880d681SAndroid Build Coastguard Worker       }
349*9880d681SAndroid Build Coastguard Worker     } else if (opcode < Prologue.OpcodeBase) {
350*9880d681SAndroid Build Coastguard Worker       switch (opcode) {
351*9880d681SAndroid Build Coastguard Worker       // Standard Opcodes
352*9880d681SAndroid Build Coastguard Worker       case DW_LNS_copy:
353*9880d681SAndroid Build Coastguard Worker         // Takes no arguments. Append a row to the matrix using the
354*9880d681SAndroid Build Coastguard Worker         // current values of the state-machine registers. Then set
355*9880d681SAndroid Build Coastguard Worker         // the basic_block register to false.
356*9880d681SAndroid Build Coastguard Worker         State.appendRowToMatrix(*offset_ptr);
357*9880d681SAndroid Build Coastguard Worker         break;
358*9880d681SAndroid Build Coastguard Worker 
359*9880d681SAndroid Build Coastguard Worker       case DW_LNS_advance_pc:
360*9880d681SAndroid Build Coastguard Worker         // Takes a single unsigned LEB128 operand, multiplies it by the
361*9880d681SAndroid Build Coastguard Worker         // min_inst_length field of the prologue, and adds the
362*9880d681SAndroid Build Coastguard Worker         // result to the address register of the state machine.
363*9880d681SAndroid Build Coastguard Worker         State.Row.Address +=
364*9880d681SAndroid Build Coastguard Worker             debug_line_data.getULEB128(offset_ptr) * Prologue.MinInstLength;
365*9880d681SAndroid Build Coastguard Worker         break;
366*9880d681SAndroid Build Coastguard Worker 
367*9880d681SAndroid Build Coastguard Worker       case DW_LNS_advance_line:
368*9880d681SAndroid Build Coastguard Worker         // Takes a single signed LEB128 operand and adds that value to
369*9880d681SAndroid Build Coastguard Worker         // the line register of the state machine.
370*9880d681SAndroid Build Coastguard Worker         State.Row.Line += debug_line_data.getSLEB128(offset_ptr);
371*9880d681SAndroid Build Coastguard Worker         break;
372*9880d681SAndroid Build Coastguard Worker 
373*9880d681SAndroid Build Coastguard Worker       case DW_LNS_set_file:
374*9880d681SAndroid Build Coastguard Worker         // Takes a single unsigned LEB128 operand and stores it in the file
375*9880d681SAndroid Build Coastguard Worker         // register of the state machine.
376*9880d681SAndroid Build Coastguard Worker         State.Row.File = debug_line_data.getULEB128(offset_ptr);
377*9880d681SAndroid Build Coastguard Worker         break;
378*9880d681SAndroid Build Coastguard Worker 
379*9880d681SAndroid Build Coastguard Worker       case DW_LNS_set_column:
380*9880d681SAndroid Build Coastguard Worker         // Takes a single unsigned LEB128 operand and stores it in the
381*9880d681SAndroid Build Coastguard Worker         // column register of the state machine.
382*9880d681SAndroid Build Coastguard Worker         State.Row.Column = debug_line_data.getULEB128(offset_ptr);
383*9880d681SAndroid Build Coastguard Worker         break;
384*9880d681SAndroid Build Coastguard Worker 
385*9880d681SAndroid Build Coastguard Worker       case DW_LNS_negate_stmt:
386*9880d681SAndroid Build Coastguard Worker         // Takes no arguments. Set the is_stmt register of the state
387*9880d681SAndroid Build Coastguard Worker         // machine to the logical negation of its current value.
388*9880d681SAndroid Build Coastguard Worker         State.Row.IsStmt = !State.Row.IsStmt;
389*9880d681SAndroid Build Coastguard Worker         break;
390*9880d681SAndroid Build Coastguard Worker 
391*9880d681SAndroid Build Coastguard Worker       case DW_LNS_set_basic_block:
392*9880d681SAndroid Build Coastguard Worker         // Takes no arguments. Set the basic_block register of the
393*9880d681SAndroid Build Coastguard Worker         // state machine to true
394*9880d681SAndroid Build Coastguard Worker         State.Row.BasicBlock = true;
395*9880d681SAndroid Build Coastguard Worker         break;
396*9880d681SAndroid Build Coastguard Worker 
397*9880d681SAndroid Build Coastguard Worker       case DW_LNS_const_add_pc:
398*9880d681SAndroid Build Coastguard Worker         // Takes no arguments. Add to the address register of the state
399*9880d681SAndroid Build Coastguard Worker         // machine the address increment value corresponding to special
400*9880d681SAndroid Build Coastguard Worker         // opcode 255. The motivation for DW_LNS_const_add_pc is this:
401*9880d681SAndroid Build Coastguard Worker         // when the statement program needs to advance the address by a
402*9880d681SAndroid Build Coastguard Worker         // small amount, it can use a single special opcode, which occupies
403*9880d681SAndroid Build Coastguard Worker         // a single byte. When it needs to advance the address by up to
404*9880d681SAndroid Build Coastguard Worker         // twice the range of the last special opcode, it can use
405*9880d681SAndroid Build Coastguard Worker         // DW_LNS_const_add_pc followed by a special opcode, for a total
406*9880d681SAndroid Build Coastguard Worker         // of two bytes. Only if it needs to advance the address by more
407*9880d681SAndroid Build Coastguard Worker         // than twice that range will it need to use both DW_LNS_advance_pc
408*9880d681SAndroid Build Coastguard Worker         // and a special opcode, requiring three or more bytes.
409*9880d681SAndroid Build Coastguard Worker         {
410*9880d681SAndroid Build Coastguard Worker           uint8_t adjust_opcode = 255 - Prologue.OpcodeBase;
411*9880d681SAndroid Build Coastguard Worker           uint64_t addr_offset =
412*9880d681SAndroid Build Coastguard Worker               (adjust_opcode / Prologue.LineRange) * Prologue.MinInstLength;
413*9880d681SAndroid Build Coastguard Worker           State.Row.Address += addr_offset;
414*9880d681SAndroid Build Coastguard Worker         }
415*9880d681SAndroid Build Coastguard Worker         break;
416*9880d681SAndroid Build Coastguard Worker 
417*9880d681SAndroid Build Coastguard Worker       case DW_LNS_fixed_advance_pc:
418*9880d681SAndroid Build Coastguard Worker         // Takes a single uhalf operand. Add to the address register of
419*9880d681SAndroid Build Coastguard Worker         // the state machine the value of the (unencoded) operand. This
420*9880d681SAndroid Build Coastguard Worker         // is the only extended opcode that takes an argument that is not
421*9880d681SAndroid Build Coastguard Worker         // a variable length number. The motivation for DW_LNS_fixed_advance_pc
422*9880d681SAndroid Build Coastguard Worker         // is this: existing assemblers cannot emit DW_LNS_advance_pc or
423*9880d681SAndroid Build Coastguard Worker         // special opcodes because they cannot encode LEB128 numbers or
424*9880d681SAndroid Build Coastguard Worker         // judge when the computation of a special opcode overflows and
425*9880d681SAndroid Build Coastguard Worker         // requires the use of DW_LNS_advance_pc. Such assemblers, however,
426*9880d681SAndroid Build Coastguard Worker         // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
427*9880d681SAndroid Build Coastguard Worker         State.Row.Address += debug_line_data.getU16(offset_ptr);
428*9880d681SAndroid Build Coastguard Worker         break;
429*9880d681SAndroid Build Coastguard Worker 
430*9880d681SAndroid Build Coastguard Worker       case DW_LNS_set_prologue_end:
431*9880d681SAndroid Build Coastguard Worker         // Takes no arguments. Set the prologue_end register of the
432*9880d681SAndroid Build Coastguard Worker         // state machine to true
433*9880d681SAndroid Build Coastguard Worker         State.Row.PrologueEnd = true;
434*9880d681SAndroid Build Coastguard Worker         break;
435*9880d681SAndroid Build Coastguard Worker 
436*9880d681SAndroid Build Coastguard Worker       case DW_LNS_set_epilogue_begin:
437*9880d681SAndroid Build Coastguard Worker         // Takes no arguments. Set the basic_block register of the
438*9880d681SAndroid Build Coastguard Worker         // state machine to true
439*9880d681SAndroid Build Coastguard Worker         State.Row.EpilogueBegin = true;
440*9880d681SAndroid Build Coastguard Worker         break;
441*9880d681SAndroid Build Coastguard Worker 
442*9880d681SAndroid Build Coastguard Worker       case DW_LNS_set_isa:
443*9880d681SAndroid Build Coastguard Worker         // Takes a single unsigned LEB128 operand and stores it in the
444*9880d681SAndroid Build Coastguard Worker         // column register of the state machine.
445*9880d681SAndroid Build Coastguard Worker         State.Row.Isa = debug_line_data.getULEB128(offset_ptr);
446*9880d681SAndroid Build Coastguard Worker         break;
447*9880d681SAndroid Build Coastguard Worker 
448*9880d681SAndroid Build Coastguard Worker       default:
449*9880d681SAndroid Build Coastguard Worker         // Handle any unknown standard opcodes here. We know the lengths
450*9880d681SAndroid Build Coastguard Worker         // of such opcodes because they are specified in the prologue
451*9880d681SAndroid Build Coastguard Worker         // as a multiple of LEB128 operands for each opcode.
452*9880d681SAndroid Build Coastguard Worker         {
453*9880d681SAndroid Build Coastguard Worker           assert(opcode - 1U < Prologue.StandardOpcodeLengths.size());
454*9880d681SAndroid Build Coastguard Worker           uint8_t opcode_length = Prologue.StandardOpcodeLengths[opcode - 1];
455*9880d681SAndroid Build Coastguard Worker           for (uint8_t i = 0; i < opcode_length; ++i)
456*9880d681SAndroid Build Coastguard Worker             debug_line_data.getULEB128(offset_ptr);
457*9880d681SAndroid Build Coastguard Worker         }
458*9880d681SAndroid Build Coastguard Worker         break;
459*9880d681SAndroid Build Coastguard Worker       }
460*9880d681SAndroid Build Coastguard Worker     } else {
461*9880d681SAndroid Build Coastguard Worker       // Special Opcodes
462*9880d681SAndroid Build Coastguard Worker 
463*9880d681SAndroid Build Coastguard Worker       // A special opcode value is chosen based on the amount that needs
464*9880d681SAndroid Build Coastguard Worker       // to be added to the line and address registers. The maximum line
465*9880d681SAndroid Build Coastguard Worker       // increment for a special opcode is the value of the line_base
466*9880d681SAndroid Build Coastguard Worker       // field in the header, plus the value of the line_range field,
467*9880d681SAndroid Build Coastguard Worker       // minus 1 (line base + line range - 1). If the desired line
468*9880d681SAndroid Build Coastguard Worker       // increment is greater than the maximum line increment, a standard
469*9880d681SAndroid Build Coastguard Worker       // opcode must be used instead of a special opcode. The "address
470*9880d681SAndroid Build Coastguard Worker       // advance" is calculated by dividing the desired address increment
471*9880d681SAndroid Build Coastguard Worker       // by the minimum_instruction_length field from the header. The
472*9880d681SAndroid Build Coastguard Worker       // special opcode is then calculated using the following formula:
473*9880d681SAndroid Build Coastguard Worker       //
474*9880d681SAndroid Build Coastguard Worker       //  opcode = (desired line increment - line_base) +
475*9880d681SAndroid Build Coastguard Worker       //           (line_range * address advance) + opcode_base
476*9880d681SAndroid Build Coastguard Worker       //
477*9880d681SAndroid Build Coastguard Worker       // If the resulting opcode is greater than 255, a standard opcode
478*9880d681SAndroid Build Coastguard Worker       // must be used instead.
479*9880d681SAndroid Build Coastguard Worker       //
480*9880d681SAndroid Build Coastguard Worker       // To decode a special opcode, subtract the opcode_base from the
481*9880d681SAndroid Build Coastguard Worker       // opcode itself to give the adjusted opcode. The amount to
482*9880d681SAndroid Build Coastguard Worker       // increment the address register is the result of the adjusted
483*9880d681SAndroid Build Coastguard Worker       // opcode divided by the line_range multiplied by the
484*9880d681SAndroid Build Coastguard Worker       // minimum_instruction_length field from the header. That is:
485*9880d681SAndroid Build Coastguard Worker       //
486*9880d681SAndroid Build Coastguard Worker       //  address increment = (adjusted opcode / line_range) *
487*9880d681SAndroid Build Coastguard Worker       //                      minimum_instruction_length
488*9880d681SAndroid Build Coastguard Worker       //
489*9880d681SAndroid Build Coastguard Worker       // The amount to increment the line register is the line_base plus
490*9880d681SAndroid Build Coastguard Worker       // the result of the adjusted opcode modulo the line_range. That is:
491*9880d681SAndroid Build Coastguard Worker       //
492*9880d681SAndroid Build Coastguard Worker       // line increment = line_base + (adjusted opcode % line_range)
493*9880d681SAndroid Build Coastguard Worker 
494*9880d681SAndroid Build Coastguard Worker       uint8_t adjust_opcode = opcode - Prologue.OpcodeBase;
495*9880d681SAndroid Build Coastguard Worker       uint64_t addr_offset =
496*9880d681SAndroid Build Coastguard Worker           (adjust_opcode / Prologue.LineRange) * Prologue.MinInstLength;
497*9880d681SAndroid Build Coastguard Worker       int32_t line_offset =
498*9880d681SAndroid Build Coastguard Worker           Prologue.LineBase + (adjust_opcode % Prologue.LineRange);
499*9880d681SAndroid Build Coastguard Worker       State.Row.Line += line_offset;
500*9880d681SAndroid Build Coastguard Worker       State.Row.Address += addr_offset;
501*9880d681SAndroid Build Coastguard Worker       State.appendRowToMatrix(*offset_ptr);
502*9880d681SAndroid Build Coastguard Worker       // Reset discriminator to 0.
503*9880d681SAndroid Build Coastguard Worker       State.Row.Discriminator = 0;
504*9880d681SAndroid Build Coastguard Worker     }
505*9880d681SAndroid Build Coastguard Worker   }
506*9880d681SAndroid Build Coastguard Worker 
507*9880d681SAndroid Build Coastguard Worker   if (!State.Sequence.Empty) {
508*9880d681SAndroid Build Coastguard Worker     fprintf(stderr, "warning: last sequence in debug line table is not"
509*9880d681SAndroid Build Coastguard Worker                     "terminated!\n");
510*9880d681SAndroid Build Coastguard Worker   }
511*9880d681SAndroid Build Coastguard Worker 
512*9880d681SAndroid Build Coastguard Worker   // Sort all sequences so that address lookup will work faster.
513*9880d681SAndroid Build Coastguard Worker   if (!Sequences.empty()) {
514*9880d681SAndroid Build Coastguard Worker     std::sort(Sequences.begin(), Sequences.end(), Sequence::orderByLowPC);
515*9880d681SAndroid Build Coastguard Worker     // Note: actually, instruction address ranges of sequences should not
516*9880d681SAndroid Build Coastguard Worker     // overlap (in shared objects and executables). If they do, the address
517*9880d681SAndroid Build Coastguard Worker     // lookup would still work, though, but result would be ambiguous.
518*9880d681SAndroid Build Coastguard Worker     // We don't report warning in this case. For example,
519*9880d681SAndroid Build Coastguard Worker     // sometimes .so compiled from multiple object files contains a few
520*9880d681SAndroid Build Coastguard Worker     // rudimentary sequences for address ranges [0x0, 0xsomething).
521*9880d681SAndroid Build Coastguard Worker   }
522*9880d681SAndroid Build Coastguard Worker 
523*9880d681SAndroid Build Coastguard Worker   return end_offset;
524*9880d681SAndroid Build Coastguard Worker }
525*9880d681SAndroid Build Coastguard Worker 
526*9880d681SAndroid Build Coastguard Worker uint32_t
findRowInSeq(const DWARFDebugLine::Sequence & seq,uint64_t address) const527*9880d681SAndroid Build Coastguard Worker DWARFDebugLine::LineTable::findRowInSeq(const DWARFDebugLine::Sequence &seq,
528*9880d681SAndroid Build Coastguard Worker                                         uint64_t address) const {
529*9880d681SAndroid Build Coastguard Worker   if (!seq.containsPC(address))
530*9880d681SAndroid Build Coastguard Worker     return UnknownRowIndex;
531*9880d681SAndroid Build Coastguard Worker   // Search for instruction address in the rows describing the sequence.
532*9880d681SAndroid Build Coastguard Worker   // Rows are stored in a vector, so we may use arithmetical operations with
533*9880d681SAndroid Build Coastguard Worker   // iterators.
534*9880d681SAndroid Build Coastguard Worker   DWARFDebugLine::Row row;
535*9880d681SAndroid Build Coastguard Worker   row.Address = address;
536*9880d681SAndroid Build Coastguard Worker   RowIter first_row = Rows.begin() + seq.FirstRowIndex;
537*9880d681SAndroid Build Coastguard Worker   RowIter last_row = Rows.begin() + seq.LastRowIndex;
538*9880d681SAndroid Build Coastguard Worker   LineTable::RowIter row_pos = std::lower_bound(
539*9880d681SAndroid Build Coastguard Worker       first_row, last_row, row, DWARFDebugLine::Row::orderByAddress);
540*9880d681SAndroid Build Coastguard Worker   if (row_pos == last_row) {
541*9880d681SAndroid Build Coastguard Worker     return seq.LastRowIndex - 1;
542*9880d681SAndroid Build Coastguard Worker   }
543*9880d681SAndroid Build Coastguard Worker   uint32_t index = seq.FirstRowIndex + (row_pos - first_row);
544*9880d681SAndroid Build Coastguard Worker   if (row_pos->Address > address) {
545*9880d681SAndroid Build Coastguard Worker     if (row_pos == first_row)
546*9880d681SAndroid Build Coastguard Worker       return UnknownRowIndex;
547*9880d681SAndroid Build Coastguard Worker     else
548*9880d681SAndroid Build Coastguard Worker       index--;
549*9880d681SAndroid Build Coastguard Worker   }
550*9880d681SAndroid Build Coastguard Worker   return index;
551*9880d681SAndroid Build Coastguard Worker }
552*9880d681SAndroid Build Coastguard Worker 
lookupAddress(uint64_t address) const553*9880d681SAndroid Build Coastguard Worker uint32_t DWARFDebugLine::LineTable::lookupAddress(uint64_t address) const {
554*9880d681SAndroid Build Coastguard Worker   if (Sequences.empty())
555*9880d681SAndroid Build Coastguard Worker     return UnknownRowIndex;
556*9880d681SAndroid Build Coastguard Worker   // First, find an instruction sequence containing the given address.
557*9880d681SAndroid Build Coastguard Worker   DWARFDebugLine::Sequence sequence;
558*9880d681SAndroid Build Coastguard Worker   sequence.LowPC = address;
559*9880d681SAndroid Build Coastguard Worker   SequenceIter first_seq = Sequences.begin();
560*9880d681SAndroid Build Coastguard Worker   SequenceIter last_seq = Sequences.end();
561*9880d681SAndroid Build Coastguard Worker   SequenceIter seq_pos = std::lower_bound(
562*9880d681SAndroid Build Coastguard Worker       first_seq, last_seq, sequence, DWARFDebugLine::Sequence::orderByLowPC);
563*9880d681SAndroid Build Coastguard Worker   DWARFDebugLine::Sequence found_seq;
564*9880d681SAndroid Build Coastguard Worker   if (seq_pos == last_seq) {
565*9880d681SAndroid Build Coastguard Worker     found_seq = Sequences.back();
566*9880d681SAndroid Build Coastguard Worker   } else if (seq_pos->LowPC == address) {
567*9880d681SAndroid Build Coastguard Worker     found_seq = *seq_pos;
568*9880d681SAndroid Build Coastguard Worker   } else {
569*9880d681SAndroid Build Coastguard Worker     if (seq_pos == first_seq)
570*9880d681SAndroid Build Coastguard Worker       return UnknownRowIndex;
571*9880d681SAndroid Build Coastguard Worker     found_seq = *(seq_pos - 1);
572*9880d681SAndroid Build Coastguard Worker   }
573*9880d681SAndroid Build Coastguard Worker   return findRowInSeq(found_seq, address);
574*9880d681SAndroid Build Coastguard Worker }
575*9880d681SAndroid Build Coastguard Worker 
lookupAddressRange(uint64_t address,uint64_t size,std::vector<uint32_t> & result) const576*9880d681SAndroid Build Coastguard Worker bool DWARFDebugLine::LineTable::lookupAddressRange(
577*9880d681SAndroid Build Coastguard Worker     uint64_t address, uint64_t size, std::vector<uint32_t> &result) const {
578*9880d681SAndroid Build Coastguard Worker   if (Sequences.empty())
579*9880d681SAndroid Build Coastguard Worker     return false;
580*9880d681SAndroid Build Coastguard Worker   uint64_t end_addr = address + size;
581*9880d681SAndroid Build Coastguard Worker   // First, find an instruction sequence containing the given address.
582*9880d681SAndroid Build Coastguard Worker   DWARFDebugLine::Sequence sequence;
583*9880d681SAndroid Build Coastguard Worker   sequence.LowPC = address;
584*9880d681SAndroid Build Coastguard Worker   SequenceIter first_seq = Sequences.begin();
585*9880d681SAndroid Build Coastguard Worker   SequenceIter last_seq = Sequences.end();
586*9880d681SAndroid Build Coastguard Worker   SequenceIter seq_pos = std::lower_bound(
587*9880d681SAndroid Build Coastguard Worker       first_seq, last_seq, sequence, DWARFDebugLine::Sequence::orderByLowPC);
588*9880d681SAndroid Build Coastguard Worker   if (seq_pos == last_seq || seq_pos->LowPC != address) {
589*9880d681SAndroid Build Coastguard Worker     if (seq_pos == first_seq)
590*9880d681SAndroid Build Coastguard Worker       return false;
591*9880d681SAndroid Build Coastguard Worker     seq_pos--;
592*9880d681SAndroid Build Coastguard Worker   }
593*9880d681SAndroid Build Coastguard Worker   if (!seq_pos->containsPC(address))
594*9880d681SAndroid Build Coastguard Worker     return false;
595*9880d681SAndroid Build Coastguard Worker 
596*9880d681SAndroid Build Coastguard Worker   SequenceIter start_pos = seq_pos;
597*9880d681SAndroid Build Coastguard Worker 
598*9880d681SAndroid Build Coastguard Worker   // Add the rows from the first sequence to the vector, starting with the
599*9880d681SAndroid Build Coastguard Worker   // index we just calculated
600*9880d681SAndroid Build Coastguard Worker 
601*9880d681SAndroid Build Coastguard Worker   while (seq_pos != last_seq && seq_pos->LowPC < end_addr) {
602*9880d681SAndroid Build Coastguard Worker     const DWARFDebugLine::Sequence &cur_seq = *seq_pos;
603*9880d681SAndroid Build Coastguard Worker     // For the first sequence, we need to find which row in the sequence is the
604*9880d681SAndroid Build Coastguard Worker     // first in our range.
605*9880d681SAndroid Build Coastguard Worker     uint32_t first_row_index = cur_seq.FirstRowIndex;
606*9880d681SAndroid Build Coastguard Worker     if (seq_pos == start_pos)
607*9880d681SAndroid Build Coastguard Worker       first_row_index = findRowInSeq(cur_seq, address);
608*9880d681SAndroid Build Coastguard Worker 
609*9880d681SAndroid Build Coastguard Worker     // Figure out the last row in the range.
610*9880d681SAndroid Build Coastguard Worker     uint32_t last_row_index = findRowInSeq(cur_seq, end_addr - 1);
611*9880d681SAndroid Build Coastguard Worker     if (last_row_index == UnknownRowIndex)
612*9880d681SAndroid Build Coastguard Worker       last_row_index = cur_seq.LastRowIndex - 1;
613*9880d681SAndroid Build Coastguard Worker 
614*9880d681SAndroid Build Coastguard Worker     assert(first_row_index != UnknownRowIndex);
615*9880d681SAndroid Build Coastguard Worker     assert(last_row_index != UnknownRowIndex);
616*9880d681SAndroid Build Coastguard Worker 
617*9880d681SAndroid Build Coastguard Worker     for (uint32_t i = first_row_index; i <= last_row_index; ++i) {
618*9880d681SAndroid Build Coastguard Worker       result.push_back(i);
619*9880d681SAndroid Build Coastguard Worker     }
620*9880d681SAndroid Build Coastguard Worker 
621*9880d681SAndroid Build Coastguard Worker     ++seq_pos;
622*9880d681SAndroid Build Coastguard Worker   }
623*9880d681SAndroid Build Coastguard Worker 
624*9880d681SAndroid Build Coastguard Worker   return true;
625*9880d681SAndroid Build Coastguard Worker }
626*9880d681SAndroid Build Coastguard Worker 
getFileNameByIndex(uint64_t FileIndex,const char * CompDir,FileLineInfoKind Kind,std::string & Result) const627*9880d681SAndroid Build Coastguard Worker bool DWARFDebugLine::LineTable::getFileNameByIndex(uint64_t FileIndex,
628*9880d681SAndroid Build Coastguard Worker                                                    const char *CompDir,
629*9880d681SAndroid Build Coastguard Worker                                                    FileLineInfoKind Kind,
630*9880d681SAndroid Build Coastguard Worker                                                    std::string &Result) const {
631*9880d681SAndroid Build Coastguard Worker   if (FileIndex == 0 || FileIndex > Prologue.FileNames.size() ||
632*9880d681SAndroid Build Coastguard Worker       Kind == FileLineInfoKind::None)
633*9880d681SAndroid Build Coastguard Worker     return false;
634*9880d681SAndroid Build Coastguard Worker   const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
635*9880d681SAndroid Build Coastguard Worker   const char *FileName = Entry.Name;
636*9880d681SAndroid Build Coastguard Worker   if (Kind != FileLineInfoKind::AbsoluteFilePath ||
637*9880d681SAndroid Build Coastguard Worker       sys::path::is_absolute(FileName)) {
638*9880d681SAndroid Build Coastguard Worker     Result = FileName;
639*9880d681SAndroid Build Coastguard Worker     return true;
640*9880d681SAndroid Build Coastguard Worker   }
641*9880d681SAndroid Build Coastguard Worker 
642*9880d681SAndroid Build Coastguard Worker   SmallString<16> FilePath;
643*9880d681SAndroid Build Coastguard Worker   uint64_t IncludeDirIndex = Entry.DirIdx;
644*9880d681SAndroid Build Coastguard Worker   const char *IncludeDir = "";
645*9880d681SAndroid Build Coastguard Worker   // Be defensive about the contents of Entry.
646*9880d681SAndroid Build Coastguard Worker   if (IncludeDirIndex > 0 &&
647*9880d681SAndroid Build Coastguard Worker       IncludeDirIndex <= Prologue.IncludeDirectories.size())
648*9880d681SAndroid Build Coastguard Worker     IncludeDir = Prologue.IncludeDirectories[IncludeDirIndex - 1];
649*9880d681SAndroid Build Coastguard Worker 
650*9880d681SAndroid Build Coastguard Worker   // We may still need to append compilation directory of compile unit.
651*9880d681SAndroid Build Coastguard Worker   // We know that FileName is not absolute, the only way to have an
652*9880d681SAndroid Build Coastguard Worker   // absolute path at this point would be if IncludeDir is absolute.
653*9880d681SAndroid Build Coastguard Worker   if (CompDir && Kind == FileLineInfoKind::AbsoluteFilePath &&
654*9880d681SAndroid Build Coastguard Worker       sys::path::is_relative(IncludeDir))
655*9880d681SAndroid Build Coastguard Worker     sys::path::append(FilePath, CompDir);
656*9880d681SAndroid Build Coastguard Worker 
657*9880d681SAndroid Build Coastguard Worker   // sys::path::append skips empty strings.
658*9880d681SAndroid Build Coastguard Worker   sys::path::append(FilePath, IncludeDir, FileName);
659*9880d681SAndroid Build Coastguard Worker   Result = FilePath.str();
660*9880d681SAndroid Build Coastguard Worker   return true;
661*9880d681SAndroid Build Coastguard Worker }
662*9880d681SAndroid Build Coastguard Worker 
getFileLineInfoForAddress(uint64_t Address,const char * CompDir,FileLineInfoKind Kind,DILineInfo & Result) const663*9880d681SAndroid Build Coastguard Worker bool DWARFDebugLine::LineTable::getFileLineInfoForAddress(
664*9880d681SAndroid Build Coastguard Worker     uint64_t Address, const char *CompDir, FileLineInfoKind Kind,
665*9880d681SAndroid Build Coastguard Worker     DILineInfo &Result) const {
666*9880d681SAndroid Build Coastguard Worker   // Get the index of row we're looking for in the line table.
667*9880d681SAndroid Build Coastguard Worker   uint32_t RowIndex = lookupAddress(Address);
668*9880d681SAndroid Build Coastguard Worker   if (RowIndex == -1U)
669*9880d681SAndroid Build Coastguard Worker     return false;
670*9880d681SAndroid Build Coastguard Worker   // Take file number and line/column from the row.
671*9880d681SAndroid Build Coastguard Worker   const auto &Row = Rows[RowIndex];
672*9880d681SAndroid Build Coastguard Worker   if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName))
673*9880d681SAndroid Build Coastguard Worker     return false;
674*9880d681SAndroid Build Coastguard Worker   Result.Line = Row.Line;
675*9880d681SAndroid Build Coastguard Worker   Result.Column = Row.Column;
676*9880d681SAndroid Build Coastguard Worker   return true;
677*9880d681SAndroid Build Coastguard Worker }
678