Subversion Repositories Kolibri OS

Rev

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

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