compiling application with gcc -m32 on 64-bit system - linux

I try to understand the requirements for developing Linux application running on 64-bit OS (with 64-bit architecture).
If the code was written for 32-bits architecture, does it mean that on regular compilation (gcc -m32) it will run on the 64-bit system OS, without any problems ?
Thank you,
Ran

Yes, it will. The only caveats to this are that you need 32 bit libraries to run the program, and 32 bit development packages to compile it. Most distros provide these and make it simple to install them as well.

Related

Does a 32-bit Linux program's entire runtime lib tree need to be 32-bit?

On my 64-bit RHEL machine, I'm compiling and running a C & Fortran program that is 32-bit only. It invokes the non-static version of a lot of standard Linux libs (X11, OpenGL/Mesa, Motif, etc...), which of course invoke other libs (libjpeg, libc, etc). Do I have to install the 32-bit versions of that entire devl lib tree in order to build this program validly? And do I have to install the 32-bit versions of that entire run-time lib tree in order to run it without it segfaulting or whatever?
As far as i know you can't use 64 bit versions of your libs to run 32 bit software. So yes you need the 32 bit versions.

Is it possible to set up a gcc cross compiler on Linux to compile 64 bit targets on a 32 bit architecture?

I know you can compile to a 32 bit target on 64 bit Linux by using the -m32 flag, but is it also possible to compile to 64 bit on a 32 bit machine by using the -m64 flag?
Or alternatively, do you need to set up a cross gcc tool chain that supports 64 bit targets and is this possible on a 32 bit machine?
Regards,
Johan
As MarkR said, it will be a lot easier to run a 64-bit VM and build from there. Otherwise, yes, you will need to build a gcc cross toolchain.
Good ressources for building a gcc cross toolchain :
Building and Testing gcc/glibc cross toolchains
GCC Cross-Compiler
Cross Compiling With GCC
yes, its possible and actually quite simple. Just install gcc-multilib.
Yes, this should be possible. It's certainly possible on Mac OS X, where Apple's gcc toolchain supports x86, x86-64, ppc and ppc64, and you can compile for all 4 targets from a development machine that is 32 bit or 64 bit.
You'd need to set up a cross compiler. And don't bother. Just run a 64-bit VM instead. You can run 64-bit VMs on a 32-bit host OS provided the CPU supports it (which it will, unless it's ancient).

Compiling for amd64 under i386 Debian

Cheers,
I want to avoid problems with compiling my code on amd64, yet I don't have a 64-bit CPU available and have no hopes of getting upgrade to my machine any time soon. I have no dreams of testing the code (although that should theoretically be possible using qemu-system) but I'd like to at least compile the code using gcc -m64.
Basic idea works:
CFLAGS=-m64 CXXFLAGS=-m64 ./configure --host x86_64-debian-linux
However, the code depends on some libraries which I typically install from Debian packages, such as libsdl1.2-dev, libgmp3-dev and such. Obviously, getting 64-bit versions of packages installed alongside of 32-bit versions is not a one-liner.
What would be your practices for installing the 64-bit packages? Where would you put them, how would you get them there and how would you use them?
To repeat, I don't have 64-bit CPU and cannot afford getting a new machine.
I have already set up amd64-libs-dev to give some basic push to gcc's -m64.
Attempted so far:
Setting up a 64-bit chroot jail with debootstrap in order to simplify installation of 64-bit development packages for libraries. Failed since finishing the setup (and installing anything afterwards!) requires 64-bit CPU.
Installing gcc-multilib and g++-multilib. This appears to do nothing beside depending on libc6-dev-amd64 which I already installed through amd64-libs-dev.
If you're using debian, before you can use gcc -m64, you need to install gcc-multilib and g++-multilib. This will also install all files needed to link and create a 64bit binary.
You don't have to have a 64bit capable CPU for this either.
Then you can call GCC as follows:
$ gcc -m64 source.c -o source
As for external libraries, debian takes care of that if you have multilib installed. I have a 32bit machine that compiles 64bit code for another machine and links a handful of libraries (libpng, libz for example). Works great and the executable run (debian to debian).
You want to look into the dchroot package to set up a simple chroot(8) environment -- that way you can compile real amd64 binaries in a real 64-bit setting with proper libraries and dependencies. This surely works the other way (i.e. I am using i386 chroots on amd64 hosts) but I don't see why it shouldn't work the other way if your cpu supports amd64.
Edit: Now that you stress that you do not have a amd64-capable cpu, it gets a little trickier. "In theory" you could just rebuild gcc from source as a cross-compiler. In practice, that may be too much work. Maybe you can just get another headless box for a few dollars and install amd64 on that?
check out this fine article that describes how to easily create a 32bit chroot, where you can install all the 32bit tools (gcc and libs)
Doesn't Debian distinguish between lib32 and lib64 directories? In that case, you can just grab the packages and force them to install, regardless of architecture.
If that does not work (or would hose your system!) I would set up a chroot environment and apt-get the 64-bit libraries into there.
Check out pbuilder, It can create build environments for many architectures, some instructions here
Try cross compiling SDL, gmp and other libraries yourself. Or manually extract the files you need from the Debain packages.

Will a program complied in 64-bit work on 32-bit machine?

As title said, I have C program complied and created a binary in 64-bit machine.Will this binary work under 32-bit ?
64-bit binaries cannot run on a 32-bit OS. If file reports ELF 64-bit, you have a 64-bit binary.
In order to build 32-bit binaries on a 64-bit Linux, you need pass -m32 to gcc. You also need to have 32-bit libraries installed (sudo apt-get install libc6-dev-i386).
The compiling machine does not matter. What matters is: is the code generated 32 bits (answer: yes) or 64 bits (answer: no).
Will it work on a 32-bit machine? One with a CPU which doesn't support 64-bit mode? No.
Will it work on a 64-bit VM on a 32-bit native OS? Yes, in my experience.
A surprising result is that 64-bit VMs CAN be run under a 32-bit host OS, provided the CPU is capable and you don't want to allocate too much ram (>2G or thereabouts) to the guest.

How can I execute MIPS assembly programs on an x86 linux?

Are there any command line interpreters or any other set of programs around for x86 linux in order to run MIPS assembly programs?
I'd like to be able to write simple MIPS assembly programs and run them from the console on my local machine.
I know of SPIM but it requires X Windows and I'm curious if there are better options out there.
Edit: Turns out it doesn't require X Windows. I still have issues with SPIM. Not the best in my humble opinion. Qemu / Cross compiled toolchain is a little more work but I have less quirks.
Incidentally, Spim does not require X Windows. It has a console interface as well. Run either spim or xspim.
You will need either a cross compilation toolchain, or to build your own cross binutils.
For a prebuilt toolchain, you can visit code sourcery. If you just want to compile assembly, then all
you need is binutils. There are some guidelines on the Linux Mips wiki
For the emulation part, QEmu would be my choice.
MARS made my assembly programming for MIPS architecture so much easier. If you would like a GUI/IDE, I would recommend MARS for sure.
I was in the same situation yesterday. I also didn't like SPIM, so this is what I did:
installed gxemul and gxemul-doc (those are the package names on debian)
installed netbsd on an emulated MIPS machine following the detailed instructions in the documentation
since netbsd already includes the standard gcc toolchain and vi, you're good to go.
Setting up networking is pretty easy and well documented, too. This has the advantage of not needing to fiddle with cross compilation.
You could use gxemul, which emulates a MIPS machine (among others, including Dreamcast), and is able to run many Operating systems (included linux, netbsd and some more).
gxemul-wikipedia
gxemul-home page
QEmu has a good MIPS emulator. Combine that with a cross-compiled GCC/binutils (technically you only need binutils to get GAS, the GNU assembler) and you're good to go.
Assuming you wish to use GCC.
Steps for compiling for MIPS on an x86-64 system, and then running the executable using an emulator:
Use a cross-compilation toolchain to produce an executable.
If you are on Debian/Ubuntu, install a cross-compilation toolchain for MIPS. For example, either of these APT packages: gcc-mips-linux-gnu (MIPS big endian) or gcc-mipsel-linux-gnu (MIPS little endian).
Compile using mips-linux-gnu-gcc (mipsel-linux-gnu-gcc for little endian MIPS); assemble using mips-linux-gnu-as; link using mips-linux-gnu-ld.
Run the executable using an emulator.
Install an emulator that can launch Linux programs compiled for one architecture (e.g. MIPS) on another architecture (e.g. x86-64): sudo apt-get install qemu-user.
Run your executable compiled for MIPS using the emulator: qemu-mips ./a.out (or qemu-mipsel ./a.out for little endian MIPS). Simply running ./a.out might also work; the emulator might be used automagically if you (or your distro's qemu package) has set up binfmt-misc to transparently run qemu-user.
Maybe you can take a look at these emulators? I'm not an expert but the list seems good.

Resources