1 //===- tools/dsymutil/RelocationMap.h -------------------------- *- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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"
36 struct SymbolMapping
{
37 std::optional
<yaml::Hex64
> ObjectAddress
;
38 yaml::Hex64 BinaryAddress
;
41 SymbolMapping(std::optional
<uint64_t> ObjectAddr
, uint64_t BinaryAddress
,
43 : BinaryAddress(BinaryAddress
), Size(Size
) {
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.
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
78 std::string BinaryPath
;
79 using RelocContainer
= std::vector
<ValidReloc
>;
81 RelocContainer Relocations
;
83 /// For YAML IO support.
85 friend yaml::MappingTraits
<std::unique_ptr
<RelocationMap
>>;
86 friend yaml::MappingTraits
<RelocationMap
>;
88 RelocationMap() = default;
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
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;
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
)
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
> {
143 static void mapping(IO
&io
, dsymutil::RelocationMap
&RM
);
146 template <> struct MappingTraits
<std::unique_ptr
<dsymutil::RelocationMap
>> {
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