<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/81486>81486</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
wasm import attributes don't work together with constructor/destructor attributes
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
TerrorJack
</td>
</tr>
</table>
<pre>
Minimum repro:
```c
__attribute__((import_module("foo"), import_name("bar"), constructor)) void foobar(void);
extern void __wasm_call_ctors(void);
__attribute__((export_name("_start")))
void _start(void) {
__wasm_call_ctors();
}
```
When compiled with `clang --target=wasm32 -nostdlib asdf.c -o asdf.wasm`, it produces the following wasm module:
```wat
(module
(type (;0;) (func))
(func $__wasm_call_ctors (type 0))
(func $_start (type 0)
call $__wasm_call_ctors
return)
(table (;0;) 1 1 funcref)
(memory (;0;) 2)
(global $__stack_pointer (mut i32) (i32.const 66560))
(export "memory" (memory 0))
(export "_start" (func $_start)))
```
So `__wasm_call_ctors` does nothing, but this isn't expected behavior, it's supposed to call into the `foobar` import! If I call the `foobar` import in a separate ctor like this:
```c
__attribute__((import_module("foo"), import_name("bar"))) void foobar(void);
__attribute__((constructor)) void call_foobar(void) { foobar(); }
```
Then I get the wasm module with desired behavior:
```wat
(module
(type (;0;) (func))
(import "foo" "bar" (func $foobar (type 0)))
(func $__wasm_call_ctors (type 0)
call $call_foobar)
(func $call_foobar (type 0)
call $foobar
return)
(func $_start (type 0)
call $__wasm_call_ctors
return)
(table (;0;) 1 1 funcref)
(memory (;0;) 2)
(global $__stack_pointer (mut i32) (i32.const 66560))
(export "memory" (memory 0))
(export "_start" (func $_start)))
```
The same issue exists for dtors as well, and the issue reproduces on both clang trunk and latest release 17.0.6.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEVk1vqzgU_TXO5ioRGAJkwWI6VaSONKupNEtk4AY8NTayL03770c2tPls32zeGw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