Mixing programming languages with gcc/clang
Posted: Posted October 12th, 2017 by Kohlrak
Just for fun, and to fulfill my random desire for a short ego trip, I want to address how one would go about mixing programming langauges using GCC or CLANG toolchain. The objective of this is to mix compiled languages into one executable, as opposed to mixing progams made in separate languages, or mixing compiled programs with interpreted languages. The reasoning and logic on how this works will be demonstrated directly below, but practical examples will also be made available following that section.
In compiled langauges (including java), all code is converted (as major stages, not minor stages) from it's initial "language" to assembly (human readable machin code). The various assembly outputs are then converted to "object files" which are intermediate files between assembly and binary (not necessary for all programs, but when mixing it's absolutely necessary). The format for these "object files" varies from compiler to compiler, but the general idea is to have everything pre-compiled with references to instructions that may need to be modified to reflect changes in the final binary (memory references to code and data, for the most part). These references are recognized as "symbols" which, due to history, are basically C-compatible. Object oriented languages will need their symbol names "mangled" so that things like operator overflowing end up with c-compatible names.
So, what you want to do is get the compiler to make object files and auto-detect the programming language being used. Normally, it does this based on the file extension of the input files. .c for C, .cpp for C++, .S for assembly (not sure why, though it uses .s for assembly output), etc. The linker will automatically be run afterwards, and will take the object files (which, at this point, are independent of programming language) and make the final binary out of them.
Example (x86, 32bit): Beware, the ASM syntax on pastebin doesn't love you (more to do with the wonky way GAS works [specifically the comments]).
Example (ARM: armeabi-v7a): This is my Samsung Galaxy Tab E (using Termux), but should work on just about any android that's running ARM.
To be honest, I don't fully understand cargo.inc. There might be some extra formatting that's not necessary in there, but I made it a file from some boiler-plate code found using the -S option. It is basically just necessary to describe your target processor to clang/gcc so it knows what encoding to use and what to tell you if you try using an instruction that doesn't exist. All the assembly does here is return 0, but that should be more than enough for you if you use what's above for x86 and extrapolate upon it.
There are 9 Replies
Reply to: Mixing programming languages with gcc/clang