Subversion Repositories Kolibri OS

Rev

Rev 196 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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