Architectures and ABI Compatiblity

烈酒焚心 提交于 2019-12-23 04:21:22

问题


I have an idea to compile C++ to a binary, store the binary on the heap and execute it. I was considering one implementation of compiling to specific architectures like Google Native Client does, then knowing what architecture I have compiled to, use the same or a different compiler -- at run-time -- within my program to compile a "snippet" or "script" and output the machine code to memory allocated on the heap. Then point a function pointer to the right place and run it. My question is, independent of compilers, if C++ is compiled to the same architecture will the resulting binary have the same ABI and thus have a callable function (call convention)/entry point of any kind? Some inspiration comes from projects like this, but I want to do so in a somewhat platform independent (by that I mean I can compile to the architecture and I know what architecture I am on), compiler independent manner that works at run-time.

A possible implementation I had in mind was just being concerned with LLVM IR code, in other words, just take the IR code, assemble it (to the correct instruction set/architecture), and write the binary to the heap, with the entry point pointed at by a function pointer e.g (pseudo - code):

typedef unsigned short( * )( int ) ENTRY_POINT_T;

ENTRY_POINT_T Inject( Binary code )
{
    void* block = malloc( sizeof( code ) );
    *( ( Binary* ) block ) = code;
    ENTRY_POINT_T entryPoint = ( ENTRY_POINT_T* ) block;
    return entryPoint;
}

#ifdef x86
    #define ARC "x86"
#endif
#ifdef PowerPC
   #defnie ARC "PowerPC"
#endif

int main()
{
    Binary executableCode = llvm.assemble( irCodeString, "-" + ARC );
    auto entry = Inject( executableCode );
    int result = entry( 0 );
    std::cout << result << "\n";
    return 0;
}

As a side note the compiler-rt module in llvm look potentially useful (as well as the parser).

来源:https://stackoverflow.com/questions/50125699/architectures-and-abi-compatiblity

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