Architectures and ABI Compatiblity
Asked Answered
E

0

0

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).

Encomiastic answered 2/5, 2018 at 0:44 Comment(3)
Why are there votes to close the question? Could someone give me some tips to refactor the question?Encomiastic
Make it clearer. For example, are you talking about compiling on the same architecture with different compilers? And make it clearer what you mean by architecture you seem to switch between this term and "platform".Loathsome
@NeilButterworth Thank you for the reply. For the first part, I mean have say GCC on a Mac target x86 and MSVC on Windows target x86 and have them be binary compatible (though I know MSVC uses a different ABI, but I didn't want to use clang because it is known that GCC and clang are ABI compatible, so maybe target MSVCx86 somehow). For your second part I made the correction, but one thought in the back of my head is to include the platform compiler with the executable and compile for the platform rather than ABI, I thought if I used architectures instead I could be more platform independent.Encomiastic

© 2022 - 2024 — McMap. All rights reserved.