Subversion Repositories Kolibri OS

Rev

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