DWARF.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //===- DWARF.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 "lld/Common/DWARF.h"
  9. #include "lld/Common/ErrorHandler.h"
  10. using namespace llvm;
  11. namespace lld {
  12. DWARFCache::DWARFCache(std::unique_ptr<llvm::DWARFContext> d)
  13. : dwarf(std::move(d)) {
  14. for (std::unique_ptr<DWARFUnit> &cu : dwarf->compile_units()) {
  15. auto report = [](Error err) {
  16. handleAllErrors(std::move(err),
  17. [](ErrorInfoBase &info) { warn(info.message()); });
  18. };
  19. Expected<const DWARFDebugLine::LineTable *> expectedLT =
  20. dwarf->getLineTableForUnit(cu.get(), report);
  21. const DWARFDebugLine::LineTable *lt = nullptr;
  22. if (expectedLT)
  23. lt = *expectedLT;
  24. else
  25. report(expectedLT.takeError());
  26. if (!lt)
  27. continue;
  28. lineTables.push_back(lt);
  29. // Loop over variable records and insert them to variableLoc.
  30. for (const auto &entry : cu->dies()) {
  31. DWARFDie die(cu.get(), &entry);
  32. // Skip all tags that are not variables.
  33. if (die.getTag() != dwarf::DW_TAG_variable)
  34. continue;
  35. // Skip if a local variable because we don't need them for generating
  36. // error messages. In general, only non-local symbols can fail to be
  37. // linked.
  38. if (!dwarf::toUnsigned(die.find(dwarf::DW_AT_external), 0))
  39. continue;
  40. // Get the source filename index for the variable.
  41. unsigned file = dwarf::toUnsigned(die.find(dwarf::DW_AT_decl_file), 0);
  42. if (!lt->hasFileAtIndex(file))
  43. continue;
  44. // Get the line number on which the variable is declared.
  45. unsigned line = dwarf::toUnsigned(die.find(dwarf::DW_AT_decl_line), 0);
  46. // Here we want to take the variable name to add it into variableLoc.
  47. // Variable can have regular and linkage name associated. At first, we try
  48. // to get linkage name as it can be different, for example when we have
  49. // two variables in different namespaces of the same object. Use common
  50. // name otherwise, but handle the case when it also absent in case if the
  51. // input object file lacks some debug info.
  52. StringRef name =
  53. dwarf::toString(die.find(dwarf::DW_AT_linkage_name),
  54. dwarf::toString(die.find(dwarf::DW_AT_name), ""));
  55. if (!name.empty())
  56. variableLoc.insert({name, {lt, file, line}});
  57. }
  58. }
  59. }
  60. // Returns the pair of file name and line number describing location of data
  61. // object (variable, array, etc) definition.
  62. Optional<std::pair<std::string, unsigned>>
  63. DWARFCache::getVariableLoc(StringRef name) {
  64. // Return if we have no debug information about data object.
  65. auto it = variableLoc.find(name);
  66. if (it == variableLoc.end())
  67. return None;
  68. // Take file name string from line table.
  69. std::string fileName;
  70. if (!it->second.lt->getFileNameByIndex(
  71. it->second.file, {},
  72. DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, fileName))
  73. return None;
  74. return std::make_pair(fileName, it->second.line);
  75. }
  76. // Returns source line information for a given offset
  77. // using DWARF debug info.
  78. Optional<DILineInfo> DWARFCache::getDILineInfo(uint64_t offset,
  79. uint64_t sectionIndex) {
  80. DILineInfo info;
  81. for (const llvm::DWARFDebugLine::LineTable *lt : lineTables) {
  82. if (lt->getFileLineInfoForAddress(
  83. {offset, sectionIndex}, nullptr,
  84. DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, info))
  85. return info;
  86. }
  87. return None;
  88. }
  89. } // namespace lld