libc库增加tiny模式,最小体积更小

This commit is contained in:
zhangzheng
2024-01-21 00:11:06 +08:00
parent adde02a785
commit 5aab5811da
12 changed files with 162 additions and 27 deletions

View File

@@ -14,12 +14,14 @@ crt/*.S
crt/*.s)
list(REMOVE_ITEM deps ${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/crt/start_init.S)
list(REMOVE_ITEM deps ${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/crt/start.S)
list(REMOVE_ITEM deps ${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/crt/start_tiny.S)
list(REMOVE_ITEM deps ${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/ldso/dlstart.c)
list(REMOVE_ITEM deps ${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/ldso/dynlink.c)
list(REMOVE_ITEM deps ${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/crt/rcrt1.c)
add_library(start_init ${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/crt/start_init.S)
add_library(start ${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/crt/start.S)
add_library(start_tiny ${CMAKE_SOURCE_DIR}/mkrtos_user/lib/mlibc/crt/start_tiny.S)
add_library(muslc ${deps})
target_link_libraries(

View File

@@ -0,0 +1,21 @@
#include <features.h>
#include "libc.h"
#define START "_start"
// #include "crt_arch.h"
int main();
weak void _init();
weak void _fini();
int __libc_start_main_tiny(int (*main)(int, char **, char **), int argc, char **argv,
void (*init_dummy)(), void (*fini_dummy)());
extern void *app_start_addr;
void _start_tiny_init(long *p, void *start_addr)
{
int argc = p[0];
char **argv = (void *)(p + 1);
app_start_addr = start_addr; // ATShining add.
__libc_start_main_tiny(main, argc, argv, _init, _fini);
}

View File

@@ -0,0 +1,61 @@
.syntax unified
.section .first
.globl reloc
.align 2
.globl _start_
.type _start_, %function
_start_:
/*save r0 */
mov r12, r0
b __start
.SPACE 32 - (. - _start_)
_head_start:
.ascii "MKRTOS. "
.word __ram_size__
heap_offset: .word __heap_start__ - _start_
stack_offset: .word __stack_start__ - _start_
heap_size: .word __heap_size__
stack_size: .word __stack_size__
data_offset: .word __data_start__ - _start_
.word __bss_start__ - _start_
got_start: .word __got_start__
got_end: .word __got_end__
rel_start: .word __rel_start__
rel_end: .word __rel_end__
text_start: .word __text_start__
.SPACE 128 - (. - _start_)
.align
__start:
movs r1, #0
adr r2, _start_
bic r2, r2, #1
ldr r0, = __data_start__
add r0, r0, r2
ldr r3, = __data_end__
add r3, r3, r2
b LoopCopyDataInit
CopyDataInit:
ldr r2, [r0, r1]
str r2, [r9, r1]
adds r1, r1, #4
LoopCopyDataInit:
adds r2, r0, r1
cmp r2, r3
bcc CopyDataInit
mov r0, r9
adr r1, _start_
bic r1, r1, #1
bl _reloc
@ pop {r0, r12}
mov r0, sp
adr r1, _start_
bl _start_tiny_init
b .
.end

View File

@@ -0,0 +1,50 @@
#include <elf.h>
#include <poll.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include "libc.h"
static size_t fake_auxv[20];
#define AUX_CNT 38
int __libc_start_main_tiny(int (*main)(int, char **, char **), int argc, char **argv,
void (*init_dummy)(), void (*fini_dummy)())
{
char **envp = argv + argc + 1;
size_t i, *auxv, aux[AUX_CNT] = {0};
for (i = 0; envp[i]; i++)
;
libc.auxv = auxv = (void *)(envp + i + 1);
for (i = 0; auxv[i]; i += 2)
{
if (auxv[i] < AUX_CNT)
{
aux[auxv[i]] = auxv[i + 1];
}
else if (auxv[i] == 0xfe)
{
extern void u_env_init(void **in_env);
u_env_init((void *)auxv[i + 1]);
}
}
libc.page_size = 512;
libc.auxv = fake_auxv;
__init_tls(fake_auxv);
// _init();
extern void *app_start_addr;
extern weak hidden void (*const __init_array_start)(void), (*const __init_array_end)(void);
unsigned long start_addr = ((unsigned long)app_start_addr) & (~3UL);
uintptr_t a = (uintptr_t)&__init_array_start;
for (; a < (uintptr_t)&__init_array_end; a += sizeof(void (*)()))
{
((void (*)(void))((uintptr_t)(*((unsigned long *)a)) + start_addr | 0x1UL))();
}
exit(main(0, NULL, 0));
}

View File

@@ -1,6 +1,6 @@
#define HEAP_SIZE 512
#define STACK_SIZE 1024 * 2
#define STACK_SIZE (2048)
#if defined(__CC_ARM)
#define HEAP_ATTR SECTION("HEAP") __attribute__((zero_init))

View File

@@ -73,10 +73,10 @@ add_custom_target(
${CMAKE_SIZE} fatfs.elf
COMMAND
mkdir -p ${CMAKE_SOURCE_DIR}/build/output/cpio
COMMAND
cp fatfs.bin ${CMAKE_SOURCE_DIR}/build/output/cpio/fatfs
COMMAND
cp fatfs.elf ${CMAKE_SOURCE_DIR}/build/output/fatfs.elf
# COMMAND
# cp fatfs.bin ${CMAKE_SOURCE_DIR}/build/output/cpio/fatfs
# COMMAND
# cp fatfs.elf ${CMAKE_SOURCE_DIR}/build/output/fatfs.elf
)
add_dependencies(fatfs_dump fatfs.elf)

View File

@@ -11,7 +11,7 @@ add_executable(hello.elf
)
target_link_libraries(hello.elf
PUBLIC
start
start_tiny
muslc
sys
sys_util

View File

@@ -1,20 +1,21 @@
#include <stdio.h>
// #include <stdio.h>
#include <cons_cli.h>
int main(int argc, char *args[])
{
printf("print test0.\n");
printf("print test1.\n");
printf("print test2.\n");
float a = 1.1;
float b = 1.2;
float c;
cons_write_str("Hello world.\n");
// printf("print test0.\n");
// printf("print test1.\n");
// printf("print test2.\n");
// float a = 1.1;
// float b = 1.2;
// float c;
while (1)
{
c = a + b;
printf("%c %d %f\n", 'a', 1234, 1.1);
printf("%c %d %lf\n", 'a', 1234, a * b);
}
// while (1)
// {
// c = a + b;
// printf("%c %d %f\n", 'a', 1234, 1.1);
// printf("%c %d %lf\n", 'a', 1234, a * b);
// }
return 0;
}

View File

@@ -1,6 +1,6 @@
#define HEAP_SIZE 2048
#define STACK_SIZE 2048
#define STACK_SIZE (1024 + 256)
#if defined(__CC_ARM)
#define HEAP_ATTR SECTION("HEAP") __attribute__((zero_init))

View File

@@ -91,10 +91,10 @@ add_custom_target(
${CMAKE_SIZE} tcc.elf
COMMAND
mkdir -p ${CMAKE_SOURCE_DIR}/build/output/cpio
COMMAND
cp tcc.bin ${CMAKE_SOURCE_DIR}/build/output/cpio/tcc
COMMAND
cp tcc.elf ${CMAKE_SOURCE_DIR}/build/output/tcc.elf
# COMMAND
# cp tcc.bin ${CMAKE_SOURCE_DIR}/build/output/cpio/tcc
# COMMAND
# cp tcc.elf ${CMAKE_SOURCE_DIR}/build/output/tcc.elf
)
add_dependencies(tcc_dump tcc.elf)