ps2dev / ps2toolchain

This program will automatically build and install a compiler and other tools used in the creation of homebrew software for the Sony PlayStation® 2 videogame system.
BSD 2-Clause "Simplified" License
239 stars 74 forks source link

Toolchain not compatible with other build systems (cmake) #36

Closed rickgaiser closed 6 years ago

rickgaiser commented 6 years ago

While trying to create/port cmake projects I bumped into some problems with the ps2toolchain. cmake reports the toolchain as being broken, becouse it fails to compile simple hello world programs. For example this fails:

ee-gcc -o hello.elf helloworld.c

Becouse the configuration of the toolchain is incomplete, we need to write this as: ee-gcc -mno-crt0 -T/usr/local/ps2dev/ps2sdk/ee/startup/linkfile -o hello.elf /usr/local/ps2dev/ps2sdk/ee/startup/crt0.o helloworld.c -L/usr/local/ps2dev/ps2sdk/ee/lib -lc -lkernel

Leaving crt0.o out seems work, and the resulting executable has the same md5 hash: ee-gcc -T/usr/local/ps2dev/ps2sdk/ee/startup/linkfile -o hello.elf helloworld.c -L/usr/local/ps2dev/ps2sdk/ee/lib -lc -lkernel

The linkfile can probably be integrated into binutils/ld. The followin (untested) change makes this possible: https://github.com/rickgaiser/binutils-ps2/commit/9bfe80e18bad016381d4ffb0938917706048de01

This shrinks down the required command line to: ee-gcc -o hello.elf helloworld.c -L/usr/local/ps2dev/ps2sdk/ee/lib -lc -lkernel

Then to tell gcc to include -lc and -lkernel, I added the following line to gcc/config/mips/r5900.h: #define LIB_SPEC "-lc -lkernel"

This shrinks down the required command line to: ee-gcc -o hello.elf helloworld.c -L/usr/local/ps2dev/ps2sdk/ee/lib

Almost there, but I can't get the library path integrated. Any attempts I make result in linking errors.

sp193 commented 6 years ago

What sort of linking errors?

BTW if it is supposed to build a C program (I'm not so sure about how C++ linking works in our case), then you should specify mno-crt0 and our crt0.o object. The default crt0 module provided by GCC will not work. It should result in some difference (since crt0 is different), so I am not sure why that did not happen... D:

rickgaiser commented 6 years ago

The message I get is this:

ee-gcc    -c -o helloworld.o helloworld.c
ee-gcc -o test.elf helloworld.o
/usr/local/ps2dev/ee/lib/gcc-lib/ee/3.2.3/crt0.o(.text+0x8c): In function `_start':
src/crt0.s:91: undefined reference to `_ps2sdk_args_parse'
/usr/local/ps2dev/ee/lib/gcc-lib/ee/3.2.3/crt0.o(.text+0x90):src/crt0.s:91: undefined reference to `_ps2sdk_args_parse'
/usr/local/ps2dev/ee/lib/gcc-lib/ee/3.2.3/crt0.o(.text+0xa4):src/crt0.s:102: undefined reference to `_ps2sdk_libc_init'
/usr/local/ps2dev/ee/lib/gcc-lib/ee/3.2.3/crt0.o(.text+0xa8):src/crt0.s:102: undefined reference to `_ps2sdk_libc_init'
/usr/local/ps2dev/ee/lib/gcc-lib/ee/3.2.3/crt0.o(.text+0x110): In function `_exit':
src/crt0.s:155: undefined reference to `_ps2sdk_libc_deinit'
/usr/local/ps2dev/ee/lib/gcc-lib/ee/3.2.3/crt0.o(.text+0x114):src/crt0.s:155: undefined reference to `_ps2sdk_libc_deinit'
/usr/local/ps2dev/ee/lib/gcc-lib/ee/3.2.3/../../../../ee/lib/libc.a(sbrk.o)(.text+0x8): In function `sbrk':
../../../../../../newlib/libc/sys/ps2/sbrk.c:8: undefined reference to `ps2_sbrk'
sp193 commented 6 years ago

Have you tried the "with-lib-path" configure-time option for Binutils? There's also the "LD_LIBRARY_PATH" environmental variable.

But I haven't tried any of them, so I don't know if they're applicable to Binutils v2.14.

rickgaiser commented 6 years ago

Turns out there are two c libraries. One from ps2sdk, and one from newlib. crt0.o needs the c library from ps2sdk to link to, but instead it was linking to the newlib c library. Having two libraries with the same name in one sdk is somewhat confusing. The fix in gcc was to change LIB_SPEC to:

#define LIB_SPEC "-L/usr/local/ps2dev/ps2sdk/ee/lib /usr/local/ps2dev/ps2sdk/ee/lib/libc.a -lkernel -lc"

This produces binaries, and cmake now accepts the toolchain also. But the binaries do not work :-(. Adding -T/usr/local/ps2dev/ps2sdk/ee/startup/linkfile fixes the binaries, so this next problem is in ld's linkfile (https://github.com/rickgaiser/binutils-ps2/commit/9bfe80e18bad016381d4ffb0938917706048de01).

rickgaiser commented 6 years ago

It's working now :-). The reginfo section was placed before the data section. So ee-gcc can now create working binaries by default.

binutils patch here and here gcc path here

I'll create a pull request with these patches soon.