pspdev / pspsdk

An open-source SDK for PSP homebrew development.
Other
871 stars 148 forks source link

Undefined reference to `__builtin_allegrex_wsbh' #28

Closed joel16 closed 2 years ago

joel16 commented 5 years ago

When trying to compile pspsdk/src/samples/net/simple, it results in the following errors:


rm -f netsample.elf  main.o PARAM.SFO EBOOT.PBP EBOOT.PBP
psp-gcc -I. -I/usr/local/pspdev/psp/sdk/include -O0 -G0 -Wall -g -D_PSP_FW_VERSION=150   -c -o main.o main.c
psp-gcc -I. -I/usr/local/pspdev/psp/sdk/include -O0 -G0 -Wall -g -D_PSP_FW_VERSION=150  -L. -L/usr/local/pspdev/psp/sdk/lib   main.o  -lpspdebug -lpspdisplay -lpspge -lpspctrl -lpspsdk -lc -lpspnet -lpspnet_inet -lpspnet_apctl -lpspnet_resolver -lpsputility -lpspuser -lpspkernel -o netsample.elf
main.o: In function `make_socket':
(.text+0x17c): undefined reference to `__builtin_allegrex_wsbh'
main.o: In function `make_socket':
(.text+0x190): undefined reference to `__builtin_allegrex_wsbw'
main.o: In function `start_server':
(.text+0x480): undefined reference to `__builtin_allegrex_wsbh'
collect2: error: ld returned 1 exit status
/usr/local/pspdev/psp/sdk/lib/build.mak:176: recipe for target 'netsample.elf' failed
make: *** [netsample.elf] Error 1```
carstene1ns commented 5 years ago

Seems they got nuked with some update. You could try replacing them with __builtin_bswap16 for the short versions and __builtin_bswap32 for the long versions in in.h.

joel16 commented 5 years ago

Yup that seems to be my only option for now, but this should be addressed IMO.

f0rtesque commented 4 years ago

Is there a recommended fix for this?

I have updated the newlib in my own fork for the newlib repo https://github.com/Sir-Fortesque/newlib/commit/d47be777e51b80d036cfc5ed8f765a72afd6f4b4

The toolchain compiles successfully aswell as any code the referes to htons(x) htonl(x) ntohs(x) and ntohl(x) where the non-existent builtin methods exist. But I'm not really happy with it. Ideally I'd like the __builtin_allegrex_wsbh and __builtin_allegrex_wsbw to be available.

From reading the gcc changelogs the wsbh and wsbw opcodes were added to the mips configuration in the upstream gcc somewhere around 2013, which I'm assuming thats why the definitions in the gcc patch were removed. I'm not familiar with the internal workings of the gcc compiler but I'm trying to add the wsbw and wsbh methods.

I've looked at the builtin bswap32 and bswap16 and they get compiled to quite a few instructions

bswap16

089004e4 02 00 c2 97     lhu        v0,local_1e(fp)
089004e8 00 1a 02 00     sll        v1,v0,0x8
089004ec 02 00 c2 97     lhu        v0,local_1e(fp)
089004f0 02 12 02 00     srl        v0,v0,0x8
089004f4 25 10 62 00     or         v0,v1,v0
089004f8 00 00 c2 a7     sh         v0,0x0(fp)=>local_20

and bswap32 get compile to the use the __bswapsi2 function

**************************************************************
*                          FUNCTION                          *
**************************************************************
uint __stdcall __bswapsi2(uint param_1)
assume gp = 0x890b7d0
uint              v0:4           <RETURN>
uint              a0:4           param_1
__bswapsi2                                      XREF[3]:     Entry Point(*), main:08900540(c), 
                                                                                          .debug_frame::00000660(*)  
08902bb8 00 2e 04 00     sll        a1,param_1,0x18
    assume gp = <UNKNOWN>
08902bbc 02 16 04 00     srl        v0,param_1,0x18
08902bc0 03 1a 04 00     sra        v1,param_1,0x8
08902bc4 25 10 45 00     or         v0,v0,a1
08902bc8 00 ff 63 30     andi       v1,v1,0xff00
08902bcc 25 10 43 00     or         v0,v0,v1
08902bd0 00 22 04 00     sll        param_1,param_1,0x8
08902bd4 ff 00 03 3c     lui        v1,0xff
08902bd8 24 20 83 00     and        param_1,param_1,v1

I'll continue to work on the gcc changes and make a pull request to the toolchain repo if I can get it working. Does anyone have some good resources on gcc internals?

carstene1ns commented 4 years ago

Yeah, using the bswap methods was a quick fix to get it working, however should be properly fixed. Given they are available in current gcc ,we likely just lack some ifdef or such to actually have them.

fjtrujy commented 2 years ago

Hello @joel16, As you can check now in the CI/CD all the examples are being compiled properly, even the pspsdk/src/samples/net/simple. The issue has been solved after upgrading newlib to latest version.

Thanks

carstene1ns commented 2 years ago

@fjtrujy: I sincerely doubt an updated newlib makes missing functions magically available. In this commit a workaround was added instead: https://github.com/pspdev/newlib/commit/6dc26bb7f8bdc7dff72a811104e1d654d77f75d9 What you did was just copying this over to pspsdk.

fjtrujy commented 2 years ago

@fjtrujy: I sincerely doubt an updated newlib makes missing functions magically available. In this commit a workaround was added instead: pspdev/newlib@6dc26bb What you did was just copying this over to pspsdk.

Hello @carstene1ns, I didn't want to say that the newlib changes are making these functions magically appear. I just followed the first post, where @joel16 complained that the example pspsdk/src/samples/net/simple wasn't working, and it was showing compilation errors, however, it currently is compiling properly, as you can see here:

https://github.com/pspdev/pspsdk/runs/4321341472?check_suite_focus=true

rm -f netsample.elf  main.o PARAM.SFO EBOOT.PBP EBOOT.PBP
psp-gcc -I. -I/usr/local/pspdev/psp/include -I/usr/local/pspdev/psp/sdk/include -G0 -O0 -Wall -g -D_PSP_FW_VERSION=150   -c -o main.o main.c
main.c: In function ‘start_server’:
main.c:147:54: warning: passing argument 3 of ‘accept’ from incompatible pointer type [-Wincompatible-pointer-types]
  147 |      new = accept(sock, (struct sockaddr *) &client, &size);
      |                                                      ^~~~~
      |                                                      |
      |                                                      size_t * {aka unsigned int *}
In file included from /usr/local/pspdev/psp/sdk/include/netinet/in.h:46,
                 from /usr/local/pspdev/psp/sdk/include/pspnet_resolver.h:21,
                 from main.c:21:
/usr/local/pspdev/psp/sdk/include/sys/socket.h:256:47: note: expected ‘socklen_t * restrict’ {aka ‘long unsigned int * restrict’} but argument is of type ‘size_t *’ {aka ‘unsigned int *’}
  256 | int accept(int, struct sockaddr * __restrict, socklen_t * __restrict);
      |                                               ^
psp-gcc -I. -I/usr/local/pspdev/psp/include -I/usr/local/pspdev/psp/sdk/include -G0 -O0 -Wall -g -D_PSP_FW_VERSION=150  -L. -L/usr/local/pspdev/psp/lib -L/usr/local/pspdev/psp/sdk/lib  -Wl,-zmax-page-size=128  main.o  -lpspdebug -lpspdisplay -lpspge -lpspctrl -lpspsdk -lpspnet -lpspnet_apctl -o netsample.elf
psp-fixup-imports netsample.elf
mksfo 'netsample' PARAM.SFO
psp-strip netsample.elf -o netsample_strip.elf
pack-pbp EBOOT.PBP PARAM.SFO NULL  \
    NULL NULL NULL  \
    NULL  netsample_strip.elf NULL
[0]        408 bytes | PARAM.SFO
[1]          0 bytes | NULL
[2]          0 bytes | NULL
[3]          0 bytes | NULL
[4]          0 bytes | NULL
[5]          0 bytes | NULL
[6]     103816 bytes | netsample_strip.elf
[7]          0 bytes | NULL

Anyway, the "new" newlib 4.1.0, has been done from the very scratch by me, and over there I don't have defined any specific __builtin_allegrex function. You can see the changes here: https://github.com/pspdev/newlib/commit/dd3c854d70d2a0b4293569433d262c9ade4d60a0

Now newlib changes are super tiny, being the best for future upgrades.

Thanks

carstene1ns commented 2 years ago

@fjtrujy You did not understand correctly. __builtin_allegrex_wsbh and friends have been part of the gcc patch, which are missing since some time. The example has been compiled successfully with my workaround for some time as well.

All you say is that "your" newlib is super and everything is working, while not understanding the real problem here.

This really bugs me, because you seem to think you solved all problems, while creating some others. For example when you removed the preprocessor define PSP, just because such thing is not present for the PS2 gcc. I bet this broke some custom Makefiles, which you seem not to care about.

fjtrujy commented 2 years ago

@fjtrujy You did not understand correctly. __builtin_allegrex_wsbh and friends have been part of the gcc patch, which are missing since some time.

The example has been compiled successfully with my workaround for some time as well.

All you say is that "your" newlib is super and everything is working, while not understanding the real problem here.

This really bugs me, because you seem to think you solved all problems, while creating some others. For example when you removed the preprocessor define PSP, just because such thing is not present for the PS2 gcc. I bet this broke some custom Makefiles, which you seem not to care about.

Once again I'm not saying anything against this missing instructions, I just said that the example was compiling ( I didn't know if it was compiling before....)

Regarding the PSP flag, we can put it back (I actually don't mind), but obviously we CAN'T assure that all the software are still compiling.

The maximum we can do is to check that our repositories (PSPDEV ones) are working, and they are.