<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/54363>54363</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            DataCount section not emitted by wasm-lld when only `bulk-memory` is enabled
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          ThePuzzlemaker
      </td>
    </tr>
</table>

<pre>
    I have been doing some cursed shenaniganry with WAT and I needed to use a passive data section with wasm-lld. However, I was not using `--shared-memory` and I found that the `DataCount` section was not emitted, and thus my program did not work at all.

This appears to have been the behaviour in the past, but as of 09768c5, the code that determined whether or not this section should be emitted now works based on if shared memory is enabled. However, DataCount is not exclusive to shared memory:

> To make the memory.init and data.drop instructions valid, a [DataCount section](https://github.com/WebAssembly/bulk-memory-operations/blob/master/proposals/bulk-memory-operations/Overview.md#datacount-section) will also be emitted.

> Note that memory.init and the DataCount section are features of the bulk-memory proposal, not the atomics proposal, so any engine that supports threads needs to support both of these proposals.

([Source](https://github.com/WebAssembly/tool-conventions/blob/main/Linking.md#shared-memory-and-passive-segments))

And as described in the [Bulk Memory proposal](https://github.com/WebAssembly/bulk-memory-operations/blob/master/proposals/bulk-memory-operations/Overview.md#datacount-section), this is required for the use of memory.init:

> The WebAssembly binary format is designed to be validated in a single pass. If a section requires information to validate, it is guaranteed that this information will be present in a previous section.

> The memory.{init,drop} instructions break this guarantee. Both of these instructions are used in the Code section. They each have a data segment index immediate, but the vector of data segments is not available until the Data section is parsed, which occurs after the Code section.

> To keep single-pass validation, the number of data segments defined in the Data section must be available before the Code section. This information is provided in a new DataCount section with the code 12.

Thus it makes sense that we should not only emit the DataCount section when shared memory is enabled, but also when bulk memory is enabled. Though as shared memory implies that bulk memory is enabled (excluding with `--no-check-features`, possibly) it's possible to just replace the existing checks with one for the presence of bulk memory.
</pre>
<img width="1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