xref: /freebsd/contrib/llvm-project/llvm/lib/ObjectYAML/ObjectYAML.cpp (revision e1c4c8dd8d2d10b6104f06856a77bd5b4813a801)
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, *ObjectFile.Elf);
27     if (ObjectFile.Coff)
28       MappingTraits<COFFYAML::Object>::mapping(IO, *ObjectFile.Coff);
29     if (ObjectFile.Goff)
30       MappingTraits<GOFFYAML::Object>::mapping(IO, *ObjectFile.Goff);
31     if (ObjectFile.MachO)
32       MappingTraits<MachOYAML::Object>::mapping(IO, *ObjectFile.MachO);
33     if (ObjectFile.FatMachO)
34       MappingTraits<MachOYAML::UniversalBinary>::mapping(IO,
35                                                          *ObjectFile.FatMachO);
36   } else {
37     Input &In = (Input &)IO;
38     if (IO.mapTag("!Arch")) {
39       ObjectFile.Arch.reset(new ArchYAML::Archive());
40       MappingTraits<ArchYAML::Archive>::mapping(IO, *ObjectFile.Arch);
41       std::string Err =
42           MappingTraits<ArchYAML::Archive>::validate(IO, *ObjectFile.Arch);
43       if (!Err.empty())
44         IO.setError(Err);
45     } else if (IO.mapTag("!ELF")) {
46       ObjectFile.Elf.reset(new ELFYAML::Object());
47       MappingTraits<ELFYAML::Object>::mapping(IO, *ObjectFile.Elf);
48     } else if (IO.mapTag("!COFF")) {
49       ObjectFile.Coff.reset(new COFFYAML::Object());
50       MappingTraits<COFFYAML::Object>::mapping(IO, *ObjectFile.Coff);
51     } else if (IO.mapTag("!GOFF")) {
52       ObjectFile.Goff.reset(new GOFFYAML::Object());
53       MappingTraits<GOFFYAML::Object>::mapping(IO, *ObjectFile.Goff);
54     } else if (IO.mapTag("!mach-o")) {
55       ObjectFile.MachO.reset(new MachOYAML::Object());
56       MappingTraits<MachOYAML::Object>::mapping(IO, *ObjectFile.MachO);
57     } else if (IO.mapTag("!fat-mach-o")) {
58       ObjectFile.FatMachO.reset(new MachOYAML::UniversalBinary());
59       MappingTraits<MachOYAML::UniversalBinary>::mapping(IO,
60                                                          *ObjectFile.FatMachO);
61     } else if (IO.mapTag("!minidump")) {
62       ObjectFile.Minidump.reset(new MinidumpYAML::Object());
63       MappingTraits<MinidumpYAML::Object>::mapping(IO, *ObjectFile.Minidump);
64     } else if (IO.mapTag("!Offload")) {
65       ObjectFile.Offload.reset(new OffloadYAML::Binary());
66       MappingTraits<OffloadYAML::Binary>::mapping(IO, *ObjectFile.Offload);
67     } else if (IO.mapTag("!WASM")) {
68       ObjectFile.Wasm.reset(new WasmYAML::Object());
69       MappingTraits<WasmYAML::Object>::mapping(IO, *ObjectFile.Wasm);
70     } else if (IO.mapTag("!XCOFF")) {
71       ObjectFile.Xcoff.reset(new XCOFFYAML::Object());
72       MappingTraits<XCOFFYAML::Object>::mapping(IO, *ObjectFile.Xcoff);
73     } else if (IO.mapTag("!dxcontainer")) {
74       ObjectFile.DXContainer.reset(new DXContainerYAML::Object());
75       MappingTraits<DXContainerYAML::Object>::mapping(IO,
76                                                       *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