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