libc.obj: update exit #340

Open
Egor00f wants to merge 5 commits from Egor00f/kolibrios:libc.ob--update-exit into main
12 changed files with 164 additions and 23 deletions

View File

@@ -47,11 +47,13 @@ vsscanf
ungetc
;____STDLIB____
abs
abort
atoi
atol
atoll
atof
calloc
_exit
exit
free
itoa

View File

@@ -0,0 +1,4 @@
.tup
*.o
*.obj
*.kex

View File

@@ -48,7 +48,13 @@ 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 exit(int status);
DLLAPI void abort();
DLLAPI void srand(unsigned s);
DLLAPI int rand(void);

View File

@@ -27,7 +27,8 @@ BIN = \
libc_test.kex \
pipe.kex \
defgen.kex \
futex.kex
futex.kex \
atexit_test.kex
all: $(BIN)

View File

@@ -0,0 +1,18 @@
#include <stdlib.h>
Review

Please add copyright:

/*
* SPDX-License-Identifier: GPL-2.0-only
* Copyright (C) 2026 KolibriOS team
*/
Please add copyright: ```c /* * SPDX-License-Identifier: GPL-2.0-only * Copyright (C) 2026 KolibriOS team */ ```
#include <stdio.h>
void f()
{
static int c = 1;
printf("exit #%d\n", c);
c++;
}
int main()
{
atexit(&f);
atexit(&f);
atexit(&f);
return 0;
}

View File

@@ -23,5 +23,6 @@ cp clayer/logo.png /tmp0/1/tcc_samples/logo.png
../tcc defgen.c -o /tmp0/1/tcc_samples/defgen
../tcc pipe.c -o /tmp0/1/tcc_samples/pipe
../tcc futex.c -o /tmp0/1/tcc_samples/futex
../tcc atexit_test.c -o /tmp0/1/tcc_samples/atexit_test
"/sys/File managers/Eolite" /tmp0/1/tcc_samples
exit

View File

@@ -14,6 +14,7 @@ public start
public start as '_start'
extrn main
extrn 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 - is it a parameter (1) or delimiters (0)
; dh - character with which the parameter started (1 quotes, 0 everything else)
mov ecx, 1 ; cl = 1
; ch = 0 ïðîñòî íîëü
; ch = 0 just zero
.parse:
lodsb
test al, al
jz .run
test dl, dl
jnz .findendparam
;{åñëè áûë ðàçäåëèòåëü
;{if it was a delimiter
cmp al, ' '
jz .parse ;çàãðóæåí ïðîáåë, ãðóçèì ñëåäóþùèé ñèìâîë
mov dl, cl ;íà÷èíàåòñÿ ïàðàìåòð
jz .parse ; space loaded, load next character
mov dl, cl ; parameter starts
cmp al, '"'
jz @f ;çàãðóæåíû êàâû÷êè
mov dh, ch ;ïàðàìåòð áåç êàâû÷åê
jz @f ; quotes loaded
mov dh, ch ; parameter without quotes
Outdated
Review

Please fix indent

Please fix indent
dec esi
call push_param
inc esi
jmp .parse
@@:
mov dh, cl ;ïàðàìåòð â êàâû÷åêàõ
call push_param ;åñëè íå ïðîáåë çíà÷èò íà÷èíàåòñÿ êàêîé òî ïàðàìåòð
jmp .parse ;åñëè áûë ðàçäåëèòåëü}
mov dh, cl ; parameter in quotes
call push_param ; if not a space, then some parameter starts
jmp .parse ; if it was a delimiter}
.findendparam:
test dh, dh
jz @f ; áåç êàâû÷åê
jz @f ; without quotes
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 dword [exit]
dd -1
.crash:
jmp .exit
;============================

View File

@@ -82,6 +82,7 @@
#include "string/strstr.c"
#include "string/strtok.c"
#include "string/strxfrm.c"
#include "stdlib/abs.c"
#include "stdlib/assert.c"
#include "stdlib/atof.c"
@@ -89,7 +90,9 @@
#include "stdlib/atol.c"
#include "stdlib/atoll.c"
#include "stdlib/calloc.c"
#include "stdlib/atexit.c"
#include "stdlib/exit.c"
#include "stdlib/_exit.c"
#include "stdlib/free.c"
#include "stdlib/itoa.c"
#include "stdlib/labs.c"
@@ -100,6 +103,7 @@
#include "stdlib/realloc.c"
#include "stdlib/strtod.c"
#include "stdlib/strtol.c"
#include "stdlib/abort.c"
#include "math/acosh.c"
#include "math/asinh.c"
@@ -170,7 +174,9 @@ ksys_dll_t EXPORTS[] = {
{ "atoll", &atoll },
{ "atof", &atof },
{ "calloc", &calloc },
{ "_exit", &_exit },
{ "exit", &exit },
{ "abort", &abort },
{ "free", &free },
{ "itoa", &itoa },
{ "labs", &labs },

View File

@@ -0,0 +1,26 @@
#include <conio.h>
Review

Please add copyright:

/*
* SPDX-License-Identifier: GPL-2.0-only
* Copyright (C) 2026 KolibriOS team
*/
Please add copyright: ```c /* * SPDX-License-Identifier: GPL-2.0-only * Copyright (C) 2026 KolibriOS team */ ```
#include <stdio.h>
#include <sys/ksys.h>
void _exit(int status)
{
__libc_exit(status, NULL);
}
void __libc_exit(int status, void (*before_exit)(int status))
{
// return error and this is not abort
if (status && status != 128) {
fprintf(stderr, "\nexit code: %d\n", status);
}
if (before_exit) {
before_exit(status);
}
if (__con_is_load) {
con_exit(0);
}
_ksys_exit();
}

View File

@@ -0,0 +1,17 @@
/*
Outdated
Review

Please add copyright:

/*
* SPDX-License-Identifier: GPL-2.0-only
* Copyright (C) 2026 KolibriOS team
*/
Please add copyright: ```c /* * SPDX-License-Identifier: GPL-2.0-only * Copyright (C) 2026 KolibriOS team */ ```
* SPDX-License-Identifier: GPL-2.0-only
* Copyright (C) 2026 KolibriOS team
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/ksys.h>
void abort()
{
ksys_thread_t t;
_ksys_thread_info(&t, -1);
fprintf(stderr, "\nAbort in %d\n", t.pid);
_exit(128);
}

View File

@@ -0,0 +1,40 @@
/*
Outdated
Review

Please add copyright:

/*
* SPDX-License-Identifier: GPL-2.0-only
* Copyright (C) 2026 KolibriOS team
*/
Please add copyright: ```c /* * SPDX-License-Identifier: GPL-2.0-only * Copyright (C) 2026 KolibriOS team */ ```
* SPDX-License-Identifier: GPL-2.0-only
* Copyright (C) 2026 KolibriOS team
*/
#include <stdlib.h>
struct atexit_n {
struct atexit_n* last;
void (*func)(void);
};
static struct atexit_n* __last_n = NULL;
int atexit(void (*func)(void))
{
struct atexit_n* n = malloc(sizeof(struct atexit_n));
if (n == NULL) {
return 1;
}
n->last = __last_n;
n->func = func;
__last_n = n;
return 0;
}
void __run_atexit()
{
struct atexit_n* n = __last_n;
while (n != NULL) {
n->func();
struct atexit_n* to_free = n;
n = n->last;
free(to_free);
}
}

View File

@@ -1,12 +1,32 @@
/* Copyright (C) 2021 Logaev Maxim (turbocat2001), GPLv2 */
/*
Outdated
Review

Please udate copyright:

/*
* SPDX-License-Identifier: GPL-2.0-only
* Copyright (C) 2021 Logaev Maxim
* Copyright (C) 2026 KolibriOS team
*/
Please udate copyright: ```c /* * SPDX-License-Identifier: GPL-2.0-only * Copyright (C) 2021 Logaev Maxim * Copyright (C) 2026 KolibriOS team */ ```
* SPDX-License-Identifier: GPL-2.0-only
* Copyright (C) 2021 Logaev Maxim
* Copyright (C) 2026 KolibriOS team
*/
#include <conio.h>
#include <stdlib.h>
Review

TCC does not optimize the codegen. Comment out and write TODO.

TCC does not optimize the codegen. Comment out and write TODO.
#include <sys/ksys.h>
#include "_exit.h"
/*
TODO
static void __close_all()
{
}
static void __free_all_mem()
{
}
*/
void __normal_exit(int status)
{
__run_atexit();
__close_all();
__free_all_mem();
}
void exit(int status)
{
if (__con_is_load) {
con_exit(status);
}
_ksys_exit();
__libc_exit(status, &__normal_exit);
}