Subversion Repositories Kolibri OS

Rev

Rev 2664 | Blame | Last modification | View Log | Download | RSS feed

  1.  
  2. ; flat assembler core
  3. ; Copyright (c) 1999-2012, Tomasz Grysztar.
  4. ; All rights reserved.
  5.  
  6. formatter:
  7.         mov     [current_offset],edi
  8.         cmp     [output_file],0
  9.         jne     output_path_ok
  10.         mov     esi,[input_file]
  11.         mov     edi,[free_additional_memory]
  12.       copy_output_path:
  13.         lods    byte [esi]
  14.         cmp     edi,[structures_buffer]
  15.         jae     out_of_memory
  16.         stos    byte [edi]
  17.         or      al,al
  18.         jnz     copy_output_path
  19.         dec     edi
  20.         mov     eax,edi
  21.       find_extension:
  22.         dec     eax
  23.         cmp     eax,[free_additional_memory]
  24.         jb      extension_found
  25.         cmp     byte [eax],'\'
  26.         je      extension_found
  27.         cmp     byte [eax],'/'
  28.         je      extension_found
  29.         cmp     byte [eax],'.'
  30.         jne     find_extension
  31.         mov     edi,eax
  32.       extension_found:
  33.         lea     eax,[edi+9]
  34.         cmp     eax,[structures_buffer]
  35.         jae     out_of_memory
  36.         cmp     [file_extension],0
  37.         jne     extension_specified
  38.         mov     al,[output_format]
  39.         cmp     al,2
  40.         je      exe_extension
  41.         jb      bin_extension
  42.         cmp     al,4
  43.         je      obj_extension
  44.         cmp     al,5
  45.         je      o_extension
  46.         cmp     al,3
  47.         jne     no_extension
  48.         cmp     [subsystem],1
  49.         je      sys_extension
  50.         cmp     [subsystem],10
  51.         jae     efi_extension
  52.         bt      [format_flags],8
  53.         jnc     exe_extension
  54.         mov     eax,'.dll'
  55.         jmp     make_extension
  56.       sys_extension:
  57.         mov     eax,'.sys'
  58.         jmp     make_extension
  59.       efi_extension:
  60.         mov     eax,'.efi'
  61.         jmp     make_extension
  62.       bin_extension:
  63.         mov     eax,'.bin'
  64.         bt      [format_flags],0
  65.         jnc     make_extension
  66.         mov     eax,'.com'
  67.         jmp     make_extension
  68.       obj_extension:
  69.         mov     eax,'.obj'
  70.         jmp     make_extension
  71.       o_extension:
  72.         mov     eax,'.o'
  73.         bt      [format_flags],0
  74.         jnc     make_extension
  75.       no_extension:
  76.         xor     eax,eax
  77.         jmp     make_extension
  78.       exe_extension:
  79.         mov     eax,'.exe'
  80.       make_extension:
  81.         xchg    eax,[edi]
  82.         scas    dword [edi]
  83.         mov     byte [edi],0
  84.         scas    byte [edi]
  85.         mov     esi,edi
  86.         stos    dword [edi]
  87.         sub     edi,9
  88.         xor     eax,eax
  89.         mov     ebx,characters
  90.       adapt_case:
  91.         mov     al,[esi]
  92.         or      al,al
  93.         jz      adapt_next
  94.         xlat    byte [ebx]
  95.         cmp     al,[esi]
  96.         je      adapt_ok
  97.         sub     byte [edi],20h
  98.       adapt_ok:
  99.         inc     esi
  100.       adapt_next:
  101.         inc     edi
  102.         cmp     byte [edi],0
  103.         jne     adapt_case
  104.         jmp     extension_ok
  105.       extension_specified:
  106.         mov     al,'.'
  107.         stos    byte [edi]
  108.         mov     esi,[file_extension]
  109.       copy_extension:
  110.         lods    byte [esi]
  111.         stos    byte [edi]
  112.         test    al,al
  113.         jnz     copy_extension
  114.         dec     edi
  115.       extension_ok:
  116.         mov     esi,edi
  117.         lea     ecx,[esi+1]
  118.         sub     ecx,[free_additional_memory]
  119.         mov     edi,[structures_buffer]
  120.         dec     edi
  121.         std
  122.         rep     movs byte [edi],[esi]
  123.         cld
  124.         inc     edi
  125.         mov     [structures_buffer],edi
  126.         mov     [output_file],edi
  127.       output_path_ok:
  128.         cmp     [symbols_file],0
  129.         je      labels_table_ok
  130.         mov     ecx,[memory_end]
  131.         sub     ecx,[labels_list]
  132.         mov     edi,[display_buffer]
  133.         sub     edi,8
  134.         mov     [edi],ecx
  135.         or      dword [edi+4],-1
  136.         sub     edi,ecx
  137.         cmp     edi,[current_offset]
  138.         jbe     out_of_memory
  139.         mov     [display_buffer],edi
  140.         mov     esi,[memory_end]
  141.       copy_labels:
  142.         sub     esi,32
  143.         cmp     esi,[labels_list]
  144.         jb      labels_table_ok
  145.         mov     ecx,32 shr 2
  146.         rep     movs dword [edi],[esi]
  147.         sub     esi,32
  148.         jmp     copy_labels
  149.       labels_table_ok:
  150.         mov     edi,[current_offset]
  151.         cmp     [output_format],4
  152.         je      coff_formatter
  153.         cmp     [output_format],5
  154.         jne     common_formatter
  155.         bt      [format_flags],0
  156.         jnc     elf_formatter
  157.       common_formatter:
  158.         mov     eax,edi
  159.         sub     eax,[code_start]
  160.         mov     [real_code_size],eax
  161.         cmp     edi,[undefined_data_end]
  162.         jne     calculate_code_size
  163.         mov     edi,[undefined_data_start]
  164.       calculate_code_size:
  165.         mov     [current_offset],edi
  166.         sub     edi,[code_start]
  167.         mov     [code_size],edi
  168.         and     [written_size],0
  169.         mov     edx,[output_file]
  170.         call    create
  171.         jc      write_failed
  172.         cmp     [output_format],3
  173.         jne     stub_written
  174.         mov     edx,[code_start]
  175.         mov     ecx,[stub_size]
  176.         sub     edx,ecx
  177.         add     [written_size],ecx
  178.         call    write
  179.       stub_written:
  180.         cmp     [output_format],2
  181.         jne     write_output
  182.         call    write_mz_header
  183.       write_output:
  184.         call    write_code
  185.       output_written:
  186.         call    close
  187.         cmp     [symbols_file],0
  188.         jne     dump_symbols
  189.         ret
  190.       write_code:
  191.         mov     eax,[written_size]
  192.         mov     [headers_size],eax
  193.         mov     edx,[code_start]
  194.         mov     ecx,[code_size]
  195.         add     [written_size],ecx
  196.         lea     eax,[edx+ecx]
  197.         call    write
  198.         jc      write_failed
  199.         ret
  200. format_directive:
  201.         cmp     edi,[code_start]
  202.         jne     unexpected_instruction
  203.         cmp     [virtual_data],0
  204.         jne     unexpected_instruction
  205.         cmp     [output_format],0
  206.         jne     unexpected_instruction
  207.         lods    byte [esi]
  208.         cmp     al,1Ch
  209.         je      format_prefix
  210.         cmp     al,18h
  211.         jne     invalid_argument
  212.         lods    byte [esi]
  213.       select_format:
  214.         mov     dl,al
  215.         shr     al,4
  216.         mov     [output_format],al
  217.         and     edx,0Fh
  218.         or      [format_flags],edx
  219.         cmp     al,2
  220.         je      format_mz
  221.         cmp     al,3
  222.         je      format_pe
  223.         cmp     al,4
  224.         je      format_coff
  225.         cmp     al,5
  226.         je      format_elf
  227.       format_defined:
  228.         cmp     byte [esi],86h
  229.         jne     instruction_assembled
  230.         cmp     word [esi+1],'('
  231.         jne     invalid_argument
  232.         mov     eax,[esi+3]
  233.         add     esi,3+4
  234.         mov     [file_extension],esi
  235.         lea     esi,[esi+eax+1]
  236.         jmp     instruction_assembled
  237.       format_prefix:
  238.         lods    byte [esi]
  239.         mov     ah,al
  240.         lods    byte [esi]
  241.         cmp     al,18h
  242.         jne     invalid_argument
  243.         lods    byte [esi]
  244.         mov     edx,eax
  245.         shr     dl,4
  246.         shr     dh,4
  247.         cmp     dl,dh
  248.         jne     invalid_argument
  249.         or      al,ah
  250.         jmp     select_format
  251. entry_directive:
  252.         bts     [format_flags],10h
  253.         jc      setting_already_specified
  254.         mov     al,[output_format]
  255.         cmp     al,2
  256.         je      mz_entry
  257.         cmp     al,3
  258.         je      pe_entry
  259.         cmp     al,5
  260.         jne     illegal_instruction
  261.         bt      [format_flags],0
  262.         jc      elf_entry
  263.         jmp     illegal_instruction
  264. stack_directive:
  265.         bts     [format_flags],11h
  266.         jc      setting_already_specified
  267.         mov     al,[output_format]
  268.         cmp     al,2
  269.         je      mz_stack
  270.         cmp     al,3
  271.         je      pe_stack
  272.         jmp     illegal_instruction
  273. heap_directive:
  274.         bts     [format_flags],12h
  275.         jc      setting_already_specified
  276.         mov     al,[output_format]
  277.         cmp     al,2
  278.         je      mz_heap
  279.         cmp     al,3
  280.         je      pe_heap
  281.         jmp     illegal_instruction
  282. segment_directive:
  283.         cmp     [virtual_data],0
  284.         jne     illegal_instruction
  285.         mov     al,[output_format]
  286.         cmp     al,2
  287.         je      mz_segment
  288.         cmp     al,5
  289.         je      elf_segment
  290.         jmp     illegal_instruction
  291. section_directive:
  292.         cmp     [virtual_data],0
  293.         jne     illegal_instruction
  294.         mov     al,[output_format]
  295.         cmp     al,3
  296.         je      pe_section
  297.         cmp     al,4
  298.         je      coff_section
  299.         cmp     al,5
  300.         je      elf_section
  301.         jmp     illegal_instruction
  302. public_directive:
  303.         mov     al,[output_format]
  304.         cmp     al,4
  305.         je      public_allowed
  306.         cmp     al,5
  307.         jne     illegal_instruction
  308.         bt      [format_flags],0
  309.         jc      illegal_instruction
  310.       public_allowed:
  311.         mov     [base_code],0C0h
  312.         lods    byte [esi]
  313.         cmp     al,2
  314.         je      public_label
  315.         cmp     al,1Dh
  316.         jne     invalid_argument
  317.         lods    byte [esi]
  318.         and     al,7
  319.         add     [base_code],al
  320.         lods    byte [esi]
  321.         cmp     al,2
  322.         jne     invalid_argument
  323.       public_label:
  324.         lods    dword [esi]
  325.         cmp     eax,0Fh
  326.         jb      invalid_use_of_symbol
  327.         je      reserved_word_used_as_symbol
  328.         inc     esi
  329.         mov     dx,[current_pass]
  330.         mov     [eax+18],dx
  331.         or      byte [eax+8],8
  332.         cmp     [symbols_file],0
  333.         je      public_reference_ok
  334.         cmp     [next_pass_needed],0
  335.         jne     public_reference_ok
  336.         mov     ebx,eax
  337.         call    store_label_reference
  338.         mov     eax,ebx
  339.       public_reference_ok:
  340.         mov     ebx,[free_additional_memory]
  341.         lea     edx,[ebx+10h]
  342.         cmp     edx,[structures_buffer]
  343.         jae     out_of_memory
  344.         mov     [free_additional_memory],edx
  345.         mov     [ebx+8],eax
  346.         mov     eax,[current_line]
  347.         mov     [ebx+0Ch],eax
  348.         lods    byte [esi]
  349.         cmp     al,86h
  350.         jne     invalid_argument
  351.         lods    word [esi]
  352.         cmp     ax,'('
  353.         jne     invalid_argument
  354.         mov     [ebx+4],esi
  355.         lods    dword [esi]
  356.         lea     esi,[esi+eax+1]
  357.         mov     al,[base_code]
  358.         mov     [ebx],al
  359.         jmp     instruction_assembled
  360. extrn_directive:
  361.         mov     al,[output_format]
  362.         cmp     al,4
  363.         je      extrn_allowed
  364.         cmp     al,5
  365.         jne     illegal_instruction
  366.         bt      [format_flags],0
  367.         jc      illegal_instruction
  368.       extrn_allowed:
  369.         lods    word [esi]
  370.         cmp     ax,'('
  371.         jne     invalid_argument
  372.         mov     ebx,esi
  373.         lods    dword [esi]
  374.         lea     esi,[esi+eax+1]
  375.         mov     edx,[free_additional_memory]
  376.         lea     eax,[edx+0Ch]
  377.         cmp     eax,[structures_buffer]
  378.         jae     out_of_memory
  379.         mov     [free_additional_memory],eax
  380.         mov     byte [edx],80h
  381.         mov     [edx+4],ebx
  382.         lods    byte [esi]
  383.         cmp     al,86h
  384.         jne     invalid_argument
  385.         lods    byte [esi]
  386.         cmp     al,2
  387.         jne     invalid_argument
  388.         lods    dword [esi]
  389.         cmp     eax,0Fh
  390.         jb      invalid_use_of_symbol
  391.         je      reserved_word_used_as_symbol
  392.         inc     esi
  393.         mov     ebx,eax
  394.         xor     ah,ah
  395.         lods    byte [esi]
  396.         cmp     al,':'
  397.         je      get_extrn_size
  398.         dec     esi
  399.         cmp     al,11h
  400.         jne     extrn_size_ok
  401.       get_extrn_size:
  402.         lods    word [esi]
  403.         cmp     al,11h
  404.         jne     invalid_argument
  405.       extrn_size_ok:
  406.         mov     [address_symbol],edx
  407.         mov     [label_size],ah
  408.         movzx   ecx,ah
  409.         mov     [edx+8],ecx
  410.         xor     eax,eax
  411.         xor     edx,edx
  412.         xor     ebp,ebp
  413.         mov     ch,2
  414.         test    [format_flags],8
  415.         jz      make_free_label
  416.         mov     ch,4
  417.         jmp     make_free_label
  418. mark_relocation:
  419.         cmp     [value_type],0
  420.         je      relocation_ok
  421.         cmp     [virtual_data],0
  422.         jne     relocation_ok
  423.         cmp     [output_format],2
  424.         je      mark_mz_relocation
  425.         cmp     [output_format],3
  426.         je      mark_pe_relocation
  427.         cmp     [output_format],4
  428.         je      mark_coff_relocation
  429.         cmp     [output_format],5
  430.         je      mark_elf_relocation
  431.       relocation_ok:
  432.         ret
  433. close_pass:
  434.         mov     al,[output_format]
  435.         cmp     al,3
  436.         je      close_pe
  437.         cmp     al,4
  438.         je      close_coff
  439.         cmp     al,5
  440.         je      close_elf
  441.         ret
  442.  
  443. format_mz:
  444.         mov     edx,[additional_memory]
  445.         push    edi
  446.         mov     edi,edx
  447.         mov     ecx,1Ch shr 2
  448.         xor     eax,eax
  449.         rep     stos dword [edi]
  450.         mov     [free_additional_memory],edi
  451.         pop     edi
  452.         mov     word [edx+0Ch],0FFFFh
  453.         mov     word [edx+10h],1000h
  454.         mov     [code_type],16
  455.         jmp     format_defined
  456. mark_mz_relocation:
  457.         push    eax ebx
  458.         inc     [number_of_relocations]
  459.         mov     ebx,[free_additional_memory]
  460.         mov     eax,edi
  461.         sub     eax,[code_start]
  462.         mov     [ebx],ax
  463.         shr     eax,16
  464.         shl     ax,12
  465.         mov     [ebx+2],ax
  466.         cmp     word [ebx],0FFFFh
  467.         jne     mz_relocation_ok
  468.         inc     word [ebx+2]
  469.         sub     word [ebx],10h
  470.       mz_relocation_ok:
  471.         add     ebx,4
  472.         cmp     ebx,[structures_buffer]
  473.         jae     out_of_memory
  474.         mov     [free_additional_memory],ebx
  475.         pop     ebx eax
  476.         ret
  477. mz_segment:
  478.         lods    byte [esi]
  479.         cmp     al,2
  480.         jne     invalid_argument
  481.         lods    dword [esi]
  482.         cmp     eax,0Fh
  483.         jb      invalid_use_of_symbol
  484.         je      reserved_word_used_as_symbol
  485.         inc     esi
  486.         mov     ebx,eax
  487.         mov     eax,edi
  488.         sub     eax,[code_start]
  489.         mov     ecx,0Fh
  490.         add     eax,0Fh
  491.         and     eax,1111b
  492.         sub     ecx,eax
  493.         mov     edx,edi
  494.         xor     eax,eax
  495.         rep     stos byte [edi]
  496.         mov     dword [org_origin],edi
  497.         mov     dword [org_origin+4],eax
  498.         mov     [org_origin_sign],al
  499.         mov     [org_registers],eax
  500.         mov     [org_start],edi
  501.         mov     eax,edx
  502.         call    undefined_data
  503.         mov     eax,edi
  504.         sub     eax,[code_start]
  505.         shr     eax,4
  506.         cmp     eax,10000h
  507.         jae     value_out_of_range
  508.         mov     edx,eax
  509.         mov     al,16
  510.         cmp     byte [esi],13h
  511.         jne     segment_type_ok
  512.         inc     esi
  513.         lods    byte [esi]
  514.       segment_type_ok:
  515.         mov     [code_type],al
  516.         mov     eax,edx
  517.         mov     ch,1
  518.         mov     [label_size],0
  519.         xor     edx,edx
  520.         xor     ebp,ebp
  521.         mov     [address_symbol],edx
  522.         jmp     make_free_label
  523. mz_entry:
  524.         lods    byte [esi]
  525.         cmp     al,'('
  526.         jne     invalid_argument
  527.         call    get_word_value
  528.         cmp     [value_type],1
  529.         je      initial_cs_ok
  530.         call    recoverable_invalid_address
  531.       initial_cs_ok:
  532.         mov     edx,[additional_memory]
  533.         mov     [edx+16h],ax
  534.         lods    byte [esi]
  535.         cmp     al,':'
  536.         jne     invalid_argument
  537.         lods    byte [esi]
  538.         cmp     al,'('
  539.         jne     invalid_argument
  540.         ja      invalid_address
  541.         call    get_word_value
  542.         cmp     [value_type],0
  543.         jne     invalid_use_of_symbol
  544.         mov     edx,[additional_memory]
  545.         mov     [edx+14h],ax
  546.         jmp     instruction_assembled
  547.       recoverable_invalid_address:
  548.         cmp     [error_line],0
  549.         jne     ignore_invalid_address
  550.         push    [current_line]
  551.         pop     [error_line]
  552.         mov     [error],invalid_address
  553.       ignore_invalid_address:
  554.         ret
  555. mz_stack:
  556.         lods    byte [esi]
  557.         cmp     al,'('
  558.         jne     invalid_argument
  559.         call    get_word_value
  560.         cmp     byte [esi],':'
  561.         je      stack_pointer
  562.         cmp     ax,10h
  563.         jb      invalid_value
  564.         cmp     [value_type],0
  565.         jne     invalid_use_of_symbol
  566.         mov     edx,[additional_memory]
  567.         mov     [edx+10h],ax
  568.         jmp     instruction_assembled
  569.       stack_pointer:
  570.         cmp     [value_type],1
  571.         je      initial_ss_ok
  572.         call    recoverable_invalid_address
  573.       initial_ss_ok:
  574.         mov     edx,[additional_memory]
  575.         mov     [edx+0Eh],ax
  576.         lods    byte [esi]
  577.         cmp     al,':'
  578.         jne     invalid_argument
  579.         lods    byte [esi]
  580.         cmp     al,'('
  581.         jne     invalid_argument
  582.         call    get_word_value
  583.         cmp     [value_type],0
  584.         jne     invalid_use_of_symbol
  585.         mov     edx,[additional_memory]
  586.         mov     [edx+10h],ax
  587.         bts     [format_flags],4
  588.         jmp     instruction_assembled
  589. mz_heap:
  590.         cmp     [output_format],2
  591.         jne     illegal_instruction
  592.         lods    byte [esi]
  593.         call    get_size_operator
  594.         cmp     ah,1
  595.         je      invalid_value
  596.         cmp     ah,2
  597.         ja      invalid_value
  598.         cmp     al,'('
  599.         jne     invalid_argument
  600.         call    get_word_value
  601.         cmp     [value_type],0
  602.         jne     invalid_use_of_symbol
  603.         mov     edx,[additional_memory]
  604.         mov     [edx+0Ch],ax
  605.         jmp     instruction_assembled
  606. write_mz_header:
  607.         mov     edx,[additional_memory]
  608.         bt      [format_flags],4
  609.         jc      mz_stack_ok
  610.         mov     eax,[real_code_size]
  611.         dec     eax
  612.         shr     eax,4
  613.         inc     eax
  614.         mov     [edx+0Eh],ax
  615.         shl     eax,4
  616.         movzx   ecx,word [edx+10h]
  617.         add     eax,ecx
  618.         mov     [real_code_size],eax
  619.       mz_stack_ok:
  620.         mov     edi,[free_additional_memory]
  621.         mov     eax,[number_of_relocations]
  622.         shl     eax,2
  623.         add     eax,1Ch
  624.         sub     edi,eax
  625.         xchg    edi,[free_additional_memory]
  626.         mov     ecx,0Fh
  627.         add     eax,0Fh
  628.         and     eax,1111b
  629.         sub     ecx,eax
  630.         xor     al,al
  631.         rep     stos byte [edi]
  632.         sub     edi,[free_additional_memory]
  633.         mov     ecx,edi
  634.         shr     edi,4
  635.         mov     word [edx],'MZ'         ; signature
  636.         mov     [edx+8],di              ; header size in paragraphs
  637.         mov     eax,[number_of_relocations]
  638.         mov     [edx+6],ax              ; number of relocation entries
  639.         mov     eax,[code_size]
  640.         add     eax,ecx
  641.         mov     esi,eax
  642.         shr     esi,9
  643.         and     eax,1FFh
  644.         inc     si
  645.         or      ax,ax
  646.         jnz     mz_size_ok
  647.         dec     si
  648.       mz_size_ok:
  649.         mov     [edx+2],ax              ; number of bytes in last page
  650.         mov     [edx+4],si              ; number of pages
  651.         mov     eax,[real_code_size]
  652.         dec     eax
  653.         shr     eax,4
  654.         inc     eax
  655.         mov     esi,[code_size]
  656.         dec     esi
  657.         shr     esi,4
  658.         inc     esi
  659.         sub     eax,esi
  660.         mov     [edx+0Ah],ax            ; minimum memory in addition to code
  661.         add     [edx+0Ch],ax            ; maximum memory in addition to code
  662.         salc
  663.         mov     ah,al
  664.         or      [edx+0Ch],ax
  665.         mov     word [edx+18h],1Ch      ; offset of relocation table
  666.         add     [written_size],ecx
  667.         call    write
  668.         jc      write_failed
  669.         ret
  670.  
  671. make_stub:
  672.         mov     [stub_file],edx
  673.         or      edx,edx
  674.         jnz     stub_from_file
  675.         push    esi
  676.         mov     edx,edi
  677.         xor     eax,eax
  678.         mov     ecx,20h
  679.         rep     stos dword [edi]
  680.         mov     eax,40h+default_stub_end-default_stub
  681.         mov     cx,100h+default_stub_end-default_stub
  682.         mov     word [edx],'MZ'
  683.         mov     byte [edx+4],1
  684.         mov     word [edx+2],ax
  685.         mov     byte [edx+8],4
  686.         mov     byte [edx+0Ah],10h
  687.         mov     word [edx+0Ch],0FFFFh
  688.         mov     word [edx+10h],cx
  689.         mov     word [edx+3Ch],ax
  690.         mov     byte [edx+18h],40h
  691.         lea     edi,[edx+40h]
  692.         mov     esi,default_stub
  693.         mov     ecx,default_stub_end-default_stub
  694.         rep     movs byte [edi],[esi]
  695.         pop     esi
  696.         jmp     stub_ok
  697.       default_stub:
  698.         use16
  699.         push    cs
  700.         pop     ds
  701.         mov     dx,stub_message-default_stub
  702.         mov     ah,9
  703.         int     21h
  704.         mov     ax,4C01h
  705.         int     21h
  706.       stub_message db 'This program cannot be run in DOS mode.',0Dh,0Ah,24h
  707.         rq      1
  708.       default_stub_end:
  709.         use32
  710.       stub_from_file:
  711.         push    esi
  712.         mov     esi,edx
  713.         call    open_binary_file
  714.         mov     edx,edi
  715.         mov     ecx,1Ch
  716.         mov     esi,edx
  717.         call    read
  718.         jc      binary_stub
  719.         cmp     word [esi],'MZ'
  720.         jne     binary_stub
  721.         add     edi,1Ch
  722.         movzx   ecx,word [esi+6]
  723.         add     ecx,11b
  724.         and     ecx,not 11b
  725.         add     ecx,(40h-1Ch) shr 2
  726.         lea     eax,[edi+ecx*4]
  727.         cmp     edi,[display_buffer]
  728.         jae     out_of_memory
  729.         xor     eax,eax
  730.         rep     stos dword [edi]
  731.         mov     edx,40h
  732.         xchg    dx,[esi+18h]
  733.         xor     al,al
  734.         call    lseek
  735.         movzx   ecx,word [esi+6]
  736.         shl     ecx,2
  737.         lea     edx,[esi+40h]
  738.         call    read
  739.         mov     edx,edi
  740.         sub     edx,esi
  741.         shr     edx,4
  742.         xchg    dx,[esi+8]
  743.         shl     edx,4
  744.         xor     al,al
  745.         call    lseek
  746.         movzx   ecx,word [esi+4]
  747.         dec     ecx
  748.         shl     ecx,9
  749.         movzx   edx,word [esi+2]
  750.         test    edx,edx
  751.         jnz     stub_header_size_ok
  752.         mov     dx,200h
  753.      stub_header_size_ok:
  754.         add     ecx,edx
  755.         mov     edx,edi
  756.         sub     ecx,eax
  757.         je      read_stub_code
  758.         jb      stub_code_ok
  759.         push    ecx
  760.         dec     ecx
  761.         shr     ecx,3
  762.         inc     ecx
  763.         shl     ecx,1
  764.         lea     eax,[edi+ecx*4]
  765.         cmp     eax,[display_buffer]
  766.         jae     out_of_memory
  767.         xor     eax,eax
  768.         rep     stos dword [edi]
  769.         pop     ecx
  770.      read_stub_code:
  771.         call    read
  772.      stub_code_ok:
  773.         call    close
  774.         mov     edx,edi
  775.         sub     edx,esi
  776.         mov     ax,dx
  777.         and     ax,1FFh
  778.         mov     [esi+2],ax
  779.         dec     edx
  780.         shr     edx,9
  781.         inc     edx
  782.         mov     [esi+4],dx
  783.         mov     eax,edi
  784.         sub     eax,esi
  785.         mov     [esi+3Ch],eax
  786.         pop     esi
  787.       stub_ok:
  788.         ret
  789.       binary_stub:
  790.         mov     esi,edi
  791.         mov     ecx,40h shr 2
  792.         xor     eax,eax
  793.         rep     stos dword [edi]
  794.         mov     al,2
  795.         xor     edx,edx
  796.         call    lseek
  797.         push    eax
  798.         xor     al,al
  799.         xor     edx,edx
  800.         call    lseek
  801.         mov     ecx,[esp]
  802.         add     ecx,40h+111b
  803.         and     ecx,not 111b
  804.         mov     ax,cx
  805.         and     ax,1FFh
  806.         mov     [esi+2],ax
  807.         lea     eax,[ecx+1FFh]
  808.         shr     eax,9
  809.         mov     [esi+4],ax
  810.         mov     [esi+3Ch],ecx
  811.         sub     ecx,40h
  812.         mov     eax,10000h
  813.         sub     eax,ecx
  814.         jbe     binary_heap_ok
  815.         shr     eax,4
  816.         mov     [esi+0Ah],ax
  817.       binary_heap_ok:
  818.         mov     word [esi],'MZ'
  819.         mov     byte [esi+8],4
  820.         mov     ax,0FFFFh
  821.         mov     [esi+0Ch],ax
  822.         dec     ax
  823.         mov     [esi+10h],ax
  824.         sub     ax,0Eh
  825.         mov     [esi+0Eh],ax
  826.         mov     [esi+16h],ax
  827.         mov     word [esi+14h],100h
  828.         mov     byte [esi+18h],40h
  829.         mov     eax,[display_buffer]
  830.         sub     eax,ecx
  831.         cmp     edi,eax
  832.         jae     out_of_memory
  833.         mov     edx,edi
  834.         shr     ecx,2
  835.         xor     eax,eax
  836.         rep     stos dword [edi]
  837.         pop     ecx
  838.         call    read
  839.         call    close
  840.         pop     esi
  841.         ret
  842.  
  843. format_pe:
  844.         xor     edx,edx
  845.         mov     [machine],14Ch
  846.         mov     [subsystem],3
  847.         mov     [subsystem_version],3 + 10 shl 16
  848.         mov     [image_base],400000h
  849.         and     [image_base_high],0
  850.         test    [format_flags],8
  851.         jz      pe_settings
  852.         mov     [machine],8664h
  853.         mov     [subsystem_version],5 + 0 shl 16
  854.       pe_settings:
  855.         cmp     byte [esi],84h
  856.         je      get_stub_name
  857.         cmp     byte [esi],80h
  858.         je      get_pe_base
  859.         cmp     byte [esi],1Bh
  860.         jne     pe_settings_ok
  861.         lods    byte [esi]
  862.         lods    byte [esi]
  863.         test    al,80h+40h
  864.         jz      subsystem_setting
  865.         cmp     al,80h
  866.         je      dll_flag
  867.         cmp     al,81h
  868.         je      wdm_flag
  869.         cmp     al,82h
  870.         je      large_flag
  871.         cmp     al,83h
  872.         je      nx_flag
  873.         jmp     pe_settings
  874.       dll_flag:
  875.         bts     [format_flags],8
  876.         jc      setting_already_specified
  877.         jmp     pe_settings
  878.       wdm_flag:
  879.         bts     [format_flags],9
  880.         jc      setting_already_specified
  881.         jmp     pe_settings
  882.       large_flag:
  883.         bts     [format_flags],11
  884.         jc      setting_already_specified
  885.         test    [format_flags],8
  886.         jnz     invalid_argument
  887.         jmp     pe_settings
  888.       nx_flag:
  889.         bts     [format_flags],12
  890.         jc      setting_already_specified
  891.         jmp     pe_settings
  892.       subsystem_setting:
  893.         bts     [format_flags],7
  894.         jc      setting_already_specified
  895.         and     ax,3Fh
  896.         mov     [subsystem],ax
  897.         cmp     ax,10
  898.         jb      subsystem_type_ok
  899.         or      [format_flags],4
  900.       subsystem_type_ok:
  901.         cmp     byte [esi],'('
  902.         jne     pe_settings
  903.         inc     esi
  904.         cmp     byte [esi],'.'
  905.         jne     invalid_value
  906.         inc     esi
  907.         push    edx
  908.         cmp     byte [esi+11],0
  909.         jne     invalid_value
  910.         cmp     byte [esi+10],2
  911.         ja      invalid_value
  912.         mov     dx,[esi+8]
  913.         cmp     dx,8000h
  914.         je      zero_version
  915.         mov     eax,[esi+4]
  916.         cmp     dx,7
  917.         jg      invalid_value
  918.         mov     cx,7
  919.         sub     cx,dx
  920.         mov     eax,[esi+4]
  921.         shr     eax,cl
  922.         mov     ebx,eax
  923.         shr     ebx,24
  924.         cmp     bl,100
  925.         jae     invalid_value
  926.         and     eax,0FFFFFFh
  927.         mov     ecx,100
  928.         mul     ecx
  929.         shrd    eax,edx,24
  930.         jnc     version_value_ok
  931.         inc     eax
  932.       version_value_ok:
  933.         shl     eax,16
  934.         mov     ax,bx
  935.         jmp     subsystem_version_ok
  936.       zero_version:
  937.         xor     eax,eax
  938.       subsystem_version_ok:
  939.         pop     edx
  940.         add     esi,13
  941.         mov     [subsystem_version],eax
  942.         jmp     pe_settings
  943.       get_pe_base:
  944.         bts     [format_flags],10
  945.         jc      setting_already_specified
  946.         lods    word [esi]
  947.         cmp     ah,'('
  948.         jne     invalid_argument
  949.         cmp     byte [esi],'.'
  950.         je      invalid_value
  951.         push    edx edi
  952.         add     edi,[stub_size]
  953.         test    [format_flags],4
  954.         jnz     get_peplus_base
  955.         call    get_dword_value
  956.         mov     [image_base],eax
  957.         jmp     pe_base_ok
  958.       get_peplus_base:
  959.         call    get_qword_value
  960.         mov     [image_base],eax
  961.         mov     [image_base_high],edx
  962.       pe_base_ok:
  963.         pop     edi edx
  964.         cmp     [value_type],0
  965.         jne     invalid_use_of_symbol
  966.         cmp     byte [esi],84h
  967.         jne     pe_settings_ok
  968.       get_stub_name:
  969.         lods    byte [esi]
  970.         lods    word [esi]
  971.         cmp     ax,'('
  972.         jne     invalid_argument
  973.         lods    dword [esi]
  974.         mov     edx,esi
  975.         add     esi,eax
  976.         inc     esi
  977.       pe_settings_ok:
  978.         mov     ebp,[stub_size]
  979.         or      ebp,ebp
  980.         jz      make_pe_stub
  981.         cmp     edx,[stub_file]
  982.         je      pe_stub_ok
  983.         sub     edi,[stub_size]
  984.         mov     [code_start],edi
  985.       make_pe_stub:
  986.         call    make_stub
  987.         mov     eax,edi
  988.         sub     eax,[code_start]
  989.         mov     [stub_size],eax
  990.         mov     [code_start],edi
  991.         mov     ebp,eax
  992.       pe_stub_ok:
  993.         mov     edx,edi
  994.         mov     ecx,18h+0E0h
  995.         test    [format_flags],4
  996.         jz      zero_pe_header
  997.         add     ecx,10h
  998.       zero_pe_header:
  999.         add     ebp,ecx
  1000.         shr     ecx,2
  1001.         xor     eax,eax
  1002.         rep     stos dword [edi]
  1003.         mov     word [edx],'PE'         ; signature
  1004.         mov     ax,[machine]
  1005.         mov     word [edx+4],ax
  1006.         mov     byte [edx+38h+1],10h    ; section alignment
  1007.         mov     byte [edx+3Ch+1],2      ; file alignment
  1008.         mov     byte [edx+40h],1        ; OS version
  1009.         mov     eax,[subsystem_version]
  1010.         mov     [edx+48h],eax
  1011.         mov     ax,[subsystem]
  1012.         mov     [edx+5Ch],ax
  1013.         cmp     ax,1
  1014.         jne     pe_alignment_ok
  1015.         mov     eax,20h
  1016.         mov     dword [edx+38h],eax
  1017.         mov     dword [edx+3Ch],eax
  1018.       pe_alignment_ok:
  1019.         mov     word [edx+1Ah],VERSION_MAJOR + VERSION_MINOR shl 8
  1020.         test    [format_flags],4
  1021.         jnz     init_peplus_specific
  1022.         mov     byte [edx+14h],0E0h     ; size of optional header
  1023.         mov     dword [edx+16h],10B010Fh; flags and magic value
  1024.         mov     eax,[image_base]
  1025.         mov     [edx+34h],eax
  1026.         mov     byte [edx+60h+1],10h    ; stack reserve
  1027.         mov     byte [edx+64h+1],10h    ; stack commit
  1028.         mov     byte [edx+68h+2],1      ; heap reserve
  1029.         mov     byte [edx+74h],16       ; number of directories
  1030.         jmp     pe_header_ok
  1031.       init_peplus_specific:
  1032.         mov     byte [edx+14h],0F0h     ; size of optional header
  1033.         mov     dword [edx+16h],20B002Fh; flags and magic value
  1034.         mov     eax,[image_base]
  1035.         mov     [edx+30h],eax
  1036.         mov     eax,[image_base_high]
  1037.         mov     [edx+34h],eax
  1038.         mov     byte [edx+60h+1],10h    ; stack reserve
  1039.         mov     byte [edx+68h+1],10h    ; stack commit
  1040.         mov     byte [edx+70h+2],1      ; heap reserve
  1041.         mov     byte [edx+84h],16       ; number of directories
  1042.       pe_header_ok:
  1043.         bsf     ecx,[edx+3Ch]
  1044.         imul    ebx,[number_of_sections],28h
  1045.         or      ebx,ebx
  1046.         jnz     reserve_space_for_section_headers
  1047.         mov     ebx,28h
  1048.       reserve_space_for_section_headers:
  1049.         add     ebx,ebp
  1050.         dec     ebx
  1051.         shr     ebx,cl
  1052.         inc     ebx
  1053.         shl     ebx,cl
  1054.         sub     ebx,ebp
  1055.         mov     ecx,ebx
  1056.         mov     eax,[display_buffer]
  1057.         sub     eax,ecx
  1058.         cmp     edi,eax
  1059.         jae     out_of_memory
  1060.         shr     ecx,2
  1061.         xor     eax,eax
  1062.         rep     stos dword [edi]
  1063.         mov     eax,edi
  1064.         sub     eax,[code_start]
  1065.         add     eax,[stub_size]
  1066.         mov     [edx+54h],eax           ; size of headers
  1067.         mov     ecx,[edx+38h]
  1068.         dec     ecx
  1069.         add     eax,ecx
  1070.         not     ecx
  1071.         and     eax,ecx
  1072.         bt      [format_flags],8
  1073.         jc      pe_entry_init_ok
  1074.         mov     [edx+28h],eax           ; entry point rva
  1075.       pe_entry_init_ok:
  1076.         and     [number_of_sections],0
  1077.         movzx   ebx,word [edx+14h]
  1078.         lea     ebx,[edx+18h+ebx]
  1079.         mov     [current_section],ebx
  1080.         mov     dword [ebx],'.fla'
  1081.         mov     dword [ebx+4],'t'
  1082.         mov     [ebx+14h],edi
  1083.         mov     [ebx+0Ch],eax
  1084.         mov     dword [ebx+24h],0E0000060h
  1085.         xor     ecx,ecx
  1086.         xor     bl,bl
  1087.         not     eax
  1088.         not     ecx
  1089.         not     bl
  1090.         add     eax,1
  1091.         adc     ecx,0
  1092.         adc     bl,0
  1093.         add     eax,edi
  1094.         adc     ecx,0
  1095.         adc     bl,0
  1096.         test    [format_flags],4
  1097.         jnz     peplus_org
  1098.         sub     eax,[edx+34h]
  1099.         sbb     ecx,0
  1100.         sbb     bl,0
  1101.         jmp     pe_org_ok
  1102.       peplus_org:
  1103.         sub     eax,[edx+30h]
  1104.         sbb     ecx,[edx+34h]
  1105.         sbb     bl,0
  1106.       pe_org_ok:
  1107.         test    [format_flags],8
  1108.         jnz     pe64_code
  1109.         mov     bh,2
  1110.         mov     [code_type],32
  1111.         jmp     pe_code_type_ok
  1112.       pe64_code:
  1113.         mov     bh,4
  1114.         mov     [code_type],64
  1115.       pe_code_type_ok:
  1116.         bt      [resolver_flags],0
  1117.         jc      pe_labels_type_ok
  1118.         xor     bh,bh
  1119.       pe_labels_type_ok:
  1120.         mov     [labels_type],bh
  1121.         mov     dword [org_origin],eax
  1122.         mov     dword [org_origin+4],ecx
  1123.         mov     [org_origin_sign],bl
  1124.         and     [org_registers],0
  1125.         mov     [org_start],edi
  1126.         bt      [format_flags],8
  1127.         jnc     dll_flag_ok
  1128.         or      byte [edx+16h+1],20h
  1129.       dll_flag_ok:
  1130.         bt      [format_flags],9
  1131.         jnc     wdm_flag_ok
  1132.         or      byte [edx+5Eh+1],20h
  1133.       wdm_flag_ok:
  1134.         bt      [format_flags],11
  1135.         jnc     large_flag_ok
  1136.         or      byte [edx+16h],20h
  1137.       large_flag_ok:
  1138.         bt      [format_flags],12
  1139.         jnc     nx_ok
  1140.         or      byte [edx+5Eh+1],1
  1141.       nx_ok:
  1142.         jmp     format_defined
  1143. pe_section:
  1144.         call    close_pe_section
  1145.         bts     [format_flags],5
  1146.         lea     ecx,[ebx+28h]
  1147.         add     edx,[edx+54h]
  1148.         sub     edx,[stub_size]
  1149.         cmp     ecx,edx
  1150.         jbe     new_section
  1151.         lea     ebx,[edx-28h]
  1152.         or      [next_pass_needed],-1
  1153.         push    edi
  1154.         mov     edi,ebx
  1155.         mov     ecx,28h shr 4
  1156.         xor     eax,eax
  1157.         rep     stos dword [edi]
  1158.         pop     edi
  1159.       new_section:
  1160.         mov     [ebx+0Ch],eax
  1161.         lods    word [esi]
  1162.         cmp     ax,'('
  1163.         jne     invalid_argument
  1164.         lea     edx,[esi+4]
  1165.         mov     ecx,[esi]
  1166.         lea     esi,[esi+4+ecx+1]
  1167.         cmp     ecx,8
  1168.         ja      name_too_long
  1169.         xor     eax,eax
  1170.         mov     [ebx],eax
  1171.         mov     [ebx+4],eax
  1172.         push    esi edi
  1173.         mov     edi,ebx
  1174.         mov     esi,edx
  1175.         rep     movs byte [edi],[esi]
  1176.         pop     edi esi
  1177.         and     dword [ebx+24h],0
  1178.         mov     [ebx+14h],edi
  1179.         mov     edx,[code_start]
  1180.         mov     eax,edi
  1181.         xor     ecx,ecx
  1182.         mov     [org_origin_sign],0
  1183.         sub     eax,[ebx+0Ch]
  1184.         sbb     ecx,0
  1185.         sbb     [org_origin_sign],0
  1186.         mov     [labels_type],2
  1187.         mov     [code_type],32
  1188.         test    [format_flags],8
  1189.         jz      pe_section_code_type_ok
  1190.         mov     [labels_type],4
  1191.         mov     [code_type],64
  1192.       pe_section_code_type_ok:
  1193.         test    [format_flags],4
  1194.         jnz     peplus_section_org
  1195.         sub     eax,[edx+34h]
  1196.         sbb     ecx,0
  1197.         sbb     [org_origin_sign],0
  1198.         bt      [resolver_flags],0
  1199.         jc      pe_section_org_ok
  1200.         mov     [labels_type],0
  1201.         jmp     pe_section_org_ok
  1202.       peplus_section_org:
  1203.         sub     eax,[edx+30h]
  1204.         sbb     ecx,[edx+34h]
  1205.         sbb     [org_origin_sign],0
  1206.         bt      [resolver_flags],0
  1207.         jc      pe_section_org_ok
  1208.         mov     [labels_type],0
  1209.       pe_section_org_ok:
  1210.         mov     dword [org_origin],eax
  1211.         mov     dword [org_origin+4],ecx
  1212.         and     [org_registers],0
  1213.         mov     [org_start],edi
  1214.       get_section_flags:
  1215.         lods    byte [esi]
  1216.         cmp     al,1Ah
  1217.         je      set_directory
  1218.         cmp     al,19h
  1219.         je      section_flag
  1220.         dec     esi
  1221.         jmp     instruction_assembled
  1222.       set_directory:
  1223.         movzx   eax,byte [esi]
  1224.         inc     esi
  1225.         mov     ecx,ebx
  1226.         test    [format_flags],4
  1227.         jnz     peplus_directory
  1228.         xchg    ecx,[edx+78h+eax*8]
  1229.         mov     dword [edx+78h+eax*8+4],-1
  1230.         jmp     pe_directory_set
  1231.       peplus_directory:
  1232.         xchg    ecx,[edx+88h+eax*8]
  1233.         mov     dword [edx+88h+eax*8+4],-1
  1234.       pe_directory_set:
  1235.         or      ecx,ecx
  1236.         jnz     data_already_defined
  1237.         push    ebx edx
  1238.         call    generate_pe_data
  1239.         pop     edx ebx
  1240.         jmp     get_section_flags
  1241.       section_flag:
  1242.         lods    byte [esi]
  1243.         cmp     al,9
  1244.         je      invalid_argument
  1245.         cmp     al,11
  1246.         je      invalid_argument
  1247.         mov     cl,al
  1248.         mov     eax,1
  1249.         shl     eax,cl
  1250.         test    dword [ebx+24h],eax
  1251.         jnz     setting_already_specified
  1252.         or      dword [ebx+24h],eax
  1253.         jmp     get_section_flags
  1254.       close_pe_section:
  1255.         mov     ebx,[current_section]
  1256.         mov     edx,[code_start]
  1257.         mov     eax,edi
  1258.         sub     eax,[ebx+14h]
  1259.         jnz     finish_section
  1260.         bt      [format_flags],5
  1261.         jc      finish_section
  1262.         mov     eax,[ebx+0Ch]
  1263.         ret
  1264.       finish_section:
  1265.         mov     [ebx+8],eax
  1266.         cmp     edi,[undefined_data_end]
  1267.         jne     align_section
  1268.         cmp     dword [edx+38h],1000h
  1269.         jb      align_section
  1270.         mov     edi,[undefined_data_start]
  1271.       align_section:
  1272.         and     [undefined_data_end],0
  1273.         mov     ebp,edi
  1274.         sub     ebp,[ebx+14h]
  1275.         mov     ecx,[edx+3Ch]
  1276.         dec     ecx
  1277.         lea     eax,[ebp+ecx]
  1278.         not     ecx
  1279.         and     eax,ecx
  1280.         mov     [ebx+10h],eax
  1281.         sub     eax,ebp
  1282.         mov     ecx,eax
  1283.         xor     al,al
  1284.         rep     stos byte [edi]
  1285.         mov     eax,[code_start]
  1286.         sub     eax,[stub_size]
  1287.         sub     [ebx+14h],eax
  1288.         mov     ecx,[ebx+10h]
  1289.         test    byte [ebx+24h],20h
  1290.         jz      pe_code_sum_ok
  1291.         add     [edx+1Ch],ecx
  1292.         cmp     dword [edx+2Ch],0
  1293.         jne     pe_code_sum_ok
  1294.         mov     eax,[ebx+0Ch]
  1295.         mov     [edx+2Ch],eax
  1296.       pe_code_sum_ok:
  1297.         test    byte [ebx+24h],40h
  1298.         jz      pe_data_sum_ok
  1299.         add     [edx+20h],ecx
  1300.         test    [format_flags],4
  1301.         jnz     pe_data_sum_ok
  1302.         cmp     dword [edx+30h],0
  1303.         jne     pe_data_sum_ok
  1304.         mov     eax,[ebx+0Ch]
  1305.         mov     [edx+30h],eax
  1306.       pe_data_sum_ok:
  1307.         mov     eax,[ebx+8]
  1308.         or      eax,eax
  1309.         jz      udata_ok
  1310.         cmp     dword [ebx+10h],0
  1311.         jne     udata_ok
  1312.         or      byte [ebx+24h],80h
  1313.         add     [edx+24h],ecx
  1314.       udata_ok:
  1315.         mov     ecx,[edx+38h]
  1316.         dec     ecx
  1317.         add     eax,ecx
  1318.         not     ecx
  1319.         and     eax,ecx
  1320.         add     eax,[ebx+0Ch]
  1321.         add     ebx,28h
  1322.         mov     [current_section],ebx
  1323.         inc     word [number_of_sections]
  1324.         jz      format_limitations_exceeded
  1325.         ret
  1326. data_directive:
  1327.         cmp     [output_format],3
  1328.         jne     illegal_instruction
  1329.         lods    byte [esi]
  1330.         cmp     al,1Ah
  1331.         je      predefined_data_type
  1332.         cmp     al,'('
  1333.         jne     invalid_argument
  1334.         call    get_byte_value
  1335.         cmp     al,16
  1336.         jb      data_type_ok
  1337.         jmp     invalid_value
  1338.       predefined_data_type:
  1339.         movzx   eax,byte [esi]
  1340.         inc     esi
  1341.       data_type_ok:
  1342.         mov     ebx,[current_section]
  1343.         mov     ecx,edi
  1344.         sub     ecx,[ebx+14h]
  1345.         add     ecx,[ebx+0Ch]
  1346.         mov     edx,[code_start]
  1347.         test    [format_flags],4
  1348.         jnz     peplus_data
  1349.         xchg    ecx,[edx+78h+eax*8]
  1350.         jmp     init_pe_data
  1351.       peplus_data:
  1352.         xchg    ecx,[edx+88h+eax*8]
  1353.       init_pe_data:
  1354.         or      ecx,ecx
  1355.         jnz     data_already_defined
  1356.         call    allocate_structure_data
  1357.         mov     word [ebx],data_directive-instruction_handler
  1358.         mov     [ebx+2],al
  1359.         mov     edx,[current_line]
  1360.         mov     [ebx+4],edx
  1361.         call    generate_pe_data
  1362.         jmp     instruction_assembled
  1363.       end_data:
  1364.         cmp     [output_format],3
  1365.         jne     illegal_instruction
  1366.         call    find_structure_data
  1367.         jc      unexpected_instruction
  1368.         movzx   eax,byte [ebx+2]
  1369.         mov     edx,[current_section]
  1370.         mov     ecx,edi
  1371.         sub     ecx,[edx+14h]
  1372.         add     ecx,[edx+0Ch]
  1373.         mov     edx,[code_start]
  1374.         test    [format_flags],4
  1375.         jnz     end_peplus_data
  1376.         sub     ecx,[edx+78h+eax*8]
  1377.         mov     [edx+78h+eax*8+4],ecx
  1378.         jmp     remove_structure_data
  1379.       end_peplus_data:
  1380.         sub     ecx,[edx+88h+eax*8]
  1381.         mov     [edx+88h+eax*8+4],ecx
  1382.         jmp     remove_structure_data
  1383. pe_entry:
  1384.         lods    byte [esi]
  1385.         cmp     al,'('
  1386.         jne     invalid_argument
  1387.         cmp     byte [esi],'.'
  1388.         je      invalid_value
  1389.         test    [format_flags],8
  1390.         jnz     pe64_entry
  1391.         call    get_dword_value
  1392.         mov     bl,2
  1393.         bt      [resolver_flags],0
  1394.         jc      check_pe_entry_label_type
  1395.         xor     bl,bl
  1396.       check_pe_entry_label_type:
  1397.         cmp     [value_type],bl
  1398.         je      pe_entry_ok
  1399.         call    recoverable_invalid_address
  1400.       pe_entry_ok:
  1401.       cdq
  1402.         test    [format_flags],4
  1403.         jnz     pe64_entry_type_ok
  1404.         mov     edx,[code_start]
  1405.         sub     eax,[edx+34h]
  1406.         mov     [edx+28h],eax
  1407.         jmp     instruction_assembled
  1408.       pe64_entry:
  1409.         call    get_qword_value
  1410.         mov     bl,4
  1411.         bt      [resolver_flags],0
  1412.         jc      check_pe64_entry_label_type
  1413.         xor     bl,bl
  1414.       check_pe64_entry_label_type:
  1415.         cmp     [value_type],bl
  1416.         je      pe64_entry_type_ok
  1417.         call    recoverable_invalid_address
  1418.       pe64_entry_type_ok:
  1419.         mov     ecx,[code_start]
  1420.         sub     eax,[ecx+30h]
  1421.         sbb     edx,[ecx+34h]
  1422.         jz      pe64_entry_range_ok
  1423.         call    recoverable_overflow
  1424.       pe64_entry_range_ok:
  1425.         mov     [ecx+28h],eax
  1426.         jmp     instruction_assembled
  1427. pe_stack:
  1428.         lods    byte [esi]
  1429.         cmp     al,'('
  1430.         jne     invalid_argument
  1431.         cmp     byte [esi],'.'
  1432.         je      invalid_value
  1433.         test    [format_flags],4
  1434.         jnz     peplus_stack
  1435.         call    get_count_value
  1436.         mov     edx,[code_start]
  1437.         mov     [edx+60h],eax
  1438.         cmp     byte [esi],','
  1439.         jne     default_stack_commit
  1440.         lods    byte [esi]
  1441.         lods    byte [esi]
  1442.         cmp     al,'('
  1443.         jne     invalid_argument
  1444.         cmp     byte [esi],'.'
  1445.         je      invalid_value
  1446.         call    get_count_value
  1447.         mov     edx,[code_start]
  1448.         mov     [edx+64h],eax
  1449.         cmp     eax,[edx+60h]
  1450.         ja      value_out_of_range
  1451.         jmp     instruction_assembled
  1452.       default_stack_commit:
  1453.         mov     dword [edx+64h],1000h
  1454.         mov     eax,[edx+60h]
  1455.         cmp     eax,1000h
  1456.         ja      instruction_assembled
  1457.         mov     dword [edx+64h],eax
  1458.         jmp     instruction_assembled
  1459.       peplus_stack:
  1460.         call    get_qword_value
  1461.         cmp     [value_type],0
  1462.         jne     invalid_use_of_symbol
  1463.         mov     ecx,[code_start]
  1464.         mov     [ecx+60h],eax
  1465.         mov     [ecx+64h],edx
  1466.         cmp     byte [esi],','
  1467.         jne     default_peplus_stack_commit
  1468.         lods    byte [esi]
  1469.         lods    byte [esi]
  1470.         cmp     al,'('
  1471.         jne     invalid_argument
  1472.         cmp     byte [esi],'.'
  1473.         je      invalid_value
  1474.         call    get_qword_value
  1475.         cmp     [value_type],0
  1476.         jne     invalid_use_of_symbol
  1477.         mov     ecx,[code_start]
  1478.         mov     [ecx+68h],eax
  1479.         mov     [ecx+6Ch],edx
  1480.         cmp     edx,[ecx+64h]
  1481.         ja      value_out_of_range
  1482.         jb      instruction_assembled
  1483.         cmp     eax,[ecx+60h]
  1484.         ja      value_out_of_range
  1485.         jmp     instruction_assembled
  1486.       default_peplus_stack_commit:
  1487.         mov     dword [ecx+68h],1000h
  1488.         cmp     dword [ecx+64h],0
  1489.         jne     instruction_assembled
  1490.         mov     eax,[ecx+60h]
  1491.         cmp     eax,1000h
  1492.         ja      instruction_assembled
  1493.         mov     dword [ecx+68h],eax
  1494.         jmp     instruction_assembled
  1495. pe_heap:
  1496.         lods    byte [esi]
  1497.         cmp     al,'('
  1498.         jne     invalid_argument
  1499.         cmp     byte [esi],'.'
  1500.         je      invalid_value
  1501.         test    [format_flags],4
  1502.         jnz     peplus_heap
  1503.         call    get_count_value
  1504.         mov     edx,[code_start]
  1505.         mov     [edx+68h],eax
  1506.         cmp     byte [esi],','
  1507.         jne     instruction_assembled
  1508.         lods    byte [esi]
  1509.         lods    byte [esi]
  1510.         cmp     al,'('
  1511.         jne     invalid_argument
  1512.         cmp     byte [esi],'.'
  1513.         je      invalid_value
  1514.         call    get_count_value
  1515.         mov     edx,[code_start]
  1516.         mov     [edx+6Ch],eax
  1517.         cmp     eax,[edx+68h]
  1518.         ja      value_out_of_range
  1519.         jmp     instruction_assembled
  1520.       peplus_heap:
  1521.         call    get_qword_value
  1522.         cmp     [value_type],0
  1523.         jne     invalid_use_of_symbol
  1524.         mov     ecx,[code_start]
  1525.         mov     [ecx+70h],eax
  1526.         mov     [ecx+74h],edx
  1527.         cmp     byte [esi],','
  1528.         jne     instruction_assembled
  1529.         lods    byte [esi]
  1530.         lods    byte [esi]
  1531.         cmp     al,'('
  1532.         jne     invalid_argument
  1533.         cmp     byte [esi],'.'
  1534.         je      invalid_value
  1535.         call    get_qword_value
  1536.         cmp     [value_type],0
  1537.         jne     invalid_use_of_symbol
  1538.         mov     ecx,[code_start]
  1539.         mov     [ecx+78h],eax
  1540.         mov     [ecx+7Ch],edx
  1541.         cmp     edx,[ecx+74h]
  1542.         ja      value_out_of_range
  1543.         jb      instruction_assembled
  1544.         cmp     eax,[edx+70h]
  1545.         ja      value_out_of_range
  1546.         jmp     instruction_assembled
  1547. mark_pe_relocation:
  1548.         push    eax ebx
  1549.         test    [format_flags],4
  1550.         jz      check_standard_pe_relocation_type
  1551.         cmp     [value_type],4
  1552.         je      pe_relocation_type_ok
  1553.       check_standard_pe_relocation_type:
  1554.         cmp     [value_type],2
  1555.         je      pe_relocation_type_ok
  1556.         call    recoverable_misuse
  1557.       pe_relocation_type_ok:
  1558.         mov     ebx,[current_section]
  1559.         mov     eax,edi
  1560.         sub     eax,[ebx+14h]
  1561.         add     eax,[ebx+0Ch]
  1562.         mov     ebx,[free_additional_memory]
  1563.         inc     [number_of_relocations]
  1564.         add     ebx,5
  1565.         cmp     ebx,[structures_buffer]
  1566.         jae     out_of_memory
  1567.         mov     [free_additional_memory],ebx
  1568.         mov     [ebx-5],eax
  1569.         cmp     [value_type],2
  1570.         je      fixup_32bit
  1571.         mov     byte [ebx-1],0Ah
  1572.         jmp     fixup_ok
  1573.       fixup_32bit:
  1574.         mov     byte [ebx-1],3
  1575.       fixup_ok:
  1576.         pop     ebx eax
  1577.         ret
  1578. generate_pe_data:
  1579.         cmp     al,2
  1580.         je      make_pe_resource
  1581.         cmp     al,5
  1582.         je      make_pe_fixups
  1583.         ret
  1584. make_pe_fixups:
  1585.         mov     edx,[code_start]
  1586.         and     byte [edx+16h],not 1
  1587.         or      byte [edx+5Eh],40h
  1588.         bts     [resolver_flags],0
  1589.         jc      fixups_ready
  1590.         or      [next_pass_needed],-1
  1591.       fixups_ready:
  1592.         and     [last_fixup_base],0
  1593.         call    make_fixups
  1594.         xchg    eax,[actual_fixups_size]
  1595.         sub     eax,[actual_fixups_size]
  1596.         ja      reserve_forward_fixups
  1597.         xor     eax,eax
  1598.       reserve_forward_fixups:
  1599.         mov     [reserved_fixups],edi
  1600.         add     edi,eax
  1601.         mov     [reserved_fixups_size],eax
  1602.         ret
  1603.       make_fixups:
  1604.         push    esi
  1605.         xor     ecx,ecx
  1606.         xchg    ecx,[number_of_relocations]
  1607.         mov     esi,[free_additional_memory]
  1608.         lea     eax,[ecx*5]
  1609.         sub     esi,eax
  1610.         mov     [free_additional_memory],esi
  1611.         mov     edx,[last_fixup_base]
  1612.         mov     ebp,edi
  1613.         jecxz   fixups_done
  1614.       make_fixup:
  1615.         cmp     [esi],edx
  1616.         jb      store_fixup
  1617.         mov     eax,edi
  1618.         sub     eax,ebp
  1619.         test    eax,11b
  1620.         jz      fixups_block
  1621.         xor     ax,ax
  1622.         stos    word [edi]
  1623.         add     dword [ebx],2
  1624.       fixups_block:
  1625.         mov     eax,edx
  1626.         add     edx,1000h
  1627.         cmp     [esi],edx
  1628.         jae     fixups_block
  1629.         stos    dword [edi]
  1630.         mov     ebx,edi
  1631.         mov     eax,8
  1632.         stos    dword [edi]
  1633.       store_fixup:
  1634.         add     dword [ebx],2
  1635.         mov     ah,[esi+1]
  1636.         and     ah,0Fh
  1637.         mov     al,[esi+4]
  1638.         shl     al,4
  1639.         or      ah,al
  1640.         mov     al,[esi]
  1641.         stos    word [edi]
  1642.         add     esi,5
  1643.         loop    make_fixup
  1644.       fixups_done:
  1645.         mov     [last_fixup_base],edx
  1646.         pop     esi
  1647.         mov     eax,edi
  1648.         sub     eax,ebp
  1649.         ret
  1650. make_pe_resource:
  1651.         cmp     byte [esi],82h
  1652.         jne     resource_done
  1653.         inc     esi
  1654.         lods    word [esi]
  1655.         cmp     ax,'('
  1656.         jne     invalid_argument
  1657.         lods    dword [esi]
  1658.         mov     edx,esi
  1659.         lea     esi,[esi+eax+1]
  1660.         cmp     [next_pass_needed],0
  1661.         je      resource_from_file
  1662.         cmp     [current_pass],0
  1663.         jne     reserve_space_for_resource
  1664.         and     [resource_size],0
  1665.       reserve_space_for_resource:
  1666.         add     edi,[resource_size]
  1667.         cmp     edi,[display_buffer]
  1668.         ja      out_of_memory
  1669.         jmp     resource_done
  1670.       resource_from_file:
  1671.         push    esi
  1672.         mov     esi,edx
  1673.         call    open_binary_file
  1674.         push    ebx
  1675.         mov     esi,[free_additional_memory]
  1676.         lea     eax,[esi+20h]
  1677.         cmp     eax,[structures_buffer]
  1678.         ja      out_of_memory
  1679.         mov     edx,esi
  1680.         mov     ecx,20h
  1681.         call    read
  1682.         jc      invalid_file_format
  1683.         xor     eax,eax
  1684.         cmp     [esi],eax
  1685.         jne     invalid_file_format
  1686.         mov     ax,0FFFFh
  1687.         cmp     [esi+8],eax
  1688.         jne     invalid_file_format
  1689.         cmp     [esi+12],eax
  1690.         jne     invalid_file_format
  1691.         mov     eax,20h
  1692.         cmp     [esi+4],eax
  1693.         jne     invalid_file_format
  1694.       read_resource_headers:
  1695.         test    eax,11b
  1696.         jz      resource_file_alignment_ok
  1697.         mov     edx,4
  1698.         and     eax,11b
  1699.         sub     edx,eax
  1700.         mov     al,1
  1701.         call    lseek
  1702.       resource_file_alignment_ok:
  1703.         mov     [esi],eax
  1704.         lea     edx,[esi+12]
  1705.         mov     ecx,8
  1706.         call    read
  1707.         jc      resource_headers_ok
  1708.         mov     ecx,[esi+16]
  1709.         add     [esi],ecx
  1710.         lea     edx,[esi+20]
  1711.         sub     ecx,8
  1712.         mov     [esi+16],ecx
  1713.         lea     eax,[edx+ecx]
  1714.         cmp     eax,[structures_buffer]
  1715.         ja      out_of_memory
  1716.         call    read
  1717.         jc      invalid_file_format
  1718.         mov     edx,[esi]
  1719.         add     edx,[esi+12]
  1720.         mov     eax,[esi+16]
  1721.         lea     ecx,[esi+20]
  1722.         lea     esi,[ecx+eax]
  1723.         add     ecx,2
  1724.         cmp     word [ecx-2],0FFFFh
  1725.         je      resource_header_type_ok
  1726.       check_resource_header_type:
  1727.         cmp     ecx,esi
  1728.         jae     invalid_file_format
  1729.         cmp     word [ecx],0
  1730.         je      resource_header_type_ok
  1731.         add     ecx,2
  1732.         jmp     check_resource_header_type
  1733.       resource_header_type_ok:
  1734.         add     ecx,2
  1735.         cmp     word [ecx],0FFFFh
  1736.         je      resource_header_name_ok
  1737.       check_resource_header_name:
  1738.         cmp     ecx,esi
  1739.         jae     invalid_file_format
  1740.         cmp     word [ecx],0
  1741.         je      resource_header_name_ok
  1742.         add     ecx,2
  1743.         jmp     check_resource_header_name
  1744.       resource_header_name_ok:
  1745.         xor     al,al
  1746.         call    lseek
  1747.         jmp     read_resource_headers
  1748.       resource_headers_ok:
  1749.         xor     eax,eax
  1750.         mov     [esi],eax
  1751.         mov     [resource_data],edi
  1752.         lea     eax,[edi+16]
  1753.         cmp     eax,[display_buffer]
  1754.         jae     out_of_memory
  1755.         xor     eax,eax
  1756.         stos    dword [edi]
  1757.         call    make_timestamp
  1758.         stos    dword [edi]
  1759.         xor     eax,eax
  1760.         stos    dword [edi]
  1761.         stos    dword [edi]
  1762.         xor     ebx,ebx
  1763.       make_type_name_directory:
  1764.         mov     esi,[free_additional_memory]
  1765.         xor     edx,edx
  1766.       find_type_name:
  1767.         cmp     dword [esi],0
  1768.         je      type_name_ok
  1769.         add     esi,20
  1770.         cmp     word [esi],0FFFFh
  1771.         je      check_next_type_name
  1772.         or      ebx,ebx
  1773.         jz      check_this_type_name
  1774.         xor     ecx,ecx
  1775.       compare_with_previous_type_name:
  1776.         mov     ax,[esi+ecx]
  1777.         cmp     ax,[ebx+ecx]
  1778.         ja      check_this_type_name
  1779.         jb      check_next_type_name
  1780.         add     ecx,2
  1781.         mov     ax,[esi+ecx]
  1782.         or      ax,[ebx+ecx]
  1783.         jnz     compare_with_previous_type_name
  1784.         jmp     check_next_type_name
  1785.       check_this_type_name:
  1786.         or      edx,edx
  1787.         jz      type_name_found
  1788.         xor     ecx,ecx
  1789.       compare_with_current_type_name:
  1790.         mov     ax,[esi+ecx]
  1791.         cmp     ax,[edx+ecx]
  1792.         ja      check_next_type_name
  1793.         jb      type_name_found
  1794.         add     ecx,2
  1795.         mov     ax,[esi+ecx]
  1796.         or      ax,[edx+ecx]
  1797.         jnz     compare_with_current_type_name
  1798.         jmp     same_type_name
  1799.       type_name_found:
  1800.         mov     edx,esi
  1801.       same_type_name:
  1802.         mov     [esi-16],edi
  1803.       check_next_type_name:
  1804.         mov     eax,[esi-4]
  1805.         add     esi,eax
  1806.         jmp     find_type_name
  1807.       type_name_ok:
  1808.         or      edx,edx
  1809.         jz      type_name_directory_done
  1810.         mov     ebx,edx
  1811.       make_type_name_entry:
  1812.         mov     eax,[resource_data]
  1813.         inc     word [eax+12]
  1814.         lea     eax,[edi+8]
  1815.         cmp     eax,[display_buffer]
  1816.         jae     out_of_memory
  1817.         mov     eax,ebx
  1818.         stos    dword [edi]
  1819.         xor     eax,eax
  1820.         stos    dword [edi]
  1821.         jmp     make_type_name_directory
  1822.       type_name_directory_done:
  1823.         mov     ebx,-1
  1824.       make_type_id_directory:
  1825.         mov     esi,[free_additional_memory]
  1826.         mov     edx,10000h
  1827.       find_type_id:
  1828.         cmp     dword [esi],0
  1829.         je      type_id_ok
  1830.         add     esi,20
  1831.         cmp     word [esi],0FFFFh
  1832.         jne     check_next_type_id
  1833.         movzx   eax,word [esi+2]
  1834.         cmp     eax,ebx
  1835.         jle     check_next_type_id
  1836.         cmp     eax,edx
  1837.         jg      check_next_type_id
  1838.         mov     edx,eax
  1839.         mov     [esi-16],edi
  1840.       check_next_type_id:
  1841.         mov     eax,[esi-4]
  1842.         add     esi,eax
  1843.         jmp     find_type_id
  1844.       type_id_ok:
  1845.         cmp     edx,10000h
  1846.         je      type_id_directory_done
  1847.         mov     ebx,edx
  1848.       make_type_id_entry:
  1849.         mov     eax,[resource_data]
  1850.         inc     word [eax+14]
  1851.         lea     eax,[edi+8]
  1852.         cmp     eax,[display_buffer]
  1853.         jae     out_of_memory
  1854.         mov     eax,ebx
  1855.         stos    dword [edi]
  1856.         xor     eax,eax
  1857.         stos    dword [edi]
  1858.         jmp     make_type_id_directory
  1859.       type_id_directory_done:
  1860.         mov     esi,[resource_data]
  1861.         add     esi,10h
  1862.         mov     ecx,[esi-4]
  1863.         or      cx,cx
  1864.         jz      resource_directories_ok
  1865.       make_resource_directories:
  1866.         push    ecx
  1867.         push    edi
  1868.         mov     edx,edi
  1869.         sub     edx,[resource_data]
  1870.         bts     edx,31
  1871.         mov     [esi+4],edx
  1872.         lea     eax,[edi+16]
  1873.         cmp     eax,[display_buffer]
  1874.         jae     out_of_memory
  1875.         xor     eax,eax
  1876.         stos    dword [edi]
  1877.         call    make_timestamp
  1878.         stos    dword [edi]
  1879.         xor     eax,eax
  1880.         stos    dword [edi]
  1881.         stos    dword [edi]
  1882.         mov     ebp,esi
  1883.         xor     ebx,ebx
  1884.       make_resource_name_directory:
  1885.         mov     esi,[free_additional_memory]
  1886.         xor     edx,edx
  1887.       find_resource_name:
  1888.         cmp     dword [esi],0
  1889.         je      resource_name_ok
  1890.         push    esi
  1891.         cmp     [esi+4],ebp
  1892.         jne     check_next_resource_name
  1893.         add     esi,20
  1894.         call    skip_resource_name
  1895.         cmp     word [esi],0FFFFh
  1896.         je      check_next_resource_name
  1897.         or      ebx,ebx
  1898.         jz      check_this_resource_name
  1899.         xor     ecx,ecx
  1900.       compare_with_previous_resource_name:
  1901.         mov     ax,[esi+ecx]
  1902.         cmp     ax,[ebx+ecx]
  1903.         ja      check_this_resource_name
  1904.         jb      check_next_resource_name
  1905.         add     ecx,2
  1906.         mov     ax,[esi+ecx]
  1907.         or      ax,[ebx+ecx]
  1908.         jnz     compare_with_previous_resource_name
  1909.         jmp     check_next_resource_name
  1910.       skip_resource_name:
  1911.         cmp     word [esi],0FFFFh
  1912.         jne     skip_unicode_string
  1913.         add     esi,4
  1914.         ret
  1915.       skip_unicode_string:
  1916.         add     esi,2
  1917.         cmp     word [esi-2],0
  1918.         jne     skip_unicode_string
  1919.         ret
  1920.       check_this_resource_name:
  1921.         or      edx,edx
  1922.         jz      resource_name_found
  1923.         xor     ecx,ecx
  1924.       compare_with_current_resource_name:
  1925.         mov     ax,[esi+ecx]
  1926.         cmp     ax,[edx+ecx]
  1927.         ja      check_next_resource_name
  1928.         jb      resource_name_found
  1929.         add     ecx,2
  1930.         mov     ax,[esi+ecx]
  1931.         or      ax,[edx+ecx]
  1932.         jnz     compare_with_current_resource_name
  1933.         jmp     same_resource_name
  1934.       resource_name_found:
  1935.         mov     edx,esi
  1936.       same_resource_name:
  1937.         mov     eax,[esp]
  1938.         mov     [eax+8],edi
  1939.       check_next_resource_name:
  1940.         pop     esi
  1941.         mov     eax,[esi+16]
  1942.         lea     esi,[esi+20+eax]
  1943.         jmp     find_resource_name
  1944.       resource_name_ok:
  1945.         or      edx,edx
  1946.         jz      resource_name_directory_done
  1947.         mov     ebx,edx
  1948.       make_resource_name_entry:
  1949.         mov     eax,[esp]
  1950.         inc     word [eax+12]
  1951.         lea     eax,[edi+8]
  1952.         cmp     eax,[display_buffer]
  1953.         jae     out_of_memory
  1954.         mov     eax,ebx
  1955.         stos    dword [edi]
  1956.         xor     eax,eax
  1957.         stos    dword [edi]
  1958.         jmp     make_resource_name_directory
  1959.       resource_name_directory_done:
  1960.         mov     ebx,-1
  1961.       make_resource_id_directory:
  1962.         mov     esi,[free_additional_memory]
  1963.         mov     edx,10000h
  1964.       find_resource_id:
  1965.         cmp     dword [esi],0
  1966.         je      resource_id_ok
  1967.         push    esi
  1968.         cmp     [esi+4],ebp
  1969.         jne     check_next_resource_id
  1970.         add     esi,20
  1971.         call    skip_resource_name
  1972.         cmp     word [esi],0FFFFh
  1973.         jne     check_next_resource_id
  1974.         movzx   eax,word [esi+2]
  1975.         cmp     eax,ebx
  1976.         jle     check_next_resource_id
  1977.         cmp     eax,edx
  1978.         jg      check_next_resource_id
  1979.         mov     edx,eax
  1980.         mov     eax,[esp]
  1981.         mov     [eax+8],edi
  1982.       check_next_resource_id:
  1983.         pop     esi
  1984.         mov     eax,[esi+16]
  1985.         lea     esi,[esi+20+eax]
  1986.         jmp     find_resource_id
  1987.       resource_id_ok:
  1988.         cmp     edx,10000h
  1989.         je      resource_id_directory_done
  1990.         mov     ebx,edx
  1991.       make_resource_id_entry:
  1992.         mov     eax,[esp]
  1993.         inc     word [eax+14]
  1994.         lea     eax,[edi+8]
  1995.         cmp     eax,[display_buffer]
  1996.         jae     out_of_memory
  1997.         mov     eax,ebx
  1998.         stos    dword [edi]
  1999.         xor     eax,eax
  2000.         stos    dword [edi]
  2001.         jmp     make_resource_id_directory
  2002.       resource_id_directory_done:
  2003.         pop     eax
  2004.         mov     esi,ebp
  2005.         pop     ecx
  2006.         add     esi,8
  2007.         dec     cx
  2008.         jnz     make_resource_directories
  2009.       resource_directories_ok:
  2010.         shr     ecx,16
  2011.         jnz     make_resource_directories
  2012.         mov     esi,[resource_data]
  2013.         add     esi,10h
  2014.         movzx   eax,word [esi-4]
  2015.         movzx   edx,word [esi-2]
  2016.         add     eax,edx
  2017.         lea     esi,[esi+eax*8]
  2018.         push    edi                     ; address of language directories
  2019.       update_resource_directories:
  2020.         cmp     esi,[esp]
  2021.         je      resource_directories_updated
  2022.         add     esi,10h
  2023.         mov     ecx,[esi-4]
  2024.         or      cx,cx
  2025.         jz      language_directories_ok
  2026.       make_language_directories:
  2027.         push    ecx
  2028.         push    edi
  2029.         mov     edx,edi
  2030.         sub     edx,[resource_data]
  2031.         bts     edx,31
  2032.         mov     [esi+4],edx
  2033.         lea     eax,[edi+16]
  2034.         cmp     eax,[display_buffer]
  2035.         jae     out_of_memory
  2036.         xor     eax,eax
  2037.         stos    dword [edi]
  2038.         call    make_timestamp
  2039.         stos    dword [edi]
  2040.         xor     eax,eax
  2041.         stos    dword [edi]
  2042.         stos    dword [edi]
  2043.         mov     ebp,esi
  2044.         mov     ebx,-1
  2045.       make_language_id_directory:
  2046.         mov     esi,[free_additional_memory]
  2047.         mov     edx,10000h
  2048.       find_language_id:
  2049.         cmp     dword [esi],0
  2050.         je      language_id_ok
  2051.         push    esi
  2052.         cmp     [esi+8],ebp
  2053.         jne     check_next_language_id
  2054.         add     esi,20
  2055.         mov     eax,esi
  2056.         call    skip_resource_name
  2057.         call    skip_resource_name
  2058.         neg     eax
  2059.         add     eax,esi
  2060.         and     eax,11b
  2061.         add     esi,eax
  2062.       get_language_id:
  2063.         movzx   eax,word [esi+6]
  2064.         cmp     eax,ebx
  2065.         jle     check_next_language_id
  2066.         cmp     eax,edx
  2067.         jge     check_next_language_id
  2068.         mov     edx,eax
  2069.         mov     eax,[esp]
  2070.         mov     dword [value],eax
  2071.       check_next_language_id:
  2072.         pop     esi
  2073.         mov     eax,[esi+16]
  2074.         lea     esi,[esi+20+eax]
  2075.         jmp     find_language_id
  2076.       language_id_ok:
  2077.         cmp     edx,10000h
  2078.         je      language_id_directory_done
  2079.         mov     ebx,edx
  2080.       make_language_id_entry:
  2081.         mov     eax,[esp]
  2082.         inc     word [eax+14]
  2083.         lea     eax,[edi+8]
  2084.         cmp     eax,[display_buffer]
  2085.         jae     out_of_memory
  2086.         mov     eax,ebx
  2087.         stos    dword [edi]
  2088.         mov     eax,dword [value]
  2089.         stos    dword [edi]
  2090.         jmp     make_language_id_directory
  2091.       language_id_directory_done:
  2092.         pop     eax
  2093.         mov     esi,ebp
  2094.         pop     ecx
  2095.         add     esi,8
  2096.         dec     cx
  2097.         jnz     make_language_directories
  2098.       language_directories_ok:
  2099.         shr     ecx,16
  2100.         jnz     make_language_directories
  2101.         jmp     update_resource_directories
  2102.       resource_directories_updated:
  2103.         mov     esi,[resource_data]
  2104.         push    edi
  2105.       make_name_strings:
  2106.         add     esi,10h
  2107.         movzx   eax,word [esi-2]
  2108.         movzx   ecx,word [esi-4]
  2109.         add     eax,ecx
  2110.         lea     eax,[esi+eax*8]
  2111.         push    eax
  2112.         or      ecx,ecx
  2113.         jz      string_entries_processed
  2114.       process_string_entries:
  2115.         push    ecx
  2116.         mov     edx,edi
  2117.         sub     edx,[resource_data]
  2118.         bts     edx,31
  2119.         xchg    [esi],edx
  2120.         mov     ebx,edi
  2121.         xor     ax,ax
  2122.         stos    word [edi]
  2123.       copy_string_data:
  2124.         lea     eax,[edi+2]
  2125.         cmp     eax,[display_buffer]
  2126.         jae     out_of_memory
  2127.         mov     ax,[edx]
  2128.         or      ax,ax
  2129.         jz      string_data_copied
  2130.         stos    word [edi]
  2131.         inc     word [ebx]
  2132.         add     edx,2
  2133.         jmp     copy_string_data
  2134.       string_data_copied:
  2135.         add     esi,8
  2136.         pop     ecx
  2137.         loop    process_string_entries
  2138.       string_entries_processed:
  2139.         pop     esi
  2140.         cmp     esi,[esp]
  2141.         jb      make_name_strings
  2142.         mov     eax,edi
  2143.         sub     eax,[resource_data]
  2144.         test    al,11b
  2145.         jz      resource_strings_alignment_ok
  2146.         xor     ax,ax
  2147.         stos    word [edi]
  2148.       resource_strings_alignment_ok:
  2149.         pop     edx
  2150.         pop     ebx                     ; address of language directories
  2151.         mov     ebp,edi
  2152.       update_language_directories:
  2153.         add     ebx,10h
  2154.         movzx   eax,word [ebx-2]
  2155.         movzx   ecx,word [ebx-4]
  2156.         add     ecx,eax
  2157.       make_data_records:
  2158.         push    ecx
  2159.         mov     esi,edi
  2160.         sub     esi,[resource_data]
  2161.         xchg    esi,[ebx+4]
  2162.         lea     eax,[edi+16]
  2163.         cmp     eax,[display_buffer]
  2164.         jae     out_of_memory
  2165.         mov     eax,esi
  2166.         stos    dword [edi]
  2167.         mov     eax,[esi+12]
  2168.         stos    dword [edi]
  2169.         xor     eax,eax
  2170.         stos    dword [edi]
  2171.         stos    dword [edi]
  2172.         pop     ecx
  2173.         add     ebx,8
  2174.         loop    make_data_records
  2175.         cmp     ebx,edx
  2176.         jb      update_language_directories
  2177.         pop     ebx                     ; file handle
  2178.         mov     esi,ebp
  2179.         mov     ebp,edi
  2180.       update_data_records:
  2181.         push    ebp
  2182.         mov     ecx,edi
  2183.         mov     eax,[current_section]
  2184.         sub     ecx,[eax+14h]
  2185.         add     ecx,[eax+0Ch]
  2186.         xchg    ecx,[esi]
  2187.         mov     edx,[ecx]
  2188.         xor     al,al
  2189.         call    lseek
  2190.         mov     edx,edi
  2191.         mov     ecx,[esi+4]
  2192.         add     edi,ecx
  2193.         cmp     edi,[display_buffer]
  2194.         ja      out_of_memory
  2195.         call    read
  2196.         mov     eax,edi
  2197.         sub     eax,[resource_data]
  2198.         and     eax,11b
  2199.         jz      resource_data_alignment_ok
  2200.         mov     ecx,4
  2201.         sub     ecx,eax
  2202.         xor     al,al
  2203.         rep     stos byte [edi]
  2204.       resource_data_alignment_ok:
  2205.         pop     ebp
  2206.         add     esi,16
  2207.         cmp     esi,ebp
  2208.         jb      update_data_records
  2209.         pop     esi
  2210.         call    close
  2211.         mov     eax,edi
  2212.         sub     eax,[resource_data]
  2213.         mov     [resource_size],eax
  2214.       resource_done:
  2215.         ret
  2216. close_pe:
  2217.         call    close_pe_section
  2218.         mov     edx,[code_start]
  2219.         mov     [edx+50h],eax
  2220.         call    make_timestamp
  2221.         mov     edx,[code_start]
  2222.         mov     [edx+8],eax
  2223.         mov     eax,[number_of_sections]
  2224.         mov     [edx+6],ax
  2225.         imul    eax,28h
  2226.         movzx   ecx,word [edx+14h]
  2227.         lea     eax,[eax+18h+ecx]
  2228.         add     eax,[stub_size]
  2229.         mov     ecx,[edx+3Ch]
  2230.         dec     ecx
  2231.         add     eax,ecx
  2232.         not     ecx
  2233.         and     eax,ecx
  2234.         cmp     eax,[edx+54h]
  2235.         je      pe_sections_ok
  2236.         or      [next_pass_needed],-1
  2237.       pe_sections_ok:
  2238.         xor     ecx,ecx
  2239.         add     edx,78h
  2240.         test    [format_flags],4
  2241.         jz      process_directories
  2242.         add     edx,10h
  2243.       process_directories:
  2244.         mov     eax,[edx+ecx*8]
  2245.         or      eax,eax
  2246.         jz      directory_ok
  2247.         cmp     dword [edx+ecx*8+4],-1
  2248.         jne     directory_ok
  2249.       section_data:
  2250.         mov     ebx,[edx+ecx*8]
  2251.         mov     eax,[ebx+0Ch]
  2252.         mov     [edx+ecx*8],eax         ; directory rva
  2253.         mov     eax,[ebx+8]
  2254.         mov     [edx+ecx*8+4],eax       ; directory size
  2255.       directory_ok:
  2256.         inc     cl
  2257.         cmp     cl,10h
  2258.         jb      process_directories
  2259.         cmp     dword [edx+5*8],0
  2260.         jne     finish_pe_relocations
  2261.         mov     eax,[number_of_relocations]
  2262.         shl     eax,2
  2263.         sub     [free_additional_memory],eax
  2264.         btr     [resolver_flags],0
  2265.         jnc     pe_relocations_ok
  2266.         or      [next_pass_needed],-1
  2267.         jmp     pe_relocations_ok
  2268.       finish_pe_relocations:
  2269.         push    edi
  2270.         mov     edi,[reserved_fixups]
  2271.         call    make_fixups
  2272.         pop     edi
  2273.         add     [actual_fixups_size],eax
  2274.         cmp     eax,[reserved_fixups_size]
  2275.         je      pe_relocations_ok
  2276.         or      [next_pass_needed],-1
  2277.       pe_relocations_ok:
  2278.         mov     ebx,[code_start]
  2279.         sub     ebx,[stub_size]
  2280.         mov     ecx,edi
  2281.         sub     ecx,ebx
  2282.         mov     ebp,ecx
  2283.         shr     ecx,1
  2284.         xor     eax,eax
  2285.         cdq
  2286.       calculate_checksum:
  2287.         mov     dx,[ebx]
  2288.         add     eax,edx
  2289.         mov     dx,ax
  2290.         shr     eax,16
  2291.         add     eax,edx
  2292.         add     ebx,2
  2293.         loop    calculate_checksum
  2294.         add     eax,ebp
  2295.         mov     ebx,[code_start]
  2296.         mov     [ebx+58h],eax
  2297.         ret
  2298.  
  2299. format_coff:
  2300.         mov     eax,[additional_memory]
  2301.         mov     [symbols_stream],eax
  2302.         mov     ebx,eax
  2303.         add     eax,20h
  2304.         cmp     eax,[structures_buffer]
  2305.         jae     out_of_memory
  2306.         mov     [free_additional_memory],eax
  2307.         xor     eax,eax
  2308.         mov     [ebx],al
  2309.         mov     [ebx+4],eax
  2310.         mov     [ebx+8],edi
  2311.         mov     al,4
  2312.         mov     [ebx+10h],eax
  2313.         mov     al,60h
  2314.         bt      [format_flags],0
  2315.         jnc     flat_section_flags_ok
  2316.         or      eax,0E0000000h
  2317.       flat_section_flags_ok:
  2318.         mov     dword [ebx+14h],eax
  2319.         mov     [current_section],ebx
  2320.         xor     eax,eax
  2321.         mov     [number_of_sections],eax
  2322.         call    setup_coff_section_org
  2323.         mov     [code_type],32
  2324.         test    [format_flags],8
  2325.         jz      format_defined
  2326.         mov     [code_type],64
  2327.         jmp     format_defined
  2328.       setup_coff_section_org:
  2329.         xor     eax,eax
  2330.         mov     dword [org_origin],edi
  2331.         mov     dword [org_origin+4],eax
  2332.         mov     [org_origin_sign],al
  2333.         mov     [org_registers],eax
  2334.         mov     [org_start],edi
  2335.         mov     [org_symbol],ebx
  2336.         test    [format_flags],8
  2337.         jnz     coff_64bit_labels
  2338.         mov     [labels_type],2
  2339.         ret
  2340.       coff_64bit_labels:
  2341.         mov     [labels_type],4
  2342.         ret
  2343.  
  2344. coff_section:
  2345.         call    close_coff_section
  2346.         mov     ebx,[free_additional_memory]
  2347.         lea     eax,[ebx+20h]
  2348.         cmp     eax,[structures_buffer]
  2349.         jae     out_of_memory
  2350.         mov     [free_additional_memory],eax
  2351.         mov     [current_section],ebx
  2352.         inc     [number_of_sections]
  2353.         xor     eax,eax
  2354.         mov     [ebx],al
  2355.         mov     [ebx+8],edi
  2356.         mov     [ebx+10h],eax
  2357.         mov     [ebx+14h],eax
  2358.         call    setup_coff_section_org
  2359.         lods    word [esi]
  2360.         cmp     ax,'('
  2361.         jne     invalid_argument
  2362.         mov     [ebx+4],esi
  2363.         mov     ecx,[esi]
  2364.         lea     esi,[esi+4+ecx+1]
  2365.         cmp     ecx,8
  2366.         ja      name_too_long
  2367.       coff_section_flags:
  2368.         cmp     byte [esi],8Ch
  2369.         je      coff_section_alignment
  2370.         cmp     byte [esi],19h
  2371.         jne     coff_section_settings_ok
  2372.         inc     esi
  2373.         lods    byte [esi]
  2374.         bt      [format_flags],0
  2375.         jc      coff_section_flag_ok
  2376.         cmp     al,7
  2377.         ja      invalid_argument
  2378.       coff_section_flag_ok:
  2379.         mov     cl,al
  2380.         mov     eax,1
  2381.         shl     eax,cl
  2382.         test    dword [ebx+14h],eax
  2383.         jnz     setting_already_specified
  2384.         or      dword [ebx+14h],eax
  2385.         jmp     coff_section_flags
  2386.       coff_section_alignment:
  2387.         bt      [format_flags],0
  2388.         jnc     invalid_argument
  2389.         inc     esi
  2390.         lods    byte [esi]
  2391.         cmp     al,'('
  2392.         jne     invalid_argument
  2393.         cmp     byte [esi],'.'
  2394.         je      invalid_value
  2395.         push    ebx
  2396.         call    get_count_value
  2397.         pop     ebx
  2398.         mov     edx,eax
  2399.         dec     edx
  2400.         test    eax,edx
  2401.         jnz     invalid_value
  2402.         or      eax,eax
  2403.         jz      invalid_value
  2404.         cmp     eax,2000h
  2405.         ja      invalid_value
  2406.         bsf     edx,eax
  2407.         inc     edx
  2408.         shl     edx,20
  2409.         or      [ebx+14h],edx
  2410.         xchg    [ebx+10h],eax
  2411.         or      eax,eax
  2412.         jnz     setting_already_specified
  2413.         jmp     coff_section_flags
  2414.       coff_section_settings_ok:
  2415.         cmp     dword [ebx+10h],0
  2416.         jne     instruction_assembled
  2417.         mov     dword [ebx+10h],4
  2418.         bt      [format_flags],0
  2419.         jnc     instruction_assembled
  2420.         or      dword [ebx+14h],300000h
  2421.         jmp     instruction_assembled
  2422.       close_coff_section:
  2423.         mov     ebx,[current_section]
  2424.         mov     eax,edi
  2425.         mov     edx,[ebx+8]
  2426.         sub     eax,edx
  2427.         mov     [ebx+0Ch],eax
  2428.         xor     eax,eax
  2429.         xchg    [undefined_data_end],eax
  2430.         cmp     eax,edi
  2431.         jne     coff_section_ok
  2432.         cmp     edx,[undefined_data_start]
  2433.         jne     coff_section_ok
  2434.         mov     edi,edx
  2435.         or      byte [ebx+14h],80h
  2436.       coff_section_ok:
  2437.         ret
  2438. mark_coff_relocation:
  2439.         cmp     [value_type],3
  2440.         je      coff_relocation_relative
  2441.         push    ebx eax
  2442.         test    [format_flags],8
  2443.         jnz     coff_64bit_relocation
  2444.         mov     al,6
  2445.         cmp     [value_type],5
  2446.         jne     coff_relocation
  2447.         inc     al
  2448.         jmp     coff_relocation
  2449.       coff_64bit_relocation:
  2450.         mov     al,1
  2451.         cmp     [value_type],4
  2452.         je      coff_relocation
  2453.         mov     al,2
  2454.         cmp     [value_type],5
  2455.         jne     coff_relocation
  2456.         inc     al
  2457.         jmp     coff_relocation
  2458.       coff_relocation_relative:
  2459.         push    ebx
  2460.         bt      [format_flags],0
  2461.         jnc     relative_ok
  2462.         mov     ebx,[current_section]
  2463.         mov     ebx,[ebx+8]
  2464.         sub     ebx,edi
  2465.         sub     eax,ebx
  2466.         add     eax,4
  2467.       relative_ok:
  2468.         push    eax
  2469.         mov     al,20
  2470.         test    [format_flags],8
  2471.         jnz     relative_coff_64bit_relocation
  2472.         cmp     [labels_type],2
  2473.         jne     invalid_use_of_symbol
  2474.         jmp     coff_relocation
  2475.       relative_coff_64bit_relocation:
  2476.         mov     al,4
  2477.         cmp     [labels_type],4
  2478.         jne     invalid_use_of_symbol
  2479.       coff_relocation:
  2480.         mov     ebx,[free_additional_memory]
  2481.         add     ebx,0Ch
  2482.         cmp     ebx,[structures_buffer]
  2483.         jae     out_of_memory
  2484.         mov     [free_additional_memory],ebx
  2485.         mov     byte [ebx-0Ch],al
  2486.         mov     eax,[current_section]
  2487.         mov     eax,[eax+8]
  2488.         neg     eax
  2489.         add     eax,edi
  2490.         mov     [ebx-0Ch+4],eax
  2491.         mov     eax,[symbol_identifier]
  2492.         mov     [ebx-0Ch+8],eax
  2493.         pop     eax ebx
  2494.         ret
  2495. close_coff:
  2496.         call    close_coff_section
  2497.         cmp     [next_pass_needed],0
  2498.         je      coff_closed
  2499.         mov     eax,[symbols_stream]
  2500.         mov     [free_additional_memory],eax
  2501.       coff_closed:
  2502.         ret
  2503. coff_formatter:
  2504.         sub     edi,[code_start]
  2505.         mov     [code_size],edi
  2506.         call    prepare_default_section
  2507.         mov     edi,[free_additional_memory]
  2508.         mov     ebx,edi
  2509.         mov     ecx,28h shr 2
  2510.         imul    ecx,[number_of_sections]
  2511.         add     ecx,14h shr 2
  2512.         lea     eax,[edi+ecx*4]
  2513.         cmp     eax,[structures_buffer]
  2514.         jae     out_of_memory
  2515.         xor     eax,eax
  2516.         rep     stos dword [edi]
  2517.         mov     word [ebx],14Ch
  2518.         test    [format_flags],8
  2519.         jz      coff_magic_ok
  2520.         mov     word [ebx],8664h
  2521.       coff_magic_ok:
  2522.         mov     word [ebx+12h],104h
  2523.         bt      [format_flags],0
  2524.         jnc     coff_flags_ok
  2525.         or      byte [ebx+12h],80h
  2526.       coff_flags_ok:
  2527.         push    ebx
  2528.         call    make_timestamp
  2529.         pop     ebx
  2530.         mov     [ebx+4],eax
  2531.         mov     eax,[number_of_sections]
  2532.         mov     [ebx+2],ax
  2533.         mov     esi,[symbols_stream]
  2534.         xor     eax,eax
  2535.         xor     ecx,ecx
  2536.       enumerate_symbols:
  2537.         cmp     esi,[free_additional_memory]
  2538.         je      symbols_enumerated
  2539.         mov     dl,[esi]
  2540.         or      dl,dl
  2541.         jz      enumerate_section
  2542.         cmp     dl,0C0h
  2543.         jae     enumerate_public
  2544.         cmp     dl,80h
  2545.         jae     enumerate_extrn
  2546.         add     esi,0Ch
  2547.         jmp     enumerate_symbols
  2548.       enumerate_section:
  2549.         mov     edx,eax
  2550.         shl     edx,8
  2551.         mov     [esi],edx
  2552.         inc     eax
  2553.         inc     ecx
  2554.         mov     [esi+1Eh],cx
  2555.         add     esi,20h
  2556.         jmp     enumerate_symbols
  2557.       enumerate_public:
  2558.         mov     edx,eax
  2559.         shl     edx,8
  2560.         mov     dl,[esi]
  2561.         mov     [esi],edx
  2562.         mov     edx,[esi+8]
  2563.         add     esi,10h
  2564.         inc     eax
  2565.         cmp     byte [edx+11],0
  2566.         je      enumerate_symbols
  2567.         mov     edx,[edx+20]
  2568.         cmp     byte [edx],0C0h
  2569.         jae     enumerate_symbols
  2570.         cmp     byte [edx],80h
  2571.         jb      enumerate_symbols
  2572.         inc     eax
  2573.         jmp     enumerate_symbols
  2574.       enumerate_extrn:
  2575.         mov     edx,eax
  2576.         shl     edx,8
  2577.         mov     dl,[esi]
  2578.         mov     [esi],edx
  2579.         add     esi,0Ch
  2580.         inc     eax
  2581.         jmp     enumerate_symbols
  2582.       prepare_default_section:
  2583.         mov     ebx,[symbols_stream]
  2584.         cmp     dword [ebx+0Ch],0
  2585.         jne     default_section_ok
  2586.         cmp     [number_of_sections],0
  2587.         je      default_section_ok
  2588.         mov     edx,ebx
  2589.       find_references_to_default_section:
  2590.         cmp     ebx,[free_additional_memory]
  2591.         jne     check_reference
  2592.         add     [symbols_stream],20h
  2593.         ret
  2594.       check_reference:
  2595.         mov     al,[ebx]
  2596.         or      al,al
  2597.         jz      skip_other_section
  2598.         cmp     al,0C0h
  2599.         jae     check_public_reference
  2600.         cmp     al,80h
  2601.         jae     next_reference
  2602.         cmp     edx,[ebx+8]
  2603.         je      default_section_ok
  2604.       next_reference:
  2605.         add     ebx,0Ch
  2606.         jmp     find_references_to_default_section
  2607.       check_public_reference:
  2608.         mov     eax,[ebx+8]
  2609.         add     ebx,10h
  2610.         test    byte [eax+8],1
  2611.         jz      find_references_to_default_section
  2612.         mov     cx,[current_pass]
  2613.         cmp     cx,[eax+16]
  2614.         jne     find_references_to_default_section
  2615.         cmp     edx,[eax+20]
  2616.         je      default_section_ok
  2617.         jmp     find_references_to_default_section
  2618.       skip_other_section:
  2619.         add     ebx,20h
  2620.         jmp     find_references_to_default_section
  2621.       default_section_ok:
  2622.         inc     [number_of_sections]
  2623.         ret
  2624.       symbols_enumerated:
  2625.         mov     [ebx+0Ch],eax
  2626.         mov     ebp,edi
  2627.         sub     ebp,ebx
  2628.         push    ebp
  2629.         lea     edi,[ebx+14h]
  2630.         mov     esi,[symbols_stream]
  2631.       find_section:
  2632.         cmp     esi,[free_additional_memory]
  2633.         je      sections_finished
  2634.         mov     al,[esi]
  2635.         or      al,al
  2636.         jz      section_found
  2637.         add     esi,0Ch
  2638.         cmp     al,0C0h
  2639.         jb      find_section
  2640.         add     esi,4
  2641.         jmp     find_section
  2642.       section_found:
  2643.         push    esi edi
  2644.         mov     esi,[esi+4]
  2645.         or      esi,esi
  2646.         jz      default_section
  2647.         mov     ecx,[esi]
  2648.         add     esi,4
  2649.         rep     movs byte [edi],[esi]
  2650.         jmp     section_name_ok
  2651.       default_section:
  2652.         mov     al,'.'
  2653.         stos    byte [edi]
  2654.         mov     eax,'flat'
  2655.         stos    dword [edi]
  2656.       section_name_ok:
  2657.         pop     edi esi
  2658.         mov     eax,[esi+0Ch]
  2659.         mov     [edi+10h],eax
  2660.         mov     eax,[esi+14h]
  2661.         mov     [edi+24h],eax
  2662.         test    al,80h
  2663.         jnz     section_ptr_ok
  2664.         mov     eax,[esi+8]
  2665.         sub     eax,[code_start]
  2666.         add     eax,ebp
  2667.         mov     [edi+14h],eax
  2668.       section_ptr_ok:
  2669.         mov     ebx,[code_start]
  2670.         mov     edx,[code_size]
  2671.         add     ebx,edx
  2672.         add     edx,ebp
  2673.         xor     ecx,ecx
  2674.         add     esi,20h
  2675.       find_relocations:
  2676.         cmp     esi,[free_additional_memory]
  2677.         je      section_relocations_done
  2678.         mov     al,[esi]
  2679.         or      al,al
  2680.         jz      section_relocations_done
  2681.         cmp     al,80h
  2682.         jb      add_relocation
  2683.         cmp     al,0C0h
  2684.         jb      next_relocation
  2685.         add     esi,10h
  2686.         jmp     find_relocations
  2687.       add_relocation:
  2688.         lea     eax,[ebx+0Ah]
  2689.         cmp     eax,[display_buffer]
  2690.         ja      out_of_memory
  2691.         mov     eax,[esi+4]
  2692.         mov     [ebx],eax
  2693.         mov     eax,[esi+8]
  2694.         mov     eax,[eax]
  2695.         shr     eax,8
  2696.         mov     [ebx+4],eax
  2697.         movzx   ax,byte [esi]
  2698.         mov     [ebx+8],ax
  2699.         add     ebx,0Ah
  2700.         inc     ecx
  2701.       next_relocation:
  2702.         add     esi,0Ch
  2703.         jmp     find_relocations
  2704.       section_relocations_done:
  2705.         cmp     ecx,10000h
  2706.         jb      section_relocations_count_16bit
  2707.         bt      [format_flags],0
  2708.         jnc     format_limitations_exceeded
  2709.         mov     word [edi+20h],0FFFFh
  2710.         or      dword [edi+24h],1000000h
  2711.         mov     [edi+18h],edx
  2712.         push    esi edi
  2713.         push    ecx
  2714.         lea     esi,[ebx-1]
  2715.         add     ebx,0Ah
  2716.         lea     edi,[ebx-1]
  2717.         imul    ecx,0Ah
  2718.         std
  2719.         rep     movs byte [edi],[esi]
  2720.         cld
  2721.         pop     ecx
  2722.         inc     esi
  2723.         inc     ecx
  2724.         mov     [esi],ecx
  2725.         xor     eax,eax
  2726.         mov     [esi+4],eax
  2727.         mov     [esi+8],ax
  2728.         pop     edi esi
  2729.         jmp     section_relocations_ok
  2730.       section_relocations_count_16bit:
  2731.         mov     [edi+20h],cx
  2732.         jcxz    section_relocations_ok
  2733.         mov     [edi+18h],edx
  2734.       section_relocations_ok:
  2735.         sub     ebx,[code_start]
  2736.         mov     [code_size],ebx
  2737.         add     edi,28h
  2738.         jmp     find_section
  2739.       sections_finished:
  2740.         mov     edx,[free_additional_memory]
  2741.         mov     ebx,[code_size]
  2742.         add     ebp,ebx
  2743.         mov     [edx+8],ebp
  2744.         add     ebx,[code_start]
  2745.         mov     edi,ebx
  2746.         mov     ecx,[edx+0Ch]
  2747.         imul    ecx,12h shr 1
  2748.         xor     eax,eax
  2749.         shr     ecx,1
  2750.         jnc     zero_symbols_table
  2751.         stos    word [edi]
  2752.       zero_symbols_table:
  2753.         rep     stos dword [edi]
  2754.         mov     edx,edi
  2755.         stos    dword [edi]
  2756.         mov     esi,[symbols_stream]
  2757.       make_symbols_table:
  2758.         cmp     esi,[free_additional_memory]
  2759.         je      symbols_table_ok
  2760.         mov     al,[esi]
  2761.         cmp     al,0C0h
  2762.         jae     add_public_symbol
  2763.         cmp     al,80h
  2764.         jae     add_extrn_symbol
  2765.         or      al,al
  2766.         jz      add_section_symbol
  2767.         add     esi,0Ch
  2768.         jmp     make_symbols_table
  2769.       add_section_symbol:
  2770.         call    store_symbol_name
  2771.         movzx   eax,word [esi+1Eh]
  2772.         mov     [ebx+0Ch],ax
  2773.         mov     byte [ebx+10h],3
  2774.         add     esi,20h
  2775.         add     ebx,12h
  2776.         jmp     make_symbols_table
  2777.       add_extrn_symbol:
  2778.         call    store_symbol_name
  2779.         mov     byte [ebx+10h],2
  2780.         add     esi,0Ch
  2781.         add     ebx,12h
  2782.         jmp     make_symbols_table
  2783.       add_public_symbol:
  2784.         call    store_symbol_name
  2785.         mov     eax,[esi+0Ch]
  2786.         mov     [current_line],eax
  2787.         mov     eax,[esi+8]
  2788.         test    byte [eax+8],1
  2789.         jz      undefined_coff_public
  2790.         mov     cx,[current_pass]
  2791.         cmp     cx,[eax+16]
  2792.         jne     undefined_coff_public
  2793.         mov     cl,[eax+11]
  2794.         or      cl,cl
  2795.         jz      public_constant
  2796.         test    [format_flags],8
  2797.         jnz     check_64bit_public_symbol
  2798.         cmp     cl,2
  2799.         je      public_symbol_type_ok
  2800.         jmp     invalid_use_of_symbol
  2801.       undefined_coff_public:
  2802.         mov     [error_info],eax
  2803.         jmp     undefined_symbol
  2804.       check_64bit_public_symbol:
  2805.         cmp     cl,4
  2806.         jne     invalid_use_of_symbol
  2807.       public_symbol_type_ok:
  2808.         mov     ecx,[eax+20]
  2809.         cmp     byte [ecx],80h
  2810.         je      alias_symbol
  2811.         cmp     byte [ecx],0
  2812.         jne     invalid_use_of_symbol
  2813.         mov     cx,[ecx+1Eh]
  2814.         mov     [ebx+0Ch],cx
  2815.       public_symbol_section_ok:
  2816.         movzx   ecx,byte [eax+9]
  2817.         shr     cl,1
  2818.         and     cl,1
  2819.         neg     ecx
  2820.         cmp     ecx,[eax+4]
  2821.         jne     value_out_of_range
  2822.         xor     ecx,[eax]
  2823.         js      value_out_of_range
  2824.         mov     eax,[eax]
  2825.         mov     [ebx+8],eax
  2826.         mov     al,2
  2827.         cmp     byte [esi],0C0h
  2828.         je      store_symbol_class
  2829.         inc     al
  2830.         cmp     byte [esi],0C1h
  2831.         je      store_symbol_class
  2832.         mov     al,105
  2833.       store_symbol_class:
  2834.         mov     byte [ebx+10h],al
  2835.         add     esi,10h
  2836.         add     ebx,12h
  2837.         jmp     make_symbols_table
  2838.       alias_symbol:
  2839.         bt      [format_flags],0
  2840.         jnc     invalid_use_of_symbol
  2841.         mov     ecx,[eax]
  2842.         or      ecx,[eax+4]
  2843.         jnz     invalid_use_of_symbol
  2844.         mov     byte [ebx+10h],69h
  2845.         mov     byte [ebx+11h],1
  2846.         add     ebx,12h
  2847.         mov     ecx,[eax+20]
  2848.         mov     ecx,[ecx]
  2849.         shr     ecx,8
  2850.         mov     [ebx],ecx
  2851.         mov     byte [ebx+4],3
  2852.         add     esi,10h
  2853.         add     ebx,12h
  2854.         jmp     make_symbols_table
  2855.       public_constant:
  2856.         mov     word [ebx+0Ch],0FFFFh
  2857.         jmp     public_symbol_section_ok
  2858.       symbols_table_ok:
  2859.         mov     eax,edi
  2860.         sub     eax,edx
  2861.         mov     [edx],eax
  2862.         sub     edi,[code_start]
  2863.         mov     [code_size],edi
  2864.         and     [written_size],0
  2865.         mov     edx,[output_file]
  2866.         call    create
  2867.         jc      write_failed
  2868.         mov     edx,[free_additional_memory]
  2869.         pop     ecx
  2870.         add     [written_size],ecx
  2871.         call    write
  2872.         jc      write_failed
  2873.         jmp     write_output
  2874.       store_symbol_name:
  2875.         push    esi
  2876.         mov     esi,[esi+4]
  2877.         or      esi,esi
  2878.         jz      default_name
  2879.         lods    dword [esi]
  2880.         mov     ecx,eax
  2881.         cmp     ecx,8
  2882.         ja      add_string
  2883.         push    edi
  2884.         mov     edi,ebx
  2885.         rep     movs byte [edi],[esi]
  2886.         pop     edi esi
  2887.         ret
  2888.       default_name:
  2889.         mov     dword [ebx],'.fla'
  2890.         mov     dword [ebx+4],'t'
  2891.         pop     esi
  2892.         ret
  2893.       add_string:
  2894.         mov     eax,edi
  2895.         sub     eax,edx
  2896.         mov     [ebx+4],eax
  2897.         inc     ecx
  2898.         rep     movs byte [edi],[esi]
  2899.         pop     esi
  2900.         ret
  2901.  
  2902. format_elf:
  2903.         test    [format_flags],8
  2904.         jnz     format_elf64
  2905.         mov     edx,edi
  2906.         mov     ecx,34h shr 2
  2907.         lea     eax,[edi+ecx*4]
  2908.         cmp     eax,[display_buffer]
  2909.         jae     out_of_memory
  2910.         xor     eax,eax
  2911.         rep     stos dword [edi]
  2912.         mov     dword [edx],7Fh + 'ELF' shl 8
  2913.         mov     al,1
  2914.         mov     [edx+4],al
  2915.         mov     [edx+5],al
  2916.         mov     [edx+6],al
  2917.         mov     [edx+14h],al
  2918.         mov     byte [edx+12h],3
  2919.         mov     byte [edx+28h],34h
  2920.         mov     byte [edx+2Eh],28h
  2921.         mov     [code_type],32
  2922.         cmp     word [esi],1D19h
  2923.         je      format_elf_exe
  2924.       elf_header_ok:
  2925.         mov     byte [edx+10h],1
  2926.         mov     eax,[additional_memory]
  2927.         mov     [symbols_stream],eax
  2928.         mov     ebx,eax
  2929.         add     eax,20h
  2930.         cmp     eax,[structures_buffer]
  2931.         jae     out_of_memory
  2932.         mov     [free_additional_memory],eax
  2933.         xor     eax,eax
  2934.         mov     [current_section],ebx
  2935.         mov     [number_of_sections],eax
  2936.         mov     [ebx],al
  2937.         mov     [ebx+4],eax
  2938.         mov     [ebx+8],edi
  2939.         mov     al,111b
  2940.         mov     [ebx+14h],eax
  2941.         mov     al,4
  2942.         mov     [ebx+10h],eax
  2943.         call    setup_coff_section_org
  2944.         test    [format_flags],8
  2945.         jz      format_defined
  2946.         mov     byte [ebx+10h],8
  2947.         jmp     format_defined
  2948.       format_elf64:
  2949.         mov     edx,edi
  2950.         mov     ecx,40h shr 2
  2951.         lea     eax,[edi+ecx*4]
  2952.         cmp     eax,[display_buffer]
  2953.         jae     out_of_memory
  2954.         xor     eax,eax
  2955.         rep     stos dword [edi]
  2956.         mov     dword [edx],7Fh + 'ELF' shl 8
  2957.         mov     al,1
  2958.         mov     [edx+5],al
  2959.         mov     [edx+6],al
  2960.         mov     [edx+14h],al
  2961.         mov     byte [edx+4],2
  2962.         mov     byte [edx+12h],62
  2963.         mov     byte [edx+34h],40h
  2964.         mov     byte [edx+3Ah],40h
  2965.         mov     [code_type],64
  2966.         cmp     word [esi],1D19h
  2967.         jne     elf_header_ok
  2968.         jmp     format_elf64_exe
  2969. elf_section:
  2970.         bt      [format_flags],0
  2971.         jc      illegal_instruction
  2972.         call    close_coff_section
  2973.         mov     ebx,[free_additional_memory]
  2974.         lea     eax,[ebx+20h]
  2975.         cmp     eax,[structures_buffer]
  2976.         jae     out_of_memory
  2977.         mov     [free_additional_memory],eax
  2978.         mov     [current_section],ebx
  2979.         inc     word [number_of_sections]
  2980.         jz      format_limitations_exceeded
  2981.         xor     eax,eax
  2982.         mov     [ebx],al
  2983.         mov     [ebx+8],edi
  2984.         mov     [ebx+10h],eax
  2985.         mov     al,10b
  2986.         mov     [ebx+14h],eax
  2987.         call    setup_coff_section_org
  2988.         lods    word [esi]
  2989.         cmp     ax,'('
  2990.         jne     invalid_argument
  2991.         mov     [ebx+4],esi
  2992.         mov     ecx,[esi]
  2993.         lea     esi,[esi+4+ecx+1]
  2994.       elf_section_flags:
  2995.         cmp     byte [esi],8Ch
  2996.         je      elf_section_alignment
  2997.         cmp     byte [esi],19h
  2998.         jne     elf_section_settings_ok
  2999.         inc     esi
  3000.         lods    byte [esi]
  3001.         sub     al,28
  3002.         xor     al,11b
  3003.         test    al,not 10b
  3004.         jnz     invalid_argument
  3005.         mov     cl,al
  3006.         mov     al,1
  3007.         shl     al,cl
  3008.         test    byte [ebx+14h],al
  3009.         jnz     setting_already_specified
  3010.         or      byte [ebx+14h],al
  3011.         jmp     elf_section_flags
  3012.       elf_section_alignment:
  3013.         inc     esi
  3014.         lods    byte [esi]
  3015.         cmp     al,'('
  3016.         jne     invalid_argument
  3017.         cmp     byte [esi],'.'
  3018.         je      invalid_value
  3019.         push    ebx
  3020.         call    get_count_value
  3021.         pop     ebx
  3022.         mov     edx,eax
  3023.         dec     edx
  3024.         test    eax,edx
  3025.         jnz     invalid_value
  3026.         or      eax,eax
  3027.         jz      invalid_value
  3028.         xchg    [ebx+10h],eax
  3029.         or      eax,eax
  3030.         jnz     setting_already_specified
  3031.         jmp     elf_section_flags
  3032.       elf_section_settings_ok:
  3033.         cmp     dword [ebx+10h],0
  3034.         jne     instruction_assembled
  3035.         mov     dword [ebx+10h],4
  3036.         test    [format_flags],8
  3037.         jz      instruction_assembled
  3038.         mov     byte [ebx+10h],8
  3039.         jmp     instruction_assembled
  3040. mark_elf_relocation:
  3041.         cmp     [value_type],3
  3042.         je      elf_relocation_relative
  3043.         cmp     [value_type],7
  3044.         je      elf_relocation_relative
  3045.         push    ebx eax
  3046.         cmp     [value_type],5
  3047.         je      elf_gotoff_relocation
  3048.         ja      invalid_use_of_symbol
  3049.         mov     al,1                    ; R_386_32 / R_AMD64_64
  3050.         test    [format_flags],8
  3051.         jz      coff_relocation
  3052.         cmp     [value_type],4
  3053.         je      coff_relocation
  3054.         mov     al,11                   ; R_AMD64_32S
  3055.         jmp     coff_relocation
  3056.       elf_gotoff_relocation:
  3057.         test    [format_flags],8
  3058.         jnz     invalid_use_of_symbol
  3059.         mov     al,9                    ; R_386_GOTOFF
  3060.         jmp     coff_relocation
  3061.       elf_relocation_relative:
  3062.         cmp     [labels_type],0
  3063.         je      invalid_use_of_symbol
  3064.         push    ebx
  3065.         mov     ebx,[current_section]
  3066.         mov     ebx,[ebx+8]
  3067.         sub     ebx,edi
  3068.         sub     eax,ebx
  3069.         push    eax
  3070.         mov     al,2                    ; R_386_PC32 / R_AMD64_PC32
  3071.         cmp     [value_type],3
  3072.         je      coff_relocation
  3073.         mov     al,4                    ; R_386_PLT32 / R_AMD64_PLT32
  3074.         jmp     coff_relocation
  3075. close_elf:
  3076.         bt      [format_flags],0
  3077.         jc      close_elf_exe
  3078.         call    close_coff_section
  3079.         cmp     [next_pass_needed],0
  3080.         je      elf_closed
  3081.         mov     eax,[symbols_stream]
  3082.         mov     [free_additional_memory],eax
  3083.       elf_closed:
  3084.         ret
  3085. elf_formatter:
  3086.         push    edi
  3087.         call    prepare_default_section
  3088.         mov     esi,[symbols_stream]
  3089.         mov     edi,[free_additional_memory]
  3090.         xor     eax,eax
  3091.         mov     ecx,4
  3092.         rep     stos dword [edi]
  3093.         test    [format_flags],8
  3094.         jz      find_first_section
  3095.         mov     ecx,2
  3096.         rep     stos dword [edi]
  3097.       find_first_section:
  3098.         mov     al,[esi]
  3099.         or      al,al
  3100.         jz      first_section_found
  3101.         cmp     al,0C0h
  3102.         jb      skip_other_symbol
  3103.         add     esi,4
  3104.       skip_other_symbol:
  3105.         add     esi,0Ch
  3106.         jmp     find_first_section
  3107.       first_section_found:
  3108.         mov     ebx,esi
  3109.         mov     ebp,esi
  3110.         add     esi,20h
  3111.         xor     ecx,ecx
  3112.         xor     edx,edx
  3113.       find_next_section:
  3114.         cmp     esi,[free_additional_memory]
  3115.         je      make_section_symbol
  3116.         mov     al,[esi]
  3117.         or      al,al
  3118.         jz      make_section_symbol
  3119.         cmp     al,0C0h
  3120.         jae     skip_public
  3121.         cmp     al,80h
  3122.         jae     skip_extrn
  3123.         or      byte [ebx+14h],40h
  3124.       skip_extrn:
  3125.         add     esi,0Ch
  3126.         jmp     find_next_section
  3127.       skip_public:
  3128.         add     esi,10h
  3129.         jmp     find_next_section
  3130.       make_section_symbol:
  3131.         mov     eax,edi
  3132.         xchg    eax,[ebx+4]
  3133.         stos    dword [edi]
  3134.         test    [format_flags],8
  3135.         jnz     elf64_section_symbol
  3136.         xor     eax,eax
  3137.         stos    dword [edi]
  3138.         stos    dword [edi]
  3139.         call    store_section_index
  3140.         jmp     section_symbol_ok
  3141.       store_section_index:
  3142.         inc     ecx
  3143.         mov     eax,ecx
  3144.         shl     eax,8
  3145.         mov     [ebx],eax
  3146.         inc     dx
  3147.         jz      format_limitations_exceeded
  3148.         mov     eax,edx
  3149.         shl     eax,16
  3150.         mov     al,3
  3151.         test    byte [ebx+14h],40h
  3152.         jz      section_index_ok
  3153.         or      ah,-1
  3154.         inc     dx
  3155.         jz      format_limitations_exceeded
  3156.       section_index_ok:
  3157.         stos    dword [edi]
  3158.         ret
  3159.       elf64_section_symbol:
  3160.         call    store_section_index
  3161.         xor     eax,eax
  3162.         stos    dword [edi]
  3163.         stos    dword [edi]
  3164.         stos    dword [edi]
  3165.         stos    dword [edi]
  3166.       section_symbol_ok:
  3167.         mov     ebx,esi
  3168.         add     esi,20h
  3169.         cmp     ebx,[free_additional_memory]
  3170.         jne     find_next_section
  3171.         inc     dx
  3172.         jz      format_limitations_exceeded
  3173.         mov     [current_section],edx
  3174.         mov     esi,[symbols_stream]
  3175.       find_other_symbols:
  3176.         cmp     esi,[free_additional_memory]
  3177.         je      elf_symbol_table_ok
  3178.         mov     al,[esi]
  3179.         or      al,al
  3180.         jz      skip_section
  3181.         cmp     al,0C0h
  3182.         jae     make_public_symbol
  3183.         cmp     al,80h
  3184.         jae     make_extrn_symbol
  3185.         add     esi,0Ch
  3186.         jmp     find_other_symbols
  3187.       skip_section:
  3188.         add     esi,20h
  3189.         jmp     find_other_symbols
  3190.       make_public_symbol:
  3191.         mov     eax,[esi+0Ch]
  3192.         mov     [current_line],eax
  3193.         cmp     byte [esi],0C0h
  3194.         jne     invalid_argument
  3195.         mov     ebx,[esi+8]
  3196.         test    byte [ebx+8],1
  3197.         jz      undefined_public
  3198.         mov     ax,[current_pass]
  3199.         cmp     ax,[ebx+16]
  3200.         jne     undefined_public
  3201.         mov     dl,[ebx+11]
  3202.         or      dl,dl
  3203.         jz      public_absolute
  3204.         mov     eax,[ebx+20]
  3205.         cmp     byte [eax],0
  3206.         jne     invalid_use_of_symbol
  3207.         mov     eax,[eax+4]
  3208.         test    [format_flags],8
  3209.         jnz     elf64_public
  3210.         cmp     dl,2
  3211.         jne     invalid_use_of_symbol
  3212.         mov     dx,[eax+0Eh]
  3213.         jmp     section_for_public_ok
  3214.       undefined_public:
  3215.         mov     [error_info],ebx
  3216.         jmp     undefined_symbol
  3217.       elf64_public:
  3218.         cmp     dl,4
  3219.         jne     invalid_use_of_symbol
  3220.         mov     dx,[eax+6]
  3221.         jmp     section_for_public_ok
  3222.       public_absolute:
  3223.         mov     dx,0FFF1h
  3224.       section_for_public_ok:
  3225.         mov     eax,[esi+4]
  3226.         stos    dword [edi]
  3227.         test    [format_flags],8
  3228.         jnz     elf64_public_symbol
  3229.         movzx   eax,byte [ebx+9]
  3230.         shr     al,1
  3231.         and     al,1
  3232.         neg     eax
  3233.         cmp     eax,[ebx+4]
  3234.         jne     value_out_of_range
  3235.         xor     eax,[ebx]
  3236.         js      value_out_of_range
  3237.         mov     eax,[ebx]
  3238.         stos    dword [edi]
  3239.         xor     eax,eax
  3240.         mov     al,[ebx+10]
  3241.         stos    dword [edi]
  3242.         mov     eax,edx
  3243.         shl     eax,16
  3244.         mov     al,10h
  3245.         cmp     byte [ebx+10],0
  3246.         je      elf_public_function
  3247.         or      al,1
  3248.         jmp     store_elf_public_info
  3249.       elf_public_function:
  3250.         or      al,2
  3251.       store_elf_public_info:
  3252.         stos    dword [edi]
  3253.         jmp     public_symbol_ok
  3254.       elf64_public_symbol:
  3255.         mov     eax,edx
  3256.         shl     eax,16
  3257.         mov     al,10h
  3258.         cmp     byte [ebx+10],0
  3259.         je      elf64_public_function
  3260.         or      al,1
  3261.         jmp     store_elf64_public_info
  3262.       elf64_public_function:
  3263.         or      al,2
  3264.       store_elf64_public_info:
  3265.         stos    dword [edi]
  3266.         mov     al,[ebx+9]
  3267.         shl     eax,31-1
  3268.         xor     eax,[ebx+4]
  3269.         js      value_out_of_range
  3270.         mov     eax,[ebx]
  3271.         stos    dword [edi]
  3272.         mov     eax,[ebx+4]
  3273.         stos    dword [edi]
  3274.         mov     al,[ebx+10]
  3275.         stos    dword [edi]
  3276.         xor     al,al
  3277.         stos    dword [edi]
  3278.       public_symbol_ok:
  3279.         inc     ecx
  3280.         mov     eax,ecx
  3281.         shl     eax,8
  3282.         mov     al,0C0h
  3283.         mov     [esi],eax
  3284.         add     esi,10h
  3285.         jmp     find_other_symbols
  3286.       make_extrn_symbol:
  3287.         mov     eax,[esi+4]
  3288.         stos    dword [edi]
  3289.         test    [format_flags],8
  3290.         jnz     elf64_extrn_symbol
  3291.         xor     eax,eax
  3292.         stos    dword [edi]
  3293.         mov     eax,[esi+8]
  3294.         stos    dword [edi]
  3295.         mov     eax,10h
  3296.         stos    dword [edi]
  3297.         jmp     extrn_symbol_ok
  3298.       elf64_extrn_symbol:
  3299.         mov     eax,10h
  3300.         stos    dword [edi]
  3301.         xor     al,al
  3302.         stos    dword [edi]
  3303.         stos    dword [edi]
  3304.         mov     eax,[esi+8]
  3305.         stos    dword [edi]
  3306.         xor     eax,eax
  3307.         stos    dword [edi]
  3308.       extrn_symbol_ok:
  3309.         inc     ecx
  3310.         mov     eax,ecx
  3311.         shl     eax,8
  3312.         mov     al,80h
  3313.         mov     [esi],eax
  3314.         add     esi,0Ch
  3315.         jmp     find_other_symbols
  3316.       elf_symbol_table_ok:
  3317.         mov     edx,edi
  3318.         mov     ebx,[free_additional_memory]
  3319.         xor     al,al
  3320.         stos    byte [edi]
  3321.         add     edi,16
  3322.         mov     [edx+1],edx
  3323.         add     ebx,10h
  3324.         test    [format_flags],8
  3325.         jz      make_string_table
  3326.         add     ebx,8
  3327.       make_string_table:
  3328.         cmp     ebx,edx
  3329.         je      elf_string_table_ok
  3330.         test    [format_flags],8
  3331.         jnz     make_elf64_string
  3332.         cmp     byte [ebx+0Dh],0
  3333.         je      rel_prefix_ok
  3334.         mov     byte [ebx+0Dh],0
  3335.         mov     eax,'.rel'
  3336.         stos    dword [edi]
  3337.       rel_prefix_ok:
  3338.         mov     esi,edi
  3339.         sub     esi,edx
  3340.         xchg    esi,[ebx]
  3341.         add     ebx,10h
  3342.       make_elf_string:
  3343.         or      esi,esi
  3344.         jz      default_string
  3345.         lods    dword [esi]
  3346.         mov     ecx,eax
  3347.         rep     movs byte [edi],[esi]
  3348.         xor     al,al
  3349.         stos    byte [edi]
  3350.         jmp     make_string_table
  3351.       make_elf64_string:
  3352.         cmp     byte [ebx+5],0
  3353.         je      elf64_rel_prefix_ok
  3354.         mov     byte [ebx+5],0
  3355.         mov     eax,'.rel'
  3356.         stos    dword [edi]
  3357.         mov     al,'a'
  3358.         stos    byte [edi]
  3359.       elf64_rel_prefix_ok:
  3360.         mov     esi,edi
  3361.         sub     esi,edx
  3362.         xchg    esi,[ebx]
  3363.         add     ebx,18h
  3364.         jmp     make_elf_string
  3365.       default_string:
  3366.         mov     eax,'.fla'
  3367.         stos    dword [edi]
  3368.         mov     ax,'t'
  3369.         stos    word [edi]
  3370.         jmp     make_string_table
  3371.       elf_string_table_ok:
  3372.         mov     [edx+1+8],edi
  3373.         mov     ebx,[code_start]
  3374.         mov     eax,edi
  3375.         sub     eax,[free_additional_memory]
  3376.         test    [format_flags],8
  3377.         jnz     finish_elf64_header
  3378.         mov     [ebx+20h],eax
  3379.         mov     eax,[current_section]
  3380.         inc     ax
  3381.         jz      format_limitations_exceeded
  3382.         mov     [ebx+32h],ax
  3383.         inc     ax
  3384.         jz      format_limitations_exceeded
  3385.         mov     [ebx+30h],ax
  3386.         jmp     elf_header_finished
  3387.       finish_elf64_header:
  3388.         mov     [ebx+28h],eax
  3389.         mov     eax,[current_section]
  3390.         inc     ax
  3391.         jz      format_limitations_exceeded
  3392.         mov     [ebx+3Eh],ax
  3393.         inc     ax
  3394.         jz      format_limitations_exceeded
  3395.         mov     [ebx+3Ch],ax
  3396.       elf_header_finished:
  3397.         xor     eax,eax
  3398.         mov     ecx,10
  3399.         rep     stos dword [edi]
  3400.         test    [format_flags],8
  3401.         jz      elf_null_section_ok
  3402.         mov     ecx,6
  3403.         rep     stos dword [edi]
  3404.       elf_null_section_ok:
  3405.         mov     esi,ebp
  3406.         xor     ecx,ecx
  3407.       make_section_entry:
  3408.         mov     ebx,edi
  3409.         mov     eax,[esi+4]
  3410.         mov     eax,[eax]
  3411.         stos    dword [edi]
  3412.         mov     eax,1
  3413.         cmp     dword [esi+0Ch],0
  3414.         je      bss_section
  3415.         test    byte [esi+14h],80h
  3416.         jz      section_type_ok
  3417.       bss_section:
  3418.         mov     al,8
  3419.       section_type_ok:
  3420.         stos    dword [edi]
  3421.         mov     eax,[esi+14h]
  3422.         and     al,3Fh
  3423.         call    store_elf_machine_word
  3424.         xor     eax,eax
  3425.         call    store_elf_machine_word
  3426.         mov     eax,[esi+8]
  3427.         mov     [image_base],eax
  3428.         sub     eax,[code_start]
  3429.         call    store_elf_machine_word
  3430.         mov     eax,[esi+0Ch]
  3431.         call    store_elf_machine_word
  3432.         xor     eax,eax
  3433.         stos    dword [edi]
  3434.         stos    dword [edi]
  3435.         mov     eax,[esi+10h]
  3436.         call    store_elf_machine_word
  3437.         xor     eax,eax
  3438.         call    store_elf_machine_word
  3439.         inc     ecx
  3440.         add     esi,20h
  3441.         xchg    edi,[esp]
  3442.         mov     ebp,edi
  3443.       convert_relocations:
  3444.         cmp     esi,[free_additional_memory]
  3445.         je      relocations_converted
  3446.         mov     al,[esi]
  3447.         or      al,al
  3448.         jz      relocations_converted
  3449.         cmp     al,80h
  3450.         jb      make_relocation_entry
  3451.         cmp     al,0C0h
  3452.         jb      relocation_entry_ok
  3453.         add     esi,10h
  3454.         jmp     convert_relocations
  3455.       make_relocation_entry:
  3456.         test    [format_flags],8
  3457.         jnz     make_elf64_relocation_entry
  3458.         mov     eax,[esi+4]
  3459.         stos    dword [edi]
  3460.         mov     eax,[esi+8]
  3461.         mov     eax,[eax]
  3462.         mov     al,[esi]
  3463.         stos    dword [edi]
  3464.         jmp     relocation_entry_ok
  3465.       make_elf64_relocation_entry:
  3466.         mov     eax,[esi+4]
  3467.         stos    dword [edi]
  3468.         xor     eax,eax
  3469.         stos    dword [edi]
  3470.         movzx   eax,byte [esi]
  3471.         stos    dword [edi]
  3472.         mov     eax,[esi+8]
  3473.         mov     eax,[eax]
  3474.         shr     eax,8
  3475.         stos    dword [edi]
  3476.         xor     eax,eax
  3477.         stos    dword [edi]
  3478.         stos    dword [edi]
  3479.       relocation_entry_ok:
  3480.         add     esi,0Ch
  3481.         jmp     convert_relocations
  3482.       store_elf_machine_word:
  3483.         stos    dword [edi]
  3484.         test    [format_flags],8
  3485.         jz      elf_machine_word_ok
  3486.         and     dword [edi],0
  3487.         add     edi,4
  3488.       elf_machine_word_ok:
  3489.         ret
  3490.       relocations_converted:
  3491.         cmp     edi,ebp
  3492.         xchg    edi,[esp]
  3493.         je      rel_section_ok
  3494.         mov     eax,[ebx]
  3495.         sub     eax,4
  3496.         test    [format_flags],8
  3497.         jz      store_relocations_name_offset
  3498.         dec     eax
  3499.       store_relocations_name_offset:
  3500.         stos    dword [edi]
  3501.         test    [format_flags],8
  3502.         jnz     rela_section
  3503.         mov     eax,9
  3504.         jmp     store_relocations_type
  3505.       rela_section:
  3506.         mov     eax,4
  3507.       store_relocations_type:
  3508.         stos    dword [edi]
  3509.         xor     al,al
  3510.         call    store_elf_machine_word
  3511.         call    store_elf_machine_word
  3512.         mov     eax,ebp
  3513.         sub     eax,[code_start]
  3514.         call    store_elf_machine_word
  3515.         mov     eax,[esp]
  3516.         sub     eax,ebp
  3517.         call    store_elf_machine_word
  3518.         mov     eax,[current_section]
  3519.         stos    dword [edi]
  3520.         mov     eax,ecx
  3521.         stos    dword [edi]
  3522.         inc     ecx
  3523.         test    [format_flags],8
  3524.         jnz     finish_elf64_rela_section
  3525.         mov     eax,4
  3526.         stos    dword [edi]
  3527.         mov     al,8
  3528.         stos    dword [edi]
  3529.         jmp     rel_section_ok
  3530.       finish_elf64_rela_section:
  3531.         mov     eax,8
  3532.         stos    dword [edi]
  3533.         xor     al,al
  3534.         stos    dword [edi]
  3535.         mov     al,24
  3536.         stos    dword [edi]
  3537.         xor     al,al
  3538.         stos    dword [edi]
  3539.       rel_section_ok:
  3540.         cmp     esi,[free_additional_memory]
  3541.         jne     make_section_entry
  3542.         pop     eax
  3543.         mov     ebx,[code_start]
  3544.         sub     eax,ebx
  3545.         mov     [code_size],eax
  3546.         mov     ecx,20h
  3547.         test    [format_flags],8
  3548.         jz      adjust_elf_section_headers_offset
  3549.         mov     ecx,28h
  3550.       adjust_elf_section_headers_offset:
  3551.         add     [ebx+ecx],eax
  3552.         mov     eax,1
  3553.         stos    dword [edi]
  3554.         mov     al,2
  3555.         stos    dword [edi]
  3556.         xor     al,al
  3557.         call    store_elf_machine_word
  3558.         call    store_elf_machine_word
  3559.         mov     eax,[code_size]
  3560.         call    store_elf_machine_word
  3561.         mov     eax,[edx+1]
  3562.         sub     eax,[free_additional_memory]
  3563.         call    store_elf_machine_word
  3564.         mov     eax,[current_section]
  3565.         inc     eax
  3566.         stos    dword [edi]
  3567.         mov     eax,[number_of_sections]
  3568.         inc     eax
  3569.         stos    dword [edi]
  3570.         test    [format_flags],8
  3571.         jnz     finish_elf64_sym_section
  3572.         mov     eax,4
  3573.         stos    dword [edi]
  3574.         mov     al,10h
  3575.         stos    dword [edi]
  3576.         jmp     sym_section_ok
  3577.       finish_elf64_sym_section:
  3578.         mov     eax,8
  3579.         stos    dword [edi]
  3580.         xor     al,al
  3581.         stos    dword [edi]
  3582.         mov     al,18h
  3583.         stos    dword [edi]
  3584.         xor     al,al
  3585.         stos    dword [edi]
  3586.       sym_section_ok:
  3587.         mov     al,1+8
  3588.         stos    dword [edi]
  3589.         mov     al,3
  3590.         stos    dword [edi]
  3591.         xor     al,al
  3592.         call    store_elf_machine_word
  3593.         call    store_elf_machine_word
  3594.         mov     eax,[edx+1]
  3595.         sub     eax,[free_additional_memory]
  3596.         add     eax,[code_size]
  3597.         call    store_elf_machine_word
  3598.         mov     eax,[edx+1+8]
  3599.         sub     eax,[edx+1]
  3600.         call    store_elf_machine_word
  3601.         xor     eax,eax
  3602.         stos    dword [edi]
  3603.         stos    dword [edi]
  3604.         mov     al,1
  3605.         call    store_elf_machine_word
  3606.         xor     eax,eax
  3607.         call    store_elf_machine_word
  3608.         mov     eax,'tab'
  3609.         mov     dword [edx+1],'.sym'
  3610.         mov     [edx+1+4],eax
  3611.         mov     dword [edx+1+8],'.str'
  3612.         mov     [edx+1+8+4],eax
  3613.         mov     [resource_data],edx
  3614.         mov     [written_size],0
  3615.         mov     edx,[output_file]
  3616.         call    create
  3617.         jc      write_failed
  3618.         call    write_code
  3619.         mov     ecx,edi
  3620.         mov     edx,[free_additional_memory]
  3621.         sub     ecx,edx
  3622.         add     [written_size],ecx
  3623.         call    write
  3624.         jc      write_failed
  3625.         jmp     output_written
  3626.  
  3627. format_elf_exe:
  3628.         add     esi,2
  3629.         or      [format_flags],1
  3630.         cmp     byte [esi],'('
  3631.         jne     elf_exe_brand_ok
  3632.         inc     esi
  3633.         cmp     byte [esi],'.'
  3634.         je      invalid_value
  3635.         push    edx
  3636.         call    get_byte_value
  3637.         cmp     [value_type],0
  3638.         jne     invalid_use_of_symbol
  3639.         pop     edx
  3640.         mov     [edx+7],al
  3641.       elf_exe_brand_ok:
  3642.         mov     [image_base],8048000h
  3643.         cmp     byte [esi],80h
  3644.         jne     elf_exe_base_ok
  3645.         lods    word [esi]
  3646.         cmp     ah,'('
  3647.         jne     invalid_argument
  3648.         cmp     byte [esi],'.'
  3649.         je      invalid_value
  3650.         push    edx
  3651.         call    get_dword_value
  3652.         cmp     [value_type],0
  3653.         jne     invalid_use_of_symbol
  3654.         mov     [image_base],eax
  3655.         pop     edx
  3656.       elf_exe_base_ok:
  3657.         mov     byte [edx+10h],2
  3658.         mov     byte [edx+2Ah],20h
  3659.         mov     ebx,edi
  3660.         mov     ecx,20h shr 2
  3661.         cmp     [current_pass],0
  3662.         je      init_elf_segments
  3663.         imul    ecx,[number_of_sections]
  3664.       init_elf_segments:
  3665.         xor     eax,eax
  3666.         rep     stos dword [edi]
  3667.         and     [number_of_sections],0
  3668.         mov     byte [ebx],1
  3669.         mov     word [ebx+1Ch],1000h
  3670.         mov     byte [ebx+18h],111b
  3671.         mov     eax,edi
  3672.         xor     ebp,ebp
  3673.         xor     cl,cl
  3674.         sub     eax,[code_start]
  3675.         sbb     ebp,0
  3676.         sbb     cl,0
  3677.         mov     [ebx+4],eax
  3678.         add     eax,[image_base]
  3679.         adc     ebp,0
  3680.         adc     cl,0
  3681.         mov     [ebx+8],eax
  3682.         mov     [ebx+0Ch],eax
  3683.         mov     [edx+18h],eax
  3684.         not     eax
  3685.         not     ebp
  3686.         not     cl
  3687.         add     eax,1
  3688.         adc     ebp,0
  3689.         adc     cl,0
  3690.         add     eax,edi
  3691.         adc     ebp,0
  3692.         adc     cl,0
  3693.         mov     dword [org_origin],eax
  3694.         mov     dword [org_origin+4],edx
  3695.         mov     [org_origin_sign],cl
  3696.         and     [org_registers],0
  3697.         mov     [org_start],edi
  3698.         mov     [symbols_stream],edi
  3699.         jmp     format_defined
  3700.       format_elf64_exe:
  3701.         add     esi,2
  3702.         or      [format_flags],1
  3703.         cmp     byte [esi],'('
  3704.         jne     elf64_exe_brand_ok
  3705.         inc     esi
  3706.         cmp     byte [esi],'.'
  3707.         je      invalid_value
  3708.         push    edx
  3709.         call    get_byte_value
  3710.         cmp     [value_type],0
  3711.         jne     invalid_use_of_symbol
  3712.         pop     edx
  3713.         mov     [edx+7],al
  3714.       elf64_exe_brand_ok:
  3715.         mov     [image_base],400000h
  3716.         and     [image_base_high],0
  3717.         cmp     byte [esi],80h
  3718.         jne     elf64_exe_base_ok
  3719.         lods    word [esi]
  3720.         cmp     ah,'('
  3721.         jne     invalid_argument
  3722.         cmp     byte [esi],'.'
  3723.         je      invalid_value
  3724.         push    edx
  3725.         call    get_qword_value
  3726.         cmp     [value_type],0
  3727.         jne     invalid_use_of_symbol
  3728.         mov     [image_base],eax
  3729.         mov     [image_base_high],edx
  3730.         pop     edx
  3731.       elf64_exe_base_ok:
  3732.         mov     byte [edx+10h],2
  3733.         mov     byte [edx+36h],38h
  3734.         mov     ebx,edi
  3735.         mov     ecx,38h shr 2
  3736.         cmp     [current_pass],0
  3737.         je      init_elf64_segments
  3738.         imul    ecx,[number_of_sections]
  3739.       init_elf64_segments:
  3740.         xor     eax,eax
  3741.         rep     stos dword [edi]
  3742.         and     [number_of_sections],0
  3743.         mov     byte [ebx],1
  3744.         mov     word [ebx+30h],1000h
  3745.         mov     byte [ebx+4],111b
  3746.         push    edx
  3747.         mov     eax,edi
  3748.         sub     eax,[code_start]
  3749.         mov     [ebx+8],eax
  3750.         xor     edx,edx
  3751.         xor     cl,cl
  3752.         add     eax,[image_base]
  3753.         adc     edx,[image_base_high]
  3754.         adc     cl,0
  3755.         mov     [ebx+10h],eax
  3756.         mov     [ebx+10h+4],edx
  3757.         mov     [ebx+18h],eax
  3758.         mov     [ebx+18h+4],edx
  3759.         pop     ebx
  3760.         mov     [ebx+18h],eax
  3761.         mov     [ebx+18h+4],edx
  3762.         not     eax
  3763.         not     edx
  3764.         not     cl
  3765.         add     eax,1
  3766.         adc     edx,0
  3767.         adc     cl,0
  3768.         add     eax,edi
  3769.         adc     edx,0
  3770.         adc     cl,0
  3771.         mov     dword [org_origin],eax
  3772.         mov     dword [org_origin+4],edx
  3773.         mov     [org_origin_sign],cl
  3774.         and     [org_registers],0
  3775.         mov     [org_start],edi
  3776.         mov     [symbols_stream],edi
  3777.         jmp     format_defined
  3778. elf_entry:
  3779.         lods    byte [esi]
  3780.         cmp     al,'('
  3781.         jne     invalid_argument
  3782.         cmp     byte [esi],'.'
  3783.         je      invalid_value
  3784.         test    [format_flags],8
  3785.         jnz     elf64_entry
  3786.         call    get_dword_value
  3787.         cmp     [value_type],0
  3788.         jne     invalid_use_of_symbol
  3789.         mov     edx,[code_start]
  3790.         mov     [edx+18h],eax
  3791.         jmp     instruction_assembled
  3792.       elf64_entry:
  3793.         call    get_qword_value
  3794.         cmp     [value_type],0
  3795.         jne     invalid_use_of_symbol
  3796.         mov     ebx,[code_start]
  3797.         mov     [ebx+18h],eax
  3798.         mov     [ebx+1Ch],edx
  3799.         jmp     instruction_assembled
  3800. elf_segment:
  3801.         bt      [format_flags],0
  3802.         jnc     illegal_instruction
  3803.         test    [format_flags],8
  3804.         jnz     elf64_segment
  3805.         call    close_elf_segment
  3806.         push    eax
  3807.         mov     ebx,[number_of_sections]
  3808.         shl     ebx,5
  3809.         add     ebx,[code_start]
  3810.         add     ebx,34h
  3811.         cmp     ebx,[symbols_stream]
  3812.         jb      new_elf_segment
  3813.         mov     ebx,[symbols_stream]
  3814.         sub     ebx,20h
  3815.         push    edi
  3816.         mov     edi,ebx
  3817.         mov     ecx,20h shr 2
  3818.         xor     eax,eax
  3819.         rep     stos dword [edi]
  3820.         pop     edi
  3821.         or      [next_pass_needed],-1
  3822.       new_elf_segment:
  3823.         mov     byte [ebx],1
  3824.         mov     word [ebx+1Ch],1000h
  3825.       elf_segment_flags:
  3826.         cmp     byte [esi],1Eh
  3827.         je      elf_segment_type
  3828.         cmp     byte [esi],19h
  3829.         jne     elf_segment_flags_ok
  3830.         lods    word [esi]
  3831.         sub     ah,28
  3832.         jbe     invalid_argument
  3833.         cmp     ah,1
  3834.         je      mark_elf_segment_flag
  3835.         cmp     ah,3
  3836.         ja      invalid_argument
  3837.         xor     ah,1
  3838.         cmp     ah,2
  3839.         je      mark_elf_segment_flag
  3840.         inc     ah
  3841.       mark_elf_segment_flag:
  3842.         test    [ebx+18h],ah
  3843.         jnz     setting_already_specified
  3844.         or      [ebx+18h],ah
  3845.         jmp     elf_segment_flags
  3846.       elf_segment_type:
  3847.         cmp     byte [ebx],1
  3848.         jne     setting_already_specified
  3849.         lods    word [esi]
  3850.         mov     ecx,[number_of_sections]
  3851.         jecxz   elf_segment_type_ok
  3852.         mov     edx,[code_start]
  3853.         add     edx,34h
  3854.       scan_elf_segment_types:
  3855.         cmp     edx,[symbols_stream]
  3856.         jae     elf_segment_type_ok
  3857.         cmp     [edx],ah
  3858.         je      data_already_defined
  3859.         add     edx,20h
  3860.         loop    scan_elf_segment_types
  3861.       elf_segment_type_ok:
  3862.         mov     [ebx],ah
  3863.         mov     word [ebx+1Ch],1
  3864.         jmp     elf_segment_flags
  3865.       elf_segment_flags_ok:
  3866.         mov     eax,edi
  3867.         sub     eax,[code_start]
  3868.         mov     [ebx+4],eax
  3869.         pop     edx
  3870.         and     eax,0FFFh
  3871.         add     edx,eax
  3872.         mov     [ebx+8],edx
  3873.         mov     [ebx+0Ch],edx
  3874.         mov     eax,edx
  3875.         xor     edx,edx
  3876.         xor     cl,cl
  3877.         not     eax
  3878.         not     edx
  3879.         not     cl
  3880.         add     eax,1
  3881.         adc     edx,0
  3882.         adc     cl,0
  3883.         add     eax,edi
  3884.         adc     edx,0
  3885.         adc     cl,0
  3886.         mov     dword [org_origin],eax
  3887.         mov     dword [org_origin+4],edx
  3888.         mov     [org_origin_sign],cl
  3889.         and     [org_registers],0
  3890.         mov     [org_start],edi
  3891.         inc     [number_of_sections]
  3892.         jmp     instruction_assembled
  3893.       close_elf_segment:
  3894.         cmp     [number_of_sections],0
  3895.         jne     finish_elf_segment
  3896.         cmp     edi,[symbols_stream]
  3897.         jne     first_elf_segment_ok
  3898.         push    edi
  3899.         mov     edi,[code_start]
  3900.         add     edi,34h
  3901.         mov     ecx,20h shr 2
  3902.         xor     eax,eax
  3903.         rep     stos dword [edi]
  3904.         pop     edi
  3905.         mov     eax,[image_base]
  3906.         ret
  3907.       first_elf_segment_ok:
  3908.         inc     [number_of_sections]
  3909.       finish_elf_segment:
  3910.         mov     ebx,[number_of_sections]
  3911.         dec     ebx
  3912.         shl     ebx,5
  3913.         add     ebx,[code_start]
  3914.         add     ebx,34h
  3915.         mov     eax,edi
  3916.         sub     eax,[code_start]
  3917.         sub     eax,[ebx+4]
  3918.         mov     edx,edi
  3919.         cmp     edi,[undefined_data_end]
  3920.         jne     elf_segment_size_ok
  3921.         mov     edi,[undefined_data_start]
  3922.       elf_segment_size_ok:
  3923.         mov     [ebx+14h],eax
  3924.         add     eax,edi
  3925.         sub     eax,edx
  3926.         mov     [ebx+10h],eax
  3927.         mov     eax,[ebx+8]
  3928.         cmp     byte [ebx],1
  3929.         jne     elf_segment_position_ok
  3930.         add     eax,[ebx+14h]
  3931.         add     eax,0FFFh
  3932.       elf_segment_position_ok:
  3933.         and     eax,not 0FFFh
  3934.         ret
  3935.       elf64_segment:
  3936.         call    close_elf64_segment
  3937.         push    eax edx
  3938.         mov     ebx,[number_of_sections]
  3939.         imul    ebx,38h
  3940.         add     ebx,[code_start]
  3941.         add     ebx,40h
  3942.         cmp     ebx,[symbols_stream]
  3943.         jb      new_elf64_segment
  3944.         mov     ebx,[symbols_stream]
  3945.         sub     ebx,38h
  3946.         push    edi
  3947.         mov     edi,ebx
  3948.         mov     ecx,38h shr 2
  3949.         xor     eax,eax
  3950.         rep     stos dword [edi]
  3951.         pop     edi
  3952.         or      [next_pass_needed],-1
  3953.       new_elf64_segment:
  3954.         mov     byte [ebx],1
  3955.         mov     word [ebx+30h],1000h
  3956.       elf64_segment_flags:
  3957.         cmp     byte [esi],1Eh
  3958.         je      elf64_segment_type
  3959.         cmp     byte [esi],19h
  3960.         jne     elf64_segment_flags_ok
  3961.         lods    word [esi]
  3962.         sub     ah,28
  3963.         jbe     invalid_argument
  3964.         cmp     ah,1
  3965.         je      mark_elf64_segment_flag
  3966.         cmp     ah,3
  3967.         ja      invalid_argument
  3968.         xor     ah,1
  3969.         cmp     ah,2
  3970.         je      mark_elf64_segment_flag
  3971.         inc     ah
  3972.       mark_elf64_segment_flag:
  3973.         test    [ebx+4],ah
  3974.         jnz     setting_already_specified
  3975.         or      [ebx+4],ah
  3976.         jmp     elf64_segment_flags
  3977.       elf64_segment_type:
  3978.         cmp     byte [ebx],1
  3979.         jne     setting_already_specified
  3980.         lods    word [esi]
  3981.         mov     ecx,[number_of_sections]
  3982.         jecxz   elf64_segment_type_ok
  3983.         mov     edx,[code_start]
  3984.         add     edx,40h
  3985.       scan_elf64_segment_types:
  3986.         cmp     edx,[symbols_stream]
  3987.         jae     elf64_segment_type_ok
  3988.         cmp     [edx],ah
  3989.         je      data_already_defined
  3990.         add     edx,38h
  3991.         loop    scan_elf64_segment_types
  3992.       elf64_segment_type_ok:
  3993.         mov     [ebx],ah
  3994.         mov     word [ebx+30h],1
  3995.         jmp     elf64_segment_flags
  3996.       elf64_segment_flags_ok:
  3997.         mov     ecx,edi
  3998.         sub     ecx,[code_start]
  3999.         mov     [ebx+8],ecx
  4000.         pop     edx eax
  4001.         and     ecx,0FFFh
  4002.         add     eax,ecx
  4003.         adc     edx,0
  4004.         mov     [ebx+10h],eax
  4005.         mov     [ebx+10h+4],edx
  4006.         mov     [ebx+18h],eax
  4007.         mov     [ebx+18h+4],edx
  4008.         xor     cl,cl
  4009.         not     eax
  4010.         not     edx
  4011.         not     cl
  4012.         add     eax,1
  4013.         adc     edx,0
  4014.         adc     cl,0
  4015.         add     eax,edi
  4016.         adc     edx,0
  4017.         adc     cl,0
  4018.         mov     dword [org_origin],eax
  4019.         mov     dword [org_origin+4],edx
  4020.         mov     [org_origin_sign],cl
  4021.         and     [org_registers],0
  4022.         mov     [org_start],edi
  4023.         inc     [number_of_sections]
  4024.         jmp     instruction_assembled
  4025.       close_elf64_segment:
  4026.         cmp     [number_of_sections],0
  4027.         jne     finish_elf64_segment
  4028.         cmp     edi,[symbols_stream]
  4029.         jne     first_elf64_segment_ok
  4030.         push    edi
  4031.         mov     edi,[code_start]
  4032.         add     edi,40h
  4033.         mov     ecx,38h shr 2
  4034.         xor     eax,eax
  4035.         rep     stos dword [edi]
  4036.         pop     edi
  4037.         mov     eax,[image_base]
  4038.         mov     edx,[image_base_high]
  4039.         ret
  4040.       first_elf64_segment_ok:
  4041.         inc     [number_of_sections]
  4042.       finish_elf64_segment:
  4043.         mov     ebx,[number_of_sections]
  4044.         dec     ebx
  4045.         imul    ebx,38h
  4046.         add     ebx,[code_start]
  4047.         add     ebx,40h
  4048.         mov     eax,edi
  4049.         sub     eax,[code_start]
  4050.         sub     eax,[ebx+8]
  4051.         mov     edx,edi
  4052.         cmp     edi,[undefined_data_end]
  4053.         jne     elf64_segment_size_ok
  4054.         mov     edi,[undefined_data_start]
  4055.       elf64_segment_size_ok:
  4056.         mov     [ebx+28h],eax
  4057.         add     eax,edi
  4058.         sub     eax,edx
  4059.         mov     [ebx+20h],eax
  4060.         mov     eax,[ebx+10h]
  4061.         mov     edx,[ebx+10h+4]
  4062.         cmp     byte [ebx],1
  4063.         jne     elf64_segment_position_ok
  4064.         add     eax,[ebx+28h]
  4065.         adc     edx,0
  4066.         add     eax,0FFFh
  4067.         adc     edx,0
  4068.       elf64_segment_position_ok:
  4069.         and     eax,not 0FFFh
  4070.         ret
  4071. close_elf_exe:
  4072.         test    [format_flags],8
  4073.         jnz     close_elf64_exe
  4074.         call    close_elf_segment
  4075.         mov     edx,[code_start]
  4076.         mov     eax,[number_of_sections]
  4077.         mov     byte [edx+1Ch],34h
  4078.         mov     [edx+2Ch],ax
  4079.         shl     eax,5
  4080.         add     eax,edx
  4081.         add     eax,34h
  4082.         cmp     eax,[symbols_stream]
  4083.         je      elf_exe_ok
  4084.         or      [next_pass_needed],-1
  4085.       elf_exe_ok:
  4086.         ret
  4087.       close_elf64_exe:
  4088.         call    close_elf64_segment
  4089.         mov     edx,[code_start]
  4090.         mov     eax,[number_of_sections]
  4091.         mov     byte [edx+20h],40h
  4092.         mov     [edx+38h],ax
  4093.         imul    eax,38h
  4094.         add     eax,edx
  4095.         add     eax,40h
  4096.         cmp     eax,[symbols_stream]
  4097.         je      elf64_exe_ok
  4098.         or      [next_pass_needed],-1
  4099.       elf64_exe_ok:
  4100.         ret
  4101.