MachOYAML.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- MachOYAML.h - Mach-O YAMLIO implementation ---------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// This file declares classes for handling the YAML representation
  16. /// of Mach-O.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_OBJECTYAML_MACHOYAML_H
  20. #define LLVM_OBJECTYAML_MACHOYAML_H
  21. #include "llvm/ADT/StringRef.h"
  22. #include "llvm/BinaryFormat/MachO.h"
  23. #include "llvm/ObjectYAML/DWARFYAML.h"
  24. #include "llvm/ObjectYAML/YAML.h"
  25. #include "llvm/Support/YAMLTraits.h"
  26. #include <cstdint>
  27. #include <string>
  28. #include <vector>
  29. namespace llvm {
  30. namespace MachOYAML {
  31. struct Relocation {
  32. // Offset in the section to what is being relocated.
  33. llvm::yaml::Hex32 address;
  34. // Symbol index if r_extern == 1 else section index.
  35. uint32_t symbolnum;
  36. bool is_pcrel;
  37. // Real length = 2 ^ length.
  38. uint8_t length;
  39. bool is_extern;
  40. uint8_t type;
  41. bool is_scattered;
  42. int32_t value;
  43. };
  44. struct Section {
  45. char sectname[16];
  46. char segname[16];
  47. llvm::yaml::Hex64 addr;
  48. uint64_t size;
  49. llvm::yaml::Hex32 offset;
  50. uint32_t align;
  51. llvm::yaml::Hex32 reloff;
  52. uint32_t nreloc;
  53. llvm::yaml::Hex32 flags;
  54. llvm::yaml::Hex32 reserved1;
  55. llvm::yaml::Hex32 reserved2;
  56. llvm::yaml::Hex32 reserved3;
  57. Optional<llvm::yaml::BinaryRef> content;
  58. std::vector<Relocation> relocations;
  59. };
  60. struct FileHeader {
  61. llvm::yaml::Hex32 magic;
  62. llvm::yaml::Hex32 cputype;
  63. llvm::yaml::Hex32 cpusubtype;
  64. llvm::yaml::Hex32 filetype;
  65. uint32_t ncmds;
  66. uint32_t sizeofcmds;
  67. llvm::yaml::Hex32 flags;
  68. llvm::yaml::Hex32 reserved;
  69. };
  70. struct LoadCommand {
  71. virtual ~LoadCommand();
  72. llvm::MachO::macho_load_command Data;
  73. std::vector<Section> Sections;
  74. std::vector<MachO::build_tool_version> Tools;
  75. std::vector<llvm::yaml::Hex8> PayloadBytes;
  76. std::string Content;
  77. uint64_t ZeroPadBytes;
  78. };
  79. struct NListEntry {
  80. uint32_t n_strx;
  81. llvm::yaml::Hex8 n_type;
  82. uint8_t n_sect;
  83. uint16_t n_desc;
  84. uint64_t n_value;
  85. };
  86. struct RebaseOpcode {
  87. MachO::RebaseOpcode Opcode;
  88. uint8_t Imm;
  89. std::vector<yaml::Hex64> ExtraData;
  90. };
  91. struct BindOpcode {
  92. MachO::BindOpcode Opcode;
  93. uint8_t Imm;
  94. std::vector<yaml::Hex64> ULEBExtraData;
  95. std::vector<int64_t> SLEBExtraData;
  96. StringRef Symbol;
  97. };
  98. struct ExportEntry {
  99. uint64_t TerminalSize = 0;
  100. uint64_t NodeOffset = 0;
  101. std::string Name;
  102. llvm::yaml::Hex64 Flags = 0;
  103. llvm::yaml::Hex64 Address = 0;
  104. llvm::yaml::Hex64 Other = 0;
  105. std::string ImportName;
  106. std::vector<MachOYAML::ExportEntry> Children;
  107. };
  108. struct LinkEditData {
  109. std::vector<MachOYAML::RebaseOpcode> RebaseOpcodes;
  110. std::vector<MachOYAML::BindOpcode> BindOpcodes;
  111. std::vector<MachOYAML::BindOpcode> WeakBindOpcodes;
  112. std::vector<MachOYAML::BindOpcode> LazyBindOpcodes;
  113. MachOYAML::ExportEntry ExportTrie;
  114. std::vector<NListEntry> NameList;
  115. std::vector<StringRef> StringTable;
  116. std::vector<yaml::Hex32> IndirectSymbols;
  117. bool isEmpty() const;
  118. };
  119. struct Object {
  120. bool IsLittleEndian;
  121. FileHeader Header;
  122. std::vector<LoadCommand> LoadCommands;
  123. std::vector<Section> Sections;
  124. LinkEditData LinkEdit;
  125. Optional<llvm::yaml::BinaryRef> RawLinkEditSegment;
  126. DWARFYAML::Data DWARF;
  127. };
  128. struct FatHeader {
  129. llvm::yaml::Hex32 magic;
  130. uint32_t nfat_arch;
  131. };
  132. struct FatArch {
  133. llvm::yaml::Hex32 cputype;
  134. llvm::yaml::Hex32 cpusubtype;
  135. llvm::yaml::Hex64 offset;
  136. uint64_t size;
  137. uint32_t align;
  138. llvm::yaml::Hex32 reserved;
  139. };
  140. struct UniversalBinary {
  141. FatHeader Header;
  142. std::vector<FatArch> FatArchs;
  143. std::vector<Object> Slices;
  144. };
  145. } // end namespace MachOYAML
  146. } // end namespace llvm
  147. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::LoadCommand)
  148. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::Relocation)
  149. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::Section)
  150. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::RebaseOpcode)
  151. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::BindOpcode)
  152. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::ExportEntry)
  153. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::NListEntry)
  154. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::Object)
  155. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::FatArch)
  156. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachO::build_tool_version)
  157. namespace llvm {
  158. class raw_ostream;
  159. namespace yaml {
  160. template <> struct MappingTraits<MachOYAML::FileHeader> {
  161. static void mapping(IO &IO, MachOYAML::FileHeader &FileHeader);
  162. };
  163. template <> struct MappingTraits<MachOYAML::Object> {
  164. static void mapping(IO &IO, MachOYAML::Object &Object);
  165. };
  166. template <> struct MappingTraits<MachOYAML::FatHeader> {
  167. static void mapping(IO &IO, MachOYAML::FatHeader &FatHeader);
  168. };
  169. template <> struct MappingTraits<MachOYAML::FatArch> {
  170. static void mapping(IO &IO, MachOYAML::FatArch &FatArch);
  171. };
  172. template <> struct MappingTraits<MachOYAML::UniversalBinary> {
  173. static void mapping(IO &IO, MachOYAML::UniversalBinary &UniversalBinary);
  174. };
  175. template <> struct MappingTraits<MachOYAML::LoadCommand> {
  176. static void mapping(IO &IO, MachOYAML::LoadCommand &LoadCommand);
  177. };
  178. template <> struct MappingTraits<MachOYAML::LinkEditData> {
  179. static void mapping(IO &IO, MachOYAML::LinkEditData &LinkEditData);
  180. };
  181. template <> struct MappingTraits<MachOYAML::RebaseOpcode> {
  182. static void mapping(IO &IO, MachOYAML::RebaseOpcode &RebaseOpcode);
  183. };
  184. template <> struct MappingTraits<MachOYAML::BindOpcode> {
  185. static void mapping(IO &IO, MachOYAML::BindOpcode &BindOpcode);
  186. };
  187. template <> struct MappingTraits<MachOYAML::ExportEntry> {
  188. static void mapping(IO &IO, MachOYAML::ExportEntry &ExportEntry);
  189. };
  190. template <> struct MappingTraits<MachOYAML::Relocation> {
  191. static void mapping(IO &IO, MachOYAML::Relocation &R);
  192. };
  193. template <> struct MappingTraits<MachOYAML::Section> {
  194. static void mapping(IO &IO, MachOYAML::Section &Section);
  195. static std::string validate(IO &io, MachOYAML::Section &Section);
  196. };
  197. template <> struct MappingTraits<MachOYAML::NListEntry> {
  198. static void mapping(IO &IO, MachOYAML::NListEntry &NListEntry);
  199. };
  200. template <> struct MappingTraits<MachO::build_tool_version> {
  201. static void mapping(IO &IO, MachO::build_tool_version &tool);
  202. };
  203. #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
  204. io.enumCase(value, #LCName, MachO::LCName);
  205. template <> struct ScalarEnumerationTraits<MachO::LoadCommandType> {
  206. static void enumeration(IO &io, MachO::LoadCommandType &value) {
  207. #include "llvm/BinaryFormat/MachO.def"
  208. io.enumFallback<Hex32>(value);
  209. }
  210. };
  211. #define ENUM_CASE(Enum) io.enumCase(value, #Enum, MachO::Enum);
  212. template <> struct ScalarEnumerationTraits<MachO::RebaseOpcode> {
  213. static void enumeration(IO &io, MachO::RebaseOpcode &value) {
  214. ENUM_CASE(REBASE_OPCODE_DONE)
  215. ENUM_CASE(REBASE_OPCODE_SET_TYPE_IMM)
  216. ENUM_CASE(REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB)
  217. ENUM_CASE(REBASE_OPCODE_ADD_ADDR_ULEB)
  218. ENUM_CASE(REBASE_OPCODE_ADD_ADDR_IMM_SCALED)
  219. ENUM_CASE(REBASE_OPCODE_DO_REBASE_IMM_TIMES)
  220. ENUM_CASE(REBASE_OPCODE_DO_REBASE_ULEB_TIMES)
  221. ENUM_CASE(REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB)
  222. ENUM_CASE(REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB)
  223. io.enumFallback<Hex8>(value);
  224. }
  225. };
  226. template <> struct ScalarEnumerationTraits<MachO::BindOpcode> {
  227. static void enumeration(IO &io, MachO::BindOpcode &value) {
  228. ENUM_CASE(BIND_OPCODE_DONE)
  229. ENUM_CASE(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM)
  230. ENUM_CASE(BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB)
  231. ENUM_CASE(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM)
  232. ENUM_CASE(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM)
  233. ENUM_CASE(BIND_OPCODE_SET_TYPE_IMM)
  234. ENUM_CASE(BIND_OPCODE_SET_ADDEND_SLEB)
  235. ENUM_CASE(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB)
  236. ENUM_CASE(BIND_OPCODE_ADD_ADDR_ULEB)
  237. ENUM_CASE(BIND_OPCODE_DO_BIND)
  238. ENUM_CASE(BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB)
  239. ENUM_CASE(BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED)
  240. ENUM_CASE(BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB)
  241. io.enumFallback<Hex8>(value);
  242. }
  243. };
  244. // This trait is used for 16-byte chars in Mach structures used for strings
  245. using char_16 = char[16];
  246. template <> struct ScalarTraits<char_16> {
  247. static void output(const char_16 &Val, void *, raw_ostream &Out);
  248. static StringRef input(StringRef Scalar, void *, char_16 &Val);
  249. static QuotingType mustQuote(StringRef S);
  250. };
  251. // This trait is used for UUIDs. It reads and writes them matching otool's
  252. // formatting style.
  253. using uuid_t = raw_ostream::uuid_t;
  254. template <> struct ScalarTraits<uuid_t> {
  255. static void output(const uuid_t &Val, void *, raw_ostream &Out);
  256. static StringRef input(StringRef Scalar, void *, uuid_t &Val);
  257. static QuotingType mustQuote(StringRef S);
  258. };
  259. // Load Command struct mapping traits
  260. #define LOAD_COMMAND_STRUCT(LCStruct) \
  261. template <> struct MappingTraits<MachO::LCStruct> { \
  262. static void mapping(IO &IO, MachO::LCStruct &LoadCommand); \
  263. };
  264. #include "llvm/BinaryFormat/MachO.def"
  265. // Extra structures used by load commands
  266. template <> struct MappingTraits<MachO::dylib> {
  267. static void mapping(IO &IO, MachO::dylib &LoadCommand);
  268. };
  269. template <> struct MappingTraits<MachO::fvmlib> {
  270. static void mapping(IO &IO, MachO::fvmlib &LoadCommand);
  271. };
  272. template <> struct MappingTraits<MachO::section> {
  273. static void mapping(IO &IO, MachO::section &LoadCommand);
  274. };
  275. template <> struct MappingTraits<MachO::section_64> {
  276. static void mapping(IO &IO, MachO::section_64 &LoadCommand);
  277. };
  278. } // end namespace yaml
  279. } // end namespace llvm
  280. #endif // LLVM_OBJECTYAML_MACHOYAML_H
  281. #ifdef __GNUC__
  282. #pragma GCC diagnostic pop
  283. #endif