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( 101 IO &IO, MachOYAML::LinkEditData &LinkEditData) { 102 IO.mapOptional("RebaseOpcodes", LinkEditData.RebaseOpcodes); 103 IO.mapOptional("BindOpcodes", LinkEditData.BindOpcodes); 104 IO.mapOptional("WeakBindOpcodes", LinkEditData.WeakBindOpcodes); 105 IO.mapOptional("LazyBindOpcodes", LinkEditData.LazyBindOpcodes); 106 IO.mapOptional("ExportTrie", LinkEditData.ExportTrie); 107 } 108 109 void MappingTraits<MachOYAML::RebaseOpcode>::mapping( 110 IO &IO, MachOYAML::RebaseOpcode &RebaseOpcode) { 111 IO.mapRequired("Opcode", RebaseOpcode.Opcode); 112 IO.mapRequired("Imm", RebaseOpcode.Imm); 113 IO.mapOptional("ExtraData", RebaseOpcode.ExtraData); 114 } 115 116 void MappingTraits<MachOYAML::BindOpcode>::mapping( 117 IO &IO, MachOYAML::BindOpcode &BindOpcode) { 118 IO.mapRequired("Opcode", BindOpcode.Opcode); 119 IO.mapRequired("Imm", BindOpcode.Imm); 120 IO.mapOptional("ULEBExtraData", BindOpcode.ULEBExtraData); 121 IO.mapOptional("SLEBExtraData", BindOpcode.SLEBExtraData); 122 IO.mapOptional("Symbol", BindOpcode.Symbol); 123 } 124 125 void MappingTraits<MachOYAML::ExportEntry>::mapping( 126 IO &IO, MachOYAML::ExportEntry &ExportEntry) { 127 IO.mapRequired("TerminalSize", ExportEntry.TerminalSize); 128 IO.mapOptional("NodeOffset", ExportEntry.NodeOffset); 129 IO.mapOptional("Name", ExportEntry.Name); 130 IO.mapOptional("Flags", ExportEntry.Flags); 131 IO.mapOptional("Address", ExportEntry.Address); 132 IO.mapOptional("Other", ExportEntry.Other); 133 IO.mapOptional("ImportName", ExportEntry.ImportName); 134 IO.mapOptional("Children", ExportEntry.Children); 135 } 136 137 template <typename StructType> 138 void mapLoadCommandData(IO &IO, MachOYAML::LoadCommand &LoadCommand) {} 139 140 template <> 141 void mapLoadCommandData<MachO::segment_command>( 142 IO &IO, MachOYAML::LoadCommand &LoadCommand) { 143 IO.mapOptional("Sections", LoadCommand.Sections); 144 } 145 146 template <> 147 void mapLoadCommandData<MachO::segment_command_64>( 148 IO &IO, MachOYAML::LoadCommand &LoadCommand) { 149 IO.mapOptional("Sections", LoadCommand.Sections); 150 } 151 152 template <> 153 void mapLoadCommandData<MachO::dylib_command>( 154 IO &IO, MachOYAML::LoadCommand &LoadCommand) { 155 IO.mapOptional("PayloadString", LoadCommand.PayloadString); 156 } 157 158 template <> 159 void mapLoadCommandData<MachO::rpath_command>( 160 IO &IO, MachOYAML::LoadCommand &LoadCommand) { 161 IO.mapOptional("PayloadString", LoadCommand.PayloadString); 162 } 163 164 template <> 165 void mapLoadCommandData<MachO::dylinker_command>( 166 IO &IO, MachOYAML::LoadCommand &LoadCommand) { 167 IO.mapOptional("PayloadString", LoadCommand.PayloadString); 168 } 169 170 void MappingTraits<MachOYAML::LoadCommand>::mapping( 171 IO &IO, MachOYAML::LoadCommand &LoadCommand) { 172 IO.mapRequired( 173 "cmd", (MachO::LoadCommandType &)LoadCommand.Data.load_command_data.cmd); 174 IO.mapRequired("cmdsize", LoadCommand.Data.load_command_data.cmdsize); 175 176 #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \ 177 case MachO::LCName: \ 178 MappingTraits<MachO::LCStruct>::mapping(IO, \ 179 LoadCommand.Data.LCStruct##_data); \ 180 mapLoadCommandData<MachO::LCStruct>(IO, LoadCommand); \ 181 break; 182 183 switch (LoadCommand.Data.load_command_data.cmd) { 184 #include "llvm/Support/MachO.def" 185 } 186 IO.mapOptional("PayloadBytes", LoadCommand.PayloadBytes); 187 IO.mapOptional("ZeroPadBytes", LoadCommand.ZeroPadBytes, (uint64_t)0ull); 188 } 189 190 void MappingTraits<MachO::dyld_info_command>::mapping( 191 IO &IO, MachO::dyld_info_command &LoadCommand) { 192 IO.mapRequired("rebase_off", LoadCommand.rebase_off); 193 IO.mapRequired("rebase_size", LoadCommand.rebase_size); 194 IO.mapRequired("bind_off", LoadCommand.bind_off); 195 IO.mapRequired("bind_size", LoadCommand.bind_size); 196 IO.mapRequired("weak_bind_off", LoadCommand.weak_bind_off); 197 IO.mapRequired("weak_bind_size", LoadCommand.weak_bind_size); 198 IO.mapRequired("lazy_bind_off", LoadCommand.lazy_bind_off); 199 IO.mapRequired("lazy_bind_size", LoadCommand.lazy_bind_size); 200 IO.mapRequired("export_off", LoadCommand.export_off); 201 IO.mapRequired("export_size", LoadCommand.export_size); 202 } 203 204 void MappingTraits<MachOYAML::Section>::mapping(IO &IO, 205 MachOYAML::Section &Section) { 206 IO.mapRequired("sectname", Section.sectname); 207 IO.mapRequired("segname", Section.segname); 208 IO.mapRequired("addr", Section.addr); 209 IO.mapRequired("size", Section.size); 210 IO.mapRequired("offset", Section.offset); 211 IO.mapRequired("align", Section.align); 212 IO.mapRequired("reloff", Section.reloff); 213 IO.mapRequired("nreloc", Section.nreloc); 214 IO.mapRequired("flags", Section.flags); 215 IO.mapRequired("reserved1", Section.reserved1); 216 IO.mapRequired("reserved2", Section.reserved2); 217 IO.mapOptional("reserved3", Section.reserved3); 218 } 219 220 void MappingTraits<MachO::dylib>::mapping(IO &IO, MachO::dylib &DylibStruct) { 221 IO.mapRequired("name", DylibStruct.name); 222 IO.mapRequired("timestamp", DylibStruct.timestamp); 223 IO.mapRequired("current_version", DylibStruct.current_version); 224 IO.mapRequired("compatibility_version", DylibStruct.compatibility_version); 225 } 226 227 void MappingTraits<MachO::dylib_command>::mapping( 228 IO &IO, MachO::dylib_command &LoadCommand) { 229 IO.mapRequired("dylib", LoadCommand.dylib); 230 } 231 232 void MappingTraits<MachO::dylinker_command>::mapping( 233 IO &IO, MachO::dylinker_command &LoadCommand) { 234 235 IO.mapRequired("name", LoadCommand.name); 236 } 237 238 void MappingTraits<MachO::dysymtab_command>::mapping( 239 IO &IO, MachO::dysymtab_command &LoadCommand) { 240 241 IO.mapRequired("ilocalsym", LoadCommand.ilocalsym); 242 IO.mapRequired("nlocalsym", LoadCommand.nlocalsym); 243 IO.mapRequired("iextdefsym", LoadCommand.iextdefsym); 244 IO.mapRequired("nextdefsym", LoadCommand.nextdefsym); 245 IO.mapRequired("iundefsym", LoadCommand.iundefsym); 246 IO.mapRequired("nundefsym", LoadCommand.nundefsym); 247 IO.mapRequired("tocoff", LoadCommand.tocoff); 248 IO.mapRequired("ntoc", LoadCommand.ntoc); 249 IO.mapRequired("modtaboff", LoadCommand.modtaboff); 250 IO.mapRequired("nmodtab", LoadCommand.nmodtab); 251 IO.mapRequired("extrefsymoff", LoadCommand.extrefsymoff); 252 IO.mapRequired("nextrefsyms", LoadCommand.nextrefsyms); 253 IO.mapRequired("indirectsymoff", LoadCommand.indirectsymoff); 254 IO.mapRequired("nindirectsyms", LoadCommand.nindirectsyms); 255 IO.mapRequired("extreloff", LoadCommand.extreloff); 256 IO.mapRequired("nextrel", LoadCommand.nextrel); 257 IO.mapRequired("locreloff", LoadCommand.locreloff); 258 IO.mapRequired("nlocrel", LoadCommand.nlocrel); 259 } 260 261 void MappingTraits<MachO::encryption_info_command>::mapping( 262 IO &IO, MachO::encryption_info_command &LoadCommand) { 263 264 IO.mapRequired("cryptoff", LoadCommand.cryptoff); 265 IO.mapRequired("cryptsize", LoadCommand.cryptsize); 266 IO.mapRequired("cryptid", LoadCommand.cryptid); 267 } 268 269 void MappingTraits<MachO::encryption_info_command_64>::mapping( 270 IO &IO, MachO::encryption_info_command_64 &LoadCommand) { 271 272 IO.mapRequired("cryptoff", LoadCommand.cryptoff); 273 IO.mapRequired("cryptsize", LoadCommand.cryptsize); 274 IO.mapRequired("cryptid", LoadCommand.cryptid); 275 IO.mapRequired("pad", LoadCommand.pad); 276 } 277 278 void MappingTraits<MachO::entry_point_command>::mapping( 279 IO &IO, MachO::entry_point_command &LoadCommand) { 280 281 IO.mapRequired("entryoff", LoadCommand.entryoff); 282 IO.mapRequired("stacksize", LoadCommand.stacksize); 283 } 284 285 void MappingTraits<MachO::fvmfile_command>::mapping( 286 IO &IO, MachO::fvmfile_command &LoadCommand) { 287 288 IO.mapRequired("name", LoadCommand.name); 289 IO.mapRequired("header_addr", LoadCommand.header_addr); 290 } 291 292 void MappingTraits<MachO::fvmlib>::mapping(IO &IO, MachO::fvmlib &FVMLib) { 293 IO.mapRequired("name", FVMLib.name); 294 IO.mapRequired("minor_version", FVMLib.minor_version); 295 IO.mapRequired("header_addr", FVMLib.header_addr); 296 } 297 298 void MappingTraits<MachO::fvmlib_command>::mapping( 299 IO &IO, MachO::fvmlib_command &LoadCommand) { 300 301 IO.mapRequired("fvmlib", LoadCommand.fvmlib); 302 } 303 304 void MappingTraits<MachO::ident_command>::mapping( 305 IO &IO, MachO::ident_command &LoadCommand) {} 306 307 void MappingTraits<MachO::linkedit_data_command>::mapping( 308 IO &IO, MachO::linkedit_data_command &LoadCommand) { 309 310 IO.mapRequired("dataoff", LoadCommand.dataoff); 311 IO.mapRequired("datasize", LoadCommand.datasize); 312 } 313 314 void MappingTraits<MachO::linker_option_command>::mapping( 315 IO &IO, MachO::linker_option_command &LoadCommand) { 316 317 IO.mapRequired("count", LoadCommand.count); 318 } 319 320 void MappingTraits<MachO::prebind_cksum_command>::mapping( 321 IO &IO, MachO::prebind_cksum_command &LoadCommand) { 322 323 IO.mapRequired("cksum", LoadCommand.cksum); 324 } 325 326 void MappingTraits<MachO::load_command>::mapping( 327 IO &IO, MachO::load_command &LoadCommand) {} 328 329 void MappingTraits<MachO::prebound_dylib_command>::mapping( 330 IO &IO, MachO::prebound_dylib_command &LoadCommand) { 331 332 IO.mapRequired("name", LoadCommand.name); 333 IO.mapRequired("nmodules", LoadCommand.nmodules); 334 IO.mapRequired("linked_modules", LoadCommand.linked_modules); 335 } 336 337 void MappingTraits<MachO::routines_command>::mapping( 338 IO &IO, MachO::routines_command &LoadCommand) { 339 340 IO.mapRequired("init_address", LoadCommand.init_address); 341 IO.mapRequired("init_module", LoadCommand.init_module); 342 IO.mapRequired("reserved1", LoadCommand.reserved1); 343 IO.mapRequired("reserved2", LoadCommand.reserved2); 344 IO.mapRequired("reserved3", LoadCommand.reserved3); 345 IO.mapRequired("reserved4", LoadCommand.reserved4); 346 IO.mapRequired("reserved5", LoadCommand.reserved5); 347 IO.mapRequired("reserved6", LoadCommand.reserved6); 348 } 349 350 void MappingTraits<MachO::routines_command_64>::mapping( 351 IO &IO, MachO::routines_command_64 &LoadCommand) { 352 353 IO.mapRequired("init_address", LoadCommand.init_address); 354 IO.mapRequired("init_module", LoadCommand.init_module); 355 IO.mapRequired("reserved1", LoadCommand.reserved1); 356 IO.mapRequired("reserved2", LoadCommand.reserved2); 357 IO.mapRequired("reserved3", LoadCommand.reserved3); 358 IO.mapRequired("reserved4", LoadCommand.reserved4); 359 IO.mapRequired("reserved5", LoadCommand.reserved5); 360 IO.mapRequired("reserved6", LoadCommand.reserved6); 361 } 362 363 void MappingTraits<MachO::rpath_command>::mapping( 364 IO &IO, MachO::rpath_command &LoadCommand) { 365 366 IO.mapRequired("path", LoadCommand.path); 367 } 368 369 void MappingTraits<MachO::section>::mapping(IO &IO, MachO::section &Section) { 370 IO.mapRequired("sectname", Section.sectname); 371 IO.mapRequired("segname", Section.segname); 372 IO.mapRequired("addr", Section.addr); 373 IO.mapRequired("size", Section.size); 374 IO.mapRequired("offset", Section.offset); 375 IO.mapRequired("align", Section.align); 376 IO.mapRequired("reloff", Section.reloff); 377 IO.mapRequired("nreloc", Section.nreloc); 378 IO.mapRequired("flags", Section.flags); 379 IO.mapRequired("reserved1", Section.reserved1); 380 IO.mapRequired("reserved2", Section.reserved2); 381 } 382 383 void MappingTraits<MachO::section_64>::mapping(IO &IO, 384 MachO::section_64 &Section) { 385 IO.mapRequired("sectname", Section.sectname); 386 IO.mapRequired("segname", Section.segname); 387 IO.mapRequired("addr", Section.addr); 388 IO.mapRequired("size", Section.size); 389 IO.mapRequired("offset", Section.offset); 390 IO.mapRequired("align", Section.align); 391 IO.mapRequired("reloff", Section.reloff); 392 IO.mapRequired("nreloc", Section.nreloc); 393 IO.mapRequired("flags", Section.flags); 394 IO.mapRequired("reserved1", Section.reserved1); 395 IO.mapRequired("reserved2", Section.reserved2); 396 IO.mapRequired("reserved3", Section.reserved3); 397 } 398 399 void MappingTraits<MachO::segment_command>::mapping( 400 IO &IO, MachO::segment_command &LoadCommand) { 401 402 IO.mapRequired("segname", LoadCommand.segname); 403 IO.mapRequired("vmaddr", LoadCommand.vmaddr); 404 IO.mapRequired("vmsize", LoadCommand.vmsize); 405 IO.mapRequired("fileoff", LoadCommand.fileoff); 406 IO.mapRequired("filesize", LoadCommand.filesize); 407 IO.mapRequired("maxprot", LoadCommand.maxprot); 408 IO.mapRequired("initprot", LoadCommand.initprot); 409 IO.mapRequired("nsects", LoadCommand.nsects); 410 IO.mapRequired("flags", LoadCommand.flags); 411 } 412 413 void MappingTraits<MachO::segment_command_64>::mapping( 414 IO &IO, MachO::segment_command_64 &LoadCommand) { 415 416 IO.mapRequired("segname", LoadCommand.segname); 417 IO.mapRequired("vmaddr", LoadCommand.vmaddr); 418 IO.mapRequired("vmsize", LoadCommand.vmsize); 419 IO.mapRequired("fileoff", LoadCommand.fileoff); 420 IO.mapRequired("filesize", LoadCommand.filesize); 421 IO.mapRequired("maxprot", LoadCommand.maxprot); 422 IO.mapRequired("initprot", LoadCommand.initprot); 423 IO.mapRequired("nsects", LoadCommand.nsects); 424 IO.mapRequired("flags", LoadCommand.flags); 425 } 426 427 void MappingTraits<MachO::source_version_command>::mapping( 428 IO &IO, MachO::source_version_command &LoadCommand) { 429 430 IO.mapRequired("version", LoadCommand.version); 431 } 432 433 void MappingTraits<MachO::sub_client_command>::mapping( 434 IO &IO, MachO::sub_client_command &LoadCommand) { 435 436 IO.mapRequired("client", LoadCommand.client); 437 } 438 439 void MappingTraits<MachO::sub_framework_command>::mapping( 440 IO &IO, MachO::sub_framework_command &LoadCommand) { 441 442 IO.mapRequired("umbrella", LoadCommand.umbrella); 443 } 444 445 void MappingTraits<MachO::sub_library_command>::mapping( 446 IO &IO, MachO::sub_library_command &LoadCommand) { 447 448 IO.mapRequired("sub_library", LoadCommand.sub_library); 449 } 450 451 void MappingTraits<MachO::sub_umbrella_command>::mapping( 452 IO &IO, MachO::sub_umbrella_command &LoadCommand) { 453 454 IO.mapRequired("sub_umbrella", LoadCommand.sub_umbrella); 455 } 456 457 void MappingTraits<MachO::symseg_command>::mapping( 458 IO &IO, MachO::symseg_command &LoadCommand) { 459 460 IO.mapRequired("offset", LoadCommand.offset); 461 IO.mapRequired("size", LoadCommand.size); 462 } 463 464 void MappingTraits<MachO::symtab_command>::mapping( 465 IO &IO, MachO::symtab_command &LoadCommand) { 466 467 IO.mapRequired("symoff", LoadCommand.symoff); 468 IO.mapRequired("nsyms", LoadCommand.nsyms); 469 IO.mapRequired("stroff", LoadCommand.stroff); 470 IO.mapRequired("strsize", LoadCommand.strsize); 471 } 472 473 void MappingTraits<MachO::thread_command>::mapping( 474 IO &IO, MachO::thread_command &LoadCommand) {} 475 476 void MappingTraits<MachO::twolevel_hints_command>::mapping( 477 IO &IO, MachO::twolevel_hints_command &LoadCommand) { 478 479 IO.mapRequired("offset", LoadCommand.offset); 480 IO.mapRequired("nhints", LoadCommand.nhints); 481 } 482 483 void MappingTraits<MachO::uuid_command>::mapping( 484 IO &IO, MachO::uuid_command &LoadCommand) { 485 486 IO.mapRequired("uuid", LoadCommand.uuid); 487 } 488 489 void MappingTraits<MachO::version_min_command>::mapping( 490 IO &IO, MachO::version_min_command &LoadCommand) { 491 492 IO.mapRequired("version", LoadCommand.version); 493 IO.mapRequired("sdk", LoadCommand.sdk); 494 } 495 496 } // namespace llvm::yaml 497 498 } // namespace llvm 499