19062ab94SChris Bieneman //===- MachOYAML.cpp - MachO YAMLIO implementation ------------------------===//
29062ab94SChris Bieneman //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
69062ab94SChris Bieneman //
79062ab94SChris Bieneman //===----------------------------------------------------------------------===//
89062ab94SChris Bieneman //
99062ab94SChris Bieneman // This file defines classes for handling the YAML representation of MachO.
109062ab94SChris Bieneman //
119062ab94SChris Bieneman //===----------------------------------------------------------------------===//
129062ab94SChris Bieneman 
139062ab94SChris Bieneman #include "llvm/ObjectYAML/MachOYAML.h"
1428082ab0SEugene Zelenko #include "llvm/ADT/StringRef.h"
15264b5d9eSZachary Turner #include "llvm/BinaryFormat/MachO.h"
163f2eb836SChris Bieneman #include "llvm/Support/Format.h"
1755de3a24SChris Bieneman #include "llvm/Support/Host.h"
1828082ab0SEugene Zelenko #include "llvm/Support/YAMLTraits.h"
1928082ab0SEugene Zelenko #include "llvm/Support/raw_ostream.h"
2028082ab0SEugene Zelenko #include <cinttypes>
2128082ab0SEugene Zelenko #include <cstdint>
2228082ab0SEugene Zelenko #include <cstring>
239062ab94SChris Bieneman 
249062ab94SChris Bieneman namespace llvm {
259062ab94SChris Bieneman 
2628082ab0SEugene Zelenko MachOYAML::LoadCommand::~LoadCommand() = default;
278b5906eaSChris Bieneman 
28432ba9d8SChris Bieneman bool MachOYAML::LinkEditData::isEmpty() const {
298b058aecSChris Bieneman   return 0 ==
308b058aecSChris Bieneman          RebaseOpcodes.size() + BindOpcodes.size() + WeakBindOpcodes.size() +
318b058aecSChris Bieneman              LazyBindOpcodes.size() + ExportTrie.Children.size() +
328b058aecSChris Bieneman              NameList.size() + StringTable.size();
338b058aecSChris Bieneman }
348b058aecSChris Bieneman 
359062ab94SChris Bieneman namespace yaml {
369062ab94SChris Bieneman 
373f2eb836SChris Bieneman void ScalarTraits<char_16>::output(const char_16 &Val, void *,
3828082ab0SEugene Zelenko                                    raw_ostream &Out) {
392de17d49SChris Bieneman   auto Len = strnlen(&Val[0], 16);
402de17d49SChris Bieneman   Out << StringRef(&Val[0], Len);
413f2eb836SChris Bieneman }
423f2eb836SChris Bieneman 
433f2eb836SChris Bieneman StringRef ScalarTraits<char_16>::input(StringRef Scalar, void *, char_16 &Val) {
443f2eb836SChris Bieneman   size_t CopySize = 16 >= Scalar.size() ? 16 : Scalar.size();
453f2eb836SChris Bieneman   memcpy((void *)Val, Scalar.data(), CopySize);
463f2eb836SChris Bieneman 
473f2eb836SChris Bieneman   if (Scalar.size() < 16) {
483f2eb836SChris Bieneman     memset((void *)&Val[Scalar.size()], 0, 16 - Scalar.size());
493f2eb836SChris Bieneman   }
503f2eb836SChris Bieneman 
513f2eb836SChris Bieneman   return StringRef();
523f2eb836SChris Bieneman }
533f2eb836SChris Bieneman 
54b213b27eSFrancis Visoiu Mistrih QuotingType ScalarTraits<char_16>::mustQuote(StringRef S) {
55b213b27eSFrancis Visoiu Mistrih   return needsQuotes(S);
56b213b27eSFrancis Visoiu Mistrih }
573f2eb836SChris Bieneman 
5828082ab0SEugene Zelenko void ScalarTraits<uuid_t>::output(const uuid_t &Val, void *, raw_ostream &Out) {
593dcd1221SAdrian Prantl   Out.write_uuid(Val);
603f2eb836SChris Bieneman }
613f2eb836SChris Bieneman 
623f2eb836SChris Bieneman StringRef ScalarTraits<uuid_t>::input(StringRef Scalar, void *, uuid_t &Val) {
633f2eb836SChris Bieneman   size_t OutIdx = 0;
643f2eb836SChris Bieneman   for (size_t Idx = 0; Idx < Scalar.size(); ++Idx) {
653f2eb836SChris Bieneman     if (Scalar[Idx] == '-' || OutIdx >= 16)
663f2eb836SChris Bieneman       continue;
673f2eb836SChris Bieneman     unsigned long long TempInt;
683f2eb836SChris Bieneman     if (getAsUnsignedInteger(Scalar.slice(Idx, Idx + 2), 16, TempInt))
693f2eb836SChris Bieneman       return "invalid number";
703f2eb836SChris Bieneman     if (TempInt > 0xFF)
713f2eb836SChris Bieneman       return "out of range number";
723f2eb836SChris Bieneman     Val[OutIdx] = static_cast<uint8_t>(TempInt);
733f2eb836SChris Bieneman     ++Idx; // increment idx an extra time because we're consuming 2 chars
743f2eb836SChris Bieneman     ++OutIdx;
753f2eb836SChris Bieneman   }
763f2eb836SChris Bieneman   return StringRef();
773f2eb836SChris Bieneman }
783f2eb836SChris Bieneman 
79b213b27eSFrancis Visoiu Mistrih QuotingType ScalarTraits<uuid_t>::mustQuote(StringRef S) {
80b213b27eSFrancis Visoiu Mistrih   return needsQuotes(S);
81b213b27eSFrancis Visoiu Mistrih }
823f2eb836SChris Bieneman 
839062ab94SChris Bieneman void MappingTraits<MachOYAML::FileHeader>::mapping(
849062ab94SChris Bieneman     IO &IO, MachOYAML::FileHeader &FileHdr) {
8524f07478SChris Bieneman   IO.mapRequired("magic", FileHdr.magic);
869062ab94SChris Bieneman   IO.mapRequired("cputype", FileHdr.cputype);
879062ab94SChris Bieneman   IO.mapRequired("cpusubtype", FileHdr.cpusubtype);
88668beb2cSChris Bieneman   IO.mapRequired("filetype", FileHdr.filetype);
899062ab94SChris Bieneman   IO.mapRequired("ncmds", FileHdr.ncmds);
9024f07478SChris Bieneman   IO.mapRequired("sizeofcmds", FileHdr.sizeofcmds);
919062ab94SChris Bieneman   IO.mapRequired("flags", FileHdr.flags);
9210dcd3bdSChris Bieneman   if (FileHdr.magic == MachO::MH_MAGIC_64 ||
9310dcd3bdSChris Bieneman       FileHdr.magic == MachO::MH_CIGAM_64)
9410dcd3bdSChris Bieneman     IO.mapRequired("reserved", FileHdr.reserved);
959062ab94SChris Bieneman }
969062ab94SChris Bieneman 
979062ab94SChris Bieneman void MappingTraits<MachOYAML::Object>::mapping(IO &IO,
989062ab94SChris Bieneman                                                MachOYAML::Object &Object) {
999062ab94SChris Bieneman   // If the context isn't already set, tag the document as !mach-o.
1009062ab94SChris Bieneman   // For Fat files there will be a different tag so they can be differentiated.
1019062ab94SChris Bieneman   if (!IO.getContext()) {
1029062ab94SChris Bieneman     IO.setContext(&Object);
1039062ab94SChris Bieneman   }
10492b2e8a2SChris Bieneman   IO.mapTag("!mach-o", true);
10555de3a24SChris Bieneman   IO.mapOptional("IsLittleEndian", Object.IsLittleEndian,
10655de3a24SChris Bieneman                  sys::IsLittleEndianHost);
10755de3a24SChris Bieneman   Object.DWARF.IsLittleEndian = Object.IsLittleEndian;
10855de3a24SChris Bieneman 
1099062ab94SChris Bieneman   IO.mapRequired("FileHeader", Object.Header);
1108b5906eaSChris Bieneman   IO.mapOptional("LoadCommands", Object.LoadCommands);
111432ba9d8SChris Bieneman   if(!Object.LinkEdit.isEmpty() || !IO.outputting())
112e8e7555bSChris Bieneman     IO.mapOptional("LinkEditData", Object.LinkEdit);
11393e71193SChris Bieneman 
1148b058aecSChris Bieneman   if(!Object.DWARF.isEmpty() || !IO.outputting())
1158b058aecSChris Bieneman     IO.mapOptional("DWARF", Object.DWARF);
1168b058aecSChris Bieneman 
11793e71193SChris Bieneman   if (IO.getContext() == &Object)
11893e71193SChris Bieneman     IO.setContext(nullptr);
11993e71193SChris Bieneman }
12093e71193SChris Bieneman 
12193e71193SChris Bieneman void MappingTraits<MachOYAML::FatHeader>::mapping(
12293e71193SChris Bieneman     IO &IO, MachOYAML::FatHeader &FatHeader) {
12393e71193SChris Bieneman   IO.mapRequired("magic", FatHeader.magic);
12493e71193SChris Bieneman   IO.mapRequired("nfat_arch", FatHeader.nfat_arch);
12593e71193SChris Bieneman }
12693e71193SChris Bieneman 
12793e71193SChris Bieneman void MappingTraits<MachOYAML::FatArch>::mapping(IO &IO,
12893e71193SChris Bieneman                                                 MachOYAML::FatArch &FatArch) {
12993e71193SChris Bieneman   IO.mapRequired("cputype", FatArch.cputype);
13093e71193SChris Bieneman   IO.mapRequired("cpusubtype", FatArch.cpusubtype);
13193e71193SChris Bieneman   IO.mapRequired("offset", FatArch.offset);
13293e71193SChris Bieneman   IO.mapRequired("size", FatArch.size);
13393e71193SChris Bieneman   IO.mapRequired("align", FatArch.align);
13493e71193SChris Bieneman   IO.mapOptional("reserved", FatArch.reserved,
13593e71193SChris Bieneman                  static_cast<llvm::yaml::Hex32>(0));
13693e71193SChris Bieneman }
13793e71193SChris Bieneman 
13893e71193SChris Bieneman void MappingTraits<MachOYAML::UniversalBinary>::mapping(
13993e71193SChris Bieneman     IO &IO, MachOYAML::UniversalBinary &UniversalBinary) {
14093e71193SChris Bieneman   if (!IO.getContext()) {
14193e71193SChris Bieneman     IO.setContext(&UniversalBinary);
14293e71193SChris Bieneman     IO.mapTag("!fat-mach-o", true);
14393e71193SChris Bieneman   }
14493e71193SChris Bieneman   IO.mapRequired("FatHeader", UniversalBinary.Header);
14593e71193SChris Bieneman   IO.mapRequired("FatArchs", UniversalBinary.FatArchs);
14693e71193SChris Bieneman   IO.mapRequired("Slices", UniversalBinary.Slices);
14793e71193SChris Bieneman 
14893e71193SChris Bieneman   if (IO.getContext() == &UniversalBinary)
14993e71193SChris Bieneman     IO.setContext(nullptr);
15093e71193SChris Bieneman }
15193e71193SChris Bieneman 
152524243d6SChris Bieneman void MappingTraits<MachOYAML::LinkEditData>::mapping(
153524243d6SChris Bieneman     IO &IO, MachOYAML::LinkEditData &LinkEditData) {
154e8e7555bSChris Bieneman   IO.mapOptional("RebaseOpcodes", LinkEditData.RebaseOpcodes);
155524243d6SChris Bieneman   IO.mapOptional("BindOpcodes", LinkEditData.BindOpcodes);
156659b35a5SChris Bieneman   IO.mapOptional("WeakBindOpcodes", LinkEditData.WeakBindOpcodes);
15744474c48SChris Bieneman   IO.mapOptional("LazyBindOpcodes", LinkEditData.LazyBindOpcodes);
15828082ab0SEugene Zelenko   if (!LinkEditData.ExportTrie.Children.empty() || !IO.outputting())
15968527754SChris Bieneman     IO.mapOptional("ExportTrie", LinkEditData.ExportTrie);
16007bb3c84SChris Bieneman   IO.mapOptional("NameList", LinkEditData.NameList);
16107bb3c84SChris Bieneman   IO.mapOptional("StringTable", LinkEditData.StringTable);
162e8e7555bSChris Bieneman }
163e8e7555bSChris Bieneman 
164524243d6SChris Bieneman void MappingTraits<MachOYAML::RebaseOpcode>::mapping(
165524243d6SChris Bieneman     IO &IO, MachOYAML::RebaseOpcode &RebaseOpcode) {
166e8e7555bSChris Bieneman   IO.mapRequired("Opcode", RebaseOpcode.Opcode);
167e8e7555bSChris Bieneman   IO.mapRequired("Imm", RebaseOpcode.Imm);
168e8e7555bSChris Bieneman   IO.mapOptional("ExtraData", RebaseOpcode.ExtraData);
169e8e7555bSChris Bieneman }
170e8e7555bSChris Bieneman 
171524243d6SChris Bieneman void MappingTraits<MachOYAML::BindOpcode>::mapping(
172524243d6SChris Bieneman     IO &IO, MachOYAML::BindOpcode &BindOpcode) {
173524243d6SChris Bieneman   IO.mapRequired("Opcode", BindOpcode.Opcode);
174524243d6SChris Bieneman   IO.mapRequired("Imm", BindOpcode.Imm);
175524243d6SChris Bieneman   IO.mapOptional("ULEBExtraData", BindOpcode.ULEBExtraData);
176524243d6SChris Bieneman   IO.mapOptional("SLEBExtraData", BindOpcode.SLEBExtraData);
177524243d6SChris Bieneman   IO.mapOptional("Symbol", BindOpcode.Symbol);
178524243d6SChris Bieneman }
179524243d6SChris Bieneman 
18068527754SChris Bieneman void MappingTraits<MachOYAML::ExportEntry>::mapping(
18168527754SChris Bieneman     IO &IO, MachOYAML::ExportEntry &ExportEntry) {
18268527754SChris Bieneman   IO.mapRequired("TerminalSize", ExportEntry.TerminalSize);
18368527754SChris Bieneman   IO.mapOptional("NodeOffset", ExportEntry.NodeOffset);
18468527754SChris Bieneman   IO.mapOptional("Name", ExportEntry.Name);
18568527754SChris Bieneman   IO.mapOptional("Flags", ExportEntry.Flags);
18668527754SChris Bieneman   IO.mapOptional("Address", ExportEntry.Address);
18768527754SChris Bieneman   IO.mapOptional("Other", ExportEntry.Other);
18868527754SChris Bieneman   IO.mapOptional("ImportName", ExportEntry.ImportName);
18968527754SChris Bieneman   IO.mapOptional("Children", ExportEntry.Children);
19068527754SChris Bieneman }
19168527754SChris Bieneman 
19207bb3c84SChris Bieneman void MappingTraits<MachOYAML::NListEntry>::mapping(
19307bb3c84SChris Bieneman     IO &IO, MachOYAML::NListEntry &NListEntry) {
19407bb3c84SChris Bieneman   IO.mapRequired("n_strx", NListEntry.n_strx);
19507bb3c84SChris Bieneman   IO.mapRequired("n_type", NListEntry.n_type);
19607bb3c84SChris Bieneman   IO.mapRequired("n_sect", NListEntry.n_sect);
19707bb3c84SChris Bieneman   IO.mapRequired("n_desc", NListEntry.n_desc);
19807bb3c84SChris Bieneman   IO.mapRequired("n_value", NListEntry.n_value);
19907bb3c84SChris Bieneman }
20007bb3c84SChris Bieneman 
2019f243e9aSChris Bieneman template <typename StructType>
2029f243e9aSChris Bieneman void mapLoadCommandData(IO &IO, MachOYAML::LoadCommand &LoadCommand) {}
2039f243e9aSChris Bieneman 
2049f243e9aSChris Bieneman template <>
2059f243e9aSChris Bieneman void mapLoadCommandData<MachO::segment_command>(
2069f243e9aSChris Bieneman     IO &IO, MachOYAML::LoadCommand &LoadCommand) {
2079f243e9aSChris Bieneman   IO.mapOptional("Sections", LoadCommand.Sections);
2089f243e9aSChris Bieneman }
2099f243e9aSChris Bieneman 
2109f243e9aSChris Bieneman template <>
2119f243e9aSChris Bieneman void mapLoadCommandData<MachO::segment_command_64>(
2129f243e9aSChris Bieneman     IO &IO, MachOYAML::LoadCommand &LoadCommand) {
2139f243e9aSChris Bieneman   IO.mapOptional("Sections", LoadCommand.Sections);
2149f243e9aSChris Bieneman }
2159f243e9aSChris Bieneman 
2169f243e9aSChris Bieneman template <>
2179f243e9aSChris Bieneman void mapLoadCommandData<MachO::dylib_command>(
2189f243e9aSChris Bieneman     IO &IO, MachOYAML::LoadCommand &LoadCommand) {
2199f243e9aSChris Bieneman   IO.mapOptional("PayloadString", LoadCommand.PayloadString);
2209f243e9aSChris Bieneman }
2219f243e9aSChris Bieneman 
2229f243e9aSChris Bieneman template <>
22368527754SChris Bieneman void mapLoadCommandData<MachO::rpath_command>(
22468527754SChris Bieneman     IO &IO, MachOYAML::LoadCommand &LoadCommand) {
22568527754SChris Bieneman   IO.mapOptional("PayloadString", LoadCommand.PayloadString);
22668527754SChris Bieneman }
22768527754SChris Bieneman 
22868527754SChris Bieneman template <>
2299f243e9aSChris Bieneman void mapLoadCommandData<MachO::dylinker_command>(
2309f243e9aSChris Bieneman     IO &IO, MachOYAML::LoadCommand &LoadCommand) {
2319f243e9aSChris Bieneman   IO.mapOptional("PayloadString", LoadCommand.PayloadString);
2329f243e9aSChris Bieneman }
2339f243e9aSChris Bieneman 
2345b54a42cSSteven Wu template <>
2355b54a42cSSteven Wu void mapLoadCommandData<MachO::build_version_command>(
2365b54a42cSSteven Wu     IO &IO, MachOYAML::LoadCommand &LoadCommand) {
2375b54a42cSSteven Wu   IO.mapOptional("Tools", LoadCommand.Tools);
2385b54a42cSSteven Wu }
2395b54a42cSSteven Wu 
2403f2eb836SChris Bieneman void MappingTraits<MachOYAML::LoadCommand>::mapping(
2413f2eb836SChris Bieneman     IO &IO, MachOYAML::LoadCommand &LoadCommand) {
242606f178aSChris Bieneman   MachO::LoadCommandType TempCmd = static_cast<MachO::LoadCommandType>(
243606f178aSChris Bieneman       LoadCommand.Data.load_command_data.cmd);
244606f178aSChris Bieneman   IO.mapRequired("cmd", TempCmd);
245606f178aSChris Bieneman   LoadCommand.Data.load_command_data.cmd = TempCmd;
2463f2eb836SChris Bieneman   IO.mapRequired("cmdsize", LoadCommand.Data.load_command_data.cmdsize);
2473f2eb836SChris Bieneman 
2483f2eb836SChris Bieneman #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct)                         \
2493f2eb836SChris Bieneman   case MachO::LCName:                                                          \
2503f2eb836SChris Bieneman     MappingTraits<MachO::LCStruct>::mapping(IO,                                \
2513f2eb836SChris Bieneman                                             LoadCommand.Data.LCStruct##_data); \
2529f243e9aSChris Bieneman     mapLoadCommandData<MachO::LCStruct>(IO, LoadCommand);                      \
2533f2eb836SChris Bieneman     break;
2543f2eb836SChris Bieneman 
2553f2eb836SChris Bieneman   switch (LoadCommand.Data.load_command_data.cmd) {
256264b5d9eSZachary Turner #include "llvm/BinaryFormat/MachO.def"
2573f2eb836SChris Bieneman   }
2589f243e9aSChris Bieneman   IO.mapOptional("PayloadBytes", LoadCommand.PayloadBytes);
2599f243e9aSChris Bieneman   IO.mapOptional("ZeroPadBytes", LoadCommand.ZeroPadBytes, (uint64_t)0ull);
2603f2eb836SChris Bieneman }
2613f2eb836SChris Bieneman 
2623f2eb836SChris Bieneman void MappingTraits<MachO::dyld_info_command>::mapping(
2633f2eb836SChris Bieneman     IO &IO, MachO::dyld_info_command &LoadCommand) {
2643f2eb836SChris Bieneman   IO.mapRequired("rebase_off", LoadCommand.rebase_off);
2653f2eb836SChris Bieneman   IO.mapRequired("rebase_size", LoadCommand.rebase_size);
2662de17d49SChris Bieneman   IO.mapRequired("bind_off", LoadCommand.bind_off);
2672de17d49SChris Bieneman   IO.mapRequired("bind_size", LoadCommand.bind_size);
2683f2eb836SChris Bieneman   IO.mapRequired("weak_bind_off", LoadCommand.weak_bind_off);
2693f2eb836SChris Bieneman   IO.mapRequired("weak_bind_size", LoadCommand.weak_bind_size);
2702de17d49SChris Bieneman   IO.mapRequired("lazy_bind_off", LoadCommand.lazy_bind_off);
2712de17d49SChris Bieneman   IO.mapRequired("lazy_bind_size", LoadCommand.lazy_bind_size);
2723f2eb836SChris Bieneman   IO.mapRequired("export_off", LoadCommand.export_off);
2733f2eb836SChris Bieneman   IO.mapRequired("export_size", LoadCommand.export_size);
2743f2eb836SChris Bieneman }
2753f2eb836SChris Bieneman 
276*c19c3293SAlexander Shaposhnikov void MappingTraits<MachOYAML::Relocation>::mapping(
277*c19c3293SAlexander Shaposhnikov     IO &IO, MachOYAML::Relocation &Relocation) {
278*c19c3293SAlexander Shaposhnikov   IO.mapRequired("address", Relocation.address);
279*c19c3293SAlexander Shaposhnikov   IO.mapRequired("symbolnum", Relocation.symbolnum);
280*c19c3293SAlexander Shaposhnikov   IO.mapRequired("pcrel", Relocation.is_pcrel);
281*c19c3293SAlexander Shaposhnikov   IO.mapRequired("length", Relocation.length);
282*c19c3293SAlexander Shaposhnikov   IO.mapRequired("extern", Relocation.is_extern);
283*c19c3293SAlexander Shaposhnikov   IO.mapRequired("type", Relocation.type);
284*c19c3293SAlexander Shaposhnikov   IO.mapRequired("scattered", Relocation.is_scattered);
285*c19c3293SAlexander Shaposhnikov   IO.mapRequired("value", Relocation.value);
286*c19c3293SAlexander Shaposhnikov }
287*c19c3293SAlexander Shaposhnikov 
2882de17d49SChris Bieneman void MappingTraits<MachOYAML::Section>::mapping(IO &IO,
2892de17d49SChris Bieneman                                                 MachOYAML::Section &Section) {
2902de17d49SChris Bieneman   IO.mapRequired("sectname", Section.sectname);
2912de17d49SChris Bieneman   IO.mapRequired("segname", Section.segname);
2922de17d49SChris Bieneman   IO.mapRequired("addr", Section.addr);
2932de17d49SChris Bieneman   IO.mapRequired("size", Section.size);
2942de17d49SChris Bieneman   IO.mapRequired("offset", Section.offset);
2952de17d49SChris Bieneman   IO.mapRequired("align", Section.align);
2962de17d49SChris Bieneman   IO.mapRequired("reloff", Section.reloff);
2972de17d49SChris Bieneman   IO.mapRequired("nreloc", Section.nreloc);
2982de17d49SChris Bieneman   IO.mapRequired("flags", Section.flags);
2992de17d49SChris Bieneman   IO.mapRequired("reserved1", Section.reserved1);
3002de17d49SChris Bieneman   IO.mapRequired("reserved2", Section.reserved2);
3012de17d49SChris Bieneman   IO.mapOptional("reserved3", Section.reserved3);
30252237749SSeiya Nuta   IO.mapOptional("content", Section.content);
303*c19c3293SAlexander Shaposhnikov   IO.mapOptional("relocations", Section.relocations);
30452237749SSeiya Nuta }
30552237749SSeiya Nuta 
30652237749SSeiya Nuta StringRef
30752237749SSeiya Nuta MappingTraits<MachOYAML::Section>::validate(IO &IO,
30852237749SSeiya Nuta                                             MachOYAML::Section &Section) {
30952237749SSeiya Nuta   if (Section.content && Section.size < Section.content->binary_size())
31052237749SSeiya Nuta     return "Section size must be greater than or equal to the content size";
31152237749SSeiya Nuta   return {};
3122de17d49SChris Bieneman }
3132de17d49SChris Bieneman 
3145b54a42cSSteven Wu void MappingTraits<MachO::build_tool_version>::mapping(
3155b54a42cSSteven Wu     IO &IO, MachO::build_tool_version &tool) {
3165b54a42cSSteven Wu   IO.mapRequired("tool", tool.tool);
3175b54a42cSSteven Wu   IO.mapRequired("version", tool.version);
3185b54a42cSSteven Wu }
3195b54a42cSSteven Wu 
3203f2eb836SChris Bieneman void MappingTraits<MachO::dylib>::mapping(IO &IO, MachO::dylib &DylibStruct) {
3213f2eb836SChris Bieneman   IO.mapRequired("name", DylibStruct.name);
3223f2eb836SChris Bieneman   IO.mapRequired("timestamp", DylibStruct.timestamp);
3233f2eb836SChris Bieneman   IO.mapRequired("current_version", DylibStruct.current_version);
3243f2eb836SChris Bieneman   IO.mapRequired("compatibility_version", DylibStruct.compatibility_version);
3253f2eb836SChris Bieneman }
3263f2eb836SChris Bieneman 
3273f2eb836SChris Bieneman void MappingTraits<MachO::dylib_command>::mapping(
3283f2eb836SChris Bieneman     IO &IO, MachO::dylib_command &LoadCommand) {
3293f2eb836SChris Bieneman   IO.mapRequired("dylib", LoadCommand.dylib);
3303f2eb836SChris Bieneman }
3313f2eb836SChris Bieneman 
3323f2eb836SChris Bieneman void MappingTraits<MachO::dylinker_command>::mapping(
3333f2eb836SChris Bieneman     IO &IO, MachO::dylinker_command &LoadCommand) {
3343f2eb836SChris Bieneman   IO.mapRequired("name", LoadCommand.name);
3353f2eb836SChris Bieneman }
3363f2eb836SChris Bieneman 
3373f2eb836SChris Bieneman void MappingTraits<MachO::dysymtab_command>::mapping(
3383f2eb836SChris Bieneman     IO &IO, MachO::dysymtab_command &LoadCommand) {
3393f2eb836SChris Bieneman   IO.mapRequired("ilocalsym", LoadCommand.ilocalsym);
3403f2eb836SChris Bieneman   IO.mapRequired("nlocalsym", LoadCommand.nlocalsym);
3413f2eb836SChris Bieneman   IO.mapRequired("iextdefsym", LoadCommand.iextdefsym);
3423f2eb836SChris Bieneman   IO.mapRequired("nextdefsym", LoadCommand.nextdefsym);
3433f2eb836SChris Bieneman   IO.mapRequired("iundefsym", LoadCommand.iundefsym);
3443f2eb836SChris Bieneman   IO.mapRequired("nundefsym", LoadCommand.nundefsym);
3453f2eb836SChris Bieneman   IO.mapRequired("tocoff", LoadCommand.tocoff);
3463f2eb836SChris Bieneman   IO.mapRequired("ntoc", LoadCommand.ntoc);
3473f2eb836SChris Bieneman   IO.mapRequired("modtaboff", LoadCommand.modtaboff);
3483f2eb836SChris Bieneman   IO.mapRequired("nmodtab", LoadCommand.nmodtab);
3493f2eb836SChris Bieneman   IO.mapRequired("extrefsymoff", LoadCommand.extrefsymoff);
3503f2eb836SChris Bieneman   IO.mapRequired("nextrefsyms", LoadCommand.nextrefsyms);
3513f2eb836SChris Bieneman   IO.mapRequired("indirectsymoff", LoadCommand.indirectsymoff);
3523f2eb836SChris Bieneman   IO.mapRequired("nindirectsyms", LoadCommand.nindirectsyms);
3533f2eb836SChris Bieneman   IO.mapRequired("extreloff", LoadCommand.extreloff);
3543f2eb836SChris Bieneman   IO.mapRequired("nextrel", LoadCommand.nextrel);
3553f2eb836SChris Bieneman   IO.mapRequired("locreloff", LoadCommand.locreloff);
3563f2eb836SChris Bieneman   IO.mapRequired("nlocrel", LoadCommand.nlocrel);
3573f2eb836SChris Bieneman }
3583f2eb836SChris Bieneman 
3593f2eb836SChris Bieneman void MappingTraits<MachO::encryption_info_command>::mapping(
3603f2eb836SChris Bieneman     IO &IO, MachO::encryption_info_command &LoadCommand) {
3613f2eb836SChris Bieneman   IO.mapRequired("cryptoff", LoadCommand.cryptoff);
3623f2eb836SChris Bieneman   IO.mapRequired("cryptsize", LoadCommand.cryptsize);
3633f2eb836SChris Bieneman   IO.mapRequired("cryptid", LoadCommand.cryptid);
3643f2eb836SChris Bieneman }
3653f2eb836SChris Bieneman 
3663f2eb836SChris Bieneman void MappingTraits<MachO::encryption_info_command_64>::mapping(
3673f2eb836SChris Bieneman     IO &IO, MachO::encryption_info_command_64 &LoadCommand) {
3683f2eb836SChris Bieneman   IO.mapRequired("cryptoff", LoadCommand.cryptoff);
3693f2eb836SChris Bieneman   IO.mapRequired("cryptsize", LoadCommand.cryptsize);
3703f2eb836SChris Bieneman   IO.mapRequired("cryptid", LoadCommand.cryptid);
3713f2eb836SChris Bieneman   IO.mapRequired("pad", LoadCommand.pad);
3723f2eb836SChris Bieneman }
3733f2eb836SChris Bieneman 
3743f2eb836SChris Bieneman void MappingTraits<MachO::entry_point_command>::mapping(
3753f2eb836SChris Bieneman     IO &IO, MachO::entry_point_command &LoadCommand) {
3763f2eb836SChris Bieneman   IO.mapRequired("entryoff", LoadCommand.entryoff);
3773f2eb836SChris Bieneman   IO.mapRequired("stacksize", LoadCommand.stacksize);
3783f2eb836SChris Bieneman }
3793f2eb836SChris Bieneman 
3803f2eb836SChris Bieneman void MappingTraits<MachO::fvmfile_command>::mapping(
3813f2eb836SChris Bieneman     IO &IO, MachO::fvmfile_command &LoadCommand) {
3823f2eb836SChris Bieneman   IO.mapRequired("name", LoadCommand.name);
3833f2eb836SChris Bieneman   IO.mapRequired("header_addr", LoadCommand.header_addr);
3843f2eb836SChris Bieneman }
3853f2eb836SChris Bieneman 
3863f2eb836SChris Bieneman void MappingTraits<MachO::fvmlib>::mapping(IO &IO, MachO::fvmlib &FVMLib) {
3873f2eb836SChris Bieneman   IO.mapRequired("name", FVMLib.name);
3883f2eb836SChris Bieneman   IO.mapRequired("minor_version", FVMLib.minor_version);
3893f2eb836SChris Bieneman   IO.mapRequired("header_addr", FVMLib.header_addr);
3903f2eb836SChris Bieneman }
3913f2eb836SChris Bieneman 
3923f2eb836SChris Bieneman void MappingTraits<MachO::fvmlib_command>::mapping(
3933f2eb836SChris Bieneman     IO &IO, MachO::fvmlib_command &LoadCommand) {
3943f2eb836SChris Bieneman   IO.mapRequired("fvmlib", LoadCommand.fvmlib);
3953f2eb836SChris Bieneman }
3963f2eb836SChris Bieneman 
3973f2eb836SChris Bieneman void MappingTraits<MachO::ident_command>::mapping(
3983f2eb836SChris Bieneman     IO &IO, MachO::ident_command &LoadCommand) {}
3993f2eb836SChris Bieneman 
4003f2eb836SChris Bieneman void MappingTraits<MachO::linkedit_data_command>::mapping(
4013f2eb836SChris Bieneman     IO &IO, MachO::linkedit_data_command &LoadCommand) {
4023f2eb836SChris Bieneman   IO.mapRequired("dataoff", LoadCommand.dataoff);
4033f2eb836SChris Bieneman   IO.mapRequired("datasize", LoadCommand.datasize);
4043f2eb836SChris Bieneman }
4053f2eb836SChris Bieneman 
4063f2eb836SChris Bieneman void MappingTraits<MachO::linker_option_command>::mapping(
4073f2eb836SChris Bieneman     IO &IO, MachO::linker_option_command &LoadCommand) {
4083f2eb836SChris Bieneman   IO.mapRequired("count", LoadCommand.count);
4093f2eb836SChris Bieneman }
4103f2eb836SChris Bieneman 
4113f2eb836SChris Bieneman void MappingTraits<MachO::prebind_cksum_command>::mapping(
4123f2eb836SChris Bieneman     IO &IO, MachO::prebind_cksum_command &LoadCommand) {
4133f2eb836SChris Bieneman   IO.mapRequired("cksum", LoadCommand.cksum);
4143f2eb836SChris Bieneman }
4153f2eb836SChris Bieneman 
4163f2eb836SChris Bieneman void MappingTraits<MachO::load_command>::mapping(
4173f2eb836SChris Bieneman     IO &IO, MachO::load_command &LoadCommand) {}
4183f2eb836SChris Bieneman 
4193f2eb836SChris Bieneman void MappingTraits<MachO::prebound_dylib_command>::mapping(
4203f2eb836SChris Bieneman     IO &IO, MachO::prebound_dylib_command &LoadCommand) {
4213f2eb836SChris Bieneman   IO.mapRequired("name", LoadCommand.name);
4223f2eb836SChris Bieneman   IO.mapRequired("nmodules", LoadCommand.nmodules);
4233f2eb836SChris Bieneman   IO.mapRequired("linked_modules", LoadCommand.linked_modules);
4243f2eb836SChris Bieneman }
4253f2eb836SChris Bieneman 
4263f2eb836SChris Bieneman void MappingTraits<MachO::routines_command>::mapping(
4273f2eb836SChris Bieneman     IO &IO, MachO::routines_command &LoadCommand) {
4283f2eb836SChris Bieneman   IO.mapRequired("init_address", LoadCommand.init_address);
4293f2eb836SChris Bieneman   IO.mapRequired("init_module", LoadCommand.init_module);
4303f2eb836SChris Bieneman   IO.mapRequired("reserved1", LoadCommand.reserved1);
4313f2eb836SChris Bieneman   IO.mapRequired("reserved2", LoadCommand.reserved2);
4323f2eb836SChris Bieneman   IO.mapRequired("reserved3", LoadCommand.reserved3);
4333f2eb836SChris Bieneman   IO.mapRequired("reserved4", LoadCommand.reserved4);
4343f2eb836SChris Bieneman   IO.mapRequired("reserved5", LoadCommand.reserved5);
4353f2eb836SChris Bieneman   IO.mapRequired("reserved6", LoadCommand.reserved6);
4363f2eb836SChris Bieneman }
4373f2eb836SChris Bieneman 
4383f2eb836SChris Bieneman void MappingTraits<MachO::routines_command_64>::mapping(
4393f2eb836SChris Bieneman     IO &IO, MachO::routines_command_64 &LoadCommand) {
4403f2eb836SChris Bieneman   IO.mapRequired("init_address", LoadCommand.init_address);
4413f2eb836SChris Bieneman   IO.mapRequired("init_module", LoadCommand.init_module);
4423f2eb836SChris Bieneman   IO.mapRequired("reserved1", LoadCommand.reserved1);
4433f2eb836SChris Bieneman   IO.mapRequired("reserved2", LoadCommand.reserved2);
4443f2eb836SChris Bieneman   IO.mapRequired("reserved3", LoadCommand.reserved3);
4453f2eb836SChris Bieneman   IO.mapRequired("reserved4", LoadCommand.reserved4);
4463f2eb836SChris Bieneman   IO.mapRequired("reserved5", LoadCommand.reserved5);
4473f2eb836SChris Bieneman   IO.mapRequired("reserved6", LoadCommand.reserved6);
4483f2eb836SChris Bieneman }
4493f2eb836SChris Bieneman 
4503f2eb836SChris Bieneman void MappingTraits<MachO::rpath_command>::mapping(
4513f2eb836SChris Bieneman     IO &IO, MachO::rpath_command &LoadCommand) {
4523f2eb836SChris Bieneman   IO.mapRequired("path", LoadCommand.path);
4533f2eb836SChris Bieneman }
4543f2eb836SChris Bieneman 
4553f2eb836SChris Bieneman void MappingTraits<MachO::section>::mapping(IO &IO, MachO::section &Section) {
4563f2eb836SChris Bieneman   IO.mapRequired("sectname", Section.sectname);
4573f2eb836SChris Bieneman   IO.mapRequired("segname", Section.segname);
4583f2eb836SChris Bieneman   IO.mapRequired("addr", Section.addr);
4593f2eb836SChris Bieneman   IO.mapRequired("size", Section.size);
4603f2eb836SChris Bieneman   IO.mapRequired("offset", Section.offset);
4613f2eb836SChris Bieneman   IO.mapRequired("align", Section.align);
4623f2eb836SChris Bieneman   IO.mapRequired("reloff", Section.reloff);
4633f2eb836SChris Bieneman   IO.mapRequired("nreloc", Section.nreloc);
4643f2eb836SChris Bieneman   IO.mapRequired("flags", Section.flags);
4653f2eb836SChris Bieneman   IO.mapRequired("reserved1", Section.reserved1);
4663f2eb836SChris Bieneman   IO.mapRequired("reserved2", Section.reserved2);
4673f2eb836SChris Bieneman }
4683f2eb836SChris Bieneman 
4693f2eb836SChris Bieneman void MappingTraits<MachO::section_64>::mapping(IO &IO,
4703f2eb836SChris Bieneman                                                MachO::section_64 &Section) {
4713f2eb836SChris Bieneman   IO.mapRequired("sectname", Section.sectname);
4723f2eb836SChris Bieneman   IO.mapRequired("segname", Section.segname);
4733f2eb836SChris Bieneman   IO.mapRequired("addr", Section.addr);
4743f2eb836SChris Bieneman   IO.mapRequired("size", Section.size);
4753f2eb836SChris Bieneman   IO.mapRequired("offset", Section.offset);
4763f2eb836SChris Bieneman   IO.mapRequired("align", Section.align);
4773f2eb836SChris Bieneman   IO.mapRequired("reloff", Section.reloff);
4783f2eb836SChris Bieneman   IO.mapRequired("nreloc", Section.nreloc);
4793f2eb836SChris Bieneman   IO.mapRequired("flags", Section.flags);
4803f2eb836SChris Bieneman   IO.mapRequired("reserved1", Section.reserved1);
4813f2eb836SChris Bieneman   IO.mapRequired("reserved2", Section.reserved2);
4823f2eb836SChris Bieneman   IO.mapRequired("reserved3", Section.reserved3);
4833f2eb836SChris Bieneman }
4843f2eb836SChris Bieneman 
4853f2eb836SChris Bieneman void MappingTraits<MachO::segment_command>::mapping(
4863f2eb836SChris Bieneman     IO &IO, MachO::segment_command &LoadCommand) {
4873f2eb836SChris Bieneman   IO.mapRequired("segname", LoadCommand.segname);
4883f2eb836SChris Bieneman   IO.mapRequired("vmaddr", LoadCommand.vmaddr);
4893f2eb836SChris Bieneman   IO.mapRequired("vmsize", LoadCommand.vmsize);
4903f2eb836SChris Bieneman   IO.mapRequired("fileoff", LoadCommand.fileoff);
4913f2eb836SChris Bieneman   IO.mapRequired("filesize", LoadCommand.filesize);
4923f2eb836SChris Bieneman   IO.mapRequired("maxprot", LoadCommand.maxprot);
4933f2eb836SChris Bieneman   IO.mapRequired("initprot", LoadCommand.initprot);
4943f2eb836SChris Bieneman   IO.mapRequired("nsects", LoadCommand.nsects);
4953f2eb836SChris Bieneman   IO.mapRequired("flags", LoadCommand.flags);
4963f2eb836SChris Bieneman }
4973f2eb836SChris Bieneman 
4983f2eb836SChris Bieneman void MappingTraits<MachO::segment_command_64>::mapping(
4993f2eb836SChris Bieneman     IO &IO, MachO::segment_command_64 &LoadCommand) {
5003f2eb836SChris Bieneman   IO.mapRequired("segname", LoadCommand.segname);
5013f2eb836SChris Bieneman   IO.mapRequired("vmaddr", LoadCommand.vmaddr);
5023f2eb836SChris Bieneman   IO.mapRequired("vmsize", LoadCommand.vmsize);
5033f2eb836SChris Bieneman   IO.mapRequired("fileoff", LoadCommand.fileoff);
5043f2eb836SChris Bieneman   IO.mapRequired("filesize", LoadCommand.filesize);
5053f2eb836SChris Bieneman   IO.mapRequired("maxprot", LoadCommand.maxprot);
5063f2eb836SChris Bieneman   IO.mapRequired("initprot", LoadCommand.initprot);
5073f2eb836SChris Bieneman   IO.mapRequired("nsects", LoadCommand.nsects);
5083f2eb836SChris Bieneman   IO.mapRequired("flags", LoadCommand.flags);
5093f2eb836SChris Bieneman }
5103f2eb836SChris Bieneman 
5113f2eb836SChris Bieneman void MappingTraits<MachO::source_version_command>::mapping(
5123f2eb836SChris Bieneman     IO &IO, MachO::source_version_command &LoadCommand) {
5133f2eb836SChris Bieneman   IO.mapRequired("version", LoadCommand.version);
5143f2eb836SChris Bieneman }
5153f2eb836SChris Bieneman 
5163f2eb836SChris Bieneman void MappingTraits<MachO::sub_client_command>::mapping(
5173f2eb836SChris Bieneman     IO &IO, MachO::sub_client_command &LoadCommand) {
5183f2eb836SChris Bieneman   IO.mapRequired("client", LoadCommand.client);
5193f2eb836SChris Bieneman }
5203f2eb836SChris Bieneman 
5213f2eb836SChris Bieneman void MappingTraits<MachO::sub_framework_command>::mapping(
5223f2eb836SChris Bieneman     IO &IO, MachO::sub_framework_command &LoadCommand) {
5233f2eb836SChris Bieneman   IO.mapRequired("umbrella", LoadCommand.umbrella);
5243f2eb836SChris Bieneman }
5253f2eb836SChris Bieneman 
5263f2eb836SChris Bieneman void MappingTraits<MachO::sub_library_command>::mapping(
5273f2eb836SChris Bieneman     IO &IO, MachO::sub_library_command &LoadCommand) {
5283f2eb836SChris Bieneman   IO.mapRequired("sub_library", LoadCommand.sub_library);
5293f2eb836SChris Bieneman }
5303f2eb836SChris Bieneman 
5313f2eb836SChris Bieneman void MappingTraits<MachO::sub_umbrella_command>::mapping(
5323f2eb836SChris Bieneman     IO &IO, MachO::sub_umbrella_command &LoadCommand) {
5333f2eb836SChris Bieneman   IO.mapRequired("sub_umbrella", LoadCommand.sub_umbrella);
5343f2eb836SChris Bieneman }
5353f2eb836SChris Bieneman 
5363f2eb836SChris Bieneman void MappingTraits<MachO::symseg_command>::mapping(
5373f2eb836SChris Bieneman     IO &IO, MachO::symseg_command &LoadCommand) {
5383f2eb836SChris Bieneman   IO.mapRequired("offset", LoadCommand.offset);
5393f2eb836SChris Bieneman   IO.mapRequired("size", LoadCommand.size);
5403f2eb836SChris Bieneman }
5413f2eb836SChris Bieneman 
5423f2eb836SChris Bieneman void MappingTraits<MachO::symtab_command>::mapping(
5433f2eb836SChris Bieneman     IO &IO, MachO::symtab_command &LoadCommand) {
5443f2eb836SChris Bieneman   IO.mapRequired("symoff", LoadCommand.symoff);
5453f2eb836SChris Bieneman   IO.mapRequired("nsyms", LoadCommand.nsyms);
5463f2eb836SChris Bieneman   IO.mapRequired("stroff", LoadCommand.stroff);
5473f2eb836SChris Bieneman   IO.mapRequired("strsize", LoadCommand.strsize);
5483f2eb836SChris Bieneman }
5493f2eb836SChris Bieneman 
5503f2eb836SChris Bieneman void MappingTraits<MachO::thread_command>::mapping(
5513f2eb836SChris Bieneman     IO &IO, MachO::thread_command &LoadCommand) {}
5523f2eb836SChris Bieneman 
5533f2eb836SChris Bieneman void MappingTraits<MachO::twolevel_hints_command>::mapping(
5543f2eb836SChris Bieneman     IO &IO, MachO::twolevel_hints_command &LoadCommand) {
5553f2eb836SChris Bieneman   IO.mapRequired("offset", LoadCommand.offset);
5563f2eb836SChris Bieneman   IO.mapRequired("nhints", LoadCommand.nhints);
5573f2eb836SChris Bieneman }
5583f2eb836SChris Bieneman 
5593f2eb836SChris Bieneman void MappingTraits<MachO::uuid_command>::mapping(
5603f2eb836SChris Bieneman     IO &IO, MachO::uuid_command &LoadCommand) {
5613f2eb836SChris Bieneman   IO.mapRequired("uuid", LoadCommand.uuid);
5623f2eb836SChris Bieneman }
5633f2eb836SChris Bieneman 
5643f2eb836SChris Bieneman void MappingTraits<MachO::version_min_command>::mapping(
5653f2eb836SChris Bieneman     IO &IO, MachO::version_min_command &LoadCommand) {
5663f2eb836SChris Bieneman   IO.mapRequired("version", LoadCommand.version);
5673f2eb836SChris Bieneman   IO.mapRequired("sdk", LoadCommand.sdk);
5688b5906eaSChris Bieneman }
5698b5906eaSChris Bieneman 
570a4579c41SKevin Enderby void MappingTraits<MachO::note_command>::mapping(
571a4579c41SKevin Enderby     IO &IO, MachO::note_command &LoadCommand) {
572a4579c41SKevin Enderby   IO.mapRequired("data_owner", LoadCommand.data_owner);
573a4579c41SKevin Enderby   IO.mapRequired("offset", LoadCommand.offset);
574a4579c41SKevin Enderby   IO.mapRequired("size", LoadCommand.size);
575a4579c41SKevin Enderby }
576a4579c41SKevin Enderby 
5775b54a42cSSteven Wu void MappingTraits<MachO::build_version_command>::mapping(
5785b54a42cSSteven Wu     IO &IO, MachO::build_version_command &LoadCommand) {
5795b54a42cSSteven Wu   IO.mapRequired("platform", LoadCommand.platform);
5805b54a42cSSteven Wu   IO.mapRequired("minos", LoadCommand.minos);
5815b54a42cSSteven Wu   IO.mapRequired("sdk", LoadCommand.sdk);
5825b54a42cSSteven Wu   IO.mapRequired("ntools", LoadCommand.ntools);
5835b54a42cSSteven Wu }
5845b54a42cSSteven Wu 
58528082ab0SEugene Zelenko } // end namespace yaml
5869062ab94SChris Bieneman 
58728082ab0SEugene Zelenko } // end namespace llvm
588