[RISCV] Support postRA vsetvl insertion pass (#70549)
[llvm-project.git] / llvm / tools / dsymutil / RelocationMap.h
blob3d851acf2b89264609b96159e08a61a6444d6219
1 //===- tools/dsymutil/RelocationMap.h -------------------------- *- 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 RelocationMap
12 /// entity. RelocationMap lists all the relocations of all the
13 /// atoms used in the object files linked together to
14 /// produce an executable.
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_TOOLS_DSYMUTIL_RELOCATIONMAP_H
19 #define LLVM_TOOLS_DSYMUTIL_RELOCATIONMAP_H
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/iterator_range.h"
23 #include "llvm/Support/YAMLTraits.h"
24 #include "llvm/TargetParser/Triple.h"
26 #include <optional>
27 #include <string>
28 #include <vector>
30 namespace llvm {
32 class raw_ostream;
34 namespace dsymutil {
36 struct SymbolMapping {
37 std::optional<yaml::Hex64> ObjectAddress;
38 yaml::Hex64 BinaryAddress;
39 yaml::Hex32 Size;
41 SymbolMapping(std::optional<uint64_t> ObjectAddr, uint64_t BinaryAddress,
42 uint32_t Size)
43 : BinaryAddress(BinaryAddress), Size(Size) {
44 if (ObjectAddr)
45 ObjectAddress = *ObjectAddr;
48 /// For YAML IO support
49 SymbolMapping() = default;
52 /// ValidReloc represents one relocation entry described by the RelocationMap.
53 /// It contains a list of DWARF relocations to apply to a linked binary.
54 class ValidReloc {
55 public:
56 yaml::Hex64 Offset;
57 yaml::Hex32 Size;
58 yaml::Hex64 Addend;
59 std::string SymbolName;
60 struct SymbolMapping SymbolMapping;
62 struct SymbolMapping getSymbolMapping() const { return SymbolMapping; }
64 ValidReloc(uint64_t Offset, uint32_t Size, uint64_t Addend,
65 StringRef SymbolName, struct SymbolMapping SymbolMapping)
66 : Offset(Offset), Size(Size), Addend(Addend), SymbolName(SymbolName),
67 SymbolMapping(SymbolMapping) {}
69 bool operator<(const ValidReloc &RHS) const { return Offset < RHS.Offset; }
71 /// For YAMLIO support.
72 ValidReloc() = default;
75 /// The RelocationMap object stores the list of relocation entries for a binary
76 class RelocationMap {
77 Triple BinaryTriple;
78 std::string BinaryPath;
79 using RelocContainer = std::vector<ValidReloc>;
81 RelocContainer Relocations;
83 /// For YAML IO support.
84 ///@{
85 friend yaml::MappingTraits<std::unique_ptr<RelocationMap>>;
86 friend yaml::MappingTraits<RelocationMap>;
88 RelocationMap() = default;
89 ///@}
91 public:
92 RelocationMap(const Triple &BinaryTriple, StringRef BinaryPath)
93 : BinaryTriple(BinaryTriple), BinaryPath(std::string(BinaryPath)) {}
95 using const_iterator = RelocContainer::const_iterator;
97 iterator_range<const_iterator> relocations() const {
98 return make_range(begin(), end());
101 const_iterator begin() const { return Relocations.begin(); }
103 const_iterator end() const { return Relocations.end(); }
105 size_t getNumberOfEntries() const { return Relocations.size(); }
107 /// This function adds a ValidReloc to the list owned by this
108 /// relocation map.
109 void addRelocationMapEntry(const ValidReloc &Relocation);
111 const Triple &getTriple() const { return BinaryTriple; }
113 StringRef getBinaryPath() const { return BinaryPath; }
115 void print(raw_ostream &OS) const;
117 #ifndef NDEBUG
118 void dump() const;
119 #endif
121 /// Read a relocation map from \a InputFile.
122 static ErrorOr<std::unique_ptr<RelocationMap>>
123 parseYAMLRelocationMap(StringRef InputFile, StringRef PrependPath);
126 } // end namespace dsymutil
127 } // end namespace llvm
129 LLVM_YAML_IS_SEQUENCE_VECTOR(dsymutil::ValidReloc)
131 namespace llvm {
132 namespace yaml {
134 using namespace llvm::dsymutil;
136 template <> struct MappingTraits<dsymutil::ValidReloc> {
137 static void mapping(IO &io, dsymutil::ValidReloc &VR);
138 static const bool flow = true;
141 template <> struct MappingTraits<dsymutil::RelocationMap> {
142 struct YamlRM;
143 static void mapping(IO &io, dsymutil::RelocationMap &RM);
146 template <> struct MappingTraits<std::unique_ptr<dsymutil::RelocationMap>> {
147 struct YamlRM;
148 static void mapping(IO &io, std::unique_ptr<dsymutil::RelocationMap> &RM);
151 template <> struct ScalarTraits<Triple> {
152 static void output(const Triple &val, void *, raw_ostream &out);
153 static StringRef input(StringRef scalar, void *, Triple &value);
154 static QuotingType mustQuote(StringRef) { return QuotingType::Single; }
157 } // end namespace yaml
158 } // end namespace llvm
160 #endif // LLVM_TOOLS_DSYMUTIL_RELOCATIONMAP_H