DWARFDebugLine.cpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  1. //===- DWARFDebugLine.cpp -------------------------------------------------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
  9. #include "llvm/ADT/Optional.h"
  10. #include "llvm/ADT/SmallString.h"
  11. #include "llvm/ADT/SmallVector.h"
  12. #include "llvm/ADT/StringRef.h"
  13. #include "llvm/BinaryFormat/Dwarf.h"
  14. #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
  15. #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
  16. #include "llvm/Support/Errc.h"
  17. #include "llvm/Support/Format.h"
  18. #include "llvm/Support/FormatVariadic.h"
  19. #include "llvm/Support/WithColor.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. #include <algorithm>
  22. #include <cassert>
  23. #include <cinttypes>
  24. #include <cstdint>
  25. #include <cstdio>
  26. #include <utility>
  27. using namespace llvm;
  28. using namespace dwarf;
  29. using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind;
  30. namespace {
  31. struct ContentDescriptor {
  32. dwarf::LineNumberEntryFormat Type;
  33. dwarf::Form Form;
  34. };
  35. using ContentDescriptors = SmallVector<ContentDescriptor, 4>;
  36. } // end anonymous namespace
  37. static bool versionIsSupported(uint16_t Version) {
  38. return Version >= 2 && Version <= 5;
  39. }
  40. void DWARFDebugLine::ContentTypeTracker::trackContentType(
  41. dwarf::LineNumberEntryFormat ContentType) {
  42. switch (ContentType) {
  43. case dwarf::DW_LNCT_timestamp:
  44. HasModTime = true;
  45. break;
  46. case dwarf::DW_LNCT_size:
  47. HasLength = true;
  48. break;
  49. case dwarf::DW_LNCT_MD5:
  50. HasMD5 = true;
  51. break;
  52. case dwarf::DW_LNCT_LLVM_source:
  53. HasSource = true;
  54. break;
  55. default:
  56. // We only care about values we consider optional, and new values may be
  57. // added in the vendor extension range, so we do not match exhaustively.
  58. break;
  59. }
  60. }
  61. DWARFDebugLine::Prologue::Prologue() { clear(); }
  62. bool DWARFDebugLine::Prologue::hasFileAtIndex(uint64_t FileIndex) const {
  63. uint16_t DwarfVersion = getVersion();
  64. assert(DwarfVersion != 0 &&
  65. "line table prologue has no dwarf version information");
  66. if (DwarfVersion >= 5)
  67. return FileIndex < FileNames.size();
  68. return FileIndex != 0 && FileIndex <= FileNames.size();
  69. }
  70. Optional<uint64_t> DWARFDebugLine::Prologue::getLastValidFileIndex() const {
  71. if (FileNames.empty())
  72. return None;
  73. uint16_t DwarfVersion = getVersion();
  74. assert(DwarfVersion != 0 &&
  75. "line table prologue has no dwarf version information");
  76. // In DWARF v5 the file names are 0-indexed.
  77. if (DwarfVersion >= 5)
  78. return FileNames.size() - 1;
  79. return FileNames.size();
  80. }
  81. const llvm::DWARFDebugLine::FileNameEntry &
  82. DWARFDebugLine::Prologue::getFileNameEntry(uint64_t Index) const {
  83. uint16_t DwarfVersion = getVersion();
  84. assert(DwarfVersion != 0 &&
  85. "line table prologue has no dwarf version information");
  86. // In DWARF v5 the file names are 0-indexed.
  87. if (DwarfVersion >= 5)
  88. return FileNames[Index];
  89. return FileNames[Index - 1];
  90. }
  91. void DWARFDebugLine::Prologue::clear() {
  92. TotalLength = PrologueLength = 0;
  93. SegSelectorSize = 0;
  94. MinInstLength = MaxOpsPerInst = DefaultIsStmt = LineBase = LineRange = 0;
  95. OpcodeBase = 0;
  96. FormParams = dwarf::FormParams({0, 0, DWARF32});
  97. ContentTypes = ContentTypeTracker();
  98. StandardOpcodeLengths.clear();
  99. IncludeDirectories.clear();
  100. FileNames.clear();
  101. }
  102. void DWARFDebugLine::Prologue::dump(raw_ostream &OS,
  103. DIDumpOptions DumpOptions) const {
  104. if (!totalLengthIsValid())
  105. return;
  106. int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(FormParams.Format);
  107. OS << "Line table prologue:\n"
  108. << format(" total_length: 0x%0*" PRIx64 "\n", OffsetDumpWidth,
  109. TotalLength)
  110. << " format: " << dwarf::FormatString(FormParams.Format) << "\n"
  111. << format(" version: %u\n", getVersion());
  112. if (!versionIsSupported(getVersion()))
  113. return;
  114. if (getVersion() >= 5)
  115. OS << format(" address_size: %u\n", getAddressSize())
  116. << format(" seg_select_size: %u\n", SegSelectorSize);
  117. OS << format(" prologue_length: 0x%0*" PRIx64 "\n", OffsetDumpWidth,
  118. PrologueLength)
  119. << format(" min_inst_length: %u\n", MinInstLength)
  120. << format(getVersion() >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst)
  121. << format(" default_is_stmt: %u\n", DefaultIsStmt)
  122. << format(" line_base: %i\n", LineBase)
  123. << format(" line_range: %u\n", LineRange)
  124. << format(" opcode_base: %u\n", OpcodeBase);
  125. for (uint32_t I = 0; I != StandardOpcodeLengths.size(); ++I)
  126. OS << formatv("standard_opcode_lengths[{0}] = {1}\n",
  127. static_cast<dwarf::LineNumberOps>(I + 1),
  128. StandardOpcodeLengths[I]);
  129. if (!IncludeDirectories.empty()) {
  130. // DWARF v5 starts directory indexes at 0.
  131. uint32_t DirBase = getVersion() >= 5 ? 0 : 1;
  132. for (uint32_t I = 0; I != IncludeDirectories.size(); ++I) {
  133. OS << format("include_directories[%3u] = ", I + DirBase);
  134. IncludeDirectories[I].dump(OS, DumpOptions);
  135. OS << '\n';
  136. }
  137. }
  138. if (!FileNames.empty()) {
  139. // DWARF v5 starts file indexes at 0.
  140. uint32_t FileBase = getVersion() >= 5 ? 0 : 1;
  141. for (uint32_t I = 0; I != FileNames.size(); ++I) {
  142. const FileNameEntry &FileEntry = FileNames[I];
  143. OS << format("file_names[%3u]:\n", I + FileBase);
  144. OS << " name: ";
  145. FileEntry.Name.dump(OS, DumpOptions);
  146. OS << '\n'
  147. << format(" dir_index: %" PRIu64 "\n", FileEntry.DirIdx);
  148. if (ContentTypes.HasMD5)
  149. OS << " md5_checksum: " << FileEntry.Checksum.digest() << '\n';
  150. if (ContentTypes.HasModTime)
  151. OS << format(" mod_time: 0x%8.8" PRIx64 "\n", FileEntry.ModTime);
  152. if (ContentTypes.HasLength)
  153. OS << format(" length: 0x%8.8" PRIx64 "\n", FileEntry.Length);
  154. if (ContentTypes.HasSource) {
  155. OS << " source: ";
  156. FileEntry.Source.dump(OS, DumpOptions);
  157. OS << '\n';
  158. }
  159. }
  160. }
  161. }
  162. // Parse v2-v4 directory and file tables.
  163. static Error
  164. parseV2DirFileTables(const DWARFDataExtractor &DebugLineData,
  165. uint64_t *OffsetPtr,
  166. DWARFDebugLine::ContentTypeTracker &ContentTypes,
  167. std::vector<DWARFFormValue> &IncludeDirectories,
  168. std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
  169. while (true) {
  170. Error Err = Error::success();
  171. StringRef S = DebugLineData.getCStrRef(OffsetPtr, &Err);
  172. if (Err) {
  173. consumeError(std::move(Err));
  174. return createStringError(errc::invalid_argument,
  175. "include directories table was not null "
  176. "terminated before the end of the prologue");
  177. }
  178. if (S.empty())
  179. break;
  180. DWARFFormValue Dir =
  181. DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, S.data());
  182. IncludeDirectories.push_back(Dir);
  183. }
  184. ContentTypes.HasModTime = true;
  185. ContentTypes.HasLength = true;
  186. while (true) {
  187. Error Err = Error::success();
  188. StringRef Name = DebugLineData.getCStrRef(OffsetPtr, &Err);
  189. if (!Err && Name.empty())
  190. break;
  191. DWARFDebugLine::FileNameEntry FileEntry;
  192. FileEntry.Name =
  193. DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, Name.data());
  194. FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr, &Err);
  195. FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr, &Err);
  196. FileEntry.Length = DebugLineData.getULEB128(OffsetPtr, &Err);
  197. if (Err) {
  198. consumeError(std::move(Err));
  199. return createStringError(
  200. errc::invalid_argument,
  201. "file names table was not null terminated before "
  202. "the end of the prologue");
  203. }
  204. FileNames.push_back(FileEntry);
  205. }
  206. return Error::success();
  207. }
  208. // Parse v5 directory/file entry content descriptions.
  209. // Returns the descriptors, or an error if we did not find a path or ran off
  210. // the end of the prologue.
  211. static llvm::Expected<ContentDescriptors>
  212. parseV5EntryFormat(const DWARFDataExtractor &DebugLineData, uint64_t *OffsetPtr,
  213. DWARFDebugLine::ContentTypeTracker *ContentTypes) {
  214. Error Err = Error::success();
  215. ContentDescriptors Descriptors;
  216. int FormatCount = DebugLineData.getU8(OffsetPtr, &Err);
  217. bool HasPath = false;
  218. for (int I = 0; I != FormatCount && !Err; ++I) {
  219. ContentDescriptor Descriptor;
  220. Descriptor.Type =
  221. dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr, &Err));
  222. Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr, &Err));
  223. if (Descriptor.Type == dwarf::DW_LNCT_path)
  224. HasPath = true;
  225. if (ContentTypes)
  226. ContentTypes->trackContentType(Descriptor.Type);
  227. Descriptors.push_back(Descriptor);
  228. }
  229. if (Err)
  230. return createStringError(errc::invalid_argument,
  231. "failed to parse entry content descriptors: %s",
  232. toString(std::move(Err)).c_str());
  233. if (!HasPath)
  234. return createStringError(errc::invalid_argument,
  235. "failed to parse entry content descriptions"
  236. " because no path was found");
  237. return Descriptors;
  238. }
  239. static Error
  240. parseV5DirFileTables(const DWARFDataExtractor &DebugLineData,
  241. uint64_t *OffsetPtr, const dwarf::FormParams &FormParams,
  242. const DWARFContext &Ctx, const DWARFUnit *U,
  243. DWARFDebugLine::ContentTypeTracker &ContentTypes,
  244. std::vector<DWARFFormValue> &IncludeDirectories,
  245. std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
  246. // Get the directory entry description.
  247. llvm::Expected<ContentDescriptors> DirDescriptors =
  248. parseV5EntryFormat(DebugLineData, OffsetPtr, nullptr);
  249. if (!DirDescriptors)
  250. return DirDescriptors.takeError();
  251. // Get the directory entries, according to the format described above.
  252. uint64_t DirEntryCount = DebugLineData.getULEB128(OffsetPtr);
  253. for (uint64_t I = 0; I != DirEntryCount; ++I) {
  254. for (auto Descriptor : *DirDescriptors) {
  255. DWARFFormValue Value(Descriptor.Form);
  256. switch (Descriptor.Type) {
  257. case DW_LNCT_path:
  258. if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
  259. return createStringError(errc::invalid_argument,
  260. "failed to parse directory entry because "
  261. "extracting the form value failed");
  262. IncludeDirectories.push_back(Value);
  263. break;
  264. default:
  265. if (!Value.skipValue(DebugLineData, OffsetPtr, FormParams))
  266. return createStringError(errc::invalid_argument,
  267. "failed to parse directory entry because "
  268. "skipping the form value failed");
  269. }
  270. }
  271. }
  272. // Get the file entry description.
  273. llvm::Expected<ContentDescriptors> FileDescriptors =
  274. parseV5EntryFormat(DebugLineData, OffsetPtr, &ContentTypes);
  275. if (!FileDescriptors)
  276. return FileDescriptors.takeError();
  277. // Get the file entries, according to the format described above.
  278. uint64_t FileEntryCount = DebugLineData.getULEB128(OffsetPtr);
  279. for (uint64_t I = 0; I != FileEntryCount; ++I) {
  280. DWARFDebugLine::FileNameEntry FileEntry;
  281. for (auto Descriptor : *FileDescriptors) {
  282. DWARFFormValue Value(Descriptor.Form);
  283. if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
  284. return createStringError(errc::invalid_argument,
  285. "failed to parse file entry because "
  286. "extracting the form value failed");
  287. switch (Descriptor.Type) {
  288. case DW_LNCT_path:
  289. FileEntry.Name = Value;
  290. break;
  291. case DW_LNCT_LLVM_source:
  292. FileEntry.Source = Value;
  293. break;
  294. case DW_LNCT_directory_index:
  295. FileEntry.DirIdx = Value.getAsUnsignedConstant().getValue();
  296. break;
  297. case DW_LNCT_timestamp:
  298. FileEntry.ModTime = Value.getAsUnsignedConstant().getValue();
  299. break;
  300. case DW_LNCT_size:
  301. FileEntry.Length = Value.getAsUnsignedConstant().getValue();
  302. break;
  303. case DW_LNCT_MD5:
  304. if (!Value.getAsBlock() || Value.getAsBlock().getValue().size() != 16)
  305. return createStringError(
  306. errc::invalid_argument,
  307. "failed to parse file entry because the MD5 hash is invalid");
  308. std::uninitialized_copy_n(Value.getAsBlock().getValue().begin(), 16,
  309. FileEntry.Checksum.Bytes.begin());
  310. break;
  311. default:
  312. break;
  313. }
  314. }
  315. FileNames.push_back(FileEntry);
  316. }
  317. return Error::success();
  318. }
  319. uint64_t DWARFDebugLine::Prologue::getLength() const {
  320. uint64_t Length = PrologueLength + sizeofTotalLength() +
  321. sizeof(getVersion()) + sizeofPrologueLength();
  322. if (getVersion() >= 5)
  323. Length += 2; // Address + Segment selector sizes.
  324. return Length;
  325. }
  326. Error DWARFDebugLine::Prologue::parse(
  327. DWARFDataExtractor DebugLineData, uint64_t *OffsetPtr,
  328. function_ref<void(Error)> RecoverableErrorHandler, const DWARFContext &Ctx,
  329. const DWARFUnit *U) {
  330. const uint64_t PrologueOffset = *OffsetPtr;
  331. clear();
  332. DataExtractor::Cursor Cursor(*OffsetPtr);
  333. std::tie(TotalLength, FormParams.Format) =
  334. DebugLineData.getInitialLength(Cursor);
  335. DebugLineData =
  336. DWARFDataExtractor(DebugLineData, Cursor.tell() + TotalLength);
  337. FormParams.Version = DebugLineData.getU16(Cursor);
  338. if (Cursor && !versionIsSupported(getVersion())) {
  339. // Treat this error as unrecoverable - we cannot be sure what any of
  340. // the data represents including the length field, so cannot skip it or make
  341. // any reasonable assumptions.
  342. *OffsetPtr = Cursor.tell();
  343. return createStringError(
  344. errc::not_supported,
  345. "parsing line table prologue at offset 0x%8.8" PRIx64
  346. ": unsupported version %" PRIu16,
  347. PrologueOffset, getVersion());
  348. }
  349. if (getVersion() >= 5) {
  350. FormParams.AddrSize = DebugLineData.getU8(Cursor);
  351. assert((!Cursor || DebugLineData.getAddressSize() == 0 ||
  352. DebugLineData.getAddressSize() == getAddressSize()) &&
  353. "Line table header and data extractor disagree");
  354. SegSelectorSize = DebugLineData.getU8(Cursor);
  355. }
  356. PrologueLength =
  357. DebugLineData.getRelocatedValue(Cursor, sizeofPrologueLength());
  358. const uint64_t EndPrologueOffset = PrologueLength + Cursor.tell();
  359. DebugLineData = DWARFDataExtractor(DebugLineData, EndPrologueOffset);
  360. MinInstLength = DebugLineData.getU8(Cursor);
  361. if (getVersion() >= 4)
  362. MaxOpsPerInst = DebugLineData.getU8(Cursor);
  363. DefaultIsStmt = DebugLineData.getU8(Cursor);
  364. LineBase = DebugLineData.getU8(Cursor);
  365. LineRange = DebugLineData.getU8(Cursor);
  366. OpcodeBase = DebugLineData.getU8(Cursor);
  367. if (Cursor && OpcodeBase == 0) {
  368. // If the opcode base is 0, we cannot read the standard opcode lengths (of
  369. // which there are supposed to be one fewer than the opcode base). Assume
  370. // there are no standard opcodes and continue parsing.
  371. RecoverableErrorHandler(createStringError(
  372. errc::invalid_argument,
  373. "parsing line table prologue at offset 0x%8.8" PRIx64
  374. " found opcode base of 0. Assuming no standard opcodes",
  375. PrologueOffset));
  376. } else if (Cursor) {
  377. StandardOpcodeLengths.reserve(OpcodeBase - 1);
  378. for (uint32_t I = 1; I < OpcodeBase; ++I) {
  379. uint8_t OpLen = DebugLineData.getU8(Cursor);
  380. StandardOpcodeLengths.push_back(OpLen);
  381. }
  382. }
  383. *OffsetPtr = Cursor.tell();
  384. // A corrupt file name or directory table does not prevent interpretation of
  385. // the main line program, so check the cursor state now so that its errors can
  386. // be handled separately.
  387. if (!Cursor)
  388. return createStringError(
  389. errc::invalid_argument,
  390. "parsing line table prologue at offset 0x%8.8" PRIx64 ": %s",
  391. PrologueOffset, toString(Cursor.takeError()).c_str());
  392. Error E =
  393. getVersion() >= 5
  394. ? parseV5DirFileTables(DebugLineData, OffsetPtr, FormParams, Ctx, U,
  395. ContentTypes, IncludeDirectories, FileNames)
  396. : parseV2DirFileTables(DebugLineData, OffsetPtr, ContentTypes,
  397. IncludeDirectories, FileNames);
  398. if (E) {
  399. RecoverableErrorHandler(joinErrors(
  400. createStringError(
  401. errc::invalid_argument,
  402. "parsing line table prologue at 0x%8.8" PRIx64
  403. " found an invalid directory or file table description at"
  404. " 0x%8.8" PRIx64,
  405. PrologueOffset, *OffsetPtr),
  406. std::move(E)));
  407. return Error::success();
  408. }
  409. assert(*OffsetPtr <= EndPrologueOffset);
  410. if (*OffsetPtr != EndPrologueOffset) {
  411. RecoverableErrorHandler(createStringError(
  412. errc::invalid_argument,
  413. "unknown data in line table prologue at offset 0x%8.8" PRIx64
  414. ": parsing ended (at offset 0x%8.8" PRIx64
  415. ") before reaching the prologue end at offset 0x%8.8" PRIx64,
  416. PrologueOffset, *OffsetPtr, EndPrologueOffset));
  417. }
  418. return Error::success();
  419. }
  420. DWARFDebugLine::Row::Row(bool DefaultIsStmt) { reset(DefaultIsStmt); }
  421. void DWARFDebugLine::Row::postAppend() {
  422. Discriminator = 0;
  423. BasicBlock = false;
  424. PrologueEnd = false;
  425. EpilogueBegin = false;
  426. }
  427. void DWARFDebugLine::Row::reset(bool DefaultIsStmt) {
  428. Address.Address = 0;
  429. Address.SectionIndex = object::SectionedAddress::UndefSection;
  430. Line = 1;
  431. Column = 0;
  432. File = 1;
  433. Isa = 0;
  434. Discriminator = 0;
  435. IsStmt = DefaultIsStmt;
  436. BasicBlock = false;
  437. EndSequence = false;
  438. PrologueEnd = false;
  439. EpilogueBegin = false;
  440. }
  441. void DWARFDebugLine::Row::dumpTableHeader(raw_ostream &OS, unsigned Indent) {
  442. OS.indent(Indent)
  443. << "Address Line Column File ISA Discriminator Flags\n";
  444. OS.indent(Indent)
  445. << "------------------ ------ ------ ------ --- ------------- "
  446. "-------------\n";
  447. }
  448. void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
  449. OS << format("0x%16.16" PRIx64 " %6u %6u", Address.Address, Line, Column)
  450. << format(" %6u %3u %13u ", File, Isa, Discriminator)
  451. << (IsStmt ? " is_stmt" : "") << (BasicBlock ? " basic_block" : "")
  452. << (PrologueEnd ? " prologue_end" : "")
  453. << (EpilogueBegin ? " epilogue_begin" : "")
  454. << (EndSequence ? " end_sequence" : "") << '\n';
  455. }
  456. DWARFDebugLine::Sequence::Sequence() { reset(); }
  457. void DWARFDebugLine::Sequence::reset() {
  458. LowPC = 0;
  459. HighPC = 0;
  460. SectionIndex = object::SectionedAddress::UndefSection;
  461. FirstRowIndex = 0;
  462. LastRowIndex = 0;
  463. Empty = true;
  464. }
  465. DWARFDebugLine::LineTable::LineTable() { clear(); }
  466. void DWARFDebugLine::LineTable::dump(raw_ostream &OS,
  467. DIDumpOptions DumpOptions) const {
  468. Prologue.dump(OS, DumpOptions);
  469. if (!Rows.empty()) {
  470. OS << '\n';
  471. Row::dumpTableHeader(OS, 0);
  472. for (const Row &R : Rows) {
  473. R.dump(OS);
  474. }
  475. }
  476. // Terminate the table with a final blank line to clearly delineate it from
  477. // later dumps.
  478. OS << '\n';
  479. }
  480. void DWARFDebugLine::LineTable::clear() {
  481. Prologue.clear();
  482. Rows.clear();
  483. Sequences.clear();
  484. }
  485. DWARFDebugLine::ParsingState::ParsingState(
  486. struct LineTable *LT, uint64_t TableOffset,
  487. function_ref<void(Error)> ErrorHandler)
  488. : LineTable(LT), LineTableOffset(TableOffset), ErrorHandler(ErrorHandler) {
  489. resetRowAndSequence();
  490. }
  491. void DWARFDebugLine::ParsingState::resetRowAndSequence() {
  492. Row.reset(LineTable->Prologue.DefaultIsStmt);
  493. Sequence.reset();
  494. }
  495. void DWARFDebugLine::ParsingState::appendRowToMatrix() {
  496. unsigned RowNumber = LineTable->Rows.size();
  497. if (Sequence.Empty) {
  498. // Record the beginning of instruction sequence.
  499. Sequence.Empty = false;
  500. Sequence.LowPC = Row.Address.Address;
  501. Sequence.FirstRowIndex = RowNumber;
  502. }
  503. LineTable->appendRow(Row);
  504. if (Row.EndSequence) {
  505. // Record the end of instruction sequence.
  506. Sequence.HighPC = Row.Address.Address;
  507. Sequence.LastRowIndex = RowNumber + 1;
  508. Sequence.SectionIndex = Row.Address.SectionIndex;
  509. if (Sequence.isValid())
  510. LineTable->appendSequence(Sequence);
  511. Sequence.reset();
  512. }
  513. Row.postAppend();
  514. }
  515. const DWARFDebugLine::LineTable *
  516. DWARFDebugLine::getLineTable(uint64_t Offset) const {
  517. LineTableConstIter Pos = LineTableMap.find(Offset);
  518. if (Pos != LineTableMap.end())
  519. return &Pos->second;
  520. return nullptr;
  521. }
  522. Expected<const DWARFDebugLine::LineTable *> DWARFDebugLine::getOrParseLineTable(
  523. DWARFDataExtractor &DebugLineData, uint64_t Offset, const DWARFContext &Ctx,
  524. const DWARFUnit *U, function_ref<void(Error)> RecoverableErrorHandler) {
  525. if (!DebugLineData.isValidOffset(Offset))
  526. return createStringError(errc::invalid_argument, "offset 0x%8.8" PRIx64
  527. " is not a valid debug line section offset",
  528. Offset);
  529. std::pair<LineTableIter, bool> Pos =
  530. LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable()));
  531. LineTable *LT = &Pos.first->second;
  532. if (Pos.second) {
  533. if (Error Err =
  534. LT->parse(DebugLineData, &Offset, Ctx, U, RecoverableErrorHandler))
  535. return std::move(Err);
  536. return LT;
  537. }
  538. return LT;
  539. }
  540. static StringRef getOpcodeName(uint8_t Opcode, uint8_t OpcodeBase) {
  541. assert(Opcode != 0);
  542. if (Opcode < OpcodeBase)
  543. return LNStandardString(Opcode);
  544. return "special";
  545. }
  546. uint64_t DWARFDebugLine::ParsingState::advanceAddr(uint64_t OperationAdvance,
  547. uint8_t Opcode,
  548. uint64_t OpcodeOffset) {
  549. StringRef OpcodeName = getOpcodeName(Opcode, LineTable->Prologue.OpcodeBase);
  550. // For versions less than 4, the MaxOpsPerInst member is set to 0, as the
  551. // maximum_operations_per_instruction field wasn't introduced until DWARFv4.
  552. // Don't warn about bad values in this situation.
  553. if (ReportAdvanceAddrProblem && LineTable->Prologue.getVersion() >= 4 &&
  554. LineTable->Prologue.MaxOpsPerInst != 1)
  555. ErrorHandler(createStringError(
  556. errc::not_supported,
  557. "line table program at offset 0x%8.8" PRIx64
  558. " contains a %s opcode at offset 0x%8.8" PRIx64
  559. ", but the prologue maximum_operations_per_instruction value is %" PRId8
  560. ", which is unsupported. Assuming a value of 1 instead",
  561. LineTableOffset, OpcodeName.data(), OpcodeOffset,
  562. LineTable->Prologue.MaxOpsPerInst));
  563. if (ReportAdvanceAddrProblem && LineTable->Prologue.MinInstLength == 0)
  564. ErrorHandler(
  565. createStringError(errc::invalid_argument,
  566. "line table program at offset 0x%8.8" PRIx64
  567. " contains a %s opcode at offset 0x%8.8" PRIx64
  568. ", but the prologue minimum_instruction_length value "
  569. "is 0, which prevents any address advancing",
  570. LineTableOffset, OpcodeName.data(), OpcodeOffset));
  571. ReportAdvanceAddrProblem = false;
  572. uint64_t AddrOffset = OperationAdvance * LineTable->Prologue.MinInstLength;
  573. Row.Address.Address += AddrOffset;
  574. return AddrOffset;
  575. }
  576. DWARFDebugLine::ParsingState::AddrAndAdjustedOpcode
  577. DWARFDebugLine::ParsingState::advanceAddrForOpcode(uint8_t Opcode,
  578. uint64_t OpcodeOffset) {
  579. assert(Opcode == DW_LNS_const_add_pc ||
  580. Opcode >= LineTable->Prologue.OpcodeBase);
  581. if (ReportBadLineRange && LineTable->Prologue.LineRange == 0) {
  582. StringRef OpcodeName =
  583. getOpcodeName(Opcode, LineTable->Prologue.OpcodeBase);
  584. ErrorHandler(
  585. createStringError(errc::not_supported,
  586. "line table program at offset 0x%8.8" PRIx64
  587. " contains a %s opcode at offset 0x%8.8" PRIx64
  588. ", but the prologue line_range value is 0. The "
  589. "address and line will not be adjusted",
  590. LineTableOffset, OpcodeName.data(), OpcodeOffset));
  591. ReportBadLineRange = false;
  592. }
  593. uint8_t OpcodeValue = Opcode;
  594. if (Opcode == DW_LNS_const_add_pc)
  595. OpcodeValue = 255;
  596. uint8_t AdjustedOpcode = OpcodeValue - LineTable->Prologue.OpcodeBase;
  597. uint64_t OperationAdvance =
  598. LineTable->Prologue.LineRange != 0
  599. ? AdjustedOpcode / LineTable->Prologue.LineRange
  600. : 0;
  601. uint64_t AddrOffset = advanceAddr(OperationAdvance, Opcode, OpcodeOffset);
  602. return {AddrOffset, AdjustedOpcode};
  603. }
  604. DWARFDebugLine::ParsingState::AddrAndLineDelta
  605. DWARFDebugLine::ParsingState::handleSpecialOpcode(uint8_t Opcode,
  606. uint64_t OpcodeOffset) {
  607. // A special opcode value is chosen based on the amount that needs
  608. // to be added to the line and address registers. The maximum line
  609. // increment for a special opcode is the value of the line_base
  610. // field in the header, plus the value of the line_range field,
  611. // minus 1 (line base + line range - 1). If the desired line
  612. // increment is greater than the maximum line increment, a standard
  613. // opcode must be used instead of a special opcode. The "address
  614. // advance" is calculated by dividing the desired address increment
  615. // by the minimum_instruction_length field from the header. The
  616. // special opcode is then calculated using the following formula:
  617. //
  618. // opcode = (desired line increment - line_base) +
  619. // (line_range * address advance) + opcode_base
  620. //
  621. // If the resulting opcode is greater than 255, a standard opcode
  622. // must be used instead.
  623. //
  624. // To decode a special opcode, subtract the opcode_base from the
  625. // opcode itself to give the adjusted opcode. The amount to
  626. // increment the address register is the result of the adjusted
  627. // opcode divided by the line_range multiplied by the
  628. // minimum_instruction_length field from the header. That is:
  629. //
  630. // address increment = (adjusted opcode / line_range) *
  631. // minimum_instruction_length
  632. //
  633. // The amount to increment the line register is the line_base plus
  634. // the result of the adjusted opcode modulo the line_range. That is:
  635. //
  636. // line increment = line_base + (adjusted opcode % line_range)
  637. DWARFDebugLine::ParsingState::AddrAndAdjustedOpcode AddrAdvanceResult =
  638. advanceAddrForOpcode(Opcode, OpcodeOffset);
  639. int32_t LineOffset = 0;
  640. if (LineTable->Prologue.LineRange != 0)
  641. LineOffset =
  642. LineTable->Prologue.LineBase +
  643. (AddrAdvanceResult.AdjustedOpcode % LineTable->Prologue.LineRange);
  644. Row.Line += LineOffset;
  645. return {AddrAdvanceResult.AddrDelta, LineOffset};
  646. }
  647. /// Parse a ULEB128 using the specified \p Cursor. \returns the parsed value on
  648. /// success, or None if \p Cursor is in a failing state.
  649. template <typename T>
  650. static Optional<T> parseULEB128(DWARFDataExtractor &Data,
  651. DataExtractor::Cursor &Cursor) {
  652. T Value = Data.getULEB128(Cursor);
  653. if (Cursor)
  654. return Value;
  655. return None;
  656. }
  657. Error DWARFDebugLine::LineTable::parse(
  658. DWARFDataExtractor &DebugLineData, uint64_t *OffsetPtr,
  659. const DWARFContext &Ctx, const DWARFUnit *U,
  660. function_ref<void(Error)> RecoverableErrorHandler, raw_ostream *OS,
  661. bool Verbose) {
  662. assert((OS || !Verbose) && "cannot have verbose output without stream");
  663. const uint64_t DebugLineOffset = *OffsetPtr;
  664. clear();
  665. Error PrologueErr =
  666. Prologue.parse(DebugLineData, OffsetPtr, RecoverableErrorHandler, Ctx, U);
  667. if (OS) {
  668. DIDumpOptions DumpOptions;
  669. DumpOptions.Verbose = Verbose;
  670. Prologue.dump(*OS, DumpOptions);
  671. }
  672. if (PrologueErr) {
  673. // Ensure there is a blank line after the prologue to clearly delineate it
  674. // from later dumps.
  675. if (OS)
  676. *OS << "\n";
  677. return PrologueErr;
  678. }
  679. uint64_t ProgramLength = Prologue.TotalLength + Prologue.sizeofTotalLength();
  680. if (!DebugLineData.isValidOffsetForDataOfSize(DebugLineOffset,
  681. ProgramLength)) {
  682. assert(DebugLineData.size() > DebugLineOffset &&
  683. "prologue parsing should handle invalid offset");
  684. uint64_t BytesRemaining = DebugLineData.size() - DebugLineOffset;
  685. RecoverableErrorHandler(
  686. createStringError(errc::invalid_argument,
  687. "line table program with offset 0x%8.8" PRIx64
  688. " has length 0x%8.8" PRIx64 " but only 0x%8.8" PRIx64
  689. " bytes are available",
  690. DebugLineOffset, ProgramLength, BytesRemaining));
  691. // Continue by capping the length at the number of remaining bytes.
  692. ProgramLength = BytesRemaining;
  693. }
  694. // Create a DataExtractor which can only see the data up to the end of the
  695. // table, to prevent reading past the end.
  696. const uint64_t EndOffset = DebugLineOffset + ProgramLength;
  697. DWARFDataExtractor TableData(DebugLineData, EndOffset);
  698. // See if we should tell the data extractor the address size.
  699. if (TableData.getAddressSize() == 0)
  700. TableData.setAddressSize(Prologue.getAddressSize());
  701. else
  702. assert(Prologue.getAddressSize() == 0 ||
  703. Prologue.getAddressSize() == TableData.getAddressSize());
  704. ParsingState State(this, DebugLineOffset, RecoverableErrorHandler);
  705. *OffsetPtr = DebugLineOffset + Prologue.getLength();
  706. if (OS && *OffsetPtr < EndOffset) {
  707. *OS << '\n';
  708. Row::dumpTableHeader(*OS, /*Indent=*/Verbose ? 12 : 0);
  709. }
  710. bool TombstonedAddress = false;
  711. auto EmitRow = [&] {
  712. if (!TombstonedAddress) {
  713. if (Verbose) {
  714. *OS << "\n";
  715. OS->indent(12);
  716. }
  717. if (OS)
  718. State.Row.dump(*OS);
  719. State.appendRowToMatrix();
  720. }
  721. };
  722. while (*OffsetPtr < EndOffset) {
  723. DataExtractor::Cursor Cursor(*OffsetPtr);
  724. if (Verbose)
  725. *OS << format("0x%08.08" PRIx64 ": ", *OffsetPtr);
  726. uint64_t OpcodeOffset = *OffsetPtr;
  727. uint8_t Opcode = TableData.getU8(Cursor);
  728. size_t RowCount = Rows.size();
  729. if (Cursor && Verbose)
  730. *OS << format("%02.02" PRIx8 " ", Opcode);
  731. if (Opcode == 0) {
  732. // Extended Opcodes always start with a zero opcode followed by
  733. // a uleb128 length so you can skip ones you don't know about
  734. uint64_t Len = TableData.getULEB128(Cursor);
  735. uint64_t ExtOffset = Cursor.tell();
  736. // Tolerate zero-length; assume length is correct and soldier on.
  737. if (Len == 0) {
  738. if (Cursor && Verbose)
  739. *OS << "Badly formed extended line op (length 0)\n";
  740. if (!Cursor) {
  741. if (Verbose)
  742. *OS << "\n";
  743. RecoverableErrorHandler(Cursor.takeError());
  744. }
  745. *OffsetPtr = Cursor.tell();
  746. continue;
  747. }
  748. uint8_t SubOpcode = TableData.getU8(Cursor);
  749. // OperandOffset will be the same as ExtOffset, if it was not possible to
  750. // read the SubOpcode.
  751. uint64_t OperandOffset = Cursor.tell();
  752. if (Verbose)
  753. *OS << LNExtendedString(SubOpcode);
  754. switch (SubOpcode) {
  755. case DW_LNE_end_sequence:
  756. // Set the end_sequence register of the state machine to true and
  757. // append a row to the matrix using the current values of the
  758. // state-machine registers. Then reset the registers to the initial
  759. // values specified above. Every statement program sequence must end
  760. // with a DW_LNE_end_sequence instruction which creates a row whose
  761. // address is that of the byte after the last target machine instruction
  762. // of the sequence.
  763. State.Row.EndSequence = true;
  764. // No need to test the Cursor is valid here, since it must be to get
  765. // into this code path - if it were invalid, the default case would be
  766. // followed.
  767. EmitRow();
  768. State.resetRowAndSequence();
  769. break;
  770. case DW_LNE_set_address:
  771. // Takes a single relocatable address as an operand. The size of the
  772. // operand is the size appropriate to hold an address on the target
  773. // machine. Set the address register to the value given by the
  774. // relocatable address. All of the other statement program opcodes
  775. // that affect the address register add a delta to it. This instruction
  776. // stores a relocatable value into it instead.
  777. //
  778. // Make sure the extractor knows the address size. If not, infer it
  779. // from the size of the operand.
  780. {
  781. uint8_t ExtractorAddressSize = TableData.getAddressSize();
  782. uint64_t OpcodeAddressSize = Len - 1;
  783. if (ExtractorAddressSize != OpcodeAddressSize &&
  784. ExtractorAddressSize != 0)
  785. RecoverableErrorHandler(createStringError(
  786. errc::invalid_argument,
  787. "mismatching address size at offset 0x%8.8" PRIx64
  788. " expected 0x%2.2" PRIx8 " found 0x%2.2" PRIx64,
  789. ExtOffset, ExtractorAddressSize, Len - 1));
  790. // Assume that the line table is correct and temporarily override the
  791. // address size. If the size is unsupported, give up trying to read
  792. // the address and continue to the next opcode.
  793. if (OpcodeAddressSize != 1 && OpcodeAddressSize != 2 &&
  794. OpcodeAddressSize != 4 && OpcodeAddressSize != 8) {
  795. RecoverableErrorHandler(createStringError(
  796. errc::invalid_argument,
  797. "address size 0x%2.2" PRIx64
  798. " of DW_LNE_set_address opcode at offset 0x%8.8" PRIx64
  799. " is unsupported",
  800. OpcodeAddressSize, ExtOffset));
  801. TableData.skip(Cursor, OpcodeAddressSize);
  802. } else {
  803. TableData.setAddressSize(OpcodeAddressSize);
  804. State.Row.Address.Address = TableData.getRelocatedAddress(
  805. Cursor, &State.Row.Address.SectionIndex);
  806. uint64_t Tombstone =
  807. dwarf::computeTombstoneAddress(OpcodeAddressSize);
  808. TombstonedAddress = State.Row.Address.Address == Tombstone;
  809. // Restore the address size if the extractor already had it.
  810. if (ExtractorAddressSize != 0)
  811. TableData.setAddressSize(ExtractorAddressSize);
  812. }
  813. if (Cursor && Verbose) {
  814. *OS << " (";
  815. DWARFFormValue::dumpAddress(*OS, OpcodeAddressSize, State.Row.Address.Address);
  816. *OS << ')';
  817. }
  818. }
  819. break;
  820. case DW_LNE_define_file:
  821. // Takes 4 arguments. The first is a null terminated string containing
  822. // a source file name. The second is an unsigned LEB128 number
  823. // representing the directory index of the directory in which the file
  824. // was found. The third is an unsigned LEB128 number representing the
  825. // time of last modification of the file. The fourth is an unsigned
  826. // LEB128 number representing the length in bytes of the file. The time
  827. // and length fields may contain LEB128(0) if the information is not
  828. // available.
  829. //
  830. // The directory index represents an entry in the include_directories
  831. // section of the statement program prologue. The index is LEB128(0)
  832. // if the file was found in the current directory of the compilation,
  833. // LEB128(1) if it was found in the first directory in the
  834. // include_directories section, and so on. The directory index is
  835. // ignored for file names that represent full path names.
  836. //
  837. // The files are numbered, starting at 1, in the order in which they
  838. // appear; the names in the prologue come before names defined by
  839. // the DW_LNE_define_file instruction. These numbers are used in the
  840. // the file register of the state machine.
  841. {
  842. FileNameEntry FileEntry;
  843. const char *Name = TableData.getCStr(Cursor);
  844. FileEntry.Name =
  845. DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, Name);
  846. FileEntry.DirIdx = TableData.getULEB128(Cursor);
  847. FileEntry.ModTime = TableData.getULEB128(Cursor);
  848. FileEntry.Length = TableData.getULEB128(Cursor);
  849. Prologue.FileNames.push_back(FileEntry);
  850. if (Cursor && Verbose)
  851. *OS << " (" << Name << ", dir=" << FileEntry.DirIdx << ", mod_time="
  852. << format("(0x%16.16" PRIx64 ")", FileEntry.ModTime)
  853. << ", length=" << FileEntry.Length << ")";
  854. }
  855. break;
  856. case DW_LNE_set_discriminator:
  857. State.Row.Discriminator = TableData.getULEB128(Cursor);
  858. if (Cursor && Verbose)
  859. *OS << " (" << State.Row.Discriminator << ")";
  860. break;
  861. default:
  862. if (Cursor && Verbose)
  863. *OS << format("Unrecognized extended op 0x%02.02" PRIx8, SubOpcode)
  864. << format(" length %" PRIx64, Len);
  865. // Len doesn't include the zero opcode byte or the length itself, but
  866. // it does include the sub_opcode, so we have to adjust for that.
  867. TableData.skip(Cursor, Len - 1);
  868. break;
  869. }
  870. // Make sure the length as recorded in the table and the standard length
  871. // for the opcode match. If they don't, continue from the end as claimed
  872. // by the table. Similarly, continue from the claimed end in the event of
  873. // a parsing error.
  874. uint64_t End = ExtOffset + Len;
  875. if (Cursor && Cursor.tell() != End)
  876. RecoverableErrorHandler(createStringError(
  877. errc::illegal_byte_sequence,
  878. "unexpected line op length at offset 0x%8.8" PRIx64
  879. " expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx64,
  880. ExtOffset, Len, Cursor.tell() - ExtOffset));
  881. if (!Cursor && Verbose) {
  882. DWARFDataExtractor::Cursor ByteCursor(OperandOffset);
  883. uint8_t Byte = TableData.getU8(ByteCursor);
  884. if (ByteCursor) {
  885. *OS << " (<parsing error>";
  886. do {
  887. *OS << format(" %2.2" PRIx8, Byte);
  888. Byte = TableData.getU8(ByteCursor);
  889. } while (ByteCursor);
  890. *OS << ")";
  891. }
  892. // The only parse failure in this case should be if the end was reached.
  893. // In that case, throw away the error, as the main Cursor's error will
  894. // be sufficient.
  895. consumeError(ByteCursor.takeError());
  896. }
  897. *OffsetPtr = End;
  898. } else if (Opcode < Prologue.OpcodeBase) {
  899. if (Verbose)
  900. *OS << LNStandardString(Opcode);
  901. switch (Opcode) {
  902. // Standard Opcodes
  903. case DW_LNS_copy:
  904. // Takes no arguments. Append a row to the matrix using the
  905. // current values of the state-machine registers.
  906. EmitRow();
  907. break;
  908. case DW_LNS_advance_pc:
  909. // Takes a single unsigned LEB128 operand, multiplies it by the
  910. // min_inst_length field of the prologue, and adds the
  911. // result to the address register of the state machine.
  912. if (Optional<uint64_t> Operand =
  913. parseULEB128<uint64_t>(TableData, Cursor)) {
  914. uint64_t AddrOffset =
  915. State.advanceAddr(*Operand, Opcode, OpcodeOffset);
  916. if (Verbose)
  917. *OS << " (" << AddrOffset << ")";
  918. }
  919. break;
  920. case DW_LNS_advance_line:
  921. // Takes a single signed LEB128 operand and adds that value to
  922. // the line register of the state machine.
  923. {
  924. int64_t LineDelta = TableData.getSLEB128(Cursor);
  925. if (Cursor) {
  926. State.Row.Line += LineDelta;
  927. if (Verbose)
  928. *OS << " (" << State.Row.Line << ")";
  929. }
  930. }
  931. break;
  932. case DW_LNS_set_file:
  933. // Takes a single unsigned LEB128 operand and stores it in the file
  934. // register of the state machine.
  935. if (Optional<uint16_t> File =
  936. parseULEB128<uint16_t>(TableData, Cursor)) {
  937. State.Row.File = *File;
  938. if (Verbose)
  939. *OS << " (" << State.Row.File << ")";
  940. }
  941. break;
  942. case DW_LNS_set_column:
  943. // Takes a single unsigned LEB128 operand and stores it in the
  944. // column register of the state machine.
  945. if (Optional<uint16_t> Column =
  946. parseULEB128<uint16_t>(TableData, Cursor)) {
  947. State.Row.Column = *Column;
  948. if (Verbose)
  949. *OS << " (" << State.Row.Column << ")";
  950. }
  951. break;
  952. case DW_LNS_negate_stmt:
  953. // Takes no arguments. Set the is_stmt register of the state
  954. // machine to the logical negation of its current value.
  955. State.Row.IsStmt = !State.Row.IsStmt;
  956. break;
  957. case DW_LNS_set_basic_block:
  958. // Takes no arguments. Set the basic_block register of the
  959. // state machine to true
  960. State.Row.BasicBlock = true;
  961. break;
  962. case DW_LNS_const_add_pc:
  963. // Takes no arguments. Add to the address register of the state
  964. // machine the address increment value corresponding to special
  965. // opcode 255. The motivation for DW_LNS_const_add_pc is this:
  966. // when the statement program needs to advance the address by a
  967. // small amount, it can use a single special opcode, which occupies
  968. // a single byte. When it needs to advance the address by up to
  969. // twice the range of the last special opcode, it can use
  970. // DW_LNS_const_add_pc followed by a special opcode, for a total
  971. // of two bytes. Only if it needs to advance the address by more
  972. // than twice that range will it need to use both DW_LNS_advance_pc
  973. // and a special opcode, requiring three or more bytes.
  974. {
  975. uint64_t AddrOffset =
  976. State.advanceAddrForOpcode(Opcode, OpcodeOffset).AddrDelta;
  977. if (Verbose)
  978. *OS << format(" (0x%16.16" PRIx64 ")", AddrOffset);
  979. }
  980. break;
  981. case DW_LNS_fixed_advance_pc:
  982. // Takes a single uhalf operand. Add to the address register of
  983. // the state machine the value of the (unencoded) operand. This
  984. // is the only extended opcode that takes an argument that is not
  985. // a variable length number. The motivation for DW_LNS_fixed_advance_pc
  986. // is this: existing assemblers cannot emit DW_LNS_advance_pc or
  987. // special opcodes because they cannot encode LEB128 numbers or
  988. // judge when the computation of a special opcode overflows and
  989. // requires the use of DW_LNS_advance_pc. Such assemblers, however,
  990. // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
  991. {
  992. uint16_t PCOffset =
  993. TableData.getRelocatedValue(Cursor, 2);
  994. if (Cursor) {
  995. State.Row.Address.Address += PCOffset;
  996. if (Verbose)
  997. *OS << format(" (0x%4.4" PRIx16 ")", PCOffset);
  998. }
  999. }
  1000. break;
  1001. case DW_LNS_set_prologue_end:
  1002. // Takes no arguments. Set the prologue_end register of the
  1003. // state machine to true
  1004. State.Row.PrologueEnd = true;
  1005. break;
  1006. case DW_LNS_set_epilogue_begin:
  1007. // Takes no arguments. Set the basic_block register of the
  1008. // state machine to true
  1009. State.Row.EpilogueBegin = true;
  1010. break;
  1011. case DW_LNS_set_isa:
  1012. // Takes a single unsigned LEB128 operand and stores it in the
  1013. // ISA register of the state machine.
  1014. if (Optional<uint8_t> Isa = parseULEB128<uint8_t>(TableData, Cursor)) {
  1015. State.Row.Isa = *Isa;
  1016. if (Verbose)
  1017. *OS << " (" << (uint64_t)State.Row.Isa << ")";
  1018. }
  1019. break;
  1020. default:
  1021. // Handle any unknown standard opcodes here. We know the lengths
  1022. // of such opcodes because they are specified in the prologue
  1023. // as a multiple of LEB128 operands for each opcode.
  1024. {
  1025. assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size());
  1026. if (Verbose)
  1027. *OS << "Unrecognized standard opcode";
  1028. uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1];
  1029. std::vector<uint64_t> Operands;
  1030. for (uint8_t I = 0; I < OpcodeLength; ++I) {
  1031. if (Optional<uint64_t> Value =
  1032. parseULEB128<uint64_t>(TableData, Cursor))
  1033. Operands.push_back(*Value);
  1034. else
  1035. break;
  1036. }
  1037. if (Verbose && !Operands.empty()) {
  1038. *OS << " (operands: ";
  1039. bool First = true;
  1040. for (uint64_t Value : Operands) {
  1041. if (!First)
  1042. *OS << ", ";
  1043. First = false;
  1044. *OS << format("0x%16.16" PRIx64, Value);
  1045. }
  1046. if (Verbose)
  1047. *OS << ')';
  1048. }
  1049. }
  1050. break;
  1051. }
  1052. *OffsetPtr = Cursor.tell();
  1053. } else {
  1054. // Special Opcodes.
  1055. ParsingState::AddrAndLineDelta Delta =
  1056. State.handleSpecialOpcode(Opcode, OpcodeOffset);
  1057. if (Verbose)
  1058. *OS << "address += " << Delta.Address << ", line += " << Delta.Line;
  1059. EmitRow();
  1060. *OffsetPtr = Cursor.tell();
  1061. }
  1062. // When a row is added to the matrix, it is also dumped, which includes a
  1063. // new line already, so don't add an extra one.
  1064. if (Verbose && Rows.size() == RowCount)
  1065. *OS << "\n";
  1066. // Most parse failures other than when parsing extended opcodes are due to
  1067. // failures to read ULEBs. Bail out of parsing, since we don't know where to
  1068. // continue reading from as there is no stated length for such byte
  1069. // sequences. Print the final trailing new line if needed before doing so.
  1070. if (!Cursor && Opcode != 0) {
  1071. if (Verbose)
  1072. *OS << "\n";
  1073. return Cursor.takeError();
  1074. }
  1075. if (!Cursor)
  1076. RecoverableErrorHandler(Cursor.takeError());
  1077. }
  1078. if (!State.Sequence.Empty)
  1079. RecoverableErrorHandler(createStringError(
  1080. errc::illegal_byte_sequence,
  1081. "last sequence in debug line table at offset 0x%8.8" PRIx64
  1082. " is not terminated",
  1083. DebugLineOffset));
  1084. // Sort all sequences so that address lookup will work faster.
  1085. if (!Sequences.empty()) {
  1086. llvm::sort(Sequences, Sequence::orderByHighPC);
  1087. // Note: actually, instruction address ranges of sequences should not
  1088. // overlap (in shared objects and executables). If they do, the address
  1089. // lookup would still work, though, but result would be ambiguous.
  1090. // We don't report warning in this case. For example,
  1091. // sometimes .so compiled from multiple object files contains a few
  1092. // rudimentary sequences for address ranges [0x0, 0xsomething).
  1093. }
  1094. // Terminate the table with a final blank line to clearly delineate it from
  1095. // later dumps.
  1096. if (OS)
  1097. *OS << "\n";
  1098. return Error::success();
  1099. }
  1100. uint32_t DWARFDebugLine::LineTable::findRowInSeq(
  1101. const DWARFDebugLine::Sequence &Seq,
  1102. object::SectionedAddress Address) const {
  1103. if (!Seq.containsPC(Address))
  1104. return UnknownRowIndex;
  1105. assert(Seq.SectionIndex == Address.SectionIndex);
  1106. // In some cases, e.g. first instruction in a function, the compiler generates
  1107. // two entries, both with the same address. We want the last one.
  1108. //
  1109. // In general we want a non-empty range: the last row whose address is less
  1110. // than or equal to Address. This can be computed as upper_bound - 1.
  1111. DWARFDebugLine::Row Row;
  1112. Row.Address = Address;
  1113. RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex;
  1114. RowIter LastRow = Rows.begin() + Seq.LastRowIndex;
  1115. assert(FirstRow->Address.Address <= Row.Address.Address &&
  1116. Row.Address.Address < LastRow[-1].Address.Address);
  1117. RowIter RowPos = std::upper_bound(FirstRow + 1, LastRow - 1, Row,
  1118. DWARFDebugLine::Row::orderByAddress) -
  1119. 1;
  1120. assert(Seq.SectionIndex == RowPos->Address.SectionIndex);
  1121. return RowPos - Rows.begin();
  1122. }
  1123. uint32_t DWARFDebugLine::LineTable::lookupAddress(
  1124. object::SectionedAddress Address) const {
  1125. // Search for relocatable addresses
  1126. uint32_t Result = lookupAddressImpl(Address);
  1127. if (Result != UnknownRowIndex ||
  1128. Address.SectionIndex == object::SectionedAddress::UndefSection)
  1129. return Result;
  1130. // Search for absolute addresses
  1131. Address.SectionIndex = object::SectionedAddress::UndefSection;
  1132. return lookupAddressImpl(Address);
  1133. }
  1134. uint32_t DWARFDebugLine::LineTable::lookupAddressImpl(
  1135. object::SectionedAddress Address) const {
  1136. // First, find an instruction sequence containing the given address.
  1137. DWARFDebugLine::Sequence Sequence;
  1138. Sequence.SectionIndex = Address.SectionIndex;
  1139. Sequence.HighPC = Address.Address;
  1140. SequenceIter It = llvm::upper_bound(Sequences, Sequence,
  1141. DWARFDebugLine::Sequence::orderByHighPC);
  1142. if (It == Sequences.end() || It->SectionIndex != Address.SectionIndex)
  1143. return UnknownRowIndex;
  1144. return findRowInSeq(*It, Address);
  1145. }
  1146. bool DWARFDebugLine::LineTable::lookupAddressRange(
  1147. object::SectionedAddress Address, uint64_t Size,
  1148. std::vector<uint32_t> &Result) const {
  1149. // Search for relocatable addresses
  1150. if (lookupAddressRangeImpl(Address, Size, Result))
  1151. return true;
  1152. if (Address.SectionIndex == object::SectionedAddress::UndefSection)
  1153. return false;
  1154. // Search for absolute addresses
  1155. Address.SectionIndex = object::SectionedAddress::UndefSection;
  1156. return lookupAddressRangeImpl(Address, Size, Result);
  1157. }
  1158. bool DWARFDebugLine::LineTable::lookupAddressRangeImpl(
  1159. object::SectionedAddress Address, uint64_t Size,
  1160. std::vector<uint32_t> &Result) const {
  1161. if (Sequences.empty())
  1162. return false;
  1163. uint64_t EndAddr = Address.Address + Size;
  1164. // First, find an instruction sequence containing the given address.
  1165. DWARFDebugLine::Sequence Sequence;
  1166. Sequence.SectionIndex = Address.SectionIndex;
  1167. Sequence.HighPC = Address.Address;
  1168. SequenceIter LastSeq = Sequences.end();
  1169. SequenceIter SeqPos = llvm::upper_bound(
  1170. Sequences, Sequence, DWARFDebugLine::Sequence::orderByHighPC);
  1171. if (SeqPos == LastSeq || !SeqPos->containsPC(Address))
  1172. return false;
  1173. SequenceIter StartPos = SeqPos;
  1174. // Add the rows from the first sequence to the vector, starting with the
  1175. // index we just calculated
  1176. while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) {
  1177. const DWARFDebugLine::Sequence &CurSeq = *SeqPos;
  1178. // For the first sequence, we need to find which row in the sequence is the
  1179. // first in our range.
  1180. uint32_t FirstRowIndex = CurSeq.FirstRowIndex;
  1181. if (SeqPos == StartPos)
  1182. FirstRowIndex = findRowInSeq(CurSeq, Address);
  1183. // Figure out the last row in the range.
  1184. uint32_t LastRowIndex =
  1185. findRowInSeq(CurSeq, {EndAddr - 1, Address.SectionIndex});
  1186. if (LastRowIndex == UnknownRowIndex)
  1187. LastRowIndex = CurSeq.LastRowIndex - 1;
  1188. assert(FirstRowIndex != UnknownRowIndex);
  1189. assert(LastRowIndex != UnknownRowIndex);
  1190. for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) {
  1191. Result.push_back(I);
  1192. }
  1193. ++SeqPos;
  1194. }
  1195. return true;
  1196. }
  1197. Optional<StringRef> DWARFDebugLine::LineTable::getSourceByIndex(uint64_t FileIndex,
  1198. FileLineInfoKind Kind) const {
  1199. if (Kind == FileLineInfoKind::None || !Prologue.hasFileAtIndex(FileIndex))
  1200. return None;
  1201. const FileNameEntry &Entry = Prologue.getFileNameEntry(FileIndex);
  1202. if (auto E = dwarf::toString(Entry.Source))
  1203. return StringRef(*E);
  1204. return None;
  1205. }
  1206. static bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {
  1207. // Debug info can contain paths from any OS, not necessarily
  1208. // an OS we're currently running on. Moreover different compilation units can
  1209. // be compiled on different operating systems and linked together later.
  1210. return sys::path::is_absolute(Path, sys::path::Style::posix) ||
  1211. sys::path::is_absolute(Path, sys::path::Style::windows);
  1212. }
  1213. bool DWARFDebugLine::Prologue::getFileNameByIndex(
  1214. uint64_t FileIndex, StringRef CompDir, FileLineInfoKind Kind,
  1215. std::string &Result, sys::path::Style Style) const {
  1216. if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
  1217. return false;
  1218. const FileNameEntry &Entry = getFileNameEntry(FileIndex);
  1219. auto E = dwarf::toString(Entry.Name);
  1220. if (!E)
  1221. return false;
  1222. StringRef FileName = *E;
  1223. if (Kind == FileLineInfoKind::RawValue ||
  1224. isPathAbsoluteOnWindowsOrPosix(FileName)) {
  1225. Result = std::string(FileName);
  1226. return true;
  1227. }
  1228. if (Kind == FileLineInfoKind::BaseNameOnly) {
  1229. Result = std::string(llvm::sys::path::filename(FileName));
  1230. return true;
  1231. }
  1232. SmallString<16> FilePath;
  1233. StringRef IncludeDir;
  1234. // Be defensive about the contents of Entry.
  1235. if (getVersion() >= 5) {
  1236. // DirIdx 0 is the compilation directory, so don't include it for
  1237. // relative names.
  1238. if ((Entry.DirIdx != 0 || Kind != FileLineInfoKind::RelativeFilePath) &&
  1239. Entry.DirIdx < IncludeDirectories.size())
  1240. IncludeDir = dwarf::toStringRef(IncludeDirectories[Entry.DirIdx]);
  1241. } else {
  1242. if (0 < Entry.DirIdx && Entry.DirIdx <= IncludeDirectories.size())
  1243. IncludeDir = dwarf::toStringRef(IncludeDirectories[Entry.DirIdx - 1]);
  1244. }
  1245. // For absolute paths only, include the compilation directory of compile unit.
  1246. // We know that FileName is not absolute, the only way to have an absolute
  1247. // path at this point would be if IncludeDir is absolute.
  1248. if (Kind == FileLineInfoKind::AbsoluteFilePath && !CompDir.empty() &&
  1249. !isPathAbsoluteOnWindowsOrPosix(IncludeDir))
  1250. sys::path::append(FilePath, Style, CompDir);
  1251. assert((Kind == FileLineInfoKind::AbsoluteFilePath ||
  1252. Kind == FileLineInfoKind::RelativeFilePath) &&
  1253. "invalid FileLineInfo Kind");
  1254. // sys::path::append skips empty strings.
  1255. sys::path::append(FilePath, Style, IncludeDir, FileName);
  1256. Result = std::string(FilePath.str());
  1257. return true;
  1258. }
  1259. bool DWARFDebugLine::LineTable::getFileLineInfoForAddress(
  1260. object::SectionedAddress Address, const char *CompDir,
  1261. FileLineInfoKind Kind, DILineInfo &Result) const {
  1262. // Get the index of row we're looking for in the line table.
  1263. uint32_t RowIndex = lookupAddress(Address);
  1264. if (RowIndex == -1U)
  1265. return false;
  1266. // Take file number and line/column from the row.
  1267. const auto &Row = Rows[RowIndex];
  1268. if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName))
  1269. return false;
  1270. Result.Line = Row.Line;
  1271. Result.Column = Row.Column;
  1272. Result.Discriminator = Row.Discriminator;
  1273. Result.Source = getSourceByIndex(Row.File, Kind);
  1274. return true;
  1275. }
  1276. // We want to supply the Unit associated with a .debug_line[.dwo] table when
  1277. // we dump it, if possible, but still dump the table even if there isn't a Unit.
  1278. // Therefore, collect up handles on all the Units that point into the
  1279. // line-table section.
  1280. static DWARFDebugLine::SectionParser::LineToUnitMap
  1281. buildLineToUnitMap(DWARFUnitVector::iterator_range Units) {
  1282. DWARFDebugLine::SectionParser::LineToUnitMap LineToUnit;
  1283. for (const auto &U : Units)
  1284. if (auto CUDIE = U->getUnitDIE())
  1285. if (auto StmtOffset = toSectionOffset(CUDIE.find(DW_AT_stmt_list)))
  1286. LineToUnit.insert(std::make_pair(*StmtOffset, &*U));
  1287. return LineToUnit;
  1288. }
  1289. DWARFDebugLine::SectionParser::SectionParser(
  1290. DWARFDataExtractor &Data, const DWARFContext &C,
  1291. DWARFUnitVector::iterator_range Units)
  1292. : DebugLineData(Data), Context(C) {
  1293. LineToUnit = buildLineToUnitMap(Units);
  1294. if (!DebugLineData.isValidOffset(Offset))
  1295. Done = true;
  1296. }
  1297. bool DWARFDebugLine::Prologue::totalLengthIsValid() const {
  1298. return TotalLength != 0u;
  1299. }
  1300. DWARFDebugLine::LineTable DWARFDebugLine::SectionParser::parseNext(
  1301. function_ref<void(Error)> RecoverableErrorHandler,
  1302. function_ref<void(Error)> UnrecoverableErrorHandler, raw_ostream *OS,
  1303. bool Verbose) {
  1304. assert(DebugLineData.isValidOffset(Offset) &&
  1305. "parsing should have terminated");
  1306. DWARFUnit *U = prepareToParse(Offset);
  1307. uint64_t OldOffset = Offset;
  1308. LineTable LT;
  1309. if (Error Err = LT.parse(DebugLineData, &Offset, Context, U,
  1310. RecoverableErrorHandler, OS, Verbose))
  1311. UnrecoverableErrorHandler(std::move(Err));
  1312. moveToNextTable(OldOffset, LT.Prologue);
  1313. return LT;
  1314. }
  1315. void DWARFDebugLine::SectionParser::skip(
  1316. function_ref<void(Error)> RecoverableErrorHandler,
  1317. function_ref<void(Error)> UnrecoverableErrorHandler) {
  1318. assert(DebugLineData.isValidOffset(Offset) &&
  1319. "parsing should have terminated");
  1320. DWARFUnit *U = prepareToParse(Offset);
  1321. uint64_t OldOffset = Offset;
  1322. LineTable LT;
  1323. if (Error Err = LT.Prologue.parse(DebugLineData, &Offset,
  1324. RecoverableErrorHandler, Context, U))
  1325. UnrecoverableErrorHandler(std::move(Err));
  1326. moveToNextTable(OldOffset, LT.Prologue);
  1327. }
  1328. DWARFUnit *DWARFDebugLine::SectionParser::prepareToParse(uint64_t Offset) {
  1329. DWARFUnit *U = nullptr;
  1330. auto It = LineToUnit.find(Offset);
  1331. if (It != LineToUnit.end())
  1332. U = It->second;
  1333. DebugLineData.setAddressSize(U ? U->getAddressByteSize() : 0);
  1334. return U;
  1335. }
  1336. void DWARFDebugLine::SectionParser::moveToNextTable(uint64_t OldOffset,
  1337. const Prologue &P) {
  1338. // If the length field is not valid, we don't know where the next table is, so
  1339. // cannot continue to parse. Mark the parser as done, and leave the Offset
  1340. // value as it currently is. This will be the end of the bad length field.
  1341. if (!P.totalLengthIsValid()) {
  1342. Done = true;
  1343. return;
  1344. }
  1345. Offset = OldOffset + P.TotalLength + P.sizeofTotalLength();
  1346. if (!DebugLineData.isValidOffset(Offset)) {
  1347. Done = true;
  1348. }
  1349. }