@@ -68,6 +68,8 @@ def find_segment_with_address(module, address, size=0):
6868 if seg .size == size :
6969 return (seg , 0 )
7070
71+ raise AssertionError ('unable to find segment for address: %s' % address )
72+
7173
7274def data_to_string (data ):
7375 data = data .decode ('utf8' )
@@ -82,14 +84,14 @@ def data_to_string(data):
8284 return data
8385
8486
85- def get_asm_strings (module , globls , export_map , imported_globals ):
87+ def get_asm_strings (module , export_map ):
8688 if '__start_em_asm' not in export_map or '__stop_em_asm' not in export_map :
8789 return {}
8890
8991 start = export_map ['__start_em_asm' ]
9092 end = export_map ['__stop_em_asm' ]
91- start_global = globls [ start .index - imported_globals ]
92- end_global = globls [ end .index - imported_globals ]
93+ start_global = module . get_global ( start .index )
94+ end_global = module . get_global ( end .index )
9395 start_addr = get_global_value (start_global )
9496 end_addr = get_global_value (end_global )
9597
@@ -110,29 +112,28 @@ def get_asm_strings(module, globls, export_map, imported_globals):
110112 return asm_strings
111113
112114
113- def get_main_reads_params (module , export_map , imported_funcs ):
115+ def get_main_reads_params (module , export_map ):
114116 if settings .STANDALONE_WASM :
115117 return 1
116118
117119 main = export_map .get ('main' ) or export_map .get ('__main_argc_argv' )
118120 if not main or main .kind != webassembly .ExternType .FUNC :
119121 return 0
120122
121- functions = module .get_functions ()
122- main_func = functions [main .index - imported_funcs ]
123+ main_func = module .get_function (main .index )
123124 if is_wrapper_function (module , main_func ):
124125 return 0
125126 else :
126127 return 1
127128
128129
129- def get_names_globals ( globls , exports , imported_globals ):
130+ def get_named_globals ( module , exports ):
130131 named_globals = {}
131132 for export in exports :
132133 if export .kind == webassembly .ExternType .GLOBAL :
133134 if export .name in ('__start_em_asm' , '__stop_em_asm' ) or export .name .startswith ('__em_js__' ):
134135 continue
135- g = globls [ export .index - imported_globals ]
136+ g = module . get_global ( export .index )
136137 named_globals [export .name ] = str (get_global_value (g ))
137138 return named_globals
138139
@@ -167,26 +168,20 @@ def extract_metadata(filename):
167168 export_names = []
168169 declares = []
169170 invoke_funcs = []
170- imported_funcs = 0
171- imported_globals = 0
172171 global_imports = []
173172 em_js_funcs = {}
174173 exports = module .get_exports ()
175174 imports = module .get_imports ()
176- globls = module .get_globals ()
177175
178176 for i in imports :
179- if i .kind == webassembly .ExternType .FUNC :
180- imported_funcs += 1
181- elif i .kind == webassembly .ExternType .GLOBAL :
182- imported_globals += 1
177+ if i .kind == webassembly .ExternType .GLOBAL :
183178 global_imports .append (i .field )
184179
185180 export_map = {e .name : e for e in exports }
186181 for e in exports :
187182 if e .kind == webassembly .ExternType .GLOBAL and e .name .startswith ('__em_js__' ):
188183 name = e .name [len ('__em_js__' ):]
189- globl = globls [ e .index - imported_globals ]
184+ globl = module . get_global ( e .index )
190185 string_address = get_global_value (globl )
191186 em_js_funcs [name ] = get_string_at (module , string_address )
192187
@@ -208,14 +203,14 @@ def extract_metadata(filename):
208203 # If main does not read its parameters, it will just be a stub that
209204 # calls __original_main (which has no parameters).
210205 metadata = {}
211- metadata ['asmConsts' ] = get_asm_strings (module , globls , export_map , imported_globals )
206+ metadata ['asmConsts' ] = get_asm_strings (module , export_map )
212207 metadata ['declares' ] = declares
213208 metadata ['emJsFuncs' ] = em_js_funcs
214209 metadata ['exports' ] = export_names
215210 metadata ['features' ] = features
216211 metadata ['globalImports' ] = global_imports
217212 metadata ['invokeFuncs' ] = invoke_funcs
218- metadata ['mainReadsParams' ] = get_main_reads_params (module , export_map , imported_funcs )
219- metadata ['namedGlobals' ] = get_names_globals ( globls , exports , imported_globals )
213+ metadata ['mainReadsParams' ] = get_main_reads_params (module , export_map )
214+ metadata ['namedGlobals' ] = get_named_globals ( module , exports )
220215 # print("Metadata parsed: " + pprint.pformat(metadata))
221216 return metadata
0 commit comments