a5dc56e65495f005726c85a5bce882f6355efac9
[oota-llvm.git] / tools / dsymutil / DebugMap.h
1 //===- tools/dsymutil/DebugMap.h - 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 ///
10 /// \file
11 ///
12 /// This file contains the class declaration of the DebugMap
13 /// entity. A DebugMap lists all the object files linked together to
14 /// produce an executable along with the linked address of all the
15 /// atoms used in these object files.
16 /// The DebugMap is an input to the DwarfLinker class that will
17 /// extract the Dwarf debug information from the referenced object
18 /// files and link their usefull debug info together.
19 ///
20 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H
22 #define LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H
23
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/StringMap.h"
26 #include "llvm/ADT/Triple.h"
27 #include "llvm/ADT/iterator_range.h"
28 #include "llvm/Object/ObjectFile.h"
29 #include "llvm/Support/ErrorOr.h"
30 #include "llvm/Support/Format.h"
31 #include "llvm/Support/Path.h"
32 #include "llvm/Support/YAMLTraits.h"
33 #include <vector>
34
35 namespace llvm {
36 class raw_ostream;
37
38 namespace dsymutil {
39 class DebugMapObject;
40
41 /// \brief The DebugMap object stores the list of object files to
42 /// query for debug information along with the mapping between the
43 /// symbols' addresses in the object file to their linked address in
44 /// the linked binary.
45 ///
46 /// A DebugMap producer could look like this:
47 /// DebugMap *DM = new DebugMap();
48 /// for (const auto &Obj: LinkedObjects) {
49 ///     DebugMapObject &DMO = DM->addDebugMapObject(Obj.getPath());
50 ///     for (const auto &Sym: Obj.getLinkedSymbols())
51 ///         DMO.addSymbol(Sym.getName(), Sym.getObjectFileAddress(),
52 ///                       Sym.getBinaryAddress());
53 /// }
54 ///
55 /// A DebugMap consumer can then use the map to link the debug
56 /// information. For example something along the lines of:
57 /// for (const auto &DMO: DM->objects()) {
58 ///     auto Obj = createBinary(DMO.getObjectFilename());
59 ///     for (auto &DIE: Obj.getDwarfDIEs()) {
60 ///         if (SymbolMapping *Sym = DMO.lookup(DIE.getName()))
61 ///             DIE.relocate(Sym->ObjectAddress, Sym->BinaryAddress);
62 ///         else
63 ///             DIE.discardSubtree();
64 ///     }
65 /// }
66 class DebugMap {
67   Triple BinaryTriple;
68   typedef std::vector<std::unique_ptr<DebugMapObject>> ObjectContainer;
69   ObjectContainer Objects;
70
71   /// For YAML IO support.
72   ///@{
73   friend yaml::MappingTraits<std::unique_ptr<DebugMap>>;
74   friend yaml::MappingTraits<DebugMap>;
75   DebugMap() = default;
76   ///@}
77 public:
78   DebugMap(const Triple &BinaryTriple) : BinaryTriple(BinaryTriple) {}
79
80   typedef ObjectContainer::const_iterator const_iterator;
81
82   iterator_range<const_iterator> objects() const {
83     return make_range(begin(), end());
84   }
85
86   const_iterator begin() const { return Objects.begin(); }
87
88   const_iterator end() const { return Objects.end(); }
89
90   /// This function adds an DebugMapObject to the list owned by this
91   /// debug map.
92   DebugMapObject &addDebugMapObject(StringRef ObjectFilePath);
93
94   const Triple &getTriple() const { return BinaryTriple; }
95
96   void print(raw_ostream &OS) const;
97
98 #ifndef NDEBUG
99   void dump() const;
100 #endif
101 };
102
103 /// \brief The DebugMapObject represents one object file described by
104 /// the DebugMap. It contains a list of mappings between addresses in
105 /// the object file and in the linked binary for all the linked atoms
106 /// in this object file.
107 class DebugMapObject {
108 public:
109   struct SymbolMapping {
110     yaml::Hex64 ObjectAddress;
111     yaml::Hex64 BinaryAddress;
112     yaml::Hex32 Size;
113     SymbolMapping(uint64_t ObjectAddress, uint64_t BinaryAddress, uint32_t Size)
114         : ObjectAddress(ObjectAddress), BinaryAddress(BinaryAddress),
115           Size(Size) {}
116     /// For YAML IO support
117     SymbolMapping() = default;
118   };
119
120   typedef StringMapEntry<SymbolMapping> DebugMapEntry;
121
122   /// \brief Adds a symbol mapping to this DebugMapObject.
123   /// \returns false if the symbol was already registered. The request
124   /// is discarded in this case.
125   bool addSymbol(llvm::StringRef SymName, uint64_t ObjectAddress,
126                  uint64_t LinkedAddress, uint32_t Size);
127
128   /// \brief Lookup a symbol mapping.
129   /// \returns null if the symbol isn't found.
130   const DebugMapEntry *lookupSymbol(StringRef SymbolName) const;
131
132   /// \brief Lookup an objectfile address.
133   /// \returns null if the address isn't found.
134   const DebugMapEntry *lookupObjectAddress(uint64_t Address) const;
135
136   llvm::StringRef getObjectFilename() const { return Filename; }
137
138   iterator_range<StringMap<SymbolMapping>::const_iterator> symbols() const {
139     return make_range(Symbols.begin(), Symbols.end());
140   }
141
142   void print(raw_ostream &OS) const;
143 #ifndef NDEBUG
144   void dump() const;
145 #endif
146 private:
147   friend class DebugMap;
148   /// DebugMapObjects can only be constructed by the owning DebugMap.
149   DebugMapObject(StringRef ObjectFilename);
150
151   std::string Filename;
152   StringMap<SymbolMapping> Symbols;
153   DenseMap<uint64_t, DebugMapEntry *> AddressToMapping;
154
155   /// For YAMLIO support.
156   ///@{
157   typedef std::pair<std::string, SymbolMapping> YAMLSymbolMapping;
158   friend yaml::MappingTraits<dsymutil::DebugMapObject>;
159   friend yaml::SequenceTraits<std::vector<std::unique_ptr<DebugMapObject>>>;
160   friend yaml::SequenceTraits<std::vector<YAMLSymbolMapping>>;
161   DebugMapObject() = default;
162  public:
163   DebugMapObject &operator=(DebugMapObject RHS) {
164     std::swap(Filename, RHS.Filename);
165     std::swap(Symbols, RHS.Symbols);
166     std::swap(AddressToMapping, RHS.AddressToMapping);
167     return *this;
168   }
169   DebugMapObject(DebugMapObject &&RHS) {
170     Filename = std::move(RHS.Filename);
171     Symbols = std::move(RHS.Symbols);
172     AddressToMapping = std::move(RHS.AddressToMapping);
173   }
174   ///@}
175 };
176 }
177 }
178
179 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::dsymutil::DebugMapObject::YAMLSymbolMapping)
180
181 namespace llvm {
182 namespace yaml {
183
184 using namespace llvm::dsymutil;
185
186 template <>
187 struct MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>> {
188
189   static void
190   mapping(IO &io, std::pair<std::string, DebugMapObject::SymbolMapping> &s) {
191     io.mapRequired("sym", s.first);
192     io.mapRequired("objAddr", s.second.ObjectAddress);
193     io.mapRequired("binAddr", s.second.BinaryAddress);
194     io.mapOptional("size", s.second.Size);
195   }
196
197   static const bool flow = true;
198 };
199
200 template <> struct MappingTraits<dsymutil::DebugMapObject> {
201   // Normalize/Denormalize between YAML and a DebugMapObject.
202   struct YamlDMO {
203     YamlDMO(IO &io) {}
204
205     YamlDMO(IO &io, dsymutil::DebugMapObject &Obj) {
206       Filename = Obj.Filename;
207       Entries.reserve(Obj.Symbols.size());
208       for (auto &Entry : Obj.Symbols)
209         Entries.push_back(std::make_pair(Entry.getKey(), Entry.getValue()));
210     }
211
212     dsymutil::DebugMapObject denormalize(IO &IO) {
213       void *Ctxt = IO.getContext();
214       StringRef PrependPath = *reinterpret_cast<StringRef*>(Ctxt);
215       SmallString<80> Path(PrependPath);
216       sys::path::append(Path, Filename);
217       dsymutil::DebugMapObject Res(Path);
218       for (auto &Entry : Entries) {
219         auto &Mapping = Entry.second;
220         Res.addSymbol(Entry.first, Mapping.ObjectAddress, Mapping.BinaryAddress,
221                       Mapping.Size);
222       }
223       return Res;
224     }
225
226     std::string Filename;
227     std::vector<dsymutil::DebugMapObject::YAMLSymbolMapping> Entries;
228   };
229
230   static void mapping(IO &io, dsymutil::DebugMapObject &DMO) {
231     MappingNormalization<YamlDMO, dsymutil::DebugMapObject> Norm(io, DMO);
232     io.mapRequired("filename", Norm->Filename);
233     io.mapRequired("symbols", Norm->Entries);
234   }
235 };
236
237 template <> struct ScalarTraits<Triple> {
238
239   static void output(const Triple &val, void *, llvm::raw_ostream &out) {
240     out << val.str();
241   }
242
243   static StringRef input(StringRef scalar, void *, Triple &value) {
244     value = Triple(scalar);
245     return StringRef();
246   }
247
248   static bool mustQuote(StringRef) { return true; }
249 };
250
251 template <>
252 struct SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>> {
253
254   static size_t
255   size(IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq) {
256     return seq.size();
257   }
258
259   static dsymutil::DebugMapObject &
260   element(IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
261           size_t index) {
262     if (index >= seq.size()) {
263       seq.resize(index + 1);
264       seq[index].reset(new dsymutil::DebugMapObject);
265     }
266     return *seq[index];
267   }
268 };
269
270 template <> struct MappingTraits<dsymutil::DebugMap> {
271   static void mapping(IO &io, dsymutil::DebugMap &DM) {
272     io.mapRequired("triple", DM.BinaryTriple);
273     io.mapOptional("objects", DM.Objects);
274   }
275 };
276
277  template <> struct MappingTraits<std::unique_ptr<dsymutil::DebugMap>> {
278   static void mapping(IO &io, std::unique_ptr<dsymutil::DebugMap> &DM) {
279     if (!DM)
280       DM.reset(new DebugMap());
281     io.mapRequired("triple", DM->BinaryTriple);
282     io.mapOptional("objects", DM->Objects);
283   }
284 };
285 }
286 }
287
288 #endif // LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H