File tree 6 files changed +32
-11
lines changed
6 files changed +32
-11
lines changed Original file line number Diff line number Diff line change 40
40
# CHECK-NEXT: - Name: __heap_base
41
41
# CHECK-NEXT: Kind: GLOBAL
42
42
# CHECK-NEXT: Index: 4
43
- # CHECK-NEXT: - Name: __memory_base
43
+ # CHECK-NEXT: - Name: __heap_end
44
44
# CHECK-NEXT: Kind: GLOBAL
45
45
# CHECK-NEXT: Index: 5
46
- # CHECK-NEXT: - Name: __table_base
46
+ # CHECK-NEXT: - Name: __memory_base
47
47
# CHECK-NEXT: Kind: GLOBAL
48
48
# CHECK-NEXT: Index: 6
49
+ # CHECK-NEXT: - Name: __table_base
50
+ # CHECK-NEXT: Kind: GLOBAL
51
+ # CHECK-NEXT: Index: 7
Original file line number Diff line number Diff line change @@ -79,10 +79,13 @@ _start:
79
79
# CHECK-ALL-NEXT: - Name: __heap_base
80
80
# CHECK-ALL-NEXT: Kind: GLOBAL
81
81
# CHECK-ALL-NEXT: Index: 5
82
- # CHECK-ALL-NEXT: - Name: __memory_base
82
+ # CHECK-ALL-NEXT: - Name: __heap_end
83
83
# CHECK-ALL-NEXT: Kind: GLOBAL
84
84
# CHECK-ALL-NEXT: Index: 6
85
- # CHECK-ALL-NEXT: - Name: __table_base
85
+ # CHECK-ALL-NEXT: - Name: __memory_base
86
86
# CHECK-ALL-NEXT: Kind: GLOBAL
87
87
# CHECK-ALL-NEXT: Index: 7
88
+ # CHECK-ALL-NEXT: - Name: __table_base
89
+ # CHECK-ALL-NEXT: Kind: GLOBAL
90
+ # CHECK-ALL-NEXT: Index: 8
88
91
# CHECK-ALL-NEXT: - Type: CODE
Original file line number Diff line number Diff line change @@ -681,6 +681,7 @@ static void createOptionalSymbols() {
681
681
if (!config->isPic ) {
682
682
WasmSym::globalBase = symtab->addOptionalDataSymbol (" __global_base" );
683
683
WasmSym::heapBase = symtab->addOptionalDataSymbol (" __heap_base" );
684
+ WasmSym::heapEnd = symtab->addOptionalDataSymbol (" __heap_end" );
684
685
WasmSym::definedMemoryBase = symtab->addOptionalDataSymbol (" __memory_base" );
685
686
WasmSym::definedTableBase = symtab->addOptionalDataSymbol (" __table_base" );
686
687
if (config->is64 .value_or (false ))
Original file line number Diff line number Diff line change @@ -83,6 +83,7 @@ DefinedData *WasmSym::dsoHandle;
83
83
DefinedData *WasmSym::dataEnd;
84
84
DefinedData *WasmSym::globalBase;
85
85
DefinedData *WasmSym::heapBase;
86
+ DefinedData *WasmSym::heapEnd;
86
87
DefinedData *WasmSym::initMemoryFlag;
87
88
GlobalSymbol *WasmSym::stackPointer;
88
89
GlobalSymbol *WasmSym::tlsBase;
Original file line number Diff line number Diff line change @@ -538,11 +538,14 @@ struct WasmSym {
538
538
// Symbol marking the end of the data and bss.
539
539
static DefinedData *dataEnd;
540
540
541
- // __heap_base
542
- // Symbol marking the end of the data, bss and explicit stack. Any linear
543
- // memory following this address is not used by the linked code and can
544
- // therefore be used as a backing store for brk()/malloc() implementations.
541
+ // __heap_base/__heap_end
542
+ // Symbols marking the beginning and end of the "heap". It starts at the end
543
+ // of the data, bss and explicit stack, and extends to the end of the linear
544
+ // memory allocated by wasm-ld. This region of memory is not used by the
545
+ // linked code, so it may be used as a backing store for `sbrk` or `malloc`
546
+ // implementations.
545
547
static DefinedData *heapBase;
548
+ static DefinedData *heapEnd;
546
549
547
550
// __wasm_init_memory_flag
548
551
// Symbol whose contents are nonzero iff memory has already been initialized.
Original file line number Diff line number Diff line change @@ -340,10 +340,20 @@ void Writer::layoutMemory() {
340
340
Twine (maxMemorySetting));
341
341
memoryPtr = config->initialMemory ;
342
342
}
343
- out.memorySec ->numMemoryPages =
344
- alignTo (memoryPtr, WasmPageSize) / WasmPageSize;
343
+
344
+ memoryPtr = alignTo (memoryPtr, WasmPageSize);
345
+
346
+ out.memorySec ->numMemoryPages = memoryPtr / WasmPageSize;
345
347
log (" mem: total pages = " + Twine (out.memorySec ->numMemoryPages ));
346
348
349
+ if (WasmSym::heapEnd) {
350
+ // Set `__heap_end` to follow the end of the statically allocated linear
351
+ // memory. The fact that this comes last means that a malloc/brk
352
+ // implementation can grow the heap at runtime.
353
+ log (" mem: heap end = " + Twine (memoryPtr));
354
+ WasmSym::heapEnd->setVA (memoryPtr);
355
+ }
356
+
347
357
if (config->maxMemory != 0 ) {
348
358
if (config->maxMemory != alignTo (config->maxMemory , WasmPageSize))
349
359
error (" maximum memory must be " + Twine (WasmPageSize) + " -byte aligned" );
@@ -363,7 +373,7 @@ void Writer::layoutMemory() {
363
373
if (config->isPic )
364
374
max = maxMemorySetting;
365
375
else
366
- max = alignTo ( memoryPtr, WasmPageSize) ;
376
+ max = memoryPtr;
367
377
}
368
378
out.memorySec ->maxMemoryPages = max / WasmPageSize;
369
379
log (" mem: max pages = " + Twine (out.memorySec ->maxMemoryPages ));
You can’t perform that action at this time.
0 commit comments