1

I need to use the TCC compiler to link object files generated by GCC. However, GCC in MinGW outputs object files in COFF format, and TCC only supports the ELF format. How can I make GCC generate ELF object files?

$ cat test.c
int main(void)
{
        return 0;
}
$ gcc -c test.c
$ file test.o
test.o: MS Windows COFF Intel 80386 object file
$ tcc -c test.c
$ file test.o
test.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
August Karlstrom
  • 10,773
  • 7
  • 38
  • 60
  • Why not use gcc to link the executable? I'm pretty sure Windows can't run elf executables anyways (At least, not without using WSL)... – Shawn Sep 10 '18 at 06:40

3 Answers3

3

However, GCC in MinGW outputs object files in COFF format

GCC can be configured to generate various outputs (including ELF) regardless of which host it runs on.

That is, a GCC running on Linux could be configured to generate COFF, and a GCC running on Windows could be configured to generate ELF32 or ELF64, for various processors (e.g. x86, or SPARC, or MIPS).

A compiler that runs on one kind of host, but generates code for a different kind, is called a cross-compiler.

TCC only supports the ELF format

This is not a meaningful statement: it could mean that you want GCC to generate ELF32 for i686 Linux, or ELF64 for SPARC Solaris, or any number of other processor/os/bit combinations.

You should figure out what target processor and operating system you want to run your final executable on, and then build (non-trivial) or download appropriate cross-compiler from Windows to that target.

file test.o
test.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped

Ok, you want Windows to Linux/i386/ELF32 cross-compiler.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
2

strip might help. strip accepts various object file formats for input and output type (the bfdname). strip --info for the supported formats.

strip -o outputname -O elf32-i386 objfile Doing so on a 64 bit executable, converted to 32bit control headers will lead to nothing but crash, so pick your output form carefully. Make sure you aren't changing assumed bitwidths / endians along with headers.

Not running MinGW, so, not tested, may not work for your needs, or worse, may jump and catch fire.

Brian Tiffin
  • 3,978
  • 1
  • 24
  • 34
  • 1
    This use of `strip` is highly unusual (I am surprised this works). The *usual* tool for converting one format into another is `objcopy`. – Employed Russian Sep 11 '18 at 05:15
2

You want your compiler (MinGW) to generate binaries that are not of the type usable for your host system (Windows). This is called cross-compiling, and it is a somewhat involved subject -- because to create complete executables you will also need the various libraries: standard libraries, target OS libraries, third-party libraries... so it is not merely the subject of "how do I get the compiler to create ELF", but also "how do I get the whole supporting cast of ELF libs so I can link against them?".

OSDev has quite extensive documentation on the subject of setting up a cross-compiler; however, since you did not tell us what exactly your problem is, it is difficult to advise you further.

If what you want is generating Linux binaries, my advise would be to not bother with cross-compilation (which is a tricky subject -- and much better supported the other way around, i.e. targeting Windows from Linux), but rather install a Linux distribution in parallel to your Windows, and work natively with that.

DevSolar
  • 67,862
  • 21
  • 134
  • 209