xref: /llvm-project/llvm/tools/dsymutil/RelocationMap.cpp (revision 88d00a6897d71fded96a4f806ce5ebc46fd2a0de)
1 //===- tools/dsymutil/RelocationMap.cpp - Relocation map representation---===//
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 #include "RelocationMap.h"
10 
11 namespace llvm {
12 
13 namespace dsymutil {
14 
print(raw_ostream & OS) const15 void RelocationMap::print(raw_ostream &OS) const {
16   yaml::Output yout(OS, /* Ctxt = */ nullptr, /* WrapColumn = */ 0);
17   yout << const_cast<RelocationMap &>(*this);
18 }
19 
20 #ifndef NDEBUG
dump() const21 void RelocationMap::dump() const { print(errs()); }
22 #endif
23 
addRelocationMapEntry(const ValidReloc & Relocation)24 void RelocationMap::addRelocationMapEntry(const ValidReloc &Relocation) {
25   Relocations.push_back(Relocation);
26 }
27 
28 namespace {
29 
30 struct YAMLContext {
31   StringRef PrependPath;
32   Triple BinaryTriple;
33 };
34 
35 } // end anonymous namespace
36 
37 ErrorOr<std::unique_ptr<RelocationMap>>
parseYAMLRelocationMap(StringRef InputFile,StringRef PrependPath)38 RelocationMap::parseYAMLRelocationMap(StringRef InputFile,
39                                       StringRef PrependPath) {
40   auto ErrOrFile = MemoryBuffer::getFileOrSTDIN(InputFile);
41   if (auto Err = ErrOrFile.getError())
42     return Err;
43 
44   YAMLContext Ctxt;
45 
46   Ctxt.PrependPath = PrependPath;
47 
48   std::unique_ptr<RelocationMap> Result;
49   yaml::Input yin((*ErrOrFile)->getBuffer(), &Ctxt);
50   yin >> Result;
51 
52   if (auto EC = yin.error())
53     return EC;
54   return std::move(Result);
55 }
56 
57 } // end namespace dsymutil
58 
59 namespace yaml {
60 
mapping(IO & io,dsymutil::ValidReloc & VR)61 void MappingTraits<dsymutil::ValidReloc>::mapping(IO &io,
62                                                   dsymutil::ValidReloc &VR) {
63   io.mapRequired("offset", VR.Offset);
64   io.mapRequired("size", VR.Size);
65   io.mapRequired("addend", VR.Addend);
66   io.mapRequired("symName", VR.SymbolName);
67   io.mapOptional("symObjAddr", VR.SymbolMapping.ObjectAddress);
68   io.mapRequired("symBinAddr", VR.SymbolMapping.BinaryAddress);
69   io.mapRequired("symSize", VR.SymbolMapping.Size);
70 }
71 
mapping(IO & io,dsymutil::RelocationMap & RM)72 void MappingTraits<dsymutil::RelocationMap>::mapping(
73     IO &io, dsymutil::RelocationMap &RM) {
74   io.mapRequired("triple", RM.BinaryTriple);
75   io.mapRequired("binary-path", RM.BinaryPath);
76   if (void *Ctxt = io.getContext())
77     reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = RM.BinaryTriple;
78   io.mapRequired("relocations", RM.Relocations);
79 }
80 
mapping(IO & io,std::unique_ptr<dsymutil::RelocationMap> & RM)81 void MappingTraits<std::unique_ptr<dsymutil::RelocationMap>>::mapping(
82     IO &io, std::unique_ptr<dsymutil::RelocationMap> &RM) {
83   if (!RM)
84     RM.reset(new RelocationMap());
85   io.mapRequired("triple", RM->BinaryTriple);
86   io.mapRequired("binary-path", RM->BinaryPath);
87   if (void *Ctxt = io.getContext())
88     reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = RM->BinaryTriple;
89   io.mapRequired("relocations", RM->Relocations);
90 }
91 } // end namespace yaml
92 } // end namespace llvm
93