psim, the PowerPC simulator

Psim is one of those great peices of software that has been long sice forgotten. In every copy of GDB since 4.14 it has been sitting there dormant. What is it? Well it’ll emulate various PowerPC systems to some degree (GXemul & Qemu are better now) but what is cool is the ‘run’ program. Simply put, any system that can run GDB can run powerpc NetBSD statically linked EXE’s! Now isn’t that exciting!

Now I know you’ll want to build your own copy of the ‘run’ program, and then setup a cross compile enviroment so that you, to can produce NetBSD PowerPC executables. I’m assuming that you are not on a PowerPC running NetBSD, since this whole excersize would be… redundant.

For this example I’m using OpenBSD 4.0 on the i386. You will need a real unixy envioment for this. MinGW isn’t good enough, cygwin however is. BSD/Linux will work too.

Ok first let’s start with building the run program. I’m going to download my GDB from ftp://ftp.gnu.org/ .

Gdb 5.3

Unpack it somewhere, and then run the following commadn to configure GDB.

./configure --enable-sim-powerpc --target=powerpc-unknown-eabi --prefix=/usr/local/psim

 

You will need to replace or update the sim/ppc/emul_netbsd.c program to include system calls up to 300, and make sure that 279 is setup to do_fstat. Otherwise you will be unable to run any programs. This is also a good excersize to see how libraries interact with the simulator so you can add your own native interfaces, for things like OpenGL, SDL….

 

Now we are ready to build it.. You could use -O0 in your CFLAGS to build it quicker, but it will result in a slower run times.. If you have issues with this you will want to use those flags so it doesnt take forever to be building this thing. Otherwise a simple ‘make;make install’ will suffice.

Next up is binutils. I’m using version 2.17

Binutils 2.17

Again download and unpack this somewhere (/usr/src?) Then run the following to configure your binutils


./configure --target=powerpc-unknown-netbsd --prefix=/usr/local/psim

Since execution time here isn’t as critical as the emulator, and I want to hurry it along I’m going to use the following command to build & install:


make CFLAGS=’-O0 -pipe’ ;make install

On OpenBSD 4.1 the install fails, and I had to manually copy the binutils applications into /usr/local/psim/powerpc-unknown-netbsd-bin

Before you build gcc you will want to populate your directory with headers & libraries from NetBSD 1.4 get the compiler package and copy the usr/lib files to /usr/local/psim/powerpc-unknown-netbsd/lib and the usr/include files to /usr/local/psim/include . I get my includes & lib files from here ( ftp://ftp.netbsd.org/pub/NetBSD/NetBSD-archive/NetBSD-1.4/macppc/binary/sets/comp.tgz )

Gcc 3.3

You may need to do some special tweaking to this source to get it to run correctly
Optionally running make with CFLAGS=-O0 may speed your compliation.

You know the drill by now. Download, unpack and configure as follows:


./configure --target=powerpc-unknown-netbsd --prefix=/usr/local/psim --disable-shared --enable-languages="c" --disable-threads --disable-intl

Now make the compiler as follows. Note that I’m only interested in the C compiler. I haven’t even looked at C++/ObjectiveC…

You will need to add /usr/local/psim/bin into your path, as Gcc 3.3 will expext to be able to call powerpc-unknown-netbsd-ld/ar/ranlib etc while building itself.

export PATH=$PATH:/usr/local/psim/bin

Next I would comment out the following line from gcc/builtins.c

builtins.c:2864:// error (“__builtin_saveregs not supported by this target”);

Now we can build the compiler

make CFLAGS=’-O0 –pipe’

Now before we get all giddy, lets test this thing out!

cd into the gcc directory, and lets make a simple c program… hello world is a good starting place.

use xgcc to compile it, and this will test your c compiler.

#include <stdio.h>
void main(void)
{
printf("hello from PowerPC\n");
}

bash-2.05a$ xgcc hello.c -o hello

hello.c: In function main':hello.c:2: warning: return type ofmain’ is not `int’

bash-2.05a$ file hello

hello: ELF 32-bit MSB executable Version 1

bash-2.05a$ ./hello

bash: ./hello: cannot execute binary file

Ok the executable that we have just created is NOT for our native platform, but for the PowerPC.. So lets kick in the emulator! If you didn’t patch your emul_netbsd.c you’ll get this:


% /usr/local/psim/bin/powerpc-unknown-eabi-psim hello

do_call() os_emul call 279 out-of-range

 

However if you did, this is what you’ll get!
% /usr/local/psim/bin/powerpc-unknown-eabi-psim hello
hello from PowerPC

On my computer the installation of gcc gets retarded so I manually copy the files to the right places.


cp xgcc to /usr/local/psim/bin/powerpc-unknown-netbsd-gcc

mkdir -p /usr/local/psim/lib/gcc-lib/powerpc-unknown-netbsd/3.3/

cp cc1 /usr/local/psim/lib/gcc-lib/powerpc-unknown-netbsd/3.3/

cp crtsaveres.o /usr/local/psim/lib/gcc-lib/powerpc-unknown-netbsd/3.3/

Building cross compilers is always involved, but hopefully this will help someone.

Leave a Reply