libc.obj: add call exit after main && add build for ctr0.o && use return instead exit in samples
Some checks failed
Build system / Build (pull_request) Failing after 1s
Build system / Check kernel codestyle (pull_request) Successful in 1m18s

нен работает
по стандартам после `main` должно быть закрыте всего, что закрывается в `exit`
ну терпите, crt увеличиласть на несколько байт
обертка для `exit` в crt нужна т.к. `exit` импортируется.

зачем вообще было держать бинарь `libc.obj/lib/crt0.o`, если абсолютно такой же лежит в `bin/lib/` всемсте `tcc`? Нет, зачем вообще тащить бинари в репку?
This commit is contained in:
2026-01-15 21:19:10 +05:00
parent 9a559fdae4
commit 15f27eb1c3
24 changed files with 108 additions and 34 deletions

View File

@@ -53,7 +53,10 @@ DLLAPI void free(void* ptr);
DLLAPI long int strtol(const char* str, char** endptr, int base);
DLLAPI void _exit(int status);
#ifndef _Exit
#define _Exit(status) _exit(status)
#endif
DLLAPI void abort();
DLLAPI void exit(int status);

View File

@@ -7,4 +7,6 @@ int main()
{
assert(a != b);
assert(a == b);
return 0;
}

View File

@@ -14,5 +14,5 @@ int main()
atexit(&f);
atexit(&f);
exit(0);
return 0;
}

View File

@@ -5,4 +5,6 @@ int main()
msgbox* msg1 = NULL;
msg1 = kolibri_new_msgbox("Title", "Text in window", 0, "Ok");
kolibri_start_msgbox(msg1, NULL);
return 0;
}

View File

@@ -18,4 +18,5 @@ int main()
}
(*con_exit)(0);
return 0;
}

View File

@@ -41,4 +41,6 @@ int main()
path = getcwd(NULL, PATH_MAX);
printf("Move to the directory: %s\n", path);
free(path);
return 0;
}

View File

@@ -47,4 +47,5 @@ int main(int argc, char** argv)
puts("TEST: FAIL!");
}
fclose(f);
return 0;
}

View File

@@ -55,5 +55,5 @@ int main()
close(sock);
puts("\n goodbye)\n");
exit(0);
return 0;
}

View File

@@ -86,5 +86,5 @@ int main()
printf(asctime(libc_tm));
puts("End testing.");
exit(0);
return 0;
}

View File

@@ -38,4 +38,5 @@ int main()
result = frexp(param, &n);
printf("%f = %f * 2^%d\n", param, result, n);
}
return 0;
}

View File

@@ -42,7 +42,7 @@ void create_thread(void)
printf("New thread created (TID=%u)\n", tid);
}
void main()
int main()
{
if (_ksys_posix_pipe2(pipefd, 0)) {
puts("Pipe creation error!");
@@ -51,4 +51,5 @@ void main()
printf("SEND: %s\n", send_message);
_ksys_posix_write(pipefd[1], send_message, MESSAGE_SIZE);
create_thread();
return 0;
}

View File

@@ -17,5 +17,6 @@ int main()
string[0] = shell_getc();
shell_printf("\n\rYou pressed: %c", string[0]);
shell_exit();
return 0;
}

View File

@@ -16,4 +16,5 @@ int main(int argc, char** argv)
printf("memcpy: Failure\n");
return -1;
}
return 0;
}

View File

@@ -69,4 +69,5 @@ int main()
break;
}
}
return 0;
}

View File

@@ -53,5 +53,6 @@ int main()
} else {
puts(tmpdisk_res_text[6]);
}
exit(0);
return 0;
}

View File

@@ -59,7 +59,7 @@ int main(int argc, char* argv[])
(*con_set_title)("Whois");
}
get_whois_data(domain, &data);
exit(0);
return 0;
}
/*

View File

@@ -1,4 +1,15 @@
if tup.getconfig("NO_TCC") ~= "" then return end
if tup.getconfig("NO_TCC") ~= "" then
return
end
function AddPrefix(prefix, t)
for i, v in pairs(t) do
t[i] = prefix .. v
end
return t
end
CC = "kos32-tcc"
CFLAGS = " -r -nostdinc -nostdlib -DGNUC -D_BUILD_LIBC -Wall -Werror"
INCLUDES = " -I../include"
@@ -29,14 +40,24 @@ GAS_SRC = {
"math/sqrt.s",
"math/tan.s",
"string/memset.s",
"string/memmove.s"
"string/memmove.s",
}
OBJS = {"libc.c"}
LIBC_OBJS = { "libc.c" }
tup.append_table(OBJS,
tup.foreach_rule(GAS_SRC, "as --32 %f -o %o", "%B.o")
)
tup.append_table(LIBC_OBJS, tup.foreach_rule(GAS_SRC, "as --32 %f -o %o", "%B.o"))
tup.rule(OBJS, "kos32-tcc" .. CFLAGS .. INCLUDES .. " %f -o %o " .. " && strip %o --strip-unneeded " , "libc.o")
tup.rule(LIBC_OBJS, CC .. CFLAGS .. INCLUDES .. " %f -o %o " .. " && strip %o --strip-unneeded ", "libc.o")
tup.rule("libc.o", "objconv -fcoff32 %f %o " .. tup.getconfig("KPACK_CMD"), "%B.obj")
CRT0_ASM_SRC = AddPrefix("crt/", {
"crt0.asm",
})
CRT_C_SRC = AddPrefix("crt/", {
"crt_exit.c",
})
tup.rule(CRT0_ASM_SRC, "fasm %f %o", "%B.obj")
tup.rule(CRT_C_SRC, CC .. " -c -nostdinc -nostdlib" .. " -I../include" .. " %f -o %o", "%B.o")
tup.rule({ "crt0.obj", "crt_exit.o" }, "ld -r %f -o %o -m elf_i386", "crt0.o")

View File

@@ -14,6 +14,7 @@ public start
public start as '_start'
extrn main
extrn crt_exit
include '../../../../../../proc32.inc'
include '../../../../../../macros.inc'
@@ -38,36 +39,36 @@ start:
call push_param
; retrieving parameters
mov esi, params
xor edx, edx ; dl - èä¸ò ïàðàìåòð(1) èëè ðàçäåëèòåëè(0)
; dh - ñèìâîë ñ êîòîðîãî íà÷àëñÿ ïàðàìåòð (1 êàâû÷êè, 0 îñòàëüíîå)
xor edx, edx ; dl - идёт параметр(1) или разделители(0)
; dh - символ с которого начался параметр (1 кавычки, 0 остальное)
mov ecx, 1 ; cl = 1
; ch = 0 ïðîñòî íîëü
; ch = 0 просто ноль
.parse:
lodsb
test al, al
jz .run
test dl, dl
jnz .findendparam
;{åñëè áûë ðàçäåëèòåëü
;{если был разделитель
cmp al, ' '
jz .parse ;çàãðóæåí ïðîáåë, ãðóçèì ñëåäóþùèé ñèìâîë
mov dl, cl ;íà÷èíàåòñÿ ïàðàìåòð
jz .parse ;загружен пробел, грузим следующий символ
mov dl, cl ;начинается параметр
cmp al, '"'
jz @f ;çàãðóæåíû êàâû÷êè
mov dh, ch ;ïàðàìåòð áåç êàâû÷åê
jz @f ;загружены кавычки
mov dh, ch ;параметр без кавычек
dec esi
call push_param
inc esi
jmp .parse
@@:
mov dh, cl ;ïàðàìåòð â êàâû÷åêàõ
call push_param ;åñëè íå ïðîáåë çíà÷èò íà÷èíàåòñÿ êàêîé òî ïàðàìåòð
jmp .parse ;åñëè áûë ðàçäåëèòåëü}
mov dh, cl ;параметр в кавычеках
call push_param ;если не пробел значит начинается какой то параметр
jmp .parse ;если был разделитель}
.findendparam:
test dh, dh
jz @f ; áåç êàâû÷åê
jz @f ; без кавычек
cmp al, '"'
jz .clear
jmp .parse
@@ -86,10 +87,9 @@ start:
push [argc]
call main
.exit:
xor eax,eax
dec eax
int 0x40
dd -1
push eax
call crt_exit
dd -1
.crash:
jmp .exit
;============================

View File

@@ -0,0 +1,6 @@
#include <stdlib.h>
void crt_exit(int status)
{
exit(status);
}

View File

@@ -4,13 +4,13 @@
void _exit(int status)
{
if(status && status != 128)
if(status && status != 128) // return error and this is not abort
{
printf("exit code: %d", status);
printf("exit code: %d\n", status);
}
if (__con_is_load) {
con_exit(status);
con_exit(0); // пусть удерживается, так даже лучше, можно вывод упесть проанализировать
}
_ksys_exit();
}

View File

@@ -1,12 +1,40 @@
/* Copyright (C) 2021 Logaev Maxim (turbocat2001), GPLv2 */
#include <stdlib.h>
#include <sys/ksys.h>
#include "_mem.h"
static void __close_all()
{
}
static void __delete_all_tmp()
{
}
static void __free_all_mem()
{
struct mem_node* current_node = __mem_node;
while (current_node != NULL)
{
struct mem_node* tmp = current_node;
current_node = current_node->next;
free(GET_MEM_NODE_PTR(tmp));
}
}
void exit(int status)
{
_ksys_debug_puts("exit\n");
__run_atexit();
// fflush & delete tmp files
__close_all();
__delete_all_tmp();
__free_all_mem();
_exit(status);
}

View File

@@ -70,3 +70,5 @@ void free(void* ptr)
}
}
}
#undef CHECK_SIDE_IN_OTHER_BLOCK