[AMDGPU] Test codegen'ing True16 additions.
[llvm-project.git] / llvm / tools / dsymutil / DebugMap.h
blob86cb88d32492d3d94cbbfb38b8e38456963a6ec6
1 //=- tools/dsymutil/DebugMap.h - Generic debug map representation -*- C++ -*-=//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 ///
11 /// This file contains the class declaration of the DebugMap
12 /// entity. A DebugMap lists all the object files linked together to
13 /// produce an executable along with the linked address of all the
14 /// atoms used in these object files.
15 /// The DebugMap is an input to the DwarfLinker class that will
16 /// extract the Dwarf debug information from the referenced object
17 /// files and link their usefull debug info together.
19 //===----------------------------------------------------------------------===//
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/StringRef.h"
27 #include "llvm/ADT/iterator_range.h"
28 #include "llvm/Object/MachO.h"
29 #include "llvm/Support/Chrono.h"
30 #include "llvm/Support/ErrorOr.h"
31 #include "llvm/Support/YAMLTraits.h"
32 #include "llvm/TargetParser/Triple.h"
33 #include <chrono>
34 #include <cstddef>
35 #include <cstdint>
36 #include <memory>
37 #include <optional>
38 #include <string>
39 #include <utility>
40 #include <vector>
42 namespace llvm {
44 class raw_ostream;
46 namespace dsymutil {
48 class DebugMapObject;
50 /// The DebugMap object stores the list of object files to query for debug
51 /// information along with the mapping between the symbols' addresses in the
52 /// object file to their linked address in the linked binary.
53 ///
54 /// A DebugMap producer could look like this:
55 /// DebugMap *DM = new DebugMap();
56 /// for (const auto &Obj: LinkedObjects) {
57 /// DebugMapObject &DMO = DM->addDebugMapObject(Obj.getPath());
58 /// for (const auto &Sym: Obj.getLinkedSymbols())
59 /// DMO.addSymbol(Sym.getName(), Sym.getObjectFileAddress(),
60 /// Sym.getBinaryAddress());
61 /// }
62 ///
63 /// A DebugMap consumer can then use the map to link the debug
64 /// information. For example something along the lines of:
65 /// for (const auto &DMO: DM->objects()) {
66 /// auto Obj = createBinary(DMO.getObjectFilename());
67 /// for (auto &DIE: Obj.getDwarfDIEs()) {
68 /// if (SymbolMapping *Sym = DMO.lookup(DIE.getName()))
69 /// DIE.relocate(Sym->ObjectAddress, Sym->BinaryAddress);
70 /// else
71 /// DIE.discardSubtree();
72 /// }
73 /// }
74 class DebugMap {
75 Triple BinaryTriple;
76 std::string BinaryPath;
77 std::vector<uint8_t> BinaryUUID;
78 using ObjectContainer = std::vector<std::unique_ptr<DebugMapObject>>;
80 ObjectContainer Objects;
82 /// For YAML IO support.
83 ///@{
84 friend yaml::MappingTraits<std::unique_ptr<DebugMap>>;
85 friend yaml::MappingTraits<DebugMap>;
87 DebugMap() = default;
88 ///@}
90 public:
91 DebugMap(const Triple &BinaryTriple, StringRef BinaryPath,
92 ArrayRef<uint8_t> BinaryUUID = ArrayRef<uint8_t>())
93 : BinaryTriple(BinaryTriple), BinaryPath(std::string(BinaryPath)),
94 BinaryUUID(BinaryUUID.begin(), BinaryUUID.end()) {}
96 using const_iterator = ObjectContainer::const_iterator;
98 iterator_range<const_iterator> objects() const {
99 return make_range(begin(), end());
102 const_iterator begin() const { return Objects.begin(); }
104 const_iterator end() const { return Objects.end(); }
106 unsigned getNumberOfObjects() const { return Objects.size(); }
108 /// This function adds an DebugMapObject to the list owned by this
109 /// debug map.
110 DebugMapObject &
111 addDebugMapObject(StringRef ObjectFilePath,
112 sys::TimePoint<std::chrono::seconds> Timestamp,
113 uint8_t Type = llvm::MachO::N_OSO);
115 const Triple &getTriple() const { return BinaryTriple; }
117 ArrayRef<uint8_t> getUUID() const { return ArrayRef<uint8_t>(BinaryUUID); }
119 StringRef getBinaryPath() const { return BinaryPath; }
121 void print(raw_ostream &OS) const;
123 #ifndef NDEBUG
124 void dump() const;
125 #endif
127 /// Read a debug map for \a InputFile.
128 static ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
129 parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath, bool Verbose);
132 /// The DebugMapObject represents one object file described by the DebugMap. It
133 /// contains a list of mappings between addresses in the object file and in the
134 /// linked binary for all the linked atoms in this object file.
135 class DebugMapObject {
136 public:
137 struct SymbolMapping {
138 std::optional<yaml::Hex64> ObjectAddress;
139 yaml::Hex64 BinaryAddress;
140 yaml::Hex32 Size;
142 SymbolMapping(std::optional<uint64_t> ObjectAddr, uint64_t BinaryAddress,
143 uint32_t Size)
144 : BinaryAddress(BinaryAddress), Size(Size) {
145 if (ObjectAddr)
146 ObjectAddress = *ObjectAddr;
149 /// For YAML IO support
150 SymbolMapping() = default;
153 using YAMLSymbolMapping = std::pair<std::string, SymbolMapping>;
154 using DebugMapEntry = StringMapEntry<SymbolMapping>;
156 /// Adds a symbol mapping to this DebugMapObject.
157 /// \returns false if the symbol was already registered. The request
158 /// is discarded in this case.
159 bool addSymbol(StringRef SymName, std::optional<uint64_t> ObjectAddress,
160 uint64_t LinkedAddress, uint32_t Size);
162 /// Lookup a symbol mapping.
163 /// \returns null if the symbol isn't found.
164 const DebugMapEntry *lookupSymbol(StringRef SymbolName) const;
166 /// Lookup an object file address.
167 /// \returns null if the address isn't found.
168 const DebugMapEntry *lookupObjectAddress(uint64_t Address) const;
170 StringRef getObjectFilename() const { return Filename; }
172 sys::TimePoint<std::chrono::seconds> getTimestamp() const {
173 return Timestamp;
176 uint8_t getType() const { return Type; }
178 bool empty() const { return Symbols.empty(); }
180 void addWarning(StringRef Warning) {
181 Warnings.push_back(std::string(Warning));
183 const std::vector<std::string> &getWarnings() const { return Warnings; }
185 void print(raw_ostream &OS) const;
186 #ifndef NDEBUG
187 void dump() const;
188 #endif
190 private:
191 friend class DebugMap;
193 /// DebugMapObjects can only be constructed by the owning DebugMap.
194 DebugMapObject(StringRef ObjectFilename,
195 sys::TimePoint<std::chrono::seconds> Timestamp, uint8_t Type);
197 std::string Filename;
198 sys::TimePoint<std::chrono::seconds> Timestamp;
199 StringMap<SymbolMapping> Symbols;
200 DenseMap<uint64_t, DebugMapEntry *> AddressToMapping;
201 uint8_t Type;
203 std::vector<std::string> Warnings;
205 /// For YAMLIO support.
206 ///@{
207 friend yaml::MappingTraits<dsymutil::DebugMapObject>;
208 friend yaml::SequenceTraits<std::vector<std::unique_ptr<DebugMapObject>>>;
210 DebugMapObject() = default;
212 public:
213 DebugMapObject(DebugMapObject &&) = default;
214 DebugMapObject &operator=(DebugMapObject &&) = default;
215 ///@}
218 } // end namespace dsymutil
219 } // end namespace llvm
221 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::dsymutil::DebugMapObject::YAMLSymbolMapping)
223 namespace llvm {
224 namespace yaml {
226 using namespace llvm::dsymutil;
228 template <>
229 struct MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>> {
230 static void mapping(IO &io,
231 std::pair<std::string, DebugMapObject::SymbolMapping> &s);
232 static const bool flow = true;
235 template <> struct MappingTraits<dsymutil::DebugMapObject> {
236 struct YamlDMO;
237 static void mapping(IO &io, dsymutil::DebugMapObject &DMO);
240 template <> struct ScalarTraits<Triple> {
241 static void output(const Triple &val, void *, raw_ostream &out);
242 static StringRef input(StringRef scalar, void *, Triple &value);
243 static QuotingType mustQuote(StringRef) { return QuotingType::Single; }
246 template <>
247 struct SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>> {
248 static size_t
249 size(IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq);
250 static dsymutil::DebugMapObject &
251 element(IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
252 size_t index);
255 template <> struct MappingTraits<dsymutil::DebugMap> {
256 static void mapping(IO &io, dsymutil::DebugMap &DM);
259 template <> struct MappingTraits<std::unique_ptr<dsymutil::DebugMap>> {
260 static void mapping(IO &io, std::unique_ptr<dsymutil::DebugMap> &DM);
263 } // end namespace yaml
264 } // end namespace llvm
266 #endif // LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H