libc.obj:

- Update crt0 
 - Removed autoloader generation(use libc.def).

git-svn-id: svn://kolibrios.org@9666 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
turbocat 2022-01-24 17:43:00 +00:00
parent a971f7e19b
commit 36918e3217
14 changed files with 212 additions and 480 deletions

View File

@ -1,24 +0,0 @@
#!/bin/bash
set -e
AR=ar
FASM=fasm
set -e
cd $1
echo "Compile ASM files..."
rm -f *.o
cp __lib__.asm.bak __lib__.asm
for asm_file in $(find *.asm)
do
$FASM $asm_file >> /dev/null
done
echo "Create libc.obj.a library..."
ar -rsc ../../bin/lib/libc.obj.a *.o
rm -f *.asm *.o
echo "Done!"

View File

@ -1,57 +0,0 @@
// export_table_gen
// Copyright (C) maxcodehack and turbocat2001, 2021
#include <stdio.h>
#include <string.h>
int main(int argc, char** argv) {
if (argc != 3) {
printf("Usage: %s <symbols.txt> <exports.c>\n", argv[0]);
return 0;
}
FILE *input, *output;
if ((input = fopen(argv[1], "r")) == NULL)
{
printf("error: file \"%s\" not found\n", argv[1]);
return 1;
}
char buf[10000];
// Head
strcpy(buf, \
"#include <stdio.h>\n" \
"#include <string.h>\n" \
"#include <stdlib.h>\n" \
"#include <time.h>\n" \
"#include <sys/dirent.h>\n" \
"#include <sys/ksys.h>\n\n" \
"#include <math.h>\n\n" \
"#include <setjmp.h>\n\n" \
"ksys_dll_t EXPORTS[] = {\n");
// Generate
char symbol[256];
while(fscanf(input, "%s", symbol) != EOF) {
if(symbol[0]!='!'){
char temp[256];
sprintf(temp, "{\"%s\", &%s},\n", symbol, symbol);
strcat(buf, temp);
}
}
strcat(buf, "NULL,\n};");
fclose(input);
// Output generated
output = fopen(argv[2], "w");
if (output == NULL)
{
printf("Unable to write to file: '%s'!\n", argv[2]);
return 1;
}
fputs(buf, output);
fclose(output);
printf("Done, check %s!\n", argv[2]);
return 0;
}

View File

@ -1,48 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
int main(int argc, char** argv) {
if(argc!=3){
printf("Usage: LoadGen <symbols.txt> <loader_dir>\n");
return 0;
}
argv[2][strlen(argv[1])-2]='\0';
FILE* symbols_txt = fopen(argv[1], "r");
if(!symbols_txt){
fprintf(stderr, "File '%s' not found!\n", argv[1]);
return -1;
}
char line[256];
while(fgets(line, 256, symbols_txt)) {
if(line[0]!='!'){
if(line[strlen(line)-1]=='\n'){
line[strlen(line)-1]='\0';
}
char asm_name[PATH_MAX];
sprintf(asm_name, "%s/%s.asm", argv[2], line);
FILE *out = fopen(asm_name, "wb");
if(!out){
fprintf(stderr, "Error! File '%s' not created!\n", asm_name);
return -1;
}else{
printf("File '%s' created successfully!\n", asm_name);
}
fprintf(out, "format ELF\n");
fprintf(out, "include \"__lib__.inc\"\n");
fprintf(out, "fun equ __func@%s\n", line);
fprintf(out, "fun_str equ '%s'\n", line);
fprintf(out, "section '.text'\n");
fprintf(out, "fun_name db fun_str, 0\n");
fprintf(out, "section '.data'\n");
fprintf(out, "extrn lib_name\n");
fprintf(out, "public fun as fun_str\n");
fprintf(out, "fun dd fun_name\n");
fprintf(out, "lib dd lib_name\n");
fclose(out);
}
}
}

View File

@ -1,3 +0,0 @@
all:
$(CC) ExportGen.c -o ../ExportGen
$(CC) LoaderGen.c -o ../LoaderGen

View File

@ -1,28 +0,0 @@
ifndef GCC
GCC=kos32-gcc
endif
KPACK=kpack
FASM=fasm
CFLAGS = -c -nostdinc -I../include -DGNUC -D_BUILD_LIBC -fno-common -Os -fno-builtin -fno-leading-underscore -fno-pie
SRC=libc.c
LIB=libc.obj
all:
$(MAKE) -C ../linuxtools/src
mkdir -p exports
../linuxtools/ExportGen symbols.txt exports/exports.c
$(FASM) crt/crt0.asm ../../bin/lib/crt0.o
$(GCC) $(CFLAGS) $(SRC) -o $(LIB)
$(KPACK) $(LIB)
../linuxtools/LoaderGen symbols.txt ../loader
../linuxtools/LoaderBuild ../loader
$(MAKE) -C libtcc
rm -rf exports
install:
cp -f libc.obj ~/.kex/root/RD/1/LIB
clean:
rm ../../bin/lib/libc.obj.a ../../bin/lib/libtcc.a

View File

@ -2,12 +2,7 @@ if tup.getconfig("NO_GCC") ~= "" then return end
HELPERDIR = (tup.getconfig("HELPERDIR") == "") and "../../../../../" or tup.getconfig("HELPERDIR")
tup.include(HELPERDIR .. "/use_gcc.lua")
CFLAGS = " -c -w -nostdinc -DGNUC -D_BUILD_LIBC -Os -fno-common -fno-builtin -fno-leading-underscore -fno-pie"
CFLAGS = " -c -nostdinc -DGNUC -D_BUILD_LIBC -Os -fno-common -fno-builtin -fno-leading-underscore -fno-pie"
INCLUDES = " -I../include"
tup.rule("../linuxtools/src/ExportGen.c", "gcc %f -o %o" , "../linuxtools/ExportGen")
tup.rule("../linuxtools/src/LoaderGen.c", "gcc %f -o %o" , "../linuxtools/LoaderGen")
tup.rule({"symbols.txt",extra_inputs = {"../linuxtools/ExportGen"}}, "../linuxtools/ExportGen %f %o" , "exports/exports.c")
tup.rule({"libc.c",extra_inputs = {"exports/exports.c"}} , "kos32-gcc" .. CFLAGS .. INCLUDES .. " -o %o %f " .. tup.getconfig("KPACK_CMD"), "libc.obj")
tup.rule("libc.c", "kos32-gcc" .. CFLAGS .. INCLUDES .. " -o %o %f " .. tup.getconfig("KPACK_CMD"), "libc.obj")

View File

@ -1,35 +1,32 @@
;
; 2021, Edited by Coldy
;
; This module same as original crt0.asm, but cut:
; 1. virtual header block (hparams change to __app_params, hpath change to __app_path)
; 2. init heap of memory - not needed because 68.18 (68.19) init heap implicitly
; (it is does dll.obj)
; 3. loader (he lives in dll.obj)
;
format ELF
section '.text' executable
public start
public start as '_start'
;extrn mf_init
extrn main
include '../../../../../proc32.inc'
include '../../../../../macros.inc'
include '../../../../../dll.inc'
;include '../../../../../debug.inc'
;start_:
virtual at 0
db 'MENUET01' ; 1. Magic number (8 bytes)
dd 0x01 ; 2. Version of executable file
dd start ; 3. Start address
imgsz dd 0x0 ; 4. Size of image
dd 0x100000 ; 5. Size of needed memory
dd 0x100000 ; 6. Pointer to stack
hparams dd 0x0 ; 7. Pointer to program arguments
hpath dd 0x0 ; 8. Pointer to program path
end virtual
extrn main
include '../../../../../../proc32.inc'
include '../../../../../../macros.inc'
__DEBUG__ = 0
__app_params equ 0x1C ; Pointer to program arguments
;__app_path equ 0x20 ; Pointer to program path
start:
;DEBUGF 'Start programm\n'
;init heap of memory
mov eax,68
mov ebx,11
int 0x40
mov [argc], 0
mov eax, [hparams]
mov eax, [__app_params]
test eax, eax
jz .without_path
mov eax, path
@ -85,7 +82,6 @@ start:
jmp .parse
.run:
call load_imports
push argv
push [argc]
call main
@ -112,101 +108,9 @@ push_param:
inc [argc]
.dont_add:
ret
;==============================
;==============================
load_imports:
;==============================
;parameters
; none
;description
; imports must be located at end of image (but before BSS sections)
; the address of end of imports (next byte after imports) is located in imgsz
; look at each import from that address up to illegal import
; legal import is such that:
; first pointer points to procedure name
; and is smaller than imgsz
; second pointer points lo library name, starting with 0x55, 0xAA
; and is smaller than imgsz
; each library should be initialized as appropriate, once
; so as library is initialized, its name will be replaced 0x00
mov ebx, [imgsz] ; byte after imports
.handle_next_import:
sub ebx, 4 ; ebx = pointer to pointer to library name
mov esi, dword[ebx] ; esi = pointer to library name
push ebx
push esi
call load_library ; eax = pointer to library exports
pop esi
pop ebx
test eax, eax
jz .done
sub ebx, 4 ; ebx = pointer to pointer to symbol name
push ebx
stdcall dll.GetProcAddress, eax, dword[ebx]
pop ebx
test eax, eax
jz .fail
mov dword[ebx], eax
jmp .handle_next_import
.done:
;DEBUGF 1, "Library: %s not loaded!\n", esi
;mcall -1
ret
.fail:
ret
;==============================
;==============================
load_library:
;==============================
;parameters
; ebx: library name address
;description
; each library should be initialized as appropriate, once
; so as library is initialized, its name will be replaced 0x00
; and 4 next bytes will be set to address of library
; first two bytes of library name must be 0x55, 0xAA (is like a magic)
cld ; move esi further, not back
cmp esi, [imgsz]
ja .fail
lodsb ; al = first byte of library name
cmp al, 0x55
jne .fail
lodsb ; al = second byte of library name
cmp al, 0xAA
jne .fail
lodsb ; al = third byte of library name (0x00 if the library is already loaded)
test al, al
jnz .load
lodsd ; if we here, then third byte is 0x00 => address of library is in next 4 bytes
; now eax contains address of library
ret
.load:
dec esi ; we checked on 0 before, let's go back
mov eax, 68
mov ebx, 19
mov ecx, esi
int 0x40 ; eax = address of exports
mov byte[esi], 0 ; library is loaded, let's place 0 in first byte of name
mov [esi + 1], eax ; now next 4 bytes of library name are replaced by address of library
; call lib_init
stdcall dll.GetProcAddress, eax, lib_init_str ; eax = address of lib_init
test eax, eax
jz .ret
stdcall dll.Init, eax
.ret:
mov eax, [esi + 1] ; put address of library into eax
ret
.fail:
mov eax, 0
ret
;==============================
lib_init_str db 'lib_init', 0
public argc as '__argc'
public params as '__argv'
public path as '__path'
@ -219,3 +123,5 @@ argv rd max_parameters
path rb buf_len
params rb buf_len
;section '.data'
;include_debug_strings ; ALWAYS present in data section

View File

@ -1,3 +1,5 @@
#include <setjmp.h>
#include "ctype/is.c"
#include "ctype/tolower.c"
#include "ctype/toupper.c"
@ -146,6 +148,174 @@ __asm__(
".include \"setjmp/setjmp.s\""
);
#include "libtcc/libtcc1.c"
#include "libtcc1/libtcc1.c"
#include "stdlib/___chkstk_ms.c"
#include "exports/exports.c"
ksys_dll_t EXPORTS[] = {
{"clearerr", &clearerr},
{"debug_printf", &debug_printf},
{"fclose", &fclose},
{"feof", &feof},
{"ferror", &ferror},
{"fflush", &fflush},
{"fgetc", &fgetc},
{"fgetpos", &fgetpos},
{"fgets", &fgets},
{"fopen", &fopen},
{"fprintf", &fprintf},
{"fputc", &fputc},
{"fputs", &fputs},
{"fread", &fread},
{"freopen", &freopen},
{"fscanf", &fscanf},
{"fseek", &fseek},
{"fsetpos", &fsetpos},
{"ftell", &ftell},
{"fwrite", &fwrite},
{"getchar", &getchar},
{"gets", &gets},
{"perror", &perror},
{"printf", &printf},
{"puts", &puts},
{"remove", &remove},
{"rename", &rename},
{"rewind", &rewind},
{"scanf", &scanf},
{"setbuf", &setbuf},
{"setvbuf", &setvbuf},
{"snprintf", &snprintf},
{"sprintf", &sprintf},
{"sscanf", &sscanf},
{"tmpfile", &tmpfile},
{"tmpnam", &tmpnam},
{"vfscanf", &vfscanf},
{"vprintf", &vprintf},
{"vfscanf", &vfscanf},
{"vsprintf", &vsprintf},
{"vsnprintf", &vsnprintf},
{"vsscanf", &vsscanf},
{"ungetc", &ungetc},
{"abs", &abs},
{"atoi", &atoi},
{"atol", &atol},
{"atoll", &atoll},
{"atof", &atof},
{"calloc", &calloc},
{"exit", &exit},
{"free", &free},
{"itoa", &itoa},
{"labs", &labs},
{"llabs", &llabs},
{"malloc", &malloc},
{"realloc", &realloc},
{"strtol", &strtol},
{"srand", &srand},
{"rand", &rand},
{"qsort", &qsort},
{"strtod", &strtod},
{"__assert_fail", &__assert_fail},
{"memchr", &memchr},
{"memcmp", &memcmp},
{"strncat", &strncat},
{"strchr", &strchr},
{"strcat", &strcat},
{"strcmp", &strcmp},
{"strcoll", &strcoll},
{"strcpy", &strcpy},
{"strcspn", &strcspn},
{"strdup", &strdup},
{"strerror", &strerror},
{"strlen", &strlen},
{"strncat", &strncat},
{"strncmp", &strncmp},
{"strncpy", &strncpy},
{"strrchr", &strrchr},
{"strrev", &strrev},
{"strspn", &strspn},
{"strstr", &strstr},
{"strtok", &strtok},
{"strxfrm", &strxfrm},
{"_errno", &_errno},
{"closedir", &closedir},
{"opendir", &opendir},
{"readdir", &readdir},
{"rewinddir", &rewinddir},
{"seekdir", &seekdir},
{"telldir", &telldir},
{"getcwd", &getcwd},
{"mkdir", &mkdir},
{"rmdir", &rmdir},
{"setcwd", &setcwd},
{"getcwd", &getcwd},
{"socket", &socket},
{"close", &close},
{"bind", &bind},
{"listen", &listen},
{"connect", &connect},
{"accept", &accept},
{"send", &send},
{"recv", &recv},
{"setsockopt", &setsockopt},
{"socketpair", &socketpair},
{"acosh", &acosh},
{"asinh", &asinh},
{"atanh", &atanh},
{"acosh", &acosh},
{"frexp", &frexp},
{"hypot", &hypot},
{"ldexp", &ldexp},
{"sinh", &sinh},
{"tanh", &tanh},
{"acos", &acos},
{"asin", &asin},
{"atan", &atan},
{"atan2", &atan2},
{"ceil", &ceil},
{"cos", &cos},
{"sin", &sin},
{"tan", &tan},
{"exp", &exp},
{"fabs", &fabs},
{"floor", &floor},
{"fmod", &fmod},
{"log", &log},
{"modf", &modf},
{"modfl", &modfl},
{"pow", &pow},
{"pow2", &pow2},
{"pow10", &pow10},
{"longjmp", &longjmp},
{"setjmp", &setjmp},
{"__is", &__is},
{"tolower", &tolower},
{"toupper", &toupper},
{"con_set_title", &con_set_title},
{"con_init", &con_init},
{"con_init_opt", &con_init_opt},
{"con_write_asciiz", &con_write_asciiz},
{"con_write_string", &con_write_string},
{"con_printf", &con_printf},
{"con_exit", &con_exit},
{"con_get_flags", &con_get_flags},
{"con_set_flags", &con_set_flags},
{"con_kbhit", &con_kbhit},
{"con_getch", &con_getch},
{"con_getch2", &con_getch2},
{"con_gets", &con_gets},
{"con_gets2", &con_gets2},
{"con_get_font_height", &con_get_font_height},
{"con_get_cursor_height", &con_get_cursor_height},
{"con_set_cursor_height", &con_set_cursor_height},
{"con_cls", &con_cls},
{"con_get_cursor_pos", &con_get_cursor_pos},
{"con_set_cursor_pos", &con_set_cursor_pos},
{"mktime", &mktime},
{"time", &time},
{"localtime", &localtime},
{"asctime", &asctime},
{"difftime", &difftime},
{"basename", &basename},
{"dirname", &dirname},
NULL,
};

View File

@ -1,179 +0,0 @@
!____STDIO______
clearerr
debug_printf
fclose
feof
ferror
fflush
fgetc
fgetpos
fgets
fopen
fprintf
fputc
fputs
fread
freopen
fscanf
fseek
fsetpos
ftell
fwrite
getchar
gets
perror
printf
puts
remove
rename
rewind
scanf
setbuf
setvbuf
snprintf
sprintf
sscanf
tmpfile
tmpnam
vfscanf
vprintf
vfscanf
vsprintf
vsnprintf
vsscanf
ungetc
!____STDLIB____
abs
atoi
atol
atoll
atof
calloc
exit
free
itoa
labs
llabs
malloc
realloc
strtol
srand
rand
qsort
strtod
__assert_fail
!____STRING____
!memcpy
memchr
memcmp
!memmove
!memset
strncat
strchr
strcat
strcmp
strcoll
strcpy
strcspn
strdup
strerror
strlen
strncat
strncmp
strncpy
strrchr
strrev
strspn
strstr
strtok
strxfrm
_errno
!____SYS____
closedir
opendir
readdir
rewinddir
seekdir
telldir
getcwd
mkdir
rmdir
setcwd
getcwd
!____SOCKET____
socket
close
bind
listen
connect
accept
send
recv
setsockopt
socketpair
!____UNISTD____
!____MATH____
acosh
asinh
atanh
acosh
frexp
hypot
ldexp
sinh
tanh
acos
asin
atan
atan2
ceil
cos
sin
tan
exp
fabs
floor
fmod
log
modf
modfl
pow
pow2
pow10
!____LONGJMP____
longjmp
setjmp
!____CTYPE____
__is
tolower
toupper
!___CONIO___
con_set_title
con_init
con_init_opt
con_write_asciiz
con_write_string
con_printf
con_exit
con_get_flags
con_set_flags
con_kbhit
con_getch
con_getch2
con_gets
con_gets2
con_get_font_height
con_get_cursor_height
con_set_cursor_height
con_cls
con_get_cursor_pos
con_set_cursor_pos
!____TIME____
mktime
time
localtime
asctime
difftime
!____MISC____
basename
dirname