diff --git a/libraries/config/templates/stratosphere.mk b/libraries/config/templates/stratosphere.mk index 6f4395b1f..fe7964907 100644 --- a/libraries/config/templates/stratosphere.mk +++ b/libraries/config/templates/stratosphere.mk @@ -34,7 +34,8 @@ export CXXWRAPS := -Wl,--wrap,__cxa_pure_virtual \ -Wl,--wrap,_Unwind_Resume \ -Wl,--wrap,_ZSt19__throw_logic_errorPKc \ -Wl,--wrap,_ZSt20__throw_length_errorPKc \ - -Wl,--wrap,_ZNSt11logic_errorC2EPKc + -Wl,--wrap,_ZNSt11logic_errorC2EPKc \ + -Wl,--wrap,exit export LDFLAGS = -specs=$(ATMOSPHERE_LIBRARIES_DIR)/libstratosphere/stratosphere.specs -specs=$(DEVKITPRO)/libnx/switch.specs $(SETTINGS) $(CXXWRAPS) -Wl,-Map,$(notdir $*.map) diff --git a/libraries/libstratosphere/Makefile b/libraries/libstratosphere/Makefile index 20e0c6c7e..cca5aba50 100644 --- a/libraries/libstratosphere/Makefile +++ b/libraries/libstratosphere/Makefile @@ -124,6 +124,7 @@ $(OFILES_SRC) : $(HFILES_BIN) ams_environment_weak.o: CXXFLAGS += -fno-lto pm_info_api_weak.o: CXXFLAGS += -fno-lto +hos_stratosphere_api.o: CXXFLAGS += -fno-lto #--------------------------------------------------------------------------------- %_bin.h %.bin.o : %.bin diff --git a/libraries/libstratosphere/source/ams/ams_environment_weak.cpp b/libraries/libstratosphere/source/ams/ams_environment_weak.cpp index 89b68067a..284a725ea 100644 --- a/libraries/libstratosphere/source/ams/ams_environment_weak.cpp +++ b/libraries/libstratosphere/source/ams/ams_environment_weak.cpp @@ -15,6 +15,8 @@ */ #include +extern "C" void NORETURN __real_exit(int rc); + namespace ams { WEAK_SYMBOL void *Malloc(size_t size) { @@ -37,6 +39,11 @@ namespace ams { return std::free(ptr); } + WEAK_SYMBOL void NORETURN Exit(int rc) { + __real_exit(rc); + __builtin_unreachable(); + } + } extern "C" { @@ -46,4 +53,6 @@ extern "C" { WRAP_ABORT_FUNC(__cxa_pure_virtual) #undef WRAP_ABORT_FUNC + void NORETURN __wrap_exit(int rc) { ::ams::Exit(rc); __builtin_unreachable(); } + } diff --git a/libraries/libstratosphere/source/hos/hos_stratosphere_api.cpp b/libraries/libstratosphere/source/hos/hos_stratosphere_api.cpp index 092211ffb..10af44f29 100644 --- a/libraries/libstratosphere/source/hos/hos_stratosphere_api.cpp +++ b/libraries/libstratosphere/source/hos/hos_stratosphere_api.cpp @@ -15,6 +15,7 @@ */ #include #include "hos_version_api_private.hpp" +#include "../os/impl/os_rng_manager.hpp" namespace ams::os { @@ -22,6 +23,15 @@ namespace ams::os { } +extern "C" { + + /* Provide libnx address space allocation shim. */ + uintptr_t __libnx_virtmem_rng(void) { + return static_cast(::ams::os::impl::GetRngManager().GenerateRandomU64()); + } + +} + namespace ams::hos { void InitializeForStratosphere() { diff --git a/libraries/libstratosphere/source/os/impl/os_rng_manager_impl.hpp b/libraries/libstratosphere/source/os/impl/os_rng_manager_impl.hpp index b782edde3..b981c39d2 100644 --- a/libraries/libstratosphere/source/os/impl/os_rng_manager_impl.hpp +++ b/libraries/libstratosphere/source/os/impl/os_rng_manager_impl.hpp @@ -21,12 +21,12 @@ namespace ams::os::impl { class RngManager { private: util::TinyMT mt; - os::Mutex lock; + os::SdkMutex lock; bool initialized; private: void Initialize(); public: - constexpr RngManager() : mt(), lock(false), initialized() { /* ... */ } + constexpr RngManager() : mt(), lock(), initialized() { /* ... */ } public: u64 GenerateRandomU64(); }; diff --git a/libraries/libstratosphere/source/os/os_random.cpp b/libraries/libstratosphere/source/os/os_random.cpp index b0bc60c54..1368365f4 100644 --- a/libraries/libstratosphere/source/os/os_random.cpp +++ b/libraries/libstratosphere/source/os/os_random.cpp @@ -20,9 +20,9 @@ namespace ams::os { namespace { - util::TinyMT g_random; - os::Mutex g_random_mutex(false); - bool g_initialized_random; + constinit util::TinyMT g_random; + constinit os::SdkMutex g_random_mutex; + constinit bool g_initialized_random; template inline T GenerateRandomTImpl(T max) { diff --git a/stratosphere/sm/source/sm_main.cpp b/stratosphere/sm/source/sm_main.cpp index f72b2318a..b30f9a7a4 100644 --- a/stratosphere/sm/source/sm_main.cpp +++ b/stratosphere/sm/source/sm_main.cpp @@ -19,6 +19,9 @@ extern "C" { extern u32 __start__; + extern int __system_argc; + extern char** __system_argv; + u32 __nx_applet_type = AppletType_None; #define INNER_HEAP_SIZE 0x0 @@ -26,6 +29,7 @@ extern "C" { char nx_inner_heap[INNER_HEAP_SIZE]; void __libnx_initheap(void); + void argvSetup(void); void __appInit(void); void __appExit(void); @@ -39,10 +43,20 @@ extern "C" { void __libnx_free(void *mem); } +namespace { + + constinit char *g_empty_argv = nullptr; + +} + namespace ams { ncm::ProgramId CurrentProgramId = ncm::SystemProgramId::Sm; + void NORETURN Exit(int rc) { + AMS_ABORT("Exit called by immortal process"); + } + } using namespace ams; @@ -64,6 +78,12 @@ void __libnx_initheap(void) { fake_heap_end = (char*)addr + size; } +void argvSetup(void) { + /* We don't need argc/argv, so set them to empty defaults. */ + __system_argc = 0; + __system_argv = std::addressof(g_empty_argv); +} + void __appInit(void) { hos::InitializeForStratosphere();