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 yaml::Hex8 Type;
41
42 SymbolMapping(std::optional<uint64_t> ObjectAddr, uint64_t BinaryAddress,
43 uint32_t Size)
44 : BinaryAddress(BinaryAddress), Size(Size) {
45 if (ObjectAddr)
46 ObjectAddress = *ObjectAddr;
47 }
48
49 /// For YAML IO support
50 SymbolMapping() = default;
51};
52
53/// ValidReloc represents one relocation entry described by the RelocationMap.
54/// It contains a list of DWARF relocations to apply to a linked binary.
55class ValidReloc {
56public:
57 yaml::Hex64 Offset;
58 yaml::Hex32 Size;
59 yaml::Hex64 Addend;
60 std::string SymbolName;
61 struct SymbolMapping SymbolMapping;
62
63 struct SymbolMapping getSymbolMapping() const { return SymbolMapping; }
64
65 ValidReloc(uint64_t Offset, uint32_t Size, uint64_t Addend,
66 StringRef SymbolName, struct SymbolMapping SymbolMapping)
67 : Offset(Offset), Size(Size), Addend(Addend), SymbolName(SymbolName),
68 SymbolMapping(SymbolMapping) {}
69
70 bool operator<(const ValidReloc &RHS) const { return Offset < RHS.Offset; }
71
72 /// For YAMLIO support.
73 ValidReloc() = default;
74};
75
76/// The RelocationMap object stores the list of relocation entries for a binary
77class RelocationMap {
78 Triple BinaryTriple;
79 std::string BinaryPath;
80 using RelocContainer = std::vector<ValidReloc>;
81
82 RelocContainer DebugInfoRelocations;
83 RelocContainer DebugAddrRelocations;
84
85 /// For YAML IO support.
86 ///@{
87 friend yaml::MappingTraits<std::unique_ptr<RelocationMap>>;
88 friend yaml::MappingTraits<RelocationMap>;
89
90 RelocationMap() = default;
91 ///@}
92
93public:
94 RelocationMap(const Triple &BinaryTriple, StringRef BinaryPath)
95 : BinaryTriple(BinaryTriple), BinaryPath(std::string(BinaryPath)) {}
96
97 using const_iterator = RelocContainer::const_iterator;
98
99 iterator_range<const_iterator> debugInfoRelocations() const {
100 return make_range(x: DebugInfoRelocations.begin(), y: DebugInfoRelocations.end());
101 }
102
103 iterator_range<const_iterator> debugAddrRelocations() const {
104 return make_range(x: DebugAddrRelocations.begin(), y: DebugAddrRelocations.end());
105 }
106
107 size_t getNumberOfEntries() const {
108 return DebugInfoRelocations.size() + DebugAddrRelocations.size();
109 }
110
111 void addDebugInfoRelocationMapEntry(const ValidReloc &Relocation);
112
113 void addDebugAddrRelocationMapEntry(const ValidReloc &Relocation);
114
115 const Triple &getTriple() const { return BinaryTriple; }
116
117 StringRef getBinaryPath() const { return BinaryPath; }
118
119 void print(raw_ostream &OS) const;
120
121#ifndef NDEBUG
122 void dump() const;
123#endif
124
125 /// Read a relocation map from \a InputFile.
126 static ErrorOr<std::unique_ptr<RelocationMap>>
127 parseYAMLRelocationMap(StringRef InputFile, StringRef PrependPath);
128};
129
130} // end namespace dsymutil
131} // end namespace llvm
132
133LLVM_YAML_IS_SEQUENCE_VECTOR(dsymutil::ValidReloc)
134
135namespace llvm {
136namespace yaml {
137
138using namespace llvm::dsymutil;
139
140template <> struct MappingTraits<dsymutil::ValidReloc> {
141 static void mapping(IO &io, dsymutil::ValidReloc &VR);
142 static const bool flow = true;
143};
144
145template <> struct MappingTraits<dsymutil::RelocationMap> {
146 static void mapping(IO &io, dsymutil::RelocationMap &RM);
147};
148
149template <> struct MappingTraits<std::unique_ptr<dsymutil::RelocationMap>> {
150 static void mapping(IO &io, std::unique_ptr<dsymutil::RelocationMap> &RM);
151};
152
153template <> struct ScalarTraits<Triple> {
154 static void output(const Triple &val, void *, raw_ostream &out);
155 static StringRef input(StringRef scalar, void *, Triple &value);
156 static QuotingType mustQuote(StringRef) { return QuotingType::Single; }
157};
158
159} // end namespace yaml
160} // end namespace llvm
161
162#endif // LLVM_TOOLS_DSYMUTIL_RELOCATIONMAP_H
163