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
30namespace llvm {
31
32class raw_ostream;
33
34namespace dsymutil {
35
36struct SymbolMapping {
37 std::optional<yaml::Hex64> ObjectAddress;
38 yaml::Hex64 BinaryAddress;
39 yaml::Hex32 Size;
40
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;
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.
54class ValidReloc {
55public:
56 yaml::Hex64 Offset;
57 yaml::Hex32 Size;
58 yaml::Hex64 Addend;
59 std::string SymbolName;
60 struct SymbolMapping SymbolMapping;
61
62 struct SymbolMapping getSymbolMapping() const { return SymbolMapping; }
63
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
76class 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
91public:
92 RelocationMap(const Triple &BinaryTriple, StringRef BinaryPath)
93 : BinaryTriple(BinaryTriple), BinaryPath(std::string(BinaryPath)) {}
94
95 using const_iterator = RelocContainer::const_iterator;
96
97 iterator_range<const_iterator> relocations() const {
98 return make_range(x: begin(), y: end());
99 }
100
101 const_iterator begin() const { return Relocations.begin(); }
102
103 const_iterator end() const { return Relocations.end(); }
104
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
111 const Triple &getTriple() const { return BinaryTriple; }
112
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
129LLVM_YAML_IS_SEQUENCE_VECTOR(dsymutil::ValidReloc)
130
131namespace llvm {
132namespace yaml {
133
134using namespace llvm::dsymutil;
135
136template <> struct MappingTraits<dsymutil::ValidReloc> {
137 static void mapping(IO &io, dsymutil::ValidReloc &VR);
138 static const bool flow = true;
139};
140
141template <> struct MappingTraits<dsymutil::RelocationMap> {
142 struct YamlRM;
143 static void mapping(IO &io, dsymutil::RelocationMap &RM);
144};
145
146template <> struct MappingTraits<std::unique_ptr<dsymutil::RelocationMap>> {
147 struct YamlRM;
148 static void mapping(IO &io, std::unique_ptr<dsymutil::RelocationMap> &RM);
149};
150
151template <> 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