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