Issue 170910
Summary Wrong table index for function pointers when dynamically linking for wasm32-wasi
Labels new issue
Assignees
Reporter rrwtf4vmhd-design
    Hello everyone!

I am currently experimenting with dynamic linking for wasm32-wasi (p1).
The C code below crashes at runtime for the second case in the main routine (when compiled with -Wl,-fPIC -Wl,-shared).
I could pinpoint the problem a bit: When you pass a function pointer to a designated initializer of a C struct the __table_base (offset for the function index) is not added yielding a wrong table index for the function. The table index is correct, when I use the function pointer directly (see first case).
When I use any optimisation other than -O0, the problem goes away for this simple test. However, when I try to compile a larger code base with many pointers in many structs, it does fail again even with -O[1-3].
I am using clang version 21.1.4-wasi-sdk. 

Any ideas how to fix or circumvent the problem would be highly appreciated! 
Thank you for your great work!

`#include <stdio.h>

static void apply_func(void (*f)(int), int x) {
	f(x);
}

static void func(int x) {
	printf("func(%d)\n", x);
}

typedef struct func_struct func_struct;
struct func_struct {
	void (*f)(int);
};

int main(void) {
	// (1) this does work:
	apply_func(func, 42);

	// (2) this does NOT work:
	static func_struct fs = { func };
	apply_func(fs.f, 42);
	
	return 0;
}
  `

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to