0

We have object files generated after compiling a code (not written in C/C++). Is it possible to generate equivalent RISC-V code from this object file? If so, how?

1 Answers1

3

Binary translation is a thing, but non trivial. Compared to other compiler infrastructure, it's a relatively niche thing. Furthermore, you seem to want static translation (i.e. generating a complete binary for the target platform and doing nothing more at run time). Static translators either have acceptable slowdown and no support for self-modifying code (and possibly other advanced features), or support all these things by implementing a fully features, very slow emulator.

In any case, implementing one is quite involved, especially for an architecture as complicated as x86. Unless you find an already working general binary translator from x86 to RISC-V, you may be better off by just porting the program into a form that can be compiled for RISC-V (say, a more mainstream language with broader compiler support). Since you appear to have access to the source code, this ought to be doable — and might also improve future maintainability. But first, double and triple check whether you can find a compiler for the currently used language that does target RISC-V.

  • Apparently there is an [LLVM backend](https://github.com/riscv/riscv-llvm/blob/RISCV/README.md) so you should be able to us LLVM as an inrermediate target. – rici Sep 21 '15 at 14:51
  • 2
    @rici For binary translation? Technically yes (though it will not support self-modifying code), but accurately translating all x86 opcodes and their side effects into LLVM IR is still a monumental task, only slightly better than translating to RISC-V directly. I maintain that finding an existing mature project is the only realistic way to get the binary translation route to work. –  Sep 21 '15 at 15:44
  • 1
    I agree. I was suggesting that "just porting the program into a form that can be compiled for RISC-V" could be accomplished by porting the program to emit LLVM; something which might actually be available (depending on what the program is) and which would have other advantages. Perhaps I should have been clearer. – rici Sep 21 '15 at 16:04