@@ -123,13 +123,28 @@ struct Mergeable {
123123 memoryBaseGlobals.insert (name);
124124 });
125125 if (memoryBaseGlobals.size () == 0 ) {
126- Fatal () << " no memory base was imported" ;
126+ // add one
127+ auto * import = new Import;
128+ import ->name = MEMORY_BASE;
129+ import ->module = ENV;
130+ import ->base = MEMORY_BASE;
131+ import ->kind = ExternalKind::Global;
132+ import ->globalType = i32 ;
133+ wasm.addImport (import );
134+ memoryBaseGlobals.insert (import ->name );
127135 }
128136 findImportsByBase (wasm, TABLE_BASE, [&](Name name) {
129137 tableBaseGlobals.insert (name);
130138 });
131139 if (tableBaseGlobals.size () == 0 ) {
132- Fatal () << " no table base was imported" ;
140+ auto * import = new Import;
141+ import ->name = TABLE_BASE;
142+ import ->module = ENV;
143+ import ->base = TABLE_BASE;
144+ import ->kind = ExternalKind::Global;
145+ import ->globalType = i32 ;
146+ wasm.addImport (import );
147+ tableBaseGlobals.insert (import ->name );
133148 }
134149 }
135150
@@ -169,7 +184,8 @@ struct Mergeable {
169184 // ensure a relocatable segment exists, of the proper size, including
170185 // the dylink bump applied into it, standardized into the form of
171186 // not using a dylink section and instead having enough zeros at
172- // the end. this makes linking much simpler.
187+ // the end. this makes linking much simpler.ta
188+ // there may be other non-relocatable segments too.
173189 template <typename T, typename U, typename Segment>
174190 void standardizeSegment (Module& wasm, T& what, Index size, U zero, Name globalName) {
175191 Segment* relocatable = nullptr ;
@@ -194,9 +210,10 @@ struct Mergeable {
194210 ensureSize (what, relocatable->data .size ());
195211 }
196212
197- // copies a relocatable segment from the input to the output
213+ // copies a relocatable segment from the input to the output, and
214+ // copies the non-relocatable ones as well
198215 template <typename T, typename V>
199- void copySegment (T& output, T& input, V updater) {
216+ void copySegments (T& output, T& input, V updater) {
200217 for (auto & inputSegment : input.segments ) {
201218 Expression* inputOffset = inputSegment.offset ;
202219 if (inputOffset->is <GetGlobal>()) {
@@ -213,6 +230,9 @@ struct Mergeable {
213230 }
214231 }
215232 WASM_UNREACHABLE (); // we must find a relocatable one in the output, as we standardized
233+ } else {
234+ // this is a non-relocatable one. just copy it.
235+ output.segments .push_back (inputSegment);
216236 }
217237 }
218238 }
@@ -405,8 +425,8 @@ struct InputMergeable : public ExpressionStackWalker<InputMergeable, Visitor<Inp
405425 }
406426
407427 // memory&table: we place the new memory segments at a higher position. after the existing ones.
408- copySegment (outputMergeable.wasm .memory , wasm.memory , [](char x) -> char { return x; });
409- copySegment (outputMergeable.wasm .table , wasm.table , [&](Name x) -> Name { return fNames [x]; });
428+ copySegments (outputMergeable.wasm .memory , wasm.memory , [](char x) -> char { return x; });
429+ copySegments (outputMergeable.wasm .table , wasm.table , [&](Name x) -> Name { return fNames [x]; });
410430
411431 // update the new contents about to be merged in
412432 walkModule (&wasm);
0 commit comments