MIPS Disassembly on a x86 machine using GDB; possible? - c

I have a x86 development machine and developing kernel module for mips. I wanted to disassemble a routine to find problems with the module.
So my question is
"Can I disassemble it on x86 machine or I will have to get a MIPS development machine ?"
I tried it, but it disassembles in x86 instruction set.

You basically need some form of cross compilation. A cross compiler would allow you to compile on a host machine (x86 in your case) for a target machine (MIPS in your case). So you would be able to generate MIPS binaries from your x86 machine. Moreover, you would also get all the other tools associated to the compiler, such as objdump. Here you have a guide on how to build a cross compiler for GCC.
Assuming you are using objdump to disassemble a binary, you may not need to build a cross compiler. objdump belongs to binutils and it may be possible to just compile binutils for using MIPS as the target (I have never tried to create a cross-platform build of binutils, so I am not 100% sure).
EDIT: I just read the title again, and realized that you are using gdb. In that case I believe you would need to create a full cross compiler, and create a cross-platform version of gdb.

Related

How can I cross-compile C code for a Cyrix Cx486DX?

The question says it all. I need to cross-compile for a Cyrix CPU. The system the compiler (doesn't have to be gcc) needs to run on is a 64bit Kubuntu, with an i5 processor. I couldn't find anything useful googling, except for a piece of information saying that "Cx486DX is software-compatible with i486". So I ran
gcc -m32 -march=i486 helloworld.c -o helloworld486.bin
but executing helloworld486.bin on the Cyrix machine gives me a floating point exception. My knowledge about CPUs is rather limited and I'm out of ideas now, any help would be really appreciated.
Unfortunately you need more than just a compiler that generates instructions for the 486. The compiler libraries, as well as any libraries that are linked in statically should be suitable as well. The GCC version included in most current Linux distributions is able to generate 486-only object files (I think), but its libraries and stub objects (e.g. crtbegin.o) have been pre-generated for 686 CPUs.
There are two main alternatives here:
Use a Linux build system that is compiled for 486 itself, either in a VM or in a chroot jail. Unfortunately getting a modern Linux distribution for the 486 is a bit of an issue - every single major distribution has moved on. Perhaps a (much) older Linux distribution would be of help?
Create a full cross-compiler toolchain for the 486. You can then cross-compile separate versions of all needed libraries and have your build scripts use them. Quite honestly, ensuring that nothing from the (usually 686-based) build host slips through to the build result is not very easy. It oftens amounts to cross-compiling a whole Linux system from scratch, ala CLFS.
An automated cross-compiler toolchain build script, such as crosstool-ng might be of help.
Could you add more details about your target system? Is it an embedded system or just an old PC? What OS is it using? Would it be possible to just run your compile in a VM with a version of the target OS?

Which compiler should be used for porting C code to ARM?

I am new to ARM. I have written C code,but I'm looking to port the code to ARM which runs on all ARM processors. I am going to develop high end applications such as those which run on smartphones/ tablets. Can you guys tell which is the best compiler for that? I have heard about RVDS, but that seems to be expensive.
My other question is: Can I use Microsoft Visual studio 2008 to program ARM Intrinsic like Neon? Are there any other alternatives for RVDS?
Your advice is greatly appreciated.
Several version of the gcc toolchain are available for ARM processors. You need to figure out the exact target for your code such as:
Which processor or set of processors ? What instruction sets and extensions are supported ? (There are many like ARMv6, ARMV7, ARMv7a). Some of them might have NEON support and some might not.
What kind of FPU support - hard FP vs soft FP ?
What kind of OS environment ? Linux ? Android ? Bare metal ?
Which C library - glibc, uclibc, bionic libc or something else ?
The last 2 are kind of built into the toolchain and cannot be modified. But choosing the exact instruction set and optimization can be controlled using ARM specific -m flags in gcc.
And distros like Ubuntu already have the ARM toolchain as part of their repos. You can give that a try too :
sudo apt-get install gcc-arm-linux-gnueabi

GCC Error while compiling for ARM

I am getting the following error while trying to compile some code for an ARM Cortex-M4
using
gcc -mcpu=cortex-m4 arm.c
`-mcpu=' is deprecated. Use `-mtune=' or '-march=' instead.
arm.c:1: error: bad value (cortex-m4) for -mtune= switch
I was following GCC 4.7.1 ARM options. Not sure whether I am missing some critical option. Any kickstart for using GCC for ARM will also be really helpful.
As starblue implied in a comment, that error is because you're using a native compiler built for compiling for x86 CPUs, rather than a cross-compiler for compiling to ARM.
GCC only supports a single general architecture type in any given compiler binary -- so, although the same copy of GCC can compile for both 32-bit and 64-bit x86 machines, you can't compile to both x86 and ARM with the same copy of GCC -- you need an ARM-specific GCC.
(As auselen suggests, getting a pre-built one will save you quite a lot of work, even if you're only using it as a starting point to get things set up. You need to have GCC, binutils, and a C library as a minimum, and those are all separate open-source projects that the pre-built versions have already done the work of combining. I'll recommend Sourcery CodeBench Lite since that's the one my company makes and I do think it's a fairly good one.)
As the error message says -mcpu is deprecated, and you should use the other options stated. However "deprectated" simply means that its use may not continue to be supported; it will still work.
ARM Cortex-M4 is ARM Architecture V7E-M, so you should use -march=armv7-m (the documentation does not specifically list armv7e-m, but that may have been added since the documentation was last updated. The E is essentially the difference between M3 and M4 - the DSP instructions, so the compiler will not generate code that takes advantage of these instructions. Using ARM's Cortex-M DSP library is probably the best way to use these instructions to benefit your application. If your part has an FPU, then other options will be needed enable code generation for that.
Like others already pointed out, you are using a compiler for your host machine, and you need a compiler for generating code for your target processor instead (a cross compiler). Like #Brooks suggested, you can use a pre-built toolchain, but if you want to roll out your own cross-compiler, libc and binutils, there is a nice tool called Crosstool-NG. It greatly simplifies the process of building a cross-compiler optimized to generate code for a specific processor, so you're not stuck with a generic prebuilt toolchain, which usually builds code for a family of compatible processors (e.g. you could tune the toolchain for generating ASM for your specific target, or floating point code for a hardware FPU which is specific to your processor, instead of using only software floating point routines, which are default to most pre-built toolchains).

MIPS executable / object file format

As part of learning MIPS assembly, I want to cross compile some C source files with LCC and then disassemble them. I found a MIPS disassembler that runs on Windows, but it says in the description:
Disassembles pure memory dumps (raw code) and GCC object files
I know that for x86 there are multiple executable / object file formats depending on the target OS. Is this the case for MIPS? Do you think this will work? Or am I going to be stuck having to install a linux distro so that I can use one of the precompiled gcc MIPS toolchains like CodeSourcery?
It looks like lcc supports the -S compiler option, which emits the assembly output. Perhaps that would save you some effort?
Executable formats are always according to the operating system and compiler (which obviously must work hand-in-hand). If you decide you need to use gcc, http://faculty.cs.tamu.edu/bettati/Courses/410/2006C/Projects/gxemulcygwin.html appears to discuss a means of installing one under Win32 through cygwin.
To answer the original question, Linux on MIPS uses ELF format files for executables and shared objects. Bare metal MIPS systems would likely use some form of memory dump.
The objdump utility from the GNU codesourcery toolchain will disassemble ELF files and GCC .o files.

General questions about GCC and cross compiling

Recently I've been playing around with cross compiling using GCC and discovered what seems to be a complicated area, tool-chains.
I don't quite understand this as I was under the impression GCC can create binary machine code for most of the common architectures, and all that else really matters is what libraries you link with and what type of executable is created.
Can GCC not do all these things itself? With a single build of GCC, all the appropriate libraries and the correct flags sent to GCC, could I produce a PE executable for a Windows x86 machine, then create an ELF executable for an embedded Linux MIPS device and finally an executable for an OSX PowerPC machine?
If not can someone explain how you would achieve this?
With a single build of GCC, all the
appropriate libraries and the correct
flags sent to GCC, could I produce a
PE executable for a Windows x86
machine, then create an ELF executable
for an embedded Linux MIPS device and
finally an executable for an OSX
PowerPC machine? If not can someone
explain how you would achieve this?
No. A single build of GCC produces object code for one target architecture. You would need a build targeting Intel x86, a build targeting MIPS, and a build targeting PowerPC. However, the compiler is not the only tool you need, despite the fact that you can build source code into an executable with a single invocation of GCC. Under the hood, it makes use of the assembler (as) and linker (ld) as well, and those need to be built for the target architecture and platform. Usually GCC uses the versions of these tools from the GNU binutils package, so you'd need to build that for the target platform too.
You can read more about building a cross-compiling toolchain here.
I don't quite understand this as I was
under the impression GCC can create
binary machine code for most of the
common architectures
This is true in the sense that the source code of GCC itself can be built into compilers that target various architectures, but you still require separate builds.
Regarding -march, this does not allow the same build of GCC to switch between platforms. Rather it's used to select the allowable instructions to use for the same family of processors. For example, some of the instructions supported by modern x86 processors weren't supported by the earliest x86 processors because they were introduced later on (such as extension instruction sets like MMX and SSE). When you pass -march, GCC enables all opcodes supported on that processor and its predecessors. To quote the GCC manual:
While picking a specific cpu-type will
schedule things appropriately for that
particular chip, the compiler will not
generate any code that does not run on
the i386 without the -march=cpu-type
option being used.
If you want to try cross-compiling, and don't want to build the toolchain yourself, I'd recommend looking at CodeSourcery. They have a GNU-based toolchain, and their free "Lite" version supports quite a few architectures. I've used it for Linux/ARM and Android/ARM.

Resources