Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 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 | |
Frederic Riss | 1595c5d | 2015-02-13 23:18:16 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/DenseMap.h" |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringMap.h" |
Frederic Riss | e4a6fef | 2015-01-19 23:33:14 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/iterator_range.h" |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 28 | #include "llvm/Object/ObjectFile.h" |
| 29 | #include "llvm/Support/ErrorOr.h" |
| 30 | #include "llvm/Support/Format.h" |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 31 | #include <vector> |
| 32 | |
| 33 | namespace llvm { |
| 34 | class raw_ostream; |
| 35 | |
| 36 | namespace dsymutil { |
| 37 | class DebugMapObject; |
| 38 | |
| 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. |
| 43 | /// |
| 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()); |
| 51 | /// } |
| 52 | /// |
| 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); |
| 60 | /// else |
| 61 | /// DIE.discardSubtree(); |
| 62 | /// } |
| 63 | /// } |
| 64 | class DebugMap { |
Frederic Riss | e4a6fef | 2015-01-19 23:33:14 +0000 | [diff] [blame] | 65 | Triple BinaryTriple; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 66 | typedef std::vector<std::unique_ptr<DebugMapObject>> ObjectContainer; |
| 67 | ObjectContainer Objects; |
| 68 | |
| 69 | public: |
Frederic Riss | e4a6fef | 2015-01-19 23:33:14 +0000 | [diff] [blame] | 70 | DebugMap(const Triple &BinaryTriple) : BinaryTriple(BinaryTriple) {} |
| 71 | |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 72 | typedef ObjectContainer::const_iterator const_iterator; |
| 73 | |
| 74 | iterator_range<const_iterator> objects() const { |
| 75 | return make_range(begin(), end()); |
| 76 | } |
| 77 | |
| 78 | const_iterator begin() const { return Objects.begin(); } |
| 79 | |
| 80 | const_iterator end() const { return Objects.end(); } |
| 81 | |
| 82 | /// This function adds an DebugMapObject to the list owned by this |
| 83 | /// debug map. |
| 84 | DebugMapObject &addDebugMapObject(StringRef ObjectFilePath); |
| 85 | |
Frederic Riss | 717354f | 2015-02-28 00:28:56 +0000 | [diff] [blame] | 86 | const Triple &getTriple() const { return BinaryTriple; } |
Frederic Riss | e4a6fef | 2015-01-19 23:33:14 +0000 | [diff] [blame] | 87 | |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 88 | void print(raw_ostream &OS) const; |
| 89 | |
| 90 | #ifndef NDEBUG |
| 91 | void dump() const; |
| 92 | #endif |
| 93 | }; |
| 94 | |
| 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 { |
| 100 | public: |
| 101 | struct SymbolMapping { |
| 102 | uint64_t ObjectAddress; |
| 103 | uint64_t BinaryAddress; |
Frederic Riss | 912d0f1 | 2015-03-15 01:29:30 +0000 | [diff] [blame^] | 104 | uint32_t Size; |
| 105 | SymbolMapping(uint64_t ObjectAddress, uint64_t BinaryAddress, uint32_t Size) |
| 106 | : ObjectAddress(ObjectAddress), BinaryAddress(BinaryAddress), |
| 107 | Size(Size) {} |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
Frederic Riss | 1595c5d | 2015-02-13 23:18:16 +0000 | [diff] [blame] | 110 | typedef StringMapEntry<SymbolMapping> DebugMapEntry; |
| 111 | |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 112 | /// \brief Adds a symbol mapping to this DebugMapObject. |
| 113 | /// \returns false if the symbol was already registered. The request |
| 114 | /// is discarded in this case. |
| 115 | bool addSymbol(llvm::StringRef SymName, uint64_t ObjectAddress, |
Frederic Riss | 912d0f1 | 2015-03-15 01:29:30 +0000 | [diff] [blame^] | 116 | uint64_t LinkedAddress, uint32_t Size); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 117 | |
Benjamin Kramer | 342554f | 2014-12-13 19:19:07 +0000 | [diff] [blame] | 118 | /// \brief Lookup a symbol mapping. |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 119 | /// \returns null if the symbol isn't found. |
Frederic Riss | 1595c5d | 2015-02-13 23:18:16 +0000 | [diff] [blame] | 120 | const DebugMapEntry *lookupSymbol(StringRef SymbolName) const; |
| 121 | |
| 122 | /// \brief Lookup an objectfile address. |
| 123 | /// \returns null if the address isn't found. |
| 124 | const DebugMapEntry *lookupObjectAddress(uint64_t Address) const; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 125 | |
| 126 | llvm::StringRef getObjectFilename() const { return Filename; } |
| 127 | |
| 128 | void print(raw_ostream &OS) const; |
| 129 | #ifndef NDEBUG |
| 130 | void dump() const; |
| 131 | #endif |
| 132 | private: |
| 133 | friend class DebugMap; |
| 134 | /// DebugMapObjects can only be constructed by the owning DebugMap. |
| 135 | DebugMapObject(StringRef ObjectFilename); |
| 136 | |
| 137 | std::string Filename; |
| 138 | StringMap<SymbolMapping> Symbols; |
Frederic Riss | 1595c5d | 2015-02-13 23:18:16 +0000 | [diff] [blame] | 139 | DenseMap<uint64_t, DebugMapEntry *> AddressToMapping; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 140 | }; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | #endif // LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H |