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