Providing a function alias in gcc?

為{幸葍}努か 提交于 2021-01-29 03:38:53

问题


I'm looking for a way to define an unmangled alias for an exported function in a DLL compiled with GCC.

According to this StackOverflow answer, Visual Studio supports

#pragma comment(linker, "/EXPORT:SomeFunction=_SomeFunction@@@23mangledstuff#@@@@")

to specify that an exported function shall have a given alias. I'm looking for something similar in GCC. The GCC function attribute docs suggest __attribute__ ((alias ("target"))) will do the trick, but this just generates a mangled function name:

#undef DLL_EXPORT
#ifdef CCDLL_EXPORTS
#define DLL_EXPORT __stdcall __declspec(dllexport)
#else
#define DLL_EXPORT __stdcall __declspec(dllimport)
#endif

DLL_EXPORT CCDLL_v1* GetCCDLL_premangled();

DLL_EXPORT CCDLL_v1* GetCCDLL () __attribute__ ((alias ("_Z19GetCCDLL_premangledv")));

and here's the .def file from the resulting DLL:

EXPORTS
    _Z19GetCCDLL_premangledv @1
    _Z8GetCCDLLv @2

The original function is exported and mangled as expected, but so is the function alias. If I drop the DLL_EXPORT from the function alias, I expect it would remain unmangled, but it's also not exported from the DLL. I thought the stdcall calling convention might be mangling the function alias, but when I removed it from the DLL_EXPORT definition, the .def file remained exactly the same.

This SO answer suggests I can define an alias using --defsym=alias_name=target, but ld doesn't seem to recognize this and I can't find anything in the official GCC linker docs at all.

Is there a way to do what I'm looking for, or does GCC just not support unmangled aliases for exported functions?


回答1:


Did you try in your .def file:

normalName=mangled_name @ 1

Another way may be to define an extern "C" function that calls the internal C++ one, and export that.



来源:https://stackoverflow.com/questions/22582666/providing-a-function-alias-in-gcc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!