DWARFDebugLine.cpp 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522
  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/SmallString.h"
  10. #include "llvm/ADT/SmallVector.h"
  11. #include "llvm/ADT/StringRef.h"
  12. #include "llvm/BinaryFormat/Dwarf.h"
  13. #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
  14. #include "llvm/DebugInfo/DWARF/DWARFDie.h"
  15. #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
  16. #include "llvm/Support/Errc.h"
  17. #include "llvm/Support/Format.h"
  18. #include "llvm/Support/FormatVariadic.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. #include <algorithm>
  21. #include <cassert>
  22. #include <cinttypes>
  23. #include <cstdint>
  24. #include <cstdio>
  25. #include <utility>
  26. using namespace llvm;
  27. using namespace dwarf;
  28. using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind;
  29. namespace {
  30. struct ContentDescriptor {
  31. dwarf::LineNumberEntryFormat Type;
  32. dwarf::Form Form;
  33. };
  34. using ContentDescriptors = SmallVector<ContentDescriptor, 4>;
  35. } // end anonymous namespace
  36. static bool versionIsSupported(uint16_t Version) {
  37. return Version >= 2 && Version <= 5;
  38. }
  39. void DWARFDebugLine::ContentTypeTracker::trackContentType(
  40. dwarf::LineNumberEntryFormat ContentType) {
  41. switch (ContentType) {
  42. case dwarf::DW_LNCT_timestamp:
  43. HasModTime = true;
  44. break;
  45. case dwarf::DW_LNCT_size:
  46. HasLength = true;
  47. break;
  48. case dwarf::DW_LNCT_MD5:
  49. HasMD5 = true;
  50. break;
  51. case dwarf::DW_LNCT_LLVM_source:
  52. HasSource = true;
  53. break;
  54. default:
  55. // We only care about values we consider optional, and new values may be
  56. // added in the vendor extension range, so we do not match exhaustively.
  57. break;
  58. }
  59. }
  60. DWARFDebugLine::Prologue::Prologue() { clear(); }
  61. bool DWARFDebugLine::Prologue::hasFileAtIndex(uint64_t FileIndex) const {
  62. uint16_t DwarfVersion = getVersion();
  63. assert(DwarfVersion != 0 &&
  64. "line table prologue has no dwarf version information");
  65. if (DwarfVersion >= 5)
  66. return FileIndex < FileNames.size();
  67. return FileIndex != 0 && FileIndex <= FileNames.size();
  68. }
  69. std::optional<uint64_t>
  70. DWARFDebugLine::Prologue::getLastValidFileIndex() const {
  71. if (FileNames.empty())
  72. return std::nullopt;
  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();
  296. break;
  297. case DW_LNCT_timestamp:
  298. FileEntry.ModTime = *Value.getAsUnsignedConstant();
  299. break;
  300. case DW_LNCT_size:
  301. FileEntry.Length = *Value.getAsUnsignedConstant();
  302. break;
  303. case DW_LNCT_MD5:
  304. if (!Value.getAsBlock() || Value.getAsBlock()->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()->begin(), 16,
  309. FileEntry.Checksum.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. void DWARFDebugLine::clearLineTable(uint64_t Offset) {
  541. LineTableMap.erase(Offset);
  542. }
  543. static StringRef getOpcodeName(uint8_t Opcode, uint8_t OpcodeBase) {
  544. assert(Opcode != 0);
  545. if (Opcode < OpcodeBase)
  546. return LNStandardString(Opcode);
  547. return "special";
  548. }
  549. uint64_t DWARFDebugLine::ParsingState::advanceAddr(uint64_t OperationAdvance,
  550. uint8_t Opcode,
  551. uint64_t OpcodeOffset) {
  552. StringRef OpcodeName = getOpcodeName(Opcode, LineTable->Prologue.OpcodeBase);
  553. // For versions less than 4, the MaxOpsPerInst member is set to 0, as the
  554. // maximum_operations_per_instruction field wasn't introduced until DWARFv4.
  555. // Don't warn about bad values in this situation.
  556. if (ReportAdvanceAddrProblem && LineTable->Prologue.getVersion() >= 4 &&
  557. LineTable->Prologue.MaxOpsPerInst != 1)
  558. ErrorHandler(createStringError(
  559. errc::not_supported,
  560. "line table program at offset 0x%8.8" PRIx64
  561. " contains a %s opcode at offset 0x%8.8" PRIx64
  562. ", but the prologue maximum_operations_per_instruction value is %" PRId8
  563. ", which is unsupported. Assuming a value of 1 instead",
  564. LineTableOffset, OpcodeName.data(), OpcodeOffset,
  565. LineTable->Prologue.MaxOpsPerInst));
  566. if (ReportAdvanceAddrProblem && LineTable->Prologue.MinInstLength == 0)
  567. ErrorHandler(
  568. createStringError(errc::invalid_argument,
  569. "line table program at offset 0x%8.8" PRIx64
  570. " contains a %s opcode at offset 0x%8.8" PRIx64
  571. ", but the prologue minimum_instruction_length value "
  572. "is 0, which prevents any address advancing",
  573. LineTableOffset, OpcodeName.data(), OpcodeOffset));
  574. ReportAdvanceAddrProblem = false;
  575. uint64_t AddrOffset = OperationAdvance * LineTable->Prologue.MinInstLength;
  576. Row.Address.Address += AddrOffset;
  577. return AddrOffset;
  578. }
  579. DWARFDebugLine::ParsingState::AddrAndAdjustedOpcode
  580. DWARFDebugLine::ParsingState::advanceAddrForOpcode(uint8_t Opcode,
  581. uint64_t OpcodeOffset) {
  582. assert(Opcode == DW_LNS_const_add_pc ||
  583. Opcode >= LineTable->Prologue.OpcodeBase);
  584. if (ReportBadLineRange && LineTable->Prologue.LineRange == 0) {
  585. StringRef OpcodeName =
  586. getOpcodeName(Opcode, LineTable->Prologue.OpcodeBase);
  587. ErrorHandler(
  588. createStringError(errc::not_supported,
  589. "line table program at offset 0x%8.8" PRIx64
  590. " contains a %s opcode at offset 0x%8.8" PRIx64
  591. ", but the prologue line_range value is 0. The "
  592. "address and line will not be adjusted",
  593. LineTableOffset, OpcodeName.data(), OpcodeOffset));
  594. ReportBadLineRange = false;
  595. }
  596. uint8_t OpcodeValue = Opcode;
  597. if (Opcode == DW_LNS_const_add_pc)
  598. OpcodeValue = 255;
  599. uint8_t AdjustedOpcode = OpcodeValue - LineTable->Prologue.OpcodeBase;
  600. uint64_t OperationAdvance =
  601. LineTable->Prologue.LineRange != 0
  602. ? AdjustedOpcode / LineTable->Prologue.LineRange
  603. : 0;
  604. uint64_t AddrOffset = advanceAddr(OperationAdvance, Opcode, OpcodeOffset);
  605. return {AddrOffset, AdjustedOpcode};
  606. }
  607. DWARFDebugLine::ParsingState::AddrAndLineDelta
  608. DWARFDebugLine::ParsingState::handleSpecialOpcode(uint8_t Opcode,
  609. uint64_t OpcodeOffset) {
  610. // A special opcode value is chosen based on the amount that needs
  611. // to be added to the line and address registers. The maximum line
  612. // increment for a special opcode is the value of the line_base
  613. // field in the header, plus the value of the line_range field,
  614. // minus 1 (line base + line range - 1). If the desired line
  615. // increment is greater than the maximum line increment, a standard
  616. // opcode must be used instead of a special opcode. The "address
  617. // advance" is calculated by dividing the desired address increment
  618. // by the minimum_instruction_length field from the header. The
  619. // special opcode is then calculated using the following formula:
  620. //
  621. // opcode = (desired line increment - line_base) +
  622. // (line_range * address advance) + opcode_base
  623. //
  624. // If the resulting opcode is greater than 255, a standard opcode
  625. // must be used instead.
  626. //
  627. // To decode a special opcode, subtract the opcode_base from the
  628. // opcode itself to give the adjusted opcode. The amount to
  629. // increment the address register is the result of the adjusted
  630. // opcode divided by the line_range multiplied by the
  631. // minimum_instruction_length field from the header. That is:
  632. //
  633. // address increment = (adjusted opcode / line_range) *
  634. // minimum_instruction_length
  635. //
  636. // The amount to increment the line register is the line_base plus
  637. // the result of the adjusted opcode modulo the line_range. That is:
  638. //
  639. // line increment = line_base + (adjusted opcode % line_range)
  640. DWARFDebugLine::ParsingState::AddrAndAdjustedOpcode AddrAdvanceResult =
  641. advanceAddrForOpcode(Opcode, OpcodeOffset);
  642. int32_t LineOffset = 0;
  643. if (LineTable->Prologue.LineRange != 0)
  644. LineOffset =
  645. LineTable->Prologue.LineBase +
  646. (AddrAdvanceResult.AdjustedOpcode % LineTable->Prologue.LineRange);
  647. Row.Line += LineOffset;
  648. return {AddrAdvanceResult.AddrDelta, LineOffset};
  649. }
  650. /// Parse a ULEB128 using the specified \p Cursor. \returns the parsed value on
  651. /// success, or std::nullopt if \p Cursor is in a failing state.
  652. template <typename T>
  653. static std::optional<T> parseULEB128(DWARFDataExtractor &Data,
  654. DataExtractor::Cursor &Cursor) {
  655. T Value = Data.getULEB128(Cursor);
  656. if (Cursor)
  657. return Value;
  658. return std::nullopt;
  659. }
  660. Error DWARFDebugLine::LineTable::parse(
  661. DWARFDataExtractor &DebugLineData, uint64_t *OffsetPtr,
  662. const DWARFContext &Ctx, const DWARFUnit *U,
  663. function_ref<void(Error)> RecoverableErrorHandler, raw_ostream *OS,
  664. bool Verbose) {
  665. assert((OS || !Verbose) && "cannot have verbose output without stream");
  666. const uint64_t DebugLineOffset = *OffsetPtr;
  667. clear();
  668. Error PrologueErr =
  669. Prologue.parse(DebugLineData, OffsetPtr, RecoverableErrorHandler, Ctx, U);
  670. if (OS) {
  671. DIDumpOptions DumpOptions;
  672. DumpOptions.Verbose = Verbose;
  673. Prologue.dump(*OS, DumpOptions);
  674. }
  675. if (PrologueErr) {
  676. // Ensure there is a blank line after the prologue to clearly delineate it
  677. // from later dumps.
  678. if (OS)
  679. *OS << "\n";
  680. return PrologueErr;
  681. }
  682. uint64_t ProgramLength = Prologue.TotalLength + Prologue.sizeofTotalLength();
  683. if (!DebugLineData.isValidOffsetForDataOfSize(DebugLineOffset,
  684. ProgramLength)) {
  685. assert(DebugLineData.size() > DebugLineOffset &&
  686. "prologue parsing should handle invalid offset");
  687. uint64_t BytesRemaining = DebugLineData.size() - DebugLineOffset;
  688. RecoverableErrorHandler(
  689. createStringError(errc::invalid_argument,
  690. "line table program with offset 0x%8.8" PRIx64
  691. " has length 0x%8.8" PRIx64 " but only 0x%8.8" PRIx64
  692. " bytes are available",
  693. DebugLineOffset, ProgramLength, BytesRemaining));
  694. // Continue by capping the length at the number of remaining bytes.
  695. ProgramLength = BytesRemaining;
  696. }
  697. // Create a DataExtractor which can only see the data up to the end of the
  698. // table, to prevent reading past the end.
  699. const uint64_t EndOffset = DebugLineOffset + ProgramLength;
  700. DWARFDataExtractor TableData(DebugLineData, EndOffset);
  701. // See if we should tell the data extractor the address size.
  702. if (TableData.getAddressSize() == 0)
  703. TableData.setAddressSize(Prologue.getAddressSize());
  704. else
  705. assert(Prologue.getAddressSize() == 0 ||
  706. Prologue.getAddressSize() == TableData.getAddressSize());
  707. ParsingState State(this, DebugLineOffset, RecoverableErrorHandler);
  708. *OffsetPtr = DebugLineOffset + Prologue.getLength();
  709. if (OS && *OffsetPtr < EndOffset) {
  710. *OS << '\n';
  711. Row::dumpTableHeader(*OS, /*Indent=*/Verbose ? 12 : 0);
  712. }
  713. bool TombstonedAddress = false;
  714. auto EmitRow = [&] {
  715. if (!TombstonedAddress) {
  716. if (Verbose) {
  717. *OS << "\n";
  718. OS->indent(12);
  719. }
  720. if (OS)
  721. State.Row.dump(*OS);
  722. State.appendRowToMatrix();
  723. }
  724. };
  725. while (*OffsetPtr < EndOffset) {
  726. DataExtractor::Cursor Cursor(*OffsetPtr);
  727. if (Verbose)
  728. *OS << format("0x%08.08" PRIx64 ": ", *OffsetPtr);
  729. uint64_t OpcodeOffset = *OffsetPtr;
  730. uint8_t Opcode = TableData.getU8(Cursor);
  731. size_t RowCount = Rows.size();
  732. if (Cursor && Verbose)
  733. *OS << format("%02.02" PRIx8 " ", Opcode);
  734. if (Opcode == 0) {
  735. // Extended Opcodes always start with a zero opcode followed by
  736. // a uleb128 length so you can skip ones you don't know about
  737. uint64_t Len = TableData.getULEB128(Cursor);
  738. uint64_t ExtOffset = Cursor.tell();
  739. // Tolerate zero-length; assume length is correct and soldier on.
  740. if (Len == 0) {
  741. if (Cursor && Verbose)
  742. *OS << "Badly formed extended line op (length 0)\n";
  743. if (!Cursor) {
  744. if (Verbose)
  745. *OS << "\n";
  746. RecoverableErrorHandler(Cursor.takeError());
  747. }
  748. *OffsetPtr = Cursor.tell();
  749. continue;
  750. }
  751. uint8_t SubOpcode = TableData.getU8(Cursor);
  752. // OperandOffset will be the same as ExtOffset, if it was not possible to
  753. // read the SubOpcode.
  754. uint64_t OperandOffset = Cursor.tell();
  755. if (Verbose)
  756. *OS << LNExtendedString(SubOpcode);
  757. switch (SubOpcode) {
  758. case DW_LNE_end_sequence:
  759. // Set the end_sequence register of the state machine to true and
  760. // append a row to the matrix using the current values of the
  761. // state-machine registers. Then reset the registers to the initial
  762. // values specified above. Every statement program sequence must end
  763. // with a DW_LNE_end_sequence instruction which creates a row whose
  764. // address is that of the byte after the last target machine instruction
  765. // of the sequence.
  766. State.Row.EndSequence = true;
  767. // No need to test the Cursor is valid here, since it must be to get
  768. // into this code path - if it were invalid, the default case would be
  769. // followed.
  770. EmitRow();
  771. State.resetRowAndSequence();
  772. break;
  773. case DW_LNE_set_address:
  774. // Takes a single relocatable address as an operand. The size of the
  775. // operand is the size appropriate to hold an address on the target
  776. // machine. Set the address register to the value given by the
  777. // relocatable address. All of the other statement program opcodes
  778. // that affect the address register add a delta to it. This instruction
  779. // stores a relocatable value into it instead.
  780. //
  781. // Make sure the extractor knows the address size. If not, infer it
  782. // from the size of the operand.
  783. {
  784. uint8_t ExtractorAddressSize = TableData.getAddressSize();
  785. uint64_t OpcodeAddressSize = Len - 1;
  786. if (ExtractorAddressSize != OpcodeAddressSize &&
  787. ExtractorAddressSize != 0)
  788. RecoverableErrorHandler(createStringError(
  789. errc::invalid_argument,
  790. "mismatching address size at offset 0x%8.8" PRIx64
  791. " expected 0x%2.2" PRIx8 " found 0x%2.2" PRIx64,
  792. ExtOffset, ExtractorAddressSize, Len - 1));
  793. // Assume that the line table is correct and temporarily override the
  794. // address size. If the size is unsupported, give up trying to read
  795. // the address and continue to the next opcode.
  796. if (OpcodeAddressSize != 1 && OpcodeAddressSize != 2 &&
  797. OpcodeAddressSize != 4 && OpcodeAddressSize != 8) {
  798. RecoverableErrorHandler(createStringError(
  799. errc::invalid_argument,
  800. "address size 0x%2.2" PRIx64
  801. " of DW_LNE_set_address opcode at offset 0x%8.8" PRIx64
  802. " is unsupported",
  803. OpcodeAddressSize, ExtOffset));
  804. TableData.skip(Cursor, OpcodeAddressSize);
  805. } else {
  806. TableData.setAddressSize(OpcodeAddressSize);
  807. State.Row.Address.Address = TableData.getRelocatedAddress(
  808. Cursor, &State.Row.Address.SectionIndex);
  809. uint64_t Tombstone =
  810. dwarf::computeTombstoneAddress(OpcodeAddressSize);
  811. TombstonedAddress = State.Row.Address.Address == Tombstone;
  812. // Restore the address size if the extractor already had it.
  813. if (ExtractorAddressSize != 0)
  814. TableData.setAddressSize(ExtractorAddressSize);
  815. }
  816. if (Cursor && Verbose) {
  817. *OS << " (";
  818. DWARFFormValue::dumpAddress(*OS, OpcodeAddressSize, State.Row.Address.Address);
  819. *OS << ')';
  820. }
  821. }
  822. break;
  823. case DW_LNE_define_file:
  824. // Takes 4 arguments. The first is a null terminated string containing
  825. // a source file name. The second is an unsigned LEB128 number
  826. // representing the directory index of the directory in which the file
  827. // was found. The third is an unsigned LEB128 number representing the
  828. // time of last modification of the file. The fourth is an unsigned
  829. // LEB128 number representing the length in bytes of the file. The time
  830. // and length fields may contain LEB128(0) if the information is not
  831. // available.
  832. //
  833. // The directory index represents an entry in the include_directories
  834. // section of the statement program prologue. The index is LEB128(0)
  835. // if the file was found in the current directory of the compilation,
  836. // LEB128(1) if it was found in the first directory in the
  837. // include_directories section, and so on. The directory index is
  838. // ignored for file names that represent full path names.
  839. //
  840. // The files are numbered, starting at 1, in the order in which they
  841. // appear; the names in the prologue come before names defined by
  842. // the DW_LNE_define_file instruction. These numbers are used in the
  843. // the file register of the state machine.
  844. {
  845. FileNameEntry FileEntry;
  846. const char *Name = TableData.getCStr(Cursor);
  847. FileEntry.Name =
  848. DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, Name);
  849. FileEntry.DirIdx = TableData.getULEB128(Cursor);
  850. FileEntry.ModTime = TableData.getULEB128(Cursor);
  851. FileEntry.Length = TableData.getULEB128(Cursor);
  852. Prologue.FileNames.push_back(FileEntry);
  853. if (Cursor && Verbose)
  854. *OS << " (" << Name << ", dir=" << FileEntry.DirIdx << ", mod_time="
  855. << format("(0x%16.16" PRIx64 ")", FileEntry.ModTime)
  856. << ", length=" << FileEntry.Length << ")";
  857. }
  858. break;
  859. case DW_LNE_set_discriminator:
  860. State.Row.Discriminator = TableData.getULEB128(Cursor);
  861. if (Cursor && Verbose)
  862. *OS << " (" << State.Row.Discriminator << ")";
  863. break;
  864. default:
  865. if (Cursor && Verbose)
  866. *OS << format("Unrecognized extended op 0x%02.02" PRIx8, SubOpcode)
  867. << format(" length %" PRIx64, Len);
  868. // Len doesn't include the zero opcode byte or the length itself, but
  869. // it does include the sub_opcode, so we have to adjust for that.
  870. TableData.skip(Cursor, Len - 1);
  871. break;
  872. }
  873. // Make sure the length as recorded in the table and the standard length
  874. // for the opcode match. If they don't, continue from the end as claimed
  875. // by the table. Similarly, continue from the claimed end in the event of
  876. // a parsing error.
  877. uint64_t End = ExtOffset + Len;
  878. if (Cursor && Cursor.tell() != End)
  879. RecoverableErrorHandler(createStringError(
  880. errc::illegal_byte_sequence,
  881. "unexpected line op length at offset 0x%8.8" PRIx64
  882. " expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx64,
  883. ExtOffset, Len, Cursor.tell() - ExtOffset));
  884. if (!Cursor && Verbose) {
  885. DWARFDataExtractor::Cursor ByteCursor(OperandOffset);
  886. uint8_t Byte = TableData.getU8(ByteCursor);
  887. if (ByteCursor) {
  888. *OS << " (<parsing error>";
  889. do {
  890. *OS << format(" %2.2" PRIx8, Byte);
  891. Byte = TableData.getU8(ByteCursor);
  892. } while (ByteCursor);
  893. *OS << ")";
  894. }
  895. // The only parse failure in this case should be if the end was reached.
  896. // In that case, throw away the error, as the main Cursor's error will
  897. // be sufficient.
  898. consumeError(ByteCursor.takeError());
  899. }
  900. *OffsetPtr = End;
  901. } else if (Opcode < Prologue.OpcodeBase) {
  902. if (Verbose)
  903. *OS << LNStandardString(Opcode);
  904. switch (Opcode) {
  905. // Standard Opcodes
  906. case DW_LNS_copy:
  907. // Takes no arguments. Append a row to the matrix using the
  908. // current values of the state-machine registers.
  909. EmitRow();
  910. break;
  911. case DW_LNS_advance_pc:
  912. // Takes a single unsigned LEB128 operand, multiplies it by the
  913. // min_inst_length field of the prologue, and adds the
  914. // result to the address register of the state machine.
  915. if (std::optional<uint64_t> Operand =
  916. parseULEB128<uint64_t>(TableData, Cursor)) {
  917. uint64_t AddrOffset =
  918. State.advanceAddr(*Operand, Opcode, OpcodeOffset);
  919. if (Verbose)
  920. *OS << " (" << AddrOffset << ")";
  921. }
  922. break;
  923. case DW_LNS_advance_line:
  924. // Takes a single signed LEB128 operand and adds that value to
  925. // the line register of the state machine.
  926. {
  927. int64_t LineDelta = TableData.getSLEB128(Cursor);
  928. if (Cursor) {
  929. State.Row.Line += LineDelta;
  930. if (Verbose)
  931. *OS << " (" << State.Row.Line << ")";
  932. }
  933. }
  934. break;
  935. case DW_LNS_set_file:
  936. // Takes a single unsigned LEB128 operand and stores it in the file
  937. // register of the state machine.
  938. if (std::optional<uint16_t> File =
  939. parseULEB128<uint16_t>(TableData, Cursor)) {
  940. State.Row.File = *File;
  941. if (Verbose)
  942. *OS << " (" << State.Row.File << ")";
  943. }
  944. break;
  945. case DW_LNS_set_column:
  946. // Takes a single unsigned LEB128 operand and stores it in the
  947. // column register of the state machine.
  948. if (std::optional<uint16_t> Column =
  949. parseULEB128<uint16_t>(TableData, Cursor)) {
  950. State.Row.Column = *Column;
  951. if (Verbose)
  952. *OS << " (" << State.Row.Column << ")";
  953. }
  954. break;
  955. case DW_LNS_negate_stmt:
  956. // Takes no arguments. Set the is_stmt register of the state
  957. // machine to the logical negation of its current value.
  958. State.Row.IsStmt = !State.Row.IsStmt;
  959. break;
  960. case DW_LNS_set_basic_block:
  961. // Takes no arguments. Set the basic_block register of the
  962. // state machine to true
  963. State.Row.BasicBlock = true;
  964. break;
  965. case DW_LNS_const_add_pc:
  966. // Takes no arguments. Add to the address register of the state
  967. // machine the address increment value corresponding to special
  968. // opcode 255. The motivation for DW_LNS_const_add_pc is this:
  969. // when the statement program needs to advance the address by a
  970. // small amount, it can use a single special opcode, which occupies
  971. // a single byte. When it needs to advance the address by up to
  972. // twice the range of the last special opcode, it can use
  973. // DW_LNS_const_add_pc followed by a special opcode, for a total
  974. // of two bytes. Only if it needs to advance the address by more
  975. // than twice that range will it need to use both DW_LNS_advance_pc
  976. // and a special opcode, requiring three or more bytes.
  977. {
  978. uint64_t AddrOffset =
  979. State.advanceAddrForOpcode(Opcode, OpcodeOffset).AddrDelta;
  980. if (Verbose)
  981. *OS << format(" (0x%16.16" PRIx64 ")", AddrOffset);
  982. }
  983. break;
  984. case DW_LNS_fixed_advance_pc:
  985. // Takes a single uhalf operand. Add to the address register of
  986. // the state machine the value of the (unencoded) operand. This
  987. // is the only extended opcode that takes an argument that is not
  988. // a variable length number. The motivation for DW_LNS_fixed_advance_pc
  989. // is this: existing assemblers cannot emit DW_LNS_advance_pc or
  990. // special opcodes because they cannot encode LEB128 numbers or
  991. // judge when the computation of a special opcode overflows and
  992. // requires the use of DW_LNS_advance_pc. Such assemblers, however,
  993. // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
  994. {
  995. uint16_t PCOffset =
  996. TableData.getRelocatedValue(Cursor, 2);
  997. if (Cursor) {
  998. State.Row.Address.Address += PCOffset;
  999. if (Verbose)
  1000. *OS << format(" (0x%4.4" PRIx16 ")", PCOffset);
  1001. }
  1002. }
  1003. break;
  1004. case DW_LNS_set_prologue_end:
  1005. // Takes no arguments. Set the prologue_end register of the
  1006. // state machine to true
  1007. State.Row.PrologueEnd = true;
  1008. break;
  1009. case DW_LNS_set_epilogue_begin:
  1010. // Takes no arguments. Set the basic_block register of the
  1011. // state machine to true
  1012. State.Row.EpilogueBegin = true;
  1013. break;
  1014. case DW_LNS_set_isa:
  1015. // Takes a single unsigned LEB128 operand and stores it in the
  1016. // ISA register of the state machine.
  1017. if (std::optional<uint8_t> Isa =
  1018. parseULEB128<uint8_t>(TableData, Cursor)) {
  1019. State.Row.Isa = *Isa;
  1020. if (Verbose)
  1021. *OS << " (" << (uint64_t)State.Row.Isa << ")";
  1022. }
  1023. break;
  1024. default:
  1025. // Handle any unknown standard opcodes here. We know the lengths
  1026. // of such opcodes because they are specified in the prologue
  1027. // as a multiple of LEB128 operands for each opcode.
  1028. {
  1029. assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size());
  1030. if (Verbose)
  1031. *OS << "Unrecognized standard opcode";
  1032. uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1];
  1033. std::vector<uint64_t> Operands;
  1034. for (uint8_t I = 0; I < OpcodeLength; ++I) {
  1035. if (std::optional<uint64_t> Value =
  1036. parseULEB128<uint64_t>(TableData, Cursor))
  1037. Operands.push_back(*Value);
  1038. else
  1039. break;
  1040. }
  1041. if (Verbose && !Operands.empty()) {
  1042. *OS << " (operands: ";
  1043. bool First = true;
  1044. for (uint64_t Value : Operands) {
  1045. if (!First)
  1046. *OS << ", ";
  1047. First = false;
  1048. *OS << format("0x%16.16" PRIx64, Value);
  1049. }
  1050. if (Verbose)
  1051. *OS << ')';
  1052. }
  1053. }
  1054. break;
  1055. }
  1056. *OffsetPtr = Cursor.tell();
  1057. } else {
  1058. // Special Opcodes.
  1059. ParsingState::AddrAndLineDelta Delta =
  1060. State.handleSpecialOpcode(Opcode, OpcodeOffset);
  1061. if (Verbose)
  1062. *OS << "address += " << Delta.Address << ", line += " << Delta.Line;
  1063. EmitRow();
  1064. *OffsetPtr = Cursor.tell();
  1065. }
  1066. // When a row is added to the matrix, it is also dumped, which includes a
  1067. // new line already, so don't add an extra one.
  1068. if (Verbose && Rows.size() == RowCount)
  1069. *OS << "\n";
  1070. // Most parse failures other than when parsing extended opcodes are due to
  1071. // failures to read ULEBs. Bail out of parsing, since we don't know where to
  1072. // continue reading from as there is no stated length for such byte
  1073. // sequences. Print the final trailing new line if needed before doing so.
  1074. if (!Cursor && Opcode != 0) {
  1075. if (Verbose)
  1076. *OS << "\n";
  1077. return Cursor.takeError();
  1078. }
  1079. if (!Cursor)
  1080. RecoverableErrorHandler(Cursor.takeError());
  1081. }
  1082. if (!State.Sequence.Empty)
  1083. RecoverableErrorHandler(createStringError(
  1084. errc::illegal_byte_sequence,
  1085. "last sequence in debug line table at offset 0x%8.8" PRIx64
  1086. " is not terminated",
  1087. DebugLineOffset));
  1088. // Sort all sequences so that address lookup will work faster.
  1089. if (!Sequences.empty()) {
  1090. llvm::sort(Sequences, Sequence::orderByHighPC);
  1091. // Note: actually, instruction address ranges of sequences should not
  1092. // overlap (in shared objects and executables). If they do, the address
  1093. // lookup would still work, though, but result would be ambiguous.
  1094. // We don't report warning in this case. For example,
  1095. // sometimes .so compiled from multiple object files contains a few
  1096. // rudimentary sequences for address ranges [0x0, 0xsomething).
  1097. }
  1098. // Terminate the table with a final blank line to clearly delineate it from
  1099. // later dumps.
  1100. if (OS)
  1101. *OS << "\n";
  1102. return Error::success();
  1103. }
  1104. uint32_t DWARFDebugLine::LineTable::findRowInSeq(
  1105. const DWARFDebugLine::Sequence &Seq,
  1106. object::SectionedAddress Address) const {
  1107. if (!Seq.containsPC(Address))
  1108. return UnknownRowIndex;
  1109. assert(Seq.SectionIndex == Address.SectionIndex);
  1110. // In some cases, e.g. first instruction in a function, the compiler generates
  1111. // two entries, both with the same address. We want the last one.
  1112. //
  1113. // In general we want a non-empty range: the last row whose address is less
  1114. // than or equal to Address. This can be computed as upper_bound - 1.
  1115. DWARFDebugLine::Row Row;
  1116. Row.Address = Address;
  1117. RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex;
  1118. RowIter LastRow = Rows.begin() + Seq.LastRowIndex;
  1119. assert(FirstRow->Address.Address <= Row.Address.Address &&
  1120. Row.Address.Address < LastRow[-1].Address.Address);
  1121. RowIter RowPos = std::upper_bound(FirstRow + 1, LastRow - 1, Row,
  1122. DWARFDebugLine::Row::orderByAddress) -
  1123. 1;
  1124. assert(Seq.SectionIndex == RowPos->Address.SectionIndex);
  1125. return RowPos - Rows.begin();
  1126. }
  1127. uint32_t DWARFDebugLine::LineTable::lookupAddress(
  1128. object::SectionedAddress Address) const {
  1129. // Search for relocatable addresses
  1130. uint32_t Result = lookupAddressImpl(Address);
  1131. if (Result != UnknownRowIndex ||
  1132. Address.SectionIndex == object::SectionedAddress::UndefSection)
  1133. return Result;
  1134. // Search for absolute addresses
  1135. Address.SectionIndex = object::SectionedAddress::UndefSection;
  1136. return lookupAddressImpl(Address);
  1137. }
  1138. uint32_t DWARFDebugLine::LineTable::lookupAddressImpl(
  1139. object::SectionedAddress Address) const {
  1140. // First, find an instruction sequence containing the given address.
  1141. DWARFDebugLine::Sequence Sequence;
  1142. Sequence.SectionIndex = Address.SectionIndex;
  1143. Sequence.HighPC = Address.Address;
  1144. SequenceIter It = llvm::upper_bound(Sequences, Sequence,
  1145. DWARFDebugLine::Sequence::orderByHighPC);
  1146. if (It == Sequences.end() || It->SectionIndex != Address.SectionIndex)
  1147. return UnknownRowIndex;
  1148. return findRowInSeq(*It, Address);
  1149. }
  1150. bool DWARFDebugLine::LineTable::lookupAddressRange(
  1151. object::SectionedAddress Address, uint64_t Size,
  1152. std::vector<uint32_t> &Result) const {
  1153. // Search for relocatable addresses
  1154. if (lookupAddressRangeImpl(Address, Size, Result))
  1155. return true;
  1156. if (Address.SectionIndex == object::SectionedAddress::UndefSection)
  1157. return false;
  1158. // Search for absolute addresses
  1159. Address.SectionIndex = object::SectionedAddress::UndefSection;
  1160. return lookupAddressRangeImpl(Address, Size, Result);
  1161. }
  1162. bool DWARFDebugLine::LineTable::lookupAddressRangeImpl(
  1163. object::SectionedAddress Address, uint64_t Size,
  1164. std::vector<uint32_t> &Result) const {
  1165. if (Sequences.empty())
  1166. return false;
  1167. uint64_t EndAddr = Address.Address + Size;
  1168. // First, find an instruction sequence containing the given address.
  1169. DWARFDebugLine::Sequence Sequence;
  1170. Sequence.SectionIndex = Address.SectionIndex;
  1171. Sequence.HighPC = Address.Address;
  1172. SequenceIter LastSeq = Sequences.end();
  1173. SequenceIter SeqPos = llvm::upper_bound(
  1174. Sequences, Sequence, DWARFDebugLine::Sequence::orderByHighPC);
  1175. if (SeqPos == LastSeq || !SeqPos->containsPC(Address))
  1176. return false;
  1177. SequenceIter StartPos = SeqPos;
  1178. // Add the rows from the first sequence to the vector, starting with the
  1179. // index we just calculated
  1180. while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) {
  1181. const DWARFDebugLine::Sequence &CurSeq = *SeqPos;
  1182. // For the first sequence, we need to find which row in the sequence is the
  1183. // first in our range.
  1184. uint32_t FirstRowIndex = CurSeq.FirstRowIndex;
  1185. if (SeqPos == StartPos)
  1186. FirstRowIndex = findRowInSeq(CurSeq, Address);
  1187. // Figure out the last row in the range.
  1188. uint32_t LastRowIndex =
  1189. findRowInSeq(CurSeq, {EndAddr - 1, Address.SectionIndex});
  1190. if (LastRowIndex == UnknownRowIndex)
  1191. LastRowIndex = CurSeq.LastRowIndex - 1;
  1192. assert(FirstRowIndex != UnknownRowIndex);
  1193. assert(LastRowIndex != UnknownRowIndex);
  1194. for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) {
  1195. Result.push_back(I);
  1196. }
  1197. ++SeqPos;
  1198. }
  1199. return true;
  1200. }
  1201. std::optional<StringRef>
  1202. DWARFDebugLine::LineTable::getSourceByIndex(uint64_t FileIndex,
  1203. FileLineInfoKind Kind) const {
  1204. if (Kind == FileLineInfoKind::None || !Prologue.hasFileAtIndex(FileIndex))
  1205. return std::nullopt;
  1206. const FileNameEntry &Entry = Prologue.getFileNameEntry(FileIndex);
  1207. if (auto E = dwarf::toString(Entry.Source))
  1208. return StringRef(*E);
  1209. return std::nullopt;
  1210. }
  1211. static bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {
  1212. // Debug info can contain paths from any OS, not necessarily
  1213. // an OS we're currently running on. Moreover different compilation units can
  1214. // be compiled on different operating systems and linked together later.
  1215. return sys::path::is_absolute(Path, sys::path::Style::posix) ||
  1216. sys::path::is_absolute(Path, sys::path::Style::windows);
  1217. }
  1218. bool DWARFDebugLine::Prologue::getFileNameByIndex(
  1219. uint64_t FileIndex, StringRef CompDir, FileLineInfoKind Kind,
  1220. std::string &Result, sys::path::Style Style) const {
  1221. if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
  1222. return false;
  1223. const FileNameEntry &Entry = getFileNameEntry(FileIndex);
  1224. auto E = dwarf::toString(Entry.Name);
  1225. if (!E)
  1226. return false;
  1227. StringRef FileName = *E;
  1228. if (Kind == FileLineInfoKind::RawValue ||
  1229. isPathAbsoluteOnWindowsOrPosix(FileName)) {
  1230. Result = std::string(FileName);
  1231. return true;
  1232. }
  1233. if (Kind == FileLineInfoKind::BaseNameOnly) {
  1234. Result = std::string(llvm::sys::path::filename(FileName));
  1235. return true;
  1236. }
  1237. SmallString<16> FilePath;
  1238. StringRef IncludeDir;
  1239. // Be defensive about the contents of Entry.
  1240. if (getVersion() >= 5) {
  1241. // DirIdx 0 is the compilation directory, so don't include it for
  1242. // relative names.
  1243. if ((Entry.DirIdx != 0 || Kind != FileLineInfoKind::RelativeFilePath) &&
  1244. Entry.DirIdx < IncludeDirectories.size())
  1245. IncludeDir = dwarf::toStringRef(IncludeDirectories[Entry.DirIdx]);
  1246. } else {
  1247. if (0 < Entry.DirIdx && Entry.DirIdx <= IncludeDirectories.size())
  1248. IncludeDir = dwarf::toStringRef(IncludeDirectories[Entry.DirIdx - 1]);
  1249. }
  1250. // For absolute paths only, include the compilation directory of compile unit,
  1251. // unless v5 DirIdx == 0 (IncludeDir indicates the compilation directory). We
  1252. // know that FileName is not absolute, the only way to have an absolute path
  1253. // at this point would be if IncludeDir is absolute.
  1254. if (Kind == FileLineInfoKind::AbsoluteFilePath &&
  1255. (getVersion() < 5 || Entry.DirIdx != 0) && !CompDir.empty() &&
  1256. !isPathAbsoluteOnWindowsOrPosix(IncludeDir))
  1257. sys::path::append(FilePath, Style, CompDir);
  1258. assert((Kind == FileLineInfoKind::AbsoluteFilePath ||
  1259. Kind == FileLineInfoKind::RelativeFilePath) &&
  1260. "invalid FileLineInfo Kind");
  1261. // sys::path::append skips empty strings.
  1262. sys::path::append(FilePath, Style, IncludeDir, FileName);
  1263. Result = std::string(FilePath.str());
  1264. return true;
  1265. }
  1266. bool DWARFDebugLine::LineTable::getFileLineInfoForAddress(
  1267. object::SectionedAddress Address, const char *CompDir,
  1268. FileLineInfoKind Kind, DILineInfo &Result) const {
  1269. // Get the index of row we're looking for in the line table.
  1270. uint32_t RowIndex = lookupAddress(Address);
  1271. if (RowIndex == -1U)
  1272. return false;
  1273. // Take file number and line/column from the row.
  1274. const auto &Row = Rows[RowIndex];
  1275. if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName))
  1276. return false;
  1277. Result.Line = Row.Line;
  1278. Result.Column = Row.Column;
  1279. Result.Discriminator = Row.Discriminator;
  1280. Result.Source = getSourceByIndex(Row.File, Kind);
  1281. return true;
  1282. }
  1283. bool DWARFDebugLine::LineTable::getDirectoryForEntry(
  1284. const FileNameEntry &Entry, std::string &Directory) const {
  1285. if (Prologue.getVersion() >= 5) {
  1286. if (Entry.DirIdx < Prologue.IncludeDirectories.size()) {
  1287. Directory =
  1288. dwarf::toString(Prologue.IncludeDirectories[Entry.DirIdx], "");
  1289. return true;
  1290. }
  1291. return false;
  1292. }
  1293. if (0 < Entry.DirIdx && Entry.DirIdx <= Prologue.IncludeDirectories.size()) {
  1294. Directory =
  1295. dwarf::toString(Prologue.IncludeDirectories[Entry.DirIdx - 1], "");
  1296. return true;
  1297. }
  1298. return false;
  1299. }
  1300. // We want to supply the Unit associated with a .debug_line[.dwo] table when
  1301. // we dump it, if possible, but still dump the table even if there isn't a Unit.
  1302. // Therefore, collect up handles on all the Units that point into the
  1303. // line-table section.
  1304. static DWARFDebugLine::SectionParser::LineToUnitMap
  1305. buildLineToUnitMap(DWARFUnitVector::iterator_range Units) {
  1306. DWARFDebugLine::SectionParser::LineToUnitMap LineToUnit;
  1307. for (const auto &U : Units)
  1308. if (auto CUDIE = U->getUnitDIE())
  1309. if (auto StmtOffset = toSectionOffset(CUDIE.find(DW_AT_stmt_list)))
  1310. LineToUnit.insert(std::make_pair(*StmtOffset, &*U));
  1311. return LineToUnit;
  1312. }
  1313. DWARFDebugLine::SectionParser::SectionParser(
  1314. DWARFDataExtractor &Data, const DWARFContext &C,
  1315. DWARFUnitVector::iterator_range Units)
  1316. : DebugLineData(Data), Context(C) {
  1317. LineToUnit = buildLineToUnitMap(Units);
  1318. if (!DebugLineData.isValidOffset(Offset))
  1319. Done = true;
  1320. }
  1321. bool DWARFDebugLine::Prologue::totalLengthIsValid() const {
  1322. return TotalLength != 0u;
  1323. }
  1324. DWARFDebugLine::LineTable DWARFDebugLine::SectionParser::parseNext(
  1325. function_ref<void(Error)> RecoverableErrorHandler,
  1326. function_ref<void(Error)> UnrecoverableErrorHandler, raw_ostream *OS,
  1327. bool Verbose) {
  1328. assert(DebugLineData.isValidOffset(Offset) &&
  1329. "parsing should have terminated");
  1330. DWARFUnit *U = prepareToParse(Offset);
  1331. uint64_t OldOffset = Offset;
  1332. LineTable LT;
  1333. if (Error Err = LT.parse(DebugLineData, &Offset, Context, U,
  1334. RecoverableErrorHandler, OS, Verbose))
  1335. UnrecoverableErrorHandler(std::move(Err));
  1336. moveToNextTable(OldOffset, LT.Prologue);
  1337. return LT;
  1338. }
  1339. void DWARFDebugLine::SectionParser::skip(
  1340. function_ref<void(Error)> RecoverableErrorHandler,
  1341. function_ref<void(Error)> UnrecoverableErrorHandler) {
  1342. assert(DebugLineData.isValidOffset(Offset) &&
  1343. "parsing should have terminated");
  1344. DWARFUnit *U = prepareToParse(Offset);
  1345. uint64_t OldOffset = Offset;
  1346. LineTable LT;
  1347. if (Error Err = LT.Prologue.parse(DebugLineData, &Offset,
  1348. RecoverableErrorHandler, Context, U))
  1349. UnrecoverableErrorHandler(std::move(Err));
  1350. moveToNextTable(OldOffset, LT.Prologue);
  1351. }
  1352. DWARFUnit *DWARFDebugLine::SectionParser::prepareToParse(uint64_t Offset) {
  1353. DWARFUnit *U = nullptr;
  1354. auto It = LineToUnit.find(Offset);
  1355. if (It != LineToUnit.end())
  1356. U = It->second;
  1357. DebugLineData.setAddressSize(U ? U->getAddressByteSize() : 0);
  1358. return U;
  1359. }
  1360. void DWARFDebugLine::SectionParser::moveToNextTable(uint64_t OldOffset,
  1361. const Prologue &P) {
  1362. // If the length field is not valid, we don't know where the next table is, so
  1363. // cannot continue to parse. Mark the parser as done, and leave the Offset
  1364. // value as it currently is. This will be the end of the bad length field.
  1365. if (!P.totalLengthIsValid()) {
  1366. Done = true;
  1367. return;
  1368. }
  1369. Offset = OldOffset + P.TotalLength + P.sizeofTotalLength();
  1370. if (!DebugLineData.isValidOffset(Offset)) {
  1371. Done = true;
  1372. }
  1373. }