1 //===- MachOYAML.cpp - MachO YAMLIO implementation ------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines classes for handling the YAML representation of MachO. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/ObjectYAML/MachOYAML.h" 15 #include "llvm/Support/Casting.h" 16 #include "llvm/Support/Format.h" 17 18 #include <string.h> // For memcpy, memset and strnlen. 19 20 namespace llvm { 21 22 MachOYAML::LoadCommand::~LoadCommand() {} 23 24 namespace yaml { 25 26 void ScalarTraits<char_16>::output(const char_16 &Val, void *, 27 llvm::raw_ostream &Out) { 28 auto Len = strnlen(&Val[0], 16); 29 Out << StringRef(&Val[0], Len); 30 } 31 32 StringRef ScalarTraits<char_16>::input(StringRef Scalar, void *, char_16 &Val) { 33 size_t CopySize = 16 >= Scalar.size() ? 16 : Scalar.size(); 34 memcpy((void *)Val, Scalar.data(), CopySize); 35 36 if (Scalar.size() < 16) { 37 memset((void *)&Val[Scalar.size()], 0, 16 - Scalar.size()); 38 } 39 40 return StringRef(); 41 } 42 43 bool ScalarTraits<char_16>::mustQuote(StringRef S) { return needsQuotes(S); } 44 45 void ScalarTraits<uuid_t>::output(const uuid_t &Val, void *, 46 llvm::raw_ostream &Out) { 47 for (int Idx = 0; Idx < 16; ++Idx) { 48 Out << format("%02" PRIX32, Val[Idx]); 49 if (Idx == 3 || Idx == 5 || Idx == 7 || Idx == 9) 50 Out << "-"; 51 } 52 } 53 54 StringRef ScalarTraits<uuid_t>::input(StringRef Scalar, void *, uuid_t &Val) { 55 size_t OutIdx = 0; 56 for (size_t Idx = 0; Idx < Scalar.size(); ++Idx) { 57 if (Scalar[Idx] == '-' || OutIdx >= 16) 58 continue; 59 unsigned long long TempInt; 60 if (getAsUnsignedInteger(Scalar.slice(Idx, Idx + 2), 16, TempInt)) 61 return "invalid number"; 62 if (TempInt > 0xFF) 63 return "out of range number"; 64 Val[OutIdx] = static_cast<uint8_t>(TempInt); 65 ++Idx; // increment idx an extra time because we're consuming 2 chars 66 ++OutIdx; 67 } 68 return StringRef(); 69 } 70 71 bool ScalarTraits<uuid_t>::mustQuote(StringRef S) { return needsQuotes(S); } 72 73 void MappingTraits<MachOYAML::FileHeader>::mapping( 74 IO &IO, MachOYAML::FileHeader &FileHdr) { 75 IO.mapRequired("magic", FileHdr.magic); 76 IO.mapRequired("cputype", FileHdr.cputype); 77 IO.mapRequired("cpusubtype", FileHdr.cpusubtype); 78 IO.mapRequired("filetype", FileHdr.filetype); 79 IO.mapRequired("ncmds", FileHdr.ncmds); 80 IO.mapRequired("sizeofcmds", FileHdr.sizeofcmds); 81 IO.mapRequired("flags", FileHdr.flags); 82 IO.mapOptional("reserved", FileHdr.reserved, 83 static_cast<llvm::yaml::Hex32>(0xDEADBEEFu)); 84 } 85 86 void MappingTraits<MachOYAML::Object>::mapping(IO &IO, 87 MachOYAML::Object &Object) { 88 // If the context isn't already set, tag the document as !mach-o. 89 // For Fat files there will be a different tag so they can be differentiated. 90 if (!IO.getContext()) { 91 IO.setContext(&Object); 92 IO.mapTag("!mach-o", true); 93 } 94 IO.mapRequired("FileHeader", Object.Header); 95 IO.mapOptional("LoadCommands", Object.LoadCommands); 96 IO.mapOptional("LinkEditData", Object.LinkEdit); 97 IO.setContext(nullptr); 98 } 99 100 void MappingTraits<MachOYAML::LinkEditData>::mapping(IO &IO, 101 MachOYAML::LinkEditData &LinkEditData) { 102 IO.mapOptional("RebaseOpcodes", LinkEditData.RebaseOpcodes); 103 } 104 105 void MappingTraits<MachOYAML::RebaseOpcode>::mapping(IO &IO, 106 MachOYAML::RebaseOpcode &RebaseOpcode) { 107 IO.mapRequired("Opcode", RebaseOpcode.Opcode); 108 IO.mapRequired("Imm", RebaseOpcode.Imm); 109 IO.mapOptional("ExtraData", RebaseOpcode.ExtraData); 110 } 111 112 template <typename StructType> 113 void mapLoadCommandData(IO &IO, MachOYAML::LoadCommand &LoadCommand) {} 114 115 template <> 116 void mapLoadCommandData<MachO::segment_command>( 117 IO &IO, MachOYAML::LoadCommand &LoadCommand) { 118 IO.mapOptional("Sections", LoadCommand.Sections); 119 } 120 121 template <> 122 void mapLoadCommandData<MachO::segment_command_64>( 123 IO &IO, MachOYAML::LoadCommand &LoadCommand) { 124 IO.mapOptional("Sections", LoadCommand.Sections); 125 } 126 127 template <> 128 void mapLoadCommandData<MachO::dylib_command>( 129 IO &IO, MachOYAML::LoadCommand &LoadCommand) { 130 IO.mapOptional("PayloadString", LoadCommand.PayloadString); 131 } 132 133 template <> 134 void mapLoadCommandData<MachO::dylinker_command>( 135 IO &IO, MachOYAML::LoadCommand &LoadCommand) { 136 IO.mapOptional("PayloadString", LoadCommand.PayloadString); 137 } 138 139 void MappingTraits<MachOYAML::LoadCommand>::mapping( 140 IO &IO, MachOYAML::LoadCommand &LoadCommand) { 141 IO.mapRequired( 142 "cmd", (MachO::LoadCommandType &)LoadCommand.Data.load_command_data.cmd); 143 IO.mapRequired("cmdsize", LoadCommand.Data.load_command_data.cmdsize); 144 145 #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \ 146 case MachO::LCName: \ 147 MappingTraits<MachO::LCStruct>::mapping(IO, \ 148 LoadCommand.Data.LCStruct##_data); \ 149 mapLoadCommandData<MachO::LCStruct>(IO, LoadCommand); \ 150 break; 151 152 switch (LoadCommand.Data.load_command_data.cmd) { 153 #include "llvm/Support/MachO.def" 154 } 155 IO.mapOptional("PayloadBytes", LoadCommand.PayloadBytes); 156 IO.mapOptional("ZeroPadBytes", LoadCommand.ZeroPadBytes, (uint64_t)0ull); 157 } 158 159 void MappingTraits<MachO::dyld_info_command>::mapping( 160 IO &IO, MachO::dyld_info_command &LoadCommand) { 161 IO.mapRequired("rebase_off", LoadCommand.rebase_off); 162 IO.mapRequired("rebase_size", LoadCommand.rebase_size); 163 IO.mapRequired("bind_off", LoadCommand.bind_off); 164 IO.mapRequired("bind_size", LoadCommand.bind_size); 165 IO.mapRequired("weak_bind_off", LoadCommand.weak_bind_off); 166 IO.mapRequired("weak_bind_size", LoadCommand.weak_bind_size); 167 IO.mapRequired("lazy_bind_off", LoadCommand.lazy_bind_off); 168 IO.mapRequired("lazy_bind_size", LoadCommand.lazy_bind_size); 169 IO.mapRequired("export_off", LoadCommand.export_off); 170 IO.mapRequired("export_size", LoadCommand.export_size); 171 } 172 173 void MappingTraits<MachOYAML::Section>::mapping(IO &IO, 174 MachOYAML::Section &Section) { 175 IO.mapRequired("sectname", Section.sectname); 176 IO.mapRequired("segname", Section.segname); 177 IO.mapRequired("addr", Section.addr); 178 IO.mapRequired("size", Section.size); 179 IO.mapRequired("offset", Section.offset); 180 IO.mapRequired("align", Section.align); 181 IO.mapRequired("reloff", Section.reloff); 182 IO.mapRequired("nreloc", Section.nreloc); 183 IO.mapRequired("flags", Section.flags); 184 IO.mapRequired("reserved1", Section.reserved1); 185 IO.mapRequired("reserved2", Section.reserved2); 186 IO.mapOptional("reserved3", Section.reserved3); 187 } 188 189 void MappingTraits<MachO::dylib>::mapping(IO &IO, MachO::dylib &DylibStruct) { 190 IO.mapRequired("name", DylibStruct.name); 191 IO.mapRequired("timestamp", DylibStruct.timestamp); 192 IO.mapRequired("current_version", DylibStruct.current_version); 193 IO.mapRequired("compatibility_version", DylibStruct.compatibility_version); 194 } 195 196 void MappingTraits<MachO::dylib_command>::mapping( 197 IO &IO, MachO::dylib_command &LoadCommand) { 198 IO.mapRequired("dylib", LoadCommand.dylib); 199 } 200 201 void MappingTraits<MachO::dylinker_command>::mapping( 202 IO &IO, MachO::dylinker_command &LoadCommand) { 203 204 IO.mapRequired("name", LoadCommand.name); 205 } 206 207 void MappingTraits<MachO::dysymtab_command>::mapping( 208 IO &IO, MachO::dysymtab_command &LoadCommand) { 209 210 IO.mapRequired("ilocalsym", LoadCommand.ilocalsym); 211 IO.mapRequired("nlocalsym", LoadCommand.nlocalsym); 212 IO.mapRequired("iextdefsym", LoadCommand.iextdefsym); 213 IO.mapRequired("nextdefsym", LoadCommand.nextdefsym); 214 IO.mapRequired("iundefsym", LoadCommand.iundefsym); 215 IO.mapRequired("nundefsym", LoadCommand.nundefsym); 216 IO.mapRequired("tocoff", LoadCommand.tocoff); 217 IO.mapRequired("ntoc", LoadCommand.ntoc); 218 IO.mapRequired("modtaboff", LoadCommand.modtaboff); 219 IO.mapRequired("nmodtab", LoadCommand.nmodtab); 220 IO.mapRequired("extrefsymoff", LoadCommand.extrefsymoff); 221 IO.mapRequired("nextrefsyms", LoadCommand.nextrefsyms); 222 IO.mapRequired("indirectsymoff", LoadCommand.indirectsymoff); 223 IO.mapRequired("nindirectsyms", LoadCommand.nindirectsyms); 224 IO.mapRequired("extreloff", LoadCommand.extreloff); 225 IO.mapRequired("nextrel", LoadCommand.nextrel); 226 IO.mapRequired("locreloff", LoadCommand.locreloff); 227 IO.mapRequired("nlocrel", LoadCommand.nlocrel); 228 } 229 230 void MappingTraits<MachO::encryption_info_command>::mapping( 231 IO &IO, MachO::encryption_info_command &LoadCommand) { 232 233 IO.mapRequired("cryptoff", LoadCommand.cryptoff); 234 IO.mapRequired("cryptsize", LoadCommand.cryptsize); 235 IO.mapRequired("cryptid", LoadCommand.cryptid); 236 } 237 238 void MappingTraits<MachO::encryption_info_command_64>::mapping( 239 IO &IO, MachO::encryption_info_command_64 &LoadCommand) { 240 241 IO.mapRequired("cryptoff", LoadCommand.cryptoff); 242 IO.mapRequired("cryptsize", LoadCommand.cryptsize); 243 IO.mapRequired("cryptid", LoadCommand.cryptid); 244 IO.mapRequired("pad", LoadCommand.pad); 245 } 246 247 void MappingTraits<MachO::entry_point_command>::mapping( 248 IO &IO, MachO::entry_point_command &LoadCommand) { 249 250 IO.mapRequired("entryoff", LoadCommand.entryoff); 251 IO.mapRequired("stacksize", LoadCommand.stacksize); 252 } 253 254 void MappingTraits<MachO::fvmfile_command>::mapping( 255 IO &IO, MachO::fvmfile_command &LoadCommand) { 256 257 IO.mapRequired("name", LoadCommand.name); 258 IO.mapRequired("header_addr", LoadCommand.header_addr); 259 } 260 261 void MappingTraits<MachO::fvmlib>::mapping(IO &IO, MachO::fvmlib &FVMLib) { 262 IO.mapRequired("name", FVMLib.name); 263 IO.mapRequired("minor_version", FVMLib.minor_version); 264 IO.mapRequired("header_addr", FVMLib.header_addr); 265 } 266 267 void MappingTraits<MachO::fvmlib_command>::mapping( 268 IO &IO, MachO::fvmlib_command &LoadCommand) { 269 270 IO.mapRequired("fvmlib", LoadCommand.fvmlib); 271 } 272 273 void MappingTraits<MachO::ident_command>::mapping( 274 IO &IO, MachO::ident_command &LoadCommand) {} 275 276 void MappingTraits<MachO::linkedit_data_command>::mapping( 277 IO &IO, MachO::linkedit_data_command &LoadCommand) { 278 279 IO.mapRequired("dataoff", LoadCommand.dataoff); 280 IO.mapRequired("datasize", LoadCommand.datasize); 281 } 282 283 void MappingTraits<MachO::linker_option_command>::mapping( 284 IO &IO, MachO::linker_option_command &LoadCommand) { 285 286 IO.mapRequired("count", LoadCommand.count); 287 } 288 289 void MappingTraits<MachO::prebind_cksum_command>::mapping( 290 IO &IO, MachO::prebind_cksum_command &LoadCommand) { 291 292 IO.mapRequired("cksum", LoadCommand.cksum); 293 } 294 295 void MappingTraits<MachO::load_command>::mapping( 296 IO &IO, MachO::load_command &LoadCommand) {} 297 298 void MappingTraits<MachO::prebound_dylib_command>::mapping( 299 IO &IO, MachO::prebound_dylib_command &LoadCommand) { 300 301 IO.mapRequired("name", LoadCommand.name); 302 IO.mapRequired("nmodules", LoadCommand.nmodules); 303 IO.mapRequired("linked_modules", LoadCommand.linked_modules); 304 } 305 306 void MappingTraits<MachO::routines_command>::mapping( 307 IO &IO, MachO::routines_command &LoadCommand) { 308 309 IO.mapRequired("init_address", LoadCommand.init_address); 310 IO.mapRequired("init_module", LoadCommand.init_module); 311 IO.mapRequired("reserved1", LoadCommand.reserved1); 312 IO.mapRequired("reserved2", LoadCommand.reserved2); 313 IO.mapRequired("reserved3", LoadCommand.reserved3); 314 IO.mapRequired("reserved4", LoadCommand.reserved4); 315 IO.mapRequired("reserved5", LoadCommand.reserved5); 316 IO.mapRequired("reserved6", LoadCommand.reserved6); 317 } 318 319 void MappingTraits<MachO::routines_command_64>::mapping( 320 IO &IO, MachO::routines_command_64 &LoadCommand) { 321 322 IO.mapRequired("init_address", LoadCommand.init_address); 323 IO.mapRequired("init_module", LoadCommand.init_module); 324 IO.mapRequired("reserved1", LoadCommand.reserved1); 325 IO.mapRequired("reserved2", LoadCommand.reserved2); 326 IO.mapRequired("reserved3", LoadCommand.reserved3); 327 IO.mapRequired("reserved4", LoadCommand.reserved4); 328 IO.mapRequired("reserved5", LoadCommand.reserved5); 329 IO.mapRequired("reserved6", LoadCommand.reserved6); 330 } 331 332 void MappingTraits<MachO::rpath_command>::mapping( 333 IO &IO, MachO::rpath_command &LoadCommand) { 334 335 IO.mapRequired("path", LoadCommand.path); 336 } 337 338 void MappingTraits<MachO::section>::mapping(IO &IO, MachO::section &Section) { 339 IO.mapRequired("sectname", Section.sectname); 340 IO.mapRequired("segname", Section.segname); 341 IO.mapRequired("addr", Section.addr); 342 IO.mapRequired("size", Section.size); 343 IO.mapRequired("offset", Section.offset); 344 IO.mapRequired("align", Section.align); 345 IO.mapRequired("reloff", Section.reloff); 346 IO.mapRequired("nreloc", Section.nreloc); 347 IO.mapRequired("flags", Section.flags); 348 IO.mapRequired("reserved1", Section.reserved1); 349 IO.mapRequired("reserved2", Section.reserved2); 350 } 351 352 void MappingTraits<MachO::section_64>::mapping(IO &IO, 353 MachO::section_64 &Section) { 354 IO.mapRequired("sectname", Section.sectname); 355 IO.mapRequired("segname", Section.segname); 356 IO.mapRequired("addr", Section.addr); 357 IO.mapRequired("size", Section.size); 358 IO.mapRequired("offset", Section.offset); 359 IO.mapRequired("align", Section.align); 360 IO.mapRequired("reloff", Section.reloff); 361 IO.mapRequired("nreloc", Section.nreloc); 362 IO.mapRequired("flags", Section.flags); 363 IO.mapRequired("reserved1", Section.reserved1); 364 IO.mapRequired("reserved2", Section.reserved2); 365 IO.mapRequired("reserved3", Section.reserved3); 366 } 367 368 void MappingTraits<MachO::segment_command>::mapping( 369 IO &IO, MachO::segment_command &LoadCommand) { 370 371 IO.mapRequired("segname", LoadCommand.segname); 372 IO.mapRequired("vmaddr", LoadCommand.vmaddr); 373 IO.mapRequired("vmsize", LoadCommand.vmsize); 374 IO.mapRequired("fileoff", LoadCommand.fileoff); 375 IO.mapRequired("filesize", LoadCommand.filesize); 376 IO.mapRequired("maxprot", LoadCommand.maxprot); 377 IO.mapRequired("initprot", LoadCommand.initprot); 378 IO.mapRequired("nsects", LoadCommand.nsects); 379 IO.mapRequired("flags", LoadCommand.flags); 380 } 381 382 void MappingTraits<MachO::segment_command_64>::mapping( 383 IO &IO, MachO::segment_command_64 &LoadCommand) { 384 385 IO.mapRequired("segname", LoadCommand.segname); 386 IO.mapRequired("vmaddr", LoadCommand.vmaddr); 387 IO.mapRequired("vmsize", LoadCommand.vmsize); 388 IO.mapRequired("fileoff", LoadCommand.fileoff); 389 IO.mapRequired("filesize", LoadCommand.filesize); 390 IO.mapRequired("maxprot", LoadCommand.maxprot); 391 IO.mapRequired("initprot", LoadCommand.initprot); 392 IO.mapRequired("nsects", LoadCommand.nsects); 393 IO.mapRequired("flags", LoadCommand.flags); 394 } 395 396 void MappingTraits<MachO::source_version_command>::mapping( 397 IO &IO, MachO::source_version_command &LoadCommand) { 398 399 IO.mapRequired("version", LoadCommand.version); 400 } 401 402 void MappingTraits<MachO::sub_client_command>::mapping( 403 IO &IO, MachO::sub_client_command &LoadCommand) { 404 405 IO.mapRequired("client", LoadCommand.client); 406 } 407 408 void MappingTraits<MachO::sub_framework_command>::mapping( 409 IO &IO, MachO::sub_framework_command &LoadCommand) { 410 411 IO.mapRequired("umbrella", LoadCommand.umbrella); 412 } 413 414 void MappingTraits<MachO::sub_library_command>::mapping( 415 IO &IO, MachO::sub_library_command &LoadCommand) { 416 417 IO.mapRequired("sub_library", LoadCommand.sub_library); 418 } 419 420 void MappingTraits<MachO::sub_umbrella_command>::mapping( 421 IO &IO, MachO::sub_umbrella_command &LoadCommand) { 422 423 IO.mapRequired("sub_umbrella", LoadCommand.sub_umbrella); 424 } 425 426 void MappingTraits<MachO::symseg_command>::mapping( 427 IO &IO, MachO::symseg_command &LoadCommand) { 428 429 IO.mapRequired("offset", LoadCommand.offset); 430 IO.mapRequired("size", LoadCommand.size); 431 } 432 433 void MappingTraits<MachO::symtab_command>::mapping( 434 IO &IO, MachO::symtab_command &LoadCommand) { 435 436 IO.mapRequired("symoff", LoadCommand.symoff); 437 IO.mapRequired("nsyms", LoadCommand.nsyms); 438 IO.mapRequired("stroff", LoadCommand.stroff); 439 IO.mapRequired("strsize", LoadCommand.strsize); 440 } 441 442 void MappingTraits<MachO::thread_command>::mapping( 443 IO &IO, MachO::thread_command &LoadCommand) {} 444 445 void MappingTraits<MachO::twolevel_hints_command>::mapping( 446 IO &IO, MachO::twolevel_hints_command &LoadCommand) { 447 448 IO.mapRequired("offset", LoadCommand.offset); 449 IO.mapRequired("nhints", LoadCommand.nhints); 450 } 451 452 void MappingTraits<MachO::uuid_command>::mapping( 453 IO &IO, MachO::uuid_command &LoadCommand) { 454 455 IO.mapRequired("uuid", LoadCommand.uuid); 456 } 457 458 void MappingTraits<MachO::version_min_command>::mapping( 459 IO &IO, MachO::version_min_command &LoadCommand) { 460 461 IO.mapRequired("version", LoadCommand.version); 462 IO.mapRequired("sdk", LoadCommand.sdk); 463 } 464 465 } // namespace llvm::yaml 466 467 } // namespace llvm 468