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 | c0866ad | 2015-06-05 16:35:44 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Path.h" |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 32 | #include "llvm/Support/YAMLTraits.h" |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 33 | #include <vector> |
| 34 | |
| 35 | namespace llvm { |
| 36 | class raw_ostream; |
| 37 | |
| 38 | namespace dsymutil { |
| 39 | class DebugMapObject; |
| 40 | |
| 41 | /// \brief The DebugMap object stores the list of object files to |
| 42 | /// query for debug information along with the mapping between the |
| 43 | /// symbols' addresses in the object file to their linked address in |
| 44 | /// the linked binary. |
| 45 | /// |
| 46 | /// A DebugMap producer could look like this: |
| 47 | /// DebugMap *DM = new DebugMap(); |
| 48 | /// for (const auto &Obj: LinkedObjects) { |
| 49 | /// DebugMapObject &DMO = DM->addDebugMapObject(Obj.getPath()); |
| 50 | /// for (const auto &Sym: Obj.getLinkedSymbols()) |
| 51 | /// DMO.addSymbol(Sym.getName(), Sym.getObjectFileAddress(), |
| 52 | /// Sym.getBinaryAddress()); |
| 53 | /// } |
| 54 | /// |
| 55 | /// A DebugMap consumer can then use the map to link the debug |
| 56 | /// information. For example something along the lines of: |
| 57 | /// for (const auto &DMO: DM->objects()) { |
| 58 | /// auto Obj = createBinary(DMO.getObjectFilename()); |
| 59 | /// for (auto &DIE: Obj.getDwarfDIEs()) { |
| 60 | /// if (SymbolMapping *Sym = DMO.lookup(DIE.getName())) |
| 61 | /// DIE.relocate(Sym->ObjectAddress, Sym->BinaryAddress); |
| 62 | /// else |
| 63 | /// DIE.discardSubtree(); |
| 64 | /// } |
| 65 | /// } |
| 66 | class DebugMap { |
Frederic Riss | e4a6fef | 2015-01-19 23:33:14 +0000 | [diff] [blame] | 67 | Triple BinaryTriple; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 68 | typedef std::vector<std::unique_ptr<DebugMapObject>> ObjectContainer; |
| 69 | ObjectContainer Objects; |
| 70 | |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 71 | /// For YAML IO support. |
| 72 | ///@{ |
Frederic Riss | 90e0bd9 | 2015-06-03 20:29:24 +0000 | [diff] [blame] | 73 | friend yaml::MappingTraits<std::unique_ptr<DebugMap>>; |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 74 | friend yaml::MappingTraits<DebugMap>; |
| 75 | DebugMap() = default; |
| 76 | ///@} |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 77 | public: |
Frederic Riss | e4a6fef | 2015-01-19 23:33:14 +0000 | [diff] [blame] | 78 | DebugMap(const Triple &BinaryTriple) : BinaryTriple(BinaryTriple) {} |
| 79 | |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 80 | typedef ObjectContainer::const_iterator const_iterator; |
| 81 | |
| 82 | iterator_range<const_iterator> objects() const { |
| 83 | return make_range(begin(), end()); |
| 84 | } |
| 85 | |
| 86 | const_iterator begin() const { return Objects.begin(); } |
| 87 | |
| 88 | const_iterator end() const { return Objects.end(); } |
| 89 | |
| 90 | /// This function adds an DebugMapObject to the list owned by this |
| 91 | /// debug map. |
| 92 | DebugMapObject &addDebugMapObject(StringRef ObjectFilePath); |
| 93 | |
Frederic Riss | 717354f | 2015-02-28 00:28:56 +0000 | [diff] [blame] | 94 | const Triple &getTriple() const { return BinaryTriple; } |
Frederic Riss | e4a6fef | 2015-01-19 23:33:14 +0000 | [diff] [blame] | 95 | |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 96 | void print(raw_ostream &OS) const; |
| 97 | |
| 98 | #ifndef NDEBUG |
| 99 | void dump() const; |
| 100 | #endif |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame^] | 101 | |
| 102 | /// Read a debug map for \a InputFile. |
| 103 | static ErrorOr<std::unique_ptr<DebugMap>> |
| 104 | parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath, bool Verbose); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | /// \brief The DebugMapObject represents one object file described by |
| 108 | /// the DebugMap. It contains a list of mappings between addresses in |
| 109 | /// the object file and in the linked binary for all the linked atoms |
| 110 | /// in this object file. |
| 111 | class DebugMapObject { |
| 112 | public: |
| 113 | struct SymbolMapping { |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 114 | yaml::Hex64 ObjectAddress; |
| 115 | yaml::Hex64 BinaryAddress; |
| 116 | yaml::Hex32 Size; |
Frederic Riss | 912d0f1 | 2015-03-15 01:29:30 +0000 | [diff] [blame] | 117 | SymbolMapping(uint64_t ObjectAddress, uint64_t BinaryAddress, uint32_t Size) |
| 118 | : ObjectAddress(ObjectAddress), BinaryAddress(BinaryAddress), |
| 119 | Size(Size) {} |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 120 | /// For YAML IO support |
| 121 | SymbolMapping() = default; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
Frederic Riss | 1595c5d | 2015-02-13 23:18:16 +0000 | [diff] [blame] | 124 | typedef StringMapEntry<SymbolMapping> DebugMapEntry; |
| 125 | |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 126 | /// \brief Adds a symbol mapping to this DebugMapObject. |
| 127 | /// \returns false if the symbol was already registered. The request |
| 128 | /// is discarded in this case. |
| 129 | bool addSymbol(llvm::StringRef SymName, uint64_t ObjectAddress, |
Frederic Riss | 912d0f1 | 2015-03-15 01:29:30 +0000 | [diff] [blame] | 130 | uint64_t LinkedAddress, uint32_t Size); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 131 | |
Benjamin Kramer | 342554f | 2014-12-13 19:19:07 +0000 | [diff] [blame] | 132 | /// \brief Lookup a symbol mapping. |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 133 | /// \returns null if the symbol isn't found. |
Frederic Riss | 1595c5d | 2015-02-13 23:18:16 +0000 | [diff] [blame] | 134 | const DebugMapEntry *lookupSymbol(StringRef SymbolName) const; |
| 135 | |
| 136 | /// \brief Lookup an objectfile address. |
| 137 | /// \returns null if the address isn't found. |
| 138 | const DebugMapEntry *lookupObjectAddress(uint64_t Address) const; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 139 | |
| 140 | llvm::StringRef getObjectFilename() const { return Filename; } |
| 141 | |
Frederic Riss | b046408 | 2015-03-15 02:02:53 +0000 | [diff] [blame] | 142 | iterator_range<StringMap<SymbolMapping>::const_iterator> symbols() const { |
| 143 | return make_range(Symbols.begin(), Symbols.end()); |
| 144 | } |
| 145 | |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 146 | void print(raw_ostream &OS) const; |
| 147 | #ifndef NDEBUG |
| 148 | void dump() const; |
| 149 | #endif |
| 150 | private: |
| 151 | friend class DebugMap; |
| 152 | /// DebugMapObjects can only be constructed by the owning DebugMap. |
| 153 | DebugMapObject(StringRef ObjectFilename); |
| 154 | |
| 155 | std::string Filename; |
| 156 | StringMap<SymbolMapping> Symbols; |
Frederic Riss | 1595c5d | 2015-02-13 23:18:16 +0000 | [diff] [blame] | 157 | DenseMap<uint64_t, DebugMapEntry *> AddressToMapping; |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 158 | |
| 159 | /// For YAMLIO support. |
| 160 | ///@{ |
| 161 | typedef std::pair<std::string, SymbolMapping> YAMLSymbolMapping; |
| 162 | friend yaml::MappingTraits<dsymutil::DebugMapObject>; |
| 163 | friend yaml::SequenceTraits<std::vector<std::unique_ptr<DebugMapObject>>>; |
| 164 | friend yaml::SequenceTraits<std::vector<YAMLSymbolMapping>>; |
| 165 | DebugMapObject() = default; |
Frederic Riss | 90e0bd9 | 2015-06-03 20:29:24 +0000 | [diff] [blame] | 166 | public: |
| 167 | DebugMapObject &operator=(DebugMapObject RHS) { |
| 168 | std::swap(Filename, RHS.Filename); |
| 169 | std::swap(Symbols, RHS.Symbols); |
| 170 | std::swap(AddressToMapping, RHS.AddressToMapping); |
| 171 | return *this; |
| 172 | } |
| 173 | DebugMapObject(DebugMapObject &&RHS) { |
| 174 | Filename = std::move(RHS.Filename); |
| 175 | Symbols = std::move(RHS.Symbols); |
| 176 | AddressToMapping = std::move(RHS.AddressToMapping); |
| 177 | } |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 178 | ///@} |
| 179 | }; |
| 180 | } |
| 181 | } |
| 182 | |
Frederic Riss | 88ad8d2 | 2015-06-01 21:25:53 +0000 | [diff] [blame] | 183 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::dsymutil::DebugMapObject::YAMLSymbolMapping) |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 184 | |
| 185 | namespace llvm { |
| 186 | namespace yaml { |
| 187 | |
| 188 | using namespace llvm::dsymutil; |
| 189 | |
| 190 | template <> |
| 191 | struct MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>> { |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame^] | 192 | static void mapping(IO &io, |
| 193 | std::pair<std::string, DebugMapObject::SymbolMapping> &s); |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 194 | static const bool flow = true; |
| 195 | }; |
| 196 | |
| 197 | template <> struct MappingTraits<dsymutil::DebugMapObject> { |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame^] | 198 | struct YamlDMO; |
| 199 | static void mapping(IO &io, dsymutil::DebugMapObject &DMO); |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | template <> struct ScalarTraits<Triple> { |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame^] | 203 | static void output(const Triple &val, void *, llvm::raw_ostream &out); |
| 204 | static StringRef input(StringRef scalar, void *, Triple &value); |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 205 | static bool mustQuote(StringRef) { return true; } |
| 206 | }; |
| 207 | |
| 208 | template <> |
| 209 | struct SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>> { |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 210 | static size_t |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame^] | 211 | size(IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq); |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 212 | static dsymutil::DebugMapObject & |
| 213 | element(IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq, |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame^] | 214 | size_t index); |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | template <> struct MappingTraits<dsymutil::DebugMap> { |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame^] | 218 | static void mapping(IO &io, dsymutil::DebugMap &DM); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 219 | }; |
Frederic Riss | 90e0bd9 | 2015-06-03 20:29:24 +0000 | [diff] [blame] | 220 | |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame^] | 221 | template <> struct MappingTraits<std::unique_ptr<dsymutil::DebugMap>> { |
| 222 | static void mapping(IO &io, std::unique_ptr<dsymutil::DebugMap> &DM); |
Frederic Riss | 90e0bd9 | 2015-06-03 20:29:24 +0000 | [diff] [blame] | 223 | }; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
| 227 | #endif // LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H |