[dsymutil] Add the detected target triple to the debug map.
[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/StringMap.h"
25 #include "llvm/ADT/Triple.h"
26 #include "llvm/ADT/iterator_range.h"
27 #include "llvm/Object/ObjectFile.h"
28 #include "llvm/Support/ErrorOr.h"
29 #include "llvm/Support/Format.h"
30 #include <vector>
31
32 namespace llvm {
33 class raw_ostream;
34
35 namespace dsymutil {
36 class DebugMapObject;
37
38 /// \brief The DebugMap object stores the list of object files to
39 /// query for debug information along with the mapping between the
40 /// symbols' addresses in the object file to their linked address in
41 /// the linked binary.
42 ///
43 /// A DebugMap producer could look like this:
44 /// DebugMap *DM = new DebugMap();
45 /// for (const auto &Obj: LinkedObjects) {
46 ///     DebugMapObject &DMO = DM->addDebugMapObject(Obj.getPath());
47 ///     for (const auto &Sym: Obj.getLinkedSymbols())
48 ///         DMO.addSymbol(Sym.getName(), Sym.getObjectFileAddress(),
49 ///                       Sym.getBinaryAddress());
50 /// }
51 ///
52 /// A DebugMap consumer can then use the map to link the debug
53 /// information. For example something along the lines of:
54 /// for (const auto &DMO: DM->objects()) {
55 ///     auto Obj = createBinary(DMO.getObjectFilename());
56 ///     for (auto &DIE: Obj.getDwarfDIEs()) {
57 ///         if (SymbolMapping *Sym = DMO.lookup(DIE.getName()))
58 ///             DIE.relocate(Sym->ObjectAddress, Sym->BinaryAddress);
59 ///         else
60 ///             DIE.discardSubtree();
61 ///     }
62 /// }
63 class DebugMap {
64   Triple BinaryTriple;
65   typedef std::vector<std::unique_ptr<DebugMapObject>> ObjectContainer;
66   ObjectContainer Objects;
67
68 public:
69   DebugMap(const Triple &BinaryTriple) : BinaryTriple(BinaryTriple) {}
70
71   typedef ObjectContainer::const_iterator const_iterator;
72
73   iterator_range<const_iterator> objects() const {
74     return make_range(begin(), end());
75   }
76
77   const_iterator begin() const { return Objects.begin(); }
78
79   const_iterator end() const { return Objects.end(); }
80
81   /// This function adds an DebugMapObject to the list owned by this
82   /// debug map.
83   DebugMapObject &addDebugMapObject(StringRef ObjectFilePath);
84
85   const Triple &getTriple() { return BinaryTriple; }
86
87   void print(raw_ostream &OS) const;
88
89 #ifndef NDEBUG
90   void dump() const;
91 #endif
92 };
93
94 /// \brief The DebugMapObject represents one object file described by
95 /// the DebugMap. It contains a list of mappings between addresses in
96 /// the object file and in the linked binary for all the linked atoms
97 /// in this object file.
98 class DebugMapObject {
99 public:
100   struct SymbolMapping {
101     uint64_t ObjectAddress;
102     uint64_t BinaryAddress;
103     SymbolMapping(uint64_t ObjectAddress, uint64_t BinaryAddress)
104         : ObjectAddress(ObjectAddress), BinaryAddress(BinaryAddress) {}
105   };
106
107   /// \brief Adds a symbol mapping to this DebugMapObject.
108   /// \returns false if the symbol was already registered. The request
109   /// is discarded in this case.
110   bool addSymbol(llvm::StringRef SymName, uint64_t ObjectAddress,
111                  uint64_t LinkedAddress);
112
113   /// \brief Lookup a symbol mapping.
114   /// \returns null if the symbol isn't found.
115   const SymbolMapping *lookupSymbol(StringRef SymbolName) const;
116
117   llvm::StringRef getObjectFilename() const { return Filename; }
118
119   void print(raw_ostream &OS) const;
120 #ifndef NDEBUG
121   void dump() const;
122 #endif
123 private:
124   friend class DebugMap;
125   /// DebugMapObjects can only be constructed by the owning DebugMap.
126   DebugMapObject(StringRef ObjectFilename);
127
128   std::string Filename;
129   StringMap<SymbolMapping> Symbols;
130 };
131 }
132 }
133
134 #endif // LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H