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/YAMLTraits.h"
40 /// \brief The DebugMap object stores the list of object files to
41 /// query for debug information along with the mapping between the
42 /// symbols' addresses in the object file to their linked address in
43 /// the linked binary.
45 /// A DebugMap producer could look like this:
46 /// DebugMap *DM = new DebugMap();
47 /// for (const auto &Obj: LinkedObjects) {
48 /// DebugMapObject &DMO = DM->addDebugMapObject(Obj.getPath());
49 /// for (const auto &Sym: Obj.getLinkedSymbols())
50 /// DMO.addSymbol(Sym.getName(), Sym.getObjectFileAddress(),
51 /// Sym.getBinaryAddress());
54 /// A DebugMap consumer can then use the map to link the debug
55 /// information. For example something along the lines of:
56 /// for (const auto &DMO: DM->objects()) {
57 /// auto Obj = createBinary(DMO.getObjectFilename());
58 /// for (auto &DIE: Obj.getDwarfDIEs()) {
59 /// if (SymbolMapping *Sym = DMO.lookup(DIE.getName()))
60 /// DIE.relocate(Sym->ObjectAddress, Sym->BinaryAddress);
62 /// DIE.discardSubtree();
67 typedef std::vector<std::unique_ptr<DebugMapObject>> ObjectContainer;
68 ObjectContainer Objects;
70 /// For YAML IO support.
72 friend yaml::MappingTraits<std::unique_ptr<DebugMap>>;
73 friend yaml::MappingTraits<DebugMap>;
77 DebugMap(const Triple &BinaryTriple) : BinaryTriple(BinaryTriple) {}
79 typedef ObjectContainer::const_iterator const_iterator;
81 iterator_range<const_iterator> objects() const {
82 return make_range(begin(), end());
85 const_iterator begin() const { return Objects.begin(); }
87 const_iterator end() const { return Objects.end(); }
89 /// This function adds an DebugMapObject to the list owned by this
91 DebugMapObject &addDebugMapObject(StringRef ObjectFilePath);
93 const Triple &getTriple() const { return BinaryTriple; }
95 void print(raw_ostream &OS) const;
102 /// \brief The DebugMapObject represents one object file described by
103 /// the DebugMap. It contains a list of mappings between addresses in
104 /// the object file and in the linked binary for all the linked atoms
105 /// in this object file.
106 class DebugMapObject {
108 struct SymbolMapping {
109 yaml::Hex64 ObjectAddress;
110 yaml::Hex64 BinaryAddress;
112 SymbolMapping(uint64_t ObjectAddress, uint64_t BinaryAddress, uint32_t Size)
113 : ObjectAddress(ObjectAddress), BinaryAddress(BinaryAddress),
115 /// For YAML IO support
116 SymbolMapping() = default;
119 typedef StringMapEntry<SymbolMapping> DebugMapEntry;
121 /// \brief Adds a symbol mapping to this DebugMapObject.
122 /// \returns false if the symbol was already registered. The request
123 /// is discarded in this case.
124 bool addSymbol(llvm::StringRef SymName, uint64_t ObjectAddress,
125 uint64_t LinkedAddress, uint32_t Size);
127 /// \brief Lookup a symbol mapping.
128 /// \returns null if the symbol isn't found.
129 const DebugMapEntry *lookupSymbol(StringRef SymbolName) const;
131 /// \brief Lookup an objectfile address.
132 /// \returns null if the address isn't found.
133 const DebugMapEntry *lookupObjectAddress(uint64_t Address) const;
135 llvm::StringRef getObjectFilename() const { return Filename; }
137 iterator_range<StringMap<SymbolMapping>::const_iterator> symbols() const {
138 return make_range(Symbols.begin(), Symbols.end());
141 void print(raw_ostream &OS) const;
146 friend class DebugMap;
147 /// DebugMapObjects can only be constructed by the owning DebugMap.
148 DebugMapObject(StringRef ObjectFilename);
150 std::string Filename;
151 StringMap<SymbolMapping> Symbols;
152 DenseMap<uint64_t, DebugMapEntry *> AddressToMapping;
154 /// For YAMLIO support.
156 typedef std::pair<std::string, SymbolMapping> YAMLSymbolMapping;
157 friend yaml::MappingTraits<dsymutil::DebugMapObject>;
158 friend yaml::SequenceTraits<std::vector<std::unique_ptr<DebugMapObject>>>;
159 friend yaml::SequenceTraits<std::vector<YAMLSymbolMapping>>;
160 DebugMapObject() = default;
166 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::dsymutil::DebugMapObject::YAMLSymbolMapping)
171 using namespace llvm::dsymutil;
174 struct MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>> {
177 mapping(IO &io, std::pair<std::string, DebugMapObject::SymbolMapping> &s) {
178 io.mapRequired("sym", s.first);
179 io.mapRequired("objAddr", s.second.ObjectAddress);
180 io.mapRequired("binAddr", s.second.BinaryAddress);
181 io.mapOptional("size", s.second.Size);
184 static const bool flow = true;
187 template <> struct MappingTraits<dsymutil::DebugMapObject> {
188 // Normalize/Denormalize between YAML and a DebugMapObject.
192 YamlDMO(IO &io, dsymutil::DebugMapObject &Obj) {
193 Filename = Obj.Filename;
194 Entries.reserve(Obj.Symbols.size());
195 for (auto &Entry : Obj.Symbols)
196 Entries.push_back(std::make_pair(Entry.getKey(), Entry.getValue()));
199 dsymutil::DebugMapObject denormalize(IO &) {
200 dsymutil::DebugMapObject Res(Filename);
201 for (auto &Entry : Entries) {
202 auto &Mapping = Entry.second;
203 Res.addSymbol(Entry.first, Mapping.ObjectAddress, Mapping.BinaryAddress,
209 std::string Filename;
210 std::vector<dsymutil::DebugMapObject::YAMLSymbolMapping> Entries;
213 static void mapping(IO &io, dsymutil::DebugMapObject &DMO) {
214 MappingNormalization<YamlDMO, dsymutil::DebugMapObject> Norm(io, DMO);
215 io.mapRequired("filename", Norm->Filename);
216 io.mapRequired("symbols", Norm->Entries);
220 template <> struct ScalarTraits<Triple> {
222 static void output(const Triple &val, void *, llvm::raw_ostream &out) {
226 static StringRef input(StringRef scalar, void *, Triple &value) {
227 value = Triple(scalar);
231 static bool mustQuote(StringRef) { return true; }
235 struct SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>> {
238 size(IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq) {
242 static dsymutil::DebugMapObject &
243 element(IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
245 if (index >= seq.size()) {
246 seq.resize(index + 1);
247 seq[index].reset(new dsymutil::DebugMapObject);
253 template <> struct MappingTraits<dsymutil::DebugMap> {
254 static void mapping(IO &io, dsymutil::DebugMap &DM) {
255 io.mapRequired("triple", DM.BinaryTriple);
256 io.mapOptional("objects", DM.Objects);
260 template <> struct MappingTraits<std::unique_ptr<dsymutil::DebugMap>> {
261 static void mapping(IO &io, std::unique_ptr<dsymutil::DebugMap> &DM) {
263 DM.reset(new DebugMap());
264 io.mapRequired("triple", DM->BinaryTriple);
265 io.mapOptional("objects", DM->Objects);
271 #endif // LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H