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