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