From 8c22b666fc754a3216809fe10e13e9356848b55c Mon Sep 17 00:00:00 2001 From: Chris Bieneman Date: Thu, 12 May 2016 16:04:16 +0000 Subject: [PATCH] [ObjectYAML] Support Thin MachO headers to YAML This patch adds support to ObjectYAML for serializing mach_header structs. llvm-svn: 269303 --- include/llvm/ObjectYAML/MachOYAML.h | 53 +++++++++++++++++++++++++++++ lib/ObjectYAML/CMakeLists.txt | 1 + lib/ObjectYAML/MachOYAML.cpp | 44 ++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 include/llvm/ObjectYAML/MachOYAML.h create mode 100644 lib/ObjectYAML/MachOYAML.cpp diff --git a/include/llvm/ObjectYAML/MachOYAML.h b/include/llvm/ObjectYAML/MachOYAML.h new file mode 100644 index 00000000000..bf1d274b885 --- /dev/null +++ b/include/llvm/ObjectYAML/MachOYAML.h @@ -0,0 +1,53 @@ +//===- MachOYAML.h - Mach-O YAMLIO implementation ---------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// \brief This file declares classes for handling the YAML representation +/// of Mach-O. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_OBJECTYAML_MACHOYAML_H +#define LLVM_OBJECTYAML_MACHOYAML_H + +#include "llvm/ObjectYAML/YAML.h" +#include "llvm/Support/MachO.h" + +namespace llvm { +namespace MachOYAML { + +struct FileHeader { + llvm::yaml::Hex32 cputype; + llvm::yaml::Hex32 cpusubtype; + llvm::yaml::Hex32 filetype; + uint32_t ncmds; + llvm::yaml::Hex32 flags; +}; + +struct Object { + FileHeader Header; +}; + +} // namespace llvm::MachOYAML + +namespace yaml { + +template <> struct MappingTraits { + static void mapping(IO &IO, MachOYAML::FileHeader &FileHeader); +}; + +template <> struct MappingTraits { + static void mapping(IO &IO, MachOYAML::Object &Object); +}; + +} // namespace llvm::yaml + +} // namespace llvm + +#endif diff --git a/lib/ObjectYAML/CMakeLists.txt b/lib/ObjectYAML/CMakeLists.txt index 9ebe1e30ad0..7e1363d8dac 100644 --- a/lib/ObjectYAML/CMakeLists.txt +++ b/lib/ObjectYAML/CMakeLists.txt @@ -2,4 +2,5 @@ add_llvm_library(LLVMObjectYAML YAML.cpp COFFYAML.cpp ELFYAML.cpp + MachOYAML.cpp ) diff --git a/lib/ObjectYAML/MachOYAML.cpp b/lib/ObjectYAML/MachOYAML.cpp new file mode 100644 index 00000000000..91d9583c98e --- /dev/null +++ b/lib/ObjectYAML/MachOYAML.cpp @@ -0,0 +1,44 @@ +//===- MachOYAML.cpp - MachO YAMLIO implementation ------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines classes for handling the YAML representation of MachO. +// +//===----------------------------------------------------------------------===// + +#include "llvm/ObjectYAML/MachOYAML.h" +#include "llvm/Support/Casting.h" + +namespace llvm { + +namespace yaml { + +void MappingTraits::mapping( + IO &IO, MachOYAML::FileHeader &FileHdr) { + IO.mapRequired("cputype", FileHdr.cputype); + IO.mapRequired("cpusubtype", FileHdr.cpusubtype); + IO.mapOptional("filetype", FileHdr.filetype); + IO.mapRequired("ncmds", FileHdr.ncmds); + IO.mapRequired("flags", FileHdr.flags); +} + +void MappingTraits::mapping(IO &IO, + MachOYAML::Object &Object) { + // If the context isn't already set, tag the document as !mach-o. + // For Fat files there will be a different tag so they can be differentiated. + if(!IO.getContext()) { + IO.setContext(&Object); + IO.mapTag("!mach-o", true); + } + IO.mapRequired("FileHeader", Object.Header); + IO.setContext(nullptr); +} + +} // namespace llvm::yaml + +} // namespace llvm