xref: /freebsd-src/contrib/llvm-project/llvm/lib/ObjectYAML/MachOEmitter.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
18bcb0991SDimitry Andric //===- yaml2macho - Convert YAML to a Mach object file --------------------===//
28bcb0991SDimitry Andric //
38bcb0991SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
48bcb0991SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
58bcb0991SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
68bcb0991SDimitry Andric //
78bcb0991SDimitry Andric //===----------------------------------------------------------------------===//
88bcb0991SDimitry Andric ///
98bcb0991SDimitry Andric /// \file
108bcb0991SDimitry Andric /// The Mach component of yaml2obj.
118bcb0991SDimitry Andric ///
128bcb0991SDimitry Andric //===----------------------------------------------------------------------===//
138bcb0991SDimitry Andric 
148bcb0991SDimitry Andric #include "llvm/BinaryFormat/MachO.h"
158bcb0991SDimitry Andric #include "llvm/ObjectYAML/DWARFEmitter.h"
168bcb0991SDimitry Andric #include "llvm/ObjectYAML/ObjectYAML.h"
178bcb0991SDimitry Andric #include "llvm/ObjectYAML/yaml2obj.h"
185ffd83dbSDimitry Andric #include "llvm/Support/Errc.h"
195ffd83dbSDimitry Andric #include "llvm/Support/Error.h"
208bcb0991SDimitry Andric #include "llvm/Support/LEB128.h"
218bcb0991SDimitry Andric #include "llvm/Support/YAMLTraits.h"
228bcb0991SDimitry Andric #include "llvm/Support/raw_ostream.h"
238bcb0991SDimitry Andric 
248bcb0991SDimitry Andric #include "llvm/Support/Format.h"
258bcb0991SDimitry Andric 
268bcb0991SDimitry Andric using namespace llvm;
278bcb0991SDimitry Andric 
288bcb0991SDimitry Andric namespace {
298bcb0991SDimitry Andric 
308bcb0991SDimitry Andric class MachOWriter {
318bcb0991SDimitry Andric public:
32e8d8bef9SDimitry Andric   MachOWriter(MachOYAML::Object &Obj) : Obj(Obj), fileStart(0) {
338bcb0991SDimitry Andric     is64Bit = Obj.Header.magic == MachO::MH_MAGIC_64 ||
348bcb0991SDimitry Andric               Obj.Header.magic == MachO::MH_CIGAM_64;
358bcb0991SDimitry Andric     memset(reinterpret_cast<void *>(&Header), 0, sizeof(MachO::mach_header_64));
368bcb0991SDimitry Andric   }
378bcb0991SDimitry Andric 
385ffd83dbSDimitry Andric   Error writeMachO(raw_ostream &OS);
398bcb0991SDimitry Andric 
408bcb0991SDimitry Andric private:
418bcb0991SDimitry Andric   void writeHeader(raw_ostream &OS);
428bcb0991SDimitry Andric   void writeLoadCommands(raw_ostream &OS);
435ffd83dbSDimitry Andric   Error writeSectionData(raw_ostream &OS);
445ffd83dbSDimitry Andric   void writeRelocations(raw_ostream &OS);
458bcb0991SDimitry Andric   void writeLinkEditData(raw_ostream &OS);
468bcb0991SDimitry Andric 
478bcb0991SDimitry Andric   void writeBindOpcodes(raw_ostream &OS,
488bcb0991SDimitry Andric                         std::vector<MachOYAML::BindOpcode> &BindOpcodes);
498bcb0991SDimitry Andric   // LinkEdit writers
508bcb0991SDimitry Andric   void writeRebaseOpcodes(raw_ostream &OS);
518bcb0991SDimitry Andric   void writeBasicBindOpcodes(raw_ostream &OS);
528bcb0991SDimitry Andric   void writeWeakBindOpcodes(raw_ostream &OS);
538bcb0991SDimitry Andric   void writeLazyBindOpcodes(raw_ostream &OS);
548bcb0991SDimitry Andric   void writeNameList(raw_ostream &OS);
558bcb0991SDimitry Andric   void writeStringTable(raw_ostream &OS);
568bcb0991SDimitry Andric   void writeExportTrie(raw_ostream &OS);
578bcb0991SDimitry Andric 
588bcb0991SDimitry Andric   void dumpExportEntry(raw_ostream &OS, MachOYAML::ExportEntry &Entry);
598bcb0991SDimitry Andric   void ZeroToOffset(raw_ostream &OS, size_t offset);
608bcb0991SDimitry Andric 
618bcb0991SDimitry Andric   MachOYAML::Object &Obj;
628bcb0991SDimitry Andric   bool is64Bit;
638bcb0991SDimitry Andric   uint64_t fileStart;
648bcb0991SDimitry Andric   MachO::mach_header_64 Header;
655ffd83dbSDimitry Andric 
665ffd83dbSDimitry Andric   // Old PPC Object Files didn't have __LINKEDIT segments, the data was just
675ffd83dbSDimitry Andric   // stuck at the end of the file.
685ffd83dbSDimitry Andric   bool FoundLinkEditSeg = false;
698bcb0991SDimitry Andric };
708bcb0991SDimitry Andric 
715ffd83dbSDimitry Andric Error MachOWriter::writeMachO(raw_ostream &OS) {
728bcb0991SDimitry Andric   fileStart = OS.tell();
738bcb0991SDimitry Andric   writeHeader(OS);
748bcb0991SDimitry Andric   writeLoadCommands(OS);
755ffd83dbSDimitry Andric   if (Error Err = writeSectionData(OS))
765ffd83dbSDimitry Andric     return Err;
775ffd83dbSDimitry Andric   writeRelocations(OS);
785ffd83dbSDimitry Andric   if (!FoundLinkEditSeg)
795ffd83dbSDimitry Andric     writeLinkEditData(OS);
805ffd83dbSDimitry Andric   return Error::success();
818bcb0991SDimitry Andric }
828bcb0991SDimitry Andric 
838bcb0991SDimitry Andric void MachOWriter::writeHeader(raw_ostream &OS) {
848bcb0991SDimitry Andric   Header.magic = Obj.Header.magic;
858bcb0991SDimitry Andric   Header.cputype = Obj.Header.cputype;
868bcb0991SDimitry Andric   Header.cpusubtype = Obj.Header.cpusubtype;
878bcb0991SDimitry Andric   Header.filetype = Obj.Header.filetype;
888bcb0991SDimitry Andric   Header.ncmds = Obj.Header.ncmds;
898bcb0991SDimitry Andric   Header.sizeofcmds = Obj.Header.sizeofcmds;
908bcb0991SDimitry Andric   Header.flags = Obj.Header.flags;
918bcb0991SDimitry Andric   Header.reserved = Obj.Header.reserved;
928bcb0991SDimitry Andric 
938bcb0991SDimitry Andric   if (Obj.IsLittleEndian != sys::IsLittleEndianHost)
948bcb0991SDimitry Andric     MachO::swapStruct(Header);
958bcb0991SDimitry Andric 
968bcb0991SDimitry Andric   auto header_size =
978bcb0991SDimitry Andric       is64Bit ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header);
988bcb0991SDimitry Andric   OS.write((const char *)&Header, header_size);
998bcb0991SDimitry Andric }
1008bcb0991SDimitry Andric 
1018bcb0991SDimitry Andric template <typename SectionType>
1028bcb0991SDimitry Andric SectionType constructSection(MachOYAML::Section Sec) {
1038bcb0991SDimitry Andric   SectionType TempSec;
1048bcb0991SDimitry Andric   memcpy(reinterpret_cast<void *>(&TempSec.sectname[0]), &Sec.sectname[0], 16);
1058bcb0991SDimitry Andric   memcpy(reinterpret_cast<void *>(&TempSec.segname[0]), &Sec.segname[0], 16);
1068bcb0991SDimitry Andric   TempSec.addr = Sec.addr;
1078bcb0991SDimitry Andric   TempSec.size = Sec.size;
1088bcb0991SDimitry Andric   TempSec.offset = Sec.offset;
1098bcb0991SDimitry Andric   TempSec.align = Sec.align;
1108bcb0991SDimitry Andric   TempSec.reloff = Sec.reloff;
1118bcb0991SDimitry Andric   TempSec.nreloc = Sec.nreloc;
1128bcb0991SDimitry Andric   TempSec.flags = Sec.flags;
1138bcb0991SDimitry Andric   TempSec.reserved1 = Sec.reserved1;
1148bcb0991SDimitry Andric   TempSec.reserved2 = Sec.reserved2;
1158bcb0991SDimitry Andric   return TempSec;
1168bcb0991SDimitry Andric }
1178bcb0991SDimitry Andric 
1188bcb0991SDimitry Andric template <typename StructType>
1198bcb0991SDimitry Andric size_t writeLoadCommandData(MachOYAML::LoadCommand &LC, raw_ostream &OS,
1208bcb0991SDimitry Andric                             bool IsLittleEndian) {
1218bcb0991SDimitry Andric   return 0;
1228bcb0991SDimitry Andric }
1238bcb0991SDimitry Andric 
1248bcb0991SDimitry Andric template <>
1258bcb0991SDimitry Andric size_t writeLoadCommandData<MachO::segment_command>(MachOYAML::LoadCommand &LC,
1268bcb0991SDimitry Andric                                                     raw_ostream &OS,
1278bcb0991SDimitry Andric                                                     bool IsLittleEndian) {
1288bcb0991SDimitry Andric   size_t BytesWritten = 0;
1298bcb0991SDimitry Andric   for (const auto &Sec : LC.Sections) {
1308bcb0991SDimitry Andric     auto TempSec = constructSection<MachO::section>(Sec);
1318bcb0991SDimitry Andric     if (IsLittleEndian != sys::IsLittleEndianHost)
1328bcb0991SDimitry Andric       MachO::swapStruct(TempSec);
1338bcb0991SDimitry Andric     OS.write(reinterpret_cast<const char *>(&(TempSec)),
1348bcb0991SDimitry Andric              sizeof(MachO::section));
1358bcb0991SDimitry Andric     BytesWritten += sizeof(MachO::section);
1368bcb0991SDimitry Andric   }
1378bcb0991SDimitry Andric   return BytesWritten;
1388bcb0991SDimitry Andric }
1398bcb0991SDimitry Andric 
1408bcb0991SDimitry Andric template <>
1418bcb0991SDimitry Andric size_t writeLoadCommandData<MachO::segment_command_64>(
1428bcb0991SDimitry Andric     MachOYAML::LoadCommand &LC, raw_ostream &OS, bool IsLittleEndian) {
1438bcb0991SDimitry Andric   size_t BytesWritten = 0;
1448bcb0991SDimitry Andric   for (const auto &Sec : LC.Sections) {
1458bcb0991SDimitry Andric     auto TempSec = constructSection<MachO::section_64>(Sec);
1468bcb0991SDimitry Andric     TempSec.reserved3 = Sec.reserved3;
1478bcb0991SDimitry Andric     if (IsLittleEndian != sys::IsLittleEndianHost)
1488bcb0991SDimitry Andric       MachO::swapStruct(TempSec);
1498bcb0991SDimitry Andric     OS.write(reinterpret_cast<const char *>(&(TempSec)),
1508bcb0991SDimitry Andric              sizeof(MachO::section_64));
1518bcb0991SDimitry Andric     BytesWritten += sizeof(MachO::section_64);
1528bcb0991SDimitry Andric   }
1538bcb0991SDimitry Andric   return BytesWritten;
1548bcb0991SDimitry Andric }
1558bcb0991SDimitry Andric 
1568bcb0991SDimitry Andric size_t writePayloadString(MachOYAML::LoadCommand &LC, raw_ostream &OS) {
1578bcb0991SDimitry Andric   size_t BytesWritten = 0;
158*fe6060f1SDimitry Andric   if (!LC.Content.empty()) {
159*fe6060f1SDimitry Andric     OS.write(LC.Content.c_str(), LC.Content.length());
160*fe6060f1SDimitry Andric     BytesWritten = LC.Content.length();
1618bcb0991SDimitry Andric   }
1628bcb0991SDimitry Andric   return BytesWritten;
1638bcb0991SDimitry Andric }
1648bcb0991SDimitry Andric 
1658bcb0991SDimitry Andric template <>
1668bcb0991SDimitry Andric size_t writeLoadCommandData<MachO::dylib_command>(MachOYAML::LoadCommand &LC,
1678bcb0991SDimitry Andric                                                   raw_ostream &OS,
1688bcb0991SDimitry Andric                                                   bool IsLittleEndian) {
1698bcb0991SDimitry Andric   return writePayloadString(LC, OS);
1708bcb0991SDimitry Andric }
1718bcb0991SDimitry Andric 
1728bcb0991SDimitry Andric template <>
1738bcb0991SDimitry Andric size_t writeLoadCommandData<MachO::dylinker_command>(MachOYAML::LoadCommand &LC,
1748bcb0991SDimitry Andric                                                      raw_ostream &OS,
1758bcb0991SDimitry Andric                                                      bool IsLittleEndian) {
1768bcb0991SDimitry Andric   return writePayloadString(LC, OS);
1778bcb0991SDimitry Andric }
1788bcb0991SDimitry Andric 
1798bcb0991SDimitry Andric template <>
1808bcb0991SDimitry Andric size_t writeLoadCommandData<MachO::rpath_command>(MachOYAML::LoadCommand &LC,
1818bcb0991SDimitry Andric                                                   raw_ostream &OS,
1828bcb0991SDimitry Andric                                                   bool IsLittleEndian) {
1838bcb0991SDimitry Andric   return writePayloadString(LC, OS);
1848bcb0991SDimitry Andric }
1858bcb0991SDimitry Andric 
1868bcb0991SDimitry Andric template <>
1878bcb0991SDimitry Andric size_t writeLoadCommandData<MachO::build_version_command>(
1888bcb0991SDimitry Andric     MachOYAML::LoadCommand &LC, raw_ostream &OS, bool IsLittleEndian) {
1898bcb0991SDimitry Andric   size_t BytesWritten = 0;
1908bcb0991SDimitry Andric   for (const auto &T : LC.Tools) {
1918bcb0991SDimitry Andric     struct MachO::build_tool_version tool = T;
1928bcb0991SDimitry Andric     if (IsLittleEndian != sys::IsLittleEndianHost)
1938bcb0991SDimitry Andric       MachO::swapStruct(tool);
1948bcb0991SDimitry Andric     OS.write(reinterpret_cast<const char *>(&tool),
1958bcb0991SDimitry Andric              sizeof(MachO::build_tool_version));
1968bcb0991SDimitry Andric     BytesWritten += sizeof(MachO::build_tool_version);
1978bcb0991SDimitry Andric   }
1988bcb0991SDimitry Andric   return BytesWritten;
1998bcb0991SDimitry Andric }
2008bcb0991SDimitry Andric 
2018bcb0991SDimitry Andric void ZeroFillBytes(raw_ostream &OS, size_t Size) {
202e8d8bef9SDimitry Andric   std::vector<uint8_t> FillData(Size, 0);
2038bcb0991SDimitry Andric   OS.write(reinterpret_cast<char *>(FillData.data()), Size);
2048bcb0991SDimitry Andric }
2058bcb0991SDimitry Andric 
2068bcb0991SDimitry Andric void Fill(raw_ostream &OS, size_t Size, uint32_t Data) {
207e8d8bef9SDimitry Andric   std::vector<uint32_t> FillData((Size / 4) + 1, Data);
2088bcb0991SDimitry Andric   OS.write(reinterpret_cast<char *>(FillData.data()), Size);
2098bcb0991SDimitry Andric }
2108bcb0991SDimitry Andric 
2118bcb0991SDimitry Andric void MachOWriter::ZeroToOffset(raw_ostream &OS, size_t Offset) {
2128bcb0991SDimitry Andric   auto currOffset = OS.tell() - fileStart;
2138bcb0991SDimitry Andric   if (currOffset < Offset)
2148bcb0991SDimitry Andric     ZeroFillBytes(OS, Offset - currOffset);
2158bcb0991SDimitry Andric }
2168bcb0991SDimitry Andric 
2178bcb0991SDimitry Andric void MachOWriter::writeLoadCommands(raw_ostream &OS) {
2188bcb0991SDimitry Andric   for (auto &LC : Obj.LoadCommands) {
2198bcb0991SDimitry Andric     size_t BytesWritten = 0;
2208bcb0991SDimitry Andric     llvm::MachO::macho_load_command Data = LC.Data;
2218bcb0991SDimitry Andric 
2228bcb0991SDimitry Andric #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct)                         \
2238bcb0991SDimitry Andric   case MachO::LCName:                                                          \
2248bcb0991SDimitry Andric     if (Obj.IsLittleEndian != sys::IsLittleEndianHost)                         \
2258bcb0991SDimitry Andric       MachO::swapStruct(Data.LCStruct##_data);                                 \
2268bcb0991SDimitry Andric     OS.write(reinterpret_cast<const char *>(&(Data.LCStruct##_data)),          \
2278bcb0991SDimitry Andric              sizeof(MachO::LCStruct));                                         \
2288bcb0991SDimitry Andric     BytesWritten = sizeof(MachO::LCStruct);                                    \
2298bcb0991SDimitry Andric     BytesWritten +=                                                            \
2308bcb0991SDimitry Andric         writeLoadCommandData<MachO::LCStruct>(LC, OS, Obj.IsLittleEndian);     \
2318bcb0991SDimitry Andric     break;
2328bcb0991SDimitry Andric 
2338bcb0991SDimitry Andric     switch (LC.Data.load_command_data.cmd) {
2348bcb0991SDimitry Andric     default:
2358bcb0991SDimitry Andric       if (Obj.IsLittleEndian != sys::IsLittleEndianHost)
2368bcb0991SDimitry Andric         MachO::swapStruct(Data.load_command_data);
2378bcb0991SDimitry Andric       OS.write(reinterpret_cast<const char *>(&(Data.load_command_data)),
2388bcb0991SDimitry Andric                sizeof(MachO::load_command));
2398bcb0991SDimitry Andric       BytesWritten = sizeof(MachO::load_command);
2408bcb0991SDimitry Andric       BytesWritten +=
2418bcb0991SDimitry Andric           writeLoadCommandData<MachO::load_command>(LC, OS, Obj.IsLittleEndian);
2428bcb0991SDimitry Andric       break;
2438bcb0991SDimitry Andric #include "llvm/BinaryFormat/MachO.def"
2448bcb0991SDimitry Andric     }
2458bcb0991SDimitry Andric 
2468bcb0991SDimitry Andric     if (LC.PayloadBytes.size() > 0) {
2478bcb0991SDimitry Andric       OS.write(reinterpret_cast<const char *>(LC.PayloadBytes.data()),
2488bcb0991SDimitry Andric                LC.PayloadBytes.size());
2498bcb0991SDimitry Andric       BytesWritten += LC.PayloadBytes.size();
2508bcb0991SDimitry Andric     }
2518bcb0991SDimitry Andric 
2528bcb0991SDimitry Andric     if (LC.ZeroPadBytes > 0) {
2538bcb0991SDimitry Andric       ZeroFillBytes(OS, LC.ZeroPadBytes);
2548bcb0991SDimitry Andric       BytesWritten += LC.ZeroPadBytes;
2558bcb0991SDimitry Andric     }
2568bcb0991SDimitry Andric 
2578bcb0991SDimitry Andric     // Fill remaining bytes with 0. This will only get hit in partially
2588bcb0991SDimitry Andric     // specified test cases.
2598bcb0991SDimitry Andric     auto BytesRemaining = LC.Data.load_command_data.cmdsize - BytesWritten;
2608bcb0991SDimitry Andric     if (BytesRemaining > 0) {
2618bcb0991SDimitry Andric       ZeroFillBytes(OS, BytesRemaining);
2628bcb0991SDimitry Andric     }
2638bcb0991SDimitry Andric   }
2648bcb0991SDimitry Andric }
2658bcb0991SDimitry Andric 
2665ffd83dbSDimitry Andric Error MachOWriter::writeSectionData(raw_ostream &OS) {
2678bcb0991SDimitry Andric   for (auto &LC : Obj.LoadCommands) {
2688bcb0991SDimitry Andric     switch (LC.Data.load_command_data.cmd) {
2698bcb0991SDimitry Andric     case MachO::LC_SEGMENT:
2708bcb0991SDimitry Andric     case MachO::LC_SEGMENT_64:
2718bcb0991SDimitry Andric       uint64_t segOff = is64Bit ? LC.Data.segment_command_64_data.fileoff
2728bcb0991SDimitry Andric                                 : LC.Data.segment_command_data.fileoff;
2738bcb0991SDimitry Andric       if (0 ==
2748bcb0991SDimitry Andric           strncmp(&LC.Data.segment_command_data.segname[0], "__LINKEDIT", 16)) {
2758bcb0991SDimitry Andric         FoundLinkEditSeg = true;
2768bcb0991SDimitry Andric         writeLinkEditData(OS);
2778bcb0991SDimitry Andric       }
2788bcb0991SDimitry Andric       for (auto &Sec : LC.Sections) {
2798bcb0991SDimitry Andric         ZeroToOffset(OS, Sec.offset);
2808bcb0991SDimitry Andric         // Zero Fill any data between the end of the last thing we wrote and the
2818bcb0991SDimitry Andric         // start of this section.
2825ffd83dbSDimitry Andric         if (OS.tell() - fileStart > Sec.offset && Sec.offset != (uint32_t)0)
2835ffd83dbSDimitry Andric           return createStringError(
2845ffd83dbSDimitry Andric               errc::invalid_argument,
2855ffd83dbSDimitry Andric               "wrote too much data somewhere, section offsets don't line up");
2865ffd83dbSDimitry Andric 
287e8d8bef9SDimitry Andric         StringRef SectName(Sec.sectname,
288e8d8bef9SDimitry Andric                            strnlen(Sec.sectname, sizeof(Sec.sectname)));
289e8d8bef9SDimitry Andric         // If the section's content is specified in the 'DWARF' entry, we will
290e8d8bef9SDimitry Andric         // emit it regardless of the section's segname.
291e8d8bef9SDimitry Andric         if (Obj.DWARF.getNonEmptySectionNames().count(SectName.substr(2))) {
292e8d8bef9SDimitry Andric           if (Sec.content)
293e8d8bef9SDimitry Andric             return createStringError(errc::invalid_argument,
294e8d8bef9SDimitry Andric                                      "cannot specify section '" + SectName +
295e8d8bef9SDimitry Andric                                          "' contents in the 'DWARF' entry and "
296e8d8bef9SDimitry Andric                                          "the 'content' at the same time");
297e8d8bef9SDimitry Andric           auto EmitFunc = DWARFYAML::getDWARFEmitterByName(SectName.substr(2));
298e8d8bef9SDimitry Andric           if (Error Err = EmitFunc(OS, Obj.DWARF))
2995ffd83dbSDimitry Andric             return Err;
3008bcb0991SDimitry Andric           continue;
3018bcb0991SDimitry Andric         }
3028bcb0991SDimitry Andric 
3038bcb0991SDimitry Andric         // Skip if it's a virtual section.
3048bcb0991SDimitry Andric         if (MachO::isVirtualSection(Sec.flags & MachO::SECTION_TYPE))
3058bcb0991SDimitry Andric           continue;
3068bcb0991SDimitry Andric 
3078bcb0991SDimitry Andric         if (Sec.content) {
3088bcb0991SDimitry Andric           yaml::BinaryRef Content = *Sec.content;
3098bcb0991SDimitry Andric           Content.writeAsBinary(OS);
3108bcb0991SDimitry Andric           ZeroFillBytes(OS, Sec.size - Content.binary_size());
3118bcb0991SDimitry Andric         } else {
3128bcb0991SDimitry Andric           // Fill section data with 0xDEADBEEF.
3138bcb0991SDimitry Andric           Fill(OS, Sec.size, 0xDEADBEEFu);
3148bcb0991SDimitry Andric         }
3158bcb0991SDimitry Andric       }
3168bcb0991SDimitry Andric       uint64_t segSize = is64Bit ? LC.Data.segment_command_64_data.filesize
3178bcb0991SDimitry Andric                                  : LC.Data.segment_command_data.filesize;
3188bcb0991SDimitry Andric       ZeroToOffset(OS, segOff + segSize);
3198bcb0991SDimitry Andric       break;
3208bcb0991SDimitry Andric     }
3218bcb0991SDimitry Andric   }
3225ffd83dbSDimitry Andric 
3235ffd83dbSDimitry Andric   return Error::success();
3245ffd83dbSDimitry Andric }
3255ffd83dbSDimitry Andric 
3265ffd83dbSDimitry Andric // The implementation of makeRelocationInfo and makeScatteredRelocationInfo is
3275ffd83dbSDimitry Andric // consistent with how libObject parses MachO binary files. For the reference
3285ffd83dbSDimitry Andric // see getStruct, getRelocation, getPlainRelocationPCRel,
3295ffd83dbSDimitry Andric // getPlainRelocationLength and related methods in MachOObjectFile.cpp
3305ffd83dbSDimitry Andric static MachO::any_relocation_info
3315ffd83dbSDimitry Andric makeRelocationInfo(const MachOYAML::Relocation &R, bool IsLE) {
3325ffd83dbSDimitry Andric   assert(!R.is_scattered && "non-scattered relocation expected");
3335ffd83dbSDimitry Andric   MachO::any_relocation_info MRE;
3345ffd83dbSDimitry Andric   MRE.r_word0 = R.address;
3355ffd83dbSDimitry Andric   if (IsLE)
3365ffd83dbSDimitry Andric     MRE.r_word1 = ((unsigned)R.symbolnum << 0) | ((unsigned)R.is_pcrel << 24) |
3375ffd83dbSDimitry Andric                   ((unsigned)R.length << 25) | ((unsigned)R.is_extern << 27) |
3385ffd83dbSDimitry Andric                   ((unsigned)R.type << 28);
3395ffd83dbSDimitry Andric   else
3405ffd83dbSDimitry Andric     MRE.r_word1 = ((unsigned)R.symbolnum << 8) | ((unsigned)R.is_pcrel << 7) |
3415ffd83dbSDimitry Andric                   ((unsigned)R.length << 5) | ((unsigned)R.is_extern << 4) |
3425ffd83dbSDimitry Andric                   ((unsigned)R.type << 0);
3435ffd83dbSDimitry Andric   return MRE;
3445ffd83dbSDimitry Andric }
3455ffd83dbSDimitry Andric 
3465ffd83dbSDimitry Andric static MachO::any_relocation_info
3475ffd83dbSDimitry Andric makeScatteredRelocationInfo(const MachOYAML::Relocation &R) {
3485ffd83dbSDimitry Andric   assert(R.is_scattered && "scattered relocation expected");
3495ffd83dbSDimitry Andric   MachO::any_relocation_info MRE;
3505ffd83dbSDimitry Andric   MRE.r_word0 = (((unsigned)R.address << 0) | ((unsigned)R.type << 24) |
3515ffd83dbSDimitry Andric                  ((unsigned)R.length << 28) | ((unsigned)R.is_pcrel << 30) |
3525ffd83dbSDimitry Andric                  MachO::R_SCATTERED);
3535ffd83dbSDimitry Andric   MRE.r_word1 = R.value;
3545ffd83dbSDimitry Andric   return MRE;
3555ffd83dbSDimitry Andric }
3565ffd83dbSDimitry Andric 
3575ffd83dbSDimitry Andric void MachOWriter::writeRelocations(raw_ostream &OS) {
3585ffd83dbSDimitry Andric   for (const MachOYAML::LoadCommand &LC : Obj.LoadCommands) {
3595ffd83dbSDimitry Andric     switch (LC.Data.load_command_data.cmd) {
3605ffd83dbSDimitry Andric     case MachO::LC_SEGMENT:
3615ffd83dbSDimitry Andric     case MachO::LC_SEGMENT_64:
3625ffd83dbSDimitry Andric       for (const MachOYAML::Section &Sec : LC.Sections) {
3635ffd83dbSDimitry Andric         if (Sec.relocations.empty())
3645ffd83dbSDimitry Andric           continue;
3655ffd83dbSDimitry Andric         ZeroToOffset(OS, Sec.reloff);
3665ffd83dbSDimitry Andric         for (const MachOYAML::Relocation &R : Sec.relocations) {
3675ffd83dbSDimitry Andric           MachO::any_relocation_info MRE =
3685ffd83dbSDimitry Andric               R.is_scattered ? makeScatteredRelocationInfo(R)
3695ffd83dbSDimitry Andric                              : makeRelocationInfo(R, Obj.IsLittleEndian);
3705ffd83dbSDimitry Andric           if (Obj.IsLittleEndian != sys::IsLittleEndianHost)
3715ffd83dbSDimitry Andric             MachO::swapStruct(MRE);
3725ffd83dbSDimitry Andric           OS.write(reinterpret_cast<const char *>(&MRE),
3735ffd83dbSDimitry Andric                    sizeof(MachO::any_relocation_info));
3745ffd83dbSDimitry Andric         }
3755ffd83dbSDimitry Andric       }
3765ffd83dbSDimitry Andric     }
3775ffd83dbSDimitry Andric   }
3788bcb0991SDimitry Andric }
3798bcb0991SDimitry Andric 
3808bcb0991SDimitry Andric void MachOWriter::writeBindOpcodes(
3818bcb0991SDimitry Andric     raw_ostream &OS, std::vector<MachOYAML::BindOpcode> &BindOpcodes) {
3828bcb0991SDimitry Andric 
3838bcb0991SDimitry Andric   for (auto Opcode : BindOpcodes) {
3848bcb0991SDimitry Andric     uint8_t OpByte = Opcode.Opcode | Opcode.Imm;
3858bcb0991SDimitry Andric     OS.write(reinterpret_cast<char *>(&OpByte), 1);
3868bcb0991SDimitry Andric     for (auto Data : Opcode.ULEBExtraData) {
3878bcb0991SDimitry Andric       encodeULEB128(Data, OS);
3888bcb0991SDimitry Andric     }
3898bcb0991SDimitry Andric     for (auto Data : Opcode.SLEBExtraData) {
3908bcb0991SDimitry Andric       encodeSLEB128(Data, OS);
3918bcb0991SDimitry Andric     }
3928bcb0991SDimitry Andric     if (!Opcode.Symbol.empty()) {
3938bcb0991SDimitry Andric       OS.write(Opcode.Symbol.data(), Opcode.Symbol.size());
3948bcb0991SDimitry Andric       OS.write('\0');
3958bcb0991SDimitry Andric     }
3968bcb0991SDimitry Andric   }
3978bcb0991SDimitry Andric }
3988bcb0991SDimitry Andric 
3998bcb0991SDimitry Andric void MachOWriter::dumpExportEntry(raw_ostream &OS,
4008bcb0991SDimitry Andric                                   MachOYAML::ExportEntry &Entry) {
4018bcb0991SDimitry Andric   encodeSLEB128(Entry.TerminalSize, OS);
4028bcb0991SDimitry Andric   if (Entry.TerminalSize > 0) {
4038bcb0991SDimitry Andric     encodeSLEB128(Entry.Flags, OS);
4048bcb0991SDimitry Andric     if (Entry.Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT) {
4058bcb0991SDimitry Andric       encodeSLEB128(Entry.Other, OS);
4068bcb0991SDimitry Andric       OS << Entry.ImportName;
4078bcb0991SDimitry Andric       OS.write('\0');
4088bcb0991SDimitry Andric     } else {
4098bcb0991SDimitry Andric       encodeSLEB128(Entry.Address, OS);
4108bcb0991SDimitry Andric       if (Entry.Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER)
4118bcb0991SDimitry Andric         encodeSLEB128(Entry.Other, OS);
4128bcb0991SDimitry Andric     }
4138bcb0991SDimitry Andric   }
4148bcb0991SDimitry Andric   OS.write(static_cast<uint8_t>(Entry.Children.size()));
4158bcb0991SDimitry Andric   for (auto EE : Entry.Children) {
4168bcb0991SDimitry Andric     OS << EE.Name;
4178bcb0991SDimitry Andric     OS.write('\0');
4188bcb0991SDimitry Andric     encodeSLEB128(EE.NodeOffset, OS);
4198bcb0991SDimitry Andric   }
4208bcb0991SDimitry Andric   for (auto EE : Entry.Children)
4218bcb0991SDimitry Andric     dumpExportEntry(OS, EE);
4228bcb0991SDimitry Andric }
4238bcb0991SDimitry Andric 
4248bcb0991SDimitry Andric void MachOWriter::writeExportTrie(raw_ostream &OS) {
4258bcb0991SDimitry Andric   dumpExportEntry(OS, Obj.LinkEdit.ExportTrie);
4268bcb0991SDimitry Andric }
4278bcb0991SDimitry Andric 
4288bcb0991SDimitry Andric template <typename NListType>
4298bcb0991SDimitry Andric void writeNListEntry(MachOYAML::NListEntry &NLE, raw_ostream &OS,
4308bcb0991SDimitry Andric                      bool IsLittleEndian) {
4318bcb0991SDimitry Andric   NListType ListEntry;
4328bcb0991SDimitry Andric   ListEntry.n_strx = NLE.n_strx;
4338bcb0991SDimitry Andric   ListEntry.n_type = NLE.n_type;
4348bcb0991SDimitry Andric   ListEntry.n_sect = NLE.n_sect;
4358bcb0991SDimitry Andric   ListEntry.n_desc = NLE.n_desc;
4368bcb0991SDimitry Andric   ListEntry.n_value = NLE.n_value;
4378bcb0991SDimitry Andric 
4388bcb0991SDimitry Andric   if (IsLittleEndian != sys::IsLittleEndianHost)
4398bcb0991SDimitry Andric     MachO::swapStruct(ListEntry);
4408bcb0991SDimitry Andric   OS.write(reinterpret_cast<const char *>(&ListEntry), sizeof(NListType));
4418bcb0991SDimitry Andric }
4428bcb0991SDimitry Andric 
4438bcb0991SDimitry Andric void MachOWriter::writeLinkEditData(raw_ostream &OS) {
4448bcb0991SDimitry Andric   typedef void (MachOWriter::*writeHandler)(raw_ostream &);
4458bcb0991SDimitry Andric   typedef std::pair<uint64_t, writeHandler> writeOperation;
4468bcb0991SDimitry Andric   std::vector<writeOperation> WriteQueue;
4478bcb0991SDimitry Andric 
4488bcb0991SDimitry Andric   MachO::dyld_info_command *DyldInfoOnlyCmd = 0;
4498bcb0991SDimitry Andric   MachO::symtab_command *SymtabCmd = 0;
4508bcb0991SDimitry Andric   for (auto &LC : Obj.LoadCommands) {
4518bcb0991SDimitry Andric     switch (LC.Data.load_command_data.cmd) {
4528bcb0991SDimitry Andric     case MachO::LC_SYMTAB:
4538bcb0991SDimitry Andric       SymtabCmd = &LC.Data.symtab_command_data;
4548bcb0991SDimitry Andric       WriteQueue.push_back(
4558bcb0991SDimitry Andric           std::make_pair(SymtabCmd->symoff, &MachOWriter::writeNameList));
4568bcb0991SDimitry Andric       WriteQueue.push_back(
4578bcb0991SDimitry Andric           std::make_pair(SymtabCmd->stroff, &MachOWriter::writeStringTable));
4588bcb0991SDimitry Andric       break;
4598bcb0991SDimitry Andric     case MachO::LC_DYLD_INFO_ONLY:
4608bcb0991SDimitry Andric       DyldInfoOnlyCmd = &LC.Data.dyld_info_command_data;
4618bcb0991SDimitry Andric       WriteQueue.push_back(std::make_pair(DyldInfoOnlyCmd->rebase_off,
4628bcb0991SDimitry Andric                                           &MachOWriter::writeRebaseOpcodes));
4638bcb0991SDimitry Andric       WriteQueue.push_back(std::make_pair(DyldInfoOnlyCmd->bind_off,
4648bcb0991SDimitry Andric                                           &MachOWriter::writeBasicBindOpcodes));
4658bcb0991SDimitry Andric       WriteQueue.push_back(std::make_pair(DyldInfoOnlyCmd->weak_bind_off,
4668bcb0991SDimitry Andric                                           &MachOWriter::writeWeakBindOpcodes));
4678bcb0991SDimitry Andric       WriteQueue.push_back(std::make_pair(DyldInfoOnlyCmd->lazy_bind_off,
4688bcb0991SDimitry Andric                                           &MachOWriter::writeLazyBindOpcodes));
4698bcb0991SDimitry Andric       WriteQueue.push_back(std::make_pair(DyldInfoOnlyCmd->export_off,
4708bcb0991SDimitry Andric                                           &MachOWriter::writeExportTrie));
4718bcb0991SDimitry Andric       break;
4728bcb0991SDimitry Andric     }
4738bcb0991SDimitry Andric   }
4748bcb0991SDimitry Andric 
4758bcb0991SDimitry Andric   llvm::sort(WriteQueue, [](const writeOperation &a, const writeOperation &b) {
4768bcb0991SDimitry Andric     return a.first < b.first;
4778bcb0991SDimitry Andric   });
4788bcb0991SDimitry Andric 
4798bcb0991SDimitry Andric   for (auto writeOp : WriteQueue) {
4808bcb0991SDimitry Andric     ZeroToOffset(OS, writeOp.first);
4818bcb0991SDimitry Andric     (this->*writeOp.second)(OS);
4828bcb0991SDimitry Andric   }
4838bcb0991SDimitry Andric }
4848bcb0991SDimitry Andric 
4858bcb0991SDimitry Andric void MachOWriter::writeRebaseOpcodes(raw_ostream &OS) {
4868bcb0991SDimitry Andric   MachOYAML::LinkEditData &LinkEdit = Obj.LinkEdit;
4878bcb0991SDimitry Andric 
4888bcb0991SDimitry Andric   for (auto Opcode : LinkEdit.RebaseOpcodes) {
4898bcb0991SDimitry Andric     uint8_t OpByte = Opcode.Opcode | Opcode.Imm;
4908bcb0991SDimitry Andric     OS.write(reinterpret_cast<char *>(&OpByte), 1);
4918bcb0991SDimitry Andric     for (auto Data : Opcode.ExtraData)
4928bcb0991SDimitry Andric       encodeULEB128(Data, OS);
4938bcb0991SDimitry Andric   }
4948bcb0991SDimitry Andric }
4958bcb0991SDimitry Andric 
4968bcb0991SDimitry Andric void MachOWriter::writeBasicBindOpcodes(raw_ostream &OS) {
4978bcb0991SDimitry Andric   writeBindOpcodes(OS, Obj.LinkEdit.BindOpcodes);
4988bcb0991SDimitry Andric }
4998bcb0991SDimitry Andric 
5008bcb0991SDimitry Andric void MachOWriter::writeWeakBindOpcodes(raw_ostream &OS) {
5018bcb0991SDimitry Andric   writeBindOpcodes(OS, Obj.LinkEdit.WeakBindOpcodes);
5028bcb0991SDimitry Andric }
5038bcb0991SDimitry Andric 
5048bcb0991SDimitry Andric void MachOWriter::writeLazyBindOpcodes(raw_ostream &OS) {
5058bcb0991SDimitry Andric   writeBindOpcodes(OS, Obj.LinkEdit.LazyBindOpcodes);
5068bcb0991SDimitry Andric }
5078bcb0991SDimitry Andric 
5088bcb0991SDimitry Andric void MachOWriter::writeNameList(raw_ostream &OS) {
5098bcb0991SDimitry Andric   for (auto NLE : Obj.LinkEdit.NameList) {
5108bcb0991SDimitry Andric     if (is64Bit)
5118bcb0991SDimitry Andric       writeNListEntry<MachO::nlist_64>(NLE, OS, Obj.IsLittleEndian);
5128bcb0991SDimitry Andric     else
5138bcb0991SDimitry Andric       writeNListEntry<MachO::nlist>(NLE, OS, Obj.IsLittleEndian);
5148bcb0991SDimitry Andric   }
5158bcb0991SDimitry Andric }
5168bcb0991SDimitry Andric 
5178bcb0991SDimitry Andric void MachOWriter::writeStringTable(raw_ostream &OS) {
5188bcb0991SDimitry Andric   for (auto Str : Obj.LinkEdit.StringTable) {
5198bcb0991SDimitry Andric     OS.write(Str.data(), Str.size());
5208bcb0991SDimitry Andric     OS.write('\0');
5218bcb0991SDimitry Andric   }
5228bcb0991SDimitry Andric }
5238bcb0991SDimitry Andric 
5248bcb0991SDimitry Andric class UniversalWriter {
5258bcb0991SDimitry Andric public:
5268bcb0991SDimitry Andric   UniversalWriter(yaml::YamlObjectFile &ObjectFile)
5278bcb0991SDimitry Andric       : ObjectFile(ObjectFile), fileStart(0) {}
5288bcb0991SDimitry Andric 
5295ffd83dbSDimitry Andric   Error writeMachO(raw_ostream &OS);
5308bcb0991SDimitry Andric 
5318bcb0991SDimitry Andric private:
5328bcb0991SDimitry Andric   void writeFatHeader(raw_ostream &OS);
5338bcb0991SDimitry Andric   void writeFatArchs(raw_ostream &OS);
5348bcb0991SDimitry Andric 
5358bcb0991SDimitry Andric   void ZeroToOffset(raw_ostream &OS, size_t offset);
5368bcb0991SDimitry Andric 
5378bcb0991SDimitry Andric   yaml::YamlObjectFile &ObjectFile;
5388bcb0991SDimitry Andric   uint64_t fileStart;
5398bcb0991SDimitry Andric };
5408bcb0991SDimitry Andric 
5415ffd83dbSDimitry Andric Error UniversalWriter::writeMachO(raw_ostream &OS) {
5428bcb0991SDimitry Andric   fileStart = OS.tell();
5438bcb0991SDimitry Andric   if (ObjectFile.MachO) {
5448bcb0991SDimitry Andric     MachOWriter Writer(*ObjectFile.MachO);
5455ffd83dbSDimitry Andric     return Writer.writeMachO(OS);
5468bcb0991SDimitry Andric   }
5478bcb0991SDimitry Andric 
5488bcb0991SDimitry Andric   writeFatHeader(OS);
5498bcb0991SDimitry Andric   writeFatArchs(OS);
5508bcb0991SDimitry Andric 
5518bcb0991SDimitry Andric   auto &FatFile = *ObjectFile.FatMachO;
5525ffd83dbSDimitry Andric   if (FatFile.FatArchs.size() < FatFile.Slices.size())
5535ffd83dbSDimitry Andric     return createStringError(
5545ffd83dbSDimitry Andric         errc::invalid_argument,
5555ffd83dbSDimitry Andric         "cannot write 'Slices' if not described in 'FatArches'");
5565ffd83dbSDimitry Andric 
5578bcb0991SDimitry Andric   for (size_t i = 0; i < FatFile.Slices.size(); i++) {
5588bcb0991SDimitry Andric     ZeroToOffset(OS, FatFile.FatArchs[i].offset);
5598bcb0991SDimitry Andric     MachOWriter Writer(FatFile.Slices[i]);
5605ffd83dbSDimitry Andric     if (Error Err = Writer.writeMachO(OS))
5615ffd83dbSDimitry Andric       return Err;
5628bcb0991SDimitry Andric 
5638bcb0991SDimitry Andric     auto SliceEnd = FatFile.FatArchs[i].offset + FatFile.FatArchs[i].size;
5648bcb0991SDimitry Andric     ZeroToOffset(OS, SliceEnd);
5658bcb0991SDimitry Andric   }
5665ffd83dbSDimitry Andric 
5675ffd83dbSDimitry Andric   return Error::success();
5688bcb0991SDimitry Andric }
5698bcb0991SDimitry Andric 
5708bcb0991SDimitry Andric void UniversalWriter::writeFatHeader(raw_ostream &OS) {
5718bcb0991SDimitry Andric   auto &FatFile = *ObjectFile.FatMachO;
5728bcb0991SDimitry Andric   MachO::fat_header header;
5738bcb0991SDimitry Andric   header.magic = FatFile.Header.magic;
5748bcb0991SDimitry Andric   header.nfat_arch = FatFile.Header.nfat_arch;
5758bcb0991SDimitry Andric   if (sys::IsLittleEndianHost)
5768bcb0991SDimitry Andric     swapStruct(header);
5778bcb0991SDimitry Andric   OS.write(reinterpret_cast<const char *>(&header), sizeof(MachO::fat_header));
5788bcb0991SDimitry Andric }
5798bcb0991SDimitry Andric 
5808bcb0991SDimitry Andric template <typename FatArchType>
5818bcb0991SDimitry Andric FatArchType constructFatArch(MachOYAML::FatArch &Arch) {
5828bcb0991SDimitry Andric   FatArchType FatArch;
5838bcb0991SDimitry Andric   FatArch.cputype = Arch.cputype;
5848bcb0991SDimitry Andric   FatArch.cpusubtype = Arch.cpusubtype;
5858bcb0991SDimitry Andric   FatArch.offset = Arch.offset;
5868bcb0991SDimitry Andric   FatArch.size = Arch.size;
5878bcb0991SDimitry Andric   FatArch.align = Arch.align;
5888bcb0991SDimitry Andric   return FatArch;
5898bcb0991SDimitry Andric }
5908bcb0991SDimitry Andric 
5918bcb0991SDimitry Andric template <typename StructType>
5928bcb0991SDimitry Andric void writeFatArch(MachOYAML::FatArch &LC, raw_ostream &OS) {}
5938bcb0991SDimitry Andric 
5948bcb0991SDimitry Andric template <>
5958bcb0991SDimitry Andric void writeFatArch<MachO::fat_arch>(MachOYAML::FatArch &Arch, raw_ostream &OS) {
5968bcb0991SDimitry Andric   auto FatArch = constructFatArch<MachO::fat_arch>(Arch);
5978bcb0991SDimitry Andric   if (sys::IsLittleEndianHost)
5988bcb0991SDimitry Andric     swapStruct(FatArch);
5998bcb0991SDimitry Andric   OS.write(reinterpret_cast<const char *>(&FatArch), sizeof(MachO::fat_arch));
6008bcb0991SDimitry Andric }
6018bcb0991SDimitry Andric 
6028bcb0991SDimitry Andric template <>
6038bcb0991SDimitry Andric void writeFatArch<MachO::fat_arch_64>(MachOYAML::FatArch &Arch,
6048bcb0991SDimitry Andric                                       raw_ostream &OS) {
6058bcb0991SDimitry Andric   auto FatArch = constructFatArch<MachO::fat_arch_64>(Arch);
6068bcb0991SDimitry Andric   FatArch.reserved = Arch.reserved;
6078bcb0991SDimitry Andric   if (sys::IsLittleEndianHost)
6088bcb0991SDimitry Andric     swapStruct(FatArch);
6098bcb0991SDimitry Andric   OS.write(reinterpret_cast<const char *>(&FatArch),
6108bcb0991SDimitry Andric            sizeof(MachO::fat_arch_64));
6118bcb0991SDimitry Andric }
6128bcb0991SDimitry Andric 
6138bcb0991SDimitry Andric void UniversalWriter::writeFatArchs(raw_ostream &OS) {
6148bcb0991SDimitry Andric   auto &FatFile = *ObjectFile.FatMachO;
6158bcb0991SDimitry Andric   bool is64Bit = FatFile.Header.magic == MachO::FAT_MAGIC_64;
6168bcb0991SDimitry Andric   for (auto Arch : FatFile.FatArchs) {
6178bcb0991SDimitry Andric     if (is64Bit)
6188bcb0991SDimitry Andric       writeFatArch<MachO::fat_arch_64>(Arch, OS);
6198bcb0991SDimitry Andric     else
6208bcb0991SDimitry Andric       writeFatArch<MachO::fat_arch>(Arch, OS);
6218bcb0991SDimitry Andric   }
6228bcb0991SDimitry Andric }
6238bcb0991SDimitry Andric 
6248bcb0991SDimitry Andric void UniversalWriter::ZeroToOffset(raw_ostream &OS, size_t Offset) {
6258bcb0991SDimitry Andric   auto currOffset = OS.tell() - fileStart;
6268bcb0991SDimitry Andric   if (currOffset < Offset)
6278bcb0991SDimitry Andric     ZeroFillBytes(OS, Offset - currOffset);
6288bcb0991SDimitry Andric }
6298bcb0991SDimitry Andric 
6308bcb0991SDimitry Andric } // end anonymous namespace
6318bcb0991SDimitry Andric 
6328bcb0991SDimitry Andric namespace llvm {
6338bcb0991SDimitry Andric namespace yaml {
6348bcb0991SDimitry Andric 
6355ffd83dbSDimitry Andric bool yaml2macho(YamlObjectFile &Doc, raw_ostream &Out, ErrorHandler EH) {
6368bcb0991SDimitry Andric   UniversalWriter Writer(Doc);
6375ffd83dbSDimitry Andric   if (Error Err = Writer.writeMachO(Out)) {
6385ffd83dbSDimitry Andric     handleAllErrors(std::move(Err),
6395ffd83dbSDimitry Andric                     [&](const ErrorInfoBase &Err) { EH(Err.message()); });
6405ffd83dbSDimitry Andric     return false;
6415ffd83dbSDimitry Andric   }
6428bcb0991SDimitry Andric   return true;
6438bcb0991SDimitry Andric }
6448bcb0991SDimitry Andric 
6458bcb0991SDimitry Andric } // namespace yaml
6468bcb0991SDimitry Andric } // namespace llvm
647