@@ -342,8 +342,10 @@ assert(DYNAMIC_BASE % 16 === 0, 'heap must start aligned');
342342
343343#if RELOCATABLE
344344// We support some amount of allocation during startup in the case of
345- // dynamic linking, which needs to allocate memory for the dynamic library
346- // during startup (before the main program initialized).
345+ // dynamic linking, which needs to allocate memory for dynamic libraries that
346+ // are loaded. That has to happen before the main program can start to run,
347+ // because the main program needs those linked in before it runs (so we can't
348+ // use normally malloc from the main program to do these allocations).
347349
348350// Allocate memory no even if malloc isn't ready yet.
349351function getMemory ( size ) {
@@ -353,7 +355,9 @@ function getMemory(size) {
353355
354356// To support such allocations during startup, track them on __heap_base and
355357// then when the main module is loaded it reads that value and uses it to
356- // initialize sbrk.
358+ // initialize sbrk (the main module is relocatable itself, and so it does not
359+ // have __heap_base hardcoded into it - it receives it from JS as an extern
360+ // global, basically).
357361Module [ '___heap_base' ] = DYNAMIC_BASE ;
358362
359363function dynamicAlloc ( size ) {
@@ -374,7 +378,6 @@ function dynamicAlloc(size) {
374378
375379#if USE_PTHREADS
376380if ( ENVIRONMENT_IS_PTHREAD ) {
377-
378381 // At the 'load' stage of Worker startup, we are just loading this script
379382 // but not ready to run yet. At 'run' we receive proper values for the stack
380383 // etc. and can launch a pthread. Set some fake values there meanwhile to
0 commit comments