DebugMap.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //===- tools/dsymutil/DebugMap.cpp - Generic debug map representation -----===//
  2. //
  3. // The LLVM Linker
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "DebugMap.h"
  10. #include "BinaryHolder.h"
  11. #include "llvm/ADT/STLExtras.h"
  12. #include "llvm/ADT/iterator_range.h"
  13. #include "llvm/Support/DataTypes.h"
  14. #include "llvm/Support/Format.h"
  15. #include "llvm/Support/raw_ostream.h"
  16. #include <algorithm>
  17. // //
  18. ///////////////////////////////////////////////////////////////////////////////
  19. namespace llvm {
  20. namespace dsymutil {
  21. using namespace llvm::object;
  22. DebugMapObject::DebugMapObject(StringRef ObjectFilename)
  23. : Filename(ObjectFilename) {}
  24. bool DebugMapObject::addSymbol(StringRef Name, uint64_t ObjectAddress,
  25. uint64_t LinkedAddress, uint32_t Size) {
  26. auto InsertResult = Symbols.insert(
  27. std::make_pair(Name, SymbolMapping(ObjectAddress, LinkedAddress, Size)));
  28. if (InsertResult.second)
  29. AddressToMapping[ObjectAddress] = &*InsertResult.first;
  30. return InsertResult.second;
  31. }
  32. void DebugMapObject::print(raw_ostream &OS) const {
  33. OS << getObjectFilename() << ":\n";
  34. // Sort the symbols in alphabetical order, like llvm-nm (and to get
  35. // deterministic output for testing).
  36. typedef std::pair<StringRef, SymbolMapping> Entry;
  37. std::vector<Entry> Entries;
  38. Entries.reserve(Symbols.getNumItems());
  39. for (const auto &Sym : make_range(Symbols.begin(), Symbols.end()))
  40. Entries.push_back(std::make_pair(Sym.getKey(), Sym.getValue()));
  41. std::sort(
  42. Entries.begin(), Entries.end(),
  43. [](const Entry &LHS, const Entry &RHS) { return LHS.first < RHS.first; });
  44. for (const auto &Sym : Entries) {
  45. OS << format("\t%016" PRIx64 " => %016" PRIx64 "+0x%x\t%s\n",
  46. uint64_t(Sym.second.ObjectAddress),
  47. uint64_t(Sym.second.BinaryAddress), uint32_t(Sym.second.Size),
  48. Sym.first.data());
  49. }
  50. OS << '\n';
  51. }
  52. #ifndef NDEBUG
  53. void DebugMapObject::dump() const { print(errs()); }
  54. #endif
  55. DebugMapObject &DebugMap::addDebugMapObject(StringRef ObjectFilePath) {
  56. Objects.emplace_back(new DebugMapObject(ObjectFilePath));
  57. return *Objects.back();
  58. }
  59. const DebugMapObject::DebugMapEntry *
  60. DebugMapObject::lookupSymbol(StringRef SymbolName) const {
  61. StringMap<SymbolMapping>::const_iterator Sym = Symbols.find(SymbolName);
  62. if (Sym == Symbols.end())
  63. return nullptr;
  64. return &*Sym;
  65. }
  66. const DebugMapObject::DebugMapEntry *
  67. DebugMapObject::lookupObjectAddress(uint64_t Address) const {
  68. auto Mapping = AddressToMapping.find(Address);
  69. if (Mapping == AddressToMapping.end())
  70. return nullptr;
  71. return Mapping->getSecond();
  72. }
  73. void DebugMap::print(raw_ostream &OS) const {
  74. yaml::Output yout(OS, /* Ctxt = */ nullptr, /* WrapColumn = */ 0);
  75. yout << const_cast<DebugMap &>(*this);
  76. }
  77. #ifndef NDEBUG
  78. void DebugMap::dump() const { print(errs()); }
  79. #endif
  80. namespace {
  81. struct YAMLContext {
  82. StringRef PrependPath;
  83. Triple BinaryTriple;
  84. };
  85. }
  86. ErrorOr<std::unique_ptr<DebugMap>>
  87. DebugMap::parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath,
  88. bool Verbose) {
  89. auto ErrOrFile = MemoryBuffer::getFileOrSTDIN(InputFile);
  90. if (auto Err = ErrOrFile.getError())
  91. return Err;
  92. YAMLContext Ctxt;
  93. Ctxt.PrependPath = PrependPath;
  94. std::unique_ptr<DebugMap> Res;
  95. yaml::Input yin((*ErrOrFile)->getBuffer(), &Ctxt);
  96. yin >> Res;
  97. if (auto EC = yin.error())
  98. return EC;
  99. return std::move(Res);
  100. }
  101. }
  102. namespace yaml {
  103. // Normalize/Denormalize between YAML and a DebugMapObject.
  104. struct MappingTraits<dsymutil::DebugMapObject>::YamlDMO {
  105. YamlDMO(IO &io) {}
  106. YamlDMO(IO &io, dsymutil::DebugMapObject &Obj);
  107. dsymutil::DebugMapObject denormalize(IO &IO);
  108. std::string Filename;
  109. std::vector<dsymutil::DebugMapObject::YAMLSymbolMapping> Entries;
  110. };
  111. void MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>>::
  112. mapping(IO &io, std::pair<std::string, DebugMapObject::SymbolMapping> &s) {
  113. io.mapRequired("sym", s.first);
  114. io.mapRequired("objAddr", s.second.ObjectAddress);
  115. io.mapRequired("binAddr", s.second.BinaryAddress);
  116. io.mapOptional("size", s.second.Size);
  117. }
  118. void MappingTraits<dsymutil::DebugMapObject>::mapping(
  119. IO &io, dsymutil::DebugMapObject &DMO) {
  120. MappingNormalization<YamlDMO, dsymutil::DebugMapObject> Norm(io, DMO);
  121. io.mapRequired("filename", Norm->Filename);
  122. io.mapRequired("symbols", Norm->Entries);
  123. }
  124. void ScalarTraits<Triple>::output(const Triple &val, void *,
  125. llvm::raw_ostream &out) {
  126. out << val.str();
  127. }
  128. StringRef ScalarTraits<Triple>::input(StringRef scalar, void *, Triple &value) {
  129. value = Triple(scalar);
  130. return StringRef();
  131. }
  132. size_t
  133. SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::size(
  134. IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq) {
  135. return seq.size();
  136. }
  137. dsymutil::DebugMapObject &
  138. SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::element(
  139. IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
  140. size_t index) {
  141. if (index >= seq.size()) {
  142. seq.resize(index + 1);
  143. seq[index].reset(new dsymutil::DebugMapObject);
  144. }
  145. return *seq[index];
  146. }
  147. void MappingTraits<dsymutil::DebugMap>::mapping(IO &io,
  148. dsymutil::DebugMap &DM) {
  149. io.mapRequired("triple", DM.BinaryTriple);
  150. io.mapOptional("objects", DM.Objects);
  151. if (void *Ctxt = io.getContext())
  152. reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM.BinaryTriple;
  153. }
  154. void MappingTraits<std::unique_ptr<dsymutil::DebugMap>>::mapping(
  155. IO &io, std::unique_ptr<dsymutil::DebugMap> &DM) {
  156. if (!DM)
  157. DM.reset(new DebugMap());
  158. io.mapRequired("triple", DM->BinaryTriple);
  159. io.mapOptional("objects", DM->Objects);
  160. if (void *Ctxt = io.getContext())
  161. reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM->BinaryTriple;
  162. }
  163. MappingTraits<dsymutil::DebugMapObject>::YamlDMO::YamlDMO(
  164. IO &io, dsymutil::DebugMapObject &Obj) {
  165. Filename = Obj.Filename;
  166. Entries.reserve(Obj.Symbols.size());
  167. for (auto &Entry : Obj.Symbols)
  168. Entries.push_back(std::make_pair(Entry.getKey(), Entry.getValue()));
  169. }
  170. dsymutil::DebugMapObject
  171. MappingTraits<dsymutil::DebugMapObject>::YamlDMO::denormalize(IO &IO) {
  172. BinaryHolder BinHolder(/* Verbose =*/false);
  173. const auto &Ctxt = *reinterpret_cast<YAMLContext *>(IO.getContext());
  174. SmallString<80> Path(Ctxt.PrependPath);
  175. StringMap<uint64_t> SymbolAddresses;
  176. sys::path::append(Path, Filename);
  177. auto ErrOrObjectFile = BinHolder.GetObjectFile(Path);
  178. if (auto EC = ErrOrObjectFile.getError()) {
  179. llvm::errs() << "warning: Unable to open " << Path << " " << EC.message()
  180. << '\n';
  181. } else {
  182. // Rewrite the object file symbol addresses in the debug map. The
  183. // YAML input is mainly used to test llvm-dsymutil without
  184. // requiring binaries checked-in. If we generate the object files
  185. // during the test, we can't hardcode the symbols addresses, so
  186. // look them up here and rewrite them.
  187. for (const auto &Sym : ErrOrObjectFile->symbols()) {
  188. uint64_t Address = Sym.getValue();
  189. ErrorOr<StringRef> Name = Sym.getName();
  190. if (!Name)
  191. continue;
  192. SymbolAddresses[*Name] = Address;
  193. }
  194. }
  195. dsymutil::DebugMapObject Res(Path);
  196. for (auto &Entry : Entries) {
  197. auto &Mapping = Entry.second;
  198. uint64_t ObjAddress = Mapping.ObjectAddress;
  199. auto AddressIt = SymbolAddresses.find(Entry.first);
  200. if (AddressIt != SymbolAddresses.end())
  201. ObjAddress = AddressIt->getValue();
  202. Res.addSymbol(Entry.first, ObjAddress, Mapping.BinaryAddress, Mapping.Size);
  203. }
  204. return Res;
  205. }
  206. }
  207. }