Search code examples
binarycompiler-constructionoperating-systemllvmsystem

Format-independent binary object?


Normally object files output by a compiler are in a specific format, e.g. ELF on Linux & etc., mach-O on macOS, and COFF/PE on Windows.

Now, assuming the source code is compatible across platforms. Is there a form of "object files" that are format-independent? In the sense that, an binary file of this "format-independent format" could be "converted" or "lowered" to real object files in whatever format (ELF/mach-O/..) later.

LLVM IR's binary form is a good example, as it could be lowered to machine code. However, LLVM IR is ISA-independent, so it is not "low-level" enough. Assuming ISA is given (e.g. x86_64), can I go even lower from ISA-independent to format-independent?


Solution

  • I'm pretty sure that there is no such format, at least not one that the major compilers support. (Even if there was such format, it would be practically useless without compiler support.)

    But, with help of objconv utility which can convert between object-file formats, you can achieve what you described. Simply choose one of the existing formats as the "independent" one and then use objconv to convert to "specific" formats (ELF/Mach-O/COFF).

    It works pretty well, although some platform-specific data (like debugging information) are not preserved during conversion.