llvm.org GIT mirror llvm / e306159
Next bit of support for llvm-objdump’s -private-headers for Mach-O files. This adds the printing of the LC_SEGMENT load command and sections, LC_SYMTAB and LC_DYSYMTAB load commands. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@216795 91177308-0d34-0410-b5e6-96231b3b80d8 Kevin Enderby 6 years ago
2 changed file(s) with 588 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
22 CHECK: Mach header
33 CHECK: magic cputype cpusubtype caps filetype ncmds sizeofcmds flags
44 CHECK: MH_MAGIC_64 X86_64 ALL 0x00 OBJECT 3 496 SUBSECTIONS_VIA_SYMBOLS
5 CHECK: Load command 0
6 CHECK: cmd LC_SEGMENT_64
7 CHECK: cmdsize 392
8 CHECK: segname
9 CHECK: vmaddr 0x0000000000000000
10 CHECK: vmsize 0x00000000000000a8
11 CHECK: fileoff 528
12 CHECK: filesize 168
13 CHECK: maxprot rwx
14 CHECK: initprot rwx
15 CHECK: nsects 4
16 CHECK: flags (none)
17 CHECK: Section
18 CHECK: sectname __text
19 CHECK: segname __TEXT
20 CHECK: addr 0x0000000000000000
21 CHECK: size 0x000000000000003b
22 CHECK: offset 528
23 CHECK: align 2^4 (16)
24 CHECK: reloff 696
25 CHECK: nreloc 2
26 CHECK: type S_REGULAR
27 CHECK: attributes PURE_INSTRUCTIONS SOME_INSTRUCTIONS
28 CHECK: reserved1 0
29 CHECK: reserved2 0
30 CHECK: Section
31 CHECK: sectname __cstring
32 CHECK: segname __TEXT
33 CHECK: addr 0x000000000000003b
34 CHECK: size 0x000000000000000d
35 CHECK: offset 587
36 CHECK: align 2^0 (1)
37 CHECK: reloff 0
38 CHECK: nreloc 0
39 CHECK: type S_CSTRING_LITERALS
40 CHECK: attributes (none)
41 CHECK: reserved1 0
42 CHECK: reserved2 0
43 CHECK: Section
44 CHECK: sectname __compact_unwind
45 CHECK: segname __LD
46 CHECK: addr 0x0000000000000048
47 CHECK: size 0x0000000000000020
48 CHECK: offset 600
49 CHECK: align 2^3 (8)
50 CHECK: reloff 712
51 CHECK: nreloc 1
52 CHECK: type S_REGULAR
53 CHECK: attributes DEBUG
54 CHECK: reserved1 0
55 CHECK: reserved2 0
56 CHECK: Section
57 CHECK: sectname __eh_frame
58 CHECK: segname __TEXT
59 CHECK: addr 0x0000000000000068
60 CHECK: size 0x0000000000000040
61 CHECK: offset 632
62 CHECK: align 2^3 (8)
63 CHECK: reloff 0
64 CHECK: nreloc 0
65 CHECK: type S_COALESCED
66 CHECK: attributes NO_TOC STRIP_STATIC_SYMS LIVE_SUPPORT
67 CHECK: reserved1 0
68 CHECK: reserved2 0
69 CHECK: Load command 1
70 CHECK: cmd LC_SYMTAB
71 CHECK: cmdsize 24
72 CHECK: symoff 720
73 CHECK: nsyms 5
74 CHECK: stroff 800
75 CHECK: strsize 44
76 CHECK: Load command 2
77 CHECK: cmd LC_DYSYMTAB
78 CHECK: cmdsize 80
79 CHECK: ilocalsym 0
80 CHECK: nlocalsym 2
81 CHECK: iextdefsym 2
82 CHECK: nextdefsym 2
83 CHECK: iundefsym 4
84 CHECK: nundefsym 1
85 CHECK: tocoff 0
86 CHECK: ntoc 0
87 CHECK: modtaboff 0
88 CHECK: nmodtab 0
89 CHECK: extrefsymoff 0
90 CHECK: nextrefsyms 0
91 CHECK: indirectsymoff 0
92 CHECK: nindirectsyms 0
93 CHECK: extreloff 0
94 CHECK: nextrel 0
95 CHECK: locreloff 0
96 CHECK: nlocrel 0
12711271 outs() << "\n";
12721272 }
12731273
1274 static void getAndPrintMachHeader(const MachOObjectFile *Obj, bool verbose) {
1274 static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
1275 StringRef SegName, uint64_t vmaddr,
1276 uint64_t vmsize, uint64_t fileoff,
1277 uint64_t filesize, uint32_t maxprot,
1278 uint32_t initprot, uint32_t nsects,
1279 uint32_t flags, uint32_t object_size,
1280 bool verbose) {
1281 uint64_t expected_cmdsize;
1282 if (cmd == MachO::LC_SEGMENT) {
1283 outs() << " cmd LC_SEGMENT\n";
1284 expected_cmdsize = nsects;
1285 expected_cmdsize *= sizeof(struct MachO::section);
1286 expected_cmdsize += sizeof(struct MachO::segment_command);
1287 } else {
1288 outs() << " cmd LC_SEGMENT_64\n";
1289 expected_cmdsize = nsects;
1290 expected_cmdsize *= sizeof(struct MachO::section_64);
1291 expected_cmdsize += sizeof(struct MachO::segment_command_64);
1292 }
1293 outs() << " cmdsize " << cmdsize;
1294 if (cmdsize != expected_cmdsize)
1295 outs() << " Inconsistent size\n";
1296 else
1297 outs() << "\n";
1298 outs() << " segname " << SegName << "\n";
1299 if (cmd == MachO::LC_SEGMENT_64) {
1300 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
1301 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
1302 } else {
1303 outs() << " vmaddr " << format("0x%08" PRIx32, vmaddr) << "\n";
1304 outs() << " vmsize " << format("0x%08" PRIx32, vmsize) << "\n";
1305 }
1306 outs() << " fileoff " << fileoff;
1307 if (fileoff > object_size)
1308 outs() << " (past end of file)\n";
1309 else
1310 outs() << "\n";
1311 outs() << " filesize " << filesize;
1312 if (fileoff + filesize > object_size)
1313 outs() << " (past end of file)\n";
1314 else
1315 outs() << "\n";
1316 if (verbose) {
1317 if ((maxprot &
1318 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
1319 MachO::VM_PROT_EXECUTE)) != 0)
1320 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
1321 else {
1322 if (maxprot & MachO::VM_PROT_READ)
1323 outs() << " maxprot r";
1324 else
1325 outs() << " maxprot -";
1326 if (maxprot & MachO::VM_PROT_WRITE)
1327 outs() << "w";
1328 else
1329 outs() << "-";
1330 if (maxprot & MachO::VM_PROT_EXECUTE)
1331 outs() << "x\n";
1332 else
1333 outs() << "-\n";
1334 }
1335 if ((initprot &
1336 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
1337 MachO::VM_PROT_EXECUTE)) != 0)
1338 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
1339 else {
1340 if (initprot & MachO::VM_PROT_READ)
1341 outs() << " initprot r";
1342 else
1343 outs() << " initprot -";
1344 if (initprot & MachO::VM_PROT_WRITE)
1345 outs() << "w";
1346 else
1347 outs() << "-";
1348 if (initprot & MachO::VM_PROT_EXECUTE)
1349 outs() << "x\n";
1350 else
1351 outs() << "-\n";
1352 }
1353 } else {
1354 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
1355 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
1356 }
1357 outs() << " nsects " << nsects << "\n";
1358 if (verbose) {
1359 outs() << " flags";
1360 if (flags == 0)
1361 outs() << " (none)\n";
1362 else {
1363 if (flags & MachO::SG_HIGHVM) {
1364 outs() << " HIGHVM";
1365 flags &= ~MachO::SG_HIGHVM;
1366 }
1367 if (flags & MachO::SG_FVMLIB) {
1368 outs() << " FVMLIB";
1369 flags &= ~MachO::SG_FVMLIB;
1370 }
1371 if (flags & MachO::SG_NORELOC) {
1372 outs() << " NORELOC";
1373 flags &= ~MachO::SG_NORELOC;
1374 }
1375 if (flags & MachO::SG_PROTECTED_VERSION_1) {
1376 outs() << " PROTECTED_VERSION_1";
1377 flags &= ~MachO::SG_PROTECTED_VERSION_1;
1378 }
1379 if (flags)
1380 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
1381 else
1382 outs() << "\n";
1383 }
1384 } else {
1385 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
1386 }
1387 }
1388
1389 static void PrintSection(const char *sectname, const char *segname,
1390 uint64_t addr, uint64_t size, uint32_t offset,
1391 uint32_t align, uint32_t reloff, uint32_t nreloc,
1392 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
1393 uint32_t cmd, const char *sg_segname,
1394 uint32_t filetype, uint32_t object_size,
1395 bool verbose) {
1396 outs() << "Section\n";
1397 outs() << " sectname " << format("%.16s\n", sectname);
1398 outs() << " segname " << format("%.16s", segname);
1399 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
1400 outs() << " (does not match segment)\n";
1401 else
1402 outs() << "\n";
1403 if (cmd == MachO::LC_SEGMENT_64) {
1404 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
1405 outs() << " size " << format("0x%016" PRIx64, size);
1406 } else {
1407 outs() << " addr " << format("0x%08" PRIx32, addr) << "\n";
1408 outs() << " size " << format("0x%08" PRIx32, size);
1409 }
1410 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
1411 outs() << " (past end of file)\n";
1412 else
1413 outs() << "\n";
1414 outs() << " offset " << offset;
1415 if (offset > object_size)
1416 outs() << " (past end of file)\n";
1417 else
1418 outs() << "\n";
1419 uint32_t align_shifted = 1 << align;
1420 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
1421 outs() << " reloff " << reloff;
1422 if (reloff > object_size)
1423 outs() << " (past end of file)\n";
1424 else
1425 outs() << "\n";
1426 outs() << " nreloc " << nreloc;
1427 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
1428 outs() << " (past end of file)\n";
1429 else
1430 outs() << "\n";
1431 uint32_t section_type = flags & MachO::SECTION_TYPE;
1432 if (verbose) {
1433 outs() << " type";
1434 if (section_type == MachO::S_REGULAR)
1435 outs() << " S_REGULAR\n";
1436 else if (section_type == MachO::S_ZEROFILL)
1437 outs() << " S_ZEROFILL\n";
1438 else if (section_type == MachO::S_CSTRING_LITERALS)
1439 outs() << " S_CSTRING_LITERALS\n";
1440 else if (section_type == MachO::S_4BYTE_LITERALS)
1441 outs() << " S_4BYTE_LITERALS\n";
1442 else if (section_type == MachO::S_8BYTE_LITERALS)
1443 outs() << " S_8BYTE_LITERALS\n";
1444 else if (section_type == MachO::S_16BYTE_LITERALS)
1445 outs() << " S_16BYTE_LITERALS\n";
1446 else if (section_type == MachO::S_LITERAL_POINTERS)
1447 outs() << " S_LITERAL_POINTERS\n";
1448 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
1449 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
1450 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
1451 outs() << " S_LAZY_SYMBOL_POINTERS\n";
1452 else if (section_type == MachO::S_SYMBOL_STUBS)
1453 outs() << " S_SYMBOL_STUBS\n";
1454 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
1455 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
1456 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
1457 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
1458 else if (section_type == MachO::S_COALESCED)
1459 outs() << " S_COALESCED\n";
1460 else if (section_type == MachO::S_INTERPOSING)
1461 outs() << " S_INTERPOSING\n";
1462 else if (section_type == MachO::S_DTRACE_DOF)
1463 outs() << " S_DTRACE_DOF\n";
1464 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
1465 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
1466 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
1467 outs() << " S_THREAD_LOCAL_REGULAR\n";
1468 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
1469 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
1470 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
1471 outs() << " S_THREAD_LOCAL_VARIABLES\n";
1472 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
1473 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
1474 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
1475 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
1476 else
1477 outs() << format("0x%08" PRIx32, section_type) << "\n";
1478 outs() << "attributes";
1479 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
1480 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
1481 outs() << " PURE_INSTRUCTIONS";
1482 if (section_attributes & MachO::S_ATTR_NO_TOC)
1483 outs() << " NO_TOC";
1484 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
1485 outs() << " STRIP_STATIC_SYMS";
1486 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
1487 outs() << " NO_DEAD_STRIP";
1488 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
1489 outs() << " LIVE_SUPPORT";
1490 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
1491 outs() << " SELF_MODIFYING_CODE";
1492 if (section_attributes & MachO::S_ATTR_DEBUG)
1493 outs() << " DEBUG";
1494 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
1495 outs() << " SOME_INSTRUCTIONS";
1496 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
1497 outs() << " EXT_RELOC";
1498 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
1499 outs() << " LOC_RELOC";
1500 if (section_attributes == 0)
1501 outs() << " (none)";
1502 outs() << "\n";
1503 } else
1504 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
1505 outs() << " reserved1 " << reserved1;
1506 if (section_type == MachO::S_SYMBOL_STUBS ||
1507 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
1508 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
1509 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
1510 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
1511 outs() << " (index into indirect symbol table)\n";
1512 else
1513 outs() << "\n";
1514 outs() << " reserved2 " << reserved2;
1515 if (section_type == MachO::S_SYMBOL_STUBS)
1516 outs() << " (size of stubs)\n";
1517 else
1518 outs() << "\n";
1519 }
1520
1521 static void PrintSymtabLoadCommand(MachO::symtab_command st, uint32_t cputype,
1522 uint32_t object_size) {
1523 outs() << " cmd LC_SYMTAB\n";
1524 outs() << " cmdsize " << st.cmdsize;
1525 if (st.cmdsize != sizeof(struct MachO::symtab_command))
1526 outs() << " Incorrect size\n";
1527 else
1528 outs() << "\n";
1529 outs() << " symoff " << st.symoff;
1530 if (st.symoff > object_size)
1531 outs() << " (past end of file)\n";
1532 else
1533 outs() << "\n";
1534 outs() << " nsyms " << st.nsyms;
1535 uint64_t big_size;
1536 if (cputype & MachO::CPU_ARCH_ABI64) {
1537 big_size = st.nsyms;
1538 big_size *= sizeof(struct MachO::nlist_64);
1539 big_size += st.symoff;
1540 if (big_size > object_size)
1541 outs() << " (past end of file)\n";
1542 else
1543 outs() << "\n";
1544 } else {
1545 big_size = st.nsyms;
1546 big_size *= sizeof(struct MachO::nlist);
1547 big_size += st.symoff;
1548 if (big_size > object_size)
1549 outs() << " (past end of file)\n";
1550 else
1551 outs() << "\n";
1552 }
1553 outs() << " stroff " << st.stroff;
1554 if (st.stroff > object_size)
1555 outs() << " (past end of file)\n";
1556 else
1557 outs() << "\n";
1558 outs() << " strsize " << st.strsize;
1559 big_size = st.stroff;
1560 big_size += st.strsize;
1561 if (big_size > object_size)
1562 outs() << " (past end of file)\n";
1563 else
1564 outs() << "\n";
1565 }
1566
1567 static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
1568 uint32_t nsyms, uint32_t object_size,
1569 uint32_t cputype) {
1570 outs() << " cmd LC_DYSYMTAB\n";
1571 outs() << " cmdsize " << dyst.cmdsize;
1572 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
1573 outs() << " Incorrect size\n";
1574 else
1575 outs() << "\n";
1576 outs() << " ilocalsym " << dyst.ilocalsym;
1577 if (dyst.ilocalsym > nsyms)
1578 outs() << " (greater than the number of symbols)\n";
1579 else
1580 outs() << "\n";
1581 outs() << " nlocalsym " << dyst.nlocalsym;
1582 uint64_t big_size;
1583 big_size = dyst.ilocalsym;
1584 big_size += dyst.nlocalsym;
1585 if (big_size > nsyms)
1586 outs() << " (past the end of the symbol table)\n";
1587 else
1588 outs() << "\n";
1589 outs() << " iextdefsym " << dyst.iextdefsym;
1590 if (dyst.iextdefsym > nsyms)
1591 outs() << " (greater than the number of symbols)\n";
1592 else
1593 outs() << "\n";
1594 outs() << " nextdefsym " << dyst.nextdefsym;
1595 big_size = dyst.iextdefsym;
1596 big_size += dyst.nextdefsym;
1597 if (big_size > nsyms)
1598 outs() << " (past the end of the symbol table)\n";
1599 else
1600 outs() << "\n";
1601 outs() << " iundefsym " << dyst.iundefsym;
1602 if (dyst.iundefsym > nsyms)
1603 outs() << " (greater than the number of symbols)\n";
1604 else
1605 outs() << "\n";
1606 outs() << " nundefsym " << dyst.nundefsym;
1607 big_size = dyst.iundefsym;
1608 big_size += dyst.nundefsym;
1609 if (big_size > nsyms)
1610 outs() << " (past the end of the symbol table)\n";
1611 else
1612 outs() << "\n";
1613 outs() << " tocoff " << dyst.tocoff;
1614 if (dyst.tocoff > object_size)
1615 outs() << " (past end of file)\n";
1616 else
1617 outs() << "\n";
1618 outs() << " ntoc " << dyst.ntoc;
1619 big_size = dyst.ntoc;
1620 big_size *= sizeof(struct MachO::dylib_table_of_contents);
1621 big_size += dyst.tocoff;
1622 if (big_size > object_size)
1623 outs() << " (past end of file)\n";
1624 else
1625 outs() << "\n";
1626 outs() << " modtaboff " << dyst.modtaboff;
1627 if (dyst.modtaboff > object_size)
1628 outs() << " (past end of file)\n";
1629 else
1630 outs() << "\n";
1631 outs() << " nmodtab " << dyst.nmodtab;
1632 uint64_t modtabend;
1633 if (cputype & MachO::CPU_ARCH_ABI64) {
1634 modtabend = dyst.nmodtab;
1635 modtabend *= sizeof(struct MachO::dylib_module_64);
1636 modtabend += dyst.modtaboff;
1637 } else {
1638 modtabend = dyst.nmodtab;
1639 modtabend *= sizeof(struct MachO::dylib_module);
1640 modtabend += dyst.modtaboff;
1641 }
1642 if (modtabend > object_size)
1643 outs() << " (past end of file)\n";
1644 else
1645 outs() << "\n";
1646 outs() << " extrefsymoff " << dyst.extrefsymoff;
1647 if (dyst.extrefsymoff > object_size)
1648 outs() << " (past end of file)\n";
1649 else
1650 outs() << "\n";
1651 outs() << " nextrefsyms " << dyst.nextrefsyms;
1652 big_size = dyst.nextrefsyms;
1653 big_size *= sizeof(struct MachO::dylib_reference);
1654 big_size += dyst.extrefsymoff;
1655 if (big_size > object_size)
1656 outs() << " (past end of file)\n";
1657 else
1658 outs() << "\n";
1659 outs() << " indirectsymoff " << dyst.indirectsymoff;
1660 if (dyst.indirectsymoff > object_size)
1661 outs() << " (past end of file)\n";
1662 else
1663 outs() << "\n";
1664 outs() << " nindirectsyms " << dyst.nindirectsyms;
1665 big_size = dyst.nindirectsyms;
1666 big_size *= sizeof(uint32_t);
1667 big_size += dyst.indirectsymoff;
1668 if (big_size > object_size)
1669 outs() << " (past end of file)\n";
1670 else
1671 outs() << "\n";
1672 outs() << " extreloff " << dyst.extreloff;
1673 if (dyst.extreloff > object_size)
1674 outs() << " (past end of file)\n";
1675 else
1676 outs() << "\n";
1677 outs() << " nextrel " << dyst.nextrel;
1678 big_size = dyst.nextrel;
1679 big_size *= sizeof(struct MachO::relocation_info);
1680 big_size += dyst.extreloff;
1681 if (big_size > object_size)
1682 outs() << " (past end of file)\n";
1683 else
1684 outs() << "\n";
1685 outs() << " locreloff " << dyst.locreloff;
1686 if (dyst.locreloff > object_size)
1687 outs() << " (past end of file)\n";
1688 else
1689 outs() << "\n";
1690 outs() << " nlocrel " << dyst.nlocrel;
1691 big_size = dyst.nlocrel;
1692 big_size *= sizeof(struct MachO::relocation_info);
1693 big_size += dyst.locreloff;
1694 if (big_size > object_size)
1695 outs() << " (past end of file)\n";
1696 else
1697 outs() << "\n";
1698 }
1699
1700 static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t ncmds,
1701 uint32_t filetype, uint32_t cputype,
1702 bool verbose) {
1703 StringRef Buf = Obj->getData();
1704 MachOObjectFile::LoadCommandInfo Command = Obj->getFirstLoadCommandInfo();
1705 for (unsigned i = 0;; ++i) {
1706 outs() << "Load command " << i << "\n";
1707 if (Command.C.cmd == MachO::LC_SEGMENT) {
1708 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
1709 const char *sg_segname = SLC.segname;
1710 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
1711 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
1712 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
1713 verbose);
1714 for (unsigned j = 0; j < SLC.nsects; j++) {
1715 MachO::section_64 S = Obj->getSection64(Command, j);
1716 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
1717 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
1718 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
1719 }
1720 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
1721 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
1722 const char *sg_segname = SLC_64.segname;
1723 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
1724 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
1725 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
1726 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
1727 for (unsigned j = 0; j < SLC_64.nsects; j++) {
1728 MachO::section_64 S_64 = Obj->getSection64(Command, j);
1729 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
1730 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
1731 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
1732 sg_segname, filetype, Buf.size(), verbose);
1733 }
1734 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
1735 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
1736 PrintSymtabLoadCommand(Symtab, cputype, Buf.size());
1737 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
1738 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
1739 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
1740 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(), cputype);
1741 } else {
1742 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
1743 << ")\n";
1744 outs() << " cmdsize " << Command.C.cmdsize << "\n";
1745 // TODO: get and print the raw bytes of the load command.
1746 }
1747 // TODO: print all the other kinds of load commands.
1748 if (i == ncmds - 1)
1749 break;
1750 else
1751 Command = Obj->getNextLoadCommandInfo(Command);
1752 }
1753 }
1754
1755 static void getAndPrintMachHeader(const MachOObjectFile *Obj, uint32_t &ncmds,
1756 uint32_t &filetype, uint32_t &cputype,
1757 bool verbose) {
12751758 if (Obj->is64Bit()) {
12761759 MachO::mach_header_64 H_64;
12771760 H_64 = Obj->getHeader64();
12781761 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
12791762 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
1763 ncmds = H_64.ncmds;
1764 filetype = H_64.filetype;
1765 cputype = H_64.cputype;
12801766 } else {
12811767 MachO::mach_header H;
12821768 H = Obj->getHeader();
12831769 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
12841770 H.sizeofcmds, H.flags, verbose);
1771 ncmds = H.ncmds;
1772 filetype = H.filetype;
1773 cputype = H.cputype;
12851774 }
12861775 }
12871776
12881777 void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
12891778 const MachOObjectFile *file = dyn_cast(Obj);
1290 getAndPrintMachHeader(file, true);
1291 // TODO: next get and print the load commands.
1292 }
1779 uint32_t ncmds = 0;
1780 uint32_t filetype = 0;
1781 uint32_t cputype = 0;
1782 getAndPrintMachHeader(file, ncmds, filetype, cputype, true);
1783 PrintLoadCommands(file, ncmds, filetype, cputype, true);
1784 }