66 lines
2.0 KiB
C
66 lines
2.0 KiB
C
|
// Copyright (c) 2025, Zak Fenton
|
||
|
// Zak Fenton's libc is licensed under the Mulan PSL v2. You can use this
|
||
|
// software according to the terms and conditions of the Mulan PSL v2.
|
||
|
// You may obtain a copy of Mulan PSL v2 at:
|
||
|
// http://license.coscl.org.cn/MulanPSL2
|
||
|
// THIS SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, WITHOUT warranties of
|
||
|
// any kind, either express or implied, including but not limited to
|
||
|
// non-infringement, merchantability or fit for a particular purpose.
|
||
|
// See the Mulan PSL v2 for more details.
|
||
|
|
||
|
#include "internal.h"
|
||
|
#include "simgc.h"
|
||
|
|
||
|
int main(int argc, char** argv, char** envv); // Note, the third argument is usually not implemented by the main function.
|
||
|
|
||
|
void _libc_malloc_init(void* stackptr);
|
||
|
void _libc_env_init(const char** envv);
|
||
|
void _libc_malloc_finish();
|
||
|
void _libc_stdio_init();
|
||
|
|
||
|
void _libc_start(int argc, char** argv, char** envv) {
|
||
|
_libc_env_init(envv);
|
||
|
_libc_malloc_init(&argc);
|
||
|
_libc_stdio_init();
|
||
|
/*
|
||
|
for (int i = 0; i < argc; i++) {
|
||
|
write(1, "ARG: ", 5);
|
||
|
write(1, argv[i], strlen(argv[i]));
|
||
|
write(1, "\n", 1);
|
||
|
}
|
||
|
for (int i = 0; envv[i]; i++) {
|
||
|
write(1, "ENV: ", 5);
|
||
|
write(1, envv[i], strlen(envv[i]));
|
||
|
write(1, "\n", 1);
|
||
|
}
|
||
|
*/
|
||
|
int result = main(argc, argv, envv);
|
||
|
_libc_malloc_finish();
|
||
|
exit(result);
|
||
|
FATAL("Faulty 'exit' syscall");
|
||
|
}
|
||
|
|
||
|
/* This is used to report missing functions or other critical errors, so
|
||
|
* it shouldn't use any additional library functions it should just
|
||
|
* print the error and exit using the simplest possible code path.
|
||
|
*/
|
||
|
void _libc_fatal(const char* msg, const char* fnc) {
|
||
|
const char* l1 = "FATAL ERROR:\n";
|
||
|
const char* l3 = "FUNCTION:\n";
|
||
|
write(2, l1, strlen(l1));
|
||
|
write(2, "\t", 1);
|
||
|
write(2, msg, strlen(msg));
|
||
|
write(2, "\n", 1);
|
||
|
write(2, l3, strlen(l3));
|
||
|
write(2, "\t", 1);
|
||
|
write(2, fnc, strlen(fnc));
|
||
|
write(2, "\n", 1);
|
||
|
// TODO: Trigger debugger here?
|
||
|
exit(-1);
|
||
|
l1 = "FAULTY EXIT? HOT LOOP FOREVER";
|
||
|
write(2, l1, strlen(l1));
|
||
|
while(1) {
|
||
|
// Hot loop forever
|
||
|
}
|
||
|
}
|