@@ -203,105 +203,115 @@ def sections(self):
203203 yield Section (section_type , section_size , section_offset )
204204 offset = section_offset + section_size
205205
206-
207- def parse_dylink_section (wasm_file ):
208- module = Module (wasm_file )
209-
210- dylink_section = next (module .sections ())
211- assert dylink_section .type == SecType .CUSTOM
212- module .seek (dylink_section .offset )
213- # section name
214- section_name = module .readString ()
215- needed = []
216- export_info = {}
217-
218- if section_name == 'dylink' :
219- mem_size = module .readULEB ()
220- mem_align = module .readULEB ()
221- table_size = module .readULEB ()
222- table_align = module .readULEB ()
223-
224- needed_count = module .readULEB ()
225- while needed_count :
226- libname = module .readString ()
227- needed .append (libname )
228- needed_count -= 1
229- elif section_name == 'dylink.0' :
230- section_end = dylink_section .offset + dylink_section .size
231- while module .tell () < section_end :
232- subsection_type = module .readULEB ()
233- subsection_size = module .readULEB ()
234- end = module .tell () + subsection_size
235- if subsection_type == DylinkType .MEM_INFO :
236- mem_size = module .readULEB ()
237- mem_align = module .readULEB ()
238- table_size = module .readULEB ()
239- table_align = module .readULEB ()
240- elif subsection_type == DylinkType .NEEDED :
241- needed_count = module .readULEB ()
242- while needed_count :
243- libname = module .readString ()
244- needed .append (libname )
245- needed_count -= 1
246- elif subsection_type == DylinkType .EXPORT_INFO :
247- count = module .readULEB ()
248- while count :
249- sym = module .readString ()
250- flags = module .readULEB ()
251- export_info [sym ] = flags
252- count -= 1
206+ def parse_dylink_section (self ):
207+ dylink_section = next (self .sections ())
208+ assert dylink_section .type == SecType .CUSTOM
209+ self .seek (dylink_section .offset )
210+ # section name
211+ section_name = self .readString ()
212+ needed = []
213+ export_info = {}
214+
215+ if section_name == 'dylink' :
216+ mem_size = self .readULEB ()
217+ mem_align = self .readULEB ()
218+ table_size = self .readULEB ()
219+ table_align = self .readULEB ()
220+
221+ needed_count = self .readULEB ()
222+ while needed_count :
223+ libname = self .readString ()
224+ needed .append (libname )
225+ needed_count -= 1
226+ elif section_name == 'dylink.0' :
227+ section_end = dylink_section .offset + dylink_section .size
228+ while self .tell () < section_end :
229+ subsection_type = self .readULEB ()
230+ subsection_size = self .readULEB ()
231+ end = self .tell () + subsection_size
232+ if subsection_type == DylinkType .MEM_INFO :
233+ mem_size = self .readULEB ()
234+ mem_align = self .readULEB ()
235+ table_size = self .readULEB ()
236+ table_align = self .readULEB ()
237+ elif subsection_type == DylinkType .NEEDED :
238+ needed_count = self .readULEB ()
239+ while needed_count :
240+ libname = self .readString ()
241+ needed .append (libname )
242+ needed_count -= 1
243+ elif subsection_type == DylinkType .EXPORT_INFO :
244+ count = self .readULEB ()
245+ while count :
246+ sym = self .readString ()
247+ flags = self .readULEB ()
248+ export_info [sym ] = flags
249+ count -= 1
250+ else :
251+ print (f'unknown subsection: { subsection_type } ' )
252+ # ignore unknown subsections
253+ self .skip (subsection_size )
254+ assert (self .tell () == end )
255+ else :
256+ utils .exit_with_error ('error parsing shared library' )
257+
258+ return Dylink (mem_size , mem_align , table_size , table_align , needed , export_info )
259+
260+ def get_exports (self ):
261+ export_section = next ((s for s in self .sections () if s .type == SecType .EXPORT ), None )
262+ if not export_section :
263+ return []
264+
265+ self .seek (export_section .offset )
266+ num_exports = self .readULEB ()
267+ exports = []
268+ for i in range (num_exports ):
269+ name = self .readString ()
270+ kind = ExternType (self .readByte ())
271+ index = self .readULEB ()
272+ exports .append (Export (name , kind , index ))
273+
274+ return exports
275+
276+ def get_imports (self ):
277+ import_section = next ((s for s in self .sections () if s .type == SecType .IMPORT ), None )
278+ if not import_section :
279+ return []
280+
281+ self .seek (import_section .offset )
282+ num_imports = self .readULEB ()
283+ imports = []
284+ for i in range (num_imports ):
285+ mod = self .readString ()
286+ field = self .readString ()
287+ kind = ExternType (self .readByte ())
288+ imports .append (Import (kind , mod , field ))
289+ if kind == ExternType .FUNC :
290+ self .readULEB () # sig
291+ elif kind == ExternType .GLOBAL :
292+ self .readSLEB () # global type
293+ self .readByte () # mutable
294+ elif kind == ExternType .MEMORY :
295+ self .readLimits () # limits
296+ elif kind == ExternType .TABLE :
297+ self .readSLEB () # table type
298+ self .readLimits () # limits
253299 else :
254- print (f'unknown subsection: { subsection_type } ' )
255- # ignore unknown subsections
256- module .skip (subsection_size )
257- assert (module .tell () == end )
258- else :
259- utils .exit_with_error ('error parsing shared library' )
300+ assert False
260301
261- return Dylink ( mem_size , mem_align , table_size , table_align , needed , export_info )
302+ return imports
262303
263304
264- def get_exports (wasm_file ):
305+ def parse_dylink_section (wasm_file ):
265306 module = Module (wasm_file )
266- export_section = next (( s for s in module .sections () if s . type == SecType . EXPORT ), None )
307+ return module .parse_dylink_section ( )
267308
268- module .seek (export_section .offset )
269- num_exports = module .readULEB ()
270- exports = []
271- for i in range (num_exports ):
272- name = module .readString ()
273- kind = ExternType (module .readByte ())
274- index = module .readULEB ()
275- exports .append (Export (name , kind , index ))
276309
277- return exports
310+ def get_exports (wasm_file ):
311+ module = Module (wasm_file )
312+ return module .get_exports ()
278313
279314
280315def get_imports (wasm_file ):
281316 module = Module (wasm_file )
282- import_section = next ((s for s in module .sections () if s .type == SecType .IMPORT ), None )
283- if not import_section :
284- return []
285-
286- module .seek (import_section .offset )
287- num_imports = module .readULEB ()
288- imports = []
289- for i in range (num_imports ):
290- mod = module .readString ()
291- field = module .readString ()
292- kind = ExternType (module .readByte ())
293- imports .append (Import (kind , mod , field ))
294- if kind == ExternType .FUNC :
295- module .readULEB () # sig
296- elif kind == ExternType .GLOBAL :
297- module .readSLEB () # global type
298- module .readByte () # mutable
299- elif kind == ExternType .MEMORY :
300- module .readLimits () # limits
301- elif kind == ExternType .TABLE :
302- module .readSLEB () # table type
303- module .readLimits () # limits
304- else :
305- assert False
306-
307- return imports
317+ return module .get_imports ()
0 commit comments