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 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 9 | #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h" |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/Optional.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" |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h" |
Victor Leschuk | cba595d | 2018-08-20 09:59:08 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Errc.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" |
Jonas Devlieghere | 84e9926 | 2018-04-14 22:07:23 +0000 | [diff] [blame] | 20 | #include "llvm/Support/WithColor.h" |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | a57c46a | 2011-09-15 02:19:33 +0000 | [diff] [blame] | 22 | #include <algorithm> |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 23 | #include <cassert> |
| 24 | #include <cinttypes> |
| 25 | #include <cstdint> |
| 26 | #include <cstdio> |
| 27 | #include <utility> |
| 28 | |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | using namespace dwarf; |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 31 | |
Eugene Zelenko | 2db0cfa | 2017-06-23 21:57:40 +0000 | [diff] [blame] | 32 | using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind; |
| 33 | |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 34 | namespace { |
Eugene Zelenko | 2db0cfa | 2017-06-23 21:57:40 +0000 | [diff] [blame] | 35 | |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 36 | struct ContentDescriptor { |
| 37 | dwarf::LineNumberEntryFormat Type; |
| 38 | dwarf::Form Form; |
| 39 | }; |
Eugene Zelenko | 2db0cfa | 2017-06-23 21:57:40 +0000 | [diff] [blame] | 40 | |
| 41 | using ContentDescriptors = SmallVector<ContentDescriptor, 4>; |
| 42 | |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 43 | } // end anonmyous namespace |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 44 | |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 45 | void DWARFDebugLine::ContentTypeTracker::trackContentType( |
| 46 | dwarf::LineNumberEntryFormat ContentType) { |
| 47 | switch (ContentType) { |
| 48 | case dwarf::DW_LNCT_timestamp: |
| 49 | HasModTime = true; |
| 50 | break; |
| 51 | case dwarf::DW_LNCT_size: |
| 52 | HasLength = true; |
| 53 | break; |
| 54 | case dwarf::DW_LNCT_MD5: |
| 55 | HasMD5 = true; |
| 56 | break; |
| 57 | case dwarf::DW_LNCT_LLVM_source: |
| 58 | HasSource = true; |
| 59 | break; |
| 60 | default: |
| 61 | // We only care about values we consider optional, and new values may be |
| 62 | // added in the vendor extension range, so we do not match exhaustively. |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | |
Dehao Chen | 1b54fce | 2016-04-28 22:09:37 +0000 | [diff] [blame] | 67 | DWARFDebugLine::Prologue::Prologue() { clear(); } |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 68 | |
Jonas Devlieghere | ca16d28 | 2019-07-16 01:21:25 +0000 | [diff] [blame] | 69 | bool DWARFDebugLine::Prologue::hasFileAtIndex(uint64_t FileIndex) const { |
| 70 | uint16_t DwarfVersion = getVersion(); |
| 71 | assert(DwarfVersion != 0 && |
| 72 | "line table prologue has no dwarf version information"); |
| 73 | if (DwarfVersion >= 5) |
| 74 | return FileIndex < FileNames.size(); |
| 75 | return FileIndex != 0 && FileIndex <= FileNames.size(); |
| 76 | } |
| 77 | |
| 78 | const llvm::DWARFDebugLine::FileNameEntry & |
| 79 | DWARFDebugLine::Prologue::getFileNameEntry(uint64_t Index) const { |
| 80 | uint16_t DwarfVersion = getVersion(); |
| 81 | assert(DwarfVersion != 0 && |
| 82 | "line table prologue has no dwarf version information"); |
| 83 | // In DWARF v5 the file names are 0-indexed. |
| 84 | if (DwarfVersion >= 5) |
| 85 | return FileNames[Index]; |
| 86 | return FileNames[Index - 1]; |
| 87 | } |
| 88 | |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 89 | void DWARFDebugLine::Prologue::clear() { |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 90 | TotalLength = PrologueLength = 0; |
| 91 | SegSelectorSize = 0; |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 92 | MinInstLength = MaxOpsPerInst = DefaultIsStmt = LineBase = LineRange = 0; |
| 93 | OpcodeBase = 0; |
Pavel Labath | 322711f | 2018-03-14 09:39:54 +0000 | [diff] [blame] | 94 | FormParams = dwarf::FormParams({0, 0, DWARF32}); |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 95 | ContentTypes = ContentTypeTracker(); |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 96 | StandardOpcodeLengths.clear(); |
| 97 | IncludeDirectories.clear(); |
| 98 | FileNames.clear(); |
| 99 | } |
| 100 | |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame] | 101 | void DWARFDebugLine::Prologue::dump(raw_ostream &OS, |
| 102 | DIDumpOptions DumpOptions) const { |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 103 | OS << "Line table prologue:\n" |
Ed Maste | 6d0bee5 | 2015-05-28 15:38:17 +0000 | [diff] [blame] | 104 | << format(" total_length: 0x%8.8" PRIx64 "\n", TotalLength) |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 105 | << format(" version: %u\n", getVersion()); |
| 106 | if (getVersion() >= 5) |
| 107 | OS << format(" address_size: %u\n", getAddressSize()) |
| 108 | << format(" seg_select_size: %u\n", SegSelectorSize); |
| 109 | OS << format(" prologue_length: 0x%8.8" PRIx64 "\n", PrologueLength) |
David Blaikie | 1d4736e | 2014-02-24 23:58:54 +0000 | [diff] [blame] | 110 | << format(" min_inst_length: %u\n", MinInstLength) |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 111 | << format(getVersion() >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst) |
David Blaikie | 1d4736e | 2014-02-24 23:58:54 +0000 | [diff] [blame] | 112 | << format(" default_is_stmt: %u\n", DefaultIsStmt) |
| 113 | << format(" line_base: %i\n", LineBase) |
| 114 | << format(" line_range: %u\n", LineRange) |
| 115 | << format(" opcode_base: %u\n", OpcodeBase); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 116 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 117 | for (uint32_t I = 0; I != StandardOpcodeLengths.size(); ++I) |
Mehdi Amini | 149f6ea | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 118 | OS << format("standard_opcode_lengths[%s] = %u\n", |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 119 | LNStandardString(I + 1).data(), StandardOpcodeLengths[I]); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 120 | |
Paul Robinson | 8181d23 | 2018-01-18 20:33:35 +0000 | [diff] [blame] | 121 | if (!IncludeDirectories.empty()) { |
| 122 | // DWARF v5 starts directory indexes at 0. |
| 123 | uint32_t DirBase = getVersion() >= 5 ? 0 : 1; |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame] | 124 | for (uint32_t I = 0; I != IncludeDirectories.size(); ++I) { |
| 125 | OS << format("include_directories[%3u] = ", I + DirBase); |
| 126 | IncludeDirectories[I].dump(OS, DumpOptions); |
| 127 | OS << '\n'; |
| 128 | } |
Paul Robinson | 8181d23 | 2018-01-18 20:33:35 +0000 | [diff] [blame] | 129 | } |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 130 | |
| 131 | if (!FileNames.empty()) { |
Paul Robinson | ceafcd4 | 2018-02-08 23:08:02 +0000 | [diff] [blame] | 132 | // DWARF v5 starts file indexes at 0. |
| 133 | uint32_t FileBase = getVersion() >= 5 ? 0 : 1; |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 134 | for (uint32_t I = 0; I != FileNames.size(); ++I) { |
| 135 | const FileNameEntry &FileEntry = FileNames[I]; |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 136 | OS << format("file_names[%3u]:\n", I + FileBase); |
| 137 | OS << " name: "; |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame] | 138 | FileEntry.Name.dump(OS, DumpOptions); |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 139 | OS << '\n' |
| 140 | << format(" dir_index: %" PRIu64 "\n", FileEntry.DirIdx); |
| 141 | if (ContentTypes.HasMD5) |
| 142 | OS << " md5_checksum: " << FileEntry.Checksum.digest() << '\n'; |
| 143 | if (ContentTypes.HasModTime) |
| 144 | OS << format(" mod_time: 0x%8.8" PRIx64 "\n", FileEntry.ModTime); |
| 145 | if (ContentTypes.HasLength) |
| 146 | OS << format(" length: 0x%8.8" PRIx64 "\n", FileEntry.Length); |
| 147 | if (ContentTypes.HasSource) { |
| 148 | OS << " source: "; |
| 149 | FileEntry.Source.dump(OS, DumpOptions); |
| 150 | OS << '\n'; |
| 151 | } |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 156 | // Parse v2-v4 directory and file tables. |
| 157 | static void |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 158 | parseV2DirFileTables(const DWARFDataExtractor &DebugLineData, |
| 159 | uint32_t *OffsetPtr, uint64_t EndPrologueOffset, |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 160 | DWARFDebugLine::ContentTypeTracker &ContentTypes, |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame] | 161 | std::vector<DWARFFormValue> &IncludeDirectories, |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 162 | std::vector<DWARFDebugLine::FileNameEntry> &FileNames) { |
| 163 | while (*OffsetPtr < EndPrologueOffset) { |
| 164 | StringRef S = DebugLineData.getCStrRef(OffsetPtr); |
| 165 | if (S.empty()) |
| 166 | break; |
Jonas Devlieghere | bb11115 | 2019-02-27 00:58:09 +0000 | [diff] [blame] | 167 | DWARFFormValue Dir = |
| 168 | DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, S.data()); |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame] | 169 | IncludeDirectories.push_back(Dir); |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | while (*OffsetPtr < EndPrologueOffset) { |
| 173 | StringRef Name = DebugLineData.getCStrRef(OffsetPtr); |
| 174 | if (Name.empty()) |
| 175 | break; |
| 176 | DWARFDebugLine::FileNameEntry FileEntry; |
Jonas Devlieghere | bb11115 | 2019-02-27 00:58:09 +0000 | [diff] [blame] | 177 | FileEntry.Name = |
| 178 | DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, Name.data()); |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 179 | FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr); |
| 180 | FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr); |
| 181 | FileEntry.Length = DebugLineData.getULEB128(OffsetPtr); |
| 182 | FileNames.push_back(FileEntry); |
| 183 | } |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 184 | |
| 185 | ContentTypes.HasModTime = true; |
| 186 | ContentTypes.HasLength = true; |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // Parse v5 directory/file entry content descriptions. |
| 190 | // Returns the descriptors, or an empty vector if we did not find a path or |
| 191 | // ran off the end of the prologue. |
| 192 | static ContentDescriptors |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 193 | parseV5EntryFormat(const DWARFDataExtractor &DebugLineData, uint32_t |
| 194 | *OffsetPtr, uint64_t EndPrologueOffset, DWARFDebugLine::ContentTypeTracker |
| 195 | *ContentTypes) { |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 196 | ContentDescriptors Descriptors; |
| 197 | int FormatCount = DebugLineData.getU8(OffsetPtr); |
| 198 | bool HasPath = false; |
| 199 | for (int I = 0; I != FormatCount; ++I) { |
| 200 | if (*OffsetPtr >= EndPrologueOffset) |
| 201 | return ContentDescriptors(); |
| 202 | ContentDescriptor Descriptor; |
| 203 | Descriptor.Type = |
| 204 | dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr)); |
| 205 | Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr)); |
| 206 | if (Descriptor.Type == dwarf::DW_LNCT_path) |
| 207 | HasPath = true; |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 208 | if (ContentTypes) |
| 209 | ContentTypes->trackContentType(Descriptor.Type); |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 210 | Descriptors.push_back(Descriptor); |
| 211 | } |
| 212 | return HasPath ? Descriptors : ContentDescriptors(); |
| 213 | } |
| 214 | |
| 215 | static bool |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 216 | parseV5DirFileTables(const DWARFDataExtractor &DebugLineData, |
| 217 | uint32_t *OffsetPtr, uint64_t EndPrologueOffset, |
Pavel Labath | 322711f | 2018-03-14 09:39:54 +0000 | [diff] [blame] | 218 | const dwarf::FormParams &FormParams, |
| 219 | const DWARFContext &Ctx, const DWARFUnit *U, |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 220 | DWARFDebugLine::ContentTypeTracker &ContentTypes, |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame] | 221 | std::vector<DWARFFormValue> &IncludeDirectories, |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 222 | std::vector<DWARFDebugLine::FileNameEntry> &FileNames) { |
| 223 | // Get the directory entry description. |
| 224 | ContentDescriptors DirDescriptors = |
Paul Robinson | a06f8dc | 2017-12-18 19:08:35 +0000 | [diff] [blame] | 225 | parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset, nullptr); |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 226 | if (DirDescriptors.empty()) |
| 227 | return false; |
| 228 | |
| 229 | // Get the directory entries, according to the format described above. |
| 230 | int DirEntryCount = DebugLineData.getU8(OffsetPtr); |
| 231 | for (int I = 0; I != DirEntryCount; ++I) { |
| 232 | if (*OffsetPtr >= EndPrologueOffset) |
| 233 | return false; |
| 234 | for (auto Descriptor : DirDescriptors) { |
Vlad Tsyrklevich | 53a9f1d | 2019-03-02 01:10:00 +0000 | [diff] [blame] | 235 | DWARFFormValue Value(Descriptor.Form); |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 236 | switch (Descriptor.Type) { |
| 237 | case DW_LNCT_path: |
Vlad Tsyrklevich | 53a9f1d | 2019-03-02 01:10:00 +0000 | [diff] [blame] | 238 | if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U)) |
| 239 | return false; |
| 240 | IncludeDirectories.push_back(Value); |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 241 | break; |
| 242 | default: |
Vlad Tsyrklevich | 53a9f1d | 2019-03-02 01:10:00 +0000 | [diff] [blame] | 243 | if (!Value.skipValue(DebugLineData, OffsetPtr, FormParams)) |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 244 | return false; |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | // Get the file entry description. |
| 250 | ContentDescriptors FileDescriptors = |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 251 | parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset, |
| 252 | &ContentTypes); |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 253 | if (FileDescriptors.empty()) |
| 254 | return false; |
| 255 | |
| 256 | // Get the file entries, according to the format described above. |
| 257 | int FileEntryCount = DebugLineData.getU8(OffsetPtr); |
| 258 | for (int I = 0; I != FileEntryCount; ++I) { |
| 259 | if (*OffsetPtr >= EndPrologueOffset) |
| 260 | return false; |
| 261 | DWARFDebugLine::FileNameEntry FileEntry; |
| 262 | for (auto Descriptor : FileDescriptors) { |
Vlad Tsyrklevich | 53a9f1d | 2019-03-02 01:10:00 +0000 | [diff] [blame] | 263 | DWARFFormValue Value(Descriptor.Form); |
| 264 | if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U)) |
| 265 | return false; |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 266 | switch (Descriptor.Type) { |
| 267 | case DW_LNCT_path: |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame] | 268 | FileEntry.Name = Value; |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 269 | break; |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 270 | case DW_LNCT_LLVM_source: |
| 271 | FileEntry.Source = Value; |
| 272 | break; |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 273 | case DW_LNCT_directory_index: |
| 274 | FileEntry.DirIdx = Value.getAsUnsignedConstant().getValue(); |
| 275 | break; |
| 276 | case DW_LNCT_timestamp: |
| 277 | FileEntry.ModTime = Value.getAsUnsignedConstant().getValue(); |
| 278 | break; |
| 279 | case DW_LNCT_size: |
| 280 | FileEntry.Length = Value.getAsUnsignedConstant().getValue(); |
| 281 | break; |
Paul Robinson | a06f8dc | 2017-12-18 19:08:35 +0000 | [diff] [blame] | 282 | case DW_LNCT_MD5: |
| 283 | assert(Value.getAsBlock().getValue().size() == 16); |
| 284 | std::uninitialized_copy_n(Value.getAsBlock().getValue().begin(), 16, |
| 285 | FileEntry.Checksum.Bytes.begin()); |
| 286 | break; |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 287 | default: |
| 288 | break; |
| 289 | } |
| 290 | } |
| 291 | FileNames.push_back(FileEntry); |
| 292 | } |
| 293 | return true; |
| 294 | } |
| 295 | |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 296 | Error DWARFDebugLine::Prologue::parse(const DWARFDataExtractor &DebugLineData, |
| 297 | uint32_t *OffsetPtr, |
| 298 | const DWARFContext &Ctx, |
| 299 | const DWARFUnit *U) { |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 300 | const uint64_t PrologueOffset = *OffsetPtr; |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 301 | |
| 302 | clear(); |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 303 | TotalLength = DebugLineData.getU32(OffsetPtr); |
Ed Maste | 6d0bee5 | 2015-05-28 15:38:17 +0000 | [diff] [blame] | 304 | if (TotalLength == UINT32_MAX) { |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 305 | FormParams.Format = dwarf::DWARF64; |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 306 | TotalLength = DebugLineData.getU64(OffsetPtr); |
Igor Kudrin | f48bc01 | 2019-07-16 07:01:08 +0000 | [diff] [blame] | 307 | } else if (TotalLength >= 0xfffffff0) { |
Victor Leschuk | cba595d | 2018-08-20 09:59:08 +0000 | [diff] [blame] | 308 | return createStringError(errc::invalid_argument, |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 309 | "parsing line table prologue at offset 0x%8.8" PRIx64 |
| 310 | " unsupported reserved unit length found of value 0x%8.8" PRIx64, |
| 311 | PrologueOffset, TotalLength); |
Ed Maste | 6d0bee5 | 2015-05-28 15:38:17 +0000 | [diff] [blame] | 312 | } |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 313 | FormParams.Version = DebugLineData.getU16(OffsetPtr); |
| 314 | if (getVersion() < 2) |
Victor Leschuk | cba595d | 2018-08-20 09:59:08 +0000 | [diff] [blame] | 315 | return createStringError(errc::not_supported, |
| 316 | "parsing line table prologue at offset 0x%8.8" PRIx64 |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 317 | " found unsupported version 0x%2.2" PRIx16, |
| 318 | PrologueOffset, getVersion()); |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 319 | |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 320 | if (getVersion() >= 5) { |
| 321 | FormParams.AddrSize = DebugLineData.getU8(OffsetPtr); |
Paul Robinson | 63811a4 | 2017-11-22 15:33:17 +0000 | [diff] [blame] | 322 | assert((DebugLineData.getAddressSize() == 0 || |
| 323 | DebugLineData.getAddressSize() == getAddressSize()) && |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 324 | "Line table header and data extractor disagree"); |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 325 | SegSelectorSize = DebugLineData.getU8(OffsetPtr); |
| 326 | } |
| 327 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 328 | PrologueLength = DebugLineData.getUnsigned(OffsetPtr, sizeofPrologueLength()); |
| 329 | const uint64_t EndPrologueOffset = PrologueLength + *OffsetPtr; |
| 330 | MinInstLength = DebugLineData.getU8(OffsetPtr); |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 331 | if (getVersion() >= 4) |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 332 | MaxOpsPerInst = DebugLineData.getU8(OffsetPtr); |
| 333 | DefaultIsStmt = DebugLineData.getU8(OffsetPtr); |
| 334 | LineBase = DebugLineData.getU8(OffsetPtr); |
| 335 | LineRange = DebugLineData.getU8(OffsetPtr); |
| 336 | OpcodeBase = DebugLineData.getU8(OffsetPtr); |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 337 | |
| 338 | StandardOpcodeLengths.reserve(OpcodeBase - 1); |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 339 | for (uint32_t I = 1; I < OpcodeBase; ++I) { |
| 340 | uint8_t OpLen = DebugLineData.getU8(OffsetPtr); |
| 341 | StandardOpcodeLengths.push_back(OpLen); |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 344 | if (getVersion() >= 5) { |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 345 | if (!parseV5DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset, |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 346 | FormParams, Ctx, U, ContentTypes, |
| 347 | IncludeDirectories, FileNames)) { |
Victor Leschuk | cba595d | 2018-08-20 09:59:08 +0000 | [diff] [blame] | 348 | return createStringError(errc::invalid_argument, |
Jonas Devlieghere | 84e9926 | 2018-04-14 22:07:23 +0000 | [diff] [blame] | 349 | "parsing line table prologue at 0x%8.8" PRIx64 |
| 350 | " found an invalid directory or file table description at" |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 351 | " 0x%8.8" PRIx64, |
Jonas Devlieghere | 84e9926 | 2018-04-14 22:07:23 +0000 | [diff] [blame] | 352 | PrologueOffset, (uint64_t)*OffsetPtr); |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 353 | } |
| 354 | } else |
| 355 | parseV2DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset, |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 356 | ContentTypes, IncludeDirectories, FileNames); |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 357 | |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 358 | if (*OffsetPtr != EndPrologueOffset) |
Victor Leschuk | cba595d | 2018-08-20 09:59:08 +0000 | [diff] [blame] | 359 | return createStringError(errc::invalid_argument, |
| 360 | "parsing line table prologue at 0x%8.8" PRIx64 |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 361 | " should have ended at 0x%8.8" PRIx64 |
| 362 | " but it ended at 0x%8.8" PRIx64, |
| 363 | PrologueOffset, EndPrologueOffset, (uint64_t)*OffsetPtr); |
| 364 | return Error::success(); |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 367 | DWARFDebugLine::Row::Row(bool DefaultIsStmt) { reset(DefaultIsStmt); } |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 368 | |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 369 | void DWARFDebugLine::Row::postAppend() { |
Fangrui Song | 6a285df | 2019-04-11 02:02:44 +0000 | [diff] [blame] | 370 | Discriminator = 0; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 371 | BasicBlock = false; |
| 372 | PrologueEnd = false; |
| 373 | EpilogueBegin = false; |
| 374 | } |
| 375 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 376 | void DWARFDebugLine::Row::reset(bool DefaultIsStmt) { |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 377 | Address.Address = 0; |
| 378 | Address.SectionIndex = object::SectionedAddress::UndefSection; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 379 | Line = 1; |
| 380 | Column = 0; |
| 381 | File = 1; |
| 382 | Isa = 0; |
Diego Novillo | 5b5cf50 | 2014-02-14 19:27:53 +0000 | [diff] [blame] | 383 | Discriminator = 0; |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 384 | IsStmt = DefaultIsStmt; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 385 | BasicBlock = false; |
| 386 | EndSequence = false; |
| 387 | PrologueEnd = false; |
| 388 | EpilogueBegin = false; |
| 389 | } |
| 390 | |
Greg Clayton | 6707046 | 2017-05-02 22:48:52 +0000 | [diff] [blame] | 391 | void DWARFDebugLine::Row::dumpTableHeader(raw_ostream &OS) { |
| 392 | OS << "Address Line Column File ISA Discriminator Flags\n" |
| 393 | << "------------------ ------ ------ ------ --- ------------- " |
| 394 | "-------------\n"; |
| 395 | } |
| 396 | |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 397 | void DWARFDebugLine::Row::dump(raw_ostream &OS) const { |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 398 | OS << format("0x%16.16" PRIx64 " %6u %6u", Address.Address, Line, Column) |
Diego Novillo | 5b5cf50 | 2014-02-14 19:27:53 +0000 | [diff] [blame] | 399 | << format(" %6u %3u %13u ", File, Isa, Discriminator) |
Dehao Chen | 1b54fce | 2016-04-28 22:09:37 +0000 | [diff] [blame] | 400 | << (IsStmt ? " is_stmt" : "") << (BasicBlock ? " basic_block" : "") |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 401 | << (PrologueEnd ? " prologue_end" : "") |
| 402 | << (EpilogueBegin ? " epilogue_begin" : "") |
Dehao Chen | 1b54fce | 2016-04-28 22:09:37 +0000 | [diff] [blame] | 403 | << (EndSequence ? " end_sequence" : "") << '\n'; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Dehao Chen | 1b54fce | 2016-04-28 22:09:37 +0000 | [diff] [blame] | 406 | DWARFDebugLine::Sequence::Sequence() { reset(); } |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 407 | |
| 408 | void DWARFDebugLine::Sequence::reset() { |
| 409 | LowPC = 0; |
| 410 | HighPC = 0; |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 411 | SectionIndex = object::SectionedAddress::UndefSection; |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 412 | FirstRowIndex = 0; |
| 413 | LastRowIndex = 0; |
| 414 | Empty = true; |
| 415 | } |
| 416 | |
Dehao Chen | 1b54fce | 2016-04-28 22:09:37 +0000 | [diff] [blame] | 417 | DWARFDebugLine::LineTable::LineTable() { clear(); } |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 418 | |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame] | 419 | void DWARFDebugLine::LineTable::dump(raw_ostream &OS, |
| 420 | DIDumpOptions DumpOptions) const { |
| 421 | Prologue.dump(OS, DumpOptions); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 422 | OS << '\n'; |
| 423 | |
| 424 | if (!Rows.empty()) { |
Greg Clayton | 6707046 | 2017-05-02 22:48:52 +0000 | [diff] [blame] | 425 | Row::dumpTableHeader(OS); |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 426 | for (const Row &R : Rows) { |
| 427 | R.dump(OS); |
| 428 | } |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 432 | void DWARFDebugLine::LineTable::clear() { |
| 433 | Prologue.clear(); |
| 434 | Rows.clear(); |
| 435 | Sequences.clear(); |
| 436 | } |
| 437 | |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 438 | DWARFDebugLine::ParsingState::ParsingState(struct LineTable *LT) |
Eugene Zelenko | 2db0cfa | 2017-06-23 21:57:40 +0000 | [diff] [blame] | 439 | : LineTable(LT) { |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 440 | resetRowAndSequence(); |
| 441 | } |
Nick Lewycky | 4d04492 | 2011-09-15 03:41:51 +0000 | [diff] [blame] | 442 | |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 443 | void DWARFDebugLine::ParsingState::resetRowAndSequence() { |
| 444 | Row.reset(LineTable->Prologue.DefaultIsStmt); |
| 445 | Sequence.reset(); |
| 446 | } |
| 447 | |
Fangrui Song | c4c8bca | 2019-04-07 13:56:14 +0000 | [diff] [blame] | 448 | void DWARFDebugLine::ParsingState::appendRowToMatrix() { |
Fangrui Song | 50a0967 | 2019-04-15 07:40:30 +0000 | [diff] [blame] | 449 | unsigned RowNumber = LineTable->Rows.size(); |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 450 | if (Sequence.Empty) { |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 451 | // Record the beginning of instruction sequence. |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 452 | Sequence.Empty = false; |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 453 | Sequence.LowPC = Row.Address.Address; |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 454 | Sequence.FirstRowIndex = RowNumber; |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 455 | } |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 456 | LineTable->appendRow(Row); |
| 457 | if (Row.EndSequence) { |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 458 | // Record the end of instruction sequence. |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 459 | Sequence.HighPC = Row.Address.Address; |
Fangrui Song | 50a0967 | 2019-04-15 07:40:30 +0000 | [diff] [blame] | 460 | Sequence.LastRowIndex = RowNumber + 1; |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 461 | Sequence.SectionIndex = Row.Address.SectionIndex; |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 462 | if (Sequence.isValid()) |
| 463 | LineTable->appendSequence(Sequence); |
| 464 | Sequence.reset(); |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 465 | } |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 466 | Row.postAppend(); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 469 | const DWARFDebugLine::LineTable * |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 470 | DWARFDebugLine::getLineTable(uint32_t Offset) const { |
| 471 | LineTableConstIter Pos = LineTableMap.find(Offset); |
| 472 | if (Pos != LineTableMap.end()) |
| 473 | return &Pos->second; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 474 | return nullptr; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 475 | } |
| 476 | |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 477 | Expected<const DWARFDebugLine::LineTable *> DWARFDebugLine::getOrParseLineTable( |
| 478 | DWARFDataExtractor &DebugLineData, uint32_t Offset, const DWARFContext &Ctx, |
James Henderson | 004b729 | 2018-05-21 15:30:54 +0000 | [diff] [blame] | 479 | const DWARFUnit *U, std::function<void(Error)> RecoverableErrorCallback) { |
James Henderson | 6670262 | 2018-03-08 10:53:34 +0000 | [diff] [blame] | 480 | if (!DebugLineData.isValidOffset(Offset)) |
Victor Leschuk | cba595d | 2018-08-20 09:59:08 +0000 | [diff] [blame] | 481 | return createStringError(errc::invalid_argument, "offset 0x%8.8" PRIx32 |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 482 | " is not a valid debug line section offset", |
| 483 | Offset); |
James Henderson | 6670262 | 2018-03-08 10:53:34 +0000 | [diff] [blame] | 484 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 485 | std::pair<LineTableIter, bool> Pos = |
| 486 | LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable())); |
| 487 | LineTable *LT = &Pos.first->second; |
| 488 | if (Pos.second) { |
James Henderson | 004b729 | 2018-05-21 15:30:54 +0000 | [diff] [blame] | 489 | if (Error Err = |
| 490 | LT->parse(DebugLineData, &Offset, Ctx, U, RecoverableErrorCallback)) |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 491 | return std::move(Err); |
| 492 | return LT; |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 493 | } |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 494 | return LT; |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 495 | } |
| 496 | |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 497 | Error DWARFDebugLine::LineTable::parse( |
| 498 | DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr, |
| 499 | const DWARFContext &Ctx, const DWARFUnit *U, |
James Henderson | 004b729 | 2018-05-21 15:30:54 +0000 | [diff] [blame] | 500 | std::function<void(Error)> RecoverableErrorCallback, raw_ostream *OS) { |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 501 | const uint32_t DebugLineOffset = *OffsetPtr; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 502 | |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 503 | clear(); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 504 | |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 505 | Error PrologueErr = Prologue.parse(DebugLineData, OffsetPtr, Ctx, U); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 506 | |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame] | 507 | if (OS) { |
| 508 | // The presence of OS signals verbose dumping. |
| 509 | DIDumpOptions DumpOptions; |
| 510 | DumpOptions.Verbose = true; |
| 511 | Prologue.dump(*OS, DumpOptions); |
| 512 | } |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 513 | |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 514 | if (PrologueErr) |
| 515 | return PrologueErr; |
| 516 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 517 | const uint32_t EndOffset = |
| 518 | DebugLineOffset + Prologue.TotalLength + Prologue.sizeofTotalLength(); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 519 | |
Paul Robinson | 511b54c | 2017-11-22 15:48:30 +0000 | [diff] [blame] | 520 | // See if we should tell the data extractor the address size. |
| 521 | if (DebugLineData.getAddressSize() == 0) |
| 522 | DebugLineData.setAddressSize(Prologue.getAddressSize()); |
| 523 | else |
| 524 | assert(Prologue.getAddressSize() == 0 || |
| 525 | Prologue.getAddressSize() == DebugLineData.getAddressSize()); |
| 526 | |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 527 | ParsingState State(this); |
Benjamin Kramer | 112ec17 | 2011-09-15 21:59:13 +0000 | [diff] [blame] | 528 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 529 | while (*OffsetPtr < EndOffset) { |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 530 | if (OS) |
| 531 | *OS << format("0x%08.08" PRIx32 ": ", *OffsetPtr); |
| 532 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 533 | uint8_t Opcode = DebugLineData.getU8(OffsetPtr); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 534 | |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 535 | if (OS) |
| 536 | *OS << format("%02.02" PRIx8 " ", Opcode); |
| 537 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 538 | if (Opcode == 0) { |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 539 | // Extended Opcodes always start with a zero opcode followed by |
| 540 | // 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] | 541 | uint64_t Len = DebugLineData.getULEB128(OffsetPtr); |
Paul Robinson | e083334 | 2017-11-22 15:14:49 +0000 | [diff] [blame] | 542 | uint32_t ExtOffset = *OffsetPtr; |
| 543 | |
| 544 | // Tolerate zero-length; assume length is correct and soldier on. |
| 545 | if (Len == 0) { |
| 546 | if (OS) |
| 547 | *OS << "Badly formed extended line op (length 0)\n"; |
| 548 | continue; |
| 549 | } |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 550 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 551 | uint8_t SubOpcode = DebugLineData.getU8(OffsetPtr); |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 552 | if (OS) |
| 553 | *OS << LNExtendedString(SubOpcode); |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 554 | switch (SubOpcode) { |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 555 | case DW_LNE_end_sequence: |
| 556 | // Set the end_sequence register of the state machine to true and |
| 557 | // append a row to the matrix using the current values of the |
| 558 | // state-machine registers. Then reset the registers to the initial |
| 559 | // values specified above. Every statement program sequence must end |
| 560 | // with a DW_LNE_end_sequence instruction which creates a row whose |
| 561 | // address is that of the byte after the last target machine instruction |
| 562 | // of the sequence. |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 563 | State.Row.EndSequence = true; |
Fangrui Song | c4c8bca | 2019-04-07 13:56:14 +0000 | [diff] [blame] | 564 | State.appendRowToMatrix(); |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 565 | if (OS) { |
| 566 | *OS << "\n"; |
| 567 | OS->indent(12); |
| 568 | State.Row.dump(*OS); |
| 569 | } |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 570 | State.resetRowAndSequence(); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 571 | break; |
| 572 | |
| 573 | case DW_LNE_set_address: |
| 574 | // Takes a single relocatable address as an operand. The size of the |
| 575 | // operand is the size appropriate to hold an address on the target |
| 576 | // machine. Set the address register to the value given by the |
| 577 | // relocatable address. All of the other statement program opcodes |
| 578 | // that affect the address register add a delta to it. This instruction |
| 579 | // stores a relocatable value into it instead. |
Paul Robinson | 511b54c | 2017-11-22 15:48:30 +0000 | [diff] [blame] | 580 | // |
| 581 | // Make sure the extractor knows the address size. If not, infer it |
| 582 | // from the size of the operand. |
| 583 | if (DebugLineData.getAddressSize() == 0) |
| 584 | DebugLineData.setAddressSize(Len - 1); |
Paul Robinson | 7947468 | 2018-03-22 19:37:56 +0000 | [diff] [blame] | 585 | else if (DebugLineData.getAddressSize() != Len - 1) { |
Victor Leschuk | cba595d | 2018-08-20 09:59:08 +0000 | [diff] [blame] | 586 | return createStringError(errc::invalid_argument, |
| 587 | "mismatching address size at offset 0x%8.8" PRIx32 |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 588 | " expected 0x%2.2" PRIx8 " found 0x%2.2" PRIx64, |
| 589 | ExtOffset, DebugLineData.getAddressSize(), |
| 590 | Len - 1); |
Paul Robinson | 7947468 | 2018-03-22 19:37:56 +0000 | [diff] [blame] | 591 | } |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 592 | State.Row.Address.Address = DebugLineData.getRelocatedAddress( |
| 593 | OffsetPtr, &State.Row.Address.SectionIndex); |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 594 | if (OS) |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 595 | *OS << format(" (0x%16.16" PRIx64 ")", State.Row.Address.Address); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 596 | break; |
| 597 | |
| 598 | case DW_LNE_define_file: |
| 599 | // Takes 4 arguments. The first is a null terminated string containing |
| 600 | // a source file name. The second is an unsigned LEB128 number |
| 601 | // representing the directory index of the directory in which the file |
| 602 | // was found. The third is an unsigned LEB128 number representing the |
| 603 | // time of last modification of the file. The fourth is an unsigned |
| 604 | // LEB128 number representing the length in bytes of the file. The time |
| 605 | // and length fields may contain LEB128(0) if the information is not |
| 606 | // available. |
| 607 | // |
| 608 | // The directory index represents an entry in the include_directories |
| 609 | // section of the statement program prologue. The index is LEB128(0) |
| 610 | // if the file was found in the current directory of the compilation, |
| 611 | // LEB128(1) if it was found in the first directory in the |
| 612 | // include_directories section, and so on. The directory index is |
| 613 | // ignored for file names that represent full path names. |
| 614 | // |
| 615 | // The files are numbered, starting at 1, in the order in which they |
| 616 | // appear; the names in the prologue come before names defined by |
| 617 | // the DW_LNE_define_file instruction. These numbers are used in the |
| 618 | // the file register of the state machine. |
| 619 | { |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 620 | FileNameEntry FileEntry; |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame] | 621 | const char *Name = DebugLineData.getCStr(OffsetPtr); |
Jonas Devlieghere | bb11115 | 2019-02-27 00:58:09 +0000 | [diff] [blame] | 622 | FileEntry.Name = |
| 623 | DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, Name); |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 624 | FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr); |
| 625 | FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr); |
| 626 | FileEntry.Length = DebugLineData.getULEB128(OffsetPtr); |
| 627 | Prologue.FileNames.push_back(FileEntry); |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 628 | if (OS) |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame] | 629 | *OS << " (" << Name << ", dir=" << FileEntry.DirIdx << ", mod_time=" |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 630 | << format("(0x%16.16" PRIx64 ")", FileEntry.ModTime) |
| 631 | << ", length=" << FileEntry.Length << ")"; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 632 | } |
| 633 | break; |
| 634 | |
Diego Novillo | 5b5cf50 | 2014-02-14 19:27:53 +0000 | [diff] [blame] | 635 | case DW_LNE_set_discriminator: |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 636 | State.Row.Discriminator = DebugLineData.getULEB128(OffsetPtr); |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 637 | if (OS) |
| 638 | *OS << " (" << State.Row.Discriminator << ")"; |
Diego Novillo | 5b5cf50 | 2014-02-14 19:27:53 +0000 | [diff] [blame] | 639 | break; |
| 640 | |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 641 | default: |
Paul Robinson | e083334 | 2017-11-22 15:14:49 +0000 | [diff] [blame] | 642 | if (OS) |
| 643 | *OS << format("Unrecognized extended op 0x%02.02" PRIx8, SubOpcode) |
| 644 | << format(" length %" PRIx64, Len); |
| 645 | // Len doesn't include the zero opcode byte or the length itself, but |
| 646 | // it does include the sub_opcode, so we have to adjust for that. |
| 647 | (*OffsetPtr) += Len - 1; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 648 | break; |
| 649 | } |
Paul Robinson | e083334 | 2017-11-22 15:14:49 +0000 | [diff] [blame] | 650 | // Make sure the stated and parsed lengths are the same. |
| 651 | // Otherwise we have an unparseable line-number program. |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 652 | if (*OffsetPtr - ExtOffset != Len) |
Victor Leschuk | cba595d | 2018-08-20 09:59:08 +0000 | [diff] [blame] | 653 | return createStringError(errc::illegal_byte_sequence, |
| 654 | "unexpected line op length at offset 0x%8.8" PRIx32 |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 655 | " expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx32, |
| 656 | ExtOffset, Len, *OffsetPtr - ExtOffset); |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 657 | } else if (Opcode < Prologue.OpcodeBase) { |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 658 | if (OS) |
| 659 | *OS << LNStandardString(Opcode); |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 660 | switch (Opcode) { |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 661 | // Standard Opcodes |
| 662 | case DW_LNS_copy: |
| 663 | // Takes no arguments. Append a row to the matrix using the |
Fangrui Song | 6a285df | 2019-04-11 02:02:44 +0000 | [diff] [blame] | 664 | // current values of the state-machine registers. |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 665 | if (OS) { |
| 666 | *OS << "\n"; |
| 667 | OS->indent(12); |
| 668 | State.Row.dump(*OS); |
| 669 | *OS << "\n"; |
| 670 | } |
Fangrui Song | 6a285df | 2019-04-11 02:02:44 +0000 | [diff] [blame] | 671 | State.appendRowToMatrix(); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 672 | break; |
| 673 | |
| 674 | case DW_LNS_advance_pc: |
| 675 | // Takes a single unsigned LEB128 operand, multiplies it by the |
| 676 | // min_inst_length field of the prologue, and adds the |
| 677 | // result to the address register of the state machine. |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 678 | { |
| 679 | uint64_t AddrOffset = |
| 680 | DebugLineData.getULEB128(OffsetPtr) * Prologue.MinInstLength; |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 681 | State.Row.Address.Address += AddrOffset; |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 682 | if (OS) |
| 683 | *OS << " (" << AddrOffset << ")"; |
| 684 | } |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 685 | break; |
| 686 | |
| 687 | case DW_LNS_advance_line: |
| 688 | // Takes a single signed LEB128 operand and adds that value to |
| 689 | // the line register of the state machine. |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 690 | State.Row.Line += DebugLineData.getSLEB128(OffsetPtr); |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 691 | if (OS) |
| 692 | *OS << " (" << State.Row.Line << ")"; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 693 | break; |
| 694 | |
| 695 | case DW_LNS_set_file: |
| 696 | // Takes a single unsigned LEB128 operand and stores it in the file |
| 697 | // register of the state machine. |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 698 | State.Row.File = DebugLineData.getULEB128(OffsetPtr); |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 699 | if (OS) |
| 700 | *OS << " (" << State.Row.File << ")"; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 701 | break; |
| 702 | |
| 703 | case DW_LNS_set_column: |
| 704 | // Takes a single unsigned LEB128 operand and stores it in the |
| 705 | // column register of the state machine. |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 706 | State.Row.Column = DebugLineData.getULEB128(OffsetPtr); |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 707 | if (OS) |
| 708 | *OS << " (" << State.Row.Column << ")"; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 709 | break; |
| 710 | |
| 711 | case DW_LNS_negate_stmt: |
| 712 | // Takes no arguments. Set the is_stmt register of the state |
| 713 | // machine to the logical negation of its current value. |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 714 | State.Row.IsStmt = !State.Row.IsStmt; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 715 | break; |
| 716 | |
| 717 | case DW_LNS_set_basic_block: |
| 718 | // Takes no arguments. Set the basic_block register of the |
| 719 | // state machine to true |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 720 | State.Row.BasicBlock = true; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 721 | break; |
| 722 | |
| 723 | case DW_LNS_const_add_pc: |
| 724 | // Takes no arguments. Add to the address register of the state |
| 725 | // machine the address increment value corresponding to special |
| 726 | // opcode 255. The motivation for DW_LNS_const_add_pc is this: |
| 727 | // when the statement program needs to advance the address by a |
| 728 | // small amount, it can use a single special opcode, which occupies |
| 729 | // a single byte. When it needs to advance the address by up to |
| 730 | // twice the range of the last special opcode, it can use |
| 731 | // DW_LNS_const_add_pc followed by a special opcode, for a total |
| 732 | // of two bytes. Only if it needs to advance the address by more |
| 733 | // than twice that range will it need to use both DW_LNS_advance_pc |
| 734 | // and a special opcode, requiring three or more bytes. |
| 735 | { |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 736 | uint8_t AdjustOpcode = 255 - Prologue.OpcodeBase; |
| 737 | uint64_t AddrOffset = |
| 738 | (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength; |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 739 | State.Row.Address.Address += AddrOffset; |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 740 | if (OS) |
| 741 | *OS |
| 742 | << format(" (0x%16.16" PRIx64 ")", AddrOffset); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 743 | } |
| 744 | break; |
| 745 | |
| 746 | case DW_LNS_fixed_advance_pc: |
| 747 | // Takes a single uhalf operand. Add to the address register of |
| 748 | // the state machine the value of the (unencoded) operand. This |
| 749 | // is the only extended opcode that takes an argument that is not |
| 750 | // a variable length number. The motivation for DW_LNS_fixed_advance_pc |
| 751 | // is this: existing assemblers cannot emit DW_LNS_advance_pc or |
| 752 | // special opcodes because they cannot encode LEB128 numbers or |
| 753 | // judge when the computation of a special opcode overflows and |
| 754 | // requires the use of DW_LNS_advance_pc. Such assemblers, however, |
| 755 | // can use DW_LNS_fixed_advance_pc instead, sacrificing compression. |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 756 | { |
| 757 | uint16_t PCOffset = DebugLineData.getU16(OffsetPtr); |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 758 | State.Row.Address.Address += PCOffset; |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 759 | if (OS) |
| 760 | *OS |
Igor Kudrin | 74c350a | 2019-07-16 06:56:10 +0000 | [diff] [blame] | 761 | << format(" (0x%4.4" PRIx16 ")", PCOffset); |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 762 | } |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 763 | break; |
| 764 | |
| 765 | case DW_LNS_set_prologue_end: |
| 766 | // Takes no arguments. Set the prologue_end register of the |
| 767 | // state machine to true |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 768 | State.Row.PrologueEnd = true; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 769 | break; |
| 770 | |
| 771 | case DW_LNS_set_epilogue_begin: |
| 772 | // Takes no arguments. Set the basic_block register of the |
| 773 | // state machine to true |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 774 | State.Row.EpilogueBegin = true; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 775 | break; |
| 776 | |
| 777 | case DW_LNS_set_isa: |
| 778 | // Takes a single unsigned LEB128 operand and stores it in the |
| 779 | // column register of the state machine. |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 780 | State.Row.Isa = DebugLineData.getULEB128(OffsetPtr); |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 781 | if (OS) |
| 782 | *OS << " (" << State.Row.Isa << ")"; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 783 | break; |
| 784 | |
| 785 | default: |
| 786 | // Handle any unknown standard opcodes here. We know the lengths |
| 787 | // of such opcodes because they are specified in the prologue |
| 788 | // as a multiple of LEB128 operands for each opcode. |
| 789 | { |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 790 | assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size()); |
| 791 | uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1]; |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 792 | for (uint8_t I = 0; I < OpcodeLength; ++I) { |
| 793 | uint64_t Value = DebugLineData.getULEB128(OffsetPtr); |
| 794 | if (OS) |
| 795 | *OS << format("Skipping ULEB128 value: 0x%16.16" PRIx64 ")\n", |
| 796 | Value); |
| 797 | } |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 798 | } |
| 799 | break; |
| 800 | } |
| 801 | } else { |
| 802 | // Special Opcodes |
| 803 | |
| 804 | // A special opcode value is chosen based on the amount that needs |
| 805 | // to be added to the line and address registers. The maximum line |
| 806 | // increment for a special opcode is the value of the line_base |
| 807 | // field in the header, plus the value of the line_range field, |
| 808 | // minus 1 (line base + line range - 1). If the desired line |
| 809 | // increment is greater than the maximum line increment, a standard |
NAKAMURA Takumi | f995985 | 2011-10-08 11:22:47 +0000 | [diff] [blame] | 810 | // opcode must be used instead of a special opcode. The "address |
| 811 | // advance" is calculated by dividing the desired address increment |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 812 | // by the minimum_instruction_length field from the header. The |
| 813 | // special opcode is then calculated using the following formula: |
| 814 | // |
| 815 | // opcode = (desired line increment - line_base) + |
| 816 | // (line_range * address advance) + opcode_base |
| 817 | // |
| 818 | // If the resulting opcode is greater than 255, a standard opcode |
| 819 | // must be used instead. |
| 820 | // |
| 821 | // To decode a special opcode, subtract the opcode_base from the |
| 822 | // opcode itself to give the adjusted opcode. The amount to |
| 823 | // increment the address register is the result of the adjusted |
| 824 | // opcode divided by the line_range multiplied by the |
| 825 | // minimum_instruction_length field from the header. That is: |
| 826 | // |
| 827 | // address increment = (adjusted opcode / line_range) * |
| 828 | // minimum_instruction_length |
| 829 | // |
| 830 | // The amount to increment the line register is the line_base plus |
| 831 | // the result of the adjusted opcode modulo the line_range. That is: |
| 832 | // |
| 833 | // line increment = line_base + (adjusted opcode % line_range) |
| 834 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 835 | uint8_t AdjustOpcode = Opcode - Prologue.OpcodeBase; |
| 836 | uint64_t AddrOffset = |
| 837 | (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength; |
| 838 | int32_t LineOffset = |
| 839 | Prologue.LineBase + (AdjustOpcode % Prologue.LineRange); |
| 840 | State.Row.Line += LineOffset; |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 841 | State.Row.Address.Address += AddrOffset; |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 842 | |
| 843 | if (OS) { |
James Henderson | b6b5b1a | 2019-02-06 10:31:50 +0000 | [diff] [blame] | 844 | *OS << "address += " << AddrOffset << ", line += " << LineOffset |
| 845 | << "\n"; |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 846 | OS->indent(12); |
| 847 | State.Row.dump(*OS); |
| 848 | } |
| 849 | |
Fangrui Song | c4c8bca | 2019-04-07 13:56:14 +0000 | [diff] [blame] | 850 | State.appendRowToMatrix(); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 851 | } |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 852 | if(OS) |
| 853 | *OS << "\n"; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Jonas Devlieghere | 84e9926 | 2018-04-14 22:07:23 +0000 | [diff] [blame] | 856 | if (!State.Sequence.Empty) |
James Henderson | 004b729 | 2018-05-21 15:30:54 +0000 | [diff] [blame] | 857 | RecoverableErrorCallback( |
Victor Leschuk | cba595d | 2018-08-20 09:59:08 +0000 | [diff] [blame] | 858 | createStringError(errc::illegal_byte_sequence, |
| 859 | "last sequence in debug line table is not terminated!")); |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 860 | |
| 861 | // Sort all sequences so that address lookup will work faster. |
| 862 | if (!Sequences.empty()) { |
Fangrui Song | 9b22c46 | 2019-04-09 15:08:32 +0000 | [diff] [blame] | 863 | llvm::sort(Sequences, Sequence::orderByHighPC); |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 864 | // Note: actually, instruction address ranges of sequences should not |
| 865 | // overlap (in shared objects and executables). If they do, the address |
| 866 | // lookup would still work, though, but result would be ambiguous. |
| 867 | // We don't report warning in this case. For example, |
| 868 | // sometimes .so compiled from multiple object files contains a few |
| 869 | // rudimentary sequences for address ranges [0x0, 0xsomething). |
| 870 | } |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 871 | |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 872 | return Error::success(); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 875 | uint32_t DWARFDebugLine::LineTable::findRowInSeq( |
| 876 | const DWARFDebugLine::Sequence &Seq, |
| 877 | object::SectionedAddress Address) const { |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 878 | if (!Seq.containsPC(Address)) |
Keno Fischer | c2c6018 | 2015-05-31 23:37:04 +0000 | [diff] [blame] | 879 | return UnknownRowIndex; |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 880 | assert(Seq.SectionIndex == Address.SectionIndex); |
Fangrui Song | b3be23d | 2019-04-10 07:44:23 +0000 | [diff] [blame] | 881 | // In some cases, e.g. first instruction in a function, the compiler generates |
| 882 | // two entries, both with the same address. We want the last one. |
| 883 | // |
| 884 | // In general we want a non-empty range: the last row whose address is less |
| 885 | // than or equal to Address. This can be computed as upper_bound - 1. |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 886 | DWARFDebugLine::Row Row; |
| 887 | Row.Address = Address; |
| 888 | RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex; |
| 889 | RowIter LastRow = Rows.begin() + Seq.LastRowIndex; |
Fangrui Song | b3be23d | 2019-04-10 07:44:23 +0000 | [diff] [blame] | 890 | assert(FirstRow->Address.Address <= Row.Address.Address && |
| 891 | Row.Address.Address < LastRow[-1].Address.Address); |
| 892 | RowIter RowPos = std::upper_bound(FirstRow + 1, LastRow - 1, Row, |
| 893 | DWARFDebugLine::Row::orderByAddress) - |
| 894 | 1; |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 895 | assert(Seq.SectionIndex == RowPos->Address.SectionIndex); |
Fangrui Song | b3be23d | 2019-04-10 07:44:23 +0000 | [diff] [blame] | 896 | return RowPos - Rows.begin(); |
Keno Fischer | c2c6018 | 2015-05-31 23:37:04 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 899 | uint32_t DWARFDebugLine::LineTable::lookupAddress( |
| 900 | object::SectionedAddress Address) const { |
| 901 | |
| 902 | // Search for relocatable addresses |
| 903 | uint32_t Result = lookupAddressImpl(Address); |
| 904 | |
| 905 | if (Result != UnknownRowIndex || |
| 906 | Address.SectionIndex == object::SectionedAddress::UndefSection) |
| 907 | return Result; |
| 908 | |
| 909 | // Search for absolute addresses |
| 910 | Address.SectionIndex = object::SectionedAddress::UndefSection; |
| 911 | return lookupAddressImpl(Address); |
| 912 | } |
| 913 | |
| 914 | uint32_t DWARFDebugLine::LineTable::lookupAddressImpl( |
| 915 | object::SectionedAddress Address) const { |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 916 | // First, find an instruction sequence containing the given address. |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 917 | DWARFDebugLine::Sequence Sequence; |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 918 | Sequence.SectionIndex = Address.SectionIndex; |
Fangrui Song | 9b22c46 | 2019-04-09 15:08:32 +0000 | [diff] [blame] | 919 | Sequence.HighPC = Address.Address; |
| 920 | SequenceIter It = llvm::upper_bound(Sequences, Sequence, |
| 921 | DWARFDebugLine::Sequence::orderByHighPC); |
| 922 | if (It == Sequences.end() || It->SectionIndex != Address.SectionIndex) |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 923 | return UnknownRowIndex; |
Fangrui Song | 9b22c46 | 2019-04-09 15:08:32 +0000 | [diff] [blame] | 924 | return findRowInSeq(*It, Address); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 925 | } |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 926 | |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 927 | bool DWARFDebugLine::LineTable::lookupAddressRange( |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 928 | object::SectionedAddress Address, uint64_t Size, |
| 929 | std::vector<uint32_t> &Result) const { |
| 930 | |
| 931 | // Search for relocatable addresses |
| 932 | if (lookupAddressRangeImpl(Address, Size, Result)) |
| 933 | return true; |
| 934 | |
| 935 | if (Address.SectionIndex == object::SectionedAddress::UndefSection) |
| 936 | return false; |
| 937 | |
| 938 | // Search for absolute addresses |
| 939 | Address.SectionIndex = object::SectionedAddress::UndefSection; |
| 940 | return lookupAddressRangeImpl(Address, Size, Result); |
| 941 | } |
| 942 | |
| 943 | bool DWARFDebugLine::LineTable::lookupAddressRangeImpl( |
| 944 | object::SectionedAddress Address, uint64_t Size, |
| 945 | std::vector<uint32_t> &Result) const { |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 946 | if (Sequences.empty()) |
| 947 | return false; |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 948 | uint64_t EndAddr = Address.Address + Size; |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 949 | // First, find an instruction sequence containing the given address. |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 950 | DWARFDebugLine::Sequence Sequence; |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 951 | Sequence.SectionIndex = Address.SectionIndex; |
Fangrui Song | 9b22c46 | 2019-04-09 15:08:32 +0000 | [diff] [blame] | 952 | Sequence.HighPC = Address.Address; |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 953 | SequenceIter LastSeq = Sequences.end(); |
Fangrui Song | 9b22c46 | 2019-04-09 15:08:32 +0000 | [diff] [blame] | 954 | SequenceIter SeqPos = llvm::upper_bound( |
| 955 | Sequences, Sequence, DWARFDebugLine::Sequence::orderByHighPC); |
| 956 | if (SeqPos == LastSeq || !SeqPos->containsPC(Address)) |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 957 | return false; |
| 958 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 959 | SequenceIter StartPos = SeqPos; |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 960 | |
| 961 | // Add the rows from the first sequence to the vector, starting with the |
| 962 | // index we just calculated |
| 963 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 964 | while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) { |
| 965 | const DWARFDebugLine::Sequence &CurSeq = *SeqPos; |
Keno Fischer | c2c6018 | 2015-05-31 23:37:04 +0000 | [diff] [blame] | 966 | // For the first sequence, we need to find which row in the sequence is the |
| 967 | // first in our range. |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 968 | uint32_t FirstRowIndex = CurSeq.FirstRowIndex; |
| 969 | if (SeqPos == StartPos) |
| 970 | FirstRowIndex = findRowInSeq(CurSeq, Address); |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 971 | |
Keno Fischer | c2c6018 | 2015-05-31 23:37:04 +0000 | [diff] [blame] | 972 | // Figure out the last row in the range. |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 973 | uint32_t LastRowIndex = |
| 974 | findRowInSeq(CurSeq, {EndAddr - 1, Address.SectionIndex}); |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 975 | if (LastRowIndex == UnknownRowIndex) |
| 976 | LastRowIndex = CurSeq.LastRowIndex - 1; |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 977 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 978 | assert(FirstRowIndex != UnknownRowIndex); |
| 979 | assert(LastRowIndex != UnknownRowIndex); |
Keno Fischer | c2c6018 | 2015-05-31 23:37:04 +0000 | [diff] [blame] | 980 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 981 | for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) { |
| 982 | Result.push_back(I); |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 985 | ++SeqPos; |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 986 | } |
NAKAMURA Takumi | 4b86cdb | 2013-01-26 01:45:06 +0000 | [diff] [blame] | 987 | |
| 988 | return true; |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 989 | } |
| 990 | |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 991 | Optional<StringRef> DWARFDebugLine::LineTable::getSourceByIndex(uint64_t FileIndex, |
| 992 | FileLineInfoKind Kind) const { |
Jonas Devlieghere | ca16d28 | 2019-07-16 01:21:25 +0000 | [diff] [blame] | 993 | if (Kind == FileLineInfoKind::None || !Prologue.hasFileAtIndex(FileIndex)) |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 994 | return None; |
Jonas Devlieghere | ca16d28 | 2019-07-16 01:21:25 +0000 | [diff] [blame] | 995 | const FileNameEntry &Entry = Prologue.getFileNameEntry(FileIndex); |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 996 | if (Optional<const char *> source = Entry.Source.getAsCString()) |
| 997 | return StringRef(*source); |
| 998 | return None; |
| 999 | } |
| 1000 | |
Eugene Zemtsov | 82d60d6 | 2018-03-13 17:54:29 +0000 | [diff] [blame] | 1001 | static bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) { |
| 1002 | // Debug info can contain paths from any OS, not necessarily |
| 1003 | // an OS we're currently running on. Moreover different compilation units can |
| 1004 | // be compiled on different operating systems and linked together later. |
| 1005 | return sys::path::is_absolute(Path, sys::path::Style::posix) || |
| 1006 | sys::path::is_absolute(Path, sys::path::Style::windows); |
| 1007 | } |
| 1008 | |
Jonas Devlieghere | ca16d28 | 2019-07-16 01:21:25 +0000 | [diff] [blame] | 1009 | bool DWARFDebugLine::Prologue::getFileNameByIndex(uint64_t FileIndex, |
| 1010 | StringRef CompDir, |
| 1011 | FileLineInfoKind Kind, |
| 1012 | std::string &Result) const { |
Pete Cooper | b2ba776 | 2016-07-22 01:41:32 +0000 | [diff] [blame] | 1013 | if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex)) |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 1014 | return false; |
Ali Tamur | 783d84b | 2019-04-19 02:26:56 +0000 | [diff] [blame] | 1015 | const FileNameEntry &Entry = getFileNameEntry(FileIndex); |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame] | 1016 | StringRef FileName = Entry.Name.getAsCString().getValue(); |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 1017 | if (Kind != FileLineInfoKind::AbsoluteFilePath || |
Eugene Zemtsov | 82d60d6 | 2018-03-13 17:54:29 +0000 | [diff] [blame] | 1018 | isPathAbsoluteOnWindowsOrPosix(FileName)) { |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 1019 | Result = FileName; |
| 1020 | return true; |
| 1021 | } |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 1022 | |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 1023 | SmallString<16> FilePath; |
Paul Robinson | ba1c915 | 2017-05-02 17:37:32 +0000 | [diff] [blame] | 1024 | StringRef IncludeDir; |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 1025 | // Be defensive about the contents of Entry. |
Jonas Devlieghere | ca16d28 | 2019-07-16 01:21:25 +0000 | [diff] [blame] | 1026 | if (getVersion() >= 5) { |
| 1027 | if (Entry.DirIdx < IncludeDirectories.size()) |
| 1028 | IncludeDir = IncludeDirectories[Entry.DirIdx].getAsCString().getValue(); |
Jonas Devlieghere | 01ee172 | 2019-07-16 00:59:04 +0000 | [diff] [blame] | 1029 | } else { |
Jonas Devlieghere | ca16d28 | 2019-07-16 01:21:25 +0000 | [diff] [blame] | 1030 | if (0 < Entry.DirIdx && Entry.DirIdx <= IncludeDirectories.size()) |
| 1031 | IncludeDir = |
| 1032 | IncludeDirectories[Entry.DirIdx - 1].getAsCString().getValue(); |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 1033 | |
Fangrui Song | 7e55672 | 2019-05-06 08:03:46 +0000 | [diff] [blame] | 1034 | // We may still need to append compilation directory of compile unit. |
| 1035 | // We know that FileName is not absolute, the only way to have an |
| 1036 | // absolute path at this point would be if IncludeDir is absolute. |
Jonas Devlieghere | ca16d28 | 2019-07-16 01:21:25 +0000 | [diff] [blame] | 1037 | if (!CompDir.empty() && !isPathAbsoluteOnWindowsOrPosix(IncludeDir)) |
Fangrui Song | 7e55672 | 2019-05-06 08:03:46 +0000 | [diff] [blame] | 1038 | sys::path::append(FilePath, CompDir); |
| 1039 | } |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 1040 | |
| 1041 | // sys::path::append skips empty strings. |
| 1042 | sys::path::append(FilePath, IncludeDir, FileName); |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 1043 | Result = FilePath.str(); |
| 1044 | return true; |
| 1045 | } |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 1046 | |
Dehao Chen | 1b54fce | 2016-04-28 22:09:37 +0000 | [diff] [blame] | 1047 | bool DWARFDebugLine::LineTable::getFileLineInfoForAddress( |
Alexey Lapshin | 77fc1f6 | 2019-02-27 13:17:36 +0000 | [diff] [blame] | 1048 | object::SectionedAddress Address, const char *CompDir, |
| 1049 | FileLineInfoKind Kind, DILineInfo &Result) const { |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 1050 | // Get the index of row we're looking for in the line table. |
| 1051 | uint32_t RowIndex = lookupAddress(Address); |
| 1052 | if (RowIndex == -1U) |
| 1053 | return false; |
| 1054 | // Take file number and line/column from the row. |
| 1055 | const auto &Row = Rows[RowIndex]; |
| 1056 | if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName)) |
| 1057 | return false; |
| 1058 | Result.Line = Row.Line; |
| 1059 | Result.Column = Row.Column; |
Eric Christopher | ba1024c | 2016-12-14 18:29:39 +0000 | [diff] [blame] | 1060 | Result.Discriminator = Row.Discriminator; |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 1061 | Result.Source = getSourceByIndex(Row.File, Kind); |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 1062 | return true; |
| 1063 | } |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 1064 | |
| 1065 | // We want to supply the Unit associated with a .debug_line[.dwo] table when |
| 1066 | // we dump it, if possible, but still dump the table even if there isn't a Unit. |
| 1067 | // Therefore, collect up handles on all the Units that point into the |
| 1068 | // line-table section. |
| 1069 | static DWARFDebugLine::SectionParser::LineToUnitMap |
| 1070 | buildLineToUnitMap(DWARFDebugLine::SectionParser::cu_range CUs, |
Paul Robinson | 7f33094 | 2018-08-01 20:46:46 +0000 | [diff] [blame] | 1071 | DWARFDebugLine::SectionParser::tu_range TUs) { |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 1072 | DWARFDebugLine::SectionParser::LineToUnitMap LineToUnit; |
| 1073 | for (const auto &CU : CUs) |
| 1074 | if (auto CUDIE = CU->getUnitDIE()) |
| 1075 | if (auto StmtOffset = toSectionOffset(CUDIE.find(DW_AT_stmt_list))) |
| 1076 | LineToUnit.insert(std::make_pair(*StmtOffset, &*CU)); |
Paul Robinson | 7f33094 | 2018-08-01 20:46:46 +0000 | [diff] [blame] | 1077 | for (const auto &TU : TUs) |
| 1078 | if (auto TUDIE = TU->getUnitDIE()) |
| 1079 | if (auto StmtOffset = toSectionOffset(TUDIE.find(DW_AT_stmt_list))) |
| 1080 | LineToUnit.insert(std::make_pair(*StmtOffset, &*TU)); |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 1081 | return LineToUnit; |
| 1082 | } |
| 1083 | |
| 1084 | DWARFDebugLine::SectionParser::SectionParser(DWARFDataExtractor &Data, |
| 1085 | const DWARFContext &C, |
| 1086 | cu_range CUs, tu_range TUs) |
| 1087 | : DebugLineData(Data), Context(C) { |
| 1088 | LineToUnit = buildLineToUnitMap(CUs, TUs); |
| 1089 | if (!DebugLineData.isValidOffset(Offset)) |
| 1090 | Done = true; |
| 1091 | } |
| 1092 | |
| 1093 | bool DWARFDebugLine::Prologue::totalLengthIsValid() const { |
Igor Kudrin | f48bc01 | 2019-07-16 07:01:08 +0000 | [diff] [blame] | 1094 | return TotalLength == 0xffffffff || TotalLength < 0xfffffff0; |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | DWARFDebugLine::LineTable DWARFDebugLine::SectionParser::parseNext( |
James Henderson | 004b729 | 2018-05-21 15:30:54 +0000 | [diff] [blame] | 1098 | function_ref<void(Error)> RecoverableErrorCallback, |
| 1099 | function_ref<void(Error)> UnrecoverableErrorCallback, raw_ostream *OS) { |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 1100 | assert(DebugLineData.isValidOffset(Offset) && |
| 1101 | "parsing should have terminated"); |
| 1102 | DWARFUnit *U = prepareToParse(Offset); |
| 1103 | uint32_t OldOffset = Offset; |
| 1104 | LineTable LT; |
James Henderson | 004b729 | 2018-05-21 15:30:54 +0000 | [diff] [blame] | 1105 | if (Error Err = LT.parse(DebugLineData, &Offset, Context, U, |
| 1106 | RecoverableErrorCallback, OS)) |
| 1107 | UnrecoverableErrorCallback(std::move(Err)); |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 1108 | moveToNextTable(OldOffset, LT.Prologue); |
| 1109 | return LT; |
| 1110 | } |
| 1111 | |
| 1112 | void DWARFDebugLine::SectionParser::skip( |
| 1113 | function_ref<void(Error)> ErrorCallback) { |
| 1114 | assert(DebugLineData.isValidOffset(Offset) && |
| 1115 | "parsing should have terminated"); |
| 1116 | DWARFUnit *U = prepareToParse(Offset); |
| 1117 | uint32_t OldOffset = Offset; |
| 1118 | LineTable LT; |
James Henderson | 004b729 | 2018-05-21 15:30:54 +0000 | [diff] [blame] | 1119 | if (Error Err = LT.Prologue.parse(DebugLineData, &Offset, Context, U)) |
| 1120 | ErrorCallback(std::move(Err)); |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 1121 | moveToNextTable(OldOffset, LT.Prologue); |
| 1122 | } |
| 1123 | |
| 1124 | DWARFUnit *DWARFDebugLine::SectionParser::prepareToParse(uint32_t Offset) { |
| 1125 | DWARFUnit *U = nullptr; |
| 1126 | auto It = LineToUnit.find(Offset); |
| 1127 | if (It != LineToUnit.end()) |
| 1128 | U = It->second; |
| 1129 | DebugLineData.setAddressSize(U ? U->getAddressByteSize() : 0); |
| 1130 | return U; |
| 1131 | } |
| 1132 | |
| 1133 | void DWARFDebugLine::SectionParser::moveToNextTable(uint32_t OldOffset, |
| 1134 | const Prologue &P) { |
| 1135 | // If the length field is not valid, we don't know where the next table is, so |
| 1136 | // cannot continue to parse. Mark the parser as done, and leave the Offset |
| 1137 | // value as it currently is. This will be the end of the bad length field. |
| 1138 | if (!P.totalLengthIsValid()) { |
| 1139 | Done = true; |
| 1140 | return; |
| 1141 | } |
| 1142 | |
| 1143 | Offset = OldOffset + P.TotalLength + P.sizeofTotalLength(); |
| 1144 | if (!DebugLineData.isValidOffset(Offset)) { |
| 1145 | Done = true; |
| 1146 | } |
| 1147 | } |