| 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 | |
| 15 | 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 |
| 21 | void RelocationMap::dump() const { print(errs()); } |
| 22 | #endif |
| 23 | |
| 24 | void RelocationMap::addRelocationMapEntry(const ValidReloc &Relocation) { |
| 25 | Relocations.push_back(x: 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>> |
| 38 | RelocationMap::parseYAMLRelocationMap(StringRef InputFile, |
| 39 | StringRef PrependPath) { |
| 40 | auto ErrOrFile = MemoryBuffer::getFileOrSTDIN(Filename: 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 | |
| 61 | void MappingTraits<dsymutil::ValidReloc>::mapping(IO &io, |
| 62 | dsymutil::ValidReloc &VR) { |
| 63 | io.mapRequired(Key: "offset" , Val&: VR.Offset); |
| 64 | io.mapRequired(Key: "size" , Val&: VR.Size); |
| 65 | io.mapRequired(Key: "addend" , Val&: VR.Addend); |
| 66 | io.mapRequired(Key: "symName" , Val&: VR.SymbolName); |
| 67 | io.mapOptional(Key: "symObjAddr" , Val&: VR.SymbolMapping.ObjectAddress); |
| 68 | io.mapRequired(Key: "symBinAddr" , Val&: VR.SymbolMapping.BinaryAddress); |
| 69 | io.mapRequired(Key: "symSize" , Val&: VR.SymbolMapping.Size); |
| 70 | } |
| 71 | |
| 72 | void MappingTraits<dsymutil::RelocationMap>::mapping( |
| 73 | IO &io, dsymutil::RelocationMap &RM) { |
| 74 | io.mapRequired(Key: "triple" , Val&: RM.BinaryTriple); |
| 75 | io.mapRequired(Key: "binary-path" , Val&: RM.BinaryPath); |
| 76 | if (void *Ctxt = io.getContext()) |
| 77 | reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = RM.BinaryTriple; |
| 78 | io.mapRequired(Key: "relocations" , Val&: RM.Relocations); |
| 79 | } |
| 80 | |
| 81 | void MappingTraits<std::unique_ptr<dsymutil::RelocationMap>>::mapping( |
| 82 | IO &io, std::unique_ptr<dsymutil::RelocationMap> &RM) { |
| 83 | if (!RM) |
| 84 | RM.reset(p: new RelocationMap()); |
| 85 | io.mapRequired(Key: "triple" , Val&: RM->BinaryTriple); |
| 86 | io.mapRequired(Key: "binary-path" , Val&: RM->BinaryPath); |
| 87 | if (void *Ctxt = io.getContext()) |
| 88 | reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = RM->BinaryTriple; |
| 89 | io.mapRequired(Key: "relocations" , Val&: RM->Relocations); |
| 90 | } |
| 91 | } // end namespace yaml |
| 92 | } // end namespace llvm |
| 93 | |