blob: 1a3d62b67b7b477b35ad64db1396a69fe7b7f33d [file] [log] [blame]
Adrian Prantla9e23832016-01-14 18:31:07 +00001//=== tools/dsymutil/DebugMap.h - Generic debug map representation -*- C++ -*-//
Frederic Riss231f7142014-12-12 17:31:24 +00002//
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 Riss1595c5d2015-02-13 23:18:16 +000024#include "llvm/ADT/DenseMap.h"
Frederic Riss231f7142014-12-12 17:31:24 +000025#include "llvm/ADT/StringMap.h"
Frederic Risse4a6fef2015-01-19 23:33:14 +000026#include "llvm/ADT/Triple.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000027#include "llvm/ADT/iterator_range.h"
Frederic Riss231f7142014-12-12 17:31:24 +000028#include "llvm/Object/ObjectFile.h"
29#include "llvm/Support/ErrorOr.h"
30#include "llvm/Support/Format.h"
Frederic Rissc0866ad2015-06-05 16:35:44 +000031#include "llvm/Support/Path.h"
Frederic Riss9ccfddc2015-07-22 23:24:00 +000032#include "llvm/Support/TimeValue.h"
Frederic Riss08462f72015-06-01 21:12:45 +000033#include "llvm/Support/YAMLTraits.h"
Frederic Riss231f7142014-12-12 17:31:24 +000034#include <vector>
35
36namespace llvm {
37class raw_ostream;
38
39namespace dsymutil {
40class DebugMapObject;
41
42/// \brief The DebugMap object stores the list of object files to
43/// query for debug information along with the mapping between the
44/// symbols' addresses in the object file to their linked address in
45/// the linked binary.
46///
47/// A DebugMap producer could look like this:
48/// DebugMap *DM = new DebugMap();
49/// for (const auto &Obj: LinkedObjects) {
50/// DebugMapObject &DMO = DM->addDebugMapObject(Obj.getPath());
51/// for (const auto &Sym: Obj.getLinkedSymbols())
52/// DMO.addSymbol(Sym.getName(), Sym.getObjectFileAddress(),
53/// Sym.getBinaryAddress());
54/// }
55///
56/// A DebugMap consumer can then use the map to link the debug
57/// information. For example something along the lines of:
58/// for (const auto &DMO: DM->objects()) {
59/// auto Obj = createBinary(DMO.getObjectFilename());
60/// for (auto &DIE: Obj.getDwarfDIEs()) {
61/// if (SymbolMapping *Sym = DMO.lookup(DIE.getName()))
62/// DIE.relocate(Sym->ObjectAddress, Sym->BinaryAddress);
63/// else
64/// DIE.discardSubtree();
65/// }
66/// }
67class DebugMap {
Frederic Risse4a6fef2015-01-19 23:33:14 +000068 Triple BinaryTriple;
Frederic Riss2c69d362015-08-26 05:09:59 +000069 std::string BinaryPath;
Frederic Riss231f7142014-12-12 17:31:24 +000070 typedef std::vector<std::unique_ptr<DebugMapObject>> ObjectContainer;
71 ObjectContainer Objects;
72
Frederic Riss08462f72015-06-01 21:12:45 +000073 /// For YAML IO support.
74 ///@{
Frederic Riss90e0bd92015-06-03 20:29:24 +000075 friend yaml::MappingTraits<std::unique_ptr<DebugMap>>;
Frederic Riss08462f72015-06-01 21:12:45 +000076 friend yaml::MappingTraits<DebugMap>;
77 DebugMap() = default;
78 ///@}
Frederic Riss231f7142014-12-12 17:31:24 +000079public:
Frederic Riss2c69d362015-08-26 05:09:59 +000080 DebugMap(const Triple &BinaryTriple, StringRef BinaryPath)
81 : BinaryTriple(BinaryTriple), BinaryPath(BinaryPath) {}
Frederic Risse4a6fef2015-01-19 23:33:14 +000082
Frederic Riss231f7142014-12-12 17:31:24 +000083 typedef ObjectContainer::const_iterator const_iterator;
84
85 iterator_range<const_iterator> objects() const {
86 return make_range(begin(), end());
87 }
88
89 const_iterator begin() const { return Objects.begin(); }
90
91 const_iterator end() const { return Objects.end(); }
92
93 /// This function adds an DebugMapObject to the list owned by this
94 /// debug map.
Frederic Riss9ccfddc2015-07-22 23:24:00 +000095 DebugMapObject &addDebugMapObject(StringRef ObjectFilePath,
96 sys::TimeValue Timestamp);
Frederic Riss231f7142014-12-12 17:31:24 +000097
Frederic Riss717354f2015-02-28 00:28:56 +000098 const Triple &getTriple() const { return BinaryTriple; }
Frederic Risse4a6fef2015-01-19 23:33:14 +000099
Frederic Riss2c69d362015-08-26 05:09:59 +0000100 StringRef getBinaryPath() const { return BinaryPath; }
101
Frederic Riss231f7142014-12-12 17:31:24 +0000102 void print(raw_ostream &OS) const;
103
104#ifndef NDEBUG
105 void dump() const;
106#endif
Frederic Riss4d0ba662015-06-05 20:27:04 +0000107
108 /// Read a debug map for \a InputFile.
Frederic Riss4dd3e0c2015-08-05 18:27:44 +0000109 static ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
Frederic Riss4d0ba662015-06-05 20:27:04 +0000110 parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath, bool Verbose);
Frederic Riss231f7142014-12-12 17:31:24 +0000111};
112
113/// \brief The DebugMapObject represents one object file described by
114/// the DebugMap. It contains a list of mappings between addresses in
115/// the object file and in the linked binary for all the linked atoms
116/// in this object file.
117class DebugMapObject {
118public:
119 struct SymbolMapping {
Frederic Rissd8c33dc2016-01-31 04:29:22 +0000120 Optional<yaml::Hex64> ObjectAddress;
Frederic Riss08462f72015-06-01 21:12:45 +0000121 yaml::Hex64 BinaryAddress;
122 yaml::Hex32 Size;
Frederic Rissd8c33dc2016-01-31 04:29:22 +0000123 SymbolMapping(Optional<uint64_t> ObjectAddr, uint64_t BinaryAddress,
124 uint32_t Size)
125 : BinaryAddress(BinaryAddress), Size(Size) {
126 if (ObjectAddr)
127 ObjectAddress = *ObjectAddr;
128 }
Frederic Riss08462f72015-06-01 21:12:45 +0000129 /// For YAML IO support
130 SymbolMapping() = default;
Frederic Riss231f7142014-12-12 17:31:24 +0000131 };
132
Frederic Riss1595c5d2015-02-13 23:18:16 +0000133 typedef StringMapEntry<SymbolMapping> DebugMapEntry;
134
Frederic Riss231f7142014-12-12 17:31:24 +0000135 /// \brief Adds a symbol mapping to this DebugMapObject.
136 /// \returns false if the symbol was already registered. The request
137 /// is discarded in this case.
Frederic Rissd8c33dc2016-01-31 04:29:22 +0000138 bool addSymbol(llvm::StringRef SymName, Optional<uint64_t> ObjectAddress,
Frederic Riss912d0f12015-03-15 01:29:30 +0000139 uint64_t LinkedAddress, uint32_t Size);
Frederic Riss231f7142014-12-12 17:31:24 +0000140
Benjamin Kramer342554f2014-12-13 19:19:07 +0000141 /// \brief Lookup a symbol mapping.
Frederic Riss231f7142014-12-12 17:31:24 +0000142 /// \returns null if the symbol isn't found.
Frederic Riss1595c5d2015-02-13 23:18:16 +0000143 const DebugMapEntry *lookupSymbol(StringRef SymbolName) const;
144
145 /// \brief Lookup an objectfile address.
146 /// \returns null if the address isn't found.
147 const DebugMapEntry *lookupObjectAddress(uint64_t Address) const;
Frederic Riss231f7142014-12-12 17:31:24 +0000148
149 llvm::StringRef getObjectFilename() const { return Filename; }
150
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000151 sys::TimeValue getTimestamp() const { return Timestamp; }
152
Frederic Rissb0464082015-03-15 02:02:53 +0000153 iterator_range<StringMap<SymbolMapping>::const_iterator> symbols() const {
154 return make_range(Symbols.begin(), Symbols.end());
155 }
156
Frederic Riss231f7142014-12-12 17:31:24 +0000157 void print(raw_ostream &OS) const;
158#ifndef NDEBUG
159 void dump() const;
160#endif
161private:
162 friend class DebugMap;
163 /// DebugMapObjects can only be constructed by the owning DebugMap.
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000164 DebugMapObject(StringRef ObjectFilename, sys::TimeValue Timestamp);
Frederic Riss231f7142014-12-12 17:31:24 +0000165
166 std::string Filename;
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000167 sys::TimeValue Timestamp;
Frederic Riss231f7142014-12-12 17:31:24 +0000168 StringMap<SymbolMapping> Symbols;
Frederic Riss1595c5d2015-02-13 23:18:16 +0000169 DenseMap<uint64_t, DebugMapEntry *> AddressToMapping;
Frederic Riss08462f72015-06-01 21:12:45 +0000170
171 /// For YAMLIO support.
172 ///@{
173 typedef std::pair<std::string, SymbolMapping> YAMLSymbolMapping;
174 friend yaml::MappingTraits<dsymutil::DebugMapObject>;
175 friend yaml::SequenceTraits<std::vector<std::unique_ptr<DebugMapObject>>>;
176 friend yaml::SequenceTraits<std::vector<YAMLSymbolMapping>>;
177 DebugMapObject() = default;
Frederic Rissf37964c2015-06-05 20:27:07 +0000178
179public:
Frederic Riss90e0bd92015-06-03 20:29:24 +0000180 DebugMapObject &operator=(DebugMapObject RHS) {
181 std::swap(Filename, RHS.Filename);
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000182 std::swap(Timestamp, RHS.Timestamp);
Frederic Riss90e0bd92015-06-03 20:29:24 +0000183 std::swap(Symbols, RHS.Symbols);
184 std::swap(AddressToMapping, RHS.AddressToMapping);
185 return *this;
186 }
187 DebugMapObject(DebugMapObject &&RHS) {
188 Filename = std::move(RHS.Filename);
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000189 Timestamp = std::move(RHS.Timestamp);
Frederic Riss90e0bd92015-06-03 20:29:24 +0000190 Symbols = std::move(RHS.Symbols);
191 AddressToMapping = std::move(RHS.AddressToMapping);
192 }
Frederic Riss08462f72015-06-01 21:12:45 +0000193 ///@}
194};
195}
196}
197
Frederic Riss88ad8d22015-06-01 21:25:53 +0000198LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::dsymutil::DebugMapObject::YAMLSymbolMapping)
Frederic Riss08462f72015-06-01 21:12:45 +0000199
200namespace llvm {
201namespace yaml {
202
203using namespace llvm::dsymutil;
204
205template <>
206struct MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>> {
Frederic Riss4d0ba662015-06-05 20:27:04 +0000207 static void mapping(IO &io,
208 std::pair<std::string, DebugMapObject::SymbolMapping> &s);
Frederic Riss08462f72015-06-01 21:12:45 +0000209 static const bool flow = true;
210};
211
212template <> struct MappingTraits<dsymutil::DebugMapObject> {
Frederic Riss4d0ba662015-06-05 20:27:04 +0000213 struct YamlDMO;
214 static void mapping(IO &io, dsymutil::DebugMapObject &DMO);
Frederic Riss08462f72015-06-01 21:12:45 +0000215};
216
217template <> struct ScalarTraits<Triple> {
Frederic Riss4d0ba662015-06-05 20:27:04 +0000218 static void output(const Triple &val, void *, llvm::raw_ostream &out);
219 static StringRef input(StringRef scalar, void *, Triple &value);
Frederic Riss08462f72015-06-01 21:12:45 +0000220 static bool mustQuote(StringRef) { return true; }
221};
222
223template <>
224struct SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>> {
Frederic Riss08462f72015-06-01 21:12:45 +0000225 static size_t
Frederic Riss4d0ba662015-06-05 20:27:04 +0000226 size(IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq);
Frederic Riss08462f72015-06-01 21:12:45 +0000227 static dsymutil::DebugMapObject &
228 element(IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
Frederic Riss4d0ba662015-06-05 20:27:04 +0000229 size_t index);
Frederic Riss08462f72015-06-01 21:12:45 +0000230};
231
232template <> struct MappingTraits<dsymutil::DebugMap> {
Frederic Riss4d0ba662015-06-05 20:27:04 +0000233 static void mapping(IO &io, dsymutil::DebugMap &DM);
Frederic Riss231f7142014-12-12 17:31:24 +0000234};
Frederic Riss90e0bd92015-06-03 20:29:24 +0000235
Frederic Riss4d0ba662015-06-05 20:27:04 +0000236template <> struct MappingTraits<std::unique_ptr<dsymutil::DebugMap>> {
237 static void mapping(IO &io, std::unique_ptr<dsymutil::DebugMap> &DM);
Frederic Riss90e0bd92015-06-03 20:29:24 +0000238};
Frederic Riss231f7142014-12-12 17:31:24 +0000239}
240}
241
242#endif // LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H