summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/binary-reader.cc21
-rw-r--r--src/tools/wasm-link.cc14
2 files changed, 34 insertions, 1 deletions
diff --git a/src/binary-reader.cc b/src/binary-reader.cc
index 48e0fbed..7c9e4d4b 100644
--- a/src/binary-reader.cc
+++ b/src/binary-reader.cc
@@ -1021,11 +1021,19 @@ Result BinaryReader::ReadNamesSection(Offset section_size) {
Index num_names;
CHECK_RESULT(ReadIndex(&num_names, "name count"));
CALLBACK(OnFunctionNamesCount, num_names);
+ Index last_function_index = kInvalidIndex;
+
for (Index j = 0; j < num_names; ++j) {
Index function_index;
StringSlice function_name;
CHECK_RESULT(ReadIndex(&function_index, "function index"));
+ ERROR_UNLESS(function_index != last_function_index,
+ "duplicate function name: %u", function_index);
+ ERROR_UNLESS(last_function_index == kInvalidIndex ||
+ function_index > last_function_index,
+ "function index out of order: %u", function_index);
+ last_function_index = function_index;
ERROR_UNLESS(function_index < NumTotalFuncs(),
"invalid function index: %" PRIindex, function_index);
CHECK_RESULT(ReadStr(&function_name, "function name"));
@@ -1039,19 +1047,32 @@ Result BinaryReader::ReadNamesSection(Offset section_size) {
Index num_funcs;
CHECK_RESULT(ReadIndex(&num_funcs, "function count"));
CALLBACK(OnLocalNameFunctionCount, num_funcs);
+ Index last_function_index = kInvalidIndex;
for (Index j = 0; j < num_funcs; ++j) {
Index function_index;
CHECK_RESULT(ReadIndex(&function_index, "function index"));
ERROR_UNLESS(function_index < NumTotalFuncs(),
"invalid function index: %u", function_index);
+ ERROR_UNLESS(last_function_index == kInvalidIndex ||
+ function_index > last_function_index,
+ "locals function index out of order: %u",
+ function_index);
+ last_function_index = function_index;
Index num_locals;
CHECK_RESULT(ReadIndex(&num_locals, "local count"));
CALLBACK(OnLocalNameLocalCount, function_index, num_locals);
+ Index last_local_index = kInvalidIndex;
for (Index k = 0; k < num_locals; ++k) {
Index local_index;
StringSlice local_name;
CHECK_RESULT(ReadIndex(&local_index, "named index"));
+ ERROR_UNLESS(local_index != last_local_index,
+ "duplicate local index: %u", local_index);
+ ERROR_UNLESS(last_local_index == kInvalidIndex ||
+ local_index > last_local_index,
+ "local index out of order: %u", local_index);
+ last_local_index = local_index;
CHECK_RESULT(ReadStr(&local_name, "name"));
CALLBACK(OnLocalName, function_index, local_index, local_name);
}
diff --git a/src/tools/wasm-link.cc b/src/tools/wasm-link.cc
index b69f2b5a..240813c1 100644
--- a/src/tools/wasm-link.cc
+++ b/src/tools/wasm-link.cc
@@ -523,9 +523,11 @@ static void write_names_section(Context* ctx) {
stream->WriteU8Enum(NameSectionSubsection::Function, "subsection code");
WRITE_UNKNOWN_SIZE(stream);
write_u32_leb128(stream, total_count, "element count");
+
+ // Write import names
for (const std::unique_ptr<LinkerInputBinary>& binary: ctx->inputs) {
for (size_t i = 0; i < binary->debug_names.size(); i++) {
- if (binary->debug_names[i].empty())
+ if (binary->debug_names[i].empty() || !binary->IsFunctionImport(i))
continue;
if (binary->IsInactiveFunctionImport(i))
continue;
@@ -534,6 +536,16 @@ static void write_names_section(Context* ctx) {
}
}
+ // Write non-import names
+ for (const std::unique_ptr<LinkerInputBinary>& binary: ctx->inputs) {
+ for (size_t i = 0; i < binary->debug_names.size(); i++) {
+ if (binary->debug_names[i].empty() || binary->IsFunctionImport(i))
+ continue;
+ write_u32_leb128(stream, binary->RelocateFuncIndex(i), "function index");
+ write_string(stream, binary->debug_names[i], "function name");
+ }
+ }
+
FIXUP_SIZE(stream);
FIXUP_SIZE(stream);