{"id":20724935,"url":"https://github.com/impiaaa/psx-newlib","last_synced_at":"2025-04-23T18:06:02.014Z","repository":{"id":145048534,"uuid":"268979390","full_name":"impiaaa/psx-newlib","owner":"impiaaa","description":"Newlib libc on the PlayStation 1","archived":false,"fork":false,"pushed_at":"2023-10-07T03:58:32.000Z","size":61,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-23T18:05:16.940Z","etag":null,"topics":["libc","newlib","playstation"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/impiaaa.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2020-06-03T03:24:44.000Z","updated_at":"2024-10-29T13:48:45.000Z","dependencies_parsed_at":null,"dependency_job_id":"6cdc4a39-fe97-40d2-8524-09a60a01c49d","html_url":"https://github.com/impiaaa/psx-newlib","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/impiaaa%2Fpsx-newlib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/impiaaa%2Fpsx-newlib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/impiaaa%2Fpsx-newlib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/impiaaa%2Fpsx-newlib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/impiaaa","download_url":"https://codeload.github.com/impiaaa/psx-newlib/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250487530,"owners_count":21438612,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["libc","newlib","playstation"],"created_at":"2024-11-17T04:16:45.769Z","updated_at":"2025-04-23T18:06:01.986Z","avatar_url":"https://github.com/impiaaa.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"This is a syscall implementation and (only slightly modified) linker script for use with **[Newlib](http://www.sourceware.org/newlib/)** on the **[Sony PlayStation](https://en.wikipedia.org/wiki/PlayStation_(console))**. Newlib is a portable libc/stdlib implementation that doesn't require a Linux or BSD kernel like most libc's, and is complete and standards-compliant unlike a custom libc.\n\nThe PlayStation includes a BIOS ROM that has a number of system support functions, which we use to implement device-specific system calls. However, it additionally includes a few libc functions itself that do not interact with peripherals. As such, Newlib can either be used for all libc functions except as necessary to interact with hardware, or Newlib can be patched to use BIOS routines whereever possible. The second option may run slower in some cases but will result in a smaller executable. Patching newlib is also **required** if you set any exception handlers, link with the legacy PsyQ libraries (which set exception handlers), or debug with the pcsx-redux emulator.\n\n### Option 1: Using a pre-patched, pre-compiled Newlib\n\nDownload a pre-compiled bundle from the [releases page](https://github.com/impiaaa/psx-newlib/releases) and install it into your existing MIPSEL prefix. Make sure to use `-nodefaultlibs -Tpsx.ld` in the linker flags, or if using another linker script, add `-lpsxsys` manually.\n\n### Option 2: Using plain Newlib\n\n1. Copy `psx.c` and `psx.ld` to `libgloss/mips/`.\n2. Add them to `libgloss/mips/Makefile.in`, for example with:\n\n        PSXOBJS = psx.o syscalls.o getpid.o kill.o putnum.o\n        libpsxsys.a: $(PSXOBJS)\n\t        ${AR} ${ARFLAGS} $@ $(PSXOBJS)\n\t        ${RANLIB} $@\n\nThen continue with \"Configuring and compiling Newlib\" below.\n\n### Option 3: Patching Newlib\n\n1. Copy `psx.c`, `psx.ld`, `psx-bonus.c`, and `psx-printf.S` to `libgloss/mips/`.\n2. In the top-level Newlib source, run `patch -p1 \u003cpsx.patch` (using the full path of `psx.patch`).\n\nThen continue with \"Configuring and compiling Newlib\" below.\n\n### Configuring and compiling Newlib\n\n1. Configure Newlib with these flags:\n    * `--target=mipsel-elf`: Required for the platform. The `mipsel-unknown-elf` triple is sometimes recommended in other tutorials, but it does the same thing.\n    * `CFLAGS` (or `CFLAGS_FOR_TARGET` for a unified build) set to:\n        * `-DHAVE_BLKSIZE`: Required. PSX file I/O can only be done in block units, so this tells Newlib to enable \"optimizations\" that transform FS calls to block alignment.\n        * `-g -O2`: From the default configuration.\n        * `-DHAVE_RENAME`: Not required, but you'll need to remove `_rename` from `psx.c` if you don't add the flag.\n    * `--prefix=/usr/local/psxsdk` or whatever. Optional but recommended to keep your host system root clean.\n    * `--disable-multilib`: Optional, but recommended to keep the prefix clean. Disables building extra, unnecessary big-endian and hard-float versions of target libraries.\n    * `--enable-lto`: Optional but recommended. Enables link-time-optimization, building intermediate code so that optimizations can be performed all at once during linking.\n    * `--disable-libssp`: Optional. Disables support for stack protection.\n    * `--disable-libstdcxx`: Optional. Disables support for GNU libstdc++-v3.\n    * `--disable-newlib-hw-fp`: Optional. Enforces disabling hard-float support.\n    * `--disable-newlib-io-float`: Optional. Disables floating point in format strings.\n    * `--disable-newlib-multithread`: Optional. Disables threading support.\n    * `--disable-shared`: Optional. Do not build shared libraries.\n    * `--enable-lite-exit`: Optional. Console games don't typically exit, so why spend time there?\n    * `--enable-newlib-nano-malloc`: Optional. Enables an alternative `malloc` implementation, \"which is for small systems with very limited memory.\"\n\nFor example, here's my full configure line, using a [\"unified\"](https://www.embecosm.com/appnotes/ean9/html/ch02s01.html) build alongside binutils and GCC:\n\n    ../configure --prefix=/usr/local/psxsdk --target=mipsel-elf \\\n\t    --disable-gcov \\\n\t    --disable-libssp \\\n\t    --disable-libstdcxx \\\n\t    --disable-multilib \\\n\t    --disable-newlib-hw-fp \\\n\t    --disable-newlib-io-float \\\n\t    --disable-newlib-multithread \\\n\t    --disable-nls \\\n\t    --disable-shared \\\n\t    --enable-gold \\\n\t    --enable-languages=c,c++ \\\n\t    --enable-lite-exit \\\n\t    --enable-lto \\\n\t    --enable-serial-configure \\\n\t    --with-float=soft \\\n\t    --with-isl \\\n\t    --with-newlib \\\n\t    --with-no-pic \\\n\t    CFLAGS_FOR_TARGET=\"-g -O2 -DHAVE_BLKSIZE -DHAVE_RENAME\"\n\nThen `make` and `make install` as usual. Add the new prefix to your `PATH` if necessary, then you can use `mipsel-elf-gcc` with `-flto -msoft-float -march=r3000` to build things, and `mipsel-elf-gcc-ld` with `-nodefaultlibs -Tpsx.ld` to link things.\n\n### Platform support\n\n#### Stubbed OS subroutines (meaningless on the system, but do not return error)\n\n* `environ`\n* `getpid`\n* `kill`\n\n#### Library functions affected by stubbed OS subroutines (may not behave as expected)\n\n* `assert`\n* `getenv`, `_getenv_r`\n* `raise`, `_raise_r`\n* `tmpfile`, `tmpfile_r`\n* `tmpfile64`, `_tmpfile64_r`\n* `tmpnam`, `tempnam`, `_tmpnam_r`, `_tempnam_r`\n\n#### Unsupported OS subroutines (always return error)\n\n* `gettimeofday`\n* `link`\n\n#### Library functions affected by unsupported OS subroutines (always return error)\n\n* `_gettimeofday_r`\n* `_link_r`\n* `time`\n\n#### Unimplemented OS subroutines (programs that use these will fail to link)\n\n* `dup2`\n* `execve`\n* `execvpe`\n* `fcntl`\n* `fork`\n* `fstat64`\n* `lseek64`\n* `mkdir`\n* `open64`\n* `pipe`\n* `sched_setparam`\n* `sched_setscheduler`\n* `setegid`\n* `seteuid`\n* `setpgid`\n* `sigaction`\n* `sigprocmask`\n* `symlink`\n* `times`\n* `vfork`\n* `wait`\n* `waitpid`\n\n#### Library functions affected by unimplemented OS subroutines (programs that use these will fail to link)\n\n* `_execve_r`\n* `_fcntl_r`\n* `_fork_r`\n* `_fstat64_r`\n* `_lseek64_r`\n* `_mkdir_r`\n* `_open64_r`\n* `_times_r`\n* `_wait_r`\n* `clock`\n* `fopen64`, `_fopen64_r`\n* `freopen64`, `_freopen64_r`\n* `fseeko64`, `_fsseko64_r`\n* `fsetpos64`, `_fsetpos64_r`\n* `mktemp`, `mkdtemp`, `mkstemp`, `mkstemps`, `mkostemp`, `mkostemps`, `_mktemp_r`, `_mkdtemp_r`, `_mkstemp_r`, `_mkstemps_r`, `_mkostemp_r`, `_mkostemps_r`\n* `popen`, `pclose`\n* `posix_spawn`, `posix_spawnp`\n* `pread64`\n* `pwrite64`\n* `system`, `_system_r`\n* `tmpfile64`, `_tmpfile64_r`\n\n### To do\n\n* Make a custom, slimmer `crt0.s`. The built-in one is great, but 1) supplies `_exit`, which could be done as a syscall, and 2) zeroes `.bss` and does some other system initialization that the BIOS boot sequence already does.\n* Pass `argv` to main. Syscall A(0x43) passes parameters to `r4` and `r5`, presumably argc and argv (though they are 0 and null for the boot executable)\n* The boot executable copies some of `system.cnf` to 0x180—could this be used for environment?\n* Add syscall versions of malloc and memset.\n* Consider `-O2` vs `-Os` vs `-O3`.\n\n### Thanks\n\n* [Adrian Siekierka](https://github.com/asiekierka) for [candyk](https://github.com/ChenThread/candyk-packages), which helped me figure out some of the configure flags\n* [Nicolas Noble](https://github.com/nicolasnoble) for [Openbios](https://github.com/grumpycoders/pcsx-redux/tree/master/src/mips/openbios), which helped me figure out how some of the BIOS calls work, and also how to do them inline in C\n* [Nocash](https://problemkaputt.de/) for the [PSX Specifications](https://problemkaputt.de/psx-spx.htm)\n* NextVolume and [Lameguy64](https://github.com/Lameguy64) for their existing PS1 SDKs\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimpiaaa%2Fpsx-newlib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fimpiaaa%2Fpsx-newlib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimpiaaa%2Fpsx-newlib/lists"}