summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xcheck.py16
-rw-r--r--src/tools/wasm-merge.cpp2
-rw-r--r--src/wasm-binary.h6
-rw-r--r--src/wasm/wasm-binary.cpp33
-rw-r--r--src/wasm/wasm.cpp2
5 files changed, 50 insertions, 9 deletions
diff --git a/check.py b/check.py
index 907c14b16..630876858 100755
--- a/check.py
+++ b/check.py
@@ -239,6 +239,21 @@ def run_crash_tests():
run_command(cmd, expected_err='parse exception:', err_contains=True, expected_status=1)
+def run_dylink_tests():
+ print "\n[ we emit dylink sections properly... ]\n"
+
+ for t in os.listdir(options.binaryen_test):
+ if t.startswith('dylib') and t.endswith('.wasm'):
+ print '..', t
+ t = os.path.join(options.binaryen_test, t)
+ cmd = WASM_OPT + [t, '-o', 'a.wasm']
+ run_command(cmd)
+ with open('a.wasm') as output:
+ index = output.read().find('dylink')
+ print ' ', index
+ assert index == 11, 'dylink section must be first, right after the magic number etc.'
+
+
def run_ctor_eval_tests():
print '\n[ checking wasm-ctor-eval... ]\n'
@@ -640,6 +655,7 @@ def main():
run_wasm_dis_tests()
run_wasm_merge_tests()
run_crash_tests()
+ run_dylink_tests()
run_ctor_eval_tests()
run_wasm_metadce_tests()
if has_shell_timeout():
diff --git a/src/tools/wasm-merge.cpp b/src/tools/wasm-merge.cpp
index 6c7bd46af..e8910860e 100644
--- a/src/tools/wasm-merge.cpp
+++ b/src/tools/wasm-merge.cpp
@@ -106,7 +106,7 @@ struct Mergeable {
}
}
for (auto& section : wasm.userSections) {
- if (section.name == "dylink") {
+ if (section.name == BinaryConsts::UserSections::Dylink) {
WasmBinaryBuilder builder(wasm, section.data, false);
totalMemorySize = std::max(totalMemorySize, builder.getU32LEB());
totalTableSize = std::max(totalTableSize, builder.getU32LEB());
diff --git a/src/wasm-binary.h b/src/wasm-binary.h
index 487979b3a..a1155f684 100644
--- a/src/wasm-binary.h
+++ b/src/wasm-binary.h
@@ -340,6 +340,8 @@ namespace UserSections {
extern const char* Name;
extern const char* SourceMapUrl;
+extern const char* Dylink;
+
enum Subsection {
NameFunction = 1,
NameLocal = 2,
@@ -713,7 +715,9 @@ public:
void writeNames();
void writeSourceMapUrl();
void writeSymbolMap();
- void writeUserSections();
+ void writeEarlyUserSections();
+ void writeLateUserSections();
+ void writeUserSection(const UserSection& section);
void writeSourceMapProlog();
void writeSourceMapEpilog();
diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp
index c6d1a6a60..a7bc9c91e 100644
--- a/src/wasm/wasm-binary.cpp
+++ b/src/wasm/wasm-binary.cpp
@@ -39,6 +39,9 @@ void WasmBinaryWriter::prepare() {
void WasmBinaryWriter::write() {
writeHeader();
+
+ writeEarlyUserSections();
+
if (sourceMap) {
writeSourceMapProlog();
}
@@ -62,7 +65,7 @@ void WasmBinaryWriter::write() {
writeSourceMapEpilog();
}
- writeUserSections();
+ writeLateUserSections();
finishUp();
}
@@ -535,17 +538,33 @@ void WasmBinaryWriter::writeSourceMapEpilog() {
*sourceMap << "\"}";
}
-void WasmBinaryWriter::writeUserSections() {
+void WasmBinaryWriter::writeEarlyUserSections() {
+ // The dylink section must be the first in the module, per
+ // the spec, to allow simple parsing by loaders.
for (auto& section : wasm->userSections) {
- auto start = startSection(0);
- writeInlineString(section.name.c_str());
- for (size_t i = 0; i < section.data.size(); i++) {
- o << uint8_t(section.data[i]);
+ if (section.name == BinaryConsts::UserSections::Dylink) {
+ writeUserSection(section);
}
- finishSection(start);
}
}
+void WasmBinaryWriter::writeLateUserSections() {
+ for (auto& section : wasm->userSections) {
+ if (section.name != BinaryConsts::UserSections::Dylink) {
+ writeUserSection(section);
+ }
+ }
+}
+
+void WasmBinaryWriter::writeUserSection(const UserSection& section) {
+ auto start = startSection(0);
+ writeInlineString(section.name.c_str());
+ for (size_t i = 0; i < section.data.size(); i++) {
+ o << uint8_t(section.data[i]);
+ }
+ finishSection(start);
+}
+
void WasmBinaryWriter::writeDebugLocation(Expression* curr, Function* func) {
auto& debugLocations = func->debugLocations;
auto iter = debugLocations.find(curr);
diff --git a/src/wasm/wasm.cpp b/src/wasm/wasm.cpp
index d88ab0fcd..433803772 100644
--- a/src/wasm/wasm.cpp
+++ b/src/wasm/wasm.cpp
@@ -29,6 +29,8 @@ namespace BinaryConsts {
namespace UserSections {
const char* Name = "name";
const char* SourceMapUrl = "sourceMappingURL";
+
+const char* Dylink = "dylink";
}
}