17330f729Sjoerg //===- ObjectYAML.cpp - YAML utilities for object files -------------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This file defines a wrapper class for handling tagged YAML input
107330f729Sjoerg //
117330f729Sjoerg //===----------------------------------------------------------------------===//
127330f729Sjoerg
137330f729Sjoerg #include "llvm/ObjectYAML/ObjectYAML.h"
147330f729Sjoerg #include "llvm/ADT/Twine.h"
157330f729Sjoerg #include "llvm/Support/YAMLParser.h"
167330f729Sjoerg #include "llvm/Support/YAMLTraits.h"
177330f729Sjoerg #include <string>
187330f729Sjoerg
197330f729Sjoerg using namespace llvm;
207330f729Sjoerg using namespace yaml;
217330f729Sjoerg
mapping(IO & IO,YamlObjectFile & ObjectFile)227330f729Sjoerg void MappingTraits<YamlObjectFile>::mapping(IO &IO,
237330f729Sjoerg YamlObjectFile &ObjectFile) {
247330f729Sjoerg if (IO.outputting()) {
257330f729Sjoerg if (ObjectFile.Elf)
267330f729Sjoerg MappingTraits<ELFYAML::Object>::mapping(IO, *ObjectFile.Elf);
277330f729Sjoerg if (ObjectFile.Coff)
287330f729Sjoerg MappingTraits<COFFYAML::Object>::mapping(IO, *ObjectFile.Coff);
297330f729Sjoerg if (ObjectFile.MachO)
307330f729Sjoerg MappingTraits<MachOYAML::Object>::mapping(IO, *ObjectFile.MachO);
317330f729Sjoerg if (ObjectFile.FatMachO)
327330f729Sjoerg MappingTraits<MachOYAML::UniversalBinary>::mapping(IO,
337330f729Sjoerg *ObjectFile.FatMachO);
347330f729Sjoerg } else {
357330f729Sjoerg Input &In = (Input &)IO;
36*82d56013Sjoerg if (IO.mapTag("!Arch")) {
37*82d56013Sjoerg ObjectFile.Arch.reset(new ArchYAML::Archive());
38*82d56013Sjoerg MappingTraits<ArchYAML::Archive>::mapping(IO, *ObjectFile.Arch);
39*82d56013Sjoerg std::string Err =
40*82d56013Sjoerg MappingTraits<ArchYAML::Archive>::validate(IO, *ObjectFile.Arch);
41*82d56013Sjoerg if (!Err.empty())
42*82d56013Sjoerg IO.setError(Err);
43*82d56013Sjoerg } else if (IO.mapTag("!ELF")) {
447330f729Sjoerg ObjectFile.Elf.reset(new ELFYAML::Object());
457330f729Sjoerg MappingTraits<ELFYAML::Object>::mapping(IO, *ObjectFile.Elf);
467330f729Sjoerg } else if (IO.mapTag("!COFF")) {
477330f729Sjoerg ObjectFile.Coff.reset(new COFFYAML::Object());
487330f729Sjoerg MappingTraits<COFFYAML::Object>::mapping(IO, *ObjectFile.Coff);
497330f729Sjoerg } else if (IO.mapTag("!mach-o")) {
507330f729Sjoerg ObjectFile.MachO.reset(new MachOYAML::Object());
517330f729Sjoerg MappingTraits<MachOYAML::Object>::mapping(IO, *ObjectFile.MachO);
527330f729Sjoerg } else if (IO.mapTag("!fat-mach-o")) {
537330f729Sjoerg ObjectFile.FatMachO.reset(new MachOYAML::UniversalBinary());
547330f729Sjoerg MappingTraits<MachOYAML::UniversalBinary>::mapping(IO,
557330f729Sjoerg *ObjectFile.FatMachO);
567330f729Sjoerg } else if (IO.mapTag("!minidump")) {
577330f729Sjoerg ObjectFile.Minidump.reset(new MinidumpYAML::Object());
587330f729Sjoerg MappingTraits<MinidumpYAML::Object>::mapping(IO, *ObjectFile.Minidump);
597330f729Sjoerg } else if (IO.mapTag("!WASM")) {
607330f729Sjoerg ObjectFile.Wasm.reset(new WasmYAML::Object());
617330f729Sjoerg MappingTraits<WasmYAML::Object>::mapping(IO, *ObjectFile.Wasm);
627330f729Sjoerg } else if (const Node *N = In.getCurrentNode()) {
637330f729Sjoerg if (N->getRawTag().empty())
647330f729Sjoerg IO.setError("YAML Object File missing document type tag!");
657330f729Sjoerg else
667330f729Sjoerg IO.setError("YAML Object File unsupported document type tag '" +
677330f729Sjoerg N->getRawTag() + "'!");
687330f729Sjoerg }
697330f729Sjoerg }
707330f729Sjoerg }
71