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
11namespace llvm {
12
13namespace dsymutil {
14
15void 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
21void RelocationMap::dump() const { print(errs()); }
22#endif
23
24void RelocationMap::addDebugInfoRelocationMapEntry(
25 const ValidReloc &Relocation) {
26 DebugInfoRelocations.push_back(x: Relocation);
27}
28
29void RelocationMap::addDebugAddrRelocationMapEntry(
30 const ValidReloc &Relocation) {
31 DebugAddrRelocations.push_back(x: Relocation);
32}
33
34namespace {
35
36struct YAMLContext {
37 StringRef PrependPath;
38 Triple BinaryTriple;
39};
40
41} // end anonymous namespace
42
43ErrorOr<std::unique_ptr<RelocationMap>>
44RelocationMap::parseYAMLRelocationMap(StringRef InputFile,
45 StringRef PrependPath) {
46 auto ErrOrFile = MemoryBuffer::getFileOrSTDIN(Filename: InputFile);
47 if (auto Err = ErrOrFile.getError())
48 return Err;
49
50 YAMLContext Ctxt;
51
52 Ctxt.PrependPath = PrependPath;
53
54 std::unique_ptr<RelocationMap> Result;
55 yaml::Input yin((*ErrOrFile)->getBuffer(), &Ctxt);
56 yin >> Result;
57
58 if (auto EC = yin.error())
59 return EC;
60 return std::move(Result);
61}
62
63} // end namespace dsymutil
64
65namespace yaml {
66
67void MappingTraits<dsymutil::ValidReloc>::mapping(IO &io,
68 dsymutil::ValidReloc &VR) {
69 io.mapRequired(Key: "offset", Val&: VR.Offset);
70 io.mapRequired(Key: "size", Val&: VR.Size);
71 io.mapRequired(Key: "addend", Val&: VR.Addend);
72 io.mapRequired(Key: "symName", Val&: VR.SymbolName);
73 io.mapOptional(Key: "symObjAddr", Val&: VR.SymbolMapping.ObjectAddress);
74 io.mapRequired(Key: "symBinAddr", Val&: VR.SymbolMapping.BinaryAddress);
75 io.mapRequired(Key: "symSize", Val&: VR.SymbolMapping.Size);
76}
77
78void MappingTraits<dsymutil::RelocationMap>::mapping(
79 IO &io, dsymutil::RelocationMap &RM) {
80 io.mapRequired(Key: "triple", Val&: RM.BinaryTriple);
81 io.mapRequired(Key: "binary-path", Val&: RM.BinaryPath);
82 if (void *Ctxt = io.getContext())
83 reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = RM.BinaryTriple;
84 io.mapRequired(Key: "relocations", Val&: RM.DebugInfoRelocations);
85 io.mapOptional(Key: "debug-addr-relocations", Val&: RM.DebugAddrRelocations);
86}
87
88void MappingTraits<std::unique_ptr<dsymutil::RelocationMap>>::mapping(
89 IO &io, std::unique_ptr<dsymutil::RelocationMap> &RM) {
90 if (!RM)
91 RM.reset(p: new RelocationMap());
92 io.mapRequired(Key: "triple", Val&: RM->BinaryTriple);
93 io.mapRequired(Key: "binary-path", Val&: RM->BinaryPath);
94 if (void *Ctxt = io.getContext())
95 reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = RM->BinaryTriple;
96 io.mapRequired(Key: "relocations", Val&: RM->DebugInfoRelocations);
97 io.mapOptional(Key: "debug-addr-relocations", Val&: RM->DebugAddrRelocations);
98}
99} // end namespace yaml
100} // end namespace llvm
101