blob: eab0cb0a8009b9aabebd5cbdf2951c0ea4f32841 [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"
Pavel Labath62d72042016-11-09 11:43:52 +000029#include "llvm/Support/Chrono.h"
Frederic Riss231f7142014-12-12 17:31:24 +000030#include "llvm/Support/ErrorOr.h"
31#include "llvm/Support/Format.h"
Frederic Rissc0866ad2015-06-05 16:35:44 +000032#include "llvm/Support/Path.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.
Pavel Labath62d72042016-11-09 11:43:52 +000095 DebugMapObject &
96 addDebugMapObject(StringRef ObjectFilePath,
97 sys::TimePoint<std::chrono::seconds> Timestamp);
Frederic Riss231f7142014-12-12 17:31:24 +000098
Frederic Riss717354f2015-02-28 00:28:56 +000099 const Triple &getTriple() const { return BinaryTriple; }
Frederic Risse4a6fef2015-01-19 23:33:14 +0000100
Frederic Riss2c69d362015-08-26 05:09:59 +0000101 StringRef getBinaryPath() const { return BinaryPath; }
102
Frederic Riss231f7142014-12-12 17:31:24 +0000103 void print(raw_ostream &OS) const;
104
105#ifndef NDEBUG
106 void dump() const;
107#endif
Frederic Riss4d0ba662015-06-05 20:27:04 +0000108
109 /// Read a debug map for \a InputFile.
Frederic Riss4dd3e0c2015-08-05 18:27:44 +0000110 static ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
Frederic Riss4d0ba662015-06-05 20:27:04 +0000111 parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath, bool Verbose);
Frederic Riss231f7142014-12-12 17:31:24 +0000112};
113
114/// \brief The DebugMapObject represents one object file described by
115/// the DebugMap. It contains a list of mappings between addresses in
116/// the object file and in the linked binary for all the linked atoms
117/// in this object file.
118class DebugMapObject {
119public:
120 struct SymbolMapping {
Frederic Rissd8c33dc2016-01-31 04:29:22 +0000121 Optional<yaml::Hex64> ObjectAddress;
Frederic Riss08462f72015-06-01 21:12:45 +0000122 yaml::Hex64 BinaryAddress;
123 yaml::Hex32 Size;
Frederic Rissd8c33dc2016-01-31 04:29:22 +0000124 SymbolMapping(Optional<uint64_t> ObjectAddr, uint64_t BinaryAddress,
125 uint32_t Size)
126 : BinaryAddress(BinaryAddress), Size(Size) {
127 if (ObjectAddr)
128 ObjectAddress = *ObjectAddr;
129 }
Frederic Riss08462f72015-06-01 21:12:45 +0000130 /// For YAML IO support
131 SymbolMapping() = default;
Frederic Riss231f7142014-12-12 17:31:24 +0000132 };
133
Zachary Turner9e91c282016-08-05 23:12:31 +0000134 typedef std::pair<std::string, SymbolMapping> YAMLSymbolMapping;
Frederic Riss1595c5d2015-02-13 23:18:16 +0000135 typedef StringMapEntry<SymbolMapping> DebugMapEntry;
136
Frederic Riss231f7142014-12-12 17:31:24 +0000137 /// \brief Adds a symbol mapping to this DebugMapObject.
138 /// \returns false if the symbol was already registered. The request
139 /// is discarded in this case.
Frederic Rissd8c33dc2016-01-31 04:29:22 +0000140 bool addSymbol(llvm::StringRef SymName, Optional<uint64_t> ObjectAddress,
Frederic Riss912d0f12015-03-15 01:29:30 +0000141 uint64_t LinkedAddress, uint32_t Size);
Frederic Riss231f7142014-12-12 17:31:24 +0000142
Benjamin Kramer342554f2014-12-13 19:19:07 +0000143 /// \brief Lookup a symbol mapping.
Frederic Riss231f7142014-12-12 17:31:24 +0000144 /// \returns null if the symbol isn't found.
Frederic Riss1595c5d2015-02-13 23:18:16 +0000145 const DebugMapEntry *lookupSymbol(StringRef SymbolName) const;
146
147 /// \brief Lookup an objectfile address.
148 /// \returns null if the address isn't found.
149 const DebugMapEntry *lookupObjectAddress(uint64_t Address) const;
Frederic Riss231f7142014-12-12 17:31:24 +0000150
151 llvm::StringRef getObjectFilename() const { return Filename; }
152
Pavel Labath62d72042016-11-09 11:43:52 +0000153 sys::TimePoint<std::chrono::seconds> getTimestamp() const {
154 return Timestamp;
155 }
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000156
Frederic Rissb0464082015-03-15 02:02:53 +0000157 iterator_range<StringMap<SymbolMapping>::const_iterator> symbols() const {
158 return make_range(Symbols.begin(), Symbols.end());
159 }
160
Frederic Riss231f7142014-12-12 17:31:24 +0000161 void print(raw_ostream &OS) const;
162#ifndef NDEBUG
163 void dump() const;
164#endif
165private:
166 friend class DebugMap;
167 /// DebugMapObjects can only be constructed by the owning DebugMap.
Pavel Labath62d72042016-11-09 11:43:52 +0000168 DebugMapObject(StringRef ObjectFilename,
169 sys::TimePoint<std::chrono::seconds> Timestamp);
Frederic Riss231f7142014-12-12 17:31:24 +0000170
171 std::string Filename;
Pavel Labath62d72042016-11-09 11:43:52 +0000172 sys::TimePoint<std::chrono::seconds> Timestamp;
Frederic Riss231f7142014-12-12 17:31:24 +0000173 StringMap<SymbolMapping> Symbols;
Frederic Riss1595c5d2015-02-13 23:18:16 +0000174 DenseMap<uint64_t, DebugMapEntry *> AddressToMapping;
Frederic Riss08462f72015-06-01 21:12:45 +0000175
176 /// For YAMLIO support.
177 ///@{
Frederic Riss08462f72015-06-01 21:12:45 +0000178 friend yaml::MappingTraits<dsymutil::DebugMapObject>;
179 friend yaml::SequenceTraits<std::vector<std::unique_ptr<DebugMapObject>>>;
Frederic Riss08462f72015-06-01 21:12:45 +0000180 DebugMapObject() = default;
Frederic Rissf37964c2015-06-05 20:27:07 +0000181
182public:
Benjamin Kramer0eae9ec2016-10-27 15:23:44 +0000183 DebugMapObject(DebugMapObject &&) = default;
184 DebugMapObject &operator=(DebugMapObject &&) = default;
Frederic Riss08462f72015-06-01 21:12:45 +0000185 ///@}
186};
187}
188}
189
Frederic Riss88ad8d22015-06-01 21:25:53 +0000190LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::dsymutil::DebugMapObject::YAMLSymbolMapping)
Frederic Riss08462f72015-06-01 21:12:45 +0000191
192namespace llvm {
193namespace yaml {
194
195using namespace llvm::dsymutil;
196
197template <>
198struct MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>> {
Frederic Riss4d0ba662015-06-05 20:27:04 +0000199 static void mapping(IO &io,
200 std::pair<std::string, DebugMapObject::SymbolMapping> &s);
Frederic Riss08462f72015-06-01 21:12:45 +0000201 static const bool flow = true;
202};
203
204template <> struct MappingTraits<dsymutil::DebugMapObject> {
Frederic Riss4d0ba662015-06-05 20:27:04 +0000205 struct YamlDMO;
206 static void mapping(IO &io, dsymutil::DebugMapObject &DMO);
Frederic Riss08462f72015-06-01 21:12:45 +0000207};
208
209template <> struct ScalarTraits<Triple> {
Frederic Riss4d0ba662015-06-05 20:27:04 +0000210 static void output(const Triple &val, void *, llvm::raw_ostream &out);
211 static StringRef input(StringRef scalar, void *, Triple &value);
Frederic Riss08462f72015-06-01 21:12:45 +0000212 static bool mustQuote(StringRef) { return true; }
213};
214
215template <>
216struct SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>> {
Frederic Riss08462f72015-06-01 21:12:45 +0000217 static size_t
Frederic Riss4d0ba662015-06-05 20:27:04 +0000218 size(IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq);
Frederic Riss08462f72015-06-01 21:12:45 +0000219 static dsymutil::DebugMapObject &
220 element(IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
Frederic Riss4d0ba662015-06-05 20:27:04 +0000221 size_t index);
Frederic Riss08462f72015-06-01 21:12:45 +0000222};
223
224template <> struct MappingTraits<dsymutil::DebugMap> {
Frederic Riss4d0ba662015-06-05 20:27:04 +0000225 static void mapping(IO &io, dsymutil::DebugMap &DM);
Frederic Riss231f7142014-12-12 17:31:24 +0000226};
Frederic Riss90e0bd92015-06-03 20:29:24 +0000227
Frederic Riss4d0ba662015-06-05 20:27:04 +0000228template <> struct MappingTraits<std::unique_ptr<dsymutil::DebugMap>> {
229 static void mapping(IO &io, std::unique_ptr<dsymutil::DebugMap> &DM);
Frederic Riss90e0bd92015-06-03 20:29:24 +0000230};
Frederic Riss231f7142014-12-12 17:31:24 +0000231}
232}
233
234#endif // LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H