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