diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index d24f7da423f..9cc55af0522 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -1373,9 +1373,19 @@ void WasmBinaryWriter::writeSourceMapEpilog() { void WasmBinaryWriter::writeLateCustomSections() { for (auto& section : wasm->customSections) { - if (section.name != BinaryConsts::CustomSections::Dylink) { - writeCustomSection(section); + if (section.name == BinaryConsts::CustomSections::Dylink) { + // This is an early custom section. + continue; } + + if (section.name == BinaryConsts::CustomSections::SourceMapUrl && + sourceMap && !sourceMapUrl.empty()) { + // We are writing a SourceMapURL manually, following the user's request. + // Do not emit the existing custom section as a second one. + continue; + } + + writeCustomSection(section); } } diff --git a/test/lit/debug/source-map-url.wast b/test/lit/debug/source-map-url.wast new file mode 100644 index 00000000000..906ae8efb61 --- /dev/null +++ b/test/lit/debug/source-map-url.wast @@ -0,0 +1,37 @@ +(module + (func $test (param i32) (result i32) + ;;@ waka:100:1 + (i32.const 42) + ) +) + +;; 1. Generate a binary with a source map and url. +;; +;; RUN: wasm-opt %s -g -o %t.wasm -osm %t.wasm.map -osu=one +;; RUN: wasm-dis %t.wasm | filecheck %s --check-prefix=ONE + +;; ONE: ;; custom section "sourceMappingURL", size 4 + + +;; 2. Round-trip that binary, again using -osu. This should not end up with two +;; sourceMappingURL sections (one could arrive from the flag, and one from the +;; existing custom section, if we copy it). Note the length increases, from +;; "one\0" (4) to "swap\0" (5), as we swap the URL. +;; +;; RUN: wasm-opt %t.wasm -o %t.wasm2 -ism %t.wasm.map -osm %t.wasm.map2 -osu=swap +;; RUN: wasm-dis %t.wasm2 | filecheck %s --check-prefix=SWAP + +;; Look for a wrong section both before and after the correct one. + +;; SWAP-NOT: ;; custom section "sourceMappingURL", size 4 +;; SWAP: ;; custom section "sourceMappingURL", size 5 +;; SWAP-NOT: ;; custom section "sourceMappingURL" + + +;; 3. Round-trip without -osu. Now we just copy the old URL. +;; +;; RUN: wasm-opt %t.wasm2 -o %t.wasm3 -ism %t.wasm.map2 -osm %t.wasm.map3 +;; RUN: wasm-dis %t.wasm3 | filecheck %s --check-prefix=ROUND + +;; ROUND: ;; custom section "sourceMappingURL", size 5 +