Skip to content

Instantly share code, notes, and snippets.

@ircmaxell
Last active February 20, 2019 14:36
Show Gist options
  • Select an option

  • Save ircmaxell/a9008fda6b60bd89cd0975bbd253aa85 to your computer and use it in GitHub Desktop.

Select an option

Save ircmaxell/a9008fda6b60bd89cd0975bbd253aa85 to your computer and use it in GitHub Desktop.

Revisions

  1. ircmaxell revised this gist Feb 20, 2019. 2 changed files with 183 additions and 0 deletions.
    1 change: 1 addition & 0 deletions compile.php
    Original file line number Diff line number Diff line change
    @@ -10,5 +10,6 @@

    $compiler = new PHPCompiler\Bootstrap\Compiler;
    $result = $compiler->compile($context);
    $result->setOptimizationLevel(3);

    $result->toFile(__DIR__ . '/test.o');
    182 changes: 182 additions & 0 deletions objdump -o3
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,182 @@

    test.o: file format elf64-x86-64


    Disassembly of section .init:

    0000000000000530 <_init>:
    530: 48 83 ec 08 sub $0x8,%rsp
    534: 48 8b 05 a5 0a 20 00 mov 0x200aa5(%rip),%rax # 200fe0 <__gmon_start__>
    53b: 48 85 c0 test %rax,%rax
    53e: 74 02 je 542 <_init+0x12>
    540: ff d0 callq *%rax
    542: 48 83 c4 08 add $0x8,%rsp
    546: c3 retq

    Disassembly of section .plt:

    0000000000000550 <.plt>:
    550: ff 35 62 0a 20 00 pushq 0x200a62(%rip) # 200fb8 <_GLOBAL_OFFSET_TABLE_+0x8>
    556: ff 25 64 0a 20 00 jmpq *0x200a64(%rip) # 200fc0 <_GLOBAL_OFFSET_TABLE_+0x10>
    55c: 0f 1f 40 00 nopl 0x0(%rax)

    Disassembly of section .plt.got:

    0000000000000560 <.plt.got>:
    560: ff 25 6a 0a 20 00 jmpq *0x200a6a(%rip) # 200fd0 <printf@GLIBC_2.2.5>
    566: 66 90 xchg %ax,%ax
    568: ff 25 8a 0a 20 00 jmpq *0x200a8a(%rip) # 200ff8 <__cxa_finalize@GLIBC_2.2.5>
    56e: 66 90 xchg %ax,%ax

    Disassembly of section .text:

    0000000000000570 <_start>:
    570: 31 ed xor %ebp,%ebp
    572: 49 89 d1 mov %rdx,%r9
    575: 5e pop %rsi
    576: 48 89 e2 mov %rsp,%rdx
    579: 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
    57d: 50 push %rax
    57e: 54 push %rsp
    57f: 4c 8d 05 aa 01 00 00 lea 0x1aa(%rip),%r8 # 730 <__libc_csu_fini>
    586: 48 8d 0d 33 01 00 00 lea 0x133(%rip),%rcx # 6c0 <__libc_csu_init>
    58d: 48 8d 3d 0c 01 00 00 lea 0x10c(%rip),%rdi # 6a0 <main>
    594: ff 15 3e 0a 20 00 callq *0x200a3e(%rip) # 200fd8 <__libc_start_main@GLIBC_2.2.5>
    59a: f4 hlt
    59b: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1)

    00000000000005a0 <deregister_tm_clones>:
    5a0: 48 8d 3d 69 0a 20 00 lea 0x200a69(%rip),%rdi # 201010 <__TMC_END__>
    5a7: 48 8d 05 69 0a 20 00 lea 0x200a69(%rip),%rax # 201017 <__TMC_END__+0x7>
    5ae: 55 push %rbp
    5af: 48 29 f8 sub %rdi,%rax
    5b2: 48 89 e5 mov %rsp,%rbp
    5b5: 48 83 f8 0e cmp $0xe,%rax
    5b9: 76 15 jbe 5d0 <deregister_tm_clones+0x30>
    5bb: 48 8b 05 06 0a 20 00 mov 0x200a06(%rip),%rax # 200fc8 <_ITM_deregisterTMCloneTable>
    5c2: 48 85 c0 test %rax,%rax
    5c5: 74 09 je 5d0 <deregister_tm_clones+0x30>
    5c7: 5d pop %rbp
    5c8: ff e0 jmpq *%rax
    5ca: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1)
    5d0: 5d pop %rbp
    5d1: c3 retq
    5d2: 0f 1f 40 00 nopl 0x0(%rax)
    5d6: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
    5dd: 00 00 00

    00000000000005e0 <register_tm_clones>:
    5e0: 48 8d 3d 29 0a 20 00 lea 0x200a29(%rip),%rdi # 201010 <__TMC_END__>
    5e7: 48 8d 35 22 0a 20 00 lea 0x200a22(%rip),%rsi # 201010 <__TMC_END__>
    5ee: 55 push %rbp
    5ef: 48 29 fe sub %rdi,%rsi
    5f2: 48 89 e5 mov %rsp,%rbp
    5f5: 48 c1 fe 03 sar $0x3,%rsi
    5f9: 48 89 f0 mov %rsi,%rax
    5fc: 48 c1 e8 3f shr $0x3f,%rax
    600: 48 01 c6 add %rax,%rsi
    603: 48 d1 fe sar %rsi
    606: 74 18 je 620 <register_tm_clones+0x40>
    608: 48 8b 05 e1 09 20 00 mov 0x2009e1(%rip),%rax # 200ff0 <_ITM_registerTMCloneTable>
    60f: 48 85 c0 test %rax,%rax
    612: 74 0c je 620 <register_tm_clones+0x40>
    614: 5d pop %rbp
    615: ff e0 jmpq *%rax
    617: 66 0f 1f 84 00 00 00 nopw 0x0(%rax,%rax,1)
    61e: 00 00
    620: 5d pop %rbp
    621: c3 retq
    622: 0f 1f 40 00 nopl 0x0(%rax)
    626: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
    62d: 00 00 00

    0000000000000630 <__do_global_dtors_aux>:
    630: 80 3d d9 09 20 00 00 cmpb $0x0,0x2009d9(%rip) # 201010 <__TMC_END__>
    637: 75 27 jne 660 <__do_global_dtors_aux+0x30>
    639: 48 83 3d b7 09 20 00 cmpq $0x0,0x2009b7(%rip) # 200ff8 <__cxa_finalize@GLIBC_2.2.5>
    640: 00
    641: 55 push %rbp
    642: 48 89 e5 mov %rsp,%rbp
    645: 74 0c je 653 <__do_global_dtors_aux+0x23>
    647: 48 8b 3d ba 09 20 00 mov 0x2009ba(%rip),%rdi # 201008 <__dso_handle>
    64e: e8 15 ff ff ff callq 568 <.plt.got+0x8>
    653: e8 48 ff ff ff callq 5a0 <deregister_tm_clones>
    658: 5d pop %rbp
    659: c6 05 b0 09 20 00 01 movb $0x1,0x2009b0(%rip) # 201010 <__TMC_END__>
    660: f3 c3 repz retq
    662: 0f 1f 40 00 nopl 0x0(%rax)
    666: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
    66d: 00 00 00

    0000000000000670 <frame_dummy>:
    670: 48 8d 3d 71 07 20 00 lea 0x200771(%rip),%rdi # 200de8 <__JCR_END__>
    677: 48 83 3f 00 cmpq $0x0,(%rdi)
    67b: 75 0b jne 688 <frame_dummy+0x18>
    67d: e9 5e ff ff ff jmpq 5e0 <register_tm_clones>
    682: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1)
    688: 48 8b 05 59 09 20 00 mov 0x200959(%rip),%rax # 200fe8 <_Jv_RegisterClasses>
    68f: 48 85 c0 test %rax,%rax
    692: 74 e9 je 67d <frame_dummy+0xd>
    694: 55 push %rbp
    695: 48 89 e5 mov %rsp,%rbp
    698: ff d0 callq *%rax
    69a: 5d pop %rbp
    69b: e9 40 ff ff ff jmpq 5e0 <register_tm_clones>

    00000000000006a0 <main>:
    6a0: 48 8d 35 9d 00 00 00 lea 0x9d(%rip),%rsi # 744 <_IO_stdin_used+0x4>
    6a7: 48 8d 3d 5f 01 00 00 lea 0x15f(%rip),%rdi # 80d <_IO_stdin_used+0xcd>
    6ae: ba 0c 00 00 00 mov $0xc,%edx
    6b3: 31 c0 xor %eax,%eax
    6b5: e9 a6 fe ff ff jmpq 560 <.plt.got>
    6ba: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1)

    00000000000006c0 <__libc_csu_init>:
    6c0: 41 57 push %r15
    6c2: 41 56 push %r14
    6c4: 41 89 ff mov %edi,%r15d
    6c7: 41 55 push %r13
    6c9: 41 54 push %r12
    6cb: 4c 8d 25 06 07 20 00 lea 0x200706(%rip),%r12 # 200dd8 <__frame_dummy_init_array_entry>
    6d2: 55 push %rbp
    6d3: 48 8d 2d 06 07 20 00 lea 0x200706(%rip),%rbp # 200de0 <__init_array_end>
    6da: 53 push %rbx
    6db: 49 89 f6 mov %rsi,%r14
    6de: 49 89 d5 mov %rdx,%r13
    6e1: 4c 29 e5 sub %r12,%rbp
    6e4: 48 83 ec 08 sub $0x8,%rsp
    6e8: 48 c1 fd 03 sar $0x3,%rbp
    6ec: e8 3f fe ff ff callq 530 <_init>
    6f1: 48 85 ed test %rbp,%rbp
    6f4: 74 20 je 716 <__libc_csu_init+0x56>
    6f6: 31 db xor %ebx,%ebx
    6f8: 0f 1f 84 00 00 00 00 nopl 0x0(%rax,%rax,1)
    6ff: 00
    700: 4c 89 ea mov %r13,%rdx
    703: 4c 89 f6 mov %r14,%rsi
    706: 44 89 ff mov %r15d,%edi
    709: 41 ff 14 dc callq *(%r12,%rbx,8)
    70d: 48 83 c3 01 add $0x1,%rbx
    711: 48 39 dd cmp %rbx,%rbp
    714: 75 ea jne 700 <__libc_csu_init+0x40>
    716: 48 83 c4 08 add $0x8,%rsp
    71a: 5b pop %rbx
    71b: 5d pop %rbp
    71c: 41 5c pop %r12
    71e: 41 5d pop %r13
    720: 41 5e pop %r14
    722: 41 5f pop %r15
    724: c3 retq
    725: 90 nop
    726: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
    72d: 00 00 00

    0000000000000730 <__libc_csu_fini>:
    730: f3 c3 repz retq

    Disassembly of section .fini:

    0000000000000734 <_fini>:
    734: 48 83 ec 08 sub $0x8,%rsp
    738: 48 83 c4 08 add $0x8,%rsp
    73c: c3 retq
  2. ircmaxell created this gist Feb 20, 2019.
    14 changes: 14 additions & 0 deletions compile.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    <?php

    declare(strict_types=1);

    require __DIR__ . '/vendor/autoload.php';
    $context = new PHPCompiler\Bootstrap\Context;
    $parser = new PHPCompiler\Bootstrap\Parser($context);

    $parser->parse(file_get_contents(__DIR__ . "/example.php"), __FILE__);

    $compiler = new PHPCompiler\Bootstrap\Compiler;
    $result = $compiler->compile($context);

    $result->toFile(__DIR__ . '/test.o');
    9 changes: 9 additions & 0 deletions example.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    <?php declare(strict_types=1);

    function main(): void {
    something('World', 12);
    }

    function something(string $name, int $number): void {
    printf("Hello %s (for the %d'th time)\n", $name, $number);
    }
    201 changes: 201 additions & 0 deletions objdump
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,201 @@

    test.o: file format elf64-x86-64


    Disassembly of section .init:

    0000000000000530 <_init>:
    530: 48 83 ec 08 sub $0x8,%rsp
    534: 48 8b 05 a5 0a 20 00 mov 0x200aa5(%rip),%rax # 200fe0 <__gmon_start__>
    53b: 48 85 c0 test %rax,%rax
    53e: 74 02 je 542 <_init+0x12>
    540: ff d0 callq *%rax
    542: 48 83 c4 08 add $0x8,%rsp
    546: c3 retq

    Disassembly of section .plt:

    0000000000000550 <.plt>:
    550: ff 35 62 0a 20 00 pushq 0x200a62(%rip) # 200fb8 <_GLOBAL_OFFSET_TABLE_+0x8>
    556: ff 25 64 0a 20 00 jmpq *0x200a64(%rip) # 200fc0 <_GLOBAL_OFFSET_TABLE_+0x10>
    55c: 0f 1f 40 00 nopl 0x0(%rax)

    Disassembly of section .plt.got:

    0000000000000560 <.plt.got>:
    560: ff 25 6a 0a 20 00 jmpq *0x200a6a(%rip) # 200fd0 <printf@GLIBC_2.2.5>
    566: 66 90 xchg %ax,%ax
    568: ff 25 8a 0a 20 00 jmpq *0x200a8a(%rip) # 200ff8 <__cxa_finalize@GLIBC_2.2.5>
    56e: 66 90 xchg %ax,%ax

    Disassembly of section .text:

    0000000000000570 <_start>:
    570: 31 ed xor %ebp,%ebp
    572: 49 89 d1 mov %rdx,%r9
    575: 5e pop %rsi
    576: 48 89 e2 mov %rsp,%rdx
    579: 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
    57d: 50 push %rax
    57e: 54 push %rsp
    57f: 4c 8d 05 da 01 00 00 lea 0x1da(%rip),%r8 # 760 <__libc_csu_fini>
    586: 48 8d 0d 63 01 00 00 lea 0x163(%rip),%rcx # 6f0 <__libc_csu_init>
    58d: 48 8d 3d 0c 01 00 00 lea 0x10c(%rip),%rdi # 6a0 <main>
    594: ff 15 3e 0a 20 00 callq *0x200a3e(%rip) # 200fd8 <__libc_start_main@GLIBC_2.2.5>
    59a: f4 hlt
    59b: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1)

    00000000000005a0 <deregister_tm_clones>:
    5a0: 48 8d 3d 69 0a 20 00 lea 0x200a69(%rip),%rdi # 201010 <__TMC_END__>
    5a7: 48 8d 05 69 0a 20 00 lea 0x200a69(%rip),%rax # 201017 <__TMC_END__+0x7>
    5ae: 55 push %rbp
    5af: 48 29 f8 sub %rdi,%rax
    5b2: 48 89 e5 mov %rsp,%rbp
    5b5: 48 83 f8 0e cmp $0xe,%rax
    5b9: 76 15 jbe 5d0 <deregister_tm_clones+0x30>
    5bb: 48 8b 05 06 0a 20 00 mov 0x200a06(%rip),%rax # 200fc8 <_ITM_deregisterTMCloneTable>
    5c2: 48 85 c0 test %rax,%rax
    5c5: 74 09 je 5d0 <deregister_tm_clones+0x30>
    5c7: 5d pop %rbp
    5c8: ff e0 jmpq *%rax
    5ca: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1)
    5d0: 5d pop %rbp
    5d1: c3 retq
    5d2: 0f 1f 40 00 nopl 0x0(%rax)
    5d6: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
    5dd: 00 00 00

    00000000000005e0 <register_tm_clones>:
    5e0: 48 8d 3d 29 0a 20 00 lea 0x200a29(%rip),%rdi # 201010 <__TMC_END__>
    5e7: 48 8d 35 22 0a 20 00 lea 0x200a22(%rip),%rsi # 201010 <__TMC_END__>
    5ee: 55 push %rbp
    5ef: 48 29 fe sub %rdi,%rsi
    5f2: 48 89 e5 mov %rsp,%rbp
    5f5: 48 c1 fe 03 sar $0x3,%rsi
    5f9: 48 89 f0 mov %rsi,%rax
    5fc: 48 c1 e8 3f shr $0x3f,%rax
    600: 48 01 c6 add %rax,%rsi
    603: 48 d1 fe sar %rsi
    606: 74 18 je 620 <register_tm_clones+0x40>
    608: 48 8b 05 e1 09 20 00 mov 0x2009e1(%rip),%rax # 200ff0 <_ITM_registerTMCloneTable>
    60f: 48 85 c0 test %rax,%rax
    612: 74 0c je 620 <register_tm_clones+0x40>
    614: 5d pop %rbp
    615: ff e0 jmpq *%rax
    617: 66 0f 1f 84 00 00 00 nopw 0x0(%rax,%rax,1)
    61e: 00 00
    620: 5d pop %rbp
    621: c3 retq
    622: 0f 1f 40 00 nopl 0x0(%rax)
    626: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
    62d: 00 00 00

    0000000000000630 <__do_global_dtors_aux>:
    630: 80 3d d9 09 20 00 00 cmpb $0x0,0x2009d9(%rip) # 201010 <__TMC_END__>
    637: 75 27 jne 660 <__do_global_dtors_aux+0x30>
    639: 48 83 3d b7 09 20 00 cmpq $0x0,0x2009b7(%rip) # 200ff8 <__cxa_finalize@GLIBC_2.2.5>
    640: 00
    641: 55 push %rbp
    642: 48 89 e5 mov %rsp,%rbp
    645: 74 0c je 653 <__do_global_dtors_aux+0x23>
    647: 48 8b 3d ba 09 20 00 mov 0x2009ba(%rip),%rdi # 201008 <__dso_handle>
    64e: e8 15 ff ff ff callq 568 <.plt.got+0x8>
    653: e8 48 ff ff ff callq 5a0 <deregister_tm_clones>
    658: 5d pop %rbp
    659: c6 05 b0 09 20 00 01 movb $0x1,0x2009b0(%rip) # 201010 <__TMC_END__>
    660: f3 c3 repz retq
    662: 0f 1f 40 00 nopl 0x0(%rax)
    666: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
    66d: 00 00 00

    0000000000000670 <frame_dummy>:
    670: 48 8d 3d 71 07 20 00 lea 0x200771(%rip),%rdi # 200de8 <__JCR_END__>
    677: 48 83 3f 00 cmpq $0x0,(%rdi)
    67b: 75 0b jne 688 <frame_dummy+0x18>
    67d: e9 5e ff ff ff jmpq 5e0 <register_tm_clones>
    682: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1)
    688: 48 8b 05 59 09 20 00 mov 0x200959(%rip),%rax # 200fe8 <_Jv_RegisterClasses>
    68f: 48 85 c0 test %rax,%rax
    692: 74 e9 je 67d <frame_dummy+0xd>
    694: 55 push %rbp
    695: 48 89 e5 mov %rsp,%rbp
    698: ff d0 callq *%rax
    69a: 5d pop %rbp
    69b: e9 40 ff ff ff jmpq 5e0 <register_tm_clones>

    00000000000006a0 <main>:
    6a0: 55 push %rbp
    6a1: 48 89 e5 mov %rsp,%rbp
    6a4: be 0c 00 00 00 mov $0xc,%esi
    6a9: 48 8d 3d c4 00 00 00 lea 0xc4(%rip),%rdi # 774 <_IO_stdin_used+0x4>
    6b0: e8 02 00 00 00 callq 6b7 <something>
    6b5: 5d pop %rbp
    6b6: c3 retq

    00000000000006b7 <something>:
    6b7: 55 push %rbp
    6b8: 48 89 e5 mov %rsp,%rbp
    6bb: 48 83 ec 10 sub $0x10,%rsp
    6bf: 48 89 7d f8 mov %rdi,-0x8(%rbp)
    6c3: 48 89 75 f0 mov %rsi,-0x10(%rbp)
    6c7: 48 8b 55 f0 mov -0x10(%rbp),%rdx
    6cb: 48 8b 45 f8 mov -0x8(%rbp),%rax
    6cf: 48 89 c6 mov %rax,%rsi
    6d2: 48 8d 3d 64 01 00 00 lea 0x164(%rip),%rdi # 83d <_IO_stdin_used+0xcd>
    6d9: b8 00 00 00 00 mov $0x0,%eax
    6de: e8 7d fe ff ff callq 560 <.plt.got>
    6e3: c9 leaveq
    6e4: c3 retq
    6e5: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
    6ec: 00 00 00
    6ef: 90 nop

    00000000000006f0 <__libc_csu_init>:
    6f0: 41 57 push %r15
    6f2: 41 56 push %r14
    6f4: 41 89 ff mov %edi,%r15d
    6f7: 41 55 push %r13
    6f9: 41 54 push %r12
    6fb: 4c 8d 25 d6 06 20 00 lea 0x2006d6(%rip),%r12 # 200dd8 <__frame_dummy_init_array_entry>
    702: 55 push %rbp
    703: 48 8d 2d d6 06 20 00 lea 0x2006d6(%rip),%rbp # 200de0 <__init_array_end>
    70a: 53 push %rbx
    70b: 49 89 f6 mov %rsi,%r14
    70e: 49 89 d5 mov %rdx,%r13
    711: 4c 29 e5 sub %r12,%rbp
    714: 48 83 ec 08 sub $0x8,%rsp
    718: 48 c1 fd 03 sar $0x3,%rbp
    71c: e8 0f fe ff ff callq 530 <_init>
    721: 48 85 ed test %rbp,%rbp
    724: 74 20 je 746 <__libc_csu_init+0x56>
    726: 31 db xor %ebx,%ebx
    728: 0f 1f 84 00 00 00 00 nopl 0x0(%rax,%rax,1)
    72f: 00
    730: 4c 89 ea mov %r13,%rdx
    733: 4c 89 f6 mov %r14,%rsi
    736: 44 89 ff mov %r15d,%edi
    739: 41 ff 14 dc callq *(%r12,%rbx,8)
    73d: 48 83 c3 01 add $0x1,%rbx
    741: 48 39 dd cmp %rbx,%rbp
    744: 75 ea jne 730 <__libc_csu_init+0x40>
    746: 48 83 c4 08 add $0x8,%rsp
    74a: 5b pop %rbx
    74b: 5d pop %rbp
    74c: 41 5c pop %r12
    74e: 41 5d pop %r13
    750: 41 5e pop %r14
    752: 41 5f pop %r15
    754: c3 retq
    755: 90 nop
    756: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
    75d: 00 00 00

    0000000000000760 <__libc_csu_fini>:
    760: f3 c3 repz retq

    Disassembly of section .fini:

    0000000000000764 <_fini>:
    764: 48 83 ec 08 sub $0x8,%rsp
    768: 48 83 c4 08 add $0x8,%rsp
    76c: c3 retq