[libk]: libk main、rt_main crt0 启动框架搭建

This commit is contained in:
2025-08-13 22:06:09 +08:00
parent 3614ce4014
commit 07cfcfad9a
23 changed files with 234 additions and 36 deletions

View File

@@ -0,0 +1,19 @@
#ifndef BITS_ALLTYPES_H
#define BITS_ALLTYPES_H
typedef unsigned long size_t;
typedef unsigned long uintptr_t;
typedef unsigned long uint64_t;
typedef signed long ssize_t;
typedef signed long intptr_t;
typedef signed long int64_t;
typedef unsigned int uint32_t;
typedef signed int int32_t;
typedef unsigned short uint16_t;
typedef signed short int16_t;
#endif

View File

@@ -0,0 +1,14 @@
#ifndef CRT_ARCH
#define CRT_ARCH
#define CTR_ARCH_START_ASM(START) \
__asm__( \
".text \n" \
".global " START "\n" \
".type " START ",%function\n" \
START ":\n" \
" b " START "_c\n" \
);
#endif

View File

@@ -7,4 +7,6 @@
#define __asm_memcpy
void crt0(void);
#endif //LIBKASM_H

12
magnitude/libk/arch/crt.h Normal file
View File

@@ -0,0 +1,12 @@
//
// Created by dongl on 25-8-12.
//
#ifndef CRT_H
#define CRT_H
#include <crt_arch.h>
void _start_c( long *p );
#endif //CRT_H

View File

@@ -0,0 +1,3 @@
libk_fini:
ret

View File

@@ -0,0 +1,3 @@
libk_init:
ret

35
magnitude/libk/crt/crt0.c Normal file
View File

@@ -0,0 +1,35 @@
//
// Created by dongl on 25-8-11.
//
#include <libk.h>
#include <features.h>
#define START "_start"
#include <crt.h>
weak void libk_init();
weak void libk_fini();
int main( int argc, char **argv );
int rt_main( int argc, char **argv );
int libk_start_main(
int (*)(),
int,
char **,
void (*)(),
void(*)(),
void(*)()
);
void _start_c( long *p )
{
// main 入口参数
const int argc = (int) p[0];
char **argv = (void *)(p+1);
(void) argv;
p[0] >> (sizeof(long) - 1)
? libk_start_main(main, argc, argv, libk_init, libk_fini, 0)
: libk_start_main(rt_main, argc, argv, libk_init, libk_fini, 0);
}

View File

@@ -0,0 +1,25 @@
#ifndef _FEATURES_H
#define _FEATURES_H
#if __STDC_VERSION__ >= 199901L
#define __restrict restrict
#elif !defined(__GNUC__)
#define __restrict
#endif
#if __STDC_VERSION__ >= 199901L || defined(__cplusplus)
#define __inline inline
#elif !defined(__GNUC__)
#define __inline
#endif
#if __STDC_VERSION__ >= 201112L
#elif defined(__GNUC__)
#define _Noreturn __attribute__((__noreturn__))
#else
#define _Noreturn
#endif
#define __REDIR(x,y) __typeof__(x) x __asm__(#y)
#endif

View File

@@ -5,7 +5,7 @@
#ifndef LIBK_MEMORY_H
#define LIBK_MEMORY_H
#include <stddef.h> // for size_t
#include "stddef.h" // for size_t
#ifdef __cplusplus
extern "C" {

View File

@@ -1,22 +1,14 @@
//
// Created by dongl on 25-7-31.
//
#ifndef _STDDEF_H
#define _STDDEF_H
#ifndef STDDEF_H
#define STDDEF_H
#if __cplusplus >= 201103L
#define NULL nullptr
#elif defined(__cplusplus)
#define NULL 0L
#else
#define NULL ((void*)0)
#endif
#include <typedef.h>
#include <bits/alltypes.h>
typedef unsigned long size_t;
typedef unsigned long uintptr_t;
typedef unsigned long uint64_t;
typedef signed long intptr_t;
typedef signed long int64_t;
typedef unsigned int uint32_t;
typedef signed int int32_t;
typedef unsigned short uint16_t;
typedef signed short int16_t;
#endif //STDDEF_H
#endif

View File

@@ -5,7 +5,7 @@
#ifndef LIBK_STRING_H
#define LIBK_STRING_H
#include <stddef.h> // for size_t
#include "stddef.h" // for size_t
#ifdef __cplusplus
extern "C" {

View File

@@ -1,13 +0,0 @@
//
// Created by dongl on 25-8-1.
//
#ifndef TYPEDEF_H
#define TYPEDEF_H
#define NULL ((void *)0)
#define General_API __attribute__((weak))
#define Specialized_API
#endif //TYPEDEF_H

View File

@@ -0,0 +1,17 @@
//
// Created by dongl on 25-8-11.
//
#ifndef FEATURES_H
#define FEATURES_H
#include "../../include/features.h"
#define weak __attribute__((__weak__))
#define hidden __attribute__((__visibility__("hidden")))
#define weak_alias(old, new) \
extern __typeof(old) new __attribute__((__weak__, __alias__(#old)))
#define General_API weak
#endif

View File

@@ -0,0 +1,10 @@
//
// Created by dongl on 25-8-11.
//
#ifndef MEMORY_H
#define MEMORY_H
#include "../../include/string.h"
#endif //MEMORY_H

View File

@@ -0,0 +1,19 @@
//
// Created by dongl on 25-8-12.
//
#ifndef STDBOOL_H
#define STDBOOL_H
#ifndef __cplusplus
#define true 1
#define false 0
#define bool _Bool
#endif
#define __bool_true_false_are_defined 1
#endif //STDBOOL_H

View File

@@ -0,0 +1,10 @@
//
// Created by dongl on 25-8-11.
//
#ifndef STDDEF_H
#define STDDEF_H
#include "../../include/stddef.h"
#endif //STDDEF_H

View File

@@ -0,0 +1,11 @@
//
// Created by dongl on 25-8-11.
//
#ifndef STRING_H
#define STRING_H
#include "../../include/string.h"
#endif

View File

@@ -0,0 +1,13 @@
#ifndef UNISTD_H
#define UNISTD_H
// #include "../../include/unistd.h"
//
// extern char **__environ;
//
// hidden int __dup3(int, int, int);
// hidden int __mkostemps(char *, int, int);
// hidden int __execvpe(const char *, char *const *, char *const *);
// hidden off_t __lseek(int, off_t, int);
#endif

View File

@@ -0,0 +1,21 @@
//
// Created by dongl on 25-8-12.
//
int exit(int code);
int libk_start_main(
int (*main)(int,char **),
int argc,
char **argv,
void (*init_dummy)(),
void (*fini_dummy)(),
void (*ldso_dummy)()
) {
(void)(ldso_dummy);
init_dummy();
exit(main(argc, argv));
fini_dummy();
return 0;
}

View File

@@ -4,6 +4,7 @@
#include <libk.h>
#include <memory.h>
#include <features.h>
#ifndef __asm_memcpy
// 复制 n 字节从 src 到 dest不处理重叠

View File

@@ -2,12 +2,16 @@
// Created by dongl on 25-7-31.
//
#include <libk.h>
#include <string.h>
#include <features.h>
size_t strlen(const char *str)
#ifndef __asm_strlen
General_API size_t strlen(const char *str)
{
size_t len = 0;
while (str[len] != '\0')
++len;
return len;
}
#endif