<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - [WebAssembly] imports not de-duplicated between different languages"
   href="https://bugs.llvm.org/show_bug.cgi?id=50938">50938</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[WebAssembly] imports not de-duplicated between different languages
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>lld
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>wasm
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>fitzgen@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, sbc@chromium.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>## Steps to Reproduce

* Ensure you have Rust toolchain that can target `wasm32-wasi`:

  ```
  $ rustup target add wasm32-wasi
  ```

* Ensure you have a C toolchain that can target `wasm32-unknown-wasi`. Multiple
ways to do this, easiest is via
[wasi-sdk](<a href="https://github.com/WebAssembly/wasi-sdk">https://github.com/WebAssembly/wasi-sdk</a>).

* Clone <a href="https://github.com/fitzgen/lld-dupe-import">https://github.com/fitzgen/lld-dupe-import</a> and `cd` into it.

* Build:

  ```
  $ CC=path/to/wasi-sdk/bin/clang \
    SYSROOT=path/to/wasi-sdk/share/wasi-sysroot \
    RANLIB=path/to/wasi-sdk/bin/ranlib \
    cargo build --target wasm32-wasi
  ```

* Inspect the built `.wasm` file's imports:

  ```
  $ wasm-objdump -x -j Import target/wasm32-wasi/debug/lld_dupe_import.wasm
  ```

## Expected Results

`"wasi_snapshot_preview1" "fd_write"` is imported once.

## Actual Results

`"wasi_snapshot_preview1" "fd_write"` is imported twice (functions 0 and 5):

```
lld_dupe_import.wasm:   file format wasm 0x1

Section Details:

Import[8]:
 - func[0] sig=7
<_ZN4wasi13lib_generated22wasi_snapshot_preview18fd_write17h133cd9fedd86597bE>
<- wasi_snapshot_preview1.fd_write
 - func[1] sig=0 <__wasi_proc_exit> <- wasi_snapshot_preview1.proc_exit
 - func[2] sig=3 <__wasi_environ_sizes_get> <-
wasi_snapshot_preview1.environ_sizes_get
 - func[3] sig=3 <__wasi_environ_get> <- wasi_snapshot_preview1.environ_get
 - func[4] sig=8 <__wasi_fd_seek> <- wasi_snapshot_preview1.fd_seek
 - func[5] sig=7 <__wasi_fd_write> <- wasi_snapshot_preview1.fd_write
 - func[6] sig=9 <__wasi_fd_close> <- wasi_snapshot_preview1.fd_close
 - func[7] sig=3 <__wasi_fd_fdstat_get> <- wasi_snapshot_preview1.fd_fdstat_get
```

## Additional Information

This test case links C into a Rust project. I've also reproduced this by
linking Rust into a C project. I assume other languages linked together with
`wasm-ld` would have the same issue?

I can't find any `wasm-ld` CLI flags that seem to control this.
Enabling/disabling LTO and GC sections, for example, doesn't make a difference.

I assume that this is not specific to WASI imports, but I haven't tried
reproducing without WASI.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>