Minimal Mach-o 64 binary

后端 未结 2 1422
陌清茗
陌清茗 2020-12-18 10:37

I thinks this is a strange question, but now I prepare to hand-made a minimal Mach-O 64 binary, like the same problem on ELF (http://timelessname.com/elfbin/).

But c

相关标签:
2条回答
  • 2020-12-18 11:18

    Now I've find the minimal Mach-O 64 binary with 4K size but now I still has some trouble when reduce the size of my binary:

    1. When I reduce the file size, run the binary will get SIGKILL,
    2. When I reduce the value fileoff for segment __LINKEDIT, still get SIGKILL,
    3. When I google it, the value of fileoff is related to the binary size.

    So now I've to fill almost 3k \x00 in my binary, and I don't know how to reduce this binary anymore.

    0000000: cffa edfe 0700 0001 0300 0080 0200 0000  ................
    0000010: 0a00 0000 c002 0000 8500 0000 0000 0000  ................
    0000020: 1900 0000 4800 0000 5f5f 5041 4745 5a45  ....H...__PAGEZE
    0000030: 524f 0000 0000 0000 0000 0000 0000 0000  RO..............
    0000040: 0010 0000 0000 0000 0000 0000 0000 0000  ................
    0000050: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000060: 0000 0000 0000 0000 1900 0000 9800 0000  ................
    0000070: 5f5f 5445 5854 0000 0000 0000 0000 0000  __TEXT..........
    0000080: 0010 0000 0000 0000 0010 0000 0000 0000  ................
    0000090: 0000 0000 0000 0000 0100 0000 0000 0000  ................
    00000a0: 0700 0000 0500 0000 0100 0000 0000 0000  ................
    00000b0: 5f5f 7465 7874 0000 0000 0000 0000 0000  __text..........
    00000c0: 5f5f 5445 5854 0000 0000 0000 0000 0000  __TEXT..........
    00000d0: e012 0000 0000 0000 1000 0000 0000 0000  ................
    00000e0: e002 0000 0400 0000 0000 0000 0000 0000  ................
    00000f0: 0004 0080 0000 0000 0000 0000 0000 0000  ................
    0000100: 1900 0000 4800 0000 5f5f 4c49 4e4b 4544  ....H...__LINKED
    0000110: 4954 0000 0000 0000 0000 0000 0000 0000  IT..............
    0000120: 0000 0000 0000 0000 0010 0000 0000 0000  ................
    0000130: 0000 0000 0000 0000 0700 0000 0100 0000  ................
    0000140: 0000 0000 0000 0000 2200 0080 3000 0000  ........"...0...
    0000150: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000160: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000170: 0000 0000 0000 0000 0200 0000 1800 0000  ................
    0000180: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000190: 0b00 0000 5000 0000 0000 0000 0000 0000  ....P...........
    00001a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00001b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00001c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00001d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00001e0: 0e00 0000 2000 0000 0c00 0000 2f75 7372  .... ......./usr
    00001f0: 2f6c 6962 2f64 796c 6400 0000 0000 0000  /lib/dyld.......
    0000200: 1b00 0000 1800 0000 506f 7765 7265 6420  ........Powered
    0000210: 6279 2063 6d6a 0000 2400 0000 1000 0000  by cmj..$.......
    0000220: 000a 0a00 000a 0a00 0500 0000 b800 0000  ................
    0000230: 0400 0000 2a00 0000 0000 0000 0000 0000  ....*...........
    0000240: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000250: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000260: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000270: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000280: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000290: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00002a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00002b0: 0000 0000 0000 0000 e012 0000 0000 0000  ................
    00002c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00002d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00002e0: 48c7 c001 0000 0248 c7c7 0400 0000 0f05  H......H........
    00002f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000300: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000310: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000320: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000330: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000340: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000350: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000360: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000370: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000380: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000390: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00003a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00003b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00003c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00003d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00003e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00003f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000400: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000410: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000420: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000430: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000440: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000450: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000460: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000470: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000480: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000490: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00004a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00004b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00004c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00004d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00004e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00004f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000500: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000510: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000520: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000530: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000540: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000550: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000560: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000570: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000580: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000590: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00005a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00005b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00005c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00005d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00005e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00005f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000600: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000610: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000620: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000630: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000640: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000650: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000660: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000670: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000680: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000690: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00006a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00006b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00006c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00006d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00006e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00006f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000700: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000710: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000720: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000730: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000740: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000750: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000760: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000770: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000780: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000790: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00007a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00007b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00007c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00007d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00007e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00007f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000800: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000810: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000820: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000830: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000840: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000850: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000860: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000870: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000880: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000890: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00008a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00008b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00008c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00008d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00008e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00008f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000900: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000910: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000920: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000930: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000940: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000950: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000960: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000970: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000980: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000990: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00009a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00009b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00009c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00009d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00009e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    00009f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000a00: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000a10: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000a20: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000a30: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000a40: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000a50: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000a60: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000a70: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000a80: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000a90: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000aa0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000ab0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000ac0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000ad0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000ae0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000af0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000b00: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000b10: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000b20: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000b30: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000b40: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000b50: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000b60: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000b70: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000b80: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000b90: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000ba0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000bb0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000bc0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000bd0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000be0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000bf0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000c00: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000c10: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000c20: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000c30: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000c40: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000c50: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000c60: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000c70: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000c80: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000c90: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000ca0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000cb0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000cc0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000cd0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000ce0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000cf0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000d00: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000d10: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000d20: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000d30: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000d40: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000d50: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000d60: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000d70: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000d80: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000d90: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000da0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000db0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000dc0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000dd0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000de0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000df0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000e00: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000e10: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000e20: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000e30: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000e40: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000e50: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000e60: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000e70: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000e80: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000e90: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000ea0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000eb0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000ec0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000ed0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000ee0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000ef0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000f00: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000f10: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000f20: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000f30: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000f40: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000f50: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000f60: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000f70: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000f80: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000f90: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000fa0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000fb0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000fc0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000fd0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000fe0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    0000ff0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
    
    0 讨论(0)
  • 2020-12-18 11:19

    You cannot go below the 4096 byte limit since Yosemite 10.10.5. It's Mach-O kernel checks are more restrictive now. Alternatively to dyld and LC_MAIN, you can go for LC_UNIXTHREAD and obviously keeping your LC_SEGMENT64. Sections are not necessary, dropping them will make tracking the binary harder.

    Since El Capitan PAGEZERO with nonzero size is required for 64 bit executables. Here is a working HelloWorld example valid Sierra 10.12.2 compiled with NASM or YASM

    ; A minimal Mach-o x64 executable for OS X Sierra
    ; $ nasm -f bin -o tiny_hello tiny_hello.s
    ; $ chmod +x tiny_hello
    ; Constants (For readability)
    %define MH_MAGIC_64                    0xfeedfacf
    %define CPU_ARCH_ABI64                0x01000000
    %define    CPU_TYPE_I386                0x00000007
    %define CPU_TYPE_X86_64                CPU_ARCH_ABI64 | CPU_TYPE_I386
    %define CPU_SUBTYPE_LIB64            0x80000000
    %define CPU_SUBTYPE_I386_ALL        0x00000003
    %define MH_EXECUTE                    0x2
    %define MH_NOUNDEFS                    0x1
    %define LC_SEGMENT_64                0x19
    %define LC_UNIXTHREAD                0x5 
    %define VM_PROT_READ                0x1
    %define VM_PROT_WRITE                0x2
    %define VM_PROT_EXECUTE                0x4
    %define x86_THREAD_STATE64            0x4
    %define    x86_EXCEPTION_STATE64_COUNT    42
    %define SYSCALL_CLASS_SHIFT            24
    %define SYSCALL_CLASS_MASK            (0xFF << SYSCALL_CLASS_SHIFT)
    %define SYSCALL_NUMBER_MASK            (~SYSCALL_CLASS_MASK)  
    %define SYSCALL_CLASS_UNIX            2
    %define SYSCALL_CONSTRUCT_UNIX(syscall_number) \
                ((SYSCALL_CLASS_UNIX << SYSCALL_CLASS_SHIFT) | \
                 (SYSCALL_NUMBER_MASK & (syscall_number)))
    %define SYS_exit                    1
    %define SYS_write                    4
    ; NASM directive, not compiled
    ; Use RIP-Relative addressing for x64
    BITS    64
    ;DEFAULT    REL
    %define __origin 0x100000000
    org __origin
    ; Mach-O header
    DD        MH_MAGIC_64                                        ; magic
    DD        CPU_TYPE_X86_64                                    ; cputype
    DD        CPU_SUBTYPE_LIB64 | CPU_SUBTYPE_I386_ALL        ; cpusubtype
    DD        MH_EXECUTE                                        ; filetype
    DD        3                                                ; ncmds
    DD        __COMMANDSend  - __COMMANDSstart                ; sizeofcmds
    DD        MH_NOUNDEFS                                        ; flags
    DD        0x0                                                ; reserved
    __COMMANDSstart:
    
    ___PAGEZEROstart:
            DD        LC_SEGMENT_64                                    ; cmd
            dd         ___PAGEZEROend - ___PAGEZEROstart                ; command size
    hello_str:
            db         '__PAGEZERO',0x0,0,0,0,0,0 ; segment name (pad to 16 bytes)
            DQ        0x0                                                ; vmaddr
            DQ        __origin                                        ; vmsize
            DQ        0                                                ; fileoff
            DQ        0                                                ; filesize
            DD        0                                                 ; maxprot
            DD        0                                                ; initprot
            DD        0x0                                                ; nsects
            DD        0x0                                                ; flags
    ___PAGEZEROend:
    ; Segment and Sections
    ___TEXTstart:
            DD        LC_SEGMENT_64                                    ; cmd
            dd ___TEXTend - ___TEXTstart    ; command size
    
            db '__TEXT',0,0,0,0,0,0,0,0,0,0 ; segment name (pad to 16 bytes)
            DQ        __origin                                        ; vmaddr
            DQ        ___codeend - __origin                ; vmsize
            DQ        0                                                ; fileoff
            DQ        ___codeend - __origin                    ; filesize
            DD        VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE    ; maxprot
            DD        VM_PROT_READ | VM_PROT_EXECUTE                            ; initprot
            DD        0x0                                                ; nsects
            DD        0x0                                                ; flags
    ___TEXTend:
    __UNIX_THREADstart:
    ; UNIX Thread Status
    DD        LC_UNIXTHREAD                                    ; cmd
    DD        __UNIX_THREADend - __UNIX_THREADstart             ; cmdsize
    DD        x86_THREAD_STATE64                                ; flavor
    DD        x86_EXCEPTION_STATE64_COUNT                        ; count
    DQ        0x0, 0x0, 0x00, 0x0                                ; rax, rbx , rcx , rdx
    DQ        0x01, hello_str, 0x00, 0x00                        ; rdi = STDOUT, rsi = address of hello_str,  rbp, rsp
    DQ        0x00, 0x00                                        ; r8 and r9
    DQ        0x00, 0x00, 0x00, 0x00, 0x00, 0x00                ; r10, r11, r12, r13, r14, r15
    DQ         ___codestart, 0x00, 0x00, 0x00, 0x00            ; rip, rflags, cs, fs, gs
    __UNIX_THREADend:
    __COMMANDSend:
    ___codestart:                                                    ; 24 bytes
        ; rdi and rsi have already been set in the initial state
        mov        rdx, 11
        mov        rax, SYSCALL_CONSTRUCT_UNIX(SYS_write)
        syscall
        mov            rdi, rax
        mov            rax, SYSCALL_CONSTRUCT_UNIX(SYS_exit)
        syscall
    ___codeend:
        times 4096-($-$$) DB  0;
        filesize    EQU    $-$$
    
    0 讨论(0)
提交回复
热议问题