问题
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