| 1 | //===- ObjectYAML.cpp - YAML utilities for object files -------------------===// |
| 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 | // This file defines a wrapper class for handling tagged YAML input |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/ObjectYAML/ObjectYAML.h" |
| 14 | #include "llvm/ADT/Twine.h" |
| 15 | #include "llvm/Support/YAMLParser.h" |
| 16 | #include "llvm/Support/YAMLTraits.h" |
| 17 | #include <string> |
| 18 | |
| 19 | using namespace llvm; |
| 20 | using namespace yaml; |
| 21 | |
| 22 | void MappingTraits<YamlObjectFile>::mapping(IO &IO, |
| 23 | YamlObjectFile &ObjectFile) { |
| 24 | if (IO.outputting()) { |
| 25 | if (ObjectFile.Elf) |
| 26 | MappingTraits<ELFYAML::Object>::mapping(IO, Object&: *ObjectFile.Elf); |
| 27 | if (ObjectFile.Coff) |
| 28 | MappingTraits<COFFYAML::Object>::mapping(IO, Obj&: *ObjectFile.Coff); |
| 29 | if (ObjectFile.Goff) |
| 30 | MappingTraits<GOFFYAML::Object>::mapping(IO, Obj&: *ObjectFile.Goff); |
| 31 | if (ObjectFile.MachO) |
| 32 | MappingTraits<MachOYAML::Object>::mapping(IO, Object&: *ObjectFile.MachO); |
| 33 | if (ObjectFile.FatMachO) |
| 34 | MappingTraits<MachOYAML::UniversalBinary>::mapping(IO, |
| 35 | UniversalBinary&: *ObjectFile.FatMachO); |
| 36 | } else { |
| 37 | Input &In = (Input &)IO; |
| 38 | if (IO.mapTag(Tag: "!Arch" )) { |
| 39 | ObjectFile.Arch.reset(p: new ArchYAML::Archive()); |
| 40 | MappingTraits<ArchYAML::Archive>::mapping(IO, A&: *ObjectFile.Arch); |
| 41 | std::string Err = |
| 42 | MappingTraits<ArchYAML::Archive>::validate(IO, A&: *ObjectFile.Arch); |
| 43 | if (!Err.empty()) |
| 44 | IO.setError(Err); |
| 45 | } else if (IO.mapTag(Tag: "!ELF" )) { |
| 46 | ObjectFile.Elf.reset(p: new ELFYAML::Object()); |
| 47 | MappingTraits<ELFYAML::Object>::mapping(IO, Object&: *ObjectFile.Elf); |
| 48 | } else if (IO.mapTag(Tag: "!COFF" )) { |
| 49 | ObjectFile.Coff.reset(p: new COFFYAML::Object()); |
| 50 | MappingTraits<COFFYAML::Object>::mapping(IO, Obj&: *ObjectFile.Coff); |
| 51 | } else if (IO.mapTag(Tag: "!GOFF" )) { |
| 52 | ObjectFile.Goff.reset(p: new GOFFYAML::Object()); |
| 53 | MappingTraits<GOFFYAML::Object>::mapping(IO, Obj&: *ObjectFile.Goff); |
| 54 | } else if (IO.mapTag(Tag: "!mach-o" )) { |
| 55 | ObjectFile.MachO.reset(p: new MachOYAML::Object()); |
| 56 | MappingTraits<MachOYAML::Object>::mapping(IO, Object&: *ObjectFile.MachO); |
| 57 | } else if (IO.mapTag(Tag: "!fat-mach-o" )) { |
| 58 | ObjectFile.FatMachO.reset(p: new MachOYAML::UniversalBinary()); |
| 59 | MappingTraits<MachOYAML::UniversalBinary>::mapping(IO, |
| 60 | UniversalBinary&: *ObjectFile.FatMachO); |
| 61 | } else if (IO.mapTag(Tag: "!minidump" )) { |
| 62 | ObjectFile.Minidump.reset(p: new MinidumpYAML::Object()); |
| 63 | MappingTraits<MinidumpYAML::Object>::mapping(IO, Obj&: *ObjectFile.Minidump); |
| 64 | } else if (IO.mapTag(Tag: "!Offload" )) { |
| 65 | ObjectFile.Offload.reset(p: new OffloadYAML::Binary()); |
| 66 | MappingTraits<OffloadYAML::Binary>::mapping(IO, O&: *ObjectFile.Offload); |
| 67 | } else if (IO.mapTag(Tag: "!WASM" )) { |
| 68 | ObjectFile.Wasm.reset(p: new WasmYAML::Object()); |
| 69 | MappingTraits<WasmYAML::Object>::mapping(IO, Object&: *ObjectFile.Wasm); |
| 70 | } else if (IO.mapTag(Tag: "!XCOFF" )) { |
| 71 | ObjectFile.Xcoff.reset(p: new XCOFFYAML::Object()); |
| 72 | MappingTraits<XCOFFYAML::Object>::mapping(IO, Obj&: *ObjectFile.Xcoff); |
| 73 | } else if (IO.mapTag(Tag: "!dxcontainer" )) { |
| 74 | ObjectFile.DXContainer.reset(p: new DXContainerYAML::Object()); |
| 75 | MappingTraits<DXContainerYAML::Object>::mapping(IO, |
| 76 | Obj&: *ObjectFile.DXContainer); |
| 77 | } else if (const Node *N = In.getCurrentNode()) { |
| 78 | if (N->getRawTag().empty()) |
| 79 | IO.setError("YAML Object File missing document type tag!" ); |
| 80 | else |
| 81 | IO.setError("YAML Object File unsupported document type tag '" + |
| 82 | N->getRawTag() + "'!" ); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |