summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Clegg <sbc@chromium.org>2021-08-31 10:28:18 -0700
committerGitHub <noreply@github.com>2021-08-31 10:28:18 -0700
commit41fff9e2284e9be6e8a99da05beda54f398b0305 (patch)
treeef301fe3e18d562e36d5367f46bd205fd6716736
parent9bb771d06843390443f82877ad58d5a7108bb623 (diff)
downloadbinaryen-41fff9e2284e9be6e8a99da05beda54f398b0305.tar.gz
binaryen-41fff9e2284e9be6e8a99da05beda54f398b0305.tar.bz2
binaryen-41fff9e2284e9be6e8a99da05beda54f398b0305.zip
Handle extra info in dylink section (#4112)
If extra data is found in this section simply propagate it. Also, remove some dead code from wasm-binary.cpp.
-rwxr-xr-xscripts/binaryen-lit.in2
-rw-r--r--src/passes/Print.cpp4
-rw-r--r--src/wasm-binary.h7
-rw-r--r--src/wasm.h1
-rw-r--r--src/wasm/wasm-binary.cpp50
-rw-r--r--test/lit/binary/dylink.test15
-rwxr-xr-xtest/lit/binary/dylink.test.wasmbin0 -> 37 bytes
-rw-r--r--test/lit/lit.cfg.py2
8 files changed, 38 insertions, 43 deletions
diff --git a/scripts/binaryen-lit.in b/scripts/binaryen-lit.in
index f26540bc3..821757082 100755
--- a/scripts/binaryen-lit.in
+++ b/scripts/binaryen-lit.in
@@ -27,7 +27,5 @@ builtin_parameters = {
}
}
-print(builtin_parameters)
-
if __name__ == '__main__':
main(builtin_parameters)
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp
index 3b072a469..4fa38442c 100644
--- a/src/passes/Print.cpp
+++ b/src/passes/Print.cpp
@@ -2891,6 +2891,10 @@ struct PrintSExpression : public UnifiedExpressionVisitor<PrintSExpression> {
for (auto& neededDynlib : dylinkSection->neededDynlibs) {
doIndent(o, indent) << ";; needed dynlib: " << neededDynlib << '\n';
}
+ if (dylinkSection->tail.size()) {
+ doIndent(o, indent) << ";; extra dylink data, size "
+ << dylinkSection->tail.size() << "\n";
+ }
}
void visitModule(Module* curr) {
currModule = curr;
diff --git a/src/wasm-binary.h b/src/wasm-binary.h
index 6938214d5..260bacecf 100644
--- a/src/wasm-binary.h
+++ b/src/wasm-binary.h
@@ -1242,6 +1242,7 @@ public:
void writeInlineString(const char* name);
void writeEscapedName(const char* name);
void writeInlineBuffer(const char* data, size_t size);
+ void writeData(const char* data, size_t size);
struct Buffer {
const char* data;
@@ -1251,12 +1252,6 @@ public:
: data(data), size(size), pointerLocation(pointerLocation) {}
};
- std::vector<Buffer> buffersToWrite;
-
- void emitBuffer(const char* data, size_t size);
- void emitString(const char* str);
- void finishUp();
-
Module* getModule() { return wasm; }
void writeType(Type type);
diff --git a/src/wasm.h b/src/wasm.h
index 00de04623..b6e57525f 100644
--- a/src/wasm.h
+++ b/src/wasm.h
@@ -1825,6 +1825,7 @@ class DylinkSection {
public:
Index memorySize, memoryAlignment, tableSize, tableAlignment;
std::vector<Name> neededDynlibs;
+ std::vector<char> tail;
};
class Module {
diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp
index ae0e24759..ab229f111 100644
--- a/src/wasm/wasm-binary.cpp
+++ b/src/wasm/wasm-binary.cpp
@@ -84,8 +84,6 @@ void WasmBinaryWriter::write() {
writeLateUserSections();
writeFeaturesSection();
-
- finishUp();
}
void WasmBinaryWriter::writeHeader() {
@@ -1087,6 +1085,9 @@ void WasmBinaryWriter::writeDylinkSection() {
for (auto& neededDynlib : wasm->dylinkSection->neededDynlibs) {
writeInlineString(neededDynlib.c_str());
}
+
+ writeData(wasm->dylinkSection->tail.data(), wasm->dylinkSection->tail.size());
+
finishSection(start);
}
@@ -1131,12 +1132,16 @@ void WasmBinaryWriter::writeExtraDebugLocation(Expression* curr,
}
}
+void WasmBinaryWriter::writeData(const char* data, size_t size) {
+ for (size_t i = 0; i < size; i++) {
+ o << int8_t(data[i]);
+ }
+}
+
void WasmBinaryWriter::writeInlineString(const char* name) {
int32_t size = strlen(name);
o << U32LEB(size);
- for (int32_t i = 0; i < size; i++) {
- o << int8_t(name[i]);
- }
+ writeData(name, size);
}
static bool isHexDigit(char ch) {
@@ -1173,36 +1178,7 @@ void WasmBinaryWriter::writeEscapedName(const char* name) {
void WasmBinaryWriter::writeInlineBuffer(const char* data, size_t size) {
o << U32LEB(size);
- for (size_t i = 0; i < size; i++) {
- o << int8_t(data[i]);
- }
-}
-
-void WasmBinaryWriter::emitBuffer(const char* data, size_t size) {
- assert(size > 0);
- buffersToWrite.emplace_back(data, size, o.size());
- // placeholder, we'll fill in the pointer to the buffer later when we have it
- o << uint32_t(0);
-}
-
-void WasmBinaryWriter::emitString(const char* str) {
- BYN_TRACE("emitString " << str << std::endl);
- emitBuffer(str, strlen(str) + 1);
-}
-
-void WasmBinaryWriter::finishUp() {
- BYN_TRACE("finishUp\n");
- // finish buffers
- for (const auto& buffer : buffersToWrite) {
- BYN_TRACE("writing buffer"
- << (int)buffer.data[0] << "," << (int)buffer.data[1] << " at "
- << o.size() << " and pointer is at " << buffer.pointerLocation
- << "\n");
- o.writeAt(buffer.pointerLocation, (uint32_t)o.size());
- for (size_t i = 0; i < buffer.size; i++) {
- o << (uint8_t)buffer.data[i];
- }
- }
+ writeData(data, size);
}
void WasmBinaryWriter::writeType(Type type) {
@@ -3289,6 +3265,10 @@ void WasmBinaryBuilder::readDylink(size_t payloadLen) {
wasm.dylinkSection->neededDynlibs.push_back(getInlineString());
}
+ size_t remaining = (sectionPos + payloadLen) - pos;
+ auto tail = getByteView(remaining);
+ wasm.dylinkSection->tail = {tail.first, tail.second};
+
if (pos != sectionPos + payloadLen) {
throwError("bad features section size");
}
diff --git a/test/lit/binary/dylink.test b/test/lit/binary/dylink.test
new file mode 100644
index 000000000..72341c9ba
--- /dev/null
+++ b/test/lit/binary/dylink.test
@@ -0,0 +1,15 @@
+# Verify that extra bytes in the dylink section are preserved.
+#
+# `dylink.test.wasm` was generated using emscripten to build a side
+# module with two TLS exports (which adds extra information to the
+# dylink section) using:
+# `emcc -s SIDE_MODULE side.c`
+#
+# side.c:
+# _Thread_local int foo = 10;
+# _Thread_local int bar = 11;
+#
+# and then removing all sections except the dylink section using:
+# `llvm-objcopy --only-section=dylink`
+RUN: wasm-opt -O1 %s.wasm -o %t.o
+RUN: cmp %s.wasm %t.o
diff --git a/test/lit/binary/dylink.test.wasm b/test/lit/binary/dylink.test.wasm
new file mode 100755
index 000000000..401028d96
--- /dev/null
+++ b/test/lit/binary/dylink.test.wasm
Binary files differ
diff --git a/test/lit/lit.cfg.py b/test/lit/lit.cfg.py
index 23b0c3de6..ac89a5991 100644
--- a/test/lit/lit.cfg.py
+++ b/test/lit/lit.cfg.py
@@ -10,6 +10,8 @@ config.test_exec_root = os.path.join(config.binaryen_build_root, 'test')
# Replace all Binaryen tools with their absolute paths
bin_dir = os.path.join(config.binaryen_build_root, 'bin')
+assert(os.path.isdir(bin_dir))
+
for tool_file in os.listdir(bin_dir):
tool_path = config.binaryen_build_root + '/bin/' + tool_file
tool = tool_file[:-4] if tool_file.endswith('.exe') else tool_file