Skip to content

Commit

Permalink
add locale stubs so we can compile libcxx
Browse files Browse the repository at this point in the history
  • Loading branch information
john-tornblom committed May 1, 2024
1 parent 3d5667b commit fdd5137
Show file tree
Hide file tree
Showing 7 changed files with 702 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ MAKE ?= make
DESTDIR ?= /opt/ps5-payload-sdk

TOPTARGETS := all clean install
SUBDIRS := crt sce_stubs include host
SUBDIRS := crt sce_stubs locale include host

$(TOPTARGETS): $(SUBDIRS)

Expand Down
1 change: 0 additions & 1 deletion host/bin/prospero-clang++
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export PATH=${SCRIPT_DIR}:$PATH
exec -a "${SCRIPT_NAME}" "${SCRIPT_DIR}/clang++" \
-target x86_64-sie-ps5 \
-isysroot "${PS5_PAYLOAD_SDK}" \
-isystem "${PS5_PAYLOAD_SDK}/target/include" \
-stdlib++-isystem "${PS5_PAYLOAD_SDK}/target/include/c++/v1" \
-fno-stack-protector -fno-plt \
"$@"
2 changes: 1 addition & 1 deletion host/bin/prospero-lld
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ exec "${BIN_PATH}" \
-z max-page-size=0x4000 \
-T "${PS5_PAYLOAD_SDK}/ldscripts/elf_x86_64.x" \
-L "${PS5_PAYLOAD_SDK}/target/lib" \
-lScePosixForWebKit -lSceLibcInternal "${LIBKERNEL}" "$@" \
-lScePosixForWebKit -lSceLibcInternal -lc_locale "${LIBKERNEL}" "$@" \
"${PS5_PAYLOAD_SDK}/target/lib/crt1.o"
52 changes: 52 additions & 0 deletions locale/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright (C) 2024 John Törnblom
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not see
# <http://www.gnu.org/licenses/>.

DESTDIR ?= /opt/ps5-payload-sdk

CCS := clang-18 clang-17 clang-16 clang-15 clang
CCS := $(foreach CC,$(CCS),$(if $(shell command -v $(CC) 2> /dev/null),$(CC)))
CC := $(firstword $(CCS))
ifndef CC
$(error No suitable compiler found)
endif

ARS := llvm-ar-18 llvm-ar-17 llvm-ar-16 llvm-ar-15 llvm-ar
ARS := $(foreach LD,$(ARS),$(if $(shell command -v $(AR) 2> /dev/null),$(AR)))
AR := $(firstword $(ARS))
ifndef AR
$(error No suitable archiver found)
endif

CFLAGS := -target x86_64-sie-ps5 -fPIC -fno-plt -fno-stack-protector
CFLAGS += -nobuiltininc -isystem ../include/freebsd

SRCS := $(wildcard *.c)
OBJS := $(SRCS:.c=.o)

ARCHIVE := libc_locale.a

all: $(ARCHIVE)

$(ARCHIVE): $(OBJS)
ar -rsc $@ $^

clean:
rm -f *.a *.o

install: $(ARCHIVE)
install -d $(DESTDIR)/target/lib
install $^ $(DESTDIR)/target/lib

78 changes: 78 additions & 0 deletions locale/mbsnrtowcs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copyright © 2005-2020 Rich Felker, et al.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <wchar.h>

size_t mbsnrtowcs(wchar_t *restrict wcs, const char **restrict src, size_t n, size_t wn, mbstate_t *restrict st)
{
size_t l, cnt=0, n2;
wchar_t *ws, wbuf[256];
const char *s = *src;
const char *tmp_s;

if (!wcs) ws = wbuf, wn = sizeof wbuf / sizeof *wbuf;
else ws = wcs;

/* making sure output buffer size is at most n/4 will ensure
* that mbsrtowcs never reads more than n input bytes. thus
* we can use mbsrtowcs as long as it's practical.. */

while ( s && wn && ( (n2=n/4)>=wn || n2>32 ) ) {
if (n2>=wn) n2=wn;
tmp_s = s;
l = mbsrtowcs(ws, &s, n2, st);
if (!(l+1)) {
cnt = l;
wn = 0;
break;
}
if (ws != wbuf) {
ws += l;
wn -= l;
}
n = s ? n - (s - tmp_s) : 0;
cnt += l;
}
if (s) while (wn && n) {
l = mbrtowc(ws, s, n, st);
if (l+2<=2) {
if (!(l+1)) {
cnt = l;
break;
}
if (!l) {
s = 0;
break;
}
/* have to roll back partial character */
*(unsigned *)st = 0;
break;
}
s += l; n -= l;
/* safe - this loop runs fewer than sizeof(wbuf)/8 times */
ws++; wn--;
cnt++;
}
if (wcs) *src = s;
return cnt;
}
Loading

0 comments on commit fdd5137

Please sign in to comment.