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