ObjectYAML.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #include "llvm/ObjectYAML/ObjectYAML.h"
  13. #include "llvm/ADT/Twine.h"
  14. #include "llvm/Support/YAMLParser.h"
  15. #include "llvm/Support/YAMLTraits.h"
  16. #include <string>
  17. using namespace llvm;
  18. using namespace yaml;
  19. void MappingTraits<YamlObjectFile>::mapping(IO &IO,
  20. YamlObjectFile &ObjectFile) {
  21. if (IO.outputting()) {
  22. if (ObjectFile.Elf)
  23. MappingTraits<ELFYAML::Object>::mapping(IO, *ObjectFile.Elf);
  24. if (ObjectFile.Coff)
  25. MappingTraits<COFFYAML::Object>::mapping(IO, *ObjectFile.Coff);
  26. if (ObjectFile.MachO)
  27. MappingTraits<MachOYAML::Object>::mapping(IO, *ObjectFile.MachO);
  28. if (ObjectFile.FatMachO)
  29. MappingTraits<MachOYAML::UniversalBinary>::mapping(IO,
  30. *ObjectFile.FatMachO);
  31. } else {
  32. Input &In = (Input &)IO;
  33. if (IO.mapTag("!Arch")) {
  34. ObjectFile.Arch.reset(new ArchYAML::Archive());
  35. MappingTraits<ArchYAML::Archive>::mapping(IO, *ObjectFile.Arch);
  36. std::string Err =
  37. MappingTraits<ArchYAML::Archive>::validate(IO, *ObjectFile.Arch);
  38. if (!Err.empty())
  39. IO.setError(Err);
  40. } else if (IO.mapTag("!ELF")) {
  41. ObjectFile.Elf.reset(new ELFYAML::Object());
  42. MappingTraits<ELFYAML::Object>::mapping(IO, *ObjectFile.Elf);
  43. } else if (IO.mapTag("!COFF")) {
  44. ObjectFile.Coff.reset(new COFFYAML::Object());
  45. MappingTraits<COFFYAML::Object>::mapping(IO, *ObjectFile.Coff);
  46. } else if (IO.mapTag("!mach-o")) {
  47. ObjectFile.MachO.reset(new MachOYAML::Object());
  48. MappingTraits<MachOYAML::Object>::mapping(IO, *ObjectFile.MachO);
  49. } else if (IO.mapTag("!fat-mach-o")) {
  50. ObjectFile.FatMachO.reset(new MachOYAML::UniversalBinary());
  51. MappingTraits<MachOYAML::UniversalBinary>::mapping(IO,
  52. *ObjectFile.FatMachO);
  53. } else if (IO.mapTag("!minidump")) {
  54. ObjectFile.Minidump.reset(new MinidumpYAML::Object());
  55. MappingTraits<MinidumpYAML::Object>::mapping(IO, *ObjectFile.Minidump);
  56. } else if (IO.mapTag("!WASM")) {
  57. ObjectFile.Wasm.reset(new WasmYAML::Object());
  58. MappingTraits<WasmYAML::Object>::mapping(IO, *ObjectFile.Wasm);
  59. } else if (IO.mapTag("!XCOFF")) {
  60. ObjectFile.Xcoff.reset(new XCOFFYAML::Object());
  61. MappingTraits<XCOFFYAML::Object>::mapping(IO, *ObjectFile.Xcoff);
  62. } else if (const Node *N = In.getCurrentNode()) {
  63. if (N->getRawTag().empty())
  64. IO.setError("YAML Object File missing document type tag!");
  65. else
  66. IO.setError("YAML Object File unsupported document type tag '" +
  67. N->getRawTag() + "'!");
  68. }
  69. }
  70. }