162 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8//9/// \file10///11/// This file contains the class declaration of the RelocationMap12/// entity. RelocationMap lists all the relocations of all the13/// atoms used in the object files linked together to14/// produce an executable.15//16//===----------------------------------------------------------------------===//17 18#ifndef LLVM_TOOLS_DSYMUTIL_RELOCATIONMAP_H19#define LLVM_TOOLS_DSYMUTIL_RELOCATIONMAP_H20 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 support50 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 binary77class RelocationMap {78 Triple BinaryTriple;79 std::string BinaryPath;80 using RelocContainer = std::vector<ValidReloc>;81 82 RelocContainer Relocations;83 84 /// For YAML IO support.85 ///@{86 friend yaml::MappingTraits<std::unique_ptr<RelocationMap>>;87 friend yaml::MappingTraits<RelocationMap>;88 89 RelocationMap() = default;90 ///@}91 92public:93 RelocationMap(const Triple &BinaryTriple, StringRef BinaryPath)94 : BinaryTriple(BinaryTriple), BinaryPath(std::string(BinaryPath)) {}95 96 using const_iterator = RelocContainer::const_iterator;97 98 iterator_range<const_iterator> relocations() const {99 return make_range(begin(), end());100 }101 102 const_iterator begin() const { return Relocations.begin(); }103 104 const_iterator end() const { return Relocations.end(); }105 106 size_t getNumberOfEntries() const { return Relocations.size(); }107 108 /// This function adds a ValidReloc to the list owned by this109 /// relocation map.110 void addRelocationMapEntry(const ValidReloc &Relocation);111 112 const Triple &getTriple() const { return BinaryTriple; }113 114 StringRef getBinaryPath() const { return BinaryPath; }115 116 void print(raw_ostream &OS) const;117 118#ifndef NDEBUG119 void dump() const;120#endif121 122 /// Read a relocation map from \a InputFile.123 static ErrorOr<std::unique_ptr<RelocationMap>>124 parseYAMLRelocationMap(StringRef InputFile, StringRef PrependPath);125};126 127} // end namespace dsymutil128} // end namespace llvm129 130LLVM_YAML_IS_SEQUENCE_VECTOR(dsymutil::ValidReloc)131 132namespace llvm {133namespace yaml {134 135using namespace llvm::dsymutil;136 137template <> struct MappingTraits<dsymutil::ValidReloc> {138 static void mapping(IO &io, dsymutil::ValidReloc &VR);139 static const bool flow = true;140};141 142template <> struct MappingTraits<dsymutil::RelocationMap> {143 struct YamlRM;144 static void mapping(IO &io, dsymutil::RelocationMap &RM);145};146 147template <> struct MappingTraits<std::unique_ptr<dsymutil::RelocationMap>> {148 struct YamlRM;149 static void mapping(IO &io, std::unique_ptr<dsymutil::RelocationMap> &RM);150};151 152template <> struct ScalarTraits<Triple> {153 static void output(const Triple &val, void *, raw_ostream &out);154 static StringRef input(StringRef scalar, void *, Triple &value);155 static QuotingType mustQuote(StringRef) { return QuotingType::Single; }156};157 158} // end namespace yaml159} // end namespace llvm160 161#endif // LLVM_TOOLS_DSYMUTIL_RELOCATIONMAP_H162