aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi92@disroot.org>2025-07-04 02:54:54 +0200
committerXavier Del Campo Romero <xavi92@disroot.org>2025-09-08 21:10:23 +0200
commita0672be24d53ec78d1a2567cf60d9f774b0d158a (patch)
tree738e4880f5afd01802751e3847b36a4ea6eb3a7a /src
parentbb49e6bf0e4c299ad860a78441a58b7c3678bed0 (diff)
downloadwnix-a0672be24d53ec78d1a2567cf60d9f774b0d158a.tar.gz
Do not rely on PSXSDK libc or _start
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt20
-rw-r--r--src/libc/CMakeLists.txt8
-rw-r--r--src/libc/include/stddef.h8
-rw-r--r--src/libc/include/stdlib.h7
-rw-r--r--src/libc/include/string.h12
-rw-r--r--src/libc/src/memcmp.c12
-rw-r--r--src/libc/src/memcpy.c13
-rw-r--r--src/libc/src/memset.c7
-rw-r--r--src/libc/src/strchr.c11
-rw-r--r--src/libc/src/strlen.c12
-rw-r--r--src/playstation.x72
-rw-r--r--src/start.c10
12 files changed, 192 insertions, 0 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 51806be..419f60e 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -14,6 +14,15 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
+add_subdirectory(nanowasm)
+
+if(PS1_BUILD)
+ add_subdirectory(libc)
+ target_link_libraries(nanowasm PRIVATE c)
+ target_link_libraries(${PROJECT_NAME} PRIVATE c)
+ target_sources(${PROJECT_NAME} PRIVATE start.c)
+endif()
+
# Avoid C11 since it is not supported by the i386-mingw32 toolchain.
set(cflags ${cflags} -Wall -ffunction-sections -fdata-sections -pedantic)
@@ -24,6 +33,7 @@ set(interfaces
)
target_compile_options(${PROJECT_NAME} PUBLIC ${cflags})
+target_compile_options(nanowasm PUBLIC ${cflags})
# Dependencies for main.c
# target_link_libraries(${PROJECT_NAME} PRIVATE)
@@ -32,6 +42,11 @@ foreach(c ${components})
target_compile_options(${c} PUBLIC ${cflags})
target_compile_features(${c} PUBLIC c_std_99)
set_target_properties(${c} PROPERTIES C_STANDARD 99 C_EXTENSIONS OFF)
+ target_link_libraries(${PROJECT_NAME} PRIVATE ${c})
+
+ if(PS1_BUILD)
+ target_link_libraries(${c} PRIVATE c)
+ endif()
endforeach()
foreach(i ${interfaces})
@@ -41,3 +56,8 @@ foreach(i ${interfaces})
endforeach()
target_link_options(${PROJECT_NAME} PRIVATE -Wl,--gc-sections)
+
+if(PS1_BUILD)
+ target_link_options(${PROJECT_NAME} PRIVATE
+ -T${CMAKE_CURRENT_LIST_DIR}/playstation.x)
+endif()
diff --git a/src/libc/CMakeLists.txt b/src/libc/CMakeLists.txt
new file mode 100644
index 0000000..b9bb597
--- /dev/null
+++ b/src/libc/CMakeLists.txt
@@ -0,0 +1,8 @@
+set(inc "include")
+set(src
+"src/memcpy.c"
+"src/memset.c"
+"src/strlen.c"
+)
+add_library(c ${src})
+target_include_directories(c PUBLIC ${inc})
diff --git a/src/libc/include/stddef.h b/src/libc/include/stddef.h
new file mode 100644
index 0000000..123e46f
--- /dev/null
+++ b/src/libc/include/stddef.h
@@ -0,0 +1,8 @@
+#ifndef _STDDEF_H
+#define _STDDEF_H
+
+#define NULL ((void *)0)
+
+typedef unsigned long size_t;
+
+#endif
diff --git a/src/libc/include/stdlib.h b/src/libc/include/stdlib.h
new file mode 100644
index 0000000..79d3122
--- /dev/null
+++ b/src/libc/include/stdlib.h
@@ -0,0 +1,7 @@
+#ifndef _STDLIB_H
+#define _STDLIB_H
+
+#define EXIT_SUCCESS 0
+#define EXIT_FAILURE 1
+
+#endif
diff --git a/src/libc/include/string.h b/src/libc/include/string.h
new file mode 100644
index 0000000..7efda4e
--- /dev/null
+++ b/src/libc/include/string.h
@@ -0,0 +1,12 @@
+#ifndef _STRING_H
+#define _STRING_H
+
+#include <stddef.h>
+
+int memcmp(const void *__s1, const void *__s2, size_t __n);
+void *memcpy(void *__dst, const void *__src, size_t __n);
+void *memset(void *__dst, int __c, size_t __n);
+char *strchr(const char *__s, int __n);
+size_t strlen(const char *__s);
+
+#endif
diff --git a/src/libc/src/memcmp.c b/src/libc/src/memcmp.c
new file mode 100644
index 0000000..f24d300
--- /dev/null
+++ b/src/libc/src/memcmp.c
@@ -0,0 +1,12 @@
+#include <string.h>
+
+int memcmp(const void *const s1, const void *const s2, const size_t n)
+{
+ const unsigned char *a = s1, *b = s2;
+
+ for (size_t i = 0; i < n; i++, a++, b++)
+ if (*a != *b)
+ return *a - *b;
+
+ return 0;
+}
diff --git a/src/libc/src/memcpy.c b/src/libc/src/memcpy.c
new file mode 100644
index 0000000..7014e63
--- /dev/null
+++ b/src/libc/src/memcpy.c
@@ -0,0 +1,13 @@
+#include <string.h>
+#include <stddef.h>
+
+void *memcpy(void *const dst, const void *const src, const size_t n)
+{
+ unsigned char *d = dst;
+ const unsigned char *s = src;
+
+ for (size_t i = 0; i < n; i++)
+ *d++ = *s++;
+
+ return dst;
+}
diff --git a/src/libc/src/memset.c b/src/libc/src/memset.c
new file mode 100644
index 0000000..d3f00c1
--- /dev/null
+++ b/src/libc/src/memset.c
@@ -0,0 +1,7 @@
+#include <string.h>
+#include <stddef.h>
+
+void *memset(void *const dst, const int c, const size_t n)
+{
+ return NULL;
+}
diff --git a/src/libc/src/strchr.c b/src/libc/src/strchr.c
new file mode 100644
index 0000000..3a1fd0b
--- /dev/null
+++ b/src/libc/src/strchr.c
@@ -0,0 +1,11 @@
+#include <string.h>
+#include <stddef.h>
+
+char *strchr(const char *s, const int n)
+{
+ for (; *s; s++)
+ if (*s == n)
+ return (char *)s;
+
+ return NULL;
+}
diff --git a/src/libc/src/strlen.c b/src/libc/src/strlen.c
new file mode 100644
index 0000000..43efcc5
--- /dev/null
+++ b/src/libc/src/strlen.c
@@ -0,0 +1,12 @@
+#include <string.h>
+#include <stddef.h>
+
+size_t strlen(const char *s)
+{
+ size_t ret = 0;
+
+ while (*s++)
+ ret++;
+
+ return ret;
+}
diff --git a/src/playstation.x b/src/playstation.x
new file mode 100644
index 0000000..00a1a74
--- /dev/null
+++ b/src/playstation.x
@@ -0,0 +1,72 @@
+/*
+ * Linker script to generate an ELF file
+ * that has to be converted to PS-X EXE.
+ */
+
+TARGET("elf32-littlemips")
+OUTPUT_ARCH("mips")
+ENTRY(_start)
+INPUT(-lgcc)
+
+SECTIONS
+{
+ . = 0x80010000;
+
+ .text ALIGN(4) :
+ {
+ *(.text.startup)
+ *(.text*)
+ }
+
+ .rodata ALIGN(4) :
+ {
+ *(.rodata)
+ }
+
+ .data ALIGN(4) :
+ {
+ *(.data)
+ }
+
+ .ctors ALIGN(4) :
+ {
+ *(.ctors)
+ }
+
+ .dtors ALIGN(4) :
+ {
+ *(.dtors)
+ }
+
+ .bss ALIGN(4) :
+ {
+ *(.bss)
+ *(.bss.*)
+ *(.gnu.linkonce.b.*)
+ *(COMMON)
+ *(.sbss)
+ *(.sbss.*)
+ *(.gnu.linkonce.sb.*)
+ *(.scommon)
+ }
+
+ __text_start = ADDR(.text);
+ __text_end = ADDR(.text) + SIZEOF(.text);
+
+ __rodata_start = ADDR(.rodata);
+ __rodata_end = ADDR(.rodata) + SIZEOF(.rodata);
+
+ __data_start = ADDR(.data);
+ __data_end = ADDR(.data) + SIZEOF(.data);
+
+ __ctor_list = ADDR(.ctors);
+ __ctor_end = ADDR(.ctors) + SIZEOF(.ctors);
+
+ __dtor_list = ADDR(.dtors);
+ __dtor_end = ADDR(.dtors) + SIZEOF(.dtors);
+
+ __bss_start = ADDR(.bss);
+ __bss_end = ADDR(.bss) + SIZEOF(.bss);
+
+ __scratchpad = 0x1f800000;
+}
diff --git a/src/start.c b/src/start.c
new file mode 100644
index 0000000..09ca6c9
--- /dev/null
+++ b/src/start.c
@@ -0,0 +1,10 @@
+__attribute__((__section__(".text.startup"))) void _start(void)
+{
+ int main(void);
+ extern char *__bss_start, *__bss_end;
+
+ for (char *s = __bss_start; s < __bss_end; s++)
+ *s = 0;
+
+ main();
+}