diff --git a/programs/develop/ktcc/trunk/bin/lib/crt0.o b/programs/develop/ktcc/trunk/bin/lib/crt0.o index b857c8056..308ccd8bf 100644 Binary files a/programs/develop/ktcc/trunk/bin/lib/crt0.o and b/programs/develop/ktcc/trunk/bin/lib/crt0.o differ diff --git a/programs/develop/ktcc/trunk/libc.obj/include/stdlib.h b/programs/develop/ktcc/trunk/libc.obj/include/stdlib.h index c65fca478..dd9438e74 100644 --- a/programs/develop/ktcc/trunk/libc.obj/include/stdlib.h +++ b/programs/develop/ktcc/trunk/libc.obj/include/stdlib.h @@ -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); diff --git a/programs/develop/ktcc/trunk/libc.obj/lib/crt0.o b/programs/develop/ktcc/trunk/libc.obj/lib/crt0.o deleted file mode 100644 index 598dbd5fc..000000000 Binary files a/programs/develop/ktcc/trunk/libc.obj/lib/crt0.o and /dev/null differ diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/assert_test.c b/programs/develop/ktcc/trunk/libc.obj/samples/assert_test.c index 99f47d7e9..22ce303a8 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/assert_test.c +++ b/programs/develop/ktcc/trunk/libc.obj/samples/assert_test.c @@ -7,4 +7,6 @@ int main() { assert(a != b); assert(a == b); + + return 0; } \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/atexit_test.c b/programs/develop/ktcc/trunk/libc.obj/samples/atexit_test.c index 63f72bcf9..3bf8609d0 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/atexit_test.c +++ b/programs/develop/ktcc/trunk/libc.obj/samples/atexit_test.c @@ -14,5 +14,5 @@ int main() atexit(&f); atexit(&f); - exit(0); + return 0; } diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/clayer/msgbox.c b/programs/develop/ktcc/trunk/libc.obj/samples/clayer/msgbox.c index 0c8890cf8..1604cb05e 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/clayer/msgbox.c +++ b/programs/develop/ktcc/trunk/libc.obj/samples/clayer/msgbox.c @@ -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; } diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/consoleio.c b/programs/develop/ktcc/trunk/libc.obj/samples/consoleio.c index 9be077b67..ae4cd88ad 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/consoleio.c +++ b/programs/develop/ktcc/trunk/libc.obj/samples/consoleio.c @@ -18,4 +18,5 @@ int main() } (*con_exit)(0); + return 0; } \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/dir_example.c b/programs/develop/ktcc/trunk/libc.obj/samples/dir_example.c index 3cbbd9aa4..6ade1e3af 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/dir_example.c +++ b/programs/develop/ktcc/trunk/libc.obj/samples/dir_example.c @@ -41,4 +41,6 @@ int main() path = getcwd(NULL, PATH_MAX); printf("Move to the directory: %s\n", path); free(path); + + return 0; } diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/file_io.c b/programs/develop/ktcc/trunk/libc.obj/samples/file_io.c index deaac4102..0a8622835 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/file_io.c +++ b/programs/develop/ktcc/trunk/libc.obj/samples/file_io.c @@ -47,4 +47,5 @@ int main(int argc, char** argv) puts("TEST: FAIL!"); } fclose(f); + return 0; } diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/http_tcp_demo.c b/programs/develop/ktcc/trunk/libc.obj/samples/http_tcp_demo.c index 199033898..c4b218fad 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/http_tcp_demo.c +++ b/programs/develop/ktcc/trunk/libc.obj/samples/http_tcp_demo.c @@ -55,5 +55,5 @@ int main() close(sock); puts("\n goodbye)\n"); - exit(0); + return 0; } diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/libc_test.c b/programs/develop/ktcc/trunk/libc.obj/samples/libc_test.c index 090305f19..49221c9f2 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/libc_test.c +++ b/programs/develop/ktcc/trunk/libc.obj/samples/libc_test.c @@ -86,5 +86,5 @@ int main() printf(asctime(libc_tm)); puts("End testing."); - exit(0); + return 0; } diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/math_test.c b/programs/develop/ktcc/trunk/libc.obj/samples/math_test.c index 4dbdc9b89..91d975894 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/math_test.c +++ b/programs/develop/ktcc/trunk/libc.obj/samples/math_test.c @@ -38,4 +38,5 @@ int main() result = frexp(param, &n); printf("%f = %f * 2^%d\n", param, result, n); } + return 0; } diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/pipe.c b/programs/develop/ktcc/trunk/libc.obj/samples/pipe.c index 36da4e097..91634f3b7 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/pipe.c +++ b/programs/develop/ktcc/trunk/libc.obj/samples/pipe.c @@ -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; } diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/shell_test.c b/programs/develop/ktcc/trunk/libc.obj/samples/shell_test.c index 84bc4caac..ecce372d6 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/shell_test.c +++ b/programs/develop/ktcc/trunk/libc.obj/samples/shell_test.c @@ -17,5 +17,6 @@ int main() string[0] = shell_getc(); shell_printf("\n\rYou pressed: %c", string[0]); shell_exit(); + return 0; } \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/string_test.c b/programs/develop/ktcc/trunk/libc.obj/samples/string_test.c index 33cca8f18..064aedc8b 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/string_test.c +++ b/programs/develop/ktcc/trunk/libc.obj/samples/string_test.c @@ -16,4 +16,5 @@ int main(int argc, char** argv) printf("memcpy: Failure\n"); return -1; } + return 0; } diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/thread_work.c b/programs/develop/ktcc/trunk/libc.obj/samples/thread_work.c index 48ce87d32..be6b9c4cc 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/thread_work.c +++ b/programs/develop/ktcc/trunk/libc.obj/samples/thread_work.c @@ -69,4 +69,5 @@ int main() break; } } + return 0; } diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/tmpdisk_work.c b/programs/develop/ktcc/trunk/libc.obj/samples/tmpdisk_work.c index 831df63d7..c8ad955a3 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/tmpdisk_work.c +++ b/programs/develop/ktcc/trunk/libc.obj/samples/tmpdisk_work.c @@ -53,5 +53,6 @@ int main() } else { puts(tmpdisk_res_text[6]); } - exit(0); + + return 0; } diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/whois.c b/programs/develop/ktcc/trunk/libc.obj/samples/whois.c index 477972826..ffaffae64 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/whois.c +++ b/programs/develop/ktcc/trunk/libc.obj/samples/whois.c @@ -59,7 +59,7 @@ int main(int argc, char* argv[]) (*con_set_title)("Whois"); } get_whois_data(domain, &data); - exit(0); + return 0; } /* diff --git a/programs/develop/ktcc/trunk/libc.obj/source/Tupfile.lua b/programs/develop/ktcc/trunk/libc.obj/source/Tupfile.lua index ed6ab9561..7cd8a1435 100755 --- a/programs/develop/ktcc/trunk/libc.obj/source/Tupfile.lua +++ b/programs/develop/ktcc/trunk/libc.obj/source/Tupfile.lua @@ -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") diff --git a/programs/develop/ktcc/trunk/libc.obj/source/crt/crt0.asm b/programs/develop/ktcc/trunk/libc.obj/source/crt/crt0.asm index 596fba564..bbb494f1d 100644 --- a/programs/develop/ktcc/trunk/libc.obj/source/crt/crt0.asm +++ b/programs/develop/ktcc/trunk/libc.obj/source/crt/crt0.asm @@ -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 ;============================ diff --git a/programs/develop/ktcc/trunk/libc.obj/source/crt/crt_exit.c b/programs/develop/ktcc/trunk/libc.obj/source/crt/crt_exit.c new file mode 100644 index 000000000..f7888f2f2 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc.obj/source/crt/crt_exit.c @@ -0,0 +1,6 @@ +#include + +void crt_exit(int status) +{ + exit(status); +} diff --git a/programs/develop/ktcc/trunk/libc.obj/source/stdlib/_exit.c b/programs/develop/ktcc/trunk/libc.obj/source/stdlib/_exit.c index 5ec2e14f6..5c9fc2439 100644 --- a/programs/develop/ktcc/trunk/libc.obj/source/stdlib/_exit.c +++ b/programs/develop/ktcc/trunk/libc.obj/source/stdlib/_exit.c @@ -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(); } diff --git a/programs/develop/ktcc/trunk/libc.obj/source/stdlib/exit.c b/programs/develop/ktcc/trunk/libc.obj/source/stdlib/exit.c index 116dd8131..b1f5542a8 100644 --- a/programs/develop/ktcc/trunk/libc.obj/source/stdlib/exit.c +++ b/programs/develop/ktcc/trunk/libc.obj/source/stdlib/exit.c @@ -1,12 +1,40 @@ /* Copyright (C) 2021 Logaev Maxim (turbocat2001), GPLv2 */ +#include #include +#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); } diff --git a/programs/develop/ktcc/trunk/libc.obj/source/stdlib/free.c b/programs/develop/ktcc/trunk/libc.obj/source/stdlib/free.c index f792b30d8..5b8509569 100644 --- a/programs/develop/ktcc/trunk/libc.obj/source/stdlib/free.c +++ b/programs/develop/ktcc/trunk/libc.obj/source/stdlib/free.c @@ -70,3 +70,5 @@ void free(void* ptr) } } } + +#undef CHECK_SIDE_IN_OTHER_BLOCK