1 //===- tools/dsymutil/DebugMap.h - Generic debug map representation -------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
20 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H
22 #define LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H
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"
39 /// \brief The DebugMap object stores the list of object files to
40 /// query for debug information along with the mapping between the
41 /// symbols' addresses in the object file to their linked address in
42 /// the linked binary.
44 /// A DebugMap producer could look like this:
45 /// DebugMap *DM = new DebugMap();
46 /// for (const auto &Obj: LinkedObjects) {
47 /// DebugMapObject &DMO = DM->addDebugMapObject(Obj.getPath());
48 /// for (const auto &Sym: Obj.getLinkedSymbols())
49 /// DMO.addSymbol(Sym.getName(), Sym.getObjectFileAddress(),
50 /// Sym.getBinaryAddress());
53 /// A DebugMap consumer can then use the map to link the debug
54 /// information. For example something along the lines of:
55 /// for (const auto &DMO: DM->objects()) {
56 /// auto Obj = createBinary(DMO.getObjectFilename());
57 /// for (auto &DIE: Obj.getDwarfDIEs()) {
58 /// if (SymbolMapping *Sym = DMO.lookup(DIE.getName()))
59 /// DIE.relocate(Sym->ObjectAddress, Sym->BinaryAddress);
61 /// DIE.discardSubtree();
66 typedef std::vector<std::unique_ptr<DebugMapObject>> ObjectContainer;
67 ObjectContainer Objects;
70 DebugMap(const Triple &BinaryTriple) : BinaryTriple(BinaryTriple) {}
72 typedef ObjectContainer::const_iterator const_iterator;
74 iterator_range<const_iterator> objects() const {
75 return make_range(begin(), end());
78 const_iterator begin() const { return Objects.begin(); }
80 const_iterator end() const { return Objects.end(); }
82 /// This function adds an DebugMapObject to the list owned by this
84 DebugMapObject &addDebugMapObject(StringRef ObjectFilePath);
86 const Triple &getTriple() { return BinaryTriple; }
88 void print(raw_ostream &OS) const;
95 /// \brief The DebugMapObject represents one object file described by
96 /// the DebugMap. It contains a list of mappings between addresses in
97 /// the object file and in the linked binary for all the linked atoms
98 /// in this object file.
99 class DebugMapObject {
101 struct SymbolMapping {
102 uint64_t ObjectAddress;
103 uint64_t BinaryAddress;
104 SymbolMapping(uint64_t ObjectAddress, uint64_t BinaryAddress)
105 : ObjectAddress(ObjectAddress), BinaryAddress(BinaryAddress) {}
108 typedef StringMapEntry<SymbolMapping> DebugMapEntry;
110 /// \brief Adds a symbol mapping to this DebugMapObject.
111 /// \returns false if the symbol was already registered. The request
112 /// is discarded in this case.
113 bool addSymbol(llvm::StringRef SymName, uint64_t ObjectAddress,
114 uint64_t LinkedAddress);
116 /// \brief Lookup a symbol mapping.
117 /// \returns null if the symbol isn't found.
118 const DebugMapEntry *lookupSymbol(StringRef SymbolName) const;
120 /// \brief Lookup an objectfile address.
121 /// \returns null if the address isn't found.
122 const DebugMapEntry *lookupObjectAddress(uint64_t Address) const;
124 llvm::StringRef getObjectFilename() const { return Filename; }
126 void print(raw_ostream &OS) const;
131 friend class DebugMap;
132 /// DebugMapObjects can only be constructed by the owning DebugMap.
133 DebugMapObject(StringRef ObjectFilename);
135 std::string Filename;
136 StringMap<SymbolMapping> Symbols;
137 DenseMap<uint64_t, DebugMapEntry *> AddressToMapping;
142 #endif // LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H