libc.obj: add new and update functions #311

Closed
Egor00f wants to merge 20 commits from Egor00f/kolibrios:update-libc.obj into main
Contributor

Changes

functions changes

  • update malloc/free/realloc
  • exit frees all memory allocated through malloc.

New functions:

  • abort
  • atexit
  • system
  • strtok_r

Other additions

  • EXIT_SUCCESS and EXIT_FAILURE
  • call exit after main
  • save main exit code in /tmp0/1/.libc/pid.status
## Changes ## functions changes + update `malloc`/`free`/`realloc` + `exit` frees all memory allocated through malloc. ### New functions: + `abort` + `atexit` + `system` + `strtok_r` ### Other additions + `EXIT_SUCCESS` and `EXIT_FAILURE` + call `exit` after `main` + save `main` exit code in `/tmp0/1/.libc/pid.status`
Egor00f added 2 commits 2026-01-10 18:23:51 +00:00
add strpbrk to EXPORTS
All checks were successful
Build system / Check kernel codestyle (pull_request) Successful in 1m12s
Build system / Build (pull_request) Successful in 16m3s
0e4c573cb5
видимо в экспорт забыли добавить
libc.obj: add void abort(), int atexit( void (*func)(void) ), EXIT_ SUCCESS/FAILURE
Some checks failed
Build system / Build (pull_request) Failing after 1s
Build system / Check kernel codestyle (pull_request) Successful in 1m12s
8322ec954b
Egor00f added 1 commit 2026-01-13 19:04:24 +00:00
libc.obj: update malloc/free/realloc
All checks were successful
Build system / Check kernel codestyle (pull_request) Successful in 1m7s
Build system / Build (pull_request) Successful in 15m52s
bb59eac8d6
гавно говна, нужно доработать
Egor00f added 2 commits 2026-01-15 16:20:06 +00:00
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
15f27eb1c3
нен работает
по стандартам после `main` должно быть закрыте всего, что закрывается в `exit`
ну терпите, crt увеличиласть на несколько байт
обертка для `exit` в crt нужна т.к. `exit` импортируется.

зачем вообще было держать бинарь `libc.obj/lib/crt0.o`, если абсолютно такой же лежит в `bin/lib/` всемсте `tcc`? Нет, зачем вообще тащить бинари в репку?
Egor00f added 1 commit 2026-01-17 08:16:22 +00:00
libc.obj: update comments
Some checks failed
Build system / Build (pull_request) Failing after 2s
Build system / Check kernel codestyle (pull_request) Successful in 1m8s
61633d4dcc
Egor00f changed title from WIP: update `libc.obj` to WIP: `libc.obj`: update allocator and `exit` 2026-01-17 14:42:13 +00:00
Egor00f changed title from WIP: `libc.obj`: update allocator and `exit` to `libc.obj`: update allocator and `exit` 2026-01-17 15:01:44 +00:00
Egor00f added 1 commit 2026-01-17 15:01:55 +00:00
Merge branch 'main' into update-libc.obj
Some checks failed
Build system / Build (pull_request) Failing after 2s
Build system / Check kernel codestyle (pull_request) Successful in 1m10s
16b520dd67
Egor00f added a new dependency 2026-01-19 07:14:44 +00:00
Egor00f added a new dependency 2026-01-19 07:15:25 +00:00
Egor00f added a new dependency 2026-01-19 07:15:36 +00:00
Egor00f added a new dependency 2026-01-19 07:15:58 +00:00
Egor00f changed title from `libc.obj`: update allocator and `exit` to WIP: `libc.obj`: update allocator and `exit` 2026-01-19 07:17:22 +00:00
Egor00f added 1 commit 2026-01-19 16:22:19 +00:00
libc.obj: add system
Some checks failed
Build system / Build (pull_request) Failing after 2s
Build system / Check kernel codestyle (pull_request) Successful in 1m20s
2818bf25e2
Egor00f added 1 commit 2026-01-19 16:34:44 +00:00
libc.obj: small fixes
Some checks failed
Build system / Build (pull_request) Failing after 1s
Build system / Check kernel codestyle (pull_request) Successful in 1m7s
de8b5f9993
Egor00f added 1 commit 2026-01-19 16:36:11 +00:00
Merge branch 'main' into update-libc.obj
All checks were successful
Build system / Check kernel codestyle (pull_request) Successful in 1m9s
Build system / Build (pull_request) Successful in 17m12s
e9ab6eee9d
Egor00f changed title from WIP: `libc.obj`: update allocator and `exit` to `libc.obj`: update allocator and `exit` 2026-01-19 16:36:23 +00:00
mxlgv requested changes 2026-01-20 23:26:13 +00:00
Dismissed
@@ -43,0 +60,4 @@
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")
Owner

Why did you decide to use GNU LD instead of tcc?

Why did you decide to use __GNU LD__ instead of __tcc__?
mxlgv marked this conversation as resolved
@@ -14,6 +14,7 @@ public start
public start as '_start'
Owner

If you decide to change the file encoding, I also suggest translating it into English. Otherwise, return it as it was.

If you decide to change the file encoding, I also suggest translating it into English. Otherwise, return it as it was.
mxlgv marked this conversation as resolved
@@ -89,6 +89,8 @@
#include "stdlib/atol.c"
#include "stdlib/atoll.c"
#include "stdlib/calloc.c"
#include "stdlib/atexit.c" // должно быть до exit.c
Owner

Please comments in English

Please comments in English
mxlgv marked this conversation as resolved
@@ -0,0 +43,4 @@
char* cmd;
char* args;
int ret = _ksys_exec(cmd, args);
Owner

C99:

If string is a null pointer, the system function determines whether the host environment has a command processor. If string is not a null pointer, the system function passes the string pointed to by string to that command processor to be executed in a manner which the implementation shall document; this might then cause the program calling system to behave in a non-conforming manner or to terminate.

If the argument is a null pointer, the system function returns nonzero only if a command processor is available. If the argument is not a null pointer, and the system function does return, it returns an implementation-defined value.

system() implies execution through the command processor and not directly. In the case of GNU Linux this is /bin/sh. In the case of KolibriOS this is SHELL. In fact, I don’t think that this is of any benefit for native applications. It's just cluttering the code. Don't forget libc.obj is on the floppy disk

C99: > If string is a null pointer, the system function determines whether the host environment has a command processor. If string is not a null pointer, the system function passes the string pointed to by string to that command processor to be executed in a manner which the implementation shall document; this might then cause the program calling system to behave in a non-conforming manner or to terminate. > If the argument is a null pointer, the system function returns nonzero only if a command processor is available. If the argument is not a null pointer, and the system function does return, it returns an implementation-defined value. `system()` implies execution through the *command processor* and not directly. In the case of *GNU Linux* this is `/bin/sh`. In the case of *KolibriOS* this is `SHELL`. In fact, I don’t think that this is of any benefit for native applications. It's just cluttering the code. Don't forget `libc.obj` is on the floppy disk
Author
Contributor

I want to add Lua assembly using TCC. It uses libc.obj. I think it would be better to add it to libc and make it a built-in system in Lua.

I want to add Lua assembly using TCC. It uses `libc.obj`. I think it would be better to add it to libc and make it a built-in `system` in Lua.
Owner

I want to add Lua assembly using TCC. It uses libc.obj. I think it would be better to add it to libc and make it a built-in system in Lua.

If in about os.execute() then there is shell-command as an argument. In any case, this is not a native application and you can insert this code into the Lua itself if you need it.

> I want to add Lua assembly using TCC. It uses `libc.obj`. I think it would be better to add it to libc and make it a built-in `system` in Lua. If in about `os.execute()` then there is `shell-command` as an argument. In any case, this is not a native application and you can insert this code into the Lua itself if you need it.
Owner

You also have the option to implement this according to the C99 standard via the SHELL call.

You also have the option to implement this according to the C99 standard via the SHELL call.
Author
Contributor

Yes i can. Would i'll do it? Maybe...
Ok i think i too stupid for this

Yes i can. Would i'll do it? Maybe... Ok i think i too stupid for this
mxlgv marked this conversation as resolved
mxlgv requested changes 2026-01-20 23:40:52 +00:00
mxlgv left a comment
Owner

If you implemented the allocator yourself, I respect you. If you took it from somewhere, it’s better to keep the copyrights. In any case, it would be nice to add at least basic tests for malloc.

If you implemented the allocator yourself, I respect you. If you took it from somewhere, it’s better to keep the copyrights. In any case, it would be nice to add at least basic tests for malloc.
mxlgv requested changes 2026-01-20 23:51:41 +00:00
@@ -91,2 +90,2 @@
int 0x40
dd -1
push eax
call crt_exit
Owner

Try call [exit]

Try `call [exit]`
mxlgv marked this conversation as resolved
Egor00f added 2 commits 2026-01-22 11:32:20 +00:00
Merge branch 'update-libc.obj' of https://git.kolibrios.org/Egor00f/kolibrios into update-libc.obj
Some checks failed
Build system / Build (pull_request) Failing after 2s
Build system / Check kernel codestyle (pull_request) Successful in 1m12s
eee5a71fdd
Egor00f requested review from mxlgv 2026-01-22 11:34:32 +00:00
Egor00f added 1 commit 2026-01-24 06:21:16 +00:00
libc.obj: translate crt0.asm
Some checks failed
Build system / Build (pull_request) Failing after 1s
Build system / Check kernel codestyle (pull_request) Successful in 1m14s
8e6c43113a
mxlgv added the CCategory/Libraries
Kind
Enhancement
Priority
Low
labels 2026-01-28 14:35:35 +00:00
mxlgv requested changes 2026-01-28 14:42:12 +00:00
@@ -0,0 +4,4 @@
int system(const char* command)
{
if (command == NULL || *command == '\0')
Owner

You did it almost right. You only need to check if /sys/shell is present when the argument is NULL. If /sys/shell exists on the system(NULL), return 1.

You did it almost right. You only need to check if `/sys/shell` is present when the argument is `NULL`. If `/sys/shell` exists on the `system(NULL)`, return 1.
mxlgv requested changes 2026-01-28 14:51:44 +00:00
@@ -2,2 +3,3 @@
end
CFLAGS = " -r -nostdinc -nostdlib -DGNUC -D_BUILD_LIBC "
function AddPrefix(prefix, t)
Owner

What is this for?

What is this for?
@@ -0,0 +6,4 @@
{
ksys_thread_t t;
_ksys_thread_info(&t, -1);
printf("\nAbort in %d\n", t.pid);
Owner

Better: fprintf(stderr, ...

Better: `fprintf(stderr, ...`
mxlgv marked this conversation as resolved
Egor00f added 1 commit 2026-02-04 07:42:20 +00:00
libc.obj: fixes and optimizations for allocator && add new samples to sh build
All checks were successful
Build system / Check kernel codestyle (pull_request) Successful in 41s
Build system / Build (pull_request) Successful in 16m37s
cb29ecffb7
fix: in `__mem_MERGE_MEM_NODES` `base->free = base->size`, its wrong.
forgot to set size for new block
optimization: add `__last_mem_node`. usually  its node with max free space among other nodes. firstly `malloc` try find space in it.
update(fix and optimizations) `realloc`.
now sdltest is working!
Egor00f added 1 commit 2026-02-04 12:50:03 +00:00
libc.obj: some small changes
All checks were successful
Build system / Check kernel codestyle (pull_request) Successful in 50s
Build system / Build (pull_request) Successful in 16m34s
7505baa105
кусок кода в `realloc`, провереющий на свободную ноду безсмысленен, т.к. в `free` в любом случае сливает освобождаемую ноду с предыдущей.
Egor00f requested review from mxlgv 2026-02-04 12:50:21 +00:00
Egor00f changed title from `libc.obj`: update allocator and `exit` to WIP: `libc.obj`: update allocator and `exit` 2026-02-08 17:42:49 +00:00
Egor00f added 1 commit 2026-02-16 18:12:40 +00:00
libc.obj: add strtok_s
All checks were successful
Build system / Check kernel codestyle (pull_request) Successful in 47s
Build system / Build (pull_request) Successful in 17m25s
97e7404c9e
Egor00f added 2 commits 2026-02-19 07:03:11 +00:00
idk why, but tests don't run `exit` after `main`
Merge branch 'update-libc.obj' of https://git.kolibrios.org/Egor00f/kolibrios into update-libc.obj
All checks were successful
Build system / Check kernel codestyle (pull_request) Successful in 39s
Build system / Build (pull_request) Successful in 16m21s
89e26439d5
Author
Contributor

shell have page fault on mkdir, because strtok is wrong.
fixed

`shell` have page fault on `mkdir`, because `strtok` is wrong. fixed
Egor00f added 1 commit 2026-02-20 13:16:20 +00:00
libc.obj: fix strtok && update change path to exit code
All checks were successful
Build system / Check kernel codestyle (pull_request) Successful in 1m33s
Build system / Build (pull_request) Successful in 10m23s
2a5f192267
Egor00f changed title from WIP: `libc.obj`: update allocator and `exit` to WIP: `libc.obj`: add new and update functions 2026-02-20 13:18:30 +00:00
Egor00f added 1 commit 2026-02-20 14:32:52 +00:00
Merge branch 'main' into update-libc.obj
All checks were successful
Build system / Check kernel codestyle (pull_request) Successful in 1m6s
Build system / Build (pull_request) Successful in 16m59s
eec7ce3900
Egor00f changed title from WIP: `libc.obj`: add new and update functions to `libc.obj`: add new and update functions 2026-02-20 14:33:41 +00:00
Owner

@Egor00f Please stop randomly adding features to your PR that you want to do! I can't review that much code. Otherwise I will close it and you will be forced to divide it into smaller parts.

@Egor00f Please stop randomly adding features to your PR that you want to do! I can't review that much code. Otherwise I will close it and you will be forced to divide it into smaller parts.
mxlgv requested changes 2026-02-20 17:01:05 +00:00
@@ -0,0 +1,296 @@
#include <stdlib.h>
Owner

English please for messages and comments

English please for messages and comments
@@ -0,0 +10,4 @@
if (func()) { \
printf("[SUCCESS]\tTest %s is ok.\n\n", #func); \
} else { \
printf("[FAIL]\tTest %s failed.\n\n", #func); \
Owner

Please use fprintf(stderr ...) for errors

Please use `fprintf(stderr ...)` for errors
@@ -0,0 +136,4 @@
unsigned char *byte_ptr = (unsigned char *)ptr;
for (size_t i = 0; i < size; ++i) {
if (byte_ptr[i] != pattern) {
fprintf(stderr, "Ошибка: Байт %zu не соответствует паттерну. Ожидалось %02X, получено %02X\n",
Owner

English please

English please
@@ -0,0 +157,4 @@
int *new_ptr = (int *)realloc(ptr, new_size * sizeof(int));
if (new_ptr == NULL) {
free(ptr); // Оригинальный блок все еще действителен
Owner

English please

English please
@@ -0,0 +161,4 @@
return false;
}
// Проверяем, что старые данные сохранились
Owner

English please

English please
@@ -0,0 +167,4 @@
return false;
}
// Проверяем, что новый участок доступен для записи
Owner

English please

English please
@@ -0,0 +220,4 @@
return false;
}
// Проверяем, что данные сохранились
Owner

English please

English please
@@ -0,0 +36,4 @@
// wait of end of shell
ksys_thread_t t;
while (_ksys_thread_info(&t, pid) != -1 && t.slot_state != 3 && t.slot_state != 4 && t.slot_state != 9) {
Owner

It was enough for you to check the existence of the binary file. Why are you using these redundant checks?

It was enough for you to check the existence of the binary file. Why are you using these redundant checks?
Egor00f closed this pull request 2026-02-20 17:52:54 +00:00
All checks were successful
Build system / Check kernel codestyle (pull_request) Successful in 1m6s
Required
Details
Build system / Build (pull_request) Successful in 16m59s
Required
Details

Pull request closed

Sign in to join this conversation.
No Reviewers
2 Participants
Notifications
Due Date
No due date set.
Reference: KolibriOS/kolibrios#311