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