Skip to content

Commit ab35a43

Browse files
authored
wasm-emscripten-finalize: separateDataSegments() fix (#1897)
We should emit a file with only the data segments, starting from the global base, and not starting from zero (the data before is unneeded, and the emscripten loading code assumes it isn't there). Also fix the auto updater to work properly on .mem test updating.
1 parent e2e0c27 commit ab35a43

6 files changed

+11
-4
lines changed

auto_update_tests.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def update_lld_tests():
8080
extension_arg_map = {
8181
'.out': [],
8282
'.jscall.out': ['--emscripten-reserved-function-pointers=3'],
83-
'.mem.out': ['--separate-data-segments', mem_file],
83+
'.mem.out': ['--separate-data-segments', mem_file + '.mem'],
8484
}
8585
for ext, ext_args in extension_arg_map.items():
8686
out_path = wast_path + ext

src/tools/wasm-emscripten-finalize.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,10 @@ int main(int argc, const char *argv[]) {
224224
// for metadata).
225225
if (!dataSegmentFile.empty()) {
226226
Output memInitFile(dataSegmentFile, Flags::Binary, Flags::Release);
227-
generator.separateDataSegments(&memInitFile);
227+
if (globalBase == INVALID_BASE) {
228+
Fatal() << "globalBase must be set";
229+
}
230+
generator.separateDataSegments(&memInitFile, globalBase);
228231
}
229232

230233
if (options.debug) {

src/wasm-emscripten.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ class EmscriptenGlueGenerator {
5757

5858
void fixInvokeFunctionNames();
5959

60-
void separateDataSegments(Output* outfile);
60+
// Emits the data segments to a file. The file contains data from address base
61+
// onwards (we must pass in base, as we can't tell it from the wasm - the first
62+
// segment may start after a run of zeros, but we need those zeros in the file).
63+
void separateDataSegments(Output* outfile, Address base);
6164

6265
private:
6366
Module& wasm;

src/wasm/wasm-emscripten.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -919,10 +919,11 @@ std::string EmscriptenGlueGenerator::generateEmscriptenMetadata(
919919
return meta.str();
920920
}
921921

922-
void EmscriptenGlueGenerator::separateDataSegments(Output* outfile) {
922+
void EmscriptenGlueGenerator::separateDataSegments(Output* outfile, Address base) {
923923
size_t lastEnd = 0;
924924
for (Memory::Segment& seg : wasm.memory.segments) {
925925
size_t offset = seg.offset->cast<Const>()->value.geti32();
926+
offset -= base;
926927
size_t fill = offset - lastEnd;
927928
if (fill > 0) {
928929
std::vector<char> buf(fill);

test/lld/em_asm.wast.mem.mem

-568 Bytes
Binary file not shown.

test/lld/hello_world.wast.mem.mem

-568 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)