
    eCijB                        d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ ej                             ej        d	                     ddlZ ej        d
          ZdZdZdZdZdZdZdZdZdZdZdZdZdZd Z d Z!d Z"d Z# G d de          Z$ G d de          Z% G d de          Z& G d de          Z' G d  d!e          Z( G d" d#e          Z) G d$ d%e          Z* G d& d'e          Z+ G d( d)e          Z, G d* d+e-          Z. ed,g d-          Z/ ed.g d/          Z0 ed0g d1          Z1 ed2g d3          Z2 ed4g d5          Z3 ed6g d7          Z4 ed8d9d:g          Z5 ed;d<d=g          Z6 ed>g d?          Z7 ed@dAdBg          Z8 G dC dD          Z9dE Z:dF Z;dG Z<dH Z=dS )Iz=Utilities for manipulating WebAssembly binaries from python.
    N)
namedtuple)IntEnumwraps   )utils)memoizethird_partywebassemblyi   s    asms                        c                 @    t           j                            |           S N)leb128uencode)nums    5/b/s/w/ir/x/w/install/emscripten/tools/webassembly.pyto_lebr   /   s    			    c                 L    t           j                            |           d         S Nr   )r   r   decode_readeriobufs    r   	read_ulebr    3       				&	&q	))r   c                 L    t           j                            |           d         S r   )r   ir   r   s    r   	read_slebr$   7   r!   r   c                 <     t                      fd            }|S )Nc                 J    }|| j         vr | g|R i || j         |<   d S d S r   )_cache)selfargskwargskeymethods       r   helperzonce.<locals>.helper=   sI    
C
$+6t666v66dk# r   r   )r,   r-   s   ` r   oncer.   ;   s3    ==7 7 7 7 =7
 
-r   c                   2    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
S )Type   ~   }   |   {   p   o   i   @   N)__name__
__module____qualname__I32I64F32F64V128FUNCREF	EXTERNREFEXNREFVOID r   r   r0   r0   F   s;        ####	$')&	$$$r   r0   c                   f    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZdZdZdZdZdZdZdS )OpCoder   r                      !   "   #   $      A   B   C   D   j   r4      r   r   N)r:   r;   r<   NOPBLOCKENDBRBR_TABLECALLDROP	LOCAL_GET	LOCAL_SET	LOCAL_TEE
GLOBAL_GET
GLOBAL_SETRETURN	I32_CONST	I64_CONST	F32_CONST	F64_CONSTI32_ADDI64_ADDREF_NULLATOMIC_PREFIXMEMORY_PREFIXrF   r   r   rH   rH   R   s|        #
%#"(	$	$)))**&))))''(----r   rH   c                       e Zd ZdZdZdZdZdS )MemoryOpCoder   	   
   rI   N)r:   r;   r<   MEMORY_INITMEMORY_DROPMEMORY_COPYMEMORY_FILLrF   r   r   rq   rq   k   s"        ++++++r   rq   c                   "    e Zd ZdZdZdZdZdZdS )AtomicOpCoder   r   r      H   N)r:   r;   r<   ATOMIC_NOTIFYATOMIC_WAIT32ATOMIC_WAIT64ATOMIC_I32_STOREATOMIC_I32_RMW_CMPXCHGrF   r   r   ry   ry   r   s+        ---r   ry   c                   F    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdS )SecTyper   r   r   r                  r   rr   rJ   rs   rI   N)r:   r;   r<   CUSTOMTYPEIMPORTFUNCTIONTABLEMEMORYTAGGLOBALEXPORTSTARTELEM	DATACOUNTCODEDATArF   r   r   r   r   z   sT        &	
$&(
%&
#&&
%	
$)	$	$$$r   r   c                   "    e Zd ZdZdZdZdZdZdS )
ExternTyper   r   r   r   r   N)r:   r;   r<   FUNCr   r   r   r   rF   r   r   r   r      s'        	
$
%&&	###r   r   c                   "    e Zd ZdZdZdZdZdZdS )
DylinkTyper   r   r   r   r   N)r:   r;   r<   MEM_INFONEEDEDEXPORT_INFOIMPORT_INFORUNTIME_PATHrF   r   r   r   r      s'        (&++,,,r   r   c                       e Zd ZdZdZdS )TargetFeaturePrefix+   -   N)r:   r;   r<   USED
DISALLOWEDrF   r   r   r   r      s        	$***r   r   c                   >    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdS )NameTyper   r   r   r   r   r   r   r   r   rr   rs   rI   N)r:   r;   r<   MODULEr   LOCALLABELr   r   r   r   ELEMSEGMENTDATASEGMENTFIELDr   rF   r   r   r   r      sJ        &(
%
%	
$
%&&++
%
###r   r   c                       e Zd ZdS )InvalidWasmErrorN)r:   r;   r<   rF   r   r   r   r      s        $r   r   Section)typesizeoffsetnameLimits)flagsinitialmaximumImport)kindmodulefieldr   Export)r   r   indexGlobal)r   mutableinitDylink)mem_size	mem_align
table_sizetable_alignneededexport_infoimport_inforuntime_pathsTable	elem_typelimitsFunctionBodyr   r   DataSegment)r   r   r   r   FuncTypeparamsreturnsc                      e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zd Zed             Zed             Zed             Zed             Z ed+d            Z!d  Z"e#d!             Z$d" Z%d# Z&d$ Z'd% Z(d& Z)d' Z*ed(             Z+ed)             Z,d*S ),Modulez\Extremely minimal wasm module reader.  Currently only used
  for parsing the dylink section.c                 Z   d | _         || _        t          j                            |          | _        t          |d          | _         | j                             d          }| j                             d          }|t          k    s|t          k    rt          | d          i | _        d S )Nrbr   z is not a valid wasm file)buffilenameospathgetsizer   openreadMAGICVERSIONr   r'   )r(   r   magicversions       r   __init__zModule.__init__   s    DHDM))DIHd##DHHMM!EhmmAG~~G++CCCDDDDKKKr   c                 (    | j         r
J d            d S )NzF`__exit__` should have already been called, please use context manager)r   r(   s    r   __del__zModule.__del__   s    xaaaaa<aar   c                     | S r   rF   r   s    r   	__enter__zModule.__enter__   s    Kr   c                 X    | j         r"| j                                          d | _         d S d S r   )r   close)r(   	_exc_type_exc_val_exc_tbs       r   __exit__zModule.__exit__   s3    x 
hnndhhh r   c                 j    | j                             |           | j                             |          S r   )r   seekr   )r(   r   counts      r   read_atzModule.read_at   s*    HMM&8==r   c                 B    | j                             d          d         S )Nr   r   )r   r   r   s    r   	read_bytezModule.read_byte   s    8==Ar   c                 *    t          | j                  S r   )r    r   r   s    r   r    zModule.read_uleb       TXr   c                 *    t          | j                  S r   )r$   r   r   s    r   r$   zModule.read_sleb   r   r   c                     |                                  }| j                            |                              d          S )Nzutf-8)r    r   r   decode)r(   r   s     r   read_stringzModule.read_string   s3    >>D8==%%g...r   c                     |                                  }|                                 }d}|t          z  r|                                 }t          |||          S r   )r   r    LIMITS_HAS_MAXr   )r(   r   r   r   s       r   read_limitszModule.read_limits   sS    NNEnnGG~ !  g%'***r   c                 D    t          |                                           S r   )r0   r    r   s    r   	read_typezModule.read_type   s      !!!r   c                    g }	 t          |                                           }g }|xt           j        k    r) |                    |                                            nxxt           j        k    rnxt           j        k    rn n*  |                    |                                            nxt           j        k    r) |                    | 	                                           nJxt           j
        k    rn$xt           j        k    rnxt           j        k    rn n n	 t          d|z            |                    ||f           |t           j
        k    rnY|S )Nr   zunexpected opcode %s)rH   r   rd   appendr    rg   rh   r$   rm   r   r\   rk   rl   	Exception)r(   codeopcoder)   s       r   	read_initzModule.read_init   sN   Ddnn&&''fdV
++dnn&&
'
'
'
'0V 0 0 0 0 0 0 0 0 0
++dnn&&
'
'
'
'V____
++dnn&&
'
'
'
'VZZZZ.&....>6>>>>>>>
069::
:
kk64.!!!	6:		!" Kr   c                 6    | j                             |          S r   )r   r   )r(   r   s     r   r   zModule.seek	  s    8==   r   c                 4    | j                                         S r   )r   tellr   s    r   r  zModule.tell  s    8==??r   c                 P    | j                             |t          j                   d S r   )r   r   r   SEEK_CUR)r(   r   s     r   skipzModule.skip  s     HMM%%%%%%r   c              #     K   t           }|| j        k     r|                     |           t          |                                           }|                                 }| j                                        }d}|t          j        k    r| 	                                }t          ||||          V  ||z   }|| j        k     dS dS )z:Generator that lazily returns sections from the wasm file.N)HEADER_SIZEr   r   r   r   r    r   r  r   r   r   )r(   r   section_typesection_sizesection_offsetr   s         r   sectionszModule.sections  s      F
49


iiT^^--..l^^%%lx}}nd		'	'!!L,EEEEE,f 49





r   c                                           t          j                  }|sg S                      |j                                                    }g }t          |          D ]}                                 }|dk    sJ                                  } fdt          |          D             }                                 } fdt          |          D             }	|                    t          ||	                     |S )N`   c                 8    g | ]}                                 S rF   r   .0_r(   s     r   
<listcomp>z$Module.get_types.<locals>.<listcomp>.  s#    <<<Q  <<<r   c                 8    g | ]}                                 S rF   r  r  s     r   r  z$Module.get_types.<locals>.<listcomp>1  s#    >>>a!!>>>r   )
get_sectionr   r   r   r   r    ranger   r   r   )
r(   type_section	num_typestypesr  	type_form
num_paramsr   num_returnsr   s
   `         r   	get_typeszModule.get_types!  s	   ##GL11L iIIl!"""  IE9 
. 
...""i$>>##j<<<<%
*;*;<<<fNN$$k>>>>5+=+=>>>gll8FG,,----Lr   c           
         t          |                                           }|j        t          j        k    sJ |                     |j                   g }i }i }g }|                                  |j        dk    r| 	                                }| 	                                }| 	                                }| 	                                }	| 	                                }
|
r0|                                 }|
                    |           |
dz  }
|
0n|j        dk    r|j        |j        z   }|                                 |k     r| 	                                }| 	                                }|                                 |z   }|xt          j        k    rS | 	                                }| 	                                }| 	                                }| 	                                }	nxt          j        k    rI | 	                                }
|
r0|                                 }|
                    |           |
dz  }
|
0ngxt          j        k    rM | 	                                }|r4|                                 }| 	                                }|||<   |dz  }|4n
xt          j        k    r| | 	                                }|rd|                                 }|                                 }| 	                                }|                    |i            |||         |<   |dz  }|dn~t          j        k    rG| 	                                }|r0|                                 }|
                    |           |dz  }|0n(	 t)          d|            |                     |           |                                 |k    sJ |                                 |k     nt-          j        d           t1          ||||	||||          S )Ndylinkr   zdylink.0zunknown subsection: zerror parsing shared library)nextr  r   r   r   r   r   r   r   r    r   r   r  r   r   r   r   r   
setdefaultr   printr
  r   exit_with_errorr   )r(   dylink_sectionr   r   r   r   r   r   r   r   needed_countlibnamesection_endsubsection_typesubsection_sizeendr   symr   r   r   rpaths                         r   parse_dylink_sectionzModule.parse_dylink_section7  s   $--//**N'.0000IIn#$$$FKKMh&&!!h..""i>>##jNN$$k^^%%l ""$$g   
	
	*	*")N,??kIIKK+%%..**..**iikkO+"z""""~~''H((I))J..**KK z    >>++L  ((**gmmG$$$al    &z%%%%NN$$E $$&&cnn&&e!&k#qje	  
 &z%%%%NN$$E ''))f&&((enn&&e$$VR000+0k&!%(qje   &&&NN$$E &&((e""5)))qje   :::;;;IIo&&&yy{{c!!!!U IIKK+%%X :;;;(Iz;U`bopppr   c                    |                      t          j                  }|sg S |                     |j                   |                                 }g }t          |          D ]o}|                                 }t          | 	                                          }|                                 }|
                    t          |||                     p|S r   )r  r   r   r   r   r    r  r   r   r   r   r   )r(   export_sectionnum_exportsexportsr  r   r   r   s           r   get_exportszModule.get_exports  s    %%gn55N iIIn#$$$..""KG; 0 0d(())dnnennVD$..////Nr   c           	         |                      t          j                  }|sg S |                     |j                   |                                 }g }t          |          D ]{}|                                 }|                                 }t          | 	                                          }d }|xt          j
        k    r |                                 }nxt          j        k    r* |                                 }| 	                                 nxt          j        k    r |                                  nxt          j        k    r* |                                 }|                                  nGt          j        k    r)| 	                                 |                                 }n	 t#                      |                    t'          ||||                     }|S r   )r  r   r   r   r   r    r  r   r   r   r   r   r$   r   r   r   r   AssertionErrorr   r   )	r(   import_sectionnum_importsimportsr  modr   r   type_s	            r   get_importszModule.get_imports  s   %%gn55N iIIn#$$$..""KG; 6 6c  e(())deZ____..""%%Z..""%
..



Z






Z..""%






^^^
..


..""%%  
 nnVD#ue445555Nr   c                    |                      t          j                  }|sg S g }|                     |j                   |                                 }t          |          D ]b}|                                 }|                                 }| 	                                }|
                    t          |||                     c|S r   )r  r   r   r   r   r    r  r   r   r  r   r   )r(   global_sectiongloblsnum_globalsr  global_typer   r   s           r   get_globalszModule.get_globals  s    %%gn55N iFIIn#$$$..""K; 8 8NN$$k  g^^dmmF;667777Mr   c                     |                      t          j                  }|sd S |                     |j                   |                                 S r   )r  r   r   r   r   r    )r(   start_sections     r   	get_startzModule.get_start  sI    $$W]33M TIIm"###>>r   c                    |                      t          j                  }|sg S g }|                     |j                   |                                 }t          |          D ]e}|                                 }|                                 }|                    t          ||                     |                     ||z              f|S r   )
r  r   r   r   r   r    r  r  r   r   )r(   code_section	functionsnum_functionsr  	body_sizestarts          r   get_functionszModule.get_functions  s    ##GL11L iIIIl!"""NN$$M=!! # #..""iiikke|E955666
ii	!""""r   c                 `    t          fd|                                 D             d           S )Nc              3   2   K   | ]}|j         k    |V  d S r   )r   )r  ssection_codes     r   	<genexpr>z%Module.get_section.<locals>.<genexpr>  s/      FFqqv/E/E/E/E/E/EFFr   )r%  r  )r(   rS  s    `r   r  zModule.get_section  s0    FFFFDMMOOFFFMMMr   c                 |    |                                  D ]&}|j        t          j        k    r|j        |k    r|c S 'd S r   )r  r   r   r   r   )r(   r   sections      r   get_custom_sectionzModule.get_custom_section  sC    ==??  		'	'GLD,@,@4r   c           	         g }|                      t          j                  }|                     |j                   |                                 }t          |          D ]}|                                 }|t          z  rd }n|                                 }|                                 }| 	                                }|
                    t          ||||                     |                     ||z              |S r   )r  r   r   r   r   r    r  SEG_PASSIVEr  r  r   r   )	r(   segmentsdata_sectionnum_segmentsr  r   r   r   r   s	            r   get_segmentszModule.get_segments  s    H##GL11LIIl!""">>##L<   	 	nne
+
  ~~^^dyy{{fook%vt<<===
iiOr   c                 f   |                      t          j                  }|sg S |                     |j                   |                                 }g }t          |          D ]M}|                                 }|                                 }|	                    t          ||                     N|S r   )r  r   r   r   r   r    r  r   r   r   r   )r(   table_section
num_tablestablesr  r   r   s          r   
get_tableszModule.get_tables  s    $$W]33M iIIm"###!!JF: . ...""i!!fmmE)V,,----Mr   c                                            t          j                  }|sg S                      |j                                                    } fdt          |          D             S )Nc                 8    g | ]}                                 S rF   )r    r  s     r   r  z-Module.get_function_types.<locals>.<listcomp>  s#    777DNN777r   )r  r   r   r   r   r    r  )r(   function_sectionr  s   `  r   get_function_typeszModule.get_function_types  sn    ''(899 iII%&&&  I7777eI&6&67777r   Tc                    |                                  t          |                                           z   }d g|z  }|                     d          }|s|S |                     |j                   |                                  |j        |j        z   }|                                 |k     r| 	                                }| 
                                }|dk    ri| 
                                }t          |          D ]D}	| 
                                }
|                                 }|
t          |          k     sJ |||
<   En|                     |           |                                 |k     |r||                                  d          n|S )Nr   r   )num_imported_funcslenrO  rW  r   r   r   r   r  r   r    r  r
  )r(   remove_imports	num_funcsnamesname_sectionr,  subsection_idr.  r   r  func_idx	func_names               r   get_function_nameszModule.get_function_names  s   ''))C0B0B0D0D,E,EEIFYE**622L lIIl!"""%(99K
))++
#
#nn&&m((o	!		  u 	& 	&A^^%%(&&(()CJJ&&&&%%//		& 			/""" ))++
#
# 1?I5((**++,,EIr   c                 0    |                      d          d uS )Nr   )rW  r   s    r   has_name_sectionzModule.has_name_section+  s    ""6**$66r   c                     i | _         |                                 D ]G}| j                             |j        g            | j         |j                                     |           Hd S r   )imports_by_kindr?  r&  r   r   )r(   r#   s     r   _calc_indexeszModule._calc_indexes.  sk    D - -
%%afb111
16"))!,,,,- -r   c                     |                                   t          | j                            t          j        g                     S r   )rv  ri  ru  getr   r   r   s    r   rh  zModule.num_imported_funcs5  s7    t#''
<<===r   c                     |                                   t          | j                            t          j        g                     S r   )rv  ri  ru  rx  r   r   r   s    r   num_imported_globalszModule.num_imported_globals9  s8    t#''
(92>>???r   c                     |                                   ||                                 k    sJ |                                 ||                                 z
           S r   )rv  rh  rO  r(   idxs     r   get_functionzModule.get_function=  sY    $))++++++d&=&=&?&? ?@@r   c              #     K   |                                   t          |                                 |                                 t          |                                           z             D ]}||                     |          fV  d S r   )rv  r  rh  ri  rO  r~  r|  s     r   iter_functions_by_indexzModule.iter_functions_by_indexB  s      T,,..,,..T5G5G5I5I1J1JJL L ( (""3'''''''( (r   c                     |                                   ||                                 k    sJ |                                 ||                                 z
           S r   )rv  rz  rE  r|  s     r   
get_globalzModule.get_globalH  sY    $++------cD$=$=$?$??@@r   c                 6   |                                   ||                                 k     r%| j        t          j                 |         }|j        }n/|                                 ||                                 z
           }|                                 |         S r   )rv  rh  ru  r   r   r   rf  r"  )r(   r}  imp	func_types       r   get_function_typezModule.get_function_typeM  s    
T$$&&&& 1#6c(ii))++C$2I2I2K2K,KLi>>I&&r   c                    |                      d          }|si S |                     |j                   |                                 dk    sJ i }|                                  |                                 |j        |j        z   k     r_t          |                                           }|                                 }|||<   |                                 |j        |j        z   k     _|S )Ntarget_features)rW  r   r   r   r   r  r   r   )r(   rV  featuresprefixfeatures        r   get_target_featureszModule.get_target_featuresV  s    %%&788G iIIgn!22222HNN
))++5
5
5"4>>#3#344f  ""g hw ))++5
5
5 Or   c                     |                      d          }|sdS |                     |j                   |                                  |                                 S )NsourceMappingURL )rW  r   r   r   )r(   rV  s     r   get_sourceMappingURLzModule.get_sourceMappingURLe  s[    %%&899G RIIgnr   N)T)-r:   r;   r<   __doc__r   r   r   r   r   r   r    r$   r   r   r   r  r   r  r
  r  r	   r"  r2  r7  r?  rE  rH  rO  r  rW  r]  rb  rf  rq  rs  r.   rv  rh  rz  r~  r  r  r  r  r  rF   r   r   r   r      s;       % %	 	 	b b b    
           / / /+ + +" " "  *! ! !  & & &- - -   7* Fq Fq 7FqP   7    7B   7   7   7N N N   7   7"   7 8 8 78 J J J 7J67 7 7 - - 4-> > >@ @ @A A A
( ( (A A A
' ' '   7   7  r   r   c                 |    t          |           5 }|                                cd d d            S # 1 swxY w Y   d S r   )r   r2  	wasm_filer   s     r   r2  r2  o  s    i )F&&(() ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) )   155c                 |    t          |           5 }|                                cd d d            S # 1 swxY w Y   d S r   )r   r7  r  s     r   r7  r7  t      i  F                                   r  c                 |    t          |           5 }|                                cd d d            S # 1 swxY w Y   d S r   )r   r?  r  s     r   r?  r?  y  r  r  c                     g }t          |           }|j                                        D ]D}|                                D ]-\  }}|t          z  t
          k    r|                    |           .E|S r   )r2  r   valuesitemsSYMBOL_BINDING_MASKSYMBOL_BINDING_WEAKr   )r  weak_imports
dylink_secsymbolssymbolr   s         r   get_weak_importsr  ~  s    ,#I..*'..00 $ $g  $ $	$	$(;	;	;F###$ 
r   )>r  loggingr   syscollectionsr   enumr   	functoolsr   r  r   r	   r   r   path_from_rootr   	getLoggerloggerWASM_PAGE_SIZEr   r   r  r   rY  PREFIX_MATHPREFIX_THREADSPREFIX_SIMDr  SYMBOL_BINDING_GLOBALr  SYMBOL_BINDING_LOCALr   r    r$   r.   r0   rH   rq   ry   r   r   r   r   r   BaseExceptionr   r   r   r   r   r   r   r   r   r   r   r   r2  r7  r?  r  rF   r   r   <module>r     sZ     				 



 " " " " " "                         $$]33 4 4 4 		=	)	)
      * * ** * *  	 	 	 	 	7 	 	 	    W   2    7            7          g   "
 
 
 
 
 
 
 
           '   
    w       }    *Y B B B
C
C	H===	>	>	HAAA	B	B	H777	8	8	H999	:	:	H  N  N  N  
O  
O
7[(344z.8V*<==j(K(K(KLL:j8Y"788m m m m m m m m`) ) )
     
     
    r   