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.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_TOOLS_DSYMUTIL_RELOCATIONMAP_H
19 #define LLVM_TOOLS_DSYMUTIL_RELOCATIONMAP_H
20
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"
25
26 #include <optional>
27 #include <string>
28 #include <vector>
29
30 namespace llvm {
31
32 class raw_ostream;
33
34 namespace dsymutil {
35
36 struct SymbolMapping {
37 std::optional<yaml::Hex64> ObjectAddress;
38 yaml::Hex64 BinaryAddress;
39 yaml::Hex32 Size;
40
SymbolMappingSymbolMapping41 SymbolMapping(std::optional<uint64_t> ObjectAddr, uint64_t BinaryAddress,
42 uint32_t Size)
43 : BinaryAddress(BinaryAddress), Size(Size) {
44 if (ObjectAddr)
45 ObjectAddress = *ObjectAddr;
46 }
47
48 /// For YAML IO support
49 SymbolMapping() = default;
50 };
51
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;
61
getSymbolMapping()62 struct SymbolMapping getSymbolMapping() const { return SymbolMapping; }
63
ValidReloc(uint64_t Offset,uint32_t Size,uint64_t Addend,StringRef SymbolName,struct SymbolMapping 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) {}
68
69 bool operator<(const ValidReloc &RHS) const { return Offset < RHS.Offset; }
70
71 /// For YAMLIO support.
72 ValidReloc() = default;
73 };
74
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>;
80
81 RelocContainer Relocations;
82
83 /// For YAML IO support.
84 ///@{
85 friend yaml::MappingTraits<std::unique_ptr<RelocationMap>>;
86 friend yaml::MappingTraits<RelocationMap>;
87
88 RelocationMap() = default;
89 ///@}
90
91 public:
RelocationMap(const Triple & BinaryTriple,StringRef BinaryPath)92 RelocationMap(const Triple &BinaryTriple, StringRef BinaryPath)
93 : BinaryTriple(BinaryTriple), BinaryPath(std::string(BinaryPath)) {}
94
95 using const_iterator = RelocContainer::const_iterator;
96
relocations()97 iterator_range<const_iterator> relocations() const {
98 return make_range(begin(), end());
99 }
100
begin()101 const_iterator begin() const { return Relocations.begin(); }
102
end()103 const_iterator end() const { return Relocations.end(); }
104
getNumberOfEntries()105 size_t getNumberOfEntries() const { return Relocations.size(); }
106
107 /// This function adds a ValidReloc to the list owned by this
108 /// relocation map.
109 void addRelocationMapEntry(const ValidReloc &Relocation);
110
getTriple()111 const Triple &getTriple() const { return BinaryTriple; }
112
getBinaryPath()113 StringRef getBinaryPath() const { return BinaryPath; }
114
115 void print(raw_ostream &OS) const;
116
117 #ifndef NDEBUG
118 void dump() const;
119 #endif
120
121 /// Read a relocation map from \a InputFile.
122 static ErrorOr<std::unique_ptr<RelocationMap>>
123 parseYAMLRelocationMap(StringRef InputFile, StringRef PrependPath);
124 };
125
126 } // end namespace dsymutil
127 } // end namespace llvm
128
LLVM_YAML_IS_SEQUENCE_VECTOR(dsymutil::ValidReloc)129 LLVM_YAML_IS_SEQUENCE_VECTOR(dsymutil::ValidReloc)
130
131 namespace llvm {
132 namespace yaml {
133
134 using namespace llvm::dsymutil;
135
136 template <> struct MappingTraits<dsymutil::ValidReloc> {
137 static void mapping(IO &io, dsymutil::ValidReloc &VR);
138 static const bool flow = true;
139 };
140
141 template <> struct MappingTraits<dsymutil::RelocationMap> {
142 struct YamlRM;
143 static void mapping(IO &io, dsymutil::RelocationMap &RM);
144 };
145
146 template <> struct MappingTraits<std::unique_ptr<dsymutil::RelocationMap>> {
147 struct YamlRM;
148 static void mapping(IO &io, std::unique_ptr<dsymutil::RelocationMap> &RM);
149 };
150
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; }
155 };
156
157 } // end namespace yaml
158 } // end namespace llvm
159
160 #endif // LLVM_TOOLS_DSYMUTIL_RELOCATIONMAP_H
161