-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Copy pathlibrary_dylink.js
1261 lines (1180 loc) · 42.7 KB
/
library_dylink.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @license
* Copyright 2020 The Emscripten Authors
* SPDX-License-Identifier: MIT
*
* Dynamic library loading
*/
#if !RELOCATABLE
#error "library_dylink.js requires RELOCATABLE"
#endif
var LibraryDylink = {
#if FILESYSTEM
$registerWasmPlugin__deps: ['$preloadPlugins'],
$registerWasmPlugin: function() {
// Use string keys here to avoid minification since the plugin consumer
// also uses string keys.
var wasmPlugin = {
'promiseChainEnd': Promise.resolve(),
'canHandle': function(name) {
return !Module.noWasmDecoding && name.endsWith('.so')
},
'handle': function(byteArray, name, onload, onerror) {
// loadWebAssemblyModule can not load modules out-of-order, so rather
// than just running the promises in parallel, this makes a chain of
// promises to run in series.
wasmPlugin['promiseChainEnd'] = wasmPlugin['promiseChainEnd'].then(
() => loadWebAssemblyModule(byteArray, {loadAsync: true, nodelete: true}, name)).then(
(exports) => {
#if DYLINK_DEBUG
dbg(`registering preloadedWasm: ${name}`);
#endif
preloadedWasm[name] = exports;
onload(byteArray);
},
(error) => {
err(`failed to instantiate wasm: ${name}: ${error}`);
onerror();
});
}
};
preloadPlugins.push(wasmPlugin);
},
$preloadedWasm__deps: ['$registerWasmPlugin'],
$preloadedWasm__postset: `
registerWasmPlugin();
`,
$preloadedWasm: {},
#endif // FILESYSTEM
$isSymbolDefined: function(symName) {
// Ignore 'stub' symbols that are auto-generated as part of the original
// `wasmImports` used to instantate the main module.
var existing = wasmImports[symName];
if (!existing || existing.stub) {
return false;
}
#if ASYNCIFY
// Even if a symbol exists in wasmImports, and is not itself a stub, it
// could be an ASYNCIFY wrapper function that wraps a stub function.
if (symName in asyncifyStubs && !asyncifyStubs[symName]) {
return false;
}
#endif
return true;
},
// Dynamic version of shared.py:make_invoke. This is needed for invokes
// that originate from side modules since these are not known at JS
// generation time.
#if !DISABLE_EXCEPTION_CATCHING || SUPPORT_LONGJMP == 'emscripten'
$createInvokeFunction__internal: true,
$createInvokeFunction__deps: ['$dynCall', 'setThrew'],
$createInvokeFunction: function(sig) {
return function() {
var sp = stackSave();
try {
return dynCall(sig, arguments[0], Array.prototype.slice.call(arguments, 1));
} catch(e) {
stackRestore(sp);
// Create a try-catch guard that rethrows the Emscripten EH exception.
#if EXCEPTION_STACK_TRACES
// Exceptions thrown from C++ and longjmps will be an instance of
// EmscriptenEH.
if (!(e instanceof EmscriptenEH)) throw e;
#else
// Exceptions thrown from C++ will be a pointer (number) and longjmp
// will throw the number Infinity. Use the compact and fast "e !== e+0"
// test to check if e was not a Number.
if (e !== e+0) throw e;
#endif
_setThrew(1, 0);
}
}
},
#endif
// Resolve a global symbol by name. This is used during module loading to
// resolve imports, and by `dlsym` when used with `RTLD_DEFAULT`.
// Returns both the resolved symbol (i.e. a function or a global) along with
// the canonical name of the symbol (in some cases is modify the symbol as
// part of the loop process, so that actual symbol looked up has a different
// name).
$resolveGlobalSymbol__deps: ['$isSymbolDefined',
#if !DISABLE_EXCEPTION_CATCHING || SUPPORT_LONGJMP == 'emscripten'
'$createInvokeFunction',
#endif
],
$resolveGlobalSymbol__internal: true,
$resolveGlobalSymbol: function(symName, direct = false) {
var sym;
#if !WASM_BIGINT
// First look for the orig$ symbol which is the symbol without i64
// legalization performed.
if (direct && ('orig$' + symName in wasmImports)) {
symName = 'orig$' + symName;
}
#endif
if (isSymbolDefined(symName)) {
sym = wasmImports[symName];
}
#if !DISABLE_EXCEPTION_CATCHING || SUPPORT_LONGJMP == 'emscripten'
// Asm.js-style exception handling: invoke wrapper generation
else if (symName.startsWith('invoke_')) {
// Create (and cache) new invoke_ functions on demand.
sym = wasmImports[symName] = createInvokeFunction(symName.split('_')[1]);
}
#endif
#if !DISABLE_EXCEPTION_CATCHING
else if (symName.startsWith('__cxa_find_matching_catch_')) {
// When the main module is linked we create whichever variants of
// `__cxa_find_matching_catch_` (see jsifier.js) that we know are needed,
// but a side module loaded at runtime might need different/additional
// variants so we create those dynamically.
sym = wasmImports[symName] = function() {
var args = Array.from(arguments);
#if MEMORY64
args = args.map(Number);
#endif
var rtn = findMatchingCatch(args);
return {{{ to64('rtn') }}};
}
}
#endif
return {sym, name: symName};
},
$GOT: {},
$currentModuleWeakSymbols: '=new Set({{{ JSON.stringify(Array.from(WEAK_IMPORTS)) }}})',
// Create globals to each imported symbol. These are all initialized to zero
// and get assigned later in `updateGOT`
$GOTHandler__internal: true,
$GOTHandler__deps: ['$GOT', '$currentModuleWeakSymbols'],
$GOTHandler: {
get(obj, symName) {
var rtn = GOT[symName];
if (!rtn) {
rtn = GOT[symName] = new WebAssembly.Global({'value': '{{{ POINTER_WASM_TYPE }}}', 'mutable': true});
#if DYLINK_DEBUG == 2
dbg("new GOT entry: " + symName);
#endif
}
if (!currentModuleWeakSymbols.has(symName)) {
// Any non-weak reference to a symbol marks it as `required`, which
// enabled `reportUndefinedSymbols` to report undefeind symbol errors
// correctly.
rtn.required = true;
}
return rtn;
}
},
$isInternalSym__internal: true,
$isInternalSym: function(symName) {
// TODO: find a way to mark these in the binary or avoid exporting them.
return [
'__cpp_exception',
'__c_longjmp',
'__wasm_apply_data_relocs',
'__dso_handle',
'__tls_size',
'__tls_align',
'__set_stack_limits',
'_emscripten_tls_init',
'__wasm_init_tls',
'__wasm_call_ctors',
'__start_em_asm',
'__stop_em_asm',
'__start_em_js',
'__stop_em_js',
].includes(symName) || symName.startsWith('__em_js__')
#if SPLIT_MODULE
// Exports synthesized by wasm-split should be prefixed with '%'
|| symName[0] == '%'
#endif
;
},
$updateGOT__internal: true,
$updateGOT__deps: ['$GOT', '$isInternalSym', '$addFunction', '$getFunctionAddress'],
$updateGOT: function(exports, replace) {
#if DYLINK_DEBUG
dbg("updateGOT: adding " + Object.keys(exports).length + " symbols");
#endif
for (var symName in exports) {
if (isInternalSym(symName)) {
continue;
}
var value = exports[symName];
#if !WASM_BIGINT
if (symName.startsWith('orig$')) {
symName = symName.split('$')[1];
replace = true;
}
#endif
if (!GOT[symName]) {
GOT[symName] = new WebAssembly.Global({'value': '{{{ POINTER_WASM_TYPE }}}', 'mutable': true});
}
if (replace || GOT[symName].value == 0) {
#if DYLINK_DEBUG == 2
dbg(`updateGOT: before: ${symName} : ${GOT[symName].value}`);
#endif
if (typeof value == 'function') {
GOT[symName].value = {{{ to64('addFunction(value)') }}};
#if DYLINK_DEBUG == 2
dbg(`updateGOT: FUNC: ${symName} : ${GOT[symName].value}`);
#endif
} else if (typeof value == {{{ POINTER_JS_TYPE }}}) {
GOT[symName].value = value;
} else {
err(`unhandled export type for '${symName}': ${typeof value}`);
}
#if DYLINK_DEBUG == 2
dbg(`updateGOT: after: ${symName} : ${GOT[symName].value} (${value})`);
#endif
}
#if DYLINK_DEBUG
else if (GOT[symName].value != value) {
dbg(`udateGOT: EXISTING SYMBOL: ${symName} : ${GOT[symName].value} (${value})`);
}
#endif
}
#if DYLINK_DEBUG
dbg("done updateGOT");
#endif
},
// Applies relocations to exported things.
$relocateExports__internal: true,
$relocateExports__deps: ['$updateGOT'],
$relocateExports__docs: '/** @param {boolean=} replace */',
$relocateExports: function(exports, memoryBase, replace) {
var relocated = {};
for (var e in exports) {
var value = exports[e];
#if SPLIT_MODULE
// Do not modify exports synthesized by wasm-split
if (e.startsWith('%')) {
relocated[e] = value
continue;
}
#endif
if (typeof value == 'object') {
// a breaking change in the wasm spec, globals are now objects
// https://github.com/WebAssembly/mutable-global/issues/1
value = value.value;
}
if (typeof value == {{{ POINTER_JS_TYPE }}}) {
value += {{{ to64('memoryBase') }}};
}
relocated[e] = value;
}
updateGOT(relocated, replace);
return relocated;
},
$reportUndefinedSymbols__internal: true,
$reportUndefinedSymbols__deps: ['$GOT', '$resolveGlobalSymbol'],
$reportUndefinedSymbols: function() {
#if DYLINK_DEBUG
dbg('reportUndefinedSymbols');
#endif
for (var symName in GOT) {
if (GOT[symName].value == 0) {
var value = resolveGlobalSymbol(symName, true).sym;
if (!value && !GOT[symName].required) {
// Ignore undefined symbols that are imported as weak.
#if DYLINK_DEBUG
dbg(`ignoring undefined weak symbol: ${symName}`);
#endif
continue;
}
#if ASSERTIONS
assert(value, `undefined symbol '${symName}'. perhaps a side module was not linked in? if this global was expected to arrive from a system library, try to build the MAIN_MODULE with EMCC_FORCE_STDLIBS=1 in the environment`);
#endif
#if DYLINK_DEBUG == 2
dbg(`assigning dynamic symbol from main module: ${symName} -> ${prettyPrint(value)}`);
#endif
if (typeof value == 'function') {
/** @suppress {checkTypes} */
GOT[symName].value = {{{ to64('addFunction(value, value.sig)') }}};
#if DYLINK_DEBUG == 2
dbg(`assigning table entry for : ${symName} -> ${GOT[symName].value}`);
#endif
} else if (typeof value == 'number') {
GOT[symName].value = {{{ to64('value') }}};
#if MEMORY64
} else if (typeof value == 'bigint') {
GOT[symName].value = value;
#endif
} else {
throw new Error(`bad export type for '${symName}': ${typeof value}`);
}
}
}
#if DYLINK_DEBUG
dbg('done reportUndefinedSymbols');
#endif
},
// dynamic linker/loader (a-la ld.so on ELF systems)
$LDSO__deps: ['$newDSO'],
$LDSO: {
// name -> dso [refcount, name, module, global]; Used by dlopen
loadedLibsByName: {},
// handle -> dso; Used by dlsym
loadedLibsByHandle: {},
init: () => {
#if ASSERTIONS
// This function needs to run after the initial wasmImports object
// as been created.
assert(wasmImports);
#endif
newDSO('__main__', {{{ cDefs.RTLD_DEFAULT }}}, wasmImports);
},
},
$dlSetError__internal: true,
$dlSetError__deps: ['__dl_seterr', '$stringToUTF8OnStack', '$withStackSave'],
$dlSetError: function(msg) {
#if DYLINK_DEBUG
dbg(`dlSetError: ${msg}`);
#endif
withStackSave(() => {
var cmsg = stringToUTF8OnStack(msg);
___dl_seterr(cmsg, 0);
});
},
// We support some amount of allocation during startup in the case of
// dynamic linking, which needs to allocate memory for dynamic libraries that
// are loaded. That has to happen before the main program can start to run,
// because the main program needs those linked in before it runs (so we can't
// use normally malloc from the main program to do these allocations).
//
// Allocate memory even if malloc isn't ready yet. The allocated memory here
// must be zero initialized since its used for all static data, including bss.
$getMemory__noleakcheck: true,
$getMemory__deps: ['$GOT', '__heap_base', '$zeroMemory', '$alignMemory', 'malloc'],
$getMemory: function(size) {
// After the runtime is initialized, we must only use sbrk() normally.
#if DYLINK_DEBUG
dbg("getMemory: " + size + " runtimeInitialized=" + runtimeInitialized);
#endif
if (runtimeInitialized) {
// Currently we don't support freeing of static data when modules are
// unloaded via dlclose. This function is tagged as `noleakcheck` to
// avoid having this reported as leak.
return zeroMemory(_malloc(size), size);
}
var ret = ___heap_base;
// Keep __heap_base stack aligned.
var end = ret + alignMemory(size, {{{ STACK_ALIGN }}});
#if ASSERTIONS
assert(end <= HEAP8.length, 'failure to getMemory - memory growth etc. is not supported there, call malloc/sbrk directly or increase INITIAL_MEMORY');
#endif
___heap_base = end;
GOT['__heap_base'].value = {{{ to64('end') }}};
return ret;
},
// returns the side module metadata as an object
// { memorySize, memoryAlign, tableSize, tableAlign, neededDynlibs}
$getDylinkMetadata__deps: ['$UTF8ArrayToString'],
$getDylinkMetadata__internal: true,
$getDylinkMetadata: function(binary) {
var offset = 0;
var end = 0;
function getU8() {
return binary[offset++];
}
function getLEB() {
var ret = 0;
var mul = 1;
while (1) {
var byte = binary[offset++];
ret += ((byte & 0x7f) * mul);
mul *= 0x80;
if (!(byte & 0x80)) break;
}
return ret;
}
function getString() {
var len = getLEB();
offset += len;
return UTF8ArrayToString(binary, offset - len, len);
}
/** @param {string=} message */
function failIf(condition, message) {
if (condition) throw new Error(message);
}
var name = 'dylink.0';
if (binary instanceof WebAssembly.Module) {
var dylinkSection = WebAssembly.Module.customSections(binary, name);
if (dylinkSection.length === 0) {
name = 'dylink'
dylinkSection = WebAssembly.Module.customSections(binary, name);
}
failIf(dylinkSection.length === 0, 'need dylink section');
binary = new Uint8Array(dylinkSection[0]);
end = binary.length
} else {
var int32View = new Uint32Array(new Uint8Array(binary.subarray(0, 24)).buffer);
#if SUPPORT_BIG_ENDIAN
var magicNumberFound = int32View[0] == 0x6d736100 || int32View[0] == 0x0061736d;
#else
var magicNumberFound = int32View[0] == 0x6d736100;
#endif
failIf(!magicNumberFound, 'need to see wasm magic number'); // \0asm
// we should see the dylink custom section right after the magic number and wasm version
failIf(binary[8] !== 0, 'need the dylink section to be first')
offset = 9;
var section_size = getLEB(); //section size
end = offset + section_size;
name = getString();
}
var customSection = { neededDynlibs: [], tlsExports: new Set(), weakImports: new Set() };
if (name == 'dylink') {
customSection.memorySize = getLEB();
customSection.memoryAlign = getLEB();
customSection.tableSize = getLEB();
customSection.tableAlign = getLEB();
// shared libraries this module needs. We need to load them first, so that
// current module could resolve its imports. (see tools/shared.py
// WebAssembly.make_shared_library() for "dylink" section extension format)
var neededDynlibsCount = getLEB();
for (var i = 0; i < neededDynlibsCount; ++i) {
var libname = getString();
customSection.neededDynlibs.push(libname);
}
} else {
failIf(name !== 'dylink.0');
var WASM_DYLINK_MEM_INFO = 0x1;
var WASM_DYLINK_NEEDED = 0x2;
var WASM_DYLINK_EXPORT_INFO = 0x3;
var WASM_DYLINK_IMPORT_INFO = 0x4;
var WASM_SYMBOL_TLS = 0x100;
var WASM_SYMBOL_BINDING_MASK = 0x3;
var WASM_SYMBOL_BINDING_WEAK = 0x1;
while (offset < end) {
var subsectionType = getU8();
var subsectionSize = getLEB();
if (subsectionType === WASM_DYLINK_MEM_INFO) {
customSection.memorySize = getLEB();
customSection.memoryAlign = getLEB();
customSection.tableSize = getLEB();
customSection.tableAlign = getLEB();
} else if (subsectionType === WASM_DYLINK_NEEDED) {
var neededDynlibsCount = getLEB();
for (var i = 0; i < neededDynlibsCount; ++i) {
libname = getString();
customSection.neededDynlibs.push(libname);
}
} else if (subsectionType === WASM_DYLINK_EXPORT_INFO) {
var count = getLEB();
while (count--) {
var symname = getString();
var flags = getLEB();
if (flags & WASM_SYMBOL_TLS) {
customSection.tlsExports.add(symname);
}
}
} else if (subsectionType === WASM_DYLINK_IMPORT_INFO) {
var count = getLEB();
while (count--) {
var modname = getString();
var symname = getString();
var flags = getLEB();
if ((flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) {
customSection.weakImports.add(symname);
}
}
} else {
#if ASSERTIONS
err(`unknown dylink.0 subsection: ${subsectionType}`)
#endif
// unknown subsection
offset += subsectionSize;
}
}
}
#if ASSERTIONS
var tableAlign = Math.pow(2, customSection.tableAlign);
assert(tableAlign === 1, `invalid tableAlign ${tableAlign}`);
assert(offset == end);
#endif
#if DYLINK_DEBUG
dbg(`dylink needed:${customSection.neededDynlibs}`);
#endif
return customSection;
},
// Module.symbols <- libModule.symbols (flags.global handler)
$mergeLibSymbols__deps: ['$isSymbolDefined'],
$mergeLibSymbols: function(exports, libName) {
// add symbols into global namespace TODO: weak linking etc.
for (var sym in exports) {
if (!exports.hasOwnProperty(sym)) {
continue;
}
#if ASSERTIONS == 2
if (isSymbolDefined(sym)) {
var curr = wasmImports[sym], next = exports[sym];
// don't warn on functions - might be odr, linkonce_odr, etc.
if (!(typeof curr == 'function' && typeof next == 'function')) {
err("warning: symbol '" + sym + "' from '" + libName + "' already exists (duplicate symbol? or weak linking, which isn't supported yet?)"); // + [curr, ' vs ', next]);
}
}
#endif
// When RTLD_GLOBAL is enabled, the symbols defined by this shared object
// will be made available for symbol resolution of subsequently loaded
// shared objects.
//
// We should copy the symbols (which include methods and variables) from
// SIDE_MODULE to MAIN_MODULE.
const setImport = (target) => {
#if ASYNCIFY
if (target in asyncifyStubs) {
asyncifyStubs[target] = exports[sym]
}
#endif
if (!isSymbolDefined(target)) {
wasmImports[target] = exports[sym];
}
}
setImport(sym);
#if !hasExportedSymbol('main')
// Special case for handling of main symbol: If a side module exports
// `main` that also acts a definition for `__main_argc_argv` and vice
// versa.
const main_alias = '__main_argc_argv';
if (sym == 'main') {
setImport(main_alias)
}
if (sym == main_alias) {
setImport('main')
}
#endif
if (sym.startsWith('dynCall_') && !Module.hasOwnProperty(sym)) {
Module[sym] = exports[sym];
}
}
},
#if DYLINK_DEBUG
$dumpTable: function() {
for (var i = 0; i < wasmTable.length; i++)
dbg(`table: ${i} : ${wasmTable.get(i)}`);
},
#endif
// Loads a side module from binary data or compiled Module. Returns the module's exports or a
// promise that resolves to its exports if the loadAsync flag is set.
$loadWebAssemblyModule__docs: `
/**
* @param {string=} libName
* @param {Object=} localScope
* @param {number=} handle
*/`,
$loadWebAssemblyModule__deps: [
'$loadDynamicLibrary', '$getMemory',
'$relocateExports', '$resolveGlobalSymbol', '$GOTHandler',
'$getDylinkMetadata', '$alignMemory', '$zeroMemory',
'$currentModuleWeakSymbols',
'$updateTableMap',
],
$loadWebAssemblyModule: function(binary, flags, libName, localScope, handle) {
#if DYLINK_DEBUG
dbg(`loadWebAssemblyModule: ${libName}`);
#endif
var metadata = getDylinkMetadata(binary);
currentModuleWeakSymbols = metadata.weakImports;
#if ASSERTIONS
var originalTable = wasmTable;
#endif
// loadModule loads the wasm module after all its dependencies have been loaded.
// can be called both sync/async.
function loadModule() {
// The first thread to load a given module needs to allocate the static
// table and memory regions. Later threads re-use the same table region
// and can ignore the memory region (since memory is shared between
// threads already).
// If `handle` is specified than it is assumed that the calling thread has
// exclusive access to it for the duration of this function. See the
// locking in `dynlink.c`.
var firstLoad = !handle || !{{{ makeGetValue('handle', C_STRUCTS.dso.mem_allocated, 'i8') }}};
if (firstLoad) {
// alignments are powers of 2
var memAlign = Math.pow(2, metadata.memoryAlign);
// prepare memory
var memoryBase = metadata.memorySize ? alignMemory(getMemory(metadata.memorySize + memAlign), memAlign) : 0; // TODO: add to cleanups
var tableBase = metadata.tableSize ? wasmTable.length : 0;
if (handle) {
{{{ makeSetValue('handle', C_STRUCTS.dso.mem_allocated, '1', 'i8') }}};
{{{ makeSetValue('handle', C_STRUCTS.dso.mem_addr, 'memoryBase', '*') }}};
{{{ makeSetValue('handle', C_STRUCTS.dso.mem_size, 'metadata.memorySize', 'i32') }}};
{{{ makeSetValue('handle', C_STRUCTS.dso.table_addr, 'tableBase', '*') }}};
{{{ makeSetValue('handle', C_STRUCTS.dso.table_size, 'metadata.tableSize', 'i32') }}};
}
} else {
memoryBase = {{{ makeGetValue('handle', C_STRUCTS.dso.mem_addr, '*') }}};
tableBase = {{{ makeGetValue('handle', C_STRUCTS.dso.table_addr, '*') }}};
}
var tableGrowthNeeded = tableBase + metadata.tableSize - wasmTable.length;
if (tableGrowthNeeded > 0) {
#if DYLINK_DEBUG
dbg("loadModule: growing table: " + tableGrowthNeeded);
#endif
wasmTable.grow(tableGrowthNeeded);
}
#if DYLINK_DEBUG
dbg("loadModule: memory[" + memoryBase + ":" + (memoryBase + metadata.memorySize) + "]" +
" table[" + tableBase + ":" + (tableBase + metadata.tableSize) + "]");
#endif
// This is the export map that we ultimately return. We declare it here
// so it can be used within resolveSymbol. We resolve symbols against
// this local symbol map in the case there they are not present on the
// global Module object. We need this fallback because Modules sometime
// need to import their own symbols
var moduleExports;
function resolveSymbol(sym) {
var resolved = resolveGlobalSymbol(sym).sym;
if (!resolved && localScope) {
resolved = localScope[sym];
}
if (!resolved) {
resolved = moduleExports[sym];
}
#if ASSERTIONS
assert(resolved, `undefined symbol '${sym}'. perhaps a side module was not linked in? if this global was expected to arrive from a system library, try to build the MAIN_MODULE with EMCC_FORCE_STDLIBS=1 in the environment`);
#endif
return resolved;
}
// TODO kill ↓↓↓ (except "symbols local to this module", it will likely be
// not needed if we require that if A wants symbols from B it has to link
// to B explicitly: similarly to -Wl,--no-undefined)
//
// wasm dynamic libraries are pure wasm, so they cannot assist in
// their own loading. When side module A wants to import something
// provided by a side module B that is loaded later, we need to
// add a layer of indirection, but worse, we can't even tell what
// to add the indirection for, without inspecting what A's imports
// are. To do that here, we use a JS proxy (another option would
// be to inspect the binary directly).
var proxyHandler = {
get(stubs, prop) {
// symbols that should be local to this module
switch (prop) {
case '__memory_base':
return {{{ to64('memoryBase') }}};
case '__table_base':
return {{{ to64('tableBase') }}};
#if MEMORY64
#if MEMORY64 == 2
case '__memory_base32':
return memoryBase;
#endif
case '__table_base32':
return tableBase;
#endif
}
if (prop in wasmImports && !wasmImports[prop].stub) {
// No stub needed, symbol already exists in symbol table
return wasmImports[prop];
}
// Return a stub function that will resolve the symbol
// when first called.
if (!(prop in stubs)) {
var resolved;
stubs[prop] = function() {
if (!resolved) resolved = resolveSymbol(prop);
return resolved.apply(null, arguments);
};
}
return stubs[prop];
}
};
var proxy = new Proxy({}, proxyHandler);
var info = {
'GOT.mem': new Proxy({}, GOTHandler),
'GOT.func': new Proxy({}, GOTHandler),
'env': proxy,
'{{{ WASI_MODULE_NAME }}}': proxy,
};
function postInstantiation(module, instance) {
#if ASSERTIONS
// the table should be unchanged
assert(wasmTable === originalTable);
#endif
#if PTHREADS
if (!ENVIRONMENT_IS_PTHREAD && libName) {
#if DYLINK_DEBUG
dbg(`registering sharedModules: ${libName}`)
#endif
// cache all loaded modules in `sharedModules`, which gets passed
// to new workers when they are created.
sharedModules[libName] = module;
}
#endif
// add new entries to functionsInTableMap
updateTableMap(tableBase, metadata.tableSize);
moduleExports = relocateExports(instance.exports, memoryBase);
#if ASYNCIFY
moduleExports = Asyncify.instrumentWasmExports(moduleExports);
#endif
if (!flags.allowUndefined) {
reportUndefinedSymbols();
}
#if STACK_OVERFLOW_CHECK >= 2
// If the runtime has already been initialized we set the stack limits
// now. Otherwise this is delayed until `setDylinkStackLimits` is
// called after initialization.
if (moduleExports['__set_stack_limits'] && runtimeInitialized) {
moduleExports['__set_stack_limits']({{{ to64('_emscripten_stack_get_base()') }}}, {{{ to64('_emscripten_stack_get_end()') }}});
}
#endif
#if MAIN_MODULE
function addEmAsm(addr, body) {
var args = [];
var arity = 0;
for (; arity < 16; arity++) {
if (body.indexOf('$' + arity) != -1) {
args.push('$' + arity);
} else {
break;
}
}
args = args.join(',');
var func = `(${args}) => { ${body} };`;
#if DYLINK_DEBUG
dbg(`adding new EM_ASM constant at: ${ptrToString(start)}`);
#endif
{{{ makeEval('ASM_CONSTS[start] = eval(func)') }}};
}
// Add any EM_ASM function that exist in the side module
if ('__start_em_asm' in moduleExports) {
var start = moduleExports['__start_em_asm'];
var stop = moduleExports['__stop_em_asm'];
{{{ from64('start') }}}
{{{ from64('stop') }}}
while (start < stop) {
var jsString = UTF8ToString(start);
addEmAsm(start, jsString);
start = HEAPU8.indexOf(0, start) + 1;
}
}
function addEmJs(name, cSig, body) {
// The signature here is a C signature (e.g. "(int foo, char* bar)").
// See `create_em_js` in emcc.py` for the build-time version of this
// code.
var jsArgs = [];
cSig = cSig.slice(1, -1)
if (cSig != 'void') {
cSig = cSig.split(',');
for (var i in cSig) {
var jsArg = cSig[i].split(' ').pop();
jsArgs.push(jsArg.replace('*', ''));
}
}
var func = `(${jsArgs}) => ${body};`;
#if DYLINK_DEBUG
dbg(`adding new EM_JS function: ${jsArgs} = ${func}`);
#endif
{{{ makeEval('moduleExports[name] = eval(func)') }}};
}
for (var name in moduleExports) {
if (name.startsWith('__em_js__')) {
var start = moduleExports[name]
{{{ from64('start') }}}
var jsString = UTF8ToString(start);
// EM_JS strings are stored in the data section in the form
// SIG<::>BODY.
var parts = jsString.split('<::>');
addEmJs(name.replace('__em_js__', ''), parts[0], parts[1]);
delete moduleExports[name];
}
}
#endif
// initialize the module
#if PTHREADS
// Only one thread should call __wasm_call_ctors, but all threads need
// to call _emscripten_tls_init
registerTLSInit(moduleExports['_emscripten_tls_init'], instance.exports, metadata)
if (firstLoad) {
#endif
var applyRelocs = moduleExports['__wasm_apply_data_relocs'];
if (applyRelocs) {
if (runtimeInitialized) {
#if DYLINK_DEBUG
dbg('applyRelocs');
#endif
applyRelocs();
} else {
__RELOC_FUNCS__.push(applyRelocs);
}
}
var init = moduleExports['__wasm_call_ctors'];
if (init) {
if (runtimeInitialized) {
init();
} else {
// we aren't ready to run compiled code yet
__ATINIT__.push(init);
}
}
#if PTHREADS
}
#endif
return moduleExports;
}
if (flags.loadAsync) {
if (binary instanceof WebAssembly.Module) {
var instance = new WebAssembly.Instance(binary, info);
return Promise.resolve(postInstantiation(binary, instance));
}
return WebAssembly.instantiate(binary, info).then(
(result) => postInstantiation(result.module, result.instance)
);
}
var module = binary instanceof WebAssembly.Module ? binary : new WebAssembly.Module(binary);
var instance = new WebAssembly.Instance(module, info);
return postInstantiation(module, instance);
}
// now load needed libraries and the module itself.
if (flags.loadAsync) {
return metadata.neededDynlibs.reduce((chain, dynNeeded) => {
return chain.then(() => {
return loadDynamicLibrary(dynNeeded, flags);
});
}, Promise.resolve()).then(loadModule);
}
metadata.neededDynlibs.forEach((needed) => loadDynamicLibrary(needed, flags, localScope));
return loadModule();
},
#if STACK_OVERFLOW_CHECK >= 2
// Sometimes we load libraries before runtime initialization. In this case
// we delay calling __set_stack_limits (which must be called for each
// module).
$setDylinkStackLimits: function(stackTop, stackMax) {
for (var name in LDSO.loadedLibsByName) {
#if DYLINK_DEBUG
dbg(`setDylinkStackLimits for '${name}'`);
#endif
var lib = LDSO.loadedLibsByName[name];
if (lib.exports['__set_stack_limits']) {
lib.exports['__set_stack_limits']({{{ to64("stackTop") }}}, {{{ to64("stackMax") }}});
}
}
},
#endif
$newDSO: function(name, handle, syms) {
var dso = {
refcount: Infinity,
name,
exports: syms,
global: true,
};
LDSO.loadedLibsByName[name] = dso;
if (handle != undefined) {
LDSO.loadedLibsByHandle[handle] = dso;
}
return dso;
},
// loadDynamicLibrary loads dynamic library @ lib URL / path and returns
// handle for loaded DSO.
//
// Several flags affect the loading:
//
// - if flags.global=true, symbols from the loaded library are merged into global
// process namespace. Flags.global is thus similar to RTLD_GLOBAL in ELF.
//
// - if flags.nodelete=true, the library will be never unloaded. Flags.nodelete
// is thus similar to RTLD_NODELETE in ELF.
//
// - if flags.loadAsync=true, the loading is performed asynchronously and
// loadDynamicLibrary returns corresponding promise.
//
// If a library was already loaded, it is not loaded a second time. However
// flags.global and flags.nodelete are handled every time a load request is made.
// Once a library becomes "global" or "nodelete", it cannot be removed or unloaded.
$loadDynamicLibrary__deps: ['$LDSO', '$loadWebAssemblyModule',
'$isInternalSym', '$mergeLibSymbols', '$newDSO',
'$asyncLoad',
#if FILESYSTEM
'$preloadedWasm',
#endif
],
$loadDynamicLibrary__docs: `
/**
* @param {number=} handle
* @param {Object=} localScope
*/`,
$loadDynamicLibrary: function(libName, flags = {global: true, nodelete: true}, localScope, handle) {
#if DYLINK_DEBUG
dbg(`loadDynamicLibrary: ${libName} handle: ${handle}`);
dbg(`existing: ${Object.keys(LDSO.loadedLibsByName)}`);
#endif
// when loadDynamicLibrary did not have flags, libraries were loaded
// globally & permanently
var dso = LDSO.loadedLibsByName[libName];
if (dso) {
// the library is being loaded or has been loaded already.
//
// however it could be previously loaded only locally and if we get
// load request with global=true we have to make it globally visible now.
if (flags.global && !dso.global) {
dso.global = true;
if (dso.exports !== 'loading') {
// ^^^ if module is 'loading' - symbols merging will be eventually done by the loader.
mergeLibSymbols(dso.exports, libName)
}
}
// same for "nodelete"
if (flags.nodelete && dso.refcount !== Infinity) {
dso.refcount = Infinity;
}
dso.refcount++
if (handle) {
LDSO.loadedLibsByHandle[handle] = dso;
}
return flags.loadAsync ? Promise.resolve(true) : true;
}
// allocate new DSO
dso = newDSO(libName, handle, 'loading');
dso.refcount = flags.nodelete ? Infinity : 1;
dso.global = flags.global;
// libName -> libData
function loadLibData() {
#if PTHREADS
var sharedMod = sharedModules[libName];
#if DYLINK_DEBUG
dbg(`checking sharedModules: ${libName}: ${sharedMod ? 'found' : 'not found'}`);
#endif
if (sharedMod) {
return flags.loadAsync ? Promise.resolve(sharedMod) : sharedMod;
}
#endif
// for wasm, we can use fetch for async, but for fs mode we can only imitate it
if (handle) {
var data = {{{ makeGetValue('handle', C_STRUCTS.dso.file_data, '*') }}};