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"
31 #include "llvm/Support/Path.h"
32 #include "llvm/Support/YAMLTraits.h"
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.
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());
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);
63 /// DIE.discardSubtree();
68 typedef std::vector<std::unique_ptr<DebugMapObject>> ObjectContainer;
69 ObjectContainer Objects;
71 /// For YAML IO support.
73 friend yaml::MappingTraits<std::unique_ptr<DebugMap>>;
74 friend yaml::MappingTraits<DebugMap>;
78 DebugMap(const Triple &BinaryTriple) : BinaryTriple(BinaryTriple) {}
80 typedef ObjectContainer::const_iterator const_iterator;
82 iterator_range<const_iterator> objects() const {
83 return make_range(begin(), end());
86 const_iterator begin() const { return Objects.begin(); }
88 const_iterator end() const { return Objects.end(); }
90 /// This function adds an DebugMapObject to the list owned by this
92 DebugMapObject &addDebugMapObject(StringRef ObjectFilePath);
94 const Triple &getTriple() const { return BinaryTriple; }
96 void print(raw_ostream &OS) const;
102 /// Read a debug map for \a InputFile.
103 static ErrorOr<std::unique_ptr<DebugMap>>
104 parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath, bool Verbose);
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 {
113 struct SymbolMapping {
114 yaml::Hex64 ObjectAddress;
115 yaml::Hex64 BinaryAddress;
117 SymbolMapping(uint64_t ObjectAddress, uint64_t BinaryAddress, uint32_t Size)
118 : ObjectAddress(ObjectAddress), BinaryAddress(BinaryAddress),
120 /// For YAML IO support
121 SymbolMapping() = default;
124 typedef StringMapEntry<SymbolMapping> DebugMapEntry;
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,
130 uint64_t LinkedAddress, uint32_t Size);
132 /// \brief Lookup a symbol mapping.
133 /// \returns null if the symbol isn't found.
134 const DebugMapEntry *lookupSymbol(StringRef SymbolName) const;
136 /// \brief Lookup an objectfile address.
137 /// \returns null if the address isn't found.
138 const DebugMapEntry *lookupObjectAddress(uint64_t Address) const;
140 llvm::StringRef getObjectFilename() const { return Filename; }
142 iterator_range<StringMap<SymbolMapping>::const_iterator> symbols() const {
143 return make_range(Symbols.begin(), Symbols.end());
146 void print(raw_ostream &OS) const;
151 friend class DebugMap;
152 /// DebugMapObjects can only be constructed by the owning DebugMap.
153 DebugMapObject(StringRef ObjectFilename);
155 std::string Filename;
156 StringMap<SymbolMapping> Symbols;
157 DenseMap<uint64_t, DebugMapEntry *> AddressToMapping;
159 /// For YAMLIO support.
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;
168 DebugMapObject &operator=(DebugMapObject RHS) {
169 std::swap(Filename, RHS.Filename);
170 std::swap(Symbols, RHS.Symbols);
171 std::swap(AddressToMapping, RHS.AddressToMapping);
174 DebugMapObject(DebugMapObject &&RHS) {
175 Filename = std::move(RHS.Filename);
176 Symbols = std::move(RHS.Symbols);
177 AddressToMapping = std::move(RHS.AddressToMapping);
184 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::dsymutil::DebugMapObject::YAMLSymbolMapping)
189 using namespace llvm::dsymutil;
192 struct MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>> {
193 static void mapping(IO &io,
194 std::pair<std::string, DebugMapObject::SymbolMapping> &s);
195 static const bool flow = true;
198 template <> struct MappingTraits<dsymutil::DebugMapObject> {
200 static void mapping(IO &io, dsymutil::DebugMapObject &DMO);
203 template <> struct ScalarTraits<Triple> {
204 static void output(const Triple &val, void *, llvm::raw_ostream &out);
205 static StringRef input(StringRef scalar, void *, Triple &value);
206 static bool mustQuote(StringRef) { return true; }
210 struct SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>> {
212 size(IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq);
213 static dsymutil::DebugMapObject &
214 element(IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
218 template <> struct MappingTraits<dsymutil::DebugMap> {
219 static void mapping(IO &io, dsymutil::DebugMap &DM);
222 template <> struct MappingTraits<std::unique_ptr<dsymutil::DebugMap>> {
223 static void mapping(IO &io, std::unique_ptr<dsymutil::DebugMap> &DM);
228 #endif // LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H