@@ -256,60 +256,106 @@ 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
280
- 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 ( ) ;
297
+ // we should see the dylink custom section right after the magic number and wasm version
298
+ if ( binary [ 8 ] !== 0 ) {
299
+ throw new Error ( 'invalid shared library' ) ;
300
+ }
301
+ offset = 9 ;
302
+ var section_size = getLEB ( ) ; //section size
303
+ end = offset + section_size ;
304
+ name = getString ( ) ;
305
+ }
306
+
307
+ assert ( name == 'dylink' || name == 'dylink.0' ) ;
308
+ var customSection = { neededDynlibs : [ ] } ;
309
+ if ( name == 'dylink' ) {
310
+ customSection . memorySize = getLEB ( ) ;
311
+ customSection . memoryAlign = getLEB ( ) ;
312
+ customSection . tableSize = getLEB ( ) ;
313
+ customSection . tableAlign = getLEB ( ) ;
314
+ // shared libraries this module needs. We need to load them first, so that
315
+ // current module could resolve its imports. (see tools/shared.py
316
+ // WebAssembly.make_shared_library() for "dylink" section extension format)
317
+ var neededDynlibsCount = getLEB ( ) ;
318
+ for ( var i = 0 ; i < neededDynlibsCount ; ++ i ) {
319
+ var name = getString ( ) ;
320
+ customSection . neededDynlibs . push ( name ) ;
321
+ }
322
+ } else if ( name == 'dylink.0' ) {
323
+ var WASM_DYLINK_MEM_INFO = 0x1 ;
324
+ var WASM_DYLINK_NEEDED = 0x2 ;
325
+ while ( offset < end ) {
326
+ var subsectionType = getU8 ( ) ;
327
+ var subsectionSize = getLEB ( ) ;
328
+ if ( subsectionType === WASM_DYLINK_MEM_INFO ) {
329
+ customSection . memorySize = getLEB ( ) ;
330
+ customSection . memoryAlign = getLEB ( ) ;
331
+ customSection . tableSize = getLEB ( ) ;
332
+ customSection . tableAlign = getLEB ( ) ;
333
+ } else if ( subsectionType === WASM_DYLINK_NEEDED ) {
334
+ var neededDynlibsCount = getLEB ( ) ;
335
+ for ( var i = 0 ; i < neededDynlibsCount ; ++ i ) {
336
+ var name = getString ( ) ;
337
+ customSection . neededDynlibs . push ( name ) ;
338
+ }
339
+ } else {
340
+ err ( 'unknown subsection: ' + subsectionType )
341
+ // unknown subsection
342
+ offset += subsectionSize ;
343
+ }
344
+ }
345
+ } else {
346
+ throw new Error ( 'invalid shared library' ) ;
347
+ }
348
+
297
349
#if ASSERTIONS
298
350
var tableAlign = Math . pow ( 2 , customSection . tableAlign ) ;
299
351
assert ( tableAlign === 1 , 'invalid tableAlign ' + tableAlign ) ;
300
352
#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
- }
353
+
354
+ #if DYLINK_DEBUG
355
+ err ( 'dylink needed:' + customSection . neededDynlibs ) ;
356
+ #endif
357
+
358
+ assert ( offset == end ) ;
313
359
return customSection ;
314
360
} ,
315
361
0 commit comments