Targeting x86 from ARM? - gcc

Is it possible to compile to x86 using gcc on an ARM machine? I can't find any -march option which would allow this to be done.. if it can't be done, no big deal, but I'm still interested to know if it's possible.

Yes, but you don't want a -march machine. You want to build or install a cross compiler with arm as the host architecture and x86 as the target architecture.
Typically (assuming a linux os) you might get binaries like x86_64-linux-gnu-gcc for the compiler and x86_64-linux-gnu-g++ for the C++ compiler.
Building Gcc cross compilers is a bit of an art, but the gcc documentation is reasonably good.

No. Its because cross compiler(including clang) can generate binaries from host gcc for a target. There is no compiler which can generate x86 instructions from arm.

Related

How can I compile for other kernel version and architecture with gcc?

I am doing a penetration test right now. The machine I have low privileged shell doesn't have gcc on it. So I have to compile the C code in my Kali machine first. But the victim has lower kernel version and different architecture. Victim(Cent-OS) has i386, Kali has x86_64 arch. How can I do it in Kali?
Search the internet for "cross compilation", it's the real name standing for "compile from one arch to another".
The process is well documented...
In my opinion, the kernel version mismatch isn't very important.
But you must take care about libc version.
Moreover, try compiling with -static flag will avoid dependency problems...
Note that is isn't a security question ... would better be asked in StackOverflow ...

what does 'build a gcc cross compiler' mean?

I am very new to linux and GCC. The price of raspberry pi lured me in. I am interested in using GCC to cross compile some C Code to target some embedded hardware, specifically a Cortex-M3 micro. I eventually want to have a full suite of compiler/programmer/debugger, but for now I'm starting with compiler.
So I did a quick non-cross compile test on the RP3, and all was well. Now I am researching how to cross compile and target my uc. The gcc documentation online seems to indicate that I can use the plain vanilla gcc, and just specify some command line options to perform cross compilation: https://gcc.gnu.org/onlinedocs/gcc/ARM-Options.html
But searching around, I find a lot of people mentioning building a gcc cross compiler. What does this mean?
Does gcc have options to double as a cross compiler? If so, why would one desire "building" a cross compiler?
A cross-compiler is one that is created on machine type A (combination of hardware and o/s) and either runs on a different machine type B or runs on type A but produces software to be run on a different machine type B.
Thus, if you have a Linux machine using an x86_64 CPU and running on some version of Linux, but you compile GCC so that it will run on an IBM PowerPC platform running some version of AIX, you would be creating a cross-compiler.
Another variant might be having a compiler on Linux using an x86_64 CPU that runs on the Linux machine but produces code for an embedded hardware chip. You'd then benefit from the CPU power of the Linux machine while deploying to a much smaller, less powerful system that maybe has no o/s of its own, or only a minimal o/s.

Using GCC on Xeon Phi

I was told one can run a program on MIC that was built with gcc.
Is that true?
If yes, how to proceed?
I'm using gcc version 4.4.7.
Intel Xeon Phi can indeed run programs that are compiled with the gcc cross compiler. However gcc is not suitable for compiling any applications for the coprocessor, since gcc does "not include support for Knights Corner vector instructions and related optimization improvements. GCC for Knights Corner is really only for building the kernel and related tools; it is not for building applications.", according to Intel.
The problem you have with the gsl library is caused because the library was not cross-compiled for the Intel Xeon Phi, instead was compiled and configured for the host machine.
In order to get gsl (or any other library) working on the Intel Xeon Phi, you have to cross compile it for the coprocessor and provide the necessary paths to the compiler and the linker when you compile a program that requires that library:
gcc hellophi.c -L/path/to/cross/compiled/glslib -lgsl
When using icc, you can transfer the cross compiled library to the coprocessor through the TCP/IP stack (e.g. using scp) and let icc do the environment variable and path handling.

what is cross compilation?

what is cross compilation?
Cross-compilation is the act of compiling code for one computer system (often known as the target) on a different system, called the host.
It's a very useful technique, for instance when the target system is too small to host the compiler and all relevant files.
Common examples include many embedded systems, but also typical game consoles.
A cross-compiler is compiles the source code from one architecture to another architecture.
For example: hello.c
gcc hello.c (gcc is a compiler for x86 architecture.)
arm-cortexa8-linux-gnueabihf-gcc hello.c
(arm-....-gcc is a compiler for the arm architecture.) This you are compiling on the host pc for a target board (e.g rpi, beaglebone, wega board). In this example arm-cortexa8-linux-gnueabihf-gcc is called the 'cross compiler'.
This process is called cross compilation.
see the link for more info cross compilation
To "cross compile" is to compile source on say a Linux box with intent on running it on a MAC or Windows box. This is usually done using a cross compilation plugin, which are readily available from various web servers across the net. If one is to install a cross compilation plugin onto their Linux box that is designed to compile for Windows boxes. Then they may compile for either a Linux/*NIX box as well as have the option to compile and link a Windows-ready executable. This is extremely convenient for a freelance programmer whom has access to no more than a single Linux/Windows/MAC box. Note that various cross compilation plugins will allow for multitudes of applications, some of which you may or may not perceive as useful, thus a thorough perusal of the plugin's README file.
Did you have a particular project in mind that you would like to apply the method of cross compilation to?
In a strict sense, it is the compilation of code on one host that is intended to run on another.
Most commonly it is used with reference to compilation for architectures that are not binary-compatible with the host -- for instance, building RISC binaries on a CISC CPU platform, or 64-bit binaries on a 32-bit system. Or, for example, building firmware intended to run on embedded devices (perhaps using the ARM CPU architecture) on Intel PC-based OSs.
A Cross Compiler is a compiler capable of creating executable code for a platform other than the one on which the compiler is running.
For e.g. a compiler that runs on a Windows 7 PC but generates code that runs on Android smartphone is a cross compiler.
A cross compiler is necessary to compile for multiple platforms from one machine.
A platform could be infeasible for a compiler to run on, such as for the microcontroller of an embedded system because those systems contain no operating system.
In paravirtualization one machine runs many operating systems, and a cross compiler could generate an executable for each of them from one main source.

Cross compiler for embedded platform in Windows

I wish to build a compiler (GCC port) for Linux, so that the built GCC runs on Windows and creates executables for an embedded platform.
Is the above possible?
Yes, it is possible.
It also utterly painful to try to do this yourself -- it is OK on Linux, mostly, but very tricky on Windows. My suggestion is to see if some ready-built compiler from a place like DENX.de, CodeSourcery, or the manufacturer of your embedded platform is available.
I have become a huge fan of the CodeSourcery compilers for platforms like MIPS and Power Architecture. They tend to come with variants for big- and little-endian, various Libc flavors, and to have both bare-metal and linux-target versions available. See their pages about their Lite edition
Building your own cross-compiler and toolset is a right pain.
If your target is running Linux, I suggest doing your cross-work on a Linux box as well, saving you plenty of pain. I use VmWare for that express purpose. You cannot expect to build a Linux for a target on Windows, for example.
In embedded systems compilers are referred based on processor architecture and not on OS. What you probably want is to build a "cross compiler" for a processor other than the one running in your Windows machine. This is of course possible. See for example the WinAVR and WinARM projects. These are compilers for AVR and ARM processor architectures, based on GCC and running in Windows machines. Another example is yagarto. These aren't just a compiler, but a full tool chain that will allow you to built Linux executables (for ARM processors), if the requires libraries are available.
Because GCC relies on POSIX functionality that is not available in Windows natively, you need a Linux-like environment for Windows (such as Cygwin or MinGW).
http://cegcc.sourceforge.net
If you are satisfied to running it on Windows then at www.gnupit.org you can donwload ready GCC cross toolchain for free. There are many target platforms.
You have to build GCC on Windows using MinGW or Cygwin for your target platform (e.g. ARM little endian).

Resources