Skip to content

Commit

Permalink
Merge pull request #1210 from 64/entry
Browse files Browse the repository at this point in the history
sysdeps: simplify entry point
  • Loading branch information
Geertiebear authored Jan 2, 2025
2 parents 07f7cb1 + 00097cc commit 6bae03c
Show file tree
Hide file tree
Showing 13 changed files with 21 additions and 240 deletions.
3 changes: 2 additions & 1 deletion options/ansi/generic/locale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ namespace mlibc {
const locale_description *messages_facet;
}

void __mlibc_initLocale() {
[[gnu::constructor]]
static void init_locale() {
mlibc::collate_facet = &mlibc::c_locale;
mlibc::ctype_facet = &mlibc::c_locale;
mlibc::monetary_facet = &mlibc::c_locale;
Expand Down
27 changes: 7 additions & 20 deletions options/elf/generic/startup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,18 @@ extern "C" size_t __init_array_end[];
extern "C" size_t __preinit_array_start[];
extern "C" size_t __preinit_array_end[];

static int constructors_ran_already = 0;
extern "C" uintptr_t *__dlapi_entrystack();

struct global_constructor_guard {
global_constructor_guard() {
constructors_ran_already = 1;
}
};
namespace mlibc {

static global_constructor_guard g;
exec_stack_data entry_stack;

void __mlibc_run_constructors() {
/*
if (!constructors_ran_already) {
size_t constructor_count = (size_t)__init_array_end - (size_t)__init_array_start;
constructor_count /= sizeof(void*);
for (size_t i = 0; i < constructor_count; i++) {
void (*ptr)(void) = (void(*)(void))(__init_array_start[i]);
ptr();
}
}
*/
[[gnu::constructor]]
void init_libc() {
mlibc::parse_exec_stack(__dlapi_entrystack(), &entry_stack);
mlibc::set_startup_data(entry_stack.argc, entry_stack.argv, entry_stack.envp);
}

namespace mlibc {

void parse_exec_stack(void *opaque_sp, exec_stack_data *data) {
auto sp = reinterpret_cast<uintptr_t *>(opaque_sp);
data->argc = *sp++;
Expand Down
8 changes: 2 additions & 6 deletions options/elf/include/mlibc/elf/startup.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
#ifndef MLIBC_ELF_STARTUP
#define MLIBC_ELF_STARTUP

#ifndef __MLIBC_ABI_ONLY

void __mlibc_run_constructors();

#endif /* !__MLIBC_ABI_ONLY */

namespace mlibc {

struct exec_stack_data {
Expand All @@ -15,6 +9,8 @@ struct exec_stack_data {
char **envp;
};

extern exec_stack_data entry_stack;

#ifndef __MLIBC_ABI_ONLY

void parse_exec_stack(void *sp, exec_stack_data *data);
Expand Down
21 changes: 1 addition & 20 deletions sysdeps/aero/generic/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,15 @@
#include <stdint.h>
#include <stdlib.h>

// defined by the POSIX library
void __mlibc_initLocale();

extern "C" uintptr_t *__dlapi_entrystack();

extern char **environ;
static mlibc::exec_stack_data __mlibc_stack_data;

struct LibraryGuard {
LibraryGuard();
};

static LibraryGuard guard;

LibraryGuard::LibraryGuard() {
__mlibc_initLocale();

// Parse the exec() stack.
mlibc::parse_exec_stack(__dlapi_entrystack(), &__mlibc_stack_data);
mlibc::set_startup_data(__mlibc_stack_data.argc, __mlibc_stack_data.argv,
__mlibc_stack_data.envp);
}

extern "C" void __mlibc_entry(int (*main_fn)(int argc, char *argv[],
char *env[])) {
// TODO: call __dlapi_enter, otherwise static builds will break (see Linux
// sysdeps)
auto result =
main_fn(__mlibc_stack_data.argc, __mlibc_stack_data.argv, environ);
main_fn(mlibc::entry_stack.argc, mlibc::entry_stack.argv, environ);
exit(result);
}
22 changes: 1 addition & 21 deletions sysdeps/astral/generic/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,13 @@
#include <bits/ensure.h>
#include <mlibc/elf/startup.h>

// defined by the POSIX library
void __mlibc_initLocale();

extern "C" uintptr_t *__dlapi_entrystack();
extern "C" void __dlapi_enter(uintptr_t *);

extern char **environ;
static mlibc::exec_stack_data __mlibc_stack_data;

struct LibraryGuard {
LibraryGuard();
};

static LibraryGuard guard;

LibraryGuard::LibraryGuard() {
__mlibc_initLocale();

// Parse the exec() stack.
mlibc::parse_exec_stack(__dlapi_entrystack(), &__mlibc_stack_data);
mlibc::set_startup_data(__mlibc_stack_data.argc, __mlibc_stack_data.argv,
__mlibc_stack_data.envp);
}

extern "C" void __mlibc_entry(int (*main_fn)(int argc, char *argv[], char *env[]), uintptr_t *entry_stack) {
__dlapi_enter(entry_stack);
auto result = main_fn(__mlibc_stack_data.argc, __mlibc_stack_data.argv, environ);
auto result = main_fn(mlibc::entry_stack.argc, mlibc::entry_stack.argv, environ);
exit(result);
}

23 changes: 1 addition & 22 deletions sysdeps/dripos/generic/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,11 @@
#include <bits/ensure.h>
#include <mlibc/elf/startup.h>

// defined by the POSIX library
void __mlibc_initLocale();

extern "C" uintptr_t *__dlapi_entrystack();

extern char **environ;
static mlibc::exec_stack_data __mlibc_stack_data;

struct LibraryGuard {
LibraryGuard();
};

static LibraryGuard guard;

LibraryGuard::LibraryGuard() {
__mlibc_initLocale();

// Parse the exec() stack.
mlibc::parse_exec_stack(__dlapi_entrystack(), &__mlibc_stack_data);
mlibc::set_startup_data(__mlibc_stack_data.argc, __mlibc_stack_data.argv,
__mlibc_stack_data.envp);
}

extern "C" void __mlibc_entry(int (*main_fn)(int argc, char *argv[], char *env[])) {
// TODO: call __dlapi_enter, otherwise static builds will break (see Linux sysdeps)
auto result = main_fn(__mlibc_stack_data.argc, __mlibc_stack_data.argv, environ);
auto result = main_fn(mlibc::entry_stack.argc, mlibc::entry_stack.argv, environ);
exit(result);
}

22 changes: 1 addition & 21 deletions sysdeps/ironclad/generic/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,14 @@
#include <bits/ensure.h>
#include <mlibc/elf/startup.h>

// defined by the POSIX library
void __mlibc_initLocale();

extern "C" uintptr_t *__dlapi_entrystack();
extern "C" void __dlapi_enter(uintptr_t *);

extern char **environ;
static mlibc::exec_stack_data __mlibc_stack_data;

struct LibraryGuard {
LibraryGuard();
};

static LibraryGuard guard;

LibraryGuard::LibraryGuard() {
__mlibc_initLocale();

// Parse the exec() stack.
mlibc::parse_exec_stack(__dlapi_entrystack(), &__mlibc_stack_data);
mlibc::set_startup_data(__mlibc_stack_data.argc, __mlibc_stack_data.argv,
__mlibc_stack_data.envp);
}

extern "C" void __mlibc_entry(uintptr_t *entry_stack, int (*main_fn)(int argc, char *argv[], char *env[])) {
__dlapi_enter(entry_stack);

auto result = main_fn(__mlibc_stack_data.argc, __mlibc_stack_data.argv, environ);
auto result = main_fn(mlibc::entry_stack.argc, mlibc::entry_stack.argv, environ);
exit(result);
}

24 changes: 1 addition & 23 deletions sysdeps/keyronex/generic/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,7 @@
#include <stdint.h>
#include <stdlib.h>

// defined by the POSIX library
void __mlibc_initLocale();

extern "C" uintptr_t *__dlapi_entrystack();

extern char **environ;
static mlibc::exec_stack_data __mlibc_stack_data;

struct LibraryGuard {
LibraryGuard();
};

static LibraryGuard guard;

LibraryGuard::LibraryGuard()
{
__mlibc_initLocale();

// Parse the exec() stack.
mlibc::parse_exec_stack(__dlapi_entrystack(), &__mlibc_stack_data);
mlibc::set_startup_data(__mlibc_stack_data.argc,
__mlibc_stack_data.argv, __mlibc_stack_data.envp);
}

namespace mlibc {
int
Expand Down Expand Up @@ -102,7 +80,7 @@ __mlibc_entry(int (*main_fn)(int argc, char *argv[], char *env[]))

// TODO: call __dlapi_enter, otherwise static builds will break (see
// Linux sysdeps)
auto result = main_fn(__mlibc_stack_data.argc, __mlibc_stack_data.argv,
auto result = main_fn(mlibc::entry_stack.argc, mlibc::entry_stack.argv,
environ);
exit(result);
}
23 changes: 1 addition & 22 deletions sysdeps/lemon/generic/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,10 @@
#include <bits/ensure.h>
#include <mlibc/elf/startup.h>

// defined by the POSIX library
void __mlibc_initLocale();

extern "C" uintptr_t *__dlapi_entrystack();

extern char **environ;
static mlibc::exec_stack_data __mlibc_stack_data;

struct LibraryGuard {
LibraryGuard();
};

static LibraryGuard guard;

LibraryGuard::LibraryGuard() {
__mlibc_initLocale();

// Parse the exec() stack.
mlibc::parse_exec_stack(__dlapi_entrystack(), &__mlibc_stack_data);
mlibc::set_startup_data(__mlibc_stack_data.argc, __mlibc_stack_data.argv,
__mlibc_stack_data.envp);
}

extern "C" void __mlibc_entry(int (*main_fn)(int argc, char *argv[], char *env[])) {
// TODO: call __dlapi_enter, otherwise static builds will break (see Linux sysdeps)
auto result = main_fn(__mlibc_stack_data.argc, __mlibc_stack_data.argv, environ);
auto result = main_fn(mlibc::entry_stack.argc, mlibc::entry_stack.argv, environ);
exit(result);
}
22 changes: 1 addition & 21 deletions sysdeps/linux/generic/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,15 @@
#include <mlibc/elf/startup.h>
#include <sys/auxv.h>

// defined by the POSIX library
void __mlibc_initLocale();

extern "C" uintptr_t *__dlapi_entrystack();
extern "C" void __dlapi_enter(uintptr_t *);

extern char **environ;
static mlibc::exec_stack_data __mlibc_stack_data;

size_t __hwcap;

struct LibraryGuard {
LibraryGuard();
};

static LibraryGuard guard;

LibraryGuard::LibraryGuard() {
__mlibc_initLocale();

// Parse the exec() stack.
mlibc::parse_exec_stack(__dlapi_entrystack(), &__mlibc_stack_data);
mlibc::set_startup_data(__mlibc_stack_data.argc, __mlibc_stack_data.argv,
__mlibc_stack_data.envp);
}

extern "C" void __mlibc_entry(uintptr_t *entry_stack, int (*main_fn)(int argc, char *argv[], char *env[])) {
__dlapi_enter(entry_stack);
__hwcap = getauxval(AT_HWCAP);
auto result = main_fn(__mlibc_stack_data.argc, __mlibc_stack_data.argv, environ);
auto result = main_fn(mlibc::entry_stack.argc, mlibc::entry_stack.argv, environ);
exit(result);
}
22 changes: 1 addition & 21 deletions sysdeps/lyre/generic/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,9 @@
#include <bits/posix/posix_signal.h>
#include <lyre/syscall.h>

// defined by the POSIX library
void __mlibc_initLocale();

extern "C" uintptr_t *__dlapi_entrystack();
extern "C" void __dlapi_enter(uintptr_t *);

extern char **environ;
static mlibc::exec_stack_data __mlibc_stack_data;

struct LibraryGuard {
LibraryGuard();
};

static LibraryGuard guard;

LibraryGuard::LibraryGuard() {
__mlibc_initLocale();

// Parse the exec() stack.
mlibc::parse_exec_stack(__dlapi_entrystack(), &__mlibc_stack_data);
mlibc::set_startup_data(__mlibc_stack_data.argc, __mlibc_stack_data.argv,
__mlibc_stack_data.envp);
}

struct GPRState {
uint64_t ds;
Expand Down Expand Up @@ -116,7 +96,7 @@ extern "C" void __mlibc_entry(uintptr_t *entry_stack, int (*main_fn)(int argc, c

//mlibc::sys_sigentry((void *)__mlibc_sigentry);

auto result = main_fn(__mlibc_stack_data.argc, __mlibc_stack_data.argv, environ);
auto result = main_fn(mlibc::entry_stack.argc, mlibc::entry_stack.argv, environ);
exit(result);
}

22 changes: 1 addition & 21 deletions sysdeps/managarm/generic/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
#include <protocols/posix/data.hpp>
#include <protocols/posix/supercalls.hpp>

// defined by the POSIX library
void __mlibc_initLocale();

extern "C" uintptr_t *__dlapi_entrystack();
extern "C" void __dlapi_enter(uintptr_t *);

Expand Down Expand Up @@ -107,28 +104,11 @@ HelHandle getHandleForFd(int fd) {

void clearCachedInfos() { has_cached_infos = PTHREAD_ONCE_INIT; }

struct LibraryGuard {
LibraryGuard();
};

static LibraryGuard guard;

extern char **environ;
static mlibc::exec_stack_data __mlibc_stack_data;

LibraryGuard::LibraryGuard() {
__mlibc_initLocale();

// Parse the exec() stack.
mlibc::parse_exec_stack(__dlapi_entrystack(), &__mlibc_stack_data);
mlibc::set_startup_data(
__mlibc_stack_data.argc, __mlibc_stack_data.argv, __mlibc_stack_data.envp
);
}

extern "C" void
__mlibc_entry(uintptr_t *entry_stack, int (*main_fn)(int argc, char *argv[], char *env[])) {
__dlapi_enter(entry_stack);
auto result = main_fn(__mlibc_stack_data.argc, __mlibc_stack_data.argv, environ);
auto result = main_fn(mlibc::entry_stack.argc, mlibc::entry_stack.argv, environ);
exit(result);
}
Loading

0 comments on commit 6bae03c

Please sign in to comment.