@@ -68,7 +68,7 @@ var LibraryDylink = {
68
68
} ,
69
69
70
70
$updateGOT__deps : [ '$GOT' , '$isInternalSym' ] ,
71
- $updateGOT : function ( exports ) {
71
+ $updateGOT : function ( exports , replace ) {
72
72
#if DYLINK_DEBUG
73
73
err ( "updateGOT: " + Object . keys ( exports ) . length ) ;
74
74
#endif
@@ -77,7 +77,6 @@ var LibraryDylink = {
77
77
continue ;
78
78
}
79
79
80
- var replace = false ;
81
80
var value = exports [ symName ] ;
82
81
#if ! WASM_BIGINT
83
82
if ( symName . startsWith ( 'orig$' ) ) {
@@ -117,7 +116,7 @@ var LibraryDylink = {
117
116
118
117
// Applies relocations to exported things.
119
118
$relocateExports__deps : [ '$updateGOT' ] ,
120
- $relocateExports : function ( exports , memoryBase ) {
119
+ $relocateExports : function ( exports , memoryBase , replace ) {
121
120
var relocated = { } ;
122
121
123
122
for ( var e in exports ) {
@@ -139,7 +138,7 @@ var LibraryDylink = {
139
138
}
140
139
relocated [ e ] = value ;
141
140
}
142
- updateGOT ( relocated ) ;
141
+ updateGOT ( relocated , replace ) ;
143
142
return relocated ;
144
143
} ,
145
144
@@ -256,37 +255,46 @@ var LibraryDylink = {
256
255
// returns the side module metadata as an object
257
256
// { memorySize, memoryAlign, tableSize, tableAlign, neededDynlibs}
258
257
$getDylinkMetadata : function ( binary ) {
259
- var next = 0 ;
258
+ var offset = 0 ;
259
+ var end = 0 ;
260
+
261
+ function getU8 ( ) {
262
+ return binary [ offset ++ ] ;
263
+ }
264
+
260
265
function getLEB ( ) {
261
266
var ret = 0 ;
262
267
var mul = 1 ;
263
268
while ( 1 ) {
264
- var byte = binary [ next ++ ] ;
269
+ var byte = binary [ offset ++ ] ;
265
270
ret += ( ( byte & 0x7f ) * mul ) ;
266
271
mul *= 0x80 ;
267
272
if ( ! ( byte & 0x80 ) ) break ;
268
273
}
269
274
return ret ;
270
275
}
271
276
277
+ function getString ( ) {
278
+ var len = getLEB ( ) ;
279
+ offset += len ;
280
+ return UTF8ArrayToString ( binary , offset - len , len ) ;
281
+ }
282
+
272
283
if ( binary instanceof WebAssembly . Module ) {
273
284
var dylinkSection = WebAssembly . Module . customSections ( binary , "dylink" ) ;
274
285
assert ( dylinkSection . length != 0 , 'need dylink section' ) ;
275
286
binary = new Uint8Array ( dylinkSection [ 0 ] ) ;
287
+ end = binary . length
276
288
} else {
277
289
var int32View = new Uint32Array ( new Uint8Array ( binary . subarray ( 0 , 24 ) ) . buffer ) ;
278
290
assert ( int32View [ 0 ] == 0x6d736100 , 'need to see wasm magic number' ) ; // \0asm
279
291
// we should see the dylink section right after the magic number and wasm version
280
292
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 ++ ;
293
+ offset = 9 ;
294
+ var section_size = getLEB ( ) ; //section size
295
+ end = offset + section_size ;
296
+ var name = getString ( ) ;
297
+ assert ( name == 'dylink' ) ;
290
298
}
291
299
292
300
var customSection = { } ;
@@ -304,12 +312,38 @@ var LibraryDylink = {
304
312
var neededDynlibsCount = getLEB ( ) ;
305
313
customSection . neededDynlibs = [ ] ;
306
314
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 ) ;
315
+ var name = getString ( ) ;
311
316
customSection . neededDynlibs . push ( name ) ;
312
317
}
318
+
319
+ #if DYLINK_DEBUG
320
+ err ( 'dylink needed:' + customSection . neededDynlibs ) ;
321
+ #endif
322
+ customSection . tlsExports = { } ;
323
+
324
+ var WASM_DYLINK_EXPORT_INFO = 0x1 ;
325
+ var WASM_DYLINK_IMPORT_INFO = 0x2 ;
326
+ var WASM_SYMBOL_TLS = 0x100 ;
327
+
328
+ while ( offset < end ) {
329
+ var subsectionType = getU8 ( ) ;
330
+ var subsectionSize = getLEB ( ) ;
331
+ if ( subsectionType === WASM_DYLINK_EXPORT_INFO ) {
332
+ var count = getLEB ( ) ;
333
+ while ( count -- ) {
334
+ var name = getString ( ) ;
335
+ var flags = getLEB ( ) ;
336
+ if ( flags & WASM_SYMBOL_TLS ) {
337
+ customSection . tlsExports [ name ] = 1 ;
338
+ }
339
+ }
340
+ } else {
341
+ err ( 'unknown subsection: ' + subsectionType )
342
+ // unknown subsection
343
+ offset += subsectionSize ;
344
+ }
345
+ }
346
+ assert ( offset == end ) ;
313
347
return customSection ;
314
348
} ,
315
349
@@ -485,17 +519,7 @@ var LibraryDylink = {
485
519
#if USE_PTHREADS
486
520
// Only one thread (currently The main thread) should call
487
521
// __wasm_call_ctors, but all threads need to call emscripten_tls_init
488
- var initTLS = moduleExports [ 'emscripten_tls_init' ] ;
489
- #if ASSERTIONS
490
- assert ( initTLS ) ;
491
- #endif
492
- #if DYLINK_DEBUG
493
- out ( "adding to tlsInitFunctions: " + initTLS ) ;
494
- #endif
495
- PThread . tlsInitFunctions . push ( initTLS ) ;
496
- if ( runtimeInitialized ) {
497
- initTLS ( ) ;
498
- }
522
+ registerTlsInit ( instance . exports, metadata )
499
523
if ( ! ENVIRONMENT_IS_PTHREAD ) {
500
524
#endif
501
525
var init = moduleExports [ '__wasm_call_ctors' ] ;
0 commit comments