One way to achieve this might be to use binaryen tools to inject your wasm code into the module. One of the simplest ways would be use wasm-dis to turn the module into text, when wasm-as to re-assembly it after adding your new imports and code.
*From: *Duan Bing <[email protected]> *Date: *Fri, May 3, 2019 at 8:14 AM *To: * <[email protected]> Thanks, Floh. > I didn't compile it into a library, so visibility attribute seems not > take effect. > As we can see from emscripth.h > #define EMSCRIPTEN_KEEPALIVE __attribute__((used)) __attribute__ > ((visibility ("default"))) actually, this macro affects the function with > definition, but not for declaration only from my trials. > > > Floh <[email protected]> 于2019年5月3日周五 上午1:12写道: > >> PS: I've been looking around a bit, and maybe this is the better solution >> (via __attribute__(...)): >> >> __attribute__((visibility("default"))) void foo(void); >> >> This should be the clang equivalent to __declspec(dllimport) in the >> Visual Studio compiler, see here: >> >> https://webassembly.org/docs/dynamic-linking/ >> >> On Thursday, 2 May 2019 17:06:27 UTC+2, Duan Bing wrote: >>> >>> Thanks, your new idea works for me. the motivation of doing this is >>> that I will call this imported function while I will insert some wasm code >>> to the original wasm file later, similar to this project had done >>> https://github.com/ewasm/wasm-metering , but by a different PL. so I >>> don't want the original wasm developer to use this function(I will insert >>> foo dynamically with a mangling name ), and I also occupy the import index >>> space by doing this in advance. >>> >>> Floh <[email protected]> 于2019年5月2日周四 下午10:56写道: >>> >>>> Ah ok, I misunderstood your question, apologies. I don't know how one >>>> would add a function declaration to the WASM imports table like this. >>>> EMSCRIPTEN_KEEPALIVE and the "-s EXPORTED_FUNCTIONS" linker option would >>>> work for the "other side" (some WASM module exporting functions to the >>>> outside). >>>> >>>> Complete guess, but maybe you need some "dummy code" that's guaranteed >>>> to stay in the importing WASM module, even though this is never called? So >>>> that the linker would recognize the function as an "import". >>>> >>>> Something like this: >>>> >>>> extern "C" void foo(); >>>> // dummy_imports() is never called, but will not be removed because of >>>> EMSCRIPTEN_KEEPALIVE >>>> EMSCRIPTEN_KEEPALIVE void dummy_imports(void) { >>>> foo(); >>>> } >>>> >>>> But as I said, this is complete guess. >>>> >>>> On Thursday, 2 May 2019 16:22:28 UTC+2, Duan Bing wrote: >>>>> >>>>> Thanks, Floh. it works when the function is defined. >>>>> Actually, my function is an external function declaration, like >>>>> extern "C" void foo(); >>>>> >>>>> It doesn't work even I declare like this: >>>>> extern "C" void EMSCRIPTEN_KEEPALIVE foo(). >>>>> >>>>> Do you have any idea for this situation? >>>>> >>>>> Floh <[email protected]> 于2019年5月2日周四 下午10:13写道: >>>>> >>>>>> Have a look at EMSCRIPTEN_KEEPALIVE: >>>>>> https://emscripten.org/docs/api_reference/emscripten.h.html#c.EMSCRIPTEN_KEEPALIVE, >>>>>> this prevents a C/C++ function from being removed even when it isn't >>>>>> referenced by other C/C++ code (normally this is used to call C functions >>>>>> from the JS side). >>>>>> >>>>>> On Thursday, 2 May 2019 08:16:43 UTC+2, Duan Bing wrote: >>>>>>> >>>>>>> I declare an external function in C source file, but never used by >>>>>>> the code, then compile it to wasm by EMCC. there is no doubt the >>>>>>> external >>>>>>> function will be eliminated by the compiler. >>>>>>> >>>>>>> Is there any approach to keep this external function and then be >>>>>>> compiled to an import function in wasm? >>>>>>> >>>>>>> The motivation to remain the external function is that I want to >>>>>>> insert some wasm code in the wasm file after it's generated. >>>>>>> >>>>>>> Thanks all! >>>>>>> >>>>>> -- >>>>>> You received this message because you are subscribed to the Google >>>>>> Groups "emscripten-discuss" group. >>>>>> To unsubscribe from this group and stop receiving emails from it, >>>>>> send an email to [email protected]. >>>>>> For more options, visit https://groups.google.com/d/optout. >>>>>> >>>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "emscripten-discuss" group. >>>> To unsubscribe from this group and stop receiving emails from it, send >>>> an email to [email protected]. >>>> For more options, visit https://groups.google.com/d/optout. >>>> >>> -- >> You received this message because you are subscribed to the Google Groups >> "emscripten-discuss" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> For more options, visit https://groups.google.com/d/optout. >> > -- > You received this message because you are subscribed to the Google Groups > "emscripten-discuss" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "emscripten-discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
