]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/dsymutil/DebugMap.h
Vendor import of llvm trunk r291274:
[FreeBSD/FreeBSD.git] / tools / dsymutil / DebugMap.h
1 //=== tools/dsymutil/DebugMap.h - Generic debug map representation -*- C++ -*-//
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/Chrono.h"
30 #include "llvm/Support/ErrorOr.h"
31 #include "llvm/Support/Format.h"
32 #include "llvm/Support/Path.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   std::string BinaryPath;
70   typedef std::vector<std::unique_ptr<DebugMapObject>> ObjectContainer;
71   ObjectContainer Objects;
72
73   /// For YAML IO support.
74   ///@{
75   friend yaml::MappingTraits<std::unique_ptr<DebugMap>>;
76   friend yaml::MappingTraits<DebugMap>;
77   DebugMap() = default;
78   ///@}
79 public:
80   DebugMap(const Triple &BinaryTriple, StringRef BinaryPath)
81       : BinaryTriple(BinaryTriple), BinaryPath(BinaryPath) {}
82
83   typedef ObjectContainer::const_iterator const_iterator;
84
85   iterator_range<const_iterator> objects() const {
86     return make_range(begin(), end());
87   }
88
89   const_iterator begin() const { return Objects.begin(); }
90
91   const_iterator end() const { return Objects.end(); }
92
93   /// This function adds an DebugMapObject to the list owned by this
94   /// debug map.
95   DebugMapObject &
96   addDebugMapObject(StringRef ObjectFilePath,
97                     sys::TimePoint<std::chrono::seconds> Timestamp);
98
99   const Triple &getTriple() const { return BinaryTriple; }
100
101   StringRef getBinaryPath() const { return BinaryPath; }
102
103   void print(raw_ostream &OS) const;
104
105 #ifndef NDEBUG
106   void dump() const;
107 #endif
108
109   /// Read a debug map for \a InputFile.
110   static ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
111   parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath, bool Verbose);
112 };
113
114 /// \brief The DebugMapObject represents one object file described by
115 /// the DebugMap. It contains a list of mappings between addresses in
116 /// the object file and in the linked binary for all the linked atoms
117 /// in this object file.
118 class DebugMapObject {
119 public:
120   struct SymbolMapping {
121     Optional<yaml::Hex64> ObjectAddress;
122     yaml::Hex64 BinaryAddress;
123     yaml::Hex32 Size;
124     SymbolMapping(Optional<uint64_t> ObjectAddr, uint64_t BinaryAddress,
125                   uint32_t Size)
126         : BinaryAddress(BinaryAddress), Size(Size) {
127       if (ObjectAddr)
128         ObjectAddress = *ObjectAddr;
129     }
130     /// For YAML IO support
131     SymbolMapping() = default;
132   };
133
134   typedef std::pair<std::string, SymbolMapping> YAMLSymbolMapping;
135   typedef StringMapEntry<SymbolMapping> DebugMapEntry;
136
137   /// \brief Adds a symbol mapping to this DebugMapObject.
138   /// \returns false if the symbol was already registered. The request
139   /// is discarded in this case.
140   bool addSymbol(llvm::StringRef SymName, Optional<uint64_t> ObjectAddress,
141                  uint64_t LinkedAddress, uint32_t Size);
142
143   /// \brief Lookup a symbol mapping.
144   /// \returns null if the symbol isn't found.
145   const DebugMapEntry *lookupSymbol(StringRef SymbolName) const;
146
147   /// \brief Lookup an objectfile address.
148   /// \returns null if the address isn't found.
149   const DebugMapEntry *lookupObjectAddress(uint64_t Address) const;
150
151   llvm::StringRef getObjectFilename() const { return Filename; }
152
153   sys::TimePoint<std::chrono::seconds> getTimestamp() const {
154     return Timestamp;
155   }
156
157   iterator_range<StringMap<SymbolMapping>::const_iterator> symbols() const {
158     return make_range(Symbols.begin(), Symbols.end());
159   }
160
161   void print(raw_ostream &OS) const;
162 #ifndef NDEBUG
163   void dump() const;
164 #endif
165 private:
166   friend class DebugMap;
167   /// DebugMapObjects can only be constructed by the owning DebugMap.
168   DebugMapObject(StringRef ObjectFilename,
169                  sys::TimePoint<std::chrono::seconds> Timestamp);
170
171   std::string Filename;
172   sys::TimePoint<std::chrono::seconds> Timestamp;
173   StringMap<SymbolMapping> Symbols;
174   DenseMap<uint64_t, DebugMapEntry *> AddressToMapping;
175
176   /// For YAMLIO support.
177   ///@{
178   friend yaml::MappingTraits<dsymutil::DebugMapObject>;
179   friend yaml::SequenceTraits<std::vector<std::unique_ptr<DebugMapObject>>>;
180   DebugMapObject() = default;
181
182 public:
183   DebugMapObject(DebugMapObject &&) = default;
184   DebugMapObject &operator=(DebugMapObject &&) = default;
185   ///@}
186 };
187 }
188 }
189
190 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::dsymutil::DebugMapObject::YAMLSymbolMapping)
191
192 namespace llvm {
193 namespace yaml {
194
195 using namespace llvm::dsymutil;
196
197 template <>
198 struct MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>> {
199   static void mapping(IO &io,
200                       std::pair<std::string, DebugMapObject::SymbolMapping> &s);
201   static const bool flow = true;
202 };
203
204 template <> struct MappingTraits<dsymutil::DebugMapObject> {
205   struct YamlDMO;
206   static void mapping(IO &io, dsymutil::DebugMapObject &DMO);
207 };
208
209 template <> struct ScalarTraits<Triple> {
210   static void output(const Triple &val, void *, llvm::raw_ostream &out);
211   static StringRef input(StringRef scalar, void *, Triple &value);
212   static bool mustQuote(StringRef) { return true; }
213 };
214
215 template <>
216 struct SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>> {
217   static size_t
218   size(IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq);
219   static dsymutil::DebugMapObject &
220   element(IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
221           size_t index);
222 };
223
224 template <> struct MappingTraits<dsymutil::DebugMap> {
225   static void mapping(IO &io, dsymutil::DebugMap &DM);
226 };
227
228 template <> struct MappingTraits<std::unique_ptr<dsymutil::DebugMap>> {
229   static void mapping(IO &io, std::unique_ptr<dsymutil::DebugMap> &DM);
230 };
231 }
232 }
233
234 #endif // LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H