llvm.org GIT mirror llvm / 6248d1b
Add the option, -universal-headers, used with -macho to print the Mach-O universal headers to llvm-objdump. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@225537 91177308-0d34-0410-b5e6-96231b3b80d8 Kevin Enderby 4 years ago
5 changed file(s) with 230 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
5050
5151 ObjectForArch getNext() const { return ObjectForArch(Parent, Index + 1); }
5252 uint32_t getCPUType() const { return Header.cputype; }
53 uint32_t getCPUSubType() const { return Header.cpusubtype; }
54 uint32_t getOffset() const { return Header.offset; }
55 uint32_t getSize() const { return Header.size; }
56 uint32_t getAlign() const { return Header.align; }
5357 std::string getArchTypeName() const {
5458 Triple T = MachOObjectFile::getArch(Header.cputype, Header.cpusubtype);
5559 return T.getArchName();
11 RUN: | FileCheck %s -check-prefix UEXE-all
22 RUN: llvm-objdump %p/Inputs/macho-universal-archive.x86_64.i386 -d -m -no-show-raw-insn -full-leading-addr -print-imm-hex -arch i386 \
33 RUN: | FileCheck %s -check-prefix UArchive-i386
4 RUN: llvm-objdump %p/Inputs/macho-universal.x86_64.i386 -universal-headers -m \
5 RUN: | FileCheck %s -check-prefix FAT
46
57 UEXE-all: macho-universal.x86_64.i386 (architecture x86_64):
68 UEXE-all: (__TEXT,__text) section
2224 UArchive-i386: 00000003 popl %ebp
2325 UArchive-i386: 00000004 retl
2426
27 FAT: Fat headers
28 FAT: fat_magic FAT_MAGIC
29 FAT: nfat_arch 2
30 FAT: architecture x86_64
31 FAT: cputype CPU_TYPE_X86_64
32 FAT: cpusubtype CPU_SUBTYPE_X86_64_ALL
33 FAT: capabilities CPU_SUBTYPE_LIB64
34 FAT: offset 4096
35 FAT: size 4360
36 FAT: align 2^12 (4096)
37 FAT: architecture i386
38 FAT: cputype CPU_TYPE_I386
39 FAT: cpusubtype CPU_SUBTYPE_I386_ALL
40 FAT: capabilities 0x0
41 FAT: offset 12288
42 FAT: size 4336
43 FAT: align 2^12 (4096)
6565 PrintImmHex("print-imm-hex",
6666 cl::desc("Use hex format for immediate values"));
6767
68 cl::opt
69 llvm::UniversalHeaders("universal-headers",
70 cl::desc("Print Mach-O universal headers"));
71
6872 static cl::list
6973 ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
7074 cl::ZeroOrMore);
293297 static void ProcessMachO(StringRef Filename, MachOObjectFile *MachOOF,
294298 StringRef ArchiveMemberName = StringRef(),
295299 StringRef ArchitectureName = StringRef()) {
296 outs() << Filename;
297 if (!ArchiveMemberName.empty())
298 outs() << '(' << ArchiveMemberName << ')';
299 if (!ArchitectureName.empty())
300 outs() << " (architecture " << ArchitectureName << ")";
301 outs() << ":\n";
300 // If we are doing some processing here on the Mach-O file print the header
301 // info. And don't print it otherwise like in the case of printing the
302 // UniversalHeaders.
303 if (Disassemble || PrivateHeaders || ExportsTrie || Rebase || Bind ||
304 LazyBind || WeakBind) {
305 outs() << Filename;
306 if (!ArchiveMemberName.empty())
307 outs() << '(' << ArchiveMemberName << ')';
308 if (!ArchitectureName.empty())
309 outs() << " (architecture " << ArchitectureName << ")";
310 outs() << ":\n";
311 }
302312
303313 if (Disassemble)
304314 DisassembleMachO(Filename, MachOOF);
330340 printWeakBindTable(MachOOF);
331341 }
332342
343 // printUnknownCPUType() helps print_fat_headers for unknown CPU's.
344 static void printUnknownCPUType(uint32_t cputype, uint32_t cpusubtype) {
345 outs() << " cputype (" << cputype << ")\n";
346 outs() << " cpusubtype (" << cpusubtype << ")\n";
347 }
348
349 // printCPUType() helps print_fat_headers by printing the cputype and
350 // pusubtype (symbolically for the one's it knows about).
351 static void printCPUType(uint32_t cputype, uint32_t cpusubtype) {
352 switch (cputype) {
353 case MachO::CPU_TYPE_I386:
354 switch (cpusubtype) {
355 case MachO::CPU_SUBTYPE_I386_ALL:
356 outs() << " cputype CPU_TYPE_I386\n";
357 outs() << " cpusubtype CPU_SUBTYPE_I386_ALL\n";
358 break;
359 default:
360 printUnknownCPUType(cputype, cpusubtype);
361 break;
362 }
363 break;
364 case MachO::CPU_TYPE_X86_64:
365 switch (cpusubtype) {
366 case MachO::CPU_SUBTYPE_X86_64_ALL:
367 outs() << " cputype CPU_TYPE_X86_64\n";
368 outs() << " cpusubtype CPU_SUBTYPE_X86_64_ALL\n";
369 break;
370 case MachO::CPU_SUBTYPE_X86_64_H:
371 outs() << " cputype CPU_TYPE_X86_64\n";
372 outs() << " cpusubtype CPU_SUBTYPE_X86_64_H\n";
373 break;
374 default:
375 printUnknownCPUType(cputype, cpusubtype);
376 break;
377 }
378 break;
379 case MachO::CPU_TYPE_ARM:
380 switch (cpusubtype) {
381 case MachO::CPU_SUBTYPE_ARM_ALL:
382 outs() << " cputype CPU_TYPE_ARM\n";
383 outs() << " cpusubtype CPU_SUBTYPE_ARM_ALL\n";
384 break;
385 case MachO::CPU_SUBTYPE_ARM_V4T:
386 outs() << " cputype CPU_TYPE_ARM\n";
387 outs() << " cpusubtype CPU_SUBTYPE_ARM_V4T\n";
388 break;
389 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
390 outs() << " cputype CPU_TYPE_ARM\n";
391 outs() << " cpusubtype CPU_SUBTYPE_ARM_V5TEJ\n";
392 break;
393 case MachO::CPU_SUBTYPE_ARM_XSCALE:
394 outs() << " cputype CPU_TYPE_ARM\n";
395 outs() << " cpusubtype CPU_SUBTYPE_ARM_XSCALE\n";
396 break;
397 case MachO::CPU_SUBTYPE_ARM_V6:
398 outs() << " cputype CPU_TYPE_ARM\n";
399 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6\n";
400 break;
401 case MachO::CPU_SUBTYPE_ARM_V6M:
402 outs() << " cputype CPU_TYPE_ARM\n";
403 outs() << " cpusubtype CPU_SUBTYPE_ARM_V6M\n";
404 break;
405 case MachO::CPU_SUBTYPE_ARM_V7:
406 outs() << " cputype CPU_TYPE_ARM\n";
407 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7\n";
408 break;
409 case MachO::CPU_SUBTYPE_ARM_V7EM:
410 outs() << " cputype CPU_TYPE_ARM\n";
411 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7EM\n";
412 break;
413 case MachO::CPU_SUBTYPE_ARM_V7K:
414 outs() << " cputype CPU_TYPE_ARM\n";
415 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7K\n";
416 break;
417 case MachO::CPU_SUBTYPE_ARM_V7M:
418 outs() << " cputype CPU_TYPE_ARM\n";
419 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7M\n";
420 break;
421 case MachO::CPU_SUBTYPE_ARM_V7S:
422 outs() << " cputype CPU_TYPE_ARM\n";
423 outs() << " cpusubtype CPU_SUBTYPE_ARM_V7S\n";
424 break;
425 default:
426 printUnknownCPUType(cputype, cpusubtype);
427 break;
428 }
429 break;
430 case MachO::CPU_TYPE_ARM64:
431 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
432 case MachO::CPU_SUBTYPE_ARM64_ALL:
433 outs() << " cputype CPU_TYPE_ARM64\n";
434 outs() << " cpusubtype CPU_SUBTYPE_ARM64_ALL\n";
435 break;
436 default:
437 printUnknownCPUType(cputype, cpusubtype);
438 break;
439 }
440 break;
441 default:
442 printUnknownCPUType(cputype, cpusubtype);
443 break;
444 }
445 }
446
447 static void printMachOUniversalHeaders(const object::MachOUniversalBinary *UB,
448 bool verbose) {
449 outs() << "Fat headers\n";
450 if (verbose)
451 outs() << "fat_magic FAT_MAGIC\n";
452 else
453 outs() << "fat_magic " << format("0x%" PRIx32, MachO::FAT_MAGIC) << "\n";
454
455 uint32_t nfat_arch = UB->getNumberOfObjects();
456 StringRef Buf = UB->getData();
457 uint64_t size = Buf.size();
458 uint64_t big_size = sizeof(struct MachO::fat_header) +
459 nfat_arch * sizeof(struct MachO::fat_arch);
460 outs() << "nfat_arch " << UB->getNumberOfObjects();
461 if (nfat_arch == 0)
462 outs() << " (malformed, contains zero architecture types)\n";
463 else if (big_size > size)
464 outs() << " (malformed, architectures past end of file)\n";
465 else
466 outs() << "\n";
467
468 for (uint32_t i = 0; i < nfat_arch; ++i) {
469 MachOUniversalBinary::ObjectForArch OFA(UB, i);
470 uint32_t cputype = OFA.getCPUType();
471 uint32_t cpusubtype = OFA.getCPUSubType();
472 outs() << "architecture ";
473 for (uint32_t j = 0; i != 0 && j <= i - 1; j++) {
474 MachOUniversalBinary::ObjectForArch other_OFA(UB, j);
475 uint32_t other_cputype = other_OFA.getCPUType();
476 uint32_t other_cpusubtype = other_OFA.getCPUSubType();
477 if (cputype != 0 && cpusubtype != 0 &&
478 cputype == other_cputype &&
479 (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) ==
480 (other_cpusubtype & ~MachO::CPU_SUBTYPE_MASK))
481 outs() << "(illegal duplicate architecture) ";
482 break;
483 }
484 if (verbose) {
485 outs() << OFA.getArchTypeName() << "\n";
486 printCPUType(cputype, cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
487 } else {
488 outs() << i << "\n";
489 outs() << " cputype " << cputype << "\n";
490 outs() << " cpusubtype " << (cpusubtype & ~MachO::CPU_SUBTYPE_MASK)
491 << "\n";
492 }
493 if (verbose &&
494 (cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64)
495 outs() << " capabilities CPU_SUBTYPE_LIB64\n";
496 else
497 outs() << " capabilities "
498 << format("0x%" PRIx32,
499 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24) << "\n";
500 outs() << " offset " << OFA.getOffset();
501 if (OFA.getOffset() > size)
502 outs() << " (past end of file)";
503 if (OFA.getOffset() % (1 << OFA.getAlign()) != 0)
504 outs() << " (not aligned on it's alignment (2^" << OFA.getAlign() << ")";
505 outs() << "\n";
506 outs() << " size " << OFA.getSize();
507 big_size = OFA.getOffset() + OFA.getSize();
508 if (big_size > size)
509 outs() << " (past end of file)";
510 outs() << "\n";
511 outs() << " align 2^" << OFA.getAlign() << " (" << (1 << OFA.getAlign())
512 << ")\n";
513 }
514 }
515
333516 // ParseInputMachO() parses the named Mach-O file in Filename and handles the
334517 // -arch flags selecting just those slices as specified by them and also parses
335518 // archive files. Then for each individual Mach-O file ProcessMachO() is
370553 }
371554 }
372555 return;
556 }
557 if (UniversalHeaders) {
558 if (MachOUniversalBinary *UB = dyn_cast(&Bin))
559 printMachOUniversalHeaders(UB, true);
373560 }
374561 if (MachOUniversalBinary *UB = dyn_cast(&Bin)) {
375562 // If we have a list of architecture flags specified dump only those.
27642951 break;
27652952 case MachO::CPU_TYPE_X86_64:
27662953 outs() << " X86_64";
2767 case MachO::CPU_SUBTYPE_X86_64_ALL:
2768 outs() << " ALL";
2769 break;
2770 case MachO::CPU_SUBTYPE_X86_64_H:
2771 outs() << " Haswell";
2772 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
2954 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
2955 case MachO::CPU_SUBTYPE_X86_64_ALL:
2956 outs() << " ALL";
2957 break;
2958 case MachO::CPU_SUBTYPE_X86_64_H:
2959 outs() << " Haswell";
2960 break;
2961 default:
2962 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
2963 break;
2964 }
27732965 break;
27742966 case MachO::CPU_TYPE_ARM:
27752967 outs() << " ARM";
890890 && !Rebase
891891 && !Bind
892892 && !LazyBind
893 && !WeakBind) {
893 && !WeakBind
894 && !(UniversalHeaders && MachOOpt)) {
894895 cl::PrintHelpMessage();
895896 return 2;
896897 }
3333 extern cl::opt Bind;
3434 extern cl::opt LazyBind;
3535 extern cl::opt WeakBind;
36 extern cl::opt UniversalHeaders;
3637
3738 // Various helper functions.
3839 bool error(std::error_code ec);