@@ -256,60 +256,104 @@ var LibraryDylink = {
256
256
// returns the side module metadata as an object
257
257
// { memorySize, memoryAlign, tableSize, tableAlign, neededDynlibs}
258
258
$getDylinkMetadata : function ( binary ) {
259
- var next = 0 ;
259
+ var offset = 0 ;
260
+ var end = 0 ;
261
+
262
+ function getU8 ( ) {
263
+ return binary [ offset ++ ] ;
264
+ }
265
+
260
266
function getLEB ( ) {
261
267
var ret = 0 ;
262
268
var mul = 1 ;
263
269
while ( 1 ) {
264
- var byte = binary [ next ++ ] ;
270
+ var byte = binary [ offset ++ ] ;
265
271
ret += ( ( byte & 0x7f ) * mul ) ;
266
272
mul *= 0x80 ;
267
273
if ( ! ( byte & 0x80 ) ) break ;
268
274
}
269
275
return ret ;
270
276
}
271
277
278
+ function getString ( ) {
279
+ var len = getLEB ( ) ;
280
+ offset += len ;
281
+ return UTF8ArrayToString ( binary , offset - len , len ) ;
282
+ }
283
+
284
+ var name = 'dylink.0' ;
272
285
if ( binary instanceof WebAssembly . Module ) {
273
- var dylinkSection = WebAssembly . Module . customSections ( binary , "dylink" ) ;
286
+ var dylinkSection = WebAssembly . Module . customSections ( binary , name ) ;
287
+ if ( dylinkSection . length === 0 ) {
288
+ name = 'dylink'
289
+ dylinkSection = WebAssembly . Module . customSections ( binary , name ) ;
290
+ }
274
291
assert ( dylinkSection . length != 0 , 'need dylink section' ) ;
275
292
binary = new Uint8Array ( dylinkSection [ 0 ] ) ;
293
+ end = binary . length
276
294
} else {
277
295
var int32View = new Uint32Array ( new Uint8Array ( binary . subarray ( 0 , 24 ) ) . buffer ) ;
278
296
assert ( int32View [ 0 ] == 0x6d736100 , 'need to see wasm magic number' ) ; // \0asm
279
- // we should see the dylink section right after the magic number and wasm version
297
+ // we should see the dylink custom section right after the magic number and wasm version
280
298
assert ( binary [ 8 ] === 0 , 'need the dylink section to be first' )
281
- next = 9 ;
282
- getLEB ( ) ; //section size
283
- assert ( binary [ next ] === 6 ) ; next ++ ; // size of "dylink" string
284
- assert ( binary [ next ] === 'd' . charCodeAt ( 0 ) ) ; next ++ ;
285
- assert ( binary [ next ] === 'y' . charCodeAt ( 0 ) ) ; next ++ ;
286
- assert ( binary [ next ] === 'l' . charCodeAt ( 0 ) ) ; next ++ ;
287
- assert ( binary [ next ] === 'i' . charCodeAt ( 0 ) ) ; next ++ ;
288
- assert ( binary [ next ] === 'n' . charCodeAt ( 0 ) ) ; next ++ ;
289
- assert ( binary [ next ] === 'k' . charCodeAt ( 0 ) ) ; next ++ ;
290
- }
291
-
292
- var customSection = { } ;
293
- customSection . memorySize = getLEB ( ) ;
294
- customSection . memoryAlign = getLEB ( ) ;
295
- customSection . tableSize = getLEB ( ) ;
296
- customSection . tableAlign = getLEB ( ) ;
299
+ offset = 9 ;
300
+ var section_size = getLEB ( ) ; //section size
301
+ end = offset + section_size ;
302
+ name = getString ( ) ;
303
+ }
304
+
305
+ var customSection = { neededDynlibs : [ ] } ;
306
+ if ( name == 'dylink' ) {
307
+ customSection . memorySize = getLEB ( ) ;
308
+ customSection . memoryAlign = getLEB ( ) ;
309
+ customSection . tableSize = getLEB ( ) ;
310
+ customSection . tableAlign = getLEB ( ) ;
311
+ // shared libraries this module needs. We need to load them first, so that
312
+ // current module could resolve its imports. (see tools/shared.py
313
+ // WebAssembly.make_shared_library() for "dylink" section extension format)
314
+ var neededDynlibsCount = getLEB ( ) ;
315
+ for ( var i = 0 ; i < neededDynlibsCount ; ++ i ) {
316
+ var name = getString ( ) ;
317
+ customSection . neededDynlibs . push ( name ) ;
318
+ }
319
+ } else {
320
+ assert ( name === 'dylink.0' ) ;
321
+ var WASM_DYLINK_MEM_INFO = 0x1 ;
322
+ var WASM_DYLINK_NEEDED = 0x2 ;
323
+ while ( offset < end ) {
324
+ var subsectionType = getU8 ( ) ;
325
+ var subsectionSize = getLEB ( ) ;
326
+ if ( subsectionType === WASM_DYLINK_MEM_INFO ) {
327
+ customSection . memorySize = getLEB ( ) ;
328
+ customSection . memoryAlign = getLEB ( ) ;
329
+ customSection . tableSize = getLEB ( ) ;
330
+ customSection . tableAlign = getLEB ( ) ;
331
+ } else if ( subsectionType === WASM_DYLINK_NEEDED ) {
332
+ var neededDynlibsCount = getLEB ( ) ;
333
+ for ( var i = 0 ; i < neededDynlibsCount ; ++ i ) {
334
+ var name = getString ( ) ;
335
+ customSection . neededDynlibs . push ( name ) ;
336
+ }
337
+ } else {
338
+ #if ASSERTIONS
339
+ err ( 'unknown dylink.0 subsection: ' + subsectionType )
340
+ #endif
341
+ // unknown subsection
342
+ offset += subsectionSize ;
343
+ }
344
+ }
345
+ }
346
+
297
347
#if ASSERTIONS
298
348
var tableAlign = Math . pow ( 2 , customSection . tableAlign ) ;
299
349
assert ( tableAlign === 1 , 'invalid tableAlign ' + tableAlign ) ;
300
350
#endif
301
- // shared libraries this module needs. We need to load them first, so that
302
- // current module could resolve its imports. (see tools/shared.py
303
- // WebAssembly.make_shared_library() for "dylink" section extension format)
304
- var neededDynlibsCount = getLEB ( ) ;
305
- customSection . neededDynlibs = [ ] ;
306
- for ( var i = 0 ; i < neededDynlibsCount ; ++ i ) {
307
- var nameLen = getLEB ( ) ;
308
- var nameUTF8 = binary . subarray ( next , next + nameLen ) ;
309
- next += nameLen ;
310
- var name = UTF8ArrayToString ( nameUTF8 , 0 ) ;
311
- customSection . neededDynlibs . push ( name ) ;
312
- }
351
+
352
+ #if DYLINK_DEBUG
353
+ err ( 'dylink needed:' + customSection . neededDynlibs ) ;
354
+ #endif
355
+
356
+ assert ( offset == end ) ;
313
357
return customSection ;
314
358
} ,
315
359
0 commit comments