diff --git a/.github/workflows/makefile.yml b/.github/workflows/makefile.yml index e6e7102a..efcad530 100644 --- a/.github/workflows/makefile.yml +++ b/.github/workflows/makefile.yml @@ -31,7 +31,6 @@ jobs: ARCH: ${{ matrix.arch }} run: | printf "\n" >> kernel/src/include/bindings/bindings.rs - printf "\n" >> user/libs/libc/src/include/internal/bindings/bindings.rs FMT_CHECK=1 make fmt kernel-static-test: diff --git a/.gitignore b/.gitignore index f3428642..52e685b5 100644 --- a/.gitignore +++ b/.gitignore @@ -10,8 +10,8 @@ kernel/kernel *.o *.a *.s +out serial_opt.txt -user/sys_api_lib docs/_build draft cppcheck.xml diff --git a/kernel/src/arch/x86_64/interrupt/handle.rs b/kernel/src/arch/x86_64/interrupt/handle.rs index 02050fea..033c16d7 100644 --- a/kernel/src/arch/x86_64/interrupt/handle.rs +++ b/kernel/src/arch/x86_64/interrupt/handle.rs @@ -20,8 +20,6 @@ unsafe extern "C" fn x86_64_do_irq(trap_frame: &mut TrapFrame, vector: u32) { if trap_frame.from_user() { x86_64::registers::segmentation::GS::swap(); - // 拒绝用户态中断 - return; } // 由于x86上面,虚拟中断号与物理中断号是一一对应的,所以这里直接使用vector作为中断号来查询irqdesc diff --git a/tools/write_disk_image.sh b/tools/write_disk_image.sh index 6f112a78..5aa915f6 100644 --- a/tools/write_disk_image.sh +++ b/tools/write_disk_image.sh @@ -115,7 +115,6 @@ mkdir -p ${root_folder}/bin/disk_mount/bin mkdir -p ${root_folder}/bin/disk_mount/dev mkdir -p ${root_folder}/bin/disk_mount/proc mkdir -p ${root_folder}/bin/disk_mount/usr -cp -r ${root_folder}/bin/user/* ${root_folder}/bin/disk_mount/bin touch ${root_folder}/bin/disk_mount/dev/keyboard.dev cp -r ${root_folder}/bin/sysroot/* ${root_folder}/bin/disk_mount/ diff --git a/user/.gitignore b/user/.gitignore new file mode 100644 index 00000000..ee27a21a --- /dev/null +++ b/user/.gitignore @@ -0,0 +1 @@ +/libs/ \ No newline at end of file diff --git a/user/Makefile b/user/Makefile index 1c888005..87ff4239 100644 --- a/user/Makefile +++ b/user/Makefile @@ -2,24 +2,14 @@ user_sub_dirs = apps SUBDIR_ROOTS := . DIRS := . $(shell find $(SUBDIR_ROOTS) -type d) -GARBAGE_PATTERNS := *.o sys_api_lib *.a +GARBAGE_PATTERNS := *.o *.a GARBAGE := $(foreach DIR,$(DIRS),$(addprefix $(DIR)/,$(GARBAGE_PATTERNS))) - -tmp_output_dir=$(ROOT_PATH)/bin/tmp/user -output_dir=$(ROOT_PATH)/bin/user - -CFLAGS := $(GLOBAL_CFLAGS) -I $(shell pwd)/libs -I $(shell pwd)/libs/libc/src/include -I $(shell pwd)/libs/libc/src/include/export -current_CFLAGS := $(CFLAGS) - DADK_VERSION=$(shell dadk -V | awk 'END {print $$2}') # 最小的DADK版本 -MIN_DADK_VERSION = 0.1.6 +MIN_DADK_VERSION = 0.1.8 DADK_CACHE_DIR = $(ROOT_PATH)/bin/dadk_cache -# 旧版的libc安装路径 -OLD_LIBC_INSTALL_PATH=$(ROOT_PATH)/bin/sysroot/usr/old_libc - ECHO: @echo "$@" @@ -56,58 +46,29 @@ dadk_run: install_dadk .PHONY: dadk_clean dadk_clean: install_dadk + @echo dadk_clean # 不运行dadk clean的原因是,把clean的工作交给应用程序自己去做,这样可以节省编译时间 #dadk --config-dir dadk/config --cache-dir $(DADK_CACHE_DIR) --dragonos-dir $(ROOT_PATH)/bin/sysroot clean src #dadk --config-dir dadk/config --cache-dir $(DADK_CACHE_DIR) --dragonos-dir $(ROOT_PATH)/bin/sysroot clean target -$(user_sub_dirs): ECHO sys_api_lib +all: + mkdir -p $(ROOT_PATH)/bin/sysroot + + $(MAKE) dadk_run + $(MAKE) copy_services - $(MAKE) -C $@ all CFLAGS="$(CFLAGS)" tmp_output_dir="$(tmp_output_dir)" output_dir="$(output_dir)" sys_libs_dir="$(shell pwd)/libs" + @echo 用户态程序编译完成 copy_services: dadk_run cp -r services/* $(ROOT_PATH)/bin/sysroot/etc/reach/system/ -app: $(user_sub_dirs) dadk_run copy_services -all: make_output_dir - - $(MAKE) app - - @echo 用户态程序编译完成 - -make_output_dir: ECHO - mkdir -p $(ROOT_PATH)/bin/user/ - mkdir -p $(ROOT_PATH)/bin/tmp/user - mkdir -p $(ROOT_PATH)/bin/sysroot/usr/include - mkdir -p $(ROOT_PATH)/bin/sysroot/usr/lib - - $(shell if [ ! -e $(tmp_output_dir) ];then mkdir -p $(tmp_output_dir); fi) - $(shell if [ ! -e $(output_dir) ];then mkdir -p $(output_dir); fi) -# 系统库 - -sys_api_lib_stage_1: make_output_dir - @echo Building sys_api_lib... - $(MAKE) -C libs all CFLAGS="$(CFLAGS)" tmp_output_dir="$(tmp_output_dir)" output_dir="$(output_dir)" sys_libs_dir="$(shell pwd)/libs" -sys_api_lib: sys_api_lib_stage_1 - -# 打包系统库 - mkdir -p $(ROOT_PATH)/bin/tmp/user/sys_api_lib_build_tmp - mkdir -p $(OLD_LIBC_INSTALL_PATH)/include - mkdir -p $(OLD_LIBC_INSTALL_PATH)/lib - $(AR) x libs/libc/target/x86_64-unknown-none/release/liblibc.a --output=$(ROOT_PATH)/bin/tmp/user/sys_api_lib_build_tmp - $(AR) crvs $(OLD_LIBC_INSTALL_PATH)/lib/libc.a $(shell find ./libs/* -name "*.o") $(shell find $(ROOT_PATH)/bin/tmp/user/sys_api_lib_build_tmp/* -name "*.o") - rm -rf $(ROOT_PATH)/bin/tmp/user/sys_api_lib_build_tmp -# $(shell find ./libs/* -name "*.o" | xargs -I {} cp {} $(ROOT_PATH)/bin/sysroot/usr/lib/) - $(shell cp -r $(ROOT_PATH)/user/libs/libc/src/include/export/* $(OLD_LIBC_INSTALL_PATH)/include/) - $(shell cp -r $(ROOT_PATH)/user/libs/libc/src/arch/x86_64/c*.o $(OLD_LIBC_INSTALL_PATH)/lib/) - .PHONY: clean -clean: +clean: rm -rf $(GARBAGE) $(MAKE) dadk_clean - $(MAKE) clean -C libs @list='$(user_sub_dirs)'; for subdir in $$list; do \ echo "Clean in dir: $$subdir";\ cd $$subdir && $(MAKE) clean;\ @@ -116,5 +77,4 @@ clean: .PHONY: fmt fmt: - FMT_CHECK=$(FMT_CHECK) $(MAKE) -C libs - FMT_CHECK=$(FMT_CHECK) $(MAKE) -C apps + FMT_CHECK=$(FMT_CHECK) $(MAKE) -C apps fmt diff --git a/user/apps/Makefile b/user/apps/Makefile index 87f9ff0b..ade05da8 100644 --- a/user/apps/Makefile +++ b/user/apps/Makefile @@ -1,15 +1,13 @@ - -user_apps_sub_dirs=shell about test_kvm - -ECHO: - @echo "$@" - -$(user_apps_sub_dirs): ECHO - - $(MAKE) -C $@ all CFLAGS="$(CFLAGS)" tmp_output_dir="$(tmp_output_dir)" output_dir="$(output_dir)" sys_libs_dir="$(sys_libs_dir)" - -all: $(user_apps_sub_dirs) +sub_dirs = $(wildcard */) .PHONY: clean +clean: + @list='$(sub_dirs)'; for subdir in $$list; do \ + $(MAKE) -C $$subdir clean;\ + done + +.PHONY: fmt fmt: - @echo "格式化代码: user/apps" + @list='$(sub_dirs)'; for subdir in $$list; do \ + FMT_CHECK=$(FMT_CHECK) $(MAKE) -C $$subdir fmt;\ + done diff --git a/user/apps/about/.gitignore b/user/apps/about/.gitignore index 3e4b7a5e..044003b3 100644 --- a/user/apps/about/.gitignore +++ b/user/apps/about/.gitignore @@ -1 +1,2 @@ -sys_version.h \ No newline at end of file +sys_version.h +about diff --git a/user/apps/about/Makefile b/user/apps/about/Makefile index 210d4afb..75787944 100644 --- a/user/apps/about/Makefile +++ b/user/apps/about/Makefile @@ -1,17 +1,26 @@ -OLD_LIBC_INSTALL_PATH=$(ROOT_PATH)/bin/sysroot/usr/old_libc +ifeq ($(ARCH), x86_64) + CROSS_COMPILE=x86_64-linux-musl- +else ifeq ($(ARCH), riscv64) + CROSS_COMPILE=riscv64-linux-musl- +endif # 获得当前git提交的sha1,并截取前8位 GIT_COMMIT_SHA1=$(shell git log -n 1 | head -n 1 | cut -d ' ' -f 2 | cut -c1-8) +CC=$(CROSS_COMPILE)gcc -all: about.o - $(LD) -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/about $(shell find . -name "*.o") $(OLD_LIBC_INSTALL_PATH)/lib/libc.a -T about.lds +all: version_header about.c + $(CC) -static -o about about.c - $(OBJCOPY) -I elf64-x86-64 -R ".eh_frame" -R ".comment" -O elf64-x86-64 $(tmp_output_dir)/about $(output_dir)/about.elf +.PHONY: install clean +install: all + mv about $(DADK_CURRENT_BUILD_DIR)/about.elf -about.o: version_header about.c - $(CC) $(CFLAGS) -c about.c -o about.o +clean: + rm about *.o # 生成版本头文件sys_version.h version_header: about.c @echo "#define DRAGONOS_GIT_COMMIT_SHA1 \"$(GIT_COMMIT_SHA1)\"" > sys_version.h + +fmt: diff --git a/user/apps/about/about.c b/user/apps/about/about.c index e26637a6..db2e78db 100644 --- a/user/apps/about/about.c +++ b/user/apps/about/about.c @@ -1,9 +1,7 @@ #include "sys_version.h" // 这是系统的版本头文件,在编译过程中自动生成 -#include #include -#include -#include #include + void print_ascii_logo() { printf(" ____ ___ ____ \n"); @@ -13,24 +11,25 @@ void print_ascii_logo() printf("|____/ |_| \\__,_| \\__, | \\___/ |_| |_| \\___/ |____/ \n"); printf(" |___/ \n"); } + void print_copyright() { printf(" DragonOS - An opensource operating system.\n"); printf(" Copyright: DragonOS Community. 2022-2024, All rights reserved.\n"); printf(" Version: "); - put_string("V0.1.8\n", COLOR_GREEN, COLOR_BLACK); + printf("\033[1;32m%s\033[0m", "V0.1.8\n"); printf(" Git commit SHA1: %s\n", DRAGONOS_GIT_COMMIT_SHA1); printf(" Build time: %s %s\n", __DATE__, __TIME__); printf(" \nYou can visit the project via:\n"); printf("\n"); - put_string(" Official Website: https://DragonOS.org\n", COLOR_INDIGO, COLOR_BLACK); - put_string(" GitHub: https://github.com/DragonOS-Community/DragonOS\n", COLOR_ORANGE, COLOR_BLACK); + printf("\x1B[1;36m%s\x1B[0m", " Official Website: https://DragonOS.org\n"); + printf("\x1B[1;33m%s\x1B[0m", " GitHub: https://github.com/DragonOS-Community/DragonOS\n"); printf("\n"); printf(" Maintainer: longjin \n"); printf(" Get contact with the community: \n"); printf("\n"); printf(" Join our development community:\n"); - put_string(" https://bbs.dragonos.org.cn\n", COLOR_ORANGE, COLOR_BLACK); + printf("\x1B[1;33m%s\x1B[0m", " https://bbs.dragonos.org.cn\n"); printf("\n"); } @@ -39,4 +38,4 @@ int main() print_ascii_logo(); print_copyright(); return 0; -} \ No newline at end of file +} diff --git a/user/apps/about/about.lds b/user/apps/about/about.lds deleted file mode 100644 index 6634d5e7..00000000 --- a/user/apps/about/about.lds +++ /dev/null @@ -1,54 +0,0 @@ - -OUTPUT_FORMAT("elf64-x86-64","elf64-x86-64","elf64-x86-64") -OUTPUT_ARCH(i386:x86-64) -ENTRY(_start) - -SECTIONS -{ - - . = 0x800000; - - - .text : - { - _text = .; - - *(.text) - *(.text.*) - - _etext = .; - } - . = ALIGN(8); - - .data : - { - _data = .; - *(.data) - *(.data.*) - - _edata = .; - } - - - rodata_start_pa = .; - .rodata : - { - _rodata = .; - *(.rodata) - *(.rodata.*) - _erodata = .; - } - - - .bss : - { - _bss = .; - *(.bss) - *(.bss.*) - _ebss = .; - } - - _end = .; - - -} \ No newline at end of file diff --git a/user/apps/dmesg/Makefile b/user/apps/dmesg/Makefile index 0bca7f4b..446c0f38 100644 --- a/user/apps/dmesg/Makefile +++ b/user/apps/dmesg/Makefile @@ -1,17 +1,20 @@ ifeq ($(ARCH), x86_64) -export PREFIX=x86_64-linux-musl- + CROSS_COMPILE=x86_64-linux-musl- else ifeq ($(ARCH), riscv64) -export PREFIX=riscv64-linux-musl- + CROSS_COMPILE=riscv64-linux-musl- endif +CC=$(CROSS_COMPILE)gcc -export CC=$(PREFIX)gcc -all: dmesg - mv dmesg $(DADK_CURRENT_BUILD_DIR) - -dmesg: main.c +all: main.c dmesg.c $(CC) -static -o dmesg main.c dmesg.c +.PHONY: install clean +install: all + mv dmesg $(DADK_CURRENT_BUILD_DIR)/dmesg + clean: - rm dmesg *.o \ No newline at end of file + rm dmesg *.o + +fmt: diff --git a/user/apps/dmesg/dmesg.c b/user/apps/dmesg/dmesg.c index 71ed5950..c8e2ef64 100644 --- a/user/apps/dmesg/dmesg.c +++ b/user/apps/dmesg/dmesg.c @@ -1,4 +1,6 @@ #include "dmesg.h" +#include +#include /** * @brief 识别dmesg程序的第一个选项参数 @@ -6,7 +8,7 @@ * @param arg dmesg命令第一个选项参数 * @return int 有效时返回对应选项码,无效时返回 -1 */ -int getopt(char *arg) +int getoption(char *arg) { if (!strcmp(arg, "-h") || !strcmp(arg, "--help")) return 0; diff --git a/user/apps/dmesg/dmesg.h b/user/apps/dmesg/dmesg.h index e2d0fbe9..40dce3ed 100644 --- a/user/apps/dmesg/dmesg.h +++ b/user/apps/dmesg/dmesg.h @@ -1,16 +1,12 @@ #pragma once -#include -#include -#include - /** * @brief 识别dmesg程序的第一个选项参数 * * @param arg dmesg命令第一个选项参数 * @return int 有效时返回对应选项码,无效时返回 -1 */ -int getopt(char *arg); +int getoption(char *arg); /** * @brief 识别dmesg程序的第二个选项参数 diff --git a/user/apps/dmesg/main.c b/user/apps/dmesg/main.c index ba479f71..41777969 100644 --- a/user/apps/dmesg/main.c +++ b/user/apps/dmesg/main.c @@ -1,4 +1,8 @@ #include "dmesg.h" +#include +#include +#include +#include int main(int argc, char **argv) { @@ -30,7 +34,7 @@ int main(int argc, char **argv) else { // 获取第一个选项参数 - opt = getopt(argv[1]); + opt = getoption(argv[1]); // 无效参数 if (opt == -1) diff --git a/user/apps/http_server/.gitignore b/user/apps/http_server/.gitignore new file mode 100644 index 00000000..91f20e78 --- /dev/null +++ b/user/apps/http_server/.gitignore @@ -0,0 +1 @@ +http_server diff --git a/user/apps/http_server/Makefile b/user/apps/http_server/Makefile index 2817222c..169af6eb 100644 --- a/user/apps/http_server/Makefile +++ b/user/apps/http_server/Makefile @@ -1,27 +1,20 @@ -CC=$(DragonOS_GCC)/x86_64-elf-gcc -LD=ld -OBJCOPY=objcopy -# 修改这里,把它改为你的relibc的sysroot路径 -RELIBC_OPT=$(DADK_BUILD_CACHE_DIR_RELIBC_0_1_0) -CFLAGS=-I $(RELIBC_OPT)/include -D__dragonos__ +ifeq ($(ARCH), x86_64) + CROSS_COMPILE=x86_64-linux-musl- +else ifeq ($(ARCH), riscv64) + CROSS_COMPILE=riscv64-linux-musl- +endif -tmp_output_dir=$(ROOT_PATH)/bin/tmp/user -output_dir=$(DADK_BUILD_CACHE_DIR_HTTP_SERVER_0_1_0) +CC=$(CROSS_COMPILE)gcc -LIBC_OBJS:=$(shell find $(RELIBC_OPT)/lib -name "*.o" | sort ) -LIBC_OBJS+=$(RELIBC_OPT)/lib/libc.a +.PHONY: all +all: main.c + $(CC) -static -o http_server main.c -all: main.o - mkdir -p $(tmp_output_dir) - - $(LD) -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/http_server $(shell find . -name "*.o") $(LIBC_OBJS) -T link.lds - - $(OBJCOPY) -I elf64-x86-64 -R ".eh_frame" -R ".comment" -O elf64-x86-64 $(tmp_output_dir)/http_server $(output_dir)/http_server.elf - - mv $(output_dir)/http_server.elf $(output_dir)/http_server - -main.o: main.c - $(CC) $(CFLAGS) -c main.c -o main.o +.PHONY: install clean +install: all + mv http_server $(DADK_CURRENT_BUILD_DIR)/http_server clean: - rm -f *.o \ No newline at end of file + rm http_server *.o + +fmt: diff --git a/user/apps/http_server/link.lds b/user/apps/http_server/link.lds deleted file mode 100644 index 1f2e57e4..00000000 --- a/user/apps/http_server/link.lds +++ /dev/null @@ -1,239 +0,0 @@ -/* Script for -z combreloc */ -/* Copyright (C) 2014-2020 Free Software Foundation, Inc. - Copying and distribution of this script, with or without modification, - are permitted in any medium without royalty provided the copyright - notice and this notice are preserved. */ -OUTPUT_FORMAT("elf64-x86-64", "elf64-x86-64", - "elf64-x86-64") -OUTPUT_ARCH(i386:x86-64) -ENTRY(_start) - -SECTIONS -{ - /* Read-only sections, merged into text segment: */ - PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000)); . = SEGMENT_START("text-segment", 0x20000000) + SIZEOF_HEADERS; - .interp : { *(.interp) } - .note.gnu.build-id : { *(.note.gnu.build-id) } - .hash : { *(.hash) } - .gnu.hash : { *(.gnu.hash) } - .dynsym : { *(.dynsym) } - .dynstr : { *(.dynstr) } - .gnu.version : { *(.gnu.version) } - .gnu.version_d : { *(.gnu.version_d) } - .gnu.version_r : { *(.gnu.version_r) } - .rela.dyn : - { - *(.rela.init) - *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*) - *(.rela.fini) - *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*) - *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*) - *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*) - *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*) - *(.rela.ctors) - *(.rela.dtors) - *(.rela.got) - *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*) - *(.rela.ldata .rela.ldata.* .rela.gnu.linkonce.l.*) - *(.rela.lbss .rela.lbss.* .rela.gnu.linkonce.lb.*) - *(.rela.lrodata .rela.lrodata.* .rela.gnu.linkonce.lr.*) - *(.rela.ifunc) - } - .rela.plt : - { - *(.rela.plt) - PROVIDE_HIDDEN (__rela_iplt_start = .); - *(.rela.iplt) - PROVIDE_HIDDEN (__rela_iplt_end = .); - } - . = ALIGN(CONSTANT (MAXPAGESIZE)); - .init : - { - KEEP (*(SORT_NONE(.init))) - } - .plt : { *(.plt) *(.iplt) } -.plt.got : { *(.plt.got) } -.plt.sec : { *(.plt.sec) } - .text : - { - *(.text.unlikely .text.*_unlikely .text.unlikely.*) - *(.text.exit .text.exit.*) - *(.text.startup .text.startup.*) - *(.text.hot .text.hot.*) - *(.text .stub .text.* .gnu.linkonce.t.*) - /* .gnu.warning sections are handled specially by elf.em. */ - *(.gnu.warning) - } - .fini : - { - KEEP (*(SORT_NONE(.fini))) - } - PROVIDE (__etext = .); - PROVIDE (_etext = .); - PROVIDE (etext = .); - . = ALIGN(CONSTANT (MAXPAGESIZE)); - /* Adjust the address for the rodata segment. We want to adjust up to - the same address within the page on the next page up. */ - . = SEGMENT_START("rodata-segment", ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1))); - .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } - .rodata1 : { *(.rodata1) } - .eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) } - .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) } - .gnu_extab : ONLY_IF_RO { *(.gnu_extab*) } - /* These sections are generated by the Sun/Oracle C++ compiler. */ - .exception_ranges : ONLY_IF_RO { *(.exception_ranges*) } - /* Adjust the address for the data segment. We want to adjust up to - the same address within the page on the next page up. */ - . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); - /* Exception handling */ - .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gnu_extab : ONLY_IF_RW { *(.gnu_extab) } - .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) } - .exception_ranges : ONLY_IF_RW { *(.exception_ranges*) } - /* Thread Local Storage sections */ - .tdata : - { - PROVIDE_HIDDEN (__tdata_start = .); - *(.tdata .tdata.* .gnu.linkonce.td.*) - } - .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) } - .preinit_array : - { - PROVIDE_HIDDEN (__preinit_array_start = .); - KEEP (*(.preinit_array)) - PROVIDE_HIDDEN (__preinit_array_end = .); - } - .init_array : - { - PROVIDE_HIDDEN (__init_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) - KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)) - PROVIDE_HIDDEN (__init_array_end = .); - } - .fini_array : - { - PROVIDE_HIDDEN (__fini_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))) - KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)) - PROVIDE_HIDDEN (__fini_array_end = .); - } - .ctors : - { - /* gcc uses crtbegin.o to find the start of - the constructors, so we make sure it is - first. Because this is a wildcard, it - doesn't matter if the user does not - actually link against crtbegin.o; the - linker won't look for a file to match a - wildcard. The wildcard also means that it - doesn't matter which directory crtbegin.o - is in. */ - KEEP (*crtbegin.o(.ctors)) - KEEP (*crtbegin?.o(.ctors)) - /* We don't want to include the .ctor section from - the crtend.o file until after the sorted ctors. - The .ctor section from the crtend file contains the - end of ctors marker and it must be last */ - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors)) - KEEP (*(SORT(.ctors.*))) - KEEP (*(.ctors)) - } - .dtors : - { - KEEP (*crtbegin.o(.dtors)) - KEEP (*crtbegin?.o(.dtors)) - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) - KEEP (*(SORT(.dtors.*))) - KEEP (*(.dtors)) - } - .jcr : { KEEP (*(.jcr)) } - .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) } - .dynamic : { *(.dynamic) } - .got : { *(.got) *(.igot) } - . = DATA_SEGMENT_RELRO_END (SIZEOF (.got.plt) >= 24 ? 24 : 0, .); - .got.plt : { *(.got.plt) *(.igot.plt) } - .data : - { - *(.data .data.* .gnu.linkonce.d.*) - SORT(CONSTRUCTORS) - } - .data1 : { *(.data1) } - _edata = .; PROVIDE (edata = .); - . = .; - __bss_start = .; - .bss : - { - *(.dynbss) - *(.bss .bss.* .gnu.linkonce.b.*) - *(COMMON) - /* Align here to ensure that the .bss section occupies space up to - _end. Align after .bss to ensure correct alignment even if the - .bss section disappears because there are no input sections. - FIXME: Why do we need it? When there is no .bss section, we do not - pad the .data section. */ - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - .lbss : - { - *(.dynlbss) - *(.lbss .lbss.* .gnu.linkonce.lb.*) - *(LARGE_COMMON) - } - . = ALIGN(64 / 8); - . = SEGMENT_START("ldata-segment", .); - .lrodata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.lrodata .lrodata.* .gnu.linkonce.lr.*) - } - .ldata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.ldata .ldata.* .gnu.linkonce.l.*) - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - . = ALIGN(64 / 8); - _end = .; PROVIDE (end = .); - . = DATA_SEGMENT_END (.); - /* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } - .gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) } - /* DWARF debug sections. - Symbols in the DWARF debugging sections are relative to the beginning - of the section so we begin them at 0. */ - /* DWARF 1 */ - .debug 0 : { *(.debug) } - .line 0 : { *(.line) } - /* GNU DWARF 1 extensions */ - .debug_srcinfo 0 : { *(.debug_srcinfo) } - .debug_sfnames 0 : { *(.debug_sfnames) } - /* DWARF 1.1 and DWARF 2 */ - .debug_aranges 0 : { *(.debug_aranges) } - .debug_pubnames 0 : { *(.debug_pubnames) } - /* DWARF 2 */ - .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end) } - .debug_frame 0 : { *(.debug_frame) } - .debug_str 0 : { *(.debug_str) } - .debug_loc 0 : { *(.debug_loc) } - .debug_macinfo 0 : { *(.debug_macinfo) } - /* SGI/MIPS DWARF 2 extensions */ - .debug_weaknames 0 : { *(.debug_weaknames) } - .debug_funcnames 0 : { *(.debug_funcnames) } - .debug_typenames 0 : { *(.debug_typenames) } - .debug_varnames 0 : { *(.debug_varnames) } - /* DWARF 3 */ - .debug_pubtypes 0 : { *(.debug_pubtypes) } - .debug_ranges 0 : { *(.debug_ranges) } - /* DWARF Extension. */ - .debug_macro 0 : { *(.debug_macro) } - .debug_addr 0 : { *(.debug_addr) } - .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) } - /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) } -} diff --git a/user/apps/shell/Makefile b/user/apps/shell/Makefile deleted file mode 100644 index 1ab5b2ce..00000000 --- a/user/apps/shell/Makefile +++ /dev/null @@ -1,18 +0,0 @@ -OLD_LIBC_INSTALL_PATH=$(ROOT_PATH)/bin/sysroot/usr/old_libc - -all: shell.o cmd.o cmd_help.o cmd_test.o - - $(LD) -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/shell $(shell find . -name "*.o") $(OLD_LIBC_INSTALL_PATH)/lib/libc.a -T shell.lds - - $(OBJCOPY) -I elf64-x86-64 -R ".eh_frame" -R ".comment" -O elf64-x86-64 $(tmp_output_dir)/shell $(output_dir)/shell.elf -shell.o: shell.c - $(CC) $(CFLAGS) -c shell.c -o shell.o - -cmd.o: cmd.c - $(CC) $(CFLAGS) -c cmd.c -o cmd.o - -cmd_test.o: cmd_test.c - $(CC) $(CFLAGS) -c cmd_test.c -o cmd_test.o - -cmd_help.o: cmd_help.c - $(CC) $(CFLAGS) -c cmd_help.c -o cmd_help.o diff --git a/user/apps/shell/cmd.c b/user/apps/shell/cmd.c deleted file mode 100644 index f412d14e..00000000 --- a/user/apps/shell/cmd.c +++ /dev/null @@ -1,679 +0,0 @@ -#include "cmd.h" -#include "cmd_help.h" -#include "cmd_test.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define MAX_PATH_LEN 4096 - -// 当前工作目录(在main_loop中初始化) -char *shell_current_path = NULL; - -/** - * @brief shell 内建函数的主命令与处理函数的映射表 - * - */ -struct built_in_cmd_t shell_cmds[] = { - {"cd", shell_cmd_cd}, - {"cat", shell_cmd_cat}, - {"exec", shell_cmd_exec}, - {"ls", shell_cmd_ls}, - {"mkdir", shell_cmd_mkdir}, - {"pwd", shell_cmd_pwd}, - {"rm", shell_cmd_rm}, - {"rmdir", shell_cmd_rmdir}, - {"reboot", shell_cmd_reboot}, - {"touch", shell_cmd_touch}, - {"about", shell_cmd_about}, - {"free", shell_cmd_free}, - {"help", shell_help}, - {"pipe", shell_pipe_test}, - {"pipe2", shell_pipe2_test}, - {"kill", shell_cmd_kill}, - -}; -// 总共的内建命令数量 -const static int total_built_in_cmd_num = sizeof(shell_cmds) / sizeof(struct built_in_cmd_t); - -/** - * @brief 将cwd与文件名进行拼接,得到最终的文件绝对路径 - * - * @param filename 文件名 - * @param result_path_len 结果字符串的大小 - * @return char* 结果字符串 - */ -static char *get_target_filepath(const char *filename, int *result_path_len) -{ - char *file_path = NULL; - if (filename[0] != '/') - { - int cwd_len = strlen(shell_current_path); - - // 计算文件完整路径的长度 - *result_path_len = cwd_len + strlen(filename); - - file_path = (char *)malloc(*result_path_len + 2); - - memset(file_path, 0, *result_path_len + 2); - - strncpy(file_path, shell_current_path, cwd_len); - - // 在文件路径中加入斜杠 - if (cwd_len > 1) - file_path[cwd_len] = '/'; - - // 拼接完整路径 - strcat(file_path, filename); - } - else - { - *result_path_len = strlen(filename); - file_path = (char *)malloc(*result_path_len + 2); - - memset(file_path, 0, *result_path_len + 2); - - strncpy(file_path, filename, *result_path_len); - if (filename[(*result_path_len) - 1] != '/') - file_path[*result_path_len] = '/'; - } - - return file_path; -} - -/** - * @brief 寻找对应的主命令编号 - * - * @param main_cmd 主命令 - * @return int 成功:主命令编号 - * 失败: -1 - */ -int shell_find_cmd(char *main_cmd) -{ - - for (int i = 0; i < total_built_in_cmd_num; ++i) - { - if (strcmp(main_cmd, shell_cmds[i].name) == 0) // 找到对应的命令号 - return i; - } - // 找不到该命令 - return -1; -} - -/** - * @brief 运行shell内建的命令 - * - * @param index 主命令编号 - * @param argc 参数数量 - * @param argv 参数列表 - */ -void shell_run_built_in_command(int index, int argc, char **argv) -{ - if (index >= total_built_in_cmd_num) - return; - // printf("run built-in command : %s\n", shell_cmds[index].name); - - shell_cmds[index].func(argc, argv); -} - -/** - * @brief cd命令:进入文件夹 - * - * @param argc - * @param argv - * @return int - */ - -int shell_cmd_cd(int argc, char **argv) -{ - - int current_dir_len = strlen(shell_current_path); - if (argc < 2) - { - shell_help_cd(); - goto done; - } - // 进入当前文件夹 - if (!strcmp(".", argv[1])) - goto done; - - // 进入父目录 - if (!strcmp("..", argv[1])) - { - - // 当前已经是根目录 - if (!strcmp("/", shell_current_path)) - goto done; - - // 返回到父目录 - int index = current_dir_len - 1; - for (; index > 1; --index) - { - if (shell_current_path[index] == '/') - break; - } - shell_current_path[index] = '\0'; - - // printf("switch to \" %s \"\n", shell_current_path); - goto done; - } - - int dest_len = strlen(argv[1]); - // 路径过长 - if (dest_len >= SHELL_CWD_MAX_SIZE - 1) - { - printf("ERROR: Path too long!\n"); - goto fail; - } - - if (argv[1][0] == '/') - { - // ======进入绝对路径===== - int ec = chdir(argv[1]); - if (ec == -1) - ec = errno; - if (ec == 0) - { - // 获取新的路径字符串 - char *new_path = (char *)malloc(MAX_PATH_LEN); - if (new_path==NULL) { - goto fail; - } - memset(new_path, 0, MAX_PATH_LEN); - getcwd(new_path, MAX_PATH_LEN); - - // 释放原有的路径字符串的内存空间 - free(shell_current_path); - - shell_current_path = new_path; - - return 0; - } - else - goto fail; - ; // 出错则直接忽略 - } - else // ======进入相对路径===== - { - int dest_offset = 0; - if (dest_len > 2) - { - if (argv[1][0] == '.' && argv[1][1] == '/') // 相对路径 - dest_offset = 2; - } - - int new_len = current_dir_len + dest_len - dest_offset; - - if (new_len >= SHELL_CWD_MAX_SIZE - 1) - { - printf("ERROR: Path too long!\n"); - goto fail; - } - - // 拼接出新的字符串 - char *new_path = (char *)malloc(new_len + 2); - memset(new_path, 0, new_len); - strncpy(new_path, shell_current_path, current_dir_len); - - if (current_dir_len > 1) - new_path[current_dir_len] = '/'; - strcat(new_path, argv[1] + dest_offset); - int x = chdir(new_path); - if (x == 0) // 成功切换目录 - { - free(new_path); - free(shell_current_path); - - char * pwd = malloc(MAX_PATH_LEN); - if (pwd==NULL) { - goto fail; - } - memset(pwd, 0, MAX_PATH_LEN); - getcwd(pwd, MAX_PATH_LEN); - shell_current_path = pwd; - goto done; - } - else - { - free(new_path); - printf("ERROR: Cannot switch to directory: %s\n", new_path); - goto fail; - } - } - -fail:; -done:; - // 释放参数所占的内存 - free(argv); - return 0; -} - -/** - * @brief 查看文件夹下的文件列表 - * - * @param argc - * @param argv - * @return int - */ -int shell_cmd_ls(int argc, char **argv) -{ - struct DIR *dir = opendir(shell_current_path); - - if (dir == NULL) - return -1; - - struct dirent *buf = NULL; - // printf("dir=%#018lx\n", dir); - - while (1) - { - buf = readdir(dir); - if (buf == NULL) - break; - - int color = COLOR_WHITE; - if (buf->d_type == DT_DIR) - color = COLOR_YELLOW; - else if (buf->d_type == DT_REG) - color = COLOR_INDIGO; - else if (buf->d_type == DT_BLK || buf->d_type == DT_CHR) - color = COLOR_GREEN; - - char output_buf[256] = {0}; - - sprintf(output_buf, "%s ", buf->d_name); - put_string(output_buf, color, COLOR_BLACK); - } - printf("\n"); - closedir(dir); - - if (argv != NULL) - free(argv); - - return 0; -} - -/** - * @brief 显示当前工作目录的命令 - * - * @param argc - * @param argv - * @return int - */ -int shell_cmd_pwd(int argc, char **argv) -{ - if (shell_current_path) - printf("%s\n", shell_current_path); - if (argv != NULL) - free(argv); - return 0; -} - -/** - * @brief 查看文件内容的命令 - * - * @param argc - * @param argv - * @return int - */ -int shell_cmd_cat(int argc, char **argv) -{ - int path_len = 0; - char *file_path = get_target_filepath(argv[1], &path_len); - - // 打开文件 - int fd = open(file_path, O_RDONLY); - if (fd <= 0) - { - printf("ERROR: Cannot open file: %s, fd=%d\n", file_path, fd); - return -1; - } - // 获取文件总大小 - int file_size = lseek(fd, 0, SEEK_END); - // 将文件指针切换回文件起始位置 - lseek(fd, 0, SEEK_SET); - - char *buf = (char *)malloc(512); - - while (file_size > 0) - { - memset(buf, 0, 512); - int l = read(fd, buf, 511); - if (l < 0) - { - printf("ERROR: Cannot read file: %s, errno = %d\n", file_path, errno); - return -1; - } - if (l == 0) - break; - buf[l] = '\0'; - - file_size -= l; - printf("%s", buf); - } - close(fd); - free(buf); - free(file_path); - if (argv != NULL) - free(argv); - return 0; -} - -/** - * @brief 创建空文件的命令 - * - * @param argc - * @param argv - * @return int - */ -int shell_cmd_touch(int argc, char **argv) -{ - int path_len = 0; - char *file_path; - bool alloc_full_path = false; - if (argv[1][0] == '/') - file_path = argv[1]; - else - { - file_path = get_target_filepath(argv[1], &path_len); - alloc_full_path = true; - } - - // 打开文件 - int fd = open(file_path, O_CREAT); - switch (fd) - { - case -ENOENT: - put_string("Parent dir not exists.\n", COLOR_RED, COLOR_BLACK); - break; - - default: - break; - } - close(fd); - if (argv != NULL) - free(argv); - if (alloc_full_path) - free(file_path); - return 0; -} - -/** - * @brief 创建文件夹的命令 - * - * @param argc - * @param argv - * @return int - */ -int shell_cmd_mkdir(int argc, char **argv) -{ - int result_path_len = -1; - char *full_path = NULL; - bool alloc_full_path = false; - if (argv[1][0] == '/') - full_path = argv[1]; - else - { - full_path = get_target_filepath(argv[1], &result_path_len); - alloc_full_path = true; - } - // printf("mkdir: full_path = %s\n", full_path); - int retval = mkdir(full_path, 0); - - if (argv != NULL) - free(argv); - if (alloc_full_path) - free(full_path); - return retval; -} - -/** - * @brief 删除文件夹的命令 - * - * @param argc - * @param argv - * @return int - */ -int shell_cmd_rmdir(int argc, char **argv) -{ - char *full_path = NULL; - int result_path_len = -1; - bool alloc_full_path = false; - - if (argv[1][0] == '/') - full_path = argv[1]; - else - { - full_path = get_target_filepath(argv[1], &result_path_len); - alloc_full_path = true; - } - int retval = rmdir(full_path); - if (retval != 0) - printf("Failed to remove %s, retval=%d\n", full_path, retval); - // printf("rmdir: path=%s, retval=%d\n", full_path, retval); - if (argv != NULL) - free(argv); - if (alloc_full_path) - free(full_path); - return retval; -} - -/** - * @brief 删除文件的命令 - * - * @param argc - * @param argv - * @return int - */ -int shell_cmd_rm(int argc, char **argv) -{ - char *full_path = NULL; - int result_path_len = -1; - int retval = 0; - bool alloc_full_path = false; - - if (argv[1][0] == '/') - full_path = argv[1]; - else - { - full_path = get_target_filepath(argv[1], &result_path_len); - alloc_full_path = true; - } - - retval = rm(full_path); - // printf("rmdir: path=%s, retval=%d\n", full_path, retval); - if (retval != 0) - printf("Failed to remove %s, retval=%d\n", full_path, retval); - if (alloc_full_path) - free(full_path); - if (argv != NULL) - free(argv); - return retval; -} - -/** - * @brief 执行新的程序的命令 - * - * @param argc - * @param argv - * @return int - */ -int shell_cmd_exec(int argc, char **argv) -{ - pid_t pid = fork(); - int retval = 0; - // printf(" pid=%d \n",pid); - - if (pid == 0) - { - - // 子进程 - int path_len = 0; - char *file_path = get_target_filepath(argv[1], &path_len); - // printf("before execv, path=%s, argc=%d\n", file_path, argc); - - char **real_argv = NULL; - if (argc > 1) - { - real_argv = &argv[1]; - } - execv(file_path, real_argv); - // printf("after execv, path=%s, argc=%d\n", file_path, argc); - free(argv); - free(file_path); - - exit(-1); - } - else - { - // 如果不指定后台运行,则等待退出 - if (strcmp(argv[argc - 1], "&") != 0) - waitpid(pid, &retval, 0); - else - printf("[1] %d\n", pid); // 输出子进程的pid - free(argv); - } -} - -int shell_cmd_about(int argc, char **argv) -{ - - if (argv != NULL) - free(argv); - int aac = 0; - char **aav; - - unsigned char input_buffer[INPUT_BUFFER_SIZE] = {0}; - - strcpy(input_buffer, "exec /bin/about.elf\0"); - - parse_command(input_buffer, &aac, &aav); - - return shell_cmd_exec(aac, aav); -} - -int shell_cmd_kill(int argc, char **argv) -{ - int retval = 0; - if (argc < 2) - { - printf("Usage: Kill \n"); - retval = -EINVAL; - goto out; - } - retval = kill(atoi(argv[1]), SIGKILL); -out:; - free(argv); - return retval; -} - -/** - * @brief 重启命令 - * - * @param argc - * @param argv - * @return int - */ -int shell_cmd_reboot(int argc, char **argv) -{ - return syscall_invoke(SYS_REBOOT, 0, 0, 0, 0, 0, 0); -} - -int shell_cmd_free(int argc, char **argv) -{ - int retval = 0; - if (argc == 2 && strcmp("-m", argv[1]) != 0) - { - retval = -EINVAL; - printf("Invalid argument: %s\n", argv[1]); - goto done; - } - - struct mstat_t mst = {0}; - retval = mstat(&mst); - if (retval != 0) - { - printf("Failed: retval=%d", retval); - goto done; - } - - printf("\ttotal\tused\tfree\tshared\tcache\tavailable\n"); - printf("Mem:\t"); - if (argc == 1) // 按照kb显示 - { - printf("%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t\n", mst.total, mst.used, mst.free, mst.shared, - mst.cache_used, mst.available); - } - else // 按照MB显示 - { - printf("%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t\n", mst.total >> 10, mst.used >> 10, mst.free >> 10, mst.shared >> 10, - mst.cache_used >> 10, mst.available >> 10); - } - -done:; - if (argv != NULL) - free(argv); - return retval; -} - -/** - * @brief 解析shell命令 - * - * @param buf 输入缓冲区 - * @param argc 返回值:参数数量 - * @param argv 返回值:参数列表 - * @return int 主命令的编号,小于零为无效命令 - */ -int parse_command(char *buf, int *argc, char ***argv) -{ - // printf("parse command\n"); - int index = 0; // 当前访问的是buf的第几位 - // 去除命令前导的空格 - while (index < INPUT_BUFFER_SIZE && buf[index] == ' ') - ++index; - // 如果去除前导空格后第一项为0x00,则归为空命令 - if (!buf[index]) - return -1; - - // 计算参数数量 - for (int i = index; i < (INPUT_BUFFER_SIZE - 1); ++i) - { - // 到达了字符串末尾 - if (!buf[i]) - break; - if (buf[i] != ' ' && (buf[i + 1] == ' ' || buf[i + 1] == '\0')) - ++(*argc); - } - - // printf("\nargc=%d\n", *argc); - - // 为指向每个指令的指针分配空间 - *argv = (char **)malloc(sizeof(char **) * (*argc + 1)); - memset(*argv, 0, sizeof(char **) * (*argc + 1)); - // 将每个命令都单独提取出来 - for (int i = 0; i < *argc && index < INPUT_BUFFER_SIZE; ++i) - { - // 提取出命令,以空格作为分割 - *((*argv) + i) = &buf[index]; - while (index < (INPUT_BUFFER_SIZE - 1) && buf[index] && buf[index] != ' ') - ++index; - buf[index++] = '\0'; - - // 删除命令间多余的空格 - while (index < INPUT_BUFFER_SIZE && buf[index] == ' ') - ++index; - - // printf("%s\n", (*argv)[i]); - } - // 以第一个命令作为主命令,查找其在命令表中的编号 - return shell_find_cmd(**argv); -} \ No newline at end of file diff --git a/user/apps/shell/cmd.h b/user/apps/shell/cmd.h deleted file mode 100644 index d979faa7..00000000 --- a/user/apps/shell/cmd.h +++ /dev/null @@ -1,154 +0,0 @@ -#pragma once - -// cwd字符串的最大大小 -#define SHELL_CWD_MAX_SIZE 256 -#define INPUT_BUFFER_SIZE 512 - -/** - * @brief shell内建命令结构体 - * - */ -struct built_in_cmd_t -{ - char *name; - int (*func)(int argc, char **argv); -}; - -extern struct built_in_cmd_t shell_cmds[]; -/** - * @brief 寻找对应的主命令编号 - * - * @param main_cmd 主命令 - * @return int 主命令编号 - */ -int shell_find_cmd(char *main_cmd); - - -/** - * @brief 运行shell内建的命令 - * - * @param index 主命令编号 - * @param argc 参数数量 - * @param argv 参数列表 - */ -void shell_run_built_in_command(int index, int argc, char **argv); - -/** - * @brief cd命令:进入文件夹 - * - * @param argc - * @param argv - * @return int - */ -int shell_cmd_cd(int argc, char **argv); - -/** - * @brief 查看文件夹下的文件列表 - * - * @param argc - * @param argv - * @return int - */ -int shell_cmd_ls(int argc, char **argv); - -/** - * @brief 显示当前工作目录的命令 - * - * @param argc - * @param argv - * @return int - */ -int shell_cmd_pwd(int argc, char **argv); - -/** - * @brief 查看文件内容的命令 - * - * @param argc - * @param argv - * @return int - */ -int shell_cmd_cat(int argc, char **argv); - -/** - * @brief 创建空文件的命令 - * - * @param argc - * @param argv - * @return int - */ -int shell_cmd_touch(int argc, char **argv); - -/** - * @brief 删除命令 - * - * @param argc - * @param argv - * @return int - */ -int shell_cmd_rm(int argc, char **argv); - -/** - * @brief 创建文件夹的命令 - * - * @param argc - * @param argv - * @return int - */ -int shell_cmd_mkdir(int argc, char **argv); - -/** - * @brief 删除文件夹的命令 - * - * @param argc - * @param argv - * @return int - */ -int shell_cmd_rmdir(int argc, char **argv); - -/** - * @brief 执行新的程序的命令 - * - * @param argc - * @param argv - * @return int - */ -int shell_cmd_exec(int argc, char **argv); - -/** - * @brief 重启命令 - * - * @param argc - * @param argv - * @return int - */ -int shell_cmd_reboot(int argc, char **argv); - -/** - * @brief 关于软件 - * - * @param argc - * @param argv - * @return int - */ -int shell_cmd_about(int argc, char **argv); - -/** - * @brief 显示系统内存空间信息的命令 - * - * @param argc - * @param argv - * @return int - */ -int shell_cmd_free(int argc, char **argv); - -/** - * @brief 解析shell命令 - * - * @param buf 输入缓冲区 - * @param argc 返回值:参数数量 - * @param argv 返回值:参数列表 - * @return int - */ -int parse_command(char *buf, int *argc, char ***argv); - -int shell_cmd_kill(int argc, char **argv); \ No newline at end of file diff --git a/user/apps/shell/cmd_help.c b/user/apps/shell/cmd_help.c deleted file mode 100644 index baa1ec10..00000000 --- a/user/apps/shell/cmd_help.c +++ /dev/null @@ -1,29 +0,0 @@ -#include "cmd_help.h" -#include -#include - -struct help_table_item_t -{ - void (*func)(); -}; -struct help_table_item_t help_table[] = { - {shell_help_cd}, -}; - -static const int help_table_num = sizeof(help_table) / sizeof(struct help_table_item_t); - -int shell_help(int argc, char **argv) -{ - printf("Help:\n"); - for (int i = 0; i < help_table_num; ++i) - help_table[i].func(); - - if (argc > 1) - free(argv); - return 0; -} - -void shell_help_cd() -{ - printf("Example of cd: cd [destination]\n"); -} \ No newline at end of file diff --git a/user/apps/shell/cmd_help.h b/user/apps/shell/cmd_help.h deleted file mode 100644 index 7cde19b9..00000000 --- a/user/apps/shell/cmd_help.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -#include "cmd.h" -int shell_help(int argc, char **argv); - -/** - * @brief cd命令的帮助信息 - * - */ -void shell_help_cd(); \ No newline at end of file diff --git a/user/apps/shell/cmd_test.c b/user/apps/shell/cmd_test.c deleted file mode 100644 index 0769813f..00000000 --- a/user/apps/shell/cmd_test.c +++ /dev/null @@ -1,166 +0,0 @@ -#include "cmd_test.h" -#include -#include -#include -#include -#include -#include -#include - -#define buf_SIZE 256 // 定义消息的最大长度 -int shell_pipe_test(int argc, char **argv) -{ - int fd[2], i, n; - - pid_t pid; - int ret = pipe(fd); // 创建一个管道 - if (ret < 0) - { - printf("pipe error"); - exit(1); - } - pid = fork(); // 创建一个子进程 - if (pid < 0) - { - printf("fork error"); - exit(1); - } - if (pid == 0) - { // 子进程 - close(fd[1]); // 关闭管道的写端 - for (i = 0; i < 3; i++) - { // 循环三次 - char buf[buf_SIZE] = {0}; - n = read(fd[0], buf, buf_SIZE); // 从管道的读端读取一条消息 - if (n > 0) - { - - printf("Child process received message: %s\n", buf); // 打印收到的消息 - if (strcmp(buf, "quit") == 0) - { // 如果收到的消息是"quit" - printf("Child process exits.\n"); // 打印退出信息 - break; // 跳出循环 - } - else - { // 如果收到的消息不是"quit" - printf("Child process is doing something...\n"); // 模拟子进程做一些操作 - usleep(100); - } - } - } - close(fd[0]); // 关闭管道的读端 - exit(0); - } - else - { // 父进程 - close(fd[0]); // 关闭管道的读端 - for (i = 0; i < 3; i++) - { // 循环三次 - char *msg = "hello world"; - if (i == 1) - { - msg = "how are you"; - usleep(1000); - } - if (i == 2) - { - msg = "quit"; - usleep(1000); - } - n = strlen(msg); - printf("Parent process send:%s\n", msg); - - write(fd[1], msg, n); // 向管道的写端写入一条消息 - if (strcmp(msg, "quit") == 0) - { // 如果发送的消息是"quit" - printf("Parent process exits.\n"); // 打印退出信息 - break; // 跳出循环 - } - } - close(fd[1]); // 关闭管道的写端 - wait(NULL); // 等待子进程结束 - } - return 0; -} -int shell_pipe2_test(int argc, char **argv) -{ - int fd[2], i, n; - - pid_t pid; - int ret = pipe2(fd, O_NONBLOCK); // 创建一个管道 - if (ret < 0) - { - printf("pipe error\n"); - exit(1); - } - pid = fork(); // 创建一个子进程 - if (pid < 0) - { - printf("fork error\n"); - exit(1); - } - if (pid == 0) - { // 子进程 - close(fd[1]); // 关闭管道的写端 - for (i = 0; i < 10; i++) - { - char buf[buf_SIZE] = {0}; - n = read(fd[0], buf, buf_SIZE); // 从管道的读端读取一条消息 - if (n > 0) - { - - printf("Child process received message: %s\n", buf); // 打印收到的消息 - if (strcmp(buf, "quit") == 0) - { // 如果收到的消息是"quit" - printf("Child process exits.\n"); // 打印退出信息 - break; // 跳出循环 - } - else - { // 如果收到的消息不是"quit" - printf("Child process is doing something...\n"); // 模拟子进程做一些操作 - // usleep(1000); - } - } - else - { - printf("read error,buf is empty\n"); - } - } - close(fd[0]); // 关闭管道的读端 - exit(0); - } - else - { // 父进程 - close(fd[0]); // 关闭管道的读端 - for (i = 0; i < 100; i++) - { - char *msg = "hello world"; - if (i < 99 & i > 0) - { - msg = "how are you"; - // usleep(1000); - } - if (i == 99) - { - msg = "quit"; - // usleep(1000); - } - n = strlen(msg); - printf("Parent process send:%s\n", msg); - - int r = write(fd[1], msg, n); // 向管道的写端写入一条消息 - if (r < 0) - { - printf("write error,buf is full\n"); - } - if (strcmp(msg, "quit") == 0) - { // 如果发送的消息是"quit" - printf("Parent process exits.\n"); // 打印退出信息 - break; // 跳出循环 - } - } - close(fd[1]); // 关闭管道的写端 - wait(NULL); // 等待子进程结束 - } - return 0; -} diff --git a/user/apps/shell/cmd_test.h b/user/apps/shell/cmd_test.h deleted file mode 100644 index b187ed1a..00000000 --- a/user/apps/shell/cmd_test.h +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once - -#include "cmd.h" -int shell_pipe_test(int argc, char **argv); -int shell_pipe2_test(int argc, char **argv); \ No newline at end of file diff --git a/user/apps/shell/shell.c b/user/apps/shell/shell.c deleted file mode 100644 index edacd1dc..00000000 --- a/user/apps/shell/shell.c +++ /dev/null @@ -1,272 +0,0 @@ -#include "cmd.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define pause_cpu() asm volatile("pause\n\t"); -#define MEM_HISTORY 1024 -/** - * @brief 循环读取每一行 - * - * @param fd 键盘文件描述符 - * @param buf 输入缓冲区 - * @return 读取的字符数 - */ - -int shell_readline(int fd, char *buf); -void print_ascii_logo(); -extern char *shell_current_path; -// 保存的历史命令(瞬时更改) -char history_commands[MEM_HISTORY][INPUT_BUFFER_SIZE]; -// 真正的历史命令 -char real_history_commands[MEM_HISTORY][INPUT_BUFFER_SIZE]; -int count_history; -// 现在对应的命令 -int current_command_index; -/** - * @brief shell主循环 - * - * @param kb_fd 键盘文件描述符 - */ -void main_loop(int kb_fd) -{ - count_history = 0; - current_command_index = 0; - unsigned char input_buffer[INPUT_BUFFER_SIZE] = {0}; - - // 初始化当前工作目录的路径 - shell_current_path = (char *)malloc(3); - - memset(shell_current_path, 0, 3); - shell_current_path[0] = '/'; - shell_current_path[1] = '\0'; - // shell命令行的主循环 - while (true) - { - int argc = 0; - char **argv; - - printf("[DragonOS] %s # ", shell_current_path); - - memset(input_buffer, 0, INPUT_BUFFER_SIZE); - - // 添加初始光标 - put_string(" ", COLOR_BLACK, COLOR_WHITE); - - // 循环读取每一行到buffer - count_history++; - int count = shell_readline(kb_fd, input_buffer); - if (!count || current_command_index < count_history - 1) - count_history--; - if (count) - { - strcpy(real_history_commands[count_history - 1], input_buffer); - count_history++; - memset(history_commands, 0, sizeof(history_commands)); - for (int i = 0; i <= count_history - 2; i++) - strcpy(history_commands[i], real_history_commands[i]); - current_command_index = count_history - 1; - } - if (count) - { - char command_origin[strlen(input_buffer)]; - strcpy(command_origin, input_buffer); - int cmd_num = parse_command(input_buffer, &argc, &argv); - printf("\n"); - - - if (cmd_num >= 0) - shell_run_built_in_command(cmd_num, argc, argv); - - - } - else - printf("\n"); - } -} - -int main() -{ - // 打开键盘文件 - // char kb_file_path[] = "/dev/char/ps2_keyboard"; - - // int kb_fd = open(kb_file_path, 0); - print_ascii_logo(); - // printf("before mkdir\n"); - // mkdir("/aaac", 0); - // printf("after mkdir\n"); - main_loop(0); - while (1) - ; -} -/** - * @brief 清除缓冲区 - * - * @param count 缓冲区大小 - * @param buf 缓冲区内容 - */ -void clear_command(int count, char *buf) -{ - for (int i = 0; i < count; i++) - printf("%c", '\b'); - memset(buf, 0, sizeof(buf)); -} -/** - * @brief 切换命令(写入到缓冲区) - * - * @param buf 缓冲区 - * @param type 如果为1,就向上,如果为-1,就向下 - */ -void change_command(char *buf, int type) -{ - current_command_index -= type; - // 处理边界 - if (current_command_index < 0) - current_command_index++; - if (current_command_index >= count_history - 1) - { - // 初始只含一条空历史记录,需单独考虑 - if (count_history == 1) - { - // 防止出现多条空历史记录 - if (current_command_index > 1) - current_command_index = 1; - } - else - current_command_index = count_history - 2; - } - - strcpy(buf, history_commands[current_command_index]); - printf("%s", buf); - put_string(" ", COLOR_BLACK, COLOR_WHITE); -} -/** - * @brief 循环读取每一行 - * - * @param fd 键盘文件描述符 - * @param buf 输入缓冲区 - * @return 读取的字符数 - */ -int shell_readline(int fd, char *buf) -{ - int key = 0; - int count = 0; - while (1) - { - // key = keyboard_analyze_keycode(fd); - key = getchar(); - // printf("key = %d\n", key); - if (key == 224) - { - key = getchar(); - // printf("key = %d\n", key); - switch (key) - { - case 72: - // 向上方向键 - if (count_history != 0) - { - // put_string(" ", COLOR_WHITE, COLOR_BLACK); - printf("%c", '\b'); - clear_command(count, buf); - count = 0; - // 向历史 - change_command(buf, 1); - count = strlen(buf); - } - key = 0xc8; - break; - case 80: - // 向下方向键 - if (count_history != 0) - { - // put_string(" ", COLOR_WHITE, COLOR_BLACK); - printf("%c", '\b'); - clear_command(count, buf); - count = 0; - // 向历史 - change_command(buf, -1); - count = strlen(buf); - } - key = 0x50; - break; - default: - break; - } - } - - if (key == '\n') - { - if (count > 0 && current_command_index >= count_history) - { - memset(history_commands[current_command_index - 1], 0, - sizeof(history_commands[current_command_index - 1])); - count_history--; - } - printf("%c", '\b'); - return count; - } - - if (key && key != 0xc8) - { - if (key == '\b') - { - if (count > 0) - { - // 回退去除先前光标 - printf("%c", '\b'); - // 去除字符 - printf("%c", '\b'); - buf[--count] = 0; - // 在最后一个字符处加光标 - put_string(" ", COLOR_BLACK, COLOR_WHITE); - } - } - else - { - printf("%c", '\b'); - buf[count++] = key; - printf("%c", key); - // 在最后一个字符处加光标 - put_string(" ", COLOR_BLACK, COLOR_WHITE); - } - if (count > 0 && current_command_index >= count_history) - { - memset(history_commands[count_history], 0, sizeof(history_commands[count_history])); - strcpy(history_commands[count_history], buf); - } - else if (count > 0) - { - memset(history_commands[current_command_index], 0, sizeof(history_commands[current_command_index])); - strcpy(history_commands[current_command_index], buf); - } - } - - // 输入缓冲区满了之后,直接返回 - if (count >= INPUT_BUFFER_SIZE - 1) - { - printf("%c", '\b'); - return count; - } - - pause_cpu(); - } -} - -void print_ascii_logo() -{ - printf("\n\n"); - printf(" ____ ___ ____ \n"); - printf("| _ \\ _ __ __ _ __ _ ___ _ __ / _ \\ / ___| \n"); - printf("| | | || '__| / _` | / _` | / _ \\ | '_ \\ | | | |\\___ \\ \n"); - printf("| |_| || | | (_| || (_| || (_) || | | || |_| | ___) |\n"); - printf("|____/ |_| \\__,_| \\__, | \\___/ |_| |_| \\___/ |____/ \n"); - printf(" |___/ \n"); - printf("\n\n"); -} \ No newline at end of file diff --git a/user/apps/shell/shell.lds b/user/apps/shell/shell.lds deleted file mode 100644 index 6634d5e7..00000000 --- a/user/apps/shell/shell.lds +++ /dev/null @@ -1,54 +0,0 @@ - -OUTPUT_FORMAT("elf64-x86-64","elf64-x86-64","elf64-x86-64") -OUTPUT_ARCH(i386:x86-64) -ENTRY(_start) - -SECTIONS -{ - - . = 0x800000; - - - .text : - { - _text = .; - - *(.text) - *(.text.*) - - _etext = .; - } - . = ALIGN(8); - - .data : - { - _data = .; - *(.data) - *(.data.*) - - _edata = .; - } - - - rodata_start_pa = .; - .rodata : - { - _rodata = .; - *(.rodata) - *(.rodata.*) - _erodata = .; - } - - - .bss : - { - _bss = .; - *(.bss) - *(.bss.*) - _ebss = .; - } - - _end = .; - - -} \ No newline at end of file diff --git a/user/apps/test_bind/.gitignore b/user/apps/test_bind/.gitignore new file mode 100644 index 00000000..ca12f1ce --- /dev/null +++ b/user/apps/test_bind/.gitignore @@ -0,0 +1 @@ +test_bind diff --git a/user/apps/test_bind/Makefile b/user/apps/test_bind/Makefile index ee0f0a37..363eddc8 100644 --- a/user/apps/test_bind/Makefile +++ b/user/apps/test_bind/Makefile @@ -1,27 +1,20 @@ -CC=$(DragonOS_GCC)/x86_64-elf-gcc -LD=ld -OBJCOPY=objcopy -# 修改这里,把它改为你的relibc的sysroot路径 -RELIBC_OPT=$(DADK_BUILD_CACHE_DIR_RELIBC_0_1_0) -CFLAGS=-I $(RELIBC_OPT)/include -D__dragonos__ +ifeq ($(ARCH), x86_64) + CROSS_COMPILE=x86_64-linux-musl- +else ifeq ($(ARCH), riscv64) + CROSS_COMPILE=riscv64-linux-musl- +endif -tmp_output_dir=$(ROOT_PATH)/bin/tmp/user -output_dir=$(DADK_BUILD_CACHE_DIR_TEST_BIND_0_1_0) +CC=$(CROSS_COMPILE)gcc -LIBC_OBJS:=$(shell find $(RELIBC_OPT)/lib -name "*.o" | sort ) -LIBC_OBJS+=$(RELIBC_OPT)/lib/libc.a +.PHONY: all +all: main.c + $(CC) -static -o test_bind main.c -all: main.o - mkdir -p $(tmp_output_dir) - - $(LD) -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/test_bind $(shell find . -name "*.o") $(LIBC_OBJS) -T link.lds - - $(OBJCOPY) -I elf64-x86-64 -R ".eh_frame" -R ".comment" -O elf64-x86-64 $(tmp_output_dir)/test_bind $(output_dir)/test_bind.elf - - mv $(output_dir)/test_bind.elf $(output_dir)/test_bind - -main.o: main.c - $(CC) $(CFLAGS) -c main.c -o main.o +.PHONY: install clean +install: all + mv test_bind $(DADK_CURRENT_BUILD_DIR)/test_bind clean: - rm -f *.o \ No newline at end of file + rm test_bind *.o + +fmt: diff --git a/user/apps/test_bind/link.lds b/user/apps/test_bind/link.lds deleted file mode 100644 index 1f2e57e4..00000000 --- a/user/apps/test_bind/link.lds +++ /dev/null @@ -1,239 +0,0 @@ -/* Script for -z combreloc */ -/* Copyright (C) 2014-2020 Free Software Foundation, Inc. - Copying and distribution of this script, with or without modification, - are permitted in any medium without royalty provided the copyright - notice and this notice are preserved. */ -OUTPUT_FORMAT("elf64-x86-64", "elf64-x86-64", - "elf64-x86-64") -OUTPUT_ARCH(i386:x86-64) -ENTRY(_start) - -SECTIONS -{ - /* Read-only sections, merged into text segment: */ - PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000)); . = SEGMENT_START("text-segment", 0x20000000) + SIZEOF_HEADERS; - .interp : { *(.interp) } - .note.gnu.build-id : { *(.note.gnu.build-id) } - .hash : { *(.hash) } - .gnu.hash : { *(.gnu.hash) } - .dynsym : { *(.dynsym) } - .dynstr : { *(.dynstr) } - .gnu.version : { *(.gnu.version) } - .gnu.version_d : { *(.gnu.version_d) } - .gnu.version_r : { *(.gnu.version_r) } - .rela.dyn : - { - *(.rela.init) - *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*) - *(.rela.fini) - *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*) - *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*) - *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*) - *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*) - *(.rela.ctors) - *(.rela.dtors) - *(.rela.got) - *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*) - *(.rela.ldata .rela.ldata.* .rela.gnu.linkonce.l.*) - *(.rela.lbss .rela.lbss.* .rela.gnu.linkonce.lb.*) - *(.rela.lrodata .rela.lrodata.* .rela.gnu.linkonce.lr.*) - *(.rela.ifunc) - } - .rela.plt : - { - *(.rela.plt) - PROVIDE_HIDDEN (__rela_iplt_start = .); - *(.rela.iplt) - PROVIDE_HIDDEN (__rela_iplt_end = .); - } - . = ALIGN(CONSTANT (MAXPAGESIZE)); - .init : - { - KEEP (*(SORT_NONE(.init))) - } - .plt : { *(.plt) *(.iplt) } -.plt.got : { *(.plt.got) } -.plt.sec : { *(.plt.sec) } - .text : - { - *(.text.unlikely .text.*_unlikely .text.unlikely.*) - *(.text.exit .text.exit.*) - *(.text.startup .text.startup.*) - *(.text.hot .text.hot.*) - *(.text .stub .text.* .gnu.linkonce.t.*) - /* .gnu.warning sections are handled specially by elf.em. */ - *(.gnu.warning) - } - .fini : - { - KEEP (*(SORT_NONE(.fini))) - } - PROVIDE (__etext = .); - PROVIDE (_etext = .); - PROVIDE (etext = .); - . = ALIGN(CONSTANT (MAXPAGESIZE)); - /* Adjust the address for the rodata segment. We want to adjust up to - the same address within the page on the next page up. */ - . = SEGMENT_START("rodata-segment", ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1))); - .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } - .rodata1 : { *(.rodata1) } - .eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) } - .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) } - .gnu_extab : ONLY_IF_RO { *(.gnu_extab*) } - /* These sections are generated by the Sun/Oracle C++ compiler. */ - .exception_ranges : ONLY_IF_RO { *(.exception_ranges*) } - /* Adjust the address for the data segment. We want to adjust up to - the same address within the page on the next page up. */ - . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); - /* Exception handling */ - .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gnu_extab : ONLY_IF_RW { *(.gnu_extab) } - .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) } - .exception_ranges : ONLY_IF_RW { *(.exception_ranges*) } - /* Thread Local Storage sections */ - .tdata : - { - PROVIDE_HIDDEN (__tdata_start = .); - *(.tdata .tdata.* .gnu.linkonce.td.*) - } - .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) } - .preinit_array : - { - PROVIDE_HIDDEN (__preinit_array_start = .); - KEEP (*(.preinit_array)) - PROVIDE_HIDDEN (__preinit_array_end = .); - } - .init_array : - { - PROVIDE_HIDDEN (__init_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) - KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)) - PROVIDE_HIDDEN (__init_array_end = .); - } - .fini_array : - { - PROVIDE_HIDDEN (__fini_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))) - KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)) - PROVIDE_HIDDEN (__fini_array_end = .); - } - .ctors : - { - /* gcc uses crtbegin.o to find the start of - the constructors, so we make sure it is - first. Because this is a wildcard, it - doesn't matter if the user does not - actually link against crtbegin.o; the - linker won't look for a file to match a - wildcard. The wildcard also means that it - doesn't matter which directory crtbegin.o - is in. */ - KEEP (*crtbegin.o(.ctors)) - KEEP (*crtbegin?.o(.ctors)) - /* We don't want to include the .ctor section from - the crtend.o file until after the sorted ctors. - The .ctor section from the crtend file contains the - end of ctors marker and it must be last */ - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors)) - KEEP (*(SORT(.ctors.*))) - KEEP (*(.ctors)) - } - .dtors : - { - KEEP (*crtbegin.o(.dtors)) - KEEP (*crtbegin?.o(.dtors)) - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) - KEEP (*(SORT(.dtors.*))) - KEEP (*(.dtors)) - } - .jcr : { KEEP (*(.jcr)) } - .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) } - .dynamic : { *(.dynamic) } - .got : { *(.got) *(.igot) } - . = DATA_SEGMENT_RELRO_END (SIZEOF (.got.plt) >= 24 ? 24 : 0, .); - .got.plt : { *(.got.plt) *(.igot.plt) } - .data : - { - *(.data .data.* .gnu.linkonce.d.*) - SORT(CONSTRUCTORS) - } - .data1 : { *(.data1) } - _edata = .; PROVIDE (edata = .); - . = .; - __bss_start = .; - .bss : - { - *(.dynbss) - *(.bss .bss.* .gnu.linkonce.b.*) - *(COMMON) - /* Align here to ensure that the .bss section occupies space up to - _end. Align after .bss to ensure correct alignment even if the - .bss section disappears because there are no input sections. - FIXME: Why do we need it? When there is no .bss section, we do not - pad the .data section. */ - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - .lbss : - { - *(.dynlbss) - *(.lbss .lbss.* .gnu.linkonce.lb.*) - *(LARGE_COMMON) - } - . = ALIGN(64 / 8); - . = SEGMENT_START("ldata-segment", .); - .lrodata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.lrodata .lrodata.* .gnu.linkonce.lr.*) - } - .ldata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.ldata .ldata.* .gnu.linkonce.l.*) - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - . = ALIGN(64 / 8); - _end = .; PROVIDE (end = .); - . = DATA_SEGMENT_END (.); - /* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } - .gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) } - /* DWARF debug sections. - Symbols in the DWARF debugging sections are relative to the beginning - of the section so we begin them at 0. */ - /* DWARF 1 */ - .debug 0 : { *(.debug) } - .line 0 : { *(.line) } - /* GNU DWARF 1 extensions */ - .debug_srcinfo 0 : { *(.debug_srcinfo) } - .debug_sfnames 0 : { *(.debug_sfnames) } - /* DWARF 1.1 and DWARF 2 */ - .debug_aranges 0 : { *(.debug_aranges) } - .debug_pubnames 0 : { *(.debug_pubnames) } - /* DWARF 2 */ - .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end) } - .debug_frame 0 : { *(.debug_frame) } - .debug_str 0 : { *(.debug_str) } - .debug_loc 0 : { *(.debug_loc) } - .debug_macinfo 0 : { *(.debug_macinfo) } - /* SGI/MIPS DWARF 2 extensions */ - .debug_weaknames 0 : { *(.debug_weaknames) } - .debug_funcnames 0 : { *(.debug_funcnames) } - .debug_typenames 0 : { *(.debug_typenames) } - .debug_varnames 0 : { *(.debug_varnames) } - /* DWARF 3 */ - .debug_pubtypes 0 : { *(.debug_pubtypes) } - .debug_ranges 0 : { *(.debug_ranges) } - /* DWARF Extension. */ - .debug_macro 0 : { *(.debug_macro) } - .debug_addr 0 : { *(.debug_addr) } - .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) } - /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) } -} diff --git a/user/apps/test_bind/main.c b/user/apps/test_bind/main.c index b5feb11e..50e3b0e9 100644 --- a/user/apps/test_bind/main.c +++ b/user/apps/test_bind/main.c @@ -2,7 +2,6 @@ #include #include #include -#include #include #include #include diff --git a/user/apps/test_fstat/.gitignore b/user/apps/test_fstat/.gitignore new file mode 100644 index 00000000..c1bfc164 --- /dev/null +++ b/user/apps/test_fstat/.gitignore @@ -0,0 +1 @@ +test_fstat \ No newline at end of file diff --git a/user/apps/test_fstat/Makefile b/user/apps/test_fstat/Makefile index 3a291c64..6fd5da61 100644 --- a/user/apps/test_fstat/Makefile +++ b/user/apps/test_fstat/Makefile @@ -1,26 +1,20 @@ -CC=$(DragonOS_GCC)/x86_64-elf-gcc -LD=ld -OBJCOPY=objcopy -# 修改这里,把它改为你的relibc的sysroot路径 -RELIBC_OPT=$(DADK_BUILD_CACHE_DIR_RELIBC_0_1_0) -CFLAGS=-I $(RELIBC_OPT)/include -D__dragonos__ +ifeq ($(ARCH), x86_64) + CROSS_COMPILE=x86_64-linux-musl- +else ifeq ($(ARCH), riscv64) + CROSS_COMPILE=riscv64-linux-musl- +endif -tmp_output_dir=$(ROOT_PATH)/bin/tmp/user -output_dir=$(DADK_BUILD_CACHE_DIR_TEST_FSTAT_0_1_0) +CC=$(CROSS_COMPILE)gcc -LIBC_OBJS:=$(shell find $(RELIBC_OPT)/lib -name "*.o" | sort ) -LIBC_OBJS+=$(RELIBC_OPT)/lib/libc.a +.PHONY: all +all: main.c + $(CC) -static -o test_fstat main.c -all: main.o - mkdir -p $(tmp_output_dir) - - $(LD) -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/test_fstat $(shell find . -name "*.o") $(LIBC_OBJS) -T link.lds - - $(OBJCOPY) -I elf64-x86-64 -R ".eh_frame" -R ".comment" -O elf64-x86-64 $(tmp_output_dir)/test_fstat $(output_dir)/test_fstat.elf - - mv $(output_dir)/test_fstat.elf $(output_dir)/test_fstat -main.o: main.c - $(CC) $(CFLAGS) -c main.c -o main.o +.PHONY: install clean +install: all + mv test_fstat $(DADK_CURRENT_BUILD_DIR)/test_fstat clean: - rm -f *.o \ No newline at end of file + rm test_fstat *.o + +fmt: diff --git a/user/apps/test_fstat/link.lds b/user/apps/test_fstat/link.lds deleted file mode 100644 index 1f2e57e4..00000000 --- a/user/apps/test_fstat/link.lds +++ /dev/null @@ -1,239 +0,0 @@ -/* Script for -z combreloc */ -/* Copyright (C) 2014-2020 Free Software Foundation, Inc. - Copying and distribution of this script, with or without modification, - are permitted in any medium without royalty provided the copyright - notice and this notice are preserved. */ -OUTPUT_FORMAT("elf64-x86-64", "elf64-x86-64", - "elf64-x86-64") -OUTPUT_ARCH(i386:x86-64) -ENTRY(_start) - -SECTIONS -{ - /* Read-only sections, merged into text segment: */ - PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000)); . = SEGMENT_START("text-segment", 0x20000000) + SIZEOF_HEADERS; - .interp : { *(.interp) } - .note.gnu.build-id : { *(.note.gnu.build-id) } - .hash : { *(.hash) } - .gnu.hash : { *(.gnu.hash) } - .dynsym : { *(.dynsym) } - .dynstr : { *(.dynstr) } - .gnu.version : { *(.gnu.version) } - .gnu.version_d : { *(.gnu.version_d) } - .gnu.version_r : { *(.gnu.version_r) } - .rela.dyn : - { - *(.rela.init) - *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*) - *(.rela.fini) - *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*) - *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*) - *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*) - *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*) - *(.rela.ctors) - *(.rela.dtors) - *(.rela.got) - *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*) - *(.rela.ldata .rela.ldata.* .rela.gnu.linkonce.l.*) - *(.rela.lbss .rela.lbss.* .rela.gnu.linkonce.lb.*) - *(.rela.lrodata .rela.lrodata.* .rela.gnu.linkonce.lr.*) - *(.rela.ifunc) - } - .rela.plt : - { - *(.rela.plt) - PROVIDE_HIDDEN (__rela_iplt_start = .); - *(.rela.iplt) - PROVIDE_HIDDEN (__rela_iplt_end = .); - } - . = ALIGN(CONSTANT (MAXPAGESIZE)); - .init : - { - KEEP (*(SORT_NONE(.init))) - } - .plt : { *(.plt) *(.iplt) } -.plt.got : { *(.plt.got) } -.plt.sec : { *(.plt.sec) } - .text : - { - *(.text.unlikely .text.*_unlikely .text.unlikely.*) - *(.text.exit .text.exit.*) - *(.text.startup .text.startup.*) - *(.text.hot .text.hot.*) - *(.text .stub .text.* .gnu.linkonce.t.*) - /* .gnu.warning sections are handled specially by elf.em. */ - *(.gnu.warning) - } - .fini : - { - KEEP (*(SORT_NONE(.fini))) - } - PROVIDE (__etext = .); - PROVIDE (_etext = .); - PROVIDE (etext = .); - . = ALIGN(CONSTANT (MAXPAGESIZE)); - /* Adjust the address for the rodata segment. We want to adjust up to - the same address within the page on the next page up. */ - . = SEGMENT_START("rodata-segment", ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1))); - .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } - .rodata1 : { *(.rodata1) } - .eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) } - .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) } - .gnu_extab : ONLY_IF_RO { *(.gnu_extab*) } - /* These sections are generated by the Sun/Oracle C++ compiler. */ - .exception_ranges : ONLY_IF_RO { *(.exception_ranges*) } - /* Adjust the address for the data segment. We want to adjust up to - the same address within the page on the next page up. */ - . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); - /* Exception handling */ - .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gnu_extab : ONLY_IF_RW { *(.gnu_extab) } - .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) } - .exception_ranges : ONLY_IF_RW { *(.exception_ranges*) } - /* Thread Local Storage sections */ - .tdata : - { - PROVIDE_HIDDEN (__tdata_start = .); - *(.tdata .tdata.* .gnu.linkonce.td.*) - } - .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) } - .preinit_array : - { - PROVIDE_HIDDEN (__preinit_array_start = .); - KEEP (*(.preinit_array)) - PROVIDE_HIDDEN (__preinit_array_end = .); - } - .init_array : - { - PROVIDE_HIDDEN (__init_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) - KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)) - PROVIDE_HIDDEN (__init_array_end = .); - } - .fini_array : - { - PROVIDE_HIDDEN (__fini_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))) - KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)) - PROVIDE_HIDDEN (__fini_array_end = .); - } - .ctors : - { - /* gcc uses crtbegin.o to find the start of - the constructors, so we make sure it is - first. Because this is a wildcard, it - doesn't matter if the user does not - actually link against crtbegin.o; the - linker won't look for a file to match a - wildcard. The wildcard also means that it - doesn't matter which directory crtbegin.o - is in. */ - KEEP (*crtbegin.o(.ctors)) - KEEP (*crtbegin?.o(.ctors)) - /* We don't want to include the .ctor section from - the crtend.o file until after the sorted ctors. - The .ctor section from the crtend file contains the - end of ctors marker and it must be last */ - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors)) - KEEP (*(SORT(.ctors.*))) - KEEP (*(.ctors)) - } - .dtors : - { - KEEP (*crtbegin.o(.dtors)) - KEEP (*crtbegin?.o(.dtors)) - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) - KEEP (*(SORT(.dtors.*))) - KEEP (*(.dtors)) - } - .jcr : { KEEP (*(.jcr)) } - .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) } - .dynamic : { *(.dynamic) } - .got : { *(.got) *(.igot) } - . = DATA_SEGMENT_RELRO_END (SIZEOF (.got.plt) >= 24 ? 24 : 0, .); - .got.plt : { *(.got.plt) *(.igot.plt) } - .data : - { - *(.data .data.* .gnu.linkonce.d.*) - SORT(CONSTRUCTORS) - } - .data1 : { *(.data1) } - _edata = .; PROVIDE (edata = .); - . = .; - __bss_start = .; - .bss : - { - *(.dynbss) - *(.bss .bss.* .gnu.linkonce.b.*) - *(COMMON) - /* Align here to ensure that the .bss section occupies space up to - _end. Align after .bss to ensure correct alignment even if the - .bss section disappears because there are no input sections. - FIXME: Why do we need it? When there is no .bss section, we do not - pad the .data section. */ - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - .lbss : - { - *(.dynlbss) - *(.lbss .lbss.* .gnu.linkonce.lb.*) - *(LARGE_COMMON) - } - . = ALIGN(64 / 8); - . = SEGMENT_START("ldata-segment", .); - .lrodata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.lrodata .lrodata.* .gnu.linkonce.lr.*) - } - .ldata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.ldata .ldata.* .gnu.linkonce.l.*) - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - . = ALIGN(64 / 8); - _end = .; PROVIDE (end = .); - . = DATA_SEGMENT_END (.); - /* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } - .gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) } - /* DWARF debug sections. - Symbols in the DWARF debugging sections are relative to the beginning - of the section so we begin them at 0. */ - /* DWARF 1 */ - .debug 0 : { *(.debug) } - .line 0 : { *(.line) } - /* GNU DWARF 1 extensions */ - .debug_srcinfo 0 : { *(.debug_srcinfo) } - .debug_sfnames 0 : { *(.debug_sfnames) } - /* DWARF 1.1 and DWARF 2 */ - .debug_aranges 0 : { *(.debug_aranges) } - .debug_pubnames 0 : { *(.debug_pubnames) } - /* DWARF 2 */ - .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end) } - .debug_frame 0 : { *(.debug_frame) } - .debug_str 0 : { *(.debug_str) } - .debug_loc 0 : { *(.debug_loc) } - .debug_macinfo 0 : { *(.debug_macinfo) } - /* SGI/MIPS DWARF 2 extensions */ - .debug_weaknames 0 : { *(.debug_weaknames) } - .debug_funcnames 0 : { *(.debug_funcnames) } - .debug_typenames 0 : { *(.debug_typenames) } - .debug_varnames 0 : { *(.debug_varnames) } - /* DWARF 3 */ - .debug_pubtypes 0 : { *(.debug_pubtypes) } - .debug_ranges 0 : { *(.debug_ranges) } - /* DWARF Extension. */ - .debug_macro 0 : { *(.debug_macro) } - .debug_addr 0 : { *(.debug_addr) } - .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) } - /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) } -} diff --git a/user/apps/test_fstat/main.c b/user/apps/test_fstat/main.c index 3938219c..44e03535 100644 --- a/user/apps/test_fstat/main.c +++ b/user/apps/test_fstat/main.c @@ -1,35 +1,34 @@ -#include -#include -#include #include #include #include +#include +#include +#include int main() { - int fd = open("/bin/about.elf", O_RDONLY); if (fd == -1) return 0; printf("fd = %d\n", fd); struct stat *st = (struct stat *)malloc(sizeof(struct stat)); fstat(fd, st); - printf("stat size = %d\n", sizeof(struct stat)); + printf("stat size = %lu\n", sizeof(struct stat)); // FIXME 打印数据时内存出错 printf("====================\n"); - printf("st address: %#018lx\n", st); - printf("st_dev = %d\n", (*st).st_dev); - printf("st_ino = %d\n", (*st).st_ino); + printf("st address: %p\n", st); + printf("st_dev = %lu\n", (*st).st_dev); + printf("st_ino = %lu\n", (*st).st_ino); printf("st_mode = %d\n", (*st).st_mode); - printf("st_nlink = %d\n", (*st).st_nlink); + printf("st_nlink = %lu\n", (*st).st_nlink); printf("st_uid = %d\n", (*st).st_uid); printf("st_gid = %d\n", (*st).st_gid); - printf("st_rdev = %d\n", (*st).st_rdev); - printf("st_size = %d\n", (*st).st_size); - printf("st_blksize = %d\n", (*st).st_blksize); - printf("st_blocks = %d\n", (*st).st_blocks); - printf("st_atim.sec= %d\tst_atim.nsec= %d\n", (*st).st_atim.tv_sec, (*st).st_atim.tv_nsec); - printf("st_mtim.sec= %d\tst_mtim.nsec= %d\n", (*st).st_mtim.tv_sec, (*st).st_mtim.tv_nsec); - printf("st_ctim.sec= %d\tst_ctim.nsec= %d\n", (*st).st_ctim.tv_sec, (*st).st_ctim.tv_nsec); + printf("st_rdev = %lu\n", (*st).st_rdev); + printf("st_size = %ld\n", (*st).st_size); + printf("st_blksize = %ld\n", (*st).st_blksize); + printf("st_blocks = %ld\n", (*st).st_blocks); + printf("st_atim.sec= %ld\tst_atim.nsec= %ld\n", (*st).st_atim.tv_sec, (*st).st_atim.tv_nsec); + printf("st_mtim.sec= %ld\tst_mtim.nsec= %ld\n", (*st).st_mtim.tv_sec, (*st).st_mtim.tv_nsec); + printf("st_ctim.sec= %ld\tst_ctim.nsec= %ld\n", (*st).st_ctim.tv_sec, (*st).st_ctim.tv_nsec); return 0; } \ No newline at end of file diff --git a/user/apps/test_gettimeofday/.gitignore b/user/apps/test_gettimeofday/.gitignore new file mode 100644 index 00000000..c98306ae --- /dev/null +++ b/user/apps/test_gettimeofday/.gitignore @@ -0,0 +1 @@ +test_gettimeofday diff --git a/user/apps/test_gettimeofday/Makefile b/user/apps/test_gettimeofday/Makefile index 16c2073e..11cbd7e3 100644 --- a/user/apps/test_gettimeofday/Makefile +++ b/user/apps/test_gettimeofday/Makefile @@ -1,25 +1,20 @@ -CC=$(DragonOS_GCC)/x86_64-elf-gcc -LD=ld -OBJCOPY=objcopy -# 修改这里,把它改为你的relibc的sysroot路径 -RELIBC_OPT=$(DADK_BUILD_CACHE_DIR_RELIBC_0_1_0) -CFLAGS=-I $(RELIBC_OPT)/include -D__dragonos__ +ifeq ($(ARCH), x86_64) + CROSS_COMPILE=x86_64-linux-musl- +else ifeq ($(ARCH), riscv64) + CROSS_COMPILE=riscv64-linux-musl- +endif -tmp_output_dir=$(ROOT_PATH)/bin/tmp/user -output_dir=$(DADK_BUILD_CACHE_DIR_TEST_GETTIMEOFDAY_0_1_0) +CC=$(CROSS_COMPILE)gcc -LIBC_OBJS:=$(shell find $(RELIBC_OPT)/lib -name "*.o" | sort ) -LIBC_OBJS+=$(RELIBC_OPT)/lib/libc.a +.PHONY: all +all: main.c + $(CC) -static -o test_gettimeofday main.c -all: main.o - mkdir -p $(tmp_output_dir) - - $(LD) -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/test_gettimeofday $(shell find . -name "*.o") $(LIBC_OBJS) -T link.lds - - $(OBJCOPY) -I elf64-x86-64 -R ".eh_frame" -R ".comment" -O elf64-x86-64 $(tmp_output_dir)/test_gettimeofday $(output_dir)/test_gettimeofday.elf - mv $(output_dir)/test_gettimeofday.elf $(output_dir)/test_gettimeofday -main.o: main.c - $(CC) $(CFLAGS) -c main.c -o main.o +.PHONY: install clean +install: all + mv test_gettimeofday $(DADK_CURRENT_BUILD_DIR)/test_gettimeofday clean: - rm -f *.o \ No newline at end of file + rm test_gettimeofday *.o + +fmt: diff --git a/user/apps/test_gettimeofday/link.lds b/user/apps/test_gettimeofday/link.lds deleted file mode 100644 index 1f2e57e4..00000000 --- a/user/apps/test_gettimeofday/link.lds +++ /dev/null @@ -1,239 +0,0 @@ -/* Script for -z combreloc */ -/* Copyright (C) 2014-2020 Free Software Foundation, Inc. - Copying and distribution of this script, with or without modification, - are permitted in any medium without royalty provided the copyright - notice and this notice are preserved. */ -OUTPUT_FORMAT("elf64-x86-64", "elf64-x86-64", - "elf64-x86-64") -OUTPUT_ARCH(i386:x86-64) -ENTRY(_start) - -SECTIONS -{ - /* Read-only sections, merged into text segment: */ - PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000)); . = SEGMENT_START("text-segment", 0x20000000) + SIZEOF_HEADERS; - .interp : { *(.interp) } - .note.gnu.build-id : { *(.note.gnu.build-id) } - .hash : { *(.hash) } - .gnu.hash : { *(.gnu.hash) } - .dynsym : { *(.dynsym) } - .dynstr : { *(.dynstr) } - .gnu.version : { *(.gnu.version) } - .gnu.version_d : { *(.gnu.version_d) } - .gnu.version_r : { *(.gnu.version_r) } - .rela.dyn : - { - *(.rela.init) - *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*) - *(.rela.fini) - *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*) - *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*) - *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*) - *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*) - *(.rela.ctors) - *(.rela.dtors) - *(.rela.got) - *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*) - *(.rela.ldata .rela.ldata.* .rela.gnu.linkonce.l.*) - *(.rela.lbss .rela.lbss.* .rela.gnu.linkonce.lb.*) - *(.rela.lrodata .rela.lrodata.* .rela.gnu.linkonce.lr.*) - *(.rela.ifunc) - } - .rela.plt : - { - *(.rela.plt) - PROVIDE_HIDDEN (__rela_iplt_start = .); - *(.rela.iplt) - PROVIDE_HIDDEN (__rela_iplt_end = .); - } - . = ALIGN(CONSTANT (MAXPAGESIZE)); - .init : - { - KEEP (*(SORT_NONE(.init))) - } - .plt : { *(.plt) *(.iplt) } -.plt.got : { *(.plt.got) } -.plt.sec : { *(.plt.sec) } - .text : - { - *(.text.unlikely .text.*_unlikely .text.unlikely.*) - *(.text.exit .text.exit.*) - *(.text.startup .text.startup.*) - *(.text.hot .text.hot.*) - *(.text .stub .text.* .gnu.linkonce.t.*) - /* .gnu.warning sections are handled specially by elf.em. */ - *(.gnu.warning) - } - .fini : - { - KEEP (*(SORT_NONE(.fini))) - } - PROVIDE (__etext = .); - PROVIDE (_etext = .); - PROVIDE (etext = .); - . = ALIGN(CONSTANT (MAXPAGESIZE)); - /* Adjust the address for the rodata segment. We want to adjust up to - the same address within the page on the next page up. */ - . = SEGMENT_START("rodata-segment", ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1))); - .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } - .rodata1 : { *(.rodata1) } - .eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) } - .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) } - .gnu_extab : ONLY_IF_RO { *(.gnu_extab*) } - /* These sections are generated by the Sun/Oracle C++ compiler. */ - .exception_ranges : ONLY_IF_RO { *(.exception_ranges*) } - /* Adjust the address for the data segment. We want to adjust up to - the same address within the page on the next page up. */ - . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); - /* Exception handling */ - .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gnu_extab : ONLY_IF_RW { *(.gnu_extab) } - .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) } - .exception_ranges : ONLY_IF_RW { *(.exception_ranges*) } - /* Thread Local Storage sections */ - .tdata : - { - PROVIDE_HIDDEN (__tdata_start = .); - *(.tdata .tdata.* .gnu.linkonce.td.*) - } - .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) } - .preinit_array : - { - PROVIDE_HIDDEN (__preinit_array_start = .); - KEEP (*(.preinit_array)) - PROVIDE_HIDDEN (__preinit_array_end = .); - } - .init_array : - { - PROVIDE_HIDDEN (__init_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) - KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)) - PROVIDE_HIDDEN (__init_array_end = .); - } - .fini_array : - { - PROVIDE_HIDDEN (__fini_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))) - KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)) - PROVIDE_HIDDEN (__fini_array_end = .); - } - .ctors : - { - /* gcc uses crtbegin.o to find the start of - the constructors, so we make sure it is - first. Because this is a wildcard, it - doesn't matter if the user does not - actually link against crtbegin.o; the - linker won't look for a file to match a - wildcard. The wildcard also means that it - doesn't matter which directory crtbegin.o - is in. */ - KEEP (*crtbegin.o(.ctors)) - KEEP (*crtbegin?.o(.ctors)) - /* We don't want to include the .ctor section from - the crtend.o file until after the sorted ctors. - The .ctor section from the crtend file contains the - end of ctors marker and it must be last */ - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors)) - KEEP (*(SORT(.ctors.*))) - KEEP (*(.ctors)) - } - .dtors : - { - KEEP (*crtbegin.o(.dtors)) - KEEP (*crtbegin?.o(.dtors)) - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) - KEEP (*(SORT(.dtors.*))) - KEEP (*(.dtors)) - } - .jcr : { KEEP (*(.jcr)) } - .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) } - .dynamic : { *(.dynamic) } - .got : { *(.got) *(.igot) } - . = DATA_SEGMENT_RELRO_END (SIZEOF (.got.plt) >= 24 ? 24 : 0, .); - .got.plt : { *(.got.plt) *(.igot.plt) } - .data : - { - *(.data .data.* .gnu.linkonce.d.*) - SORT(CONSTRUCTORS) - } - .data1 : { *(.data1) } - _edata = .; PROVIDE (edata = .); - . = .; - __bss_start = .; - .bss : - { - *(.dynbss) - *(.bss .bss.* .gnu.linkonce.b.*) - *(COMMON) - /* Align here to ensure that the .bss section occupies space up to - _end. Align after .bss to ensure correct alignment even if the - .bss section disappears because there are no input sections. - FIXME: Why do we need it? When there is no .bss section, we do not - pad the .data section. */ - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - .lbss : - { - *(.dynlbss) - *(.lbss .lbss.* .gnu.linkonce.lb.*) - *(LARGE_COMMON) - } - . = ALIGN(64 / 8); - . = SEGMENT_START("ldata-segment", .); - .lrodata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.lrodata .lrodata.* .gnu.linkonce.lr.*) - } - .ldata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.ldata .ldata.* .gnu.linkonce.l.*) - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - . = ALIGN(64 / 8); - _end = .; PROVIDE (end = .); - . = DATA_SEGMENT_END (.); - /* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } - .gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) } - /* DWARF debug sections. - Symbols in the DWARF debugging sections are relative to the beginning - of the section so we begin them at 0. */ - /* DWARF 1 */ - .debug 0 : { *(.debug) } - .line 0 : { *(.line) } - /* GNU DWARF 1 extensions */ - .debug_srcinfo 0 : { *(.debug_srcinfo) } - .debug_sfnames 0 : { *(.debug_sfnames) } - /* DWARF 1.1 and DWARF 2 */ - .debug_aranges 0 : { *(.debug_aranges) } - .debug_pubnames 0 : { *(.debug_pubnames) } - /* DWARF 2 */ - .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end) } - .debug_frame 0 : { *(.debug_frame) } - .debug_str 0 : { *(.debug_str) } - .debug_loc 0 : { *(.debug_loc) } - .debug_macinfo 0 : { *(.debug_macinfo) } - /* SGI/MIPS DWARF 2 extensions */ - .debug_weaknames 0 : { *(.debug_weaknames) } - .debug_funcnames 0 : { *(.debug_funcnames) } - .debug_typenames 0 : { *(.debug_typenames) } - .debug_varnames 0 : { *(.debug_varnames) } - /* DWARF 3 */ - .debug_pubtypes 0 : { *(.debug_pubtypes) } - .debug_ranges 0 : { *(.debug_ranges) } - /* DWARF Extension. */ - .debug_macro 0 : { *(.debug_macro) } - .debug_addr 0 : { *(.debug_addr) } - .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) } - /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) } -} diff --git a/user/apps/test_kvm/.gitignore b/user/apps/test_kvm/.gitignore new file mode 100644 index 00000000..ab5ff8b6 --- /dev/null +++ b/user/apps/test_kvm/.gitignore @@ -0,0 +1 @@ +test_kvm diff --git a/user/apps/test_kvm/Makefile b/user/apps/test_kvm/Makefile index 9ec4e293..ff0b0582 100644 --- a/user/apps/test_kvm/Makefile +++ b/user/apps/test_kvm/Makefile @@ -1,9 +1,20 @@ -OLD_LIBC_INSTALL_PATH=$(ROOT_PATH)/bin/sysroot/usr/old_libc +ifeq ($(ARCH), x86_64) + CROSS_COMPILE=x86_64-linux-musl- +else ifeq ($(ARCH), riscv64) + CROSS_COMPILE=riscv64-linux-musl- +endif -all: main.o +CC=$(CROSS_COMPILE)gcc - $(LD) -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/test_kvm $(shell find . -name "*.o") $(OLD_LIBC_INSTALL_PATH)/lib/libc.a -T link.lds +.PHONY: all +all: main.c + $(CC) -static -o test_kvm main.c - $(OBJCOPY) -I elf64-x86-64 -R ".eh_frame" -R ".comment" -O elf64-x86-64 $(tmp_output_dir)/test_kvm $(output_dir)/test_kvm.elf -main.o: main.c - $(CC) $(CFLAGS) -c main.c -o main.o +.PHONY: install clean +install: all + mv test_kvm $(DADK_CURRENT_BUILD_DIR)/test_kvm + +clean: + rm test_kvm *.o + +fmt: diff --git a/user/apps/test_kvm/bootstrap/Makefile b/user/apps/test_kvm/bootstrap/Makefile deleted file mode 100644 index 33a22e7c..00000000 --- a/user/apps/test_kvm/bootstrap/Makefile +++ /dev/null @@ -1,3 +0,0 @@ -boot.bin: boot.s - nasm boot.s -o boot.bin - xxd boot.bin > boot.hex \ No newline at end of file diff --git a/user/apps/test_kvm/bootstrap/boot.bin b/user/apps/test_kvm/bootstrap/boot.bin deleted file mode 100644 index 92ca3014..00000000 Binary files a/user/apps/test_kvm/bootstrap/boot.bin and /dev/null differ diff --git a/user/apps/test_kvm/bootstrap/boot.hex b/user/apps/test_kvm/bootstrap/boot.hex deleted file mode 100644 index f2df947f..00000000 --- a/user/apps/test_kvm/bootstrap/boot.hex +++ /dev/null @@ -1,32 +0,0 @@ -00000000: 8cc8 8ed8 8ec0 e802 00eb feb8 1e00 89c5 ................ -00000010: b910 00b8 0113 bb0c 00b2 00cd 10c3 4865 ..............He -00000020: 6c6c 6f2c 204f 5320 776f 726c 6421 0000 llo, OS world!.. -00000030: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -00000040: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -00000050: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -00000060: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -00000070: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -00000080: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -00000090: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -000000a0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -000000b0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -000000c0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -000000d0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -000000e0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -000000f0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -00000100: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -00000110: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -00000120: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -00000130: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -00000140: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -00000150: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -00000160: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -00000170: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -00000180: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -00000190: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -000001a0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -000001b0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -000001c0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -000001d0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -000001e0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ -000001f0: 0000 0000 0000 0000 0000 0000 0000 55aa ..............U. diff --git a/user/apps/test_kvm/link.lds b/user/apps/test_kvm/link.lds deleted file mode 100644 index 6634d5e7..00000000 --- a/user/apps/test_kvm/link.lds +++ /dev/null @@ -1,54 +0,0 @@ - -OUTPUT_FORMAT("elf64-x86-64","elf64-x86-64","elf64-x86-64") -OUTPUT_ARCH(i386:x86-64) -ENTRY(_start) - -SECTIONS -{ - - . = 0x800000; - - - .text : - { - _text = .; - - *(.text) - *(.text.*) - - _etext = .; - } - . = ALIGN(8); - - .data : - { - _data = .; - *(.data) - *(.data.*) - - _edata = .; - } - - - rodata_start_pa = .; - .rodata : - { - _rodata = .; - *(.rodata) - *(.rodata.*) - _erodata = .; - } - - - .bss : - { - _bss = .; - *(.bss) - *(.bss.*) - _ebss = .; - } - - _end = .; - - -} \ No newline at end of file diff --git a/user/apps/test_kvm/main.c b/user/apps/test_kvm/main.c index 817e16a1..fd60ccb6 100644 --- a/user/apps/test_kvm/main.c +++ b/user/apps/test_kvm/main.c @@ -14,10 +14,11 @@ * 1.在DragonOS的控制台输入 exec bin/test_kvm.elf * */ -#include -#include -#include #include +#include +#include +#include +#include #define KVM_CREATE_VCPU 0x00 #define KVM_SET_USER_MEMORY_REGION 0x01 diff --git a/user/apps/test_mkfifo/.gitignore b/user/apps/test_mkfifo/.gitignore new file mode 100644 index 00000000..39dccaf2 --- /dev/null +++ b/user/apps/test_mkfifo/.gitignore @@ -0,0 +1 @@ +test_mkfifo diff --git a/user/apps/test_mkfifo/Makefile b/user/apps/test_mkfifo/Makefile index a2a39cb4..1ef42e02 100644 --- a/user/apps/test_mkfifo/Makefile +++ b/user/apps/test_mkfifo/Makefile @@ -1,26 +1,20 @@ -CC=$(DragonOS_GCC)/x86_64-elf-gcc -LD=ld -OBJCOPY=objcopy -# 修改这里,把它改为你的relibc的sysroot路径 -RELIBC_OPT=$(DADK_BUILD_CACHE_DIR_RELIBC_0_1_0) -CFLAGS=-I $(RELIBC_OPT)/include -D__dragonos__ +ifeq ($(ARCH), x86_64) + CROSS_COMPILE=x86_64-linux-musl- +else ifeq ($(ARCH), riscv64) + CROSS_COMPILE=riscv64-linux-musl- +endif -tmp_output_dir=$(ROOT_PATH)/bin/tmp/user -output_dir=$(DADK_BUILD_CACHE_DIR_TEST_MKFIFO_0_1_0) +CC=$(CROSS_COMPILE)gcc -LIBC_OBJS:=$(shell find $(RELIBC_OPT)/lib -name "*.o" | sort ) -LIBC_OBJS+=$(RELIBC_OPT)/lib/libc.a +.PHONY: all +all: main.c + $(CC) -static -o test_mkfifo main.c -all: main.o - mkdir -p $(tmp_output_dir) - - $(LD) -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/test_mkfifo $(shell find . -name "*.o") $(LIBC_OBJS) -T link.lds - - $(OBJCOPY) -I elf64-x86-64 -R ".eh_frame" -R ".comment" -O elf64-x86-64 $(tmp_output_dir)/test_mkfifo $(output_dir)/test_mkfifo.elf - - mv $(output_dir)/test_mkfifo.elf $(output_dir)/test_mkfifo -main.o: main.c - $(CC) $(CFLAGS) -c main.c -o main.o +.PHONY: install clean +install: all + mv test_mkfifo $(DADK_CURRENT_BUILD_DIR)/test_mkfifo clean: - rm -f *.o \ No newline at end of file + rm test_mkfifo *.o + +fmt: diff --git a/user/apps/test_mkfifo/link.lds b/user/apps/test_mkfifo/link.lds deleted file mode 100644 index 1f2e57e4..00000000 --- a/user/apps/test_mkfifo/link.lds +++ /dev/null @@ -1,239 +0,0 @@ -/* Script for -z combreloc */ -/* Copyright (C) 2014-2020 Free Software Foundation, Inc. - Copying and distribution of this script, with or without modification, - are permitted in any medium without royalty provided the copyright - notice and this notice are preserved. */ -OUTPUT_FORMAT("elf64-x86-64", "elf64-x86-64", - "elf64-x86-64") -OUTPUT_ARCH(i386:x86-64) -ENTRY(_start) - -SECTIONS -{ - /* Read-only sections, merged into text segment: */ - PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000)); . = SEGMENT_START("text-segment", 0x20000000) + SIZEOF_HEADERS; - .interp : { *(.interp) } - .note.gnu.build-id : { *(.note.gnu.build-id) } - .hash : { *(.hash) } - .gnu.hash : { *(.gnu.hash) } - .dynsym : { *(.dynsym) } - .dynstr : { *(.dynstr) } - .gnu.version : { *(.gnu.version) } - .gnu.version_d : { *(.gnu.version_d) } - .gnu.version_r : { *(.gnu.version_r) } - .rela.dyn : - { - *(.rela.init) - *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*) - *(.rela.fini) - *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*) - *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*) - *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*) - *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*) - *(.rela.ctors) - *(.rela.dtors) - *(.rela.got) - *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*) - *(.rela.ldata .rela.ldata.* .rela.gnu.linkonce.l.*) - *(.rela.lbss .rela.lbss.* .rela.gnu.linkonce.lb.*) - *(.rela.lrodata .rela.lrodata.* .rela.gnu.linkonce.lr.*) - *(.rela.ifunc) - } - .rela.plt : - { - *(.rela.plt) - PROVIDE_HIDDEN (__rela_iplt_start = .); - *(.rela.iplt) - PROVIDE_HIDDEN (__rela_iplt_end = .); - } - . = ALIGN(CONSTANT (MAXPAGESIZE)); - .init : - { - KEEP (*(SORT_NONE(.init))) - } - .plt : { *(.plt) *(.iplt) } -.plt.got : { *(.plt.got) } -.plt.sec : { *(.plt.sec) } - .text : - { - *(.text.unlikely .text.*_unlikely .text.unlikely.*) - *(.text.exit .text.exit.*) - *(.text.startup .text.startup.*) - *(.text.hot .text.hot.*) - *(.text .stub .text.* .gnu.linkonce.t.*) - /* .gnu.warning sections are handled specially by elf.em. */ - *(.gnu.warning) - } - .fini : - { - KEEP (*(SORT_NONE(.fini))) - } - PROVIDE (__etext = .); - PROVIDE (_etext = .); - PROVIDE (etext = .); - . = ALIGN(CONSTANT (MAXPAGESIZE)); - /* Adjust the address for the rodata segment. We want to adjust up to - the same address within the page on the next page up. */ - . = SEGMENT_START("rodata-segment", ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1))); - .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } - .rodata1 : { *(.rodata1) } - .eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) } - .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) } - .gnu_extab : ONLY_IF_RO { *(.gnu_extab*) } - /* These sections are generated by the Sun/Oracle C++ compiler. */ - .exception_ranges : ONLY_IF_RO { *(.exception_ranges*) } - /* Adjust the address for the data segment. We want to adjust up to - the same address within the page on the next page up. */ - . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); - /* Exception handling */ - .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gnu_extab : ONLY_IF_RW { *(.gnu_extab) } - .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) } - .exception_ranges : ONLY_IF_RW { *(.exception_ranges*) } - /* Thread Local Storage sections */ - .tdata : - { - PROVIDE_HIDDEN (__tdata_start = .); - *(.tdata .tdata.* .gnu.linkonce.td.*) - } - .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) } - .preinit_array : - { - PROVIDE_HIDDEN (__preinit_array_start = .); - KEEP (*(.preinit_array)) - PROVIDE_HIDDEN (__preinit_array_end = .); - } - .init_array : - { - PROVIDE_HIDDEN (__init_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) - KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)) - PROVIDE_HIDDEN (__init_array_end = .); - } - .fini_array : - { - PROVIDE_HIDDEN (__fini_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))) - KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)) - PROVIDE_HIDDEN (__fini_array_end = .); - } - .ctors : - { - /* gcc uses crtbegin.o to find the start of - the constructors, so we make sure it is - first. Because this is a wildcard, it - doesn't matter if the user does not - actually link against crtbegin.o; the - linker won't look for a file to match a - wildcard. The wildcard also means that it - doesn't matter which directory crtbegin.o - is in. */ - KEEP (*crtbegin.o(.ctors)) - KEEP (*crtbegin?.o(.ctors)) - /* We don't want to include the .ctor section from - the crtend.o file until after the sorted ctors. - The .ctor section from the crtend file contains the - end of ctors marker and it must be last */ - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors)) - KEEP (*(SORT(.ctors.*))) - KEEP (*(.ctors)) - } - .dtors : - { - KEEP (*crtbegin.o(.dtors)) - KEEP (*crtbegin?.o(.dtors)) - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) - KEEP (*(SORT(.dtors.*))) - KEEP (*(.dtors)) - } - .jcr : { KEEP (*(.jcr)) } - .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) } - .dynamic : { *(.dynamic) } - .got : { *(.got) *(.igot) } - . = DATA_SEGMENT_RELRO_END (SIZEOF (.got.plt) >= 24 ? 24 : 0, .); - .got.plt : { *(.got.plt) *(.igot.plt) } - .data : - { - *(.data .data.* .gnu.linkonce.d.*) - SORT(CONSTRUCTORS) - } - .data1 : { *(.data1) } - _edata = .; PROVIDE (edata = .); - . = .; - __bss_start = .; - .bss : - { - *(.dynbss) - *(.bss .bss.* .gnu.linkonce.b.*) - *(COMMON) - /* Align here to ensure that the .bss section occupies space up to - _end. Align after .bss to ensure correct alignment even if the - .bss section disappears because there are no input sections. - FIXME: Why do we need it? When there is no .bss section, we do not - pad the .data section. */ - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - .lbss : - { - *(.dynlbss) - *(.lbss .lbss.* .gnu.linkonce.lb.*) - *(LARGE_COMMON) - } - . = ALIGN(64 / 8); - . = SEGMENT_START("ldata-segment", .); - .lrodata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.lrodata .lrodata.* .gnu.linkonce.lr.*) - } - .ldata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.ldata .ldata.* .gnu.linkonce.l.*) - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - . = ALIGN(64 / 8); - _end = .; PROVIDE (end = .); - . = DATA_SEGMENT_END (.); - /* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } - .gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) } - /* DWARF debug sections. - Symbols in the DWARF debugging sections are relative to the beginning - of the section so we begin them at 0. */ - /* DWARF 1 */ - .debug 0 : { *(.debug) } - .line 0 : { *(.line) } - /* GNU DWARF 1 extensions */ - .debug_srcinfo 0 : { *(.debug_srcinfo) } - .debug_sfnames 0 : { *(.debug_sfnames) } - /* DWARF 1.1 and DWARF 2 */ - .debug_aranges 0 : { *(.debug_aranges) } - .debug_pubnames 0 : { *(.debug_pubnames) } - /* DWARF 2 */ - .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end) } - .debug_frame 0 : { *(.debug_frame) } - .debug_str 0 : { *(.debug_str) } - .debug_loc 0 : { *(.debug_loc) } - .debug_macinfo 0 : { *(.debug_macinfo) } - /* SGI/MIPS DWARF 2 extensions */ - .debug_weaknames 0 : { *(.debug_weaknames) } - .debug_funcnames 0 : { *(.debug_funcnames) } - .debug_typenames 0 : { *(.debug_typenames) } - .debug_varnames 0 : { *(.debug_varnames) } - /* DWARF 3 */ - .debug_pubtypes 0 : { *(.debug_pubtypes) } - .debug_ranges 0 : { *(.debug_ranges) } - /* DWARF Extension. */ - .debug_macro 0 : { *(.debug_macro) } - .debug_addr 0 : { *(.debug_addr) } - .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) } - /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) } -} diff --git a/user/apps/test_mkfifo/main.c b/user/apps/test_mkfifo/main.c index 3efe8079..78763fa0 100644 --- a/user/apps/test_mkfifo/main.c +++ b/user/apps/test_mkfifo/main.c @@ -1,11 +1,11 @@ +#include #include #include -#include -#include -#include -#include #include +#include +#include #include +#include #define BUFFER_SIZE 256 #define PIPE_NAME "/bin/fifo" diff --git a/user/apps/test_relibc/Makefile b/user/apps/test_relibc/Makefile deleted file mode 100644 index bddc9a66..00000000 --- a/user/apps/test_relibc/Makefile +++ /dev/null @@ -1,24 +0,0 @@ -CC=$(DragonOS_GCC)/x86_64-elf-gcc -LD=ld -OBJCOPY=objcopy -# 修改这里,把它改为你的relibc的sysroot路径 -RELIBC_OPT=$(DADK_BUILD_CACHE_DIR_RELIBC_0_1_0) -CFLAGS=-I $(RELIBC_OPT)/include -D__dragonos__ - -tmp_output_dir=$(ROOT_PATH)/bin/tmp/user -output_dir=$(DADK_BUILD_CACHE_DIR_TEST_RELIBC_0_1_0) - -LIBC_OBJS:=$(shell find $(RELIBC_OPT)/lib -name "*.o" | sort ) -LIBC_OBJS+=$(RELIBC_OPT)/lib/libc.a - -all: main.o - mkdir -p $(tmp_output_dir) - - $(LD) -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/test $(shell find . -name "*.o") $(LIBC_OBJS) -T link.lds - - $(OBJCOPY) -I elf64-x86-64 -R ".eh_frame" -R ".comment" -O elf64-x86-64 $(tmp_output_dir)/test $(output_dir)/test.elf -main.o: main.c - $(CC) $(CFLAGS) -c main.c -o main.o - -clean: - rm -f *.o diff --git a/user/apps/test_relibc/link.lds b/user/apps/test_relibc/link.lds deleted file mode 100644 index 1f2e57e4..00000000 --- a/user/apps/test_relibc/link.lds +++ /dev/null @@ -1,239 +0,0 @@ -/* Script for -z combreloc */ -/* Copyright (C) 2014-2020 Free Software Foundation, Inc. - Copying and distribution of this script, with or without modification, - are permitted in any medium without royalty provided the copyright - notice and this notice are preserved. */ -OUTPUT_FORMAT("elf64-x86-64", "elf64-x86-64", - "elf64-x86-64") -OUTPUT_ARCH(i386:x86-64) -ENTRY(_start) - -SECTIONS -{ - /* Read-only sections, merged into text segment: */ - PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000)); . = SEGMENT_START("text-segment", 0x20000000) + SIZEOF_HEADERS; - .interp : { *(.interp) } - .note.gnu.build-id : { *(.note.gnu.build-id) } - .hash : { *(.hash) } - .gnu.hash : { *(.gnu.hash) } - .dynsym : { *(.dynsym) } - .dynstr : { *(.dynstr) } - .gnu.version : { *(.gnu.version) } - .gnu.version_d : { *(.gnu.version_d) } - .gnu.version_r : { *(.gnu.version_r) } - .rela.dyn : - { - *(.rela.init) - *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*) - *(.rela.fini) - *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*) - *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*) - *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*) - *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*) - *(.rela.ctors) - *(.rela.dtors) - *(.rela.got) - *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*) - *(.rela.ldata .rela.ldata.* .rela.gnu.linkonce.l.*) - *(.rela.lbss .rela.lbss.* .rela.gnu.linkonce.lb.*) - *(.rela.lrodata .rela.lrodata.* .rela.gnu.linkonce.lr.*) - *(.rela.ifunc) - } - .rela.plt : - { - *(.rela.plt) - PROVIDE_HIDDEN (__rela_iplt_start = .); - *(.rela.iplt) - PROVIDE_HIDDEN (__rela_iplt_end = .); - } - . = ALIGN(CONSTANT (MAXPAGESIZE)); - .init : - { - KEEP (*(SORT_NONE(.init))) - } - .plt : { *(.plt) *(.iplt) } -.plt.got : { *(.plt.got) } -.plt.sec : { *(.plt.sec) } - .text : - { - *(.text.unlikely .text.*_unlikely .text.unlikely.*) - *(.text.exit .text.exit.*) - *(.text.startup .text.startup.*) - *(.text.hot .text.hot.*) - *(.text .stub .text.* .gnu.linkonce.t.*) - /* .gnu.warning sections are handled specially by elf.em. */ - *(.gnu.warning) - } - .fini : - { - KEEP (*(SORT_NONE(.fini))) - } - PROVIDE (__etext = .); - PROVIDE (_etext = .); - PROVIDE (etext = .); - . = ALIGN(CONSTANT (MAXPAGESIZE)); - /* Adjust the address for the rodata segment. We want to adjust up to - the same address within the page on the next page up. */ - . = SEGMENT_START("rodata-segment", ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1))); - .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } - .rodata1 : { *(.rodata1) } - .eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) } - .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) } - .gnu_extab : ONLY_IF_RO { *(.gnu_extab*) } - /* These sections are generated by the Sun/Oracle C++ compiler. */ - .exception_ranges : ONLY_IF_RO { *(.exception_ranges*) } - /* Adjust the address for the data segment. We want to adjust up to - the same address within the page on the next page up. */ - . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); - /* Exception handling */ - .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gnu_extab : ONLY_IF_RW { *(.gnu_extab) } - .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) } - .exception_ranges : ONLY_IF_RW { *(.exception_ranges*) } - /* Thread Local Storage sections */ - .tdata : - { - PROVIDE_HIDDEN (__tdata_start = .); - *(.tdata .tdata.* .gnu.linkonce.td.*) - } - .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) } - .preinit_array : - { - PROVIDE_HIDDEN (__preinit_array_start = .); - KEEP (*(.preinit_array)) - PROVIDE_HIDDEN (__preinit_array_end = .); - } - .init_array : - { - PROVIDE_HIDDEN (__init_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) - KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)) - PROVIDE_HIDDEN (__init_array_end = .); - } - .fini_array : - { - PROVIDE_HIDDEN (__fini_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))) - KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)) - PROVIDE_HIDDEN (__fini_array_end = .); - } - .ctors : - { - /* gcc uses crtbegin.o to find the start of - the constructors, so we make sure it is - first. Because this is a wildcard, it - doesn't matter if the user does not - actually link against crtbegin.o; the - linker won't look for a file to match a - wildcard. The wildcard also means that it - doesn't matter which directory crtbegin.o - is in. */ - KEEP (*crtbegin.o(.ctors)) - KEEP (*crtbegin?.o(.ctors)) - /* We don't want to include the .ctor section from - the crtend.o file until after the sorted ctors. - The .ctor section from the crtend file contains the - end of ctors marker and it must be last */ - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors)) - KEEP (*(SORT(.ctors.*))) - KEEP (*(.ctors)) - } - .dtors : - { - KEEP (*crtbegin.o(.dtors)) - KEEP (*crtbegin?.o(.dtors)) - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) - KEEP (*(SORT(.dtors.*))) - KEEP (*(.dtors)) - } - .jcr : { KEEP (*(.jcr)) } - .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) } - .dynamic : { *(.dynamic) } - .got : { *(.got) *(.igot) } - . = DATA_SEGMENT_RELRO_END (SIZEOF (.got.plt) >= 24 ? 24 : 0, .); - .got.plt : { *(.got.plt) *(.igot.plt) } - .data : - { - *(.data .data.* .gnu.linkonce.d.*) - SORT(CONSTRUCTORS) - } - .data1 : { *(.data1) } - _edata = .; PROVIDE (edata = .); - . = .; - __bss_start = .; - .bss : - { - *(.dynbss) - *(.bss .bss.* .gnu.linkonce.b.*) - *(COMMON) - /* Align here to ensure that the .bss section occupies space up to - _end. Align after .bss to ensure correct alignment even if the - .bss section disappears because there are no input sections. - FIXME: Why do we need it? When there is no .bss section, we do not - pad the .data section. */ - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - .lbss : - { - *(.dynlbss) - *(.lbss .lbss.* .gnu.linkonce.lb.*) - *(LARGE_COMMON) - } - . = ALIGN(64 / 8); - . = SEGMENT_START("ldata-segment", .); - .lrodata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.lrodata .lrodata.* .gnu.linkonce.lr.*) - } - .ldata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.ldata .ldata.* .gnu.linkonce.l.*) - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - . = ALIGN(64 / 8); - _end = .; PROVIDE (end = .); - . = DATA_SEGMENT_END (.); - /* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } - .gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) } - /* DWARF debug sections. - Symbols in the DWARF debugging sections are relative to the beginning - of the section so we begin them at 0. */ - /* DWARF 1 */ - .debug 0 : { *(.debug) } - .line 0 : { *(.line) } - /* GNU DWARF 1 extensions */ - .debug_srcinfo 0 : { *(.debug_srcinfo) } - .debug_sfnames 0 : { *(.debug_sfnames) } - /* DWARF 1.1 and DWARF 2 */ - .debug_aranges 0 : { *(.debug_aranges) } - .debug_pubnames 0 : { *(.debug_pubnames) } - /* DWARF 2 */ - .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end) } - .debug_frame 0 : { *(.debug_frame) } - .debug_str 0 : { *(.debug_str) } - .debug_loc 0 : { *(.debug_loc) } - .debug_macinfo 0 : { *(.debug_macinfo) } - /* SGI/MIPS DWARF 2 extensions */ - .debug_weaknames 0 : { *(.debug_weaknames) } - .debug_funcnames 0 : { *(.debug_funcnames) } - .debug_typenames 0 : { *(.debug_typenames) } - .debug_varnames 0 : { *(.debug_varnames) } - /* DWARF 3 */ - .debug_pubtypes 0 : { *(.debug_pubtypes) } - .debug_ranges 0 : { *(.debug_ranges) } - /* DWARF Extension. */ - .debug_macro 0 : { *(.debug_macro) } - .debug_addr 0 : { *(.debug_addr) } - .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) } - /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) } -} diff --git a/user/apps/test_relibc/main.c b/user/apps/test_relibc/main.c deleted file mode 100644 index c44a5500..00000000 --- a/user/apps/test_relibc/main.c +++ /dev/null @@ -1,242 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#define CONN_QUEUE_SIZE 20 -#define BUFFER_SIZE 1024 -#define SERVER_PORT 12580 - -int server_sockfd; -int conn; - -void signal_handler(int signo) -{ - - printf("Server is exiting...\n"); - close(conn); - close(server_sockfd); - exit(0); -} - -static char logo[] = - " ____ ___ ____ \n| _ \\ _ __ __ _ __ _ ___ _ __ / _ \\ / ___| " - "\n| | | || '__| / _` | / _` | / _ \\ | '_ \\ | | | |\\___ \\ \n| |_| || | | (_| || (_| || (_) || | | || |_| | " - "___) |\n|____/ |_| \\__,_| \\__, | \\___/ |_| |_| \\___/ |____/ \n |___/ \n"; - -void tcp_server() -{ - printf("TCP Server is running...\n"); - server_sockfd = socket(AF_INET, SOCK_STREAM, 0); - printf("socket() ok, server_sockfd=%d\n", server_sockfd); - struct sockaddr_in server_sockaddr; - server_sockaddr.sin_family = AF_INET; - server_sockaddr.sin_port = htons(SERVER_PORT); - server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY); - - if (bind(server_sockfd, (struct sockaddr *)&server_sockaddr, sizeof(server_sockaddr))) - { - perror("Server bind error.\n"); - exit(1); - } - - printf("TCP Server is listening...\n"); - if (listen(server_sockfd, CONN_QUEUE_SIZE) == -1) - { - perror("Server listen error.\n"); - exit(1); - } - - printf("listen() ok\n"); - - char buffer[BUFFER_SIZE]; - struct sockaddr_in client_addr; - socklen_t client_length = sizeof(client_addr); - /* - Await a connection on socket FD. - When a connection arrives, open a new socket to communicate with it, - set *ADDR (which is *ADDR_LEN bytes long) to the address of the connecting - peer and *ADDR_LEN to the address's actual length, and return the - new socket's descriptor, or -1 for errors. - */ - conn = accept(server_sockfd, (struct sockaddr *)&client_addr, &client_length); - printf("Connection established.\n"); - if (conn < 0) - { - printf("Create connection failed, code=%d\n", conn); - exit(1); - } - send(conn, logo, sizeof(logo), 0); - while (1) - { - memset(buffer, 0, sizeof(buffer)); - int len = recv(conn, buffer, sizeof(buffer), 0); - if (len <= 0) - { - printf("Receive data failed! len=%d\n", len); - break; - } - if (strcmp(buffer, "exit\n") == 0) - { - break; - } - - printf("Received: %s\n", buffer); - send(conn, buffer, len, 0); - } - close(conn); - close(server_sockfd); -} - -void udp_server() -{ - printf("UDP Server is running...\n"); - server_sockfd = socket(AF_INET, SOCK_DGRAM, 0); - printf("socket() ok, server_sockfd=%d\n", server_sockfd); - struct sockaddr_in server_sockaddr; - server_sockaddr.sin_family = AF_INET; - server_sockaddr.sin_port = htons(SERVER_PORT); - server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY); - - if (bind(server_sockfd, (struct sockaddr *)&server_sockaddr, sizeof(server_sockaddr))) - { - perror("Server bind error.\n"); - exit(1); - } - - printf("UDP Server is listening...\n"); - - char buffer[BUFFER_SIZE]; - struct sockaddr_in client_addr; - socklen_t client_length = sizeof(client_addr); - - while (1) - { - memset(buffer, 0, sizeof(buffer)); - int len = recvfrom(server_sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *)&client_addr, &client_length); - if (len <= 0) - { - printf("Receive data failed! len=%d", len); - break; - } - if (strcmp(buffer, "exit\n") == 0) - { - break; - } - - printf("Received: %s", buffer); - sendto(server_sockfd, buffer, len, 0, (struct sockaddr *)&client_addr, client_length); - printf("Send: %s", buffer); - } - close(conn); - close(server_sockfd); -} - -void tcp_client() -{ - printf("Client is running...\n"); - int client_sockfd = socket(AF_INET, SOCK_STREAM, 0); - - struct sockaddr_in server_addr = {0}; - server_addr.sin_family = AF_INET; - server_addr.sin_port = htons(12581); - server_addr.sin_addr.s_addr = inet_addr("192.168.199.129"); - printf("to connect\n"); - if (connect(client_sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) - { - perror("Failed to establish connection to server\n"); - exit(1); - } - printf("connected to server\n"); - - char sendbuf[BUFFER_SIZE] = {0}; - char recvbuf[BUFFER_SIZE] = {0}; - - int x = recv(client_sockfd, recvbuf, sizeof(recvbuf), 0); - - fputs(recvbuf, stdout); - - memset(recvbuf, 0, sizeof(recvbuf)); - - while (1) - { - fgets(sendbuf, sizeof(sendbuf), stdin); - sendbuf[0] = 'a'; - - // printf("to send\n"); - send(client_sockfd, sendbuf, strlen(sendbuf), 0); - // printf("send ok\n"); - if (strcmp(sendbuf, "exit\n") == 0) - { - break; - } - - int x = recv(client_sockfd, recvbuf, sizeof(recvbuf), 0); - if (x < 0) - { - printf("recv error, retval=%d\n", x); - break; - } - - fputs(recvbuf, stdout); - - memset(recvbuf, 0, sizeof(recvbuf)); - memset(sendbuf, 0, sizeof(sendbuf)); - } - close(client_sockfd); -} - -void udp_client() -{ - struct sockaddr_in addr; - int sockfd, len = 0; - int addr_len = sizeof(struct sockaddr_in); - char buffer[256]; - - /* 建立socket,注意必须是SOCK_DGRAM */ - if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) - { - perror("socket"); - exit(1); - } - - /* 填写sockaddr_in*/ - bzero(&addr, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = htons(12581); - addr.sin_addr.s_addr = inet_addr("192.168.199.129"); - - printf("to send logo\n"); - sendto(sockfd, logo, sizeof(logo), 0, (struct sockaddr *)&addr, addr_len); - printf("send logo ok\n"); - while (1) - { - bzero(buffer, sizeof(buffer)); - - printf("Please enter a string to send to server: \n"); - - /* 从标准输入设备取得字符串*/ - len = read(STDIN_FILENO, buffer, sizeof(buffer)); - printf("to send: %d\n", len); - /* 将字符串传送给server端*/ - sendto(sockfd, buffer, len, 0, (struct sockaddr *)&addr, addr_len); - - /* 接收server端返回的字符串*/ - len = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *)&addr, &addr_len); - printf("Receive from server: %s\n", buffer); - } - - return 0; -} -void main() -{ - // signal(SIGKILL, signal_handler); - // signal(SIGINT, signal_handler); - tcp_server(); - // udp_server(); - // tcp_client(); - // udp_client(); -} \ No newline at end of file diff --git a/user/apps/test_signal/.gitignore b/user/apps/test_signal/.gitignore new file mode 100644 index 00000000..802b4439 --- /dev/null +++ b/user/apps/test_signal/.gitignore @@ -0,0 +1 @@ +test_signal diff --git a/user/apps/test_signal/Makefile b/user/apps/test_signal/Makefile index d25e475b..37759803 100644 --- a/user/apps/test_signal/Makefile +++ b/user/apps/test_signal/Makefile @@ -1,25 +1,20 @@ -CC=$(DragonOS_GCC)/x86_64-elf-gcc -LD=ld -OBJCOPY=objcopy -# 修改这里,把它改为你的relibc的sysroot路径 -RELIBC_OPT=$(DADK_BUILD_CACHE_DIR_RELIBC_0_1_0) -CFLAGS=-I $(RELIBC_OPT)/include -D__dragonos__ +ifeq ($(ARCH), x86_64) + CROSS_COMPILE=x86_64-linux-musl- +else ifeq ($(ARCH), riscv64) + CROSS_COMPILE=riscv64-linux-musl- +endif -tmp_output_dir=$(ROOT_PATH)/bin/tmp/user -output_dir=$(DADK_BUILD_CACHE_DIR_TEST_SIGNAL_0_1_0) +CC=$(CROSS_COMPILE)gcc +.PHONY: all +all: main.c + $(CC) -static -o test_signal main.c -LIBC_OBJS:=$(shell find $(RELIBC_OPT)/lib -name "*.o" | sort ) -LIBC_OBJS+=$(RELIBC_OPT)/lib/libc.a - -all: main.o - - $(LD) -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/test_signal $(shell find . -name "*.o") $(LIBC_OBJS) -T link.lds - - $(OBJCOPY) -I elf64-x86-64 -R ".eh_frame" -R ".comment" -O elf64-x86-64 $(tmp_output_dir)/test_signal $(output_dir)/test_signal.elf - mv $(output_dir)/test_signal.elf $(output_dir)/test_signal -main.o: main.c - $(CC) $(CFLAGS) -c main.c -o main.o +.PHONY: install clean +install: all + mv test_signal $(DADK_CURRENT_BUILD_DIR)/test_signal clean: - rm -f *.o \ No newline at end of file + rm test_signal *.o + +fmt: diff --git a/user/apps/test_signal/link.lds b/user/apps/test_signal/link.lds deleted file mode 100644 index 1f2e57e4..00000000 --- a/user/apps/test_signal/link.lds +++ /dev/null @@ -1,239 +0,0 @@ -/* Script for -z combreloc */ -/* Copyright (C) 2014-2020 Free Software Foundation, Inc. - Copying and distribution of this script, with or without modification, - are permitted in any medium without royalty provided the copyright - notice and this notice are preserved. */ -OUTPUT_FORMAT("elf64-x86-64", "elf64-x86-64", - "elf64-x86-64") -OUTPUT_ARCH(i386:x86-64) -ENTRY(_start) - -SECTIONS -{ - /* Read-only sections, merged into text segment: */ - PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000)); . = SEGMENT_START("text-segment", 0x20000000) + SIZEOF_HEADERS; - .interp : { *(.interp) } - .note.gnu.build-id : { *(.note.gnu.build-id) } - .hash : { *(.hash) } - .gnu.hash : { *(.gnu.hash) } - .dynsym : { *(.dynsym) } - .dynstr : { *(.dynstr) } - .gnu.version : { *(.gnu.version) } - .gnu.version_d : { *(.gnu.version_d) } - .gnu.version_r : { *(.gnu.version_r) } - .rela.dyn : - { - *(.rela.init) - *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*) - *(.rela.fini) - *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*) - *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*) - *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*) - *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*) - *(.rela.ctors) - *(.rela.dtors) - *(.rela.got) - *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*) - *(.rela.ldata .rela.ldata.* .rela.gnu.linkonce.l.*) - *(.rela.lbss .rela.lbss.* .rela.gnu.linkonce.lb.*) - *(.rela.lrodata .rela.lrodata.* .rela.gnu.linkonce.lr.*) - *(.rela.ifunc) - } - .rela.plt : - { - *(.rela.plt) - PROVIDE_HIDDEN (__rela_iplt_start = .); - *(.rela.iplt) - PROVIDE_HIDDEN (__rela_iplt_end = .); - } - . = ALIGN(CONSTANT (MAXPAGESIZE)); - .init : - { - KEEP (*(SORT_NONE(.init))) - } - .plt : { *(.plt) *(.iplt) } -.plt.got : { *(.plt.got) } -.plt.sec : { *(.plt.sec) } - .text : - { - *(.text.unlikely .text.*_unlikely .text.unlikely.*) - *(.text.exit .text.exit.*) - *(.text.startup .text.startup.*) - *(.text.hot .text.hot.*) - *(.text .stub .text.* .gnu.linkonce.t.*) - /* .gnu.warning sections are handled specially by elf.em. */ - *(.gnu.warning) - } - .fini : - { - KEEP (*(SORT_NONE(.fini))) - } - PROVIDE (__etext = .); - PROVIDE (_etext = .); - PROVIDE (etext = .); - . = ALIGN(CONSTANT (MAXPAGESIZE)); - /* Adjust the address for the rodata segment. We want to adjust up to - the same address within the page on the next page up. */ - . = SEGMENT_START("rodata-segment", ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1))); - .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } - .rodata1 : { *(.rodata1) } - .eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) } - .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) } - .gnu_extab : ONLY_IF_RO { *(.gnu_extab*) } - /* These sections are generated by the Sun/Oracle C++ compiler. */ - .exception_ranges : ONLY_IF_RO { *(.exception_ranges*) } - /* Adjust the address for the data segment. We want to adjust up to - the same address within the page on the next page up. */ - . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); - /* Exception handling */ - .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gnu_extab : ONLY_IF_RW { *(.gnu_extab) } - .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) } - .exception_ranges : ONLY_IF_RW { *(.exception_ranges*) } - /* Thread Local Storage sections */ - .tdata : - { - PROVIDE_HIDDEN (__tdata_start = .); - *(.tdata .tdata.* .gnu.linkonce.td.*) - } - .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) } - .preinit_array : - { - PROVIDE_HIDDEN (__preinit_array_start = .); - KEEP (*(.preinit_array)) - PROVIDE_HIDDEN (__preinit_array_end = .); - } - .init_array : - { - PROVIDE_HIDDEN (__init_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) - KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)) - PROVIDE_HIDDEN (__init_array_end = .); - } - .fini_array : - { - PROVIDE_HIDDEN (__fini_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))) - KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)) - PROVIDE_HIDDEN (__fini_array_end = .); - } - .ctors : - { - /* gcc uses crtbegin.o to find the start of - the constructors, so we make sure it is - first. Because this is a wildcard, it - doesn't matter if the user does not - actually link against crtbegin.o; the - linker won't look for a file to match a - wildcard. The wildcard also means that it - doesn't matter which directory crtbegin.o - is in. */ - KEEP (*crtbegin.o(.ctors)) - KEEP (*crtbegin?.o(.ctors)) - /* We don't want to include the .ctor section from - the crtend.o file until after the sorted ctors. - The .ctor section from the crtend file contains the - end of ctors marker and it must be last */ - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors)) - KEEP (*(SORT(.ctors.*))) - KEEP (*(.ctors)) - } - .dtors : - { - KEEP (*crtbegin.o(.dtors)) - KEEP (*crtbegin?.o(.dtors)) - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) - KEEP (*(SORT(.dtors.*))) - KEEP (*(.dtors)) - } - .jcr : { KEEP (*(.jcr)) } - .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) } - .dynamic : { *(.dynamic) } - .got : { *(.got) *(.igot) } - . = DATA_SEGMENT_RELRO_END (SIZEOF (.got.plt) >= 24 ? 24 : 0, .); - .got.plt : { *(.got.plt) *(.igot.plt) } - .data : - { - *(.data .data.* .gnu.linkonce.d.*) - SORT(CONSTRUCTORS) - } - .data1 : { *(.data1) } - _edata = .; PROVIDE (edata = .); - . = .; - __bss_start = .; - .bss : - { - *(.dynbss) - *(.bss .bss.* .gnu.linkonce.b.*) - *(COMMON) - /* Align here to ensure that the .bss section occupies space up to - _end. Align after .bss to ensure correct alignment even if the - .bss section disappears because there are no input sections. - FIXME: Why do we need it? When there is no .bss section, we do not - pad the .data section. */ - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - .lbss : - { - *(.dynlbss) - *(.lbss .lbss.* .gnu.linkonce.lb.*) - *(LARGE_COMMON) - } - . = ALIGN(64 / 8); - . = SEGMENT_START("ldata-segment", .); - .lrodata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.lrodata .lrodata.* .gnu.linkonce.lr.*) - } - .ldata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.ldata .ldata.* .gnu.linkonce.l.*) - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - . = ALIGN(64 / 8); - _end = .; PROVIDE (end = .); - . = DATA_SEGMENT_END (.); - /* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } - .gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) } - /* DWARF debug sections. - Symbols in the DWARF debugging sections are relative to the beginning - of the section so we begin them at 0. */ - /* DWARF 1 */ - .debug 0 : { *(.debug) } - .line 0 : { *(.line) } - /* GNU DWARF 1 extensions */ - .debug_srcinfo 0 : { *(.debug_srcinfo) } - .debug_sfnames 0 : { *(.debug_sfnames) } - /* DWARF 1.1 and DWARF 2 */ - .debug_aranges 0 : { *(.debug_aranges) } - .debug_pubnames 0 : { *(.debug_pubnames) } - /* DWARF 2 */ - .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end) } - .debug_frame 0 : { *(.debug_frame) } - .debug_str 0 : { *(.debug_str) } - .debug_loc 0 : { *(.debug_loc) } - .debug_macinfo 0 : { *(.debug_macinfo) } - /* SGI/MIPS DWARF 2 extensions */ - .debug_weaknames 0 : { *(.debug_weaknames) } - .debug_funcnames 0 : { *(.debug_funcnames) } - .debug_typenames 0 : { *(.debug_typenames) } - .debug_varnames 0 : { *(.debug_varnames) } - /* DWARF 3 */ - .debug_pubtypes 0 : { *(.debug_pubtypes) } - .debug_ranges 0 : { *(.debug_ranges) } - /* DWARF Extension. */ - .debug_macro 0 : { *(.debug_macro) } - .debug_addr 0 : { *(.debug_addr) } - .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) } - /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) } -} diff --git a/user/apps/test_signal/main.c b/user/apps/test_signal/main.c index 7f74b60c..3864fd60 100644 --- a/user/apps/test_signal/main.c +++ b/user/apps/test_signal/main.c @@ -17,13 +17,11 @@ * */ -#include #include -#include -#include -#include -#include #include +#include +#include + bool handle_ok = false; int count = 0; void handler(int sig) diff --git a/user/apps/test_sqlite3/.gitignore b/user/apps/test_sqlite3/.gitignore index 9c2f57c4..4d96b365 100644 --- a/user/apps/test_sqlite3/.gitignore +++ b/user/apps/test_sqlite3/.gitignore @@ -1,2 +1,3 @@ sqlite*.zip -sqlite-*/ \ No newline at end of file +sqlite-*/ +test_sqlite3 diff --git a/user/apps/test_sqlite3/Makefile b/user/apps/test_sqlite3/Makefile index 1bbbca5f..d5c0589f 100644 --- a/user/apps/test_sqlite3/Makefile +++ b/user/apps/test_sqlite3/Makefile @@ -1,34 +1,23 @@ -CC=$(DragonOS_GCC)/x86_64-elf-gcc -LD=ld -OBJCOPY=objcopy +ifeq ($(ARCH), x86_64) + CROSS_COMPILE=x86_64-linux-musl- +else ifeq ($(ARCH), riscv64) + CROSS_COMPILE=riscv64-linux-musl- +endif + SQLITE_FILENAME=sqlite-amalgamation-3420000 SQLITE3_DIR=$(shell pwd)/$(SQLITE_FILENAME) +CC=$(CROSS_COMPILE)gcc -RELIBC_OPT=$(DADK_BUILD_CACHE_DIR_RELIBC_0_1_0) -CFLAGS=-I $(RELIBC_OPT)/include -I $(SQLITE3_DIR) -D__dragonos__ -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_FLOATING_POINT -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_DEBUG +.PHONY: all +all: main.c $(SQLITE3_DIR)/sqlite3.c + $(CC) -I $(SQLITE3_DIR) -static -o test_sqlite3 main.c $(SQLITE3_DIR)/sqlite3.c -tmp_output_dir=$(ROOT_PATH)/bin/tmp/user -output_dir=$(DADK_BUILD_CACHE_DIR_TEST_SQLITE3_3_42_0) +.PHONY: install clean download_sqlite3 __download_sqlite3 +install: all + mv test_sqlite3 $(DADK_CURRENT_BUILD_DIR)/test_sqlite3 -LIBC_OBJS:=$(shell find $(RELIBC_OPT)/lib -name "*.o" | sort ) -LIBC_OBJS+=$(RELIBC_OPT)/lib/libc.a - -.PHONY: all clean download_sqlite3 __download_sqlite3 - - -all: main.o sqlite3.o - mkdir -p $(tmp_output_dir) - - $(LD) -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/test_sqlite3 $(shell find . -name "*.o") $(LIBC_OBJS) - - $(OBJCOPY) -I elf64-x86-64 -R ".eh_frame" -R ".comment" -O elf64-x86-64 $(tmp_output_dir)/test_sqlite3 $(output_dir)/test_sqlite3.elf - mv $(output_dir)/test_sqlite3.elf $(output_dir)/test_sqlite3 - -main.o: main.c - $(CC) $(CFLAGS) -c main.c -o main.o - -sqlite3.o: $(SQLITE3_DIR)/sqlite3.c - $(CC) $(CFLAGS) -c $(SQLITE3_DIR)/sqlite3.c -o sqlite3.o +clean: + rm test_sqlite3 *.o __download_sqlite3: @echo "Download sqlite3 from https://mirrors.dragonos.org.cn/pub/third_party/sqlite/$(SQLITE_FILENAME).zip" @@ -40,5 +29,4 @@ download_sqlite3: # 如果文件夹不存在,则下载,否则不下载 @test -d $(SQLITE3_DIR) || $(MAKE) __download_sqlite3 -clean: - rm -f *.o +fmt: diff --git a/user/apps/test_uart/Makefile b/user/apps/test_uart/Makefile index abcb04f1..de532fdc 100644 --- a/user/apps/test_uart/Makefile +++ b/user/apps/test_uart/Makefile @@ -1,25 +1,20 @@ -CC=$(DragonOS_GCC)/x86_64-elf-gcc -LD=ld -OBJCOPY=objcopy -# 修改这里,把它改为你的relibc的sysroot路径 -RELIBC_OPT=$(DADK_BUILD_CACHE_DIR_RELIBC_0_1_0) -CFLAGS=-I $(RELIBC_OPT)/include -D__dragonos__ +ifeq ($(ARCH), x86_64) + CROSS_COMPILE=x86_64-linux-musl- +else ifeq ($(ARCH), riscv64) + CROSS_COMPILE=riscv64-linux-musl- +endif -tmp_output_dir=$(ROOT_PATH)/bin/tmp/user -output_dir=$(DADK_BUILD_CACHE_DIR_TEST_UART_0_1_0) +CC=$(CROSS_COMPILE)gcc -LIBC_OBJS:=$(shell find $(RELIBC_OPT)/lib -name "*.o" | sort ) -LIBC_OBJS+=$(RELIBC_OPT)/lib/libc.a +.PHONY: all +all: main.c + $(CC) -static -o test_uart main.c -all: main.o - mkdir -p $(tmp_output_dir) - - $(LD) -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/test_uart $(shell find . -name "*.o") $(LIBC_OBJS) -T link.lds - - $(OBJCOPY) -I elf64-x86-64 -R ".eh_frame" -R ".comment" -O elf64-x86-64 $(tmp_output_dir)/test_uart $(output_dir)/test_uart.elf - mv $(output_dir)/test_uart.elf $(output_dir)/test_uart -main.o: main.c - $(CC) $(CFLAGS) -c main.c -o main.o +.PHONY: install clean +install: all + mv test_uart $(DADK_CURRENT_BUILD_DIR)/test_uart clean: - rm -f *.o + rm test_uart *.o + +fmt: diff --git a/user/apps/test_uart/link.lds b/user/apps/test_uart/link.lds deleted file mode 100644 index 1f2e57e4..00000000 --- a/user/apps/test_uart/link.lds +++ /dev/null @@ -1,239 +0,0 @@ -/* Script for -z combreloc */ -/* Copyright (C) 2014-2020 Free Software Foundation, Inc. - Copying and distribution of this script, with or without modification, - are permitted in any medium without royalty provided the copyright - notice and this notice are preserved. */ -OUTPUT_FORMAT("elf64-x86-64", "elf64-x86-64", - "elf64-x86-64") -OUTPUT_ARCH(i386:x86-64) -ENTRY(_start) - -SECTIONS -{ - /* Read-only sections, merged into text segment: */ - PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000)); . = SEGMENT_START("text-segment", 0x20000000) + SIZEOF_HEADERS; - .interp : { *(.interp) } - .note.gnu.build-id : { *(.note.gnu.build-id) } - .hash : { *(.hash) } - .gnu.hash : { *(.gnu.hash) } - .dynsym : { *(.dynsym) } - .dynstr : { *(.dynstr) } - .gnu.version : { *(.gnu.version) } - .gnu.version_d : { *(.gnu.version_d) } - .gnu.version_r : { *(.gnu.version_r) } - .rela.dyn : - { - *(.rela.init) - *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*) - *(.rela.fini) - *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*) - *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*) - *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*) - *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*) - *(.rela.ctors) - *(.rela.dtors) - *(.rela.got) - *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*) - *(.rela.ldata .rela.ldata.* .rela.gnu.linkonce.l.*) - *(.rela.lbss .rela.lbss.* .rela.gnu.linkonce.lb.*) - *(.rela.lrodata .rela.lrodata.* .rela.gnu.linkonce.lr.*) - *(.rela.ifunc) - } - .rela.plt : - { - *(.rela.plt) - PROVIDE_HIDDEN (__rela_iplt_start = .); - *(.rela.iplt) - PROVIDE_HIDDEN (__rela_iplt_end = .); - } - . = ALIGN(CONSTANT (MAXPAGESIZE)); - .init : - { - KEEP (*(SORT_NONE(.init))) - } - .plt : { *(.plt) *(.iplt) } -.plt.got : { *(.plt.got) } -.plt.sec : { *(.plt.sec) } - .text : - { - *(.text.unlikely .text.*_unlikely .text.unlikely.*) - *(.text.exit .text.exit.*) - *(.text.startup .text.startup.*) - *(.text.hot .text.hot.*) - *(.text .stub .text.* .gnu.linkonce.t.*) - /* .gnu.warning sections are handled specially by elf.em. */ - *(.gnu.warning) - } - .fini : - { - KEEP (*(SORT_NONE(.fini))) - } - PROVIDE (__etext = .); - PROVIDE (_etext = .); - PROVIDE (etext = .); - . = ALIGN(CONSTANT (MAXPAGESIZE)); - /* Adjust the address for the rodata segment. We want to adjust up to - the same address within the page on the next page up. */ - . = SEGMENT_START("rodata-segment", ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1))); - .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } - .rodata1 : { *(.rodata1) } - .eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) } - .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) } - .gnu_extab : ONLY_IF_RO { *(.gnu_extab*) } - /* These sections are generated by the Sun/Oracle C++ compiler. */ - .exception_ranges : ONLY_IF_RO { *(.exception_ranges*) } - /* Adjust the address for the data segment. We want to adjust up to - the same address within the page on the next page up. */ - . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); - /* Exception handling */ - .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gnu_extab : ONLY_IF_RW { *(.gnu_extab) } - .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) } - .exception_ranges : ONLY_IF_RW { *(.exception_ranges*) } - /* Thread Local Storage sections */ - .tdata : - { - PROVIDE_HIDDEN (__tdata_start = .); - *(.tdata .tdata.* .gnu.linkonce.td.*) - } - .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) } - .preinit_array : - { - PROVIDE_HIDDEN (__preinit_array_start = .); - KEEP (*(.preinit_array)) - PROVIDE_HIDDEN (__preinit_array_end = .); - } - .init_array : - { - PROVIDE_HIDDEN (__init_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) - KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)) - PROVIDE_HIDDEN (__init_array_end = .); - } - .fini_array : - { - PROVIDE_HIDDEN (__fini_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))) - KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)) - PROVIDE_HIDDEN (__fini_array_end = .); - } - .ctors : - { - /* gcc uses crtbegin.o to find the start of - the constructors, so we make sure it is - first. Because this is a wildcard, it - doesn't matter if the user does not - actually link against crtbegin.o; the - linker won't look for a file to match a - wildcard. The wildcard also means that it - doesn't matter which directory crtbegin.o - is in. */ - KEEP (*crtbegin.o(.ctors)) - KEEP (*crtbegin?.o(.ctors)) - /* We don't want to include the .ctor section from - the crtend.o file until after the sorted ctors. - The .ctor section from the crtend file contains the - end of ctors marker and it must be last */ - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors)) - KEEP (*(SORT(.ctors.*))) - KEEP (*(.ctors)) - } - .dtors : - { - KEEP (*crtbegin.o(.dtors)) - KEEP (*crtbegin?.o(.dtors)) - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) - KEEP (*(SORT(.dtors.*))) - KEEP (*(.dtors)) - } - .jcr : { KEEP (*(.jcr)) } - .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) } - .dynamic : { *(.dynamic) } - .got : { *(.got) *(.igot) } - . = DATA_SEGMENT_RELRO_END (SIZEOF (.got.plt) >= 24 ? 24 : 0, .); - .got.plt : { *(.got.plt) *(.igot.plt) } - .data : - { - *(.data .data.* .gnu.linkonce.d.*) - SORT(CONSTRUCTORS) - } - .data1 : { *(.data1) } - _edata = .; PROVIDE (edata = .); - . = .; - __bss_start = .; - .bss : - { - *(.dynbss) - *(.bss .bss.* .gnu.linkonce.b.*) - *(COMMON) - /* Align here to ensure that the .bss section occupies space up to - _end. Align after .bss to ensure correct alignment even if the - .bss section disappears because there are no input sections. - FIXME: Why do we need it? When there is no .bss section, we do not - pad the .data section. */ - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - .lbss : - { - *(.dynlbss) - *(.lbss .lbss.* .gnu.linkonce.lb.*) - *(LARGE_COMMON) - } - . = ALIGN(64 / 8); - . = SEGMENT_START("ldata-segment", .); - .lrodata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.lrodata .lrodata.* .gnu.linkonce.lr.*) - } - .ldata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.ldata .ldata.* .gnu.linkonce.l.*) - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - . = ALIGN(64 / 8); - _end = .; PROVIDE (end = .); - . = DATA_SEGMENT_END (.); - /* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } - .gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) } - /* DWARF debug sections. - Symbols in the DWARF debugging sections are relative to the beginning - of the section so we begin them at 0. */ - /* DWARF 1 */ - .debug 0 : { *(.debug) } - .line 0 : { *(.line) } - /* GNU DWARF 1 extensions */ - .debug_srcinfo 0 : { *(.debug_srcinfo) } - .debug_sfnames 0 : { *(.debug_sfnames) } - /* DWARF 1.1 and DWARF 2 */ - .debug_aranges 0 : { *(.debug_aranges) } - .debug_pubnames 0 : { *(.debug_pubnames) } - /* DWARF 2 */ - .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end) } - .debug_frame 0 : { *(.debug_frame) } - .debug_str 0 : { *(.debug_str) } - .debug_loc 0 : { *(.debug_loc) } - .debug_macinfo 0 : { *(.debug_macinfo) } - /* SGI/MIPS DWARF 2 extensions */ - .debug_weaknames 0 : { *(.debug_weaknames) } - .debug_funcnames 0 : { *(.debug_funcnames) } - .debug_typenames 0 : { *(.debug_typenames) } - .debug_varnames 0 : { *(.debug_varnames) } - /* DWARF 3 */ - .debug_pubtypes 0 : { *(.debug_pubtypes) } - .debug_ranges 0 : { *(.debug_ranges) } - /* DWARF Extension. */ - .debug_macro 0 : { *(.debug_macro) } - .debug_addr 0 : { *(.debug_addr) } - .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) } - /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) } -} diff --git a/user/apps/test_uart/main.c b/user/apps/test_uart/main.c index 60afd411..07782ea7 100644 --- a/user/apps/test_uart/main.c +++ b/user/apps/test_uart/main.c @@ -1,6 +1,5 @@ #include #include -#include #include #include @@ -21,11 +20,11 @@ int main() { } } } - printf("fd: %ld", fd); + printf("fd: %d", fd); // 写入字符串 char *str = "------fuck-----"; int len = write(fd, str, strlen(str)); - printf("len: %ld", len); + printf("len: %d", len); // 关闭文件 close(fd); return 0; diff --git a/user/dadk/config/.gitignore b/user/dadk/config/.gitignore deleted file mode 100644 index e69de29b..00000000 diff --git a/user/dadk/config/about.dadk b/user/dadk/config/about.dadk new file mode 100644 index 00000000..da18c840 --- /dev/null +++ b/user/dadk/config/about.dadk @@ -0,0 +1,24 @@ +{ + "name": "about", + "version": "0.1.0", + "description": "", + "rust_target": null, + "task_type": { + "BuildFromSource": { + "Local": { + "path": "apps/about" + } + } + }, + "depends": [], + "build": { + "build_command": "make install" + }, + "install": { + "in_dragonos_path": "/bin" + }, + "clean": { + "clean_command": "make clean" + }, + "envs": [] + } \ No newline at end of file diff --git a/user/dadk/config/dmesg-0.1.0.dadk b/user/dadk/config/dmesg-0.1.0.dadk index 2598b886..8f4e85f5 100644 --- a/user/dadk/config/dmesg-0.1.0.dadk +++ b/user/dadk/config/dmesg-0.1.0.dadk @@ -9,9 +9,9 @@ } } }, - "depends": [ ], + "depends": [], "build": { - "build_command": "make" + "build_command": "make install -j $(nproc)" }, "install": { "in_dragonos_path": "/bin" diff --git a/user/dadk/config/http_server-0.1.0.dadk b/user/dadk/config/http_server-0.1.0.dadk index 7234c710..acf383dd 100644 --- a/user/dadk/config/http_server-0.1.0.dadk +++ b/user/dadk/config/http_server-0.1.0.dadk @@ -9,14 +9,9 @@ } } }, - "depends": [ - { - "name": "relibc", - "version": "0.1.0" - } - ], + "depends": [], "build": { - "build_command": "make" + "build_command": "make install" }, "install": { "in_dragonos_path": "/bin" diff --git a/user/dadk/config/musl_1_2_4.dadk b/user/dadk/config/musl_1_2_4.dadk new file mode 100644 index 00000000..f70c84cc --- /dev/null +++ b/user/dadk/config/musl_1_2_4.dadk @@ -0,0 +1,24 @@ +{ + "name": "musl", + "version": "1.2.4", + "description": "musl libc", + "rust_target": null, + "task_type": { + "BuildFromSource": { + "Archive": { + "url": "https://mirrors.dragonos.org.cn/pub/third_party/musl/musl-1.2.4.tar.gz" + } + } + }, + "depends": [], + "build": { + "build_command": "touch config.mak && DESTDIR=$DADK_CURRENT_BUILD_DIR make install -j $(nproc)" + }, + "install": { + "in_dragonos_path": "/" + }, + "clean": { + "clean_command": "make clean" + }, + "envs": [] +} \ No newline at end of file diff --git a/user/dadk/config/relibc-0.1.0.dadk b/user/dadk/config/relibc-0.1.0.dadk deleted file mode 100644 index 0dcc1518..00000000 --- a/user/dadk/config/relibc-0.1.0.dadk +++ /dev/null @@ -1,29 +0,0 @@ -{ - "name": "relibc", - "version": "0.1.0", - "description": "从GitHub克隆最新的relibc来编译", - "task_type": { - "BuildFromSource": { - "Git": { - "url": "https://git.mirrors.dragonos.org.cn/DragonOS-Community/relibc.git", - "revision": "27e779dc23" - } - } - }, - "depends": [], - "build": { - "build_command": "bash init_dragonos_toolchain.sh && DESTDIR=$DADK_BUILD_CACHE_DIR_RELIBC_0_1_0 make install -j $(nproc)" - }, - "install": { - "in_dragonos_path": "/usr" - }, - "clean": { - "clean_command": "make clean" - }, - "envs": [ - { - "key": "TARGET", - "value": "x86_64-unknown-dragonos" - } - ] -} diff --git a/user/dadk/config/test_bind-0.1.0.dadk b/user/dadk/config/test_bind-0.1.0.dadk index c3356fef..6204fc03 100644 --- a/user/dadk/config/test_bind-0.1.0.dadk +++ b/user/dadk/config/test_bind-0.1.0.dadk @@ -9,14 +9,9 @@ } } }, - "depends": [ - { - "name": "relibc", - "version": "0.1.0" - } - ], + "depends": [], "build": { - "build_command": "make" + "build_command": "make install" }, "install": { "in_dragonos_path": "/bin" diff --git a/user/dadk/config/test_fstat-0.1.0.dadk b/user/dadk/config/test_fstat-0.1.0.dadk index b00f304e..7bda5d55 100644 --- a/user/dadk/config/test_fstat-0.1.0.dadk +++ b/user/dadk/config/test_fstat-0.1.0.dadk @@ -9,25 +9,14 @@ } } }, - "depends": [ - { - "name": "relibc", - "version": "0.1.0" - } - ], + "depends": [], "build": { - "build_command": "make" + "build_command": "make install" }, "install": { "in_dragonos_path": "/bin" }, "clean": { "clean_command": "make clean" - }, - "envs": [ - { - "key": "__dragonos__", - "value": "__dragonos__" - } - ] + } } diff --git a/user/dadk/config/test_gettimeofday-0.1.0.dadk b/user/dadk/config/test_gettimeofday-0.1.0.dadk index 6668025a..c4d62691 100644 --- a/user/dadk/config/test_gettimeofday-0.1.0.dadk +++ b/user/dadk/config/test_gettimeofday-0.1.0.dadk @@ -9,14 +9,9 @@ } } }, - "depends": [ - { - "name": "relibc", - "version": "0.1.0" - } - ], + "depends": [], "build": { - "build_command": "make" + "build_command": "make install" }, "install": { "in_dragonos_path": "/bin" diff --git a/user/dadk/config/test_kvm_0_1_0.dadk b/user/dadk/config/test_kvm_0_1_0.dadk new file mode 100644 index 00000000..4aebd063 --- /dev/null +++ b/user/dadk/config/test_kvm_0_1_0.dadk @@ -0,0 +1,24 @@ +{ + "name": "test_kvm", + "version": "0.1.0", + "description": "测试kvm的程序", + "rust_target": null, + "task_type": { + "BuildFromSource": { + "Local": { + "path": "apps/test_kvm" + } + } + }, + "depends": [], + "build": { + "build_command": "make install" + }, + "install": { + "in_dragonos_path": "/bin" + }, + "clean": { + "clean_command": "make clean" + }, + "envs": [] +} \ No newline at end of file diff --git a/user/dadk/config/test_mkfifo-0.1.0.dadk b/user/dadk/config/test_mkfifo-0.1.0.dadk index 59f4187f..8eba14ad 100644 --- a/user/dadk/config/test_mkfifo-0.1.0.dadk +++ b/user/dadk/config/test_mkfifo-0.1.0.dadk @@ -9,25 +9,14 @@ } } }, - "depends": [ - { - "name": "relibc", - "version": "0.1.0" - } - ], + "depends": [], "build": { - "build_command": "make" + "build_command": "make install" }, "install": { "in_dragonos_path": "/bin" }, "clean": { "clean_command": "make clean" - }, - "envs": [ - { - "key": "__dragonos__", - "value": "__dragonos__" - } - ] + } } diff --git a/user/dadk/config/test_relibc-0.1.0.dadk b/user/dadk/config/test_relibc-0.1.0.dadk deleted file mode 100644 index 76a00489..00000000 --- a/user/dadk/config/test_relibc-0.1.0.dadk +++ /dev/null @@ -1,28 +0,0 @@ -{ - "name": "test_relibc", - "version": "0.1.0", - "description": "一个用来测试relibc能够正常运行的app", - "task_type": { - "BuildFromSource": { - "Local": { - "path": "apps/test_relibc" - } - } - }, - "depends": [ - { - "name": "relibc", - "version": "0.1.0" - } - ], - "build": { - "build_command": "make" - }, - "install": { - "in_dragonos_path": "/bin" - }, - "clean": { - "clean_command": "make clean" - }, - "envs": [] -} \ No newline at end of file diff --git a/user/dadk/config/test_signal-0.1.0.dadk b/user/dadk/config/test_signal-0.1.0.dadk index 44b3847f..59e37750 100644 --- a/user/dadk/config/test_signal-0.1.0.dadk +++ b/user/dadk/config/test_signal-0.1.0.dadk @@ -9,14 +9,9 @@ } } }, - "depends": [ - { - "name": "relibc", - "version": "0.1.0" - } - ], + "depends": [], "build": { - "build_command": "make" + "build_command": "make install" }, "install": { "in_dragonos_path": "/bin" diff --git a/user/dadk/config/test_sqlite3-3.42.0.dadk b/user/dadk/config/test_sqlite3-3.42.0.dadk index 5aff8398..f8685376 100644 --- a/user/dadk/config/test_sqlite3-3.42.0.dadk +++ b/user/dadk/config/test_sqlite3-3.42.0.dadk @@ -9,14 +9,9 @@ } } }, - "depends": [ - { - "name": "relibc", - "version": "0.1.0" - } - ], + "depends": [], "build": { - "build_command": "make download_sqlite3 && make -j $(nproc)" + "build_command": "make download_sqlite3 && make install" }, "install": { "in_dragonos_path": "/bin" diff --git a/user/dadk/config/test_uart-0.1.0.dadk b/user/dadk/config/test_uart-0.1.0.dadk index c1b5ca37..6aee36ad 100644 --- a/user/dadk/config/test_uart-0.1.0.dadk +++ b/user/dadk/config/test_uart-0.1.0.dadk @@ -9,14 +9,9 @@ } } }, - "depends": [ - { - "name": "relibc", - "version": "0.1.0" - } - ], + "depends": [], "build": { - "build_command": "make" + "build_command": "make install" }, "install": { "in_dragonos_path": "/bin" diff --git a/user/dadk/target/x86_64-unknown-dragonos.json b/user/dadk/target/x86_64-unknown-dragonos.json deleted file mode 100644 index d1dfb39f..00000000 --- a/user/dadk/target/x86_64-unknown-dragonos.json +++ /dev/null @@ -1,35 +0,0 @@ -{ - "arch": "x86_64", - "code-model": "kernel", - "cpu": "x86-64", - "os": "dragonos", - "target-endian": "little", - "target-pointer-width": "64", - "target-family": [ - "unix" - ], - "env": "musl", - "target-c-int-width": "32", - "data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128", - "disable-redzone": true, - "features": "-3dnow,-3dnowa,-avx,-avx2", - "linker": "rust-lld", - "linker-flavor": "ld.lld", - "llvm-target": "x86_64-unknown-none", - "max-atomic-width": 64, - "panic-strategy": "abort", - "position-independent-executables": true, - "relro-level": "full", - "stack-probes": { - "kind": "inline-or-call", - "min-llvm-version-for-inline": [ - 16, - 0, - 0 - ] - }, - "static-position-independent-executables": true, - "supported-sanitizers": [ - "kcfi" - ] -} diff --git a/user/libs/Makefile b/user/libs/Makefile deleted file mode 100644 index 4d3da120..00000000 --- a/user/libs/Makefile +++ /dev/null @@ -1,24 +0,0 @@ - -user_libs_sub_dirs=libc libsystem libKeyboard - - -ECHO: - @echo "$@" - -$(user_libs_sub_dirs): ECHO - - $(MAKE) -C $@ all CFLAGS="$(CFLAGS) -I $(shell pwd)" - -all: $(user_libs_sub_dirs) - -clean: - @list='$(user_libs_sub_dirs)'; for subdir in $$list; do \ - echo "Clean in dir: $$subdir";\ - cd $$subdir && $(MAKE) clean;\ - cd .. ;\ - done - -.PHONY: clean -fmt: - @echo "格式化代码: user/libs" - FMT_CHECK=$(FMT_CHECK) $(MAKE) -C libc fmt diff --git a/user/libs/libKeyboard/Makefile b/user/libs/libKeyboard/Makefile deleted file mode 100644 index bb34dc7b..00000000 --- a/user/libs/libKeyboard/Makefile +++ /dev/null @@ -1,7 +0,0 @@ -all: keyboard.o - -CFLAGS += -I . - - -keyboard.o: keyboard.c - $(CC) $(CFLAGS) -c keyboard.c -o keyboard.o \ No newline at end of file diff --git a/user/libs/libKeyboard/keyboard.c b/user/libs/libKeyboard/keyboard.c deleted file mode 100644 index 1d1ac03f..00000000 --- a/user/libs/libKeyboard/keyboard.c +++ /dev/null @@ -1,533 +0,0 @@ -#include "keyboard.h" -#include - -// 功能键标志变量 -static bool shift_l = 0, shift_r = 0, ctrl_l = 0, ctrl_r = 0, alt_l = 0, alt_r = 0; -static bool gui_l = 0, gui_r = 0, apps = 0, insert = 0, home = 0, pgup = 0, del = 0, end = 0, pgdn = 0, arrow_u = 0, arrow_l = 0, arrow_d = 0, arrow_r = 0; -static bool kp_forward_slash = 0, kp_en = 0; - -// 键盘扫描码有三种: -// 0xE1开头的PauseBreak键 -// 0xE0开头的功能键 -// 1byte的普通按键 - -// pause break键的扫描码,没错,它就是这么长 -unsigned char pause_break_scan_code[] = {0xe1, 0x1d, 0x45, 0xe1, 0x9d, 0xc5}; - -// 第一套键盘扫描码 及其对应的字符 -uint32_t keycode_map_normal[NUM_SCAN_CODES * MAP_COLS] = - { - /*scan-code unShift Shift */ - /*--------------------------------------------------------------*/ - /*0x00*/ 0, - 0, - /*0x01*/ 0, - 0, // ESC - /*0x02*/ '1', - '!', - /*0x03*/ '2', - '@', - /*0x04*/ '3', - '#', - /*0x05*/ '4', - '$', - /*0x06*/ '5', - '%', - /*0x07*/ '6', - '^', - /*0x08*/ '7', - '&', - /*0x09*/ '8', - '*', - /*0x0a*/ '9', - '(', - /*0x0b*/ '0', - ')', - /*0x0c*/ '-', - '_', - /*0x0d*/ '=', - '+', - /*0x0e*/ '\b', - '\b', // BACKSPACE - /*0x0f*/ '\t', - '\t', // TAB - - /*0x10*/ 'q', - 'Q', - /*0x11*/ 'w', - 'W', - /*0x12*/ 'e', - 'E', - /*0x13*/ 'r', - 'R', - /*0x14*/ 't', - 'T', - /*0x15*/ 'y', - 'Y', - /*0x16*/ 'u', - 'U', - /*0x17*/ 'i', - 'I', - /*0x18*/ 'o', - 'O', - /*0x19*/ 'p', - 'P', - /*0x1a*/ '[', - '{', - /*0x1b*/ ']', - '}', - /*0x1c*/ '\n', - '\n', // ENTER - /*0x1d*/ 0x1d, - 0x1d, // CTRL Left - /*0x1e*/ 'a', - 'A', - /*0x1f*/ 's', - 'S', - - /*0x20*/ 'd', - 'D', - /*0x21*/ 'f', - 'F', - /*0x22*/ 'g', - 'G', - /*0x23*/ 'h', - 'H', - /*0x24*/ 'j', - 'J', - /*0x25*/ 'k', - 'K', - /*0x26*/ 'l', - 'L', - /*0x27*/ ';', - ':', - /*0x28*/ '\'', - '"', - /*0x29*/ '`', - '~', - /*0x2a*/ 0x2a, - 0x2a, // SHIFT Left - /*0x2b*/ '\\', - '|', - /*0x2c*/ 'z', - 'Z', - /*0x2d*/ 'x', - 'X', - /*0x2e*/ 'c', - 'C', - /*0x2f*/ 'v', - 'V', - - /*0x30*/ 'b', - 'B', - /*0x31*/ 'n', - 'N', - /*0x32*/ 'm', - 'M', - /*0x33*/ ',', - '<', - /*0x34*/ '.', - '>', - /*0x35*/ '/', - '?', - /*0x36*/ 0x36, - 0x36, // SHIFT Right - /*0x37*/ '*', - '*', - /*0x38*/ 0x38, - 0x38, // ALT Left - /*0x39*/ ' ', - ' ', - /*0x3a*/ 0, - 0, // CAPS LOCK - /*0x3b*/ 0, - 0, // F1 - /*0x3c*/ 0, - 0, // F2 - /*0x3d*/ 0, - 0, // F3 - /*0x3e*/ 0, - 0, // F4 - /*0x3f*/ 0, - 0, // F5 - - /*0x40*/ 0, - 0, // F6 - /*0x41*/ 0, - 0, // F7 - /*0x42*/ 0, - 0, // F8 - /*0x43*/ 0, - 0, // F9 - /*0x44*/ 0, - 0, // F10 - /*0x45*/ 0, - 0, // NUM LOCK - /*0x46*/ 0, - 0, // SCROLL LOCK - /*0x47*/ '7', - 0, /*PAD HONE*/ - /*0x48*/ '8', - 0, /*PAD UP*/ - /*0x49*/ '9', - 0, /*PAD PAGEUP*/ - /*0x4a*/ '-', - 0, /*PAD MINUS*/ - /*0x4b*/ '4', - 0, /*PAD LEFT*/ - /*0x4c*/ '5', - 0, /*PAD MID*/ - /*0x4d*/ '6', - 0, /*PAD RIGHT*/ - /*0x4e*/ '+', - 0, /*PAD PLUS*/ - /*0x4f*/ '1', - 0, /*PAD END*/ - - /*0x50*/ '2', - 0, /*PAD DOWN*/ - /*0x51*/ '3', - 0, /*PAD PAGEDOWN*/ - /*0x52*/ '0', - 0, /*PAD INS*/ - /*0x53*/ '.', - 0, /*PAD DOT*/ - /*0x54*/ 0, - 0, - /*0x55*/ 0, - 0, - /*0x56*/ 0, - 0, - /*0x57*/ 0, - 0, // F11 - /*0x58*/ 0, - 0, // F12 - /*0x59*/ 0, - 0, - /*0x5a*/ 0, - 0, - /*0x5b*/ 0, - 0, - /*0x5c*/ 0, - 0, - /*0x5d*/ 0, - 0, - /*0x5e*/ 0, - 0, - /*0x5f*/ 0, - 0, - - /*0x60*/ 0, - 0, - /*0x61*/ 0, - 0, - /*0x62*/ 0, - 0, - /*0x63*/ 0, - 0, - /*0x64*/ 0, - 0, - /*0x65*/ 0, - 0, - /*0x66*/ 0, - 0, - /*0x67*/ 0, - 0, - /*0x68*/ 0, - 0, - /*0x69*/ 0, - 0, - /*0x6a*/ 0, - 0, - /*0x6b*/ 0, - 0, - /*0x6c*/ 0, - 0, - /*0x6d*/ 0, - 0, - /*0x6e*/ 0, - 0, - /*0x6f*/ 0, - 0, - - /*0x70*/ 0, - 0, - /*0x71*/ 0, - 0, - /*0x72*/ 0, - 0, - /*0x73*/ 0, - 0, - /*0x74*/ 0, - 0, - /*0x75*/ 0, - 0, - /*0x76*/ 0, - 0, - /*0x77*/ 0, - 0, - /*0x78*/ 0, - 0, - /*0x79*/ 0, - 0, - /*0x7a*/ 0, - 0, - /*0x7b*/ 0, - 0, - /*0x7c*/ 0, - 0, - /*0x7d*/ 0, - 0, - /*0x7e*/ 0, - 0, - /*0x7f*/ 0, - 0, -}; - -/** - * @brief 解析键盘扫描码 - * - */ -int keyboard_analyze_keycode(int fd) -{ - bool flag_make = false; - - int c = keyboard_get_scancode(fd); - // 循环队列为空 - if (c == -1) - return 0; - - unsigned char scancode = (unsigned char)c; - - int key = 0; - if (scancode == 0xE1) // Pause Break - { - key = PAUSE_BREAK; - // 清除缓冲区中剩下的扫描码 - for (int i = 1; i < 6; ++i) - if (keyboard_get_scancode(fd) != pause_break_scan_code[i]) - { - key = 0; - break; - } - } - else if (scancode == 0xE0) // 功能键, 有多个扫描码 - { - // 获取下一个扫描码 - scancode = keyboard_get_scancode(fd); - switch (scancode) - { - case 0x2a: // print screen 按键被按下 - if (keyboard_get_scancode(fd) == 0xe0) - if (keyboard_get_scancode(fd) == 0x37) - { - key = PRINT_SCREEN; - flag_make = true; - } - break; - case 0xb7: // print screen 按键被松开 - if (keyboard_get_scancode(fd) == 0xe0) - if (keyboard_get_scancode(fd) == 0xaa) - { - key = PRINT_SCREEN; - flag_make = false; - } - break; - case 0x1d: // 按下右边的ctrl - ctrl_r = true; - key = OTHER_KEY; - break; - case 0x9d: // 松开右边的ctrl - ctrl_r = false; - key = OTHER_KEY; - break; - case 0x38: // 按下右边的alt - alt_r = true; - key = OTHER_KEY; - break; - case 0xb8: // 松开右边的alt - alt_r = false; - key = OTHER_KEY; - break; - case 0x5b: - gui_l = true; - key = OTHER_KEY; - break; - case 0xdb: - gui_l = false; - key = OTHER_KEY; - break; - case 0x5c: - gui_r = true; - key = OTHER_KEY; - break; - case 0xdc: - gui_r = false; - key = OTHER_KEY; - break; - case 0x5d: - apps = true; - key = OTHER_KEY; - break; - case 0xdd: - apps = false; - key = OTHER_KEY; - break; - case 0x52: - insert = true; - key = OTHER_KEY; - break; - case 0xd2: - insert = false; - key = OTHER_KEY; - break; - case 0x47: - home = true; - key = OTHER_KEY; - break; - case 0xc7: - home = false; - key = OTHER_KEY; - break; - case 0x49: - pgup = true; - key = OTHER_KEY; - break; - case 0xc9: - pgup = false; - key = OTHER_KEY; - break; - case 0x53: - del = true; - key = OTHER_KEY; - break; - case 0xd3: - del = false; - key = OTHER_KEY; - break; - case 0x4f: - end = true; - key = OTHER_KEY; - break; - case 0xcf: - end = false; - key = OTHER_KEY; - break; - case 0x51: - pgdn = true; - key = OTHER_KEY; - break; - case 0xd1: - pgdn = false; - key = OTHER_KEY; - break; - case 0x48: - arrow_u = true; - key = OTHER_KEY; - break; - case 0xc8: - arrow_u = false; - key = OTHER_KEY; - return 0xc8; - break; - case 0x4b: - arrow_l = true; - key = OTHER_KEY; - break; - case 0xcb: - arrow_l = false; - key = OTHER_KEY; - break; - case 0x50: - arrow_d = true; - key = OTHER_KEY; - return 0x50; - break; - case 0xd0: - arrow_d = false; - key = OTHER_KEY; - break; - case 0x4d: - arrow_r = true; - key = OTHER_KEY; - break; - case 0xcd: - arrow_r = false; - key = OTHER_KEY; - break; - - case 0x35: // 数字小键盘的 / 符号 - kp_forward_slash = true; - key = OTHER_KEY; - break; - case 0xb5: - kp_forward_slash = false; - key = OTHER_KEY; - break; - case 0x1c: - kp_en = true; - key = OTHER_KEY; - break; - case 0x9c: - kp_en = false; - key = OTHER_KEY; - break; - - default: - key = OTHER_KEY; - break; - } - } - - if (key == 0) // 属于第三类扫描码 - { - // 判断按键是被按下还是抬起 - flag_make = ((scancode & FLAG_BREAK) ? 0 : 1); - - // 计算扫描码位于码表的第几行 - uint32_t *key_row = &keycode_map_normal[(scancode & 0x7f) * MAP_COLS]; - unsigned char col = 0; - // shift被按下 - if (shift_l || shift_r) - col = 1; - key = key_row[col]; - - switch (scancode & 0x7f) - { - case 0x2a: - shift_l = flag_make; - key = 0; - break; - case 0x36: - shift_r = flag_make; - key = 0; - break; - case 0x1d: - ctrl_l = flag_make; - key = 0; - break; - case 0x38: - ctrl_r = flag_make; - key = 0; - break; - default: - if (!flag_make) - key = 0; - break; - } - if (key) - return key; - } - return 0; -} - -/** - * @brief 从键盘设备文件中获取键盘扫描码 - * - */ -int keyboard_get_scancode(int fd) -{ - unsigned int ret = 0; - read(fd, &ret, 1); - return ret; -} \ No newline at end of file diff --git a/user/libs/libKeyboard/keyboard.h b/user/libs/libKeyboard/keyboard.h deleted file mode 100644 index 863ade53..00000000 --- a/user/libs/libKeyboard/keyboard.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -// 128个按键, 每个按键包含普通按键和shift+普通按键两种状态 -#define NUM_SCAN_CODES 0x80 -#define MAP_COLS 2 - -#define PAUSE_BREAK 1 -#define PRINT_SCREEN 2 -#define OTHER_KEY 4 // 除了上面两个按键以外的功能按键(不包括下面的第三类按键) -#define FLAG_BREAK 0X80 - -/** - * @brief 从键盘设备文件中获取键盘扫描码 - * - */ -int keyboard_get_scancode(int fd); - -/** - * @brief 解析键盘扫描码 - * - */ -int keyboard_analyze_keycode(int fd); \ No newline at end of file diff --git a/user/libs/libc/.cargo/config.toml b/user/libs/libc/.cargo/config.toml deleted file mode 100644 index 7a3ab19b..00000000 --- a/user/libs/libc/.cargo/config.toml +++ /dev/null @@ -1,11 +0,0 @@ -[build] -target = "src/arch/x86_64/x86_64-unknown-none.json" - -[unstable] -build-std = ["core", "compiler_builtins", "alloc"] -build-std-features = ["compiler-builtins-mem"] - -[target.'cfg(target_os = "none")'] -runner = "bootimage runner" - -[env] \ No newline at end of file diff --git a/user/libs/libc/.gitignore b/user/libs/libc/.gitignore deleted file mode 100644 index 2c96eb1b..00000000 --- a/user/libs/libc/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -target/ -Cargo.lock diff --git a/user/libs/libc/Cargo.toml b/user/libs/libc/Cargo.toml deleted file mode 100644 index 63ae399f..00000000 --- a/user/libs/libc/Cargo.toml +++ /dev/null @@ -1,18 +0,0 @@ -[package] -name = "libc" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[lib] -crate-type = ["staticlib"] - -# 运行时依赖项 -[dependencies] -x86_64 = "0.14.10" - -# 构建时依赖项 -[build-dependencies] -bindgen = "0.61.0" - - diff --git a/user/libs/libc/Makefile b/user/libs/libc/Makefile deleted file mode 100644 index c805b5f9..00000000 --- a/user/libs/libc/Makefile +++ /dev/null @@ -1,11 +0,0 @@ - -all: - $(MAKE) CFLAGS="$(CFLAGS)" -C src all - -clean: - rm -f Cargo.lock - $(MAKE) -C src clean - -.PHONY: fmt -fmt: - cargo fmt --all $(FMT_CHECK) diff --git a/user/libs/libc/build.rs b/user/libs/libc/build.rs deleted file mode 100644 index bcc9d717..00000000 --- a/user/libs/libc/build.rs +++ /dev/null @@ -1,45 +0,0 @@ -extern crate bindgen; -// use ::std::env; - -use std::path::PathBuf; - -fn main() { - // Tell cargo to look for shared libraries in the specified directory - println!("cargo:rustc-link-search=src"); - println!("cargo:rerun-if-changed=src/include/internal/bindings/wrapper.h"); - - // let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); - let out_path = PathBuf::from(String::from("src/include/internal/bindings/")); - - // The bindgen::Builder is the main entry point - // to bindgen, and lets you build up options for - // the resulting bindings. - { - let bindings = bindgen::Builder::default() - .clang_arg("-I./src/include") - .clang_arg("-I./src/include/export") // todo: 当引入多种架构之后,需要修改这里,对于不同的架构编译时,include不同的路径 - // The input header we would like to generate - // bindings for. - .header("src/include/internal/bindings/wrapper.h") - .clang_arg("--target=x86_64-none-none") - .clang_arg("-v") - // 使用core,并将c语言的类型改为core::ffi,而不是使用std库。 - .use_core() - .ctypes_prefix("::core::ffi") - .generate_inline_functions(true) - .raw_line("#![allow(dead_code)]") - .raw_line("#![allow(non_upper_case_globals)]") - .raw_line("#![allow(non_camel_case_types)]") - // Tell cargo to invalidate the built crate whenever any of the - // included header files changed. - .parse_callbacks(Box::new(bindgen::CargoCallbacks)) - // Finish the builder and generate the bindings. - .generate() - // Unwrap the Result and panic on failure. - .expect("Unable to generate bindings"); - - bindings - .write_to_file(out_path.join("bindings.rs")) - .expect("Couldn't write bindings!"); - } -} diff --git a/user/libs/libc/src/Makefile b/user/libs/libc/src/Makefile deleted file mode 100644 index b3b0b9d0..00000000 --- a/user/libs/libc/src/Makefile +++ /dev/null @@ -1,38 +0,0 @@ -GARBAGE_PATTERNS := *.o *.s~ *.s *.S~ *.c~ *.h~ kernel -GARBAGE := $(foreach DIR,$(DIRS),$(addprefix $(DIR)/,$(GARBAGE_PATTERNS))) - -all: libc - -CFLAGS += -I . - -libc_sub_dirs=math sys - -ifeq ($(ARCH), x86_64) -libc_sub_dirs += arch/x86_64 -endif - -$(libc_sub_dirs): ECHO - $(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS) -I $(shell pwd)" - -libc_objs:= $(shell find ./*.c) - -ECHO: - @echo "$@" - - -$(libc_objs): ECHO - $(CC) $(CFLAGS) -c $@ -o $@.o - -clean: - cargo clean - rm -rf $(GARBAGE) - @list='$(libc_sub_dirs)'; for subdir in $$list; do \ - echo "Clean in dir: $$subdir";\ - cd $$subdir && $(MAKE) clean;\ - cd .. ;\ - done - -libc: $(libc_objs) $(libc_sub_dirs) libc_rust - -libc_rust: - cargo +nightly-2023-08-15 build --release --target ./arch/x86_64/x86_64-unknown-none.json diff --git a/user/libs/libc/src/arch/x86_64/Makefile b/user/libs/libc/src/arch/x86_64/Makefile deleted file mode 100644 index 08aaba5b..00000000 --- a/user/libs/libc/src/arch/x86_64/Makefile +++ /dev/null @@ -1,25 +0,0 @@ -libc_arch_objs:= $(shell find ./*.c) - -ECHO: - @echo "$@" - - -$(libc_arch_objs): ECHO - $(CC) $(CFLAGS) -c $@ -o $@.o - -# 由于目前使用的是raw的gcc,所以不需要crti.o和crtn.o(待更换为x86_64-dragonos-gcc后再改这里) -# all: $(libc_arch_objs) crti.o crtn.o -all: $(libc_arch_objs) - mv crt0.c.o crt0.o - -# crti.o: crti.S -# $(CC) -E crti.S > _crti.s # 预处理 -# $(AS) $(ASFLAGS) -o crti.o _crti.s - -# crtn.o: crtn.S -# $(CC) -E crtn.S > _crtn.s # 预处理 -# $(AS) $(ASFLAGS) -o crtn.o _crtn.s - -clean: - - echo "Done." \ No newline at end of file diff --git a/user/libs/libc/src/arch/x86_64/crt0.c b/user/libs/libc/src/arch/x86_64/crt0.c deleted file mode 100644 index 49726c92..00000000 --- a/user/libs/libc/src/arch/x86_64/crt0.c +++ /dev/null @@ -1,23 +0,0 @@ - -#include -#include - -extern int main(int, char **); -extern void _init(); -extern void _libc_init(); - -void _start(int argc, char **argv) -{ - // Run the global constructors. - _init(); - _libc_init(); - int retval = main(argc, argv); - exit(retval); -} - -void _init(){ - -} -void _fini(){ - -} \ No newline at end of file diff --git a/user/libs/libc/src/arch/x86_64/crti.S b/user/libs/libc/src/arch/x86_64/crti.S deleted file mode 100644 index 8e9c911e..00000000 --- a/user/libs/libc/src/arch/x86_64/crti.S +++ /dev/null @@ -1,13 +0,0 @@ -.section .init -.global _init -_init: - push %rbp - movq %rsp, %rbp - /* gcc will nicely put the contents of crtbegin.o's .init section here. */ - -.section .fini -.global _fini -_fini: - push %rbp - movq %rsp, %rbp - /* gcc will nicely put the contents of crtbegin.o's .fini section here. */ \ No newline at end of file diff --git a/user/libs/libc/src/arch/x86_64/crtn.S b/user/libs/libc/src/arch/x86_64/crtn.S deleted file mode 100644 index 00966552..00000000 --- a/user/libs/libc/src/arch/x86_64/crtn.S +++ /dev/null @@ -1,9 +0,0 @@ -.section .init - /* gcc will nicely put the contents of crtend.o's .init section here. */ - popq %rbp - ret - -.section .fini - /* gcc will nicely put the contents of crtend.o's .fini section here. */ - popq %rbp - ret \ No newline at end of file diff --git a/user/libs/libc/src/arch/x86_64/x86_64-unknown-none.json b/user/libs/libc/src/arch/x86_64/x86_64-unknown-none.json deleted file mode 100644 index a24b9878..00000000 --- a/user/libs/libc/src/arch/x86_64/x86_64-unknown-none.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "llvm-target": "x86_64-unknown-none", - "data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128", - "arch": "x86_64", - "target-endian": "little", - "target-pointer-width": "64", - "target-c-int-width": "32", - "os": "none", - "linker": "rust-lld", - "linker-flavor": "ld.lld", - "executables": true, - "features": "-mmx,-sse,+soft-float", - "disable-redzone": true, - "panic-strategy": "abort" -} diff --git a/user/libs/libc/src/ctype.c b/user/libs/libc/src/ctype.c deleted file mode 100644 index 25fdfd57..00000000 --- a/user/libs/libc/src/ctype.c +++ /dev/null @@ -1,75 +0,0 @@ -#include - - -int isprint(int c) -{ - if (c >= 0x20 && c <= 0x7e) - { - return 1; - } - - return 0; -} - -int islower(int c) -{ - if (c >= 'a' && c <= 'z') - { - return 1; - } - - return 0; -} - -int isupper(int c) -{ - if (c >= 'A' && c <= 'Z') - { - return 1; - } - - return 0; -} - -int isalpha(int c) -{ - if (islower(c) || isupper(c)) - { - return 1; - } - - return 0; -} - -int isdigit(int c) -{ - if (c >= '0' && c <= '9') - { - return 1; - } - - return 0; -} - -int toupper(int c) -{ - if (c >= 'a' && c <= 'z') - { - return c - 'a' + 'A'; - } - return c; -} - -int tolower(int c) -{ - if (c >= 'A' && c <= 'Z') - { - return c - 'A' + 'a'; - } - return c; -} - -int isspace(int c) -{ - return (c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v' || c == ' '); -} \ No newline at end of file diff --git a/user/libs/libc/src/cxa.c b/user/libs/libc/src/cxa.c deleted file mode 100644 index e2fe5076..00000000 --- a/user/libs/libc/src/cxa.c +++ /dev/null @@ -1,8 +0,0 @@ -/* Register a function to be called by exit or when a shared library - is unloaded. This function is only called from code generated by - the C++ compiler. */ -int __cxa_atexit(void (*func)(void *), void *arg, void *d) -{ - // todo: 使用rust实现这个函数。参考:http://redox.longjin666.cn/xref/relibc/src/cxa.rs?r=c7d499d4&mo=323&fi=15#15 - return 0; -} diff --git a/user/libs/libc/src/dirent.c b/user/libs/libc/src/dirent.c deleted file mode 100644 index b56a0565..00000000 --- a/user/libs/libc/src/dirent.c +++ /dev/null @@ -1,78 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -/** - * @brief 打开文件夹 - * - * @param dirname - * @return DIR* - */ -struct DIR *opendir(const char *path) -{ - int fd = open(path, O_DIRECTORY); - if (fd < 0) // 目录打开失败 - { - printf("Failed to open dir\n"); - return NULL; - } - // printf("open dir: %s\n", path); - - // 分配DIR结构体 - struct DIR *dirp = (struct DIR *)malloc(sizeof(struct DIR)); - // printf("dirp = %#018lx", dirp); - memset(dirp, 0, sizeof(struct DIR)); - dirp->fd = fd; - dirp->buf_len = DIR_BUF_SIZE; - dirp->buf_pos = 0; - - return dirp; -} - -/** - * @brief 关闭文件夹 - * - * @param dirp DIR结构体指针 - * @return int 成功:0, 失败:-1 -+--------+--------------------------------+ -| errno | 描述 | -+--------+--------------------------------+ -| 0 | 成功 | -| -EBADF | 当前dirp不指向一个打开了的目录 | -| -EINTR | 函数执行期间被信号打断 | -+--------+--------------------------------+ - */ -int closedir(struct DIR *dirp) -{ - int retval = close(dirp->fd); - free(dirp); - return retval; -} - -int64_t getdents(int fd, struct dirent *dirent, long count) -{ - return syscall_invoke(SYS_GET_DENTS, fd, (uint64_t)dirent, count, 0, 0, 0); -} -/** - * @brief 从目录中读取数据 - * - * @param dir - * @return struct dirent* - */ -struct dirent *readdir(struct DIR *dir) -{ - // printf("dir->buf = %#018lx\n", (dir->buf)); - memset((dir->buf), 0, DIR_BUF_SIZE); - // printf("memeset_ok\n"); - int len = getdents(dir->fd, (struct dirent *)dir->buf, DIR_BUF_SIZE); - // printf("len=%d\n", len); - if (len > 0) - return (struct dirent *)dir->buf; - else - return NULL; -} \ No newline at end of file diff --git a/user/libs/libc/src/errno.c b/user/libs/libc/src/errno.c deleted file mode 100644 index 3dd479f2..00000000 --- a/user/libs/libc/src/errno.c +++ /dev/null @@ -1,2 +0,0 @@ -#include -int errno = 0; \ No newline at end of file diff --git a/user/libs/libc/src/fcntl.c b/user/libs/libc/src/fcntl.c deleted file mode 100644 index 4ac43132..00000000 --- a/user/libs/libc/src/fcntl.c +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include - -/** - * @brief 打开文件的接口 - * - * @param path 文件路径 - * @param options 打开选项 - * @param ... - * @return int 文件描述符 - */ -int open(const char *path, int options, ...) -{ - return syscall_invoke(SYS_OPEN, (uint64_t)path, options, 0, 0, 0, 0); -} - -/** - * @brief ioctl的接口 - * - * @param fd 文件句柄 - * @param cmd 设备相关的请求类型 - * @param ... - * @return int 成功返回0 - */ -int ioctl(int fd, int cmd, uint64_t data, ...) -{ - return syscall_invoke(SYS_IOCTL, fd, cmd, data, 0, 0, 0); -} \ No newline at end of file diff --git a/user/libs/libc/src/include/export/__libc__.h b/user/libs/libc/src/include/export/__libc__.h deleted file mode 100644 index 3147bc74..00000000 --- a/user/libs/libc/src/include/export/__libc__.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#pragma once - -#ifdef __cplusplus - -# define __BEGIN_HEADER \ - extern "C" \ - { - -# define __END_HEADER \ - } - -#else - -# define __BEGIN_HEADER - -# define __END_HEADER - -#endif \ No newline at end of file diff --git a/user/libs/libc/src/include/export/ctype.h b/user/libs/libc/src/include/export/ctype.h deleted file mode 100644 index eb065e2d..00000000 --- a/user/libs/libc/src/include/export/ctype.h +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once - -#include - -#pragma once - -#include <__libc__.h> - - -#if defined(__cplusplus) -extern "C" { -#endif -// int isalnum(int c); -int isalpha(int c); -int isdigit(int c); -int islower(int c); -int isprint(int c); -// int isgraph(int c); -// int iscntrl(int c); -// int isgraph(int c); -// int ispunct(int c); -int isspace(int c); -int isupper(int c); -// int isxdigit(int c); - -int isascii(int c); - -int tolower(int c); -int toupper(int c); - -#define _U 01 -#define _L 02 -#define _N 04 -#define _S 010 -#define _P 020 -#define _C 040 -#define _X 0100 -#define _B 0200 - -extern char _ctype_[256]; - -#if defined(__cplusplus) -} /* extern "C" */ -#endif \ No newline at end of file diff --git a/user/libs/libc/src/include/export/dirent.h b/user/libs/libc/src/include/export/dirent.h deleted file mode 100644 index a7a4e07c..00000000 --- a/user/libs/libc/src/include/export/dirent.h +++ /dev/null @@ -1,117 +0,0 @@ -#pragma once -#include - -#if defined(__cplusplus) -extern "C" { -#endif - -/* - * This is a header for the common implementation of dirent - * to fs on-disk file type conversion. Although the fs on-disk - * bits are specific to every file system, in practice, many - * file systems use the exact same on-disk format to describe - * the lower 3 file type bits that represent the 7 POSIX file - * types. - * - * It is important to note that the definitions in this - * header MUST NOT change. This would break both the - * userspace ABI and the on-disk format of filesystems - * using this code. - * - * All those file systems can use this generic code for the - * conversions. - */ - -/* - * struct dirent file types - * exposed to user via getdents(2), readdir(3) - * - * These match bits 12..15 of stat.st_mode - * (ie "(i_mode >> 12) & 15"). - */ - -// 完整含义请见 http://www.gnu.org/software/libc/manual/html_node/Directory-Entries.html -#define S_DT_SHIFT 12 -#define S_DT(mode) (((mode) & S_IFMT) >> S_DT_SHIFT) -#define S_DT_MASK (S_IFMT >> S_DT_SHIFT) - -/* these are defined by POSIX and also present in glibc's dirent.h */ -#define DT_UNKNOWN 0 -// 命名管道,或者FIFO -#define DT_FIFO 1 -// 字符设备 -#define DT_CHR 2 -// 目录 -#define DT_DIR 4 -// 块设备 -#define DT_BLK 6 -// 常规文件 -#define DT_REG 8 -// 符号链接 -#define DT_LNK 10 -// 是一个socket -#define DT_SOCK 12 -// 这个是抄Linux的,还不知道含义 -#define DT_WHT 14 - -#define DT_MAX (S_DT_MASK + 1) /* 16 */ - -#define DIR_BUF_SIZE 256 - -/** - * @brief 文件夹结构体 - * - */ -struct DIR -{ - int fd; - int buf_pos; - int buf_len; - char buf[DIR_BUF_SIZE]; - - // todo: 加一个指向dirent结构体的指针 -}; - -struct dirent -{ - ino_t d_ino; // 文件序列号 - off_t d_off; // dir偏移量 - unsigned short d_reclen; // 目录下的记录数 - unsigned char d_type; // entry的类型 - char d_name[]; // 文件entry的名字(是一个零长数组) -}; - -/** - * @brief 打开文件夹 - * - * @param dirname - * @return DIR* - */ -struct DIR *opendir(const char *dirname); - -/** - * @brief 关闭文件夹 - * - * @param dirp DIR结构体指针 - * @return int 成功:0, 失败:-1 -+--------+--------------------------------+ -| errno | 描述 | -+--------+--------------------------------+ -| 0 | 成功 | -| -EBADF | 当前dirp不指向一个打开了的目录 | -| -EINTR | 函数执行期间被信号打断 | -+--------+--------------------------------+ - */ -int closedir(struct DIR *dirp); - -/** - * @brief 从目录中读取数据 - * - * @param dir - * @return struct dirent* - */ -struct dirent* readdir(struct DIR* dir); - -#if defined(__cplusplus) -} /* extern "C" */ -#endif \ No newline at end of file diff --git a/user/libs/libc/src/include/export/errno.h b/user/libs/libc/src/include/export/errno.h deleted file mode 100644 index 15a54983..00000000 --- a/user/libs/libc/src/include/export/errno.h +++ /dev/null @@ -1,155 +0,0 @@ -/** - * @file errno.h - * @author fslongjin (longjin@RinGoTek.cn) - * @brief - * @version 0.1 - * @date 2022-04-22 - * - * @copyright Copyright (c) 2022 - * - */ -#pragma once - -#if defined(__cplusplus) -extern "C" -{ -#endif - -#define EPERM 1 /* 操作不被允许 Operation not permitted. */ -#define ENOENT 2 /* 没有指定的文件或目录 No such file or directory. */ -#define ESRCH 3 /* 没有这样的进程 No such process. */ -#define EINTR 4 /* 被中断的函数 Interrupted function. */ -#define EIO 5 /* I/O错误 I/O error. */ -#define ENXIO 6 /* 没有这样的设备或地址 No such device or address. */ -#define E2BIG 7 /* 参数列表过长,或者在输出buffer中缺少空间 或者参数比系统内建的最大值要大 Argument list too long. */ -#define ENOEXEC 8 /* 可执行文件格式错误 Executable file format error */ -#define EBADF 9 /* 错误的文件描述符 Bad file descriptor. */ -#define ECHILD 10 /* 没有子进程 No child processes. */ -#define EAGAIN 11 /* 资源不可用,请重试。 Resource unavailable try again.(may be the same value as [EWOULDBLOCK]) */ -#define EWOULDBLOCK 11 /* 操作将被禁止 Operation would block.(may be the same value as [EAGAIN]). */ -#define ENOMEM 12 /* 没有足够的空间 Not enough space. */ -#define EACCES 13 /* 访问被拒绝 Permission denied */ -#define EFAULT 14 /* 错误的地址 Bad address */ -#define ENOTBLK 15 /* 需要块设备 Block device required */ -#define EBUSY 16 /* 设备或资源忙 Device or resource busy. */ -#define EEXIST 17 /* 文件已存在 File exists. */ -#define EXDEV 18 /* 跨设备连接 Cross-device link. */ -#define ENODEV 19 /* 没有指定的设备 No such device. */ -#define ENOTDIR 20 /* 不是目录 Not a directory. */ -#define EISDIR 21 /* 是一个目录 Is a directory */ -#define EINVAL 22 /* 不可用的参数 Invalid argument. */ -#define ENFILE 23 /* 系统中打开的文件过多 Too many files open in system. */ -#define EMFILE 24 /* 文件描述符的值过大 File descriptor value too large. */ -#define ENOTTY 25 /* 不正确的I/O控制操作 Inappropriate I/O control operation. */ -#define ETXTBSY 26 /* 文本文件忙 Text file busy. */ -#define EFBIG 27 /* 文件太大 File too large. */ -#define ENOSPC 28 /* 设备上没有空间 No space left on device. */ -#define ESPIPE 29 /* 错误的寻道.当前文件是pipe,不允许seek请求 Invalid seek. */ -#define EROFS 30 /* 只读的文件系统 Read-only file system. */ -#define EMLINK 31 /* 链接数过多 Too many links. */ -#define EPIPE 32 /* 断开的管道 Broken pipe. */ -#define EDOM 33 /* 数学参数超出作用域 Mathematics argument out of domain of function. */ -#define ERANGE 34 /* 结果过大 Result too large. */ -#define EDEADLK 35 /* 资源死锁将要发生 Resource deadlock would occur. */ -#define ENAMETOOLONG 36 /* 文件名过长 Filename too long. */ -#define ENOLCK 37 /* 没有可用的锁 No locks available. */ -#define ENOSYS 38 /* 功能不支持 Function not supported. */ -#define ENOTEMPTY 39 /* 目录非空 Directory not empty. */ -#define ELOOP 40 /* 符号链接级别过多 Too many levels of symbolic links. */ -#define ENOMSG 41 /* 没有期待类型的消息 No message of the desired type. */ -#define EIDRM 42 /* 标志符被移除 Identifier removed. */ -#define ECHRNG 43 /* 通道号超出范围 Channel number out of range */ -#define EL2NSYNC 44 /* 二级不同步 Level 2 not synchronized */ -#define EL3HLT 45 /* 三级暂停 Level 3 halted */ -#define EL3RST 46 /* 三级重置 Level 3 reset */ -#define ELNRNG 47 /* 链接号超出范围 Link number out of range */ -#define EUNATCH 48 /* 未连接协议驱动程序 Protocol driver not attached */ -#define ENOCSI 49 /* 没有可用的CSI结构 No CSI structure available */ -#define EL2HLT 50 /* 二级暂停 Level 2 halted */ -#define EBADE 51 /* 无效交换 Invalid exchange */ -#define EBADR 52 /* 无效的请求描述符 Invalid request descriptor */ -#define EXFULL 53 /* 交换满 Exchange full */ -#define ENOANO 54 /* 无阳极 No anode */ -#define EBADRQC 55 /* 请求码无效 Invalid request code */ -#define EBADSLT 56 /* 无效插槽 Invalid slot */ -#define EDEADLOCK 57 /* 资源死锁 Resource deadlock would occur */ -#define EBFONT 58 /* 错误的字体文件格式 Bad font file format */ -#define ENOSTR 59 /* 不是STREAM Not a STREAM */ -#define ENODATA 60 /* 队列头没有可读取的消息 No message is available on the STREAM head read queue. */ -#define ETIME 61 /* 流式ioctl()超时 Stream ioctl() timeout */ -#define ENOSR 62 /* 没有STREAM资源 No STREAM resources. */ -#define ENONET 63 /* 机器不在网络上 Machine is not on the network */ -#define ENOPKG 64 /* 未安装软件包 Package not installed */ -#define EREMOTE 65 /* 远程对象 Object is remote */ -#define ENOLINK 66 /* 保留 Reserved. */ -#define EADV 67 /* 外设错误 Advertise error. */ -#define ESRMNT 68 /* 安装错误 Srmount error */ -#define ECOMM 69 /* 发送时发生通信错误 Communication error on send */ -#define EPROTO 70 /* 协议错误 Protocol error. */ -#define EMULTIHOP 71 /* 保留使用 Reserved. */ -#define EDOTDOT 72 /* RFS特定错误 RFS specific error */ -#define EBADMSG 73 /* 错误的消息 Bad message. */ -#define EOVERFLOW 74 /* 数值过大,产生溢出 Value too large to be stored in data type. */ -#define ENOTUNIQ 75 /* 名称在网络上不是唯一的 Name not unique on network */ -#define EBADFD 76 /* 处于不良状态的文件描述符 File descriptor in bad state */ -#define EREMCHG 77 /* 远程地址已更改 Remote address changed */ -#define ELIBACC 78 /* 无法访问所需的共享库 Can not access a needed shared library */ -#define ELIBBAD 79 /* 访问损坏的共享库 Accessing a corrupted shared library */ -#define ELIBSCN 80 /* a. out中的.lib部分已损坏 .lib section in a.out corrupted */ -#define ELIBMAX 81 /* 尝试链接太多共享库 Attempting to link in too many shared libraries */ -#define ELIBEXEC 82 /* 无法直接执行共享库 Cannot exec a shared library directly */ -#define EILSEQ 83 /* 不合法的字符序列 Illegal byte sequence. */ -#define ERESTART 84 /* 中断的系统调用应该重新启动 Interrupted system call should be restarted */ -#define ESTRPIPE 85 /* 流管道错误 Streams pipe error */ -#define EUSERS 86 /* 用户太多 Too many users */ -#define ENOTSOCK 87 /* 不是一个套接字 Not a socket. */ -#define EDESTADDRREQ 88 /* 需要目标地址 Destination address required. */ -#define EMSGSIZE 89 /* 消息过大 Message too large. */ -#define EPROTOTYPE 90 /* 对于套接字而言,错误的协议 Protocol wrong type for socket. */ -#define ENOPROTOOPT 91 /* 协议不可用 Protocol not available. */ -#define EPROTONOSUPPORT 92 /* 协议不被支持 Protocol not supported. */ -#define ESOCKTNOSUPPORT 93 /* 不支持套接字类型 Socket type not supported */ -#define EOPNOTSUPP 94 /* 套接字不支持该操作 Operation not supported on socket (may be the same value as [ENOTSUP]). */ -#define ENOTSUP 94 /* 不被支持 Not supported (may be the same value as [EOPNOTSUPP]). */ -#define EPFNOSUPPORT 95 /* 不支持协议系列 Protocol family not supported */ -#define EAFNOSUPPORT 96 /* 地址family不支持 Address family not supported. */ -#define EADDRINUSE 97 /* 地址正在被使用 Address in use. */ -#define EADDRNOTAVAIL 98 /* 地址不可用 Address not available. */ -#define ENETDOWN 99 /* 网络已关闭 Network is down. */ -#define ENETUNREACH 100 /* 网络不可达 Network unreachable. */ -#define ENETRESET 101 /* 网络连接已断开 Connection aborted by network. */ -#define ECONNABORTED 102 /* 连接已断开 Connection aborted. */ -#define ECONNRESET 103 /* 连接被重置 Connection reset. */ -#define ENOBUFS 104 /* 缓冲区空间不足 No buffer space available. */ -#define EISCONN 105 /* 套接字已连接 Socket is connected. */ -#define ENOTCONN 106 /* 套接字未连接 The socket is not connected. */ -#define ESHUTDOWN 107 /* 传输端点关闭后无法发送 Cannot send after transport endpoint shutdown */ -#define ETOOMANYREFS 108 /* 引用太多:无法拼接 Too many references: cannot splice */ -#define ETIMEDOUT 109 /* 连接超时 Connection timed out. */ -#define ECONNREFUSED 110 /* 连接被拒绝 Connection refused. */ -#define EHOSTDOWN 111 /* 主机已关闭 Host is down */ -#define EHOSTUNREACH 112 /* 主机不可达 Host is unreachable. */ -#define EALREADY 113 /* 连接已经在处理 Connection already in progress. */ -#define EINPROGRESS 114 /* 操作正在处理 Operation in progress. */ -#define ESTALE 115 /* 保留 Reserved. */ -#define EUCLEAN 116 /* 结构需要清理 Structure needs cleaning */ -#define ENOTNAM 117 /* 不是XENIX命名类型文件 Not a XENIX named type file */ -#define ENAVAIL 118 /* 没有可用的XENIX信号量 No XENIX semaphores available */ -#define EISNAM 119 /* 是命名类型文件 Is a named type file */ -#define EREMOTEIO 120 /* 远程I/O错误 Remote I/O error */ -#define EDQUOT 121 /* 保留使用 Reserved */ -#define ENOMEDIUM 122 /* 没有找到媒介 No medium found */ -#define EMEDIUMTYPE 123 /* 介质类型错误 Wrong medium type */ -#define ECANCELED 124 /* 操作被取消 Operation canceled. */ -#define ENOKEY 125 /* 所需的密钥不可用 Required key not available */ -#define EKEYEXPIRED 126 /* 密钥已过期 Key has expired */ -#define EKEYREVOKED 127 /* 密钥已被撤销 Key has been revoked */ -#define EKEYREJECTED 128 /* 密钥被服务拒绝 Key has been revoked */ -#define EOWNERDEAD 129 /* 之前的拥有者挂了 Previous owner died. */ -#define ENOTRECOVERABLE 130 /* 状态不可恢复 State not recoverable. */ - - extern int errno; - -#if defined(__cplusplus) -} /* extern "C" */ -#endif \ No newline at end of file diff --git a/user/libs/libc/src/include/export/fcntl.h b/user/libs/libc/src/include/export/fcntl.h deleted file mode 100644 index 31ef4da0..00000000 --- a/user/libs/libc/src/include/export/fcntl.h +++ /dev/null @@ -1,89 +0,0 @@ -/** - * @file fcntl.h - * @author fslongjin (longjin@RinGoTek.cn) - * @brief - * @version 0.1 - * @date 2022-04-26 - * - * @copyright Copyright (c) 2022 - * - */ -#pragma once - -#if defined(__cplusplus) -extern "C" { - -#endif -#include - -#define O_RDONLY 00000000 // Open Read-only -#define O_WRONLY 00000001 // Open Write-only -#define O_RDWR 00000002 // Open read/write -#define O_ACCMODE 00000003 // Mask for file access modes - -#define O_CREAT 00000100 // Create file if it does not exist -#define O_EXCL 00000200 // Fail if file already exists -#define O_NOCTTY 00000400 // Do not assign controlling terminal - -#define O_TRUNC 00001000 // 文件存在且是普通文件,并以O_RDWR或O_WRONLY打开,则它会被清空 - -#define O_APPEND 00002000 // 文件指针会被移动到文件末尾 - -#define O_NONBLOCK 00004000 // 非阻塞式IO模式 - -#define O_DSYNC 00010000 // used to be O_SYNC, see below -#define FASYNC 00020000 // fcntl, for BSD compatibility -#define O_DIRECT 00040000 // direct disk access hint -#define O_LARGEFILE 00100000 -#define O_DIRECTORY 00200000 // 打开的必须是一个目录 -#define O_NOFOLLOW 00400000 // Do not follow symbolic links -#define O_NOATIME 01000000 -#define O_CLOEXEC 02000000 // set close_on_exec - -/* - * The constants AT_REMOVEDIR and AT_EACCESS have the same value. AT_EACCESS is - * meaningful only to faccessat, while AT_REMOVEDIR is meaningful only to - * unlinkat. The two functions do completely different things and therefore, - * the flags can be allowed to overlap. For example, passing AT_REMOVEDIR to - * faccessat would be undefined behavior and thus treating it equivalent to - * AT_EACCESS is valid undefined behavior. - */ -// 作为当前工作目录的文件描述符(用于指代cwd) -#define AT_FDCWD -100 -#define AT_SYMLINK_NOFOLLOW 0x100 /* Do not follow symbolic links. */ -#define AT_EACCESS 0x200 /* Test access permitted for effective IDs, not real IDs. */ -#define AT_REMOVEDIR 0x200 /* Remove directory instead of unlinking file. */ -#define AT_SYMLINK_FOLLOW 0x400 /* Follow symbolic links. */ -#define AT_NO_AUTOMOUNT 0x800 /* Suppress terminal automount traversal */ -#define AT_EMPTY_PATH 0x1000 /* Allow empty relative pathname */ - -#define AT_STATX_SYNC_TYPE 0x6000 /* Type of synchronisation required from statx() */ -#define AT_STATX_SYNC_AS_STAT 0x0000 /* - Do whatever stat() does */ -#define AT_STATX_FORCE_SYNC 0x2000 /* - Force the attributes to be sync'd with the server */ -#define AT_STATX_DONT_SYNC 0x4000 /* - Don't sync attributes with the server */ - -#define AT_RECURSIVE 0x8000 /* Apply to the entire subtree */ - -/** - * @brief 打开文件的接口 - * - * @param path 文件路径 - * @param options 打开选项 - * @param ... - * @return int 文件描述符 - */ -int open(const char * path, int options, ...); - -/** - * @brief ioctl的接口 - * - * @param fd 文件句柄 - * @param cmd 设备相关的请求类型 - * @param ... - * @return int 成功返回0 - */ -int ioctl(int fd, int cmd, uint64_t data, ...); - -#if defined(__cplusplus) -} /* extern "C" */ -#endif \ No newline at end of file diff --git a/user/libs/libc/src/include/export/libm.h b/user/libs/libc/src/include/export/libm.h deleted file mode 100644 index 85f0f630..00000000 --- a/user/libs/libc/src/include/export/libm.h +++ /dev/null @@ -1,83 +0,0 @@ -#pragma once -#include - -#if defined(__cplusplus) -extern "C" { -#endif - -// ===== 描述long double 的数据比特结构 -#if __LDBL_MANT_DIG__ == 53 && __LDBL_MAX_EXP__ == 1024 -#elif __LDBL_MANT_DIG__ == 64 && __LDBL_MAX_EXP__ == 16384 && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ -union ldshape -{ - long double f; - struct - { - uint64_t m; - uint16_t se; - } i; -}; -#elif __LDBL_MANT_DIG__ == 113 && __LDBL_MAX_EXP__ == 16384 && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ -union ldshape -{ - long double f; - struct - { - uint64_t lo; - uint32_t mid; - uint16_t top; - uint16_t se; - } i; - struct - { - uint64_t lo; - uint64_t hi; - } i2; -}; -#elif __LDBL_MANT_DIG__ == 113 && __LDBL_MAX_EXP__ == 16384 && __BYTE_ORDER__ == __BIG_ENDIAN -union ldshape -{ - long double f; - struct - { - uint16_t se; - uint16_t top; - uint32_t mid; - uint64_t lo; - } i; - struct - { - uint64_t hi; - uint64_t lo; - } i2; -}; -#else -#error Unsupported long double representation -#endif - -#define FORCE_EVAL(x) \ - do \ - { \ - if (sizeof(x) == sizeof(float)) \ - { \ - volatile float __x; \ - __x = (x); \ - (void)__x; \ - } \ - else if (sizeof(x) == sizeof(double)) \ - { \ - volatile double __x; \ - __x = (x); \ - (void)__x; \ - } \ - else \ - { \ - volatile long double __x; \ - __x = (x); \ - (void)__x; \ - } \ - } while (0) - -#if defined(__cplusplus) -} /* extern "C" */ -#endif \ No newline at end of file diff --git a/user/libs/libc/src/include/export/math.h b/user/libs/libc/src/include/export/math.h deleted file mode 100644 index 4476ec74..00000000 --- a/user/libs/libc/src/include/export/math.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include - -#if defined(__cplusplus) -extern "C" { -#endif - -double fabs(double x); -// float fabsf(float x); -long double fabsl(long double x); - -double round(double x); -float roundf(float x); -long double roundl(long double x); - -int64_t pow(int64_t x, int y); - -#if defined(__cplusplus) -} /* extern "C" */ -#endif \ No newline at end of file diff --git a/user/libs/libc/src/include/export/printf.h b/user/libs/libc/src/include/export/printf.h deleted file mode 100644 index fed1b3e5..00000000 --- a/user/libs/libc/src/include/export/printf.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#if defined(__cplusplus) -extern "C" { -#endif - -#define PAD_ZERO 1 // 0填充 -#define LEFT 2 // 靠左对齐 -#define RIGHT 4 // 靠右对齐 -#define PLUS 8 // 在正数前面显示加号 -#define SPACE 16 -#define SPECIAL 32 // 在八进制数前面显示 '0o',在十六进制数前面显示 '0x' 或 '0X' -#define SMALL 64 // 十进制以上数字显示小写字母 -#define SIGN 128 // 显示符号位 - -#define is_digit(c) ((c) >= '0' && (c) <= '9') // 用来判断是否是数字的宏 - -#if defined(__cplusplus) -} /* extern "C" */ -#endif \ No newline at end of file diff --git a/user/libs/libc/src/include/export/signal.h b/user/libs/libc/src/include/export/signal.h deleted file mode 100644 index 88ac5bb6..00000000 --- a/user/libs/libc/src/include/export/signal.h +++ /dev/null @@ -1,102 +0,0 @@ -#pragma once -#include - -#if defined(__cplusplus) -extern "C" { -#endif - -#define SIGHUP 1 -#define SIGINT 2 -#define SIGQUIT 3 -#define SIGILL 4 -#define SIGTRAP 5 -#define SIGABRT 6 -#define SIGIOT 6 -#define SIGBUS 7 -#define SIGFPE 8 -#define SIGKILL 9 -#define SIGUSR1 10 -#define SIGSEGV 11 -#define SIGUSR2 12 -#define SIGPIPE 13 -#define SIGALRM 14 -#define SIGTERM 15 -#define SIGSTKFLT 16 -#define SIGCHLD 17 -#define SIGCONT 18 -#define SIGSTOP 19 -#define SIGTSTP 20 -#define SIGTTIN 21 -#define SIGTTOU 22 -#define SIGURG 23 -#define SIGXCPU 24 -#define SIGXFSZ 25 -#define SIGVTALRM 26 -#define SIGPROF 27 -#define SIGWINCH 28 -#define SIGIO 29 -#define SIGPOLL SIGIO - -#define SIGPWR 30 -#define SIGSYS 31 - -/* These should not be considered constants from userland. */ -#define SIGRTMIN 32 -#define SIGRTMAX MAX_SIG_NUM - -typedef void (*__sighandler_t)(int); - -#define SIG_DFL ((__sighandler_t)0) /* Default action. */ -#define SIG_IGN ((__sighandler_t)1) /* Ignore signal. */ - -// 注意,该结构体最大16字节 -union __sifields { - /* kill() */ - struct - { - pid_t _pid; /* 信号发送者的pid */ - } _kill; -}; - -// 注意,该结构体最大大小为32字节 -#define __SIGINFO \ - struct \ - { \ - int32_t si_signo; /* signal number */ \ - int32_t si_code; \ - int32_t si_errno; \ - uint32_t reserved; /* 保留备用 */ \ - union __sifields _sifields; \ - } - -typedef struct -{ - union { - __SIGINFO; - uint64_t padding[4]; // 让siginfo占用32字节大小 - }; -} siginfo_t; - -typedef struct -{ - uint64_t set; -} sigset_t; - -struct sigaction -{ - // sa_handler和sa_sigaction二选1 - __sighandler_t sa_handler; - void (*sa_sigaction)(int, siginfo_t *, void *); - sigset_t sa_mask; - uint64_t sa_flags; - void (*sa_restorer)(void); -}; - -int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact); -int signal(int signum, __sighandler_t handler); -int raise(int sig); -int kill(pid_t, int sig); - -#if defined(__cplusplus) -} /* extern "C" */ -#endif \ No newline at end of file diff --git a/user/libs/libc/src/include/export/stddef.h b/user/libs/libc/src/include/export/stddef.h deleted file mode 100644 index 7038969d..00000000 --- a/user/libs/libc/src/include/export/stddef.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include - -#ifdef __cplusplus - #define NULL 0 -#else - #define NULL ((void *)0) -#endif - -#if defined(__cplusplus) -extern "C" { -#endif - -typedef __PTRDIFF_TYPE__ ptrdiff_t; // Signed integer type of the result of subtracting two pointers. - -#if defined(__cplusplus) -} /* extern "C" */ -#endif \ No newline at end of file diff --git a/user/libs/libc/src/include/export/stdint.h b/user/libs/libc/src/include/export/stdint.h deleted file mode 100644 index 63e3b2da..00000000 --- a/user/libs/libc/src/include/export/stdint.h +++ /dev/null @@ -1,316 +0,0 @@ -/* Copyright (C) 1997-2022 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, see - . */ - -/* - * ISO C99: 7.18 Integer types - */ - -#ifndef _STDINT_H -#define _STDINT_H 1 - - - -# define __WORDSIZE 64 - -/* Convenience types. */ -typedef unsigned char __u_char; -typedef unsigned short int __u_short; -typedef unsigned int __u_int; -typedef unsigned long int __u_long; - -/* Fixed-size types, underlying types depend on word size and compiler. */ -typedef signed char __int8_t; -typedef unsigned char __uint8_t; -typedef signed short int __int16_t; -typedef unsigned short int __uint16_t; -typedef signed int __int32_t; -typedef unsigned int __uint32_t; -#if __WORDSIZE == 64 -typedef signed long int __int64_t; -typedef unsigned long int __uint64_t; -#else -__extension__ typedef signed long long int __int64_t; -__extension__ typedef unsigned long long int __uint64_t; -#endif - -/* Smallest types with at least a given width. */ -typedef __int8_t __int_least8_t; -typedef __uint8_t __uint_least8_t; -typedef __int16_t __int_least16_t; -typedef __uint16_t __uint_least16_t; -typedef __int32_t __int_least32_t; -typedef __uint32_t __uint_least32_t; -typedef __int64_t __int_least64_t; -typedef __uint64_t __uint_least64_t; - -typedef __uint8_t uint8_t; -typedef __uint16_t uint16_t; -typedef __uint32_t uint32_t; -typedef __uint64_t uint64_t; - -typedef __int8_t int8_t; -typedef __int16_t int16_t; -typedef __int32_t int32_t; -typedef __int64_t int64_t; - -/* Small types. */ - -/* Signed. */ -typedef __int_least8_t int_least8_t; -typedef __int_least16_t int_least16_t; -typedef __int_least32_t int_least32_t; -typedef __int_least64_t int_least64_t; - -/* Unsigned. */ -typedef __uint_least8_t uint_least8_t; -typedef __uint_least16_t uint_least16_t; -typedef __uint_least32_t uint_least32_t; -typedef __uint_least64_t uint_least64_t; - - -/* Fast types. */ - -/* Signed. */ -typedef signed char int_fast8_t; -#if __WORDSIZE == 64 -typedef long int int_fast16_t; -typedef long int int_fast32_t; -typedef long int int_fast64_t; -#else -typedef int int_fast16_t; -typedef int int_fast32_t; -__extension__ -typedef long long int int_fast64_t; -#endif - -/* Unsigned. */ -typedef unsigned char uint_fast8_t; -#if __WORDSIZE == 64 -typedef unsigned long int uint_fast16_t; -typedef unsigned long int uint_fast32_t; -typedef unsigned long int uint_fast64_t; -#else -typedef unsigned int uint_fast16_t; -typedef unsigned int uint_fast32_t; -__extension__ -typedef unsigned long long int uint_fast64_t; -#endif - - -/* Types for `void *' pointers. */ -#if __WORDSIZE == 64 -# ifndef __intptr_t_defined -typedef long int intptr_t; -# define __intptr_t_defined -# endif -typedef unsigned long int uintptr_t; -#else -# ifndef __intptr_t_defined -typedef int intptr_t; -# define __intptr_t_defined -# endif -typedef unsigned int uintptr_t; -#endif - -/* Largest integral types. */ -#if __WORDSIZE == 64 -typedef long int __intmax_t; -typedef unsigned long int __uintmax_t; -#else -__extension__ typedef long long int __intmax_t; -__extension__ typedef unsigned long long int __uintmax_t; -#endif - - -/* Largest integral types. */ -typedef __intmax_t intmax_t; -typedef __uintmax_t uintmax_t; - - -# if __WORDSIZE == 64 -# define __INT64_C(c) c ## L -# define __UINT64_C(c) c ## UL -# else -# define __INT64_C(c) c ## LL -# define __UINT64_C(c) c ## ULL -# endif - -/* Limits of integral types. */ - -/* Minimum of signed integral types. */ -# define INT8_MIN (-128) -# define INT16_MIN (-32767-1) -# define INT32_MIN (-2147483647-1) -# define INT64_MIN (-__INT64_C(9223372036854775807)-1) -/* Maximum of signed integral types. */ -# define INT8_MAX (127) -# define INT16_MAX (32767) -# define INT32_MAX (2147483647) -# define INT64_MAX (__INT64_C(9223372036854775807)) - -/* Maximum of unsigned integral types. */ -# define UINT8_MAX (255) -# define UINT16_MAX (65535) -# define UINT32_MAX (4294967295U) -# define UINT64_MAX (__UINT64_C(18446744073709551615)) - - -/* Minimum of signed integral types having a minimum size. */ -# define INT_LEAST8_MIN (-128) -# define INT_LEAST16_MIN (-32767-1) -# define INT_LEAST32_MIN (-2147483647-1) -# define INT_LEAST64_MIN (-__INT64_C(9223372036854775807)-1) -/* Maximum of signed integral types having a minimum size. */ -# define INT_LEAST8_MAX (127) -# define INT_LEAST16_MAX (32767) -# define INT_LEAST32_MAX (2147483647) -# define INT_LEAST64_MAX (__INT64_C(9223372036854775807)) - -/* Maximum of unsigned integral types having a minimum size. */ -# define UINT_LEAST8_MAX (255) -# define UINT_LEAST16_MAX (65535) -# define UINT_LEAST32_MAX (4294967295U) -# define UINT_LEAST64_MAX (__UINT64_C(18446744073709551615)) - - -/* Minimum of fast signed integral types having a minimum size. */ -# define INT_FAST8_MIN (-128) -# if __WORDSIZE == 64 -# define INT_FAST16_MIN (-9223372036854775807L-1) -# define INT_FAST32_MIN (-9223372036854775807L-1) -# else -# define INT_FAST16_MIN (-2147483647-1) -# define INT_FAST32_MIN (-2147483647-1) -# endif -# define INT_FAST64_MIN (-__INT64_C(9223372036854775807)-1) -/* Maximum of fast signed integral types having a minimum size. */ -# define INT_FAST8_MAX (127) -# if __WORDSIZE == 64 -# define INT_FAST16_MAX (9223372036854775807L) -# define INT_FAST32_MAX (9223372036854775807L) -# else -# define INT_FAST16_MAX (2147483647) -# define INT_FAST32_MAX (2147483647) -# endif -# define INT_FAST64_MAX (__INT64_C(9223372036854775807)) - -/* Maximum of fast unsigned integral types having a minimum size. */ -# define UINT_FAST8_MAX (255) -# if __WORDSIZE == 64 -# define UINT_FAST16_MAX (18446744073709551615UL) -# define UINT_FAST32_MAX (18446744073709551615UL) -# else -# define UINT_FAST16_MAX (4294967295U) -# define UINT_FAST32_MAX (4294967295U) -# endif -# define UINT_FAST64_MAX (__UINT64_C(18446744073709551615)) - - -/* Values to test for integral types holding `void *' pointer. */ -# if __WORDSIZE == 64 -# define INTPTR_MIN (-9223372036854775807L-1) -# define INTPTR_MAX (9223372036854775807L) -# define UINTPTR_MAX (18446744073709551615UL) -# else -# define INTPTR_MIN (-2147483647-1) -# define INTPTR_MAX (2147483647) -# define UINTPTR_MAX (4294967295U) -# endif - - -/* Minimum for largest signed integral type. */ -# define INTMAX_MIN (-__INT64_C(9223372036854775807)-1) -/* Maximum for largest signed integral type. */ -# define INTMAX_MAX (__INT64_C(9223372036854775807)) - -/* Maximum for largest unsigned integral type. */ -# define UINTMAX_MAX (__UINT64_C(18446744073709551615)) - - -/* Limits of other integer types. */ - -/* Limits of `ptrdiff_t' type. */ -# if __WORDSIZE == 64 -# define PTRDIFF_MIN (-9223372036854775807L-1) -# define PTRDIFF_MAX (9223372036854775807L) -# else -# if __WORDSIZE32_PTRDIFF_LONG -# define PTRDIFF_MIN (-2147483647L-1) -# define PTRDIFF_MAX (2147483647L) -# else -# define PTRDIFF_MIN (-2147483647-1) -# define PTRDIFF_MAX (2147483647) -# endif -# endif - -/* Limits of `sig_atomic_t'. */ -# define SIG_ATOMIC_MIN (-2147483647-1) -# define SIG_ATOMIC_MAX (2147483647) - -/* Limit of `size_t' type. */ -# if __WORDSIZE == 64 -# define SIZE_MAX (18446744073709551615UL) -# else -# if __WORDSIZE32_SIZE_ULONG -# define SIZE_MAX (4294967295UL) -# else -# define SIZE_MAX (4294967295U) -# endif -# endif - -/* Limits of `wchar_t'. */ -# ifndef WCHAR_MIN -/* These constants might also be defined in . */ -# define WCHAR_MIN __WCHAR_MIN -# define WCHAR_MAX __WCHAR_MAX -# endif - -/* Limits of `wint_t'. */ -# define WINT_MIN (0u) -# define WINT_MAX (4294967295u) - -/* Signed. */ -# define INT8_C(c) c -# define INT16_C(c) c -# define INT32_C(c) c -# if __WORDSIZE == 64 -# define INT64_C(c) c ## L -# else -# define INT64_C(c) c ## LL -# endif - -/* Unsigned. */ -# define UINT8_C(c) c -# define UINT16_C(c) c -# define UINT32_C(c) c ## U -# if __WORDSIZE == 64 -# define UINT64_C(c) c ## UL -# else -# define UINT64_C(c) c ## ULL -# endif - -/* Maximal type. */ -# if __WORDSIZE == 64 -# define INTMAX_C(c) c ## L -# define UINTMAX_C(c) c ## UL -# else -# define INTMAX_C(c) c ## LL -# define UINTMAX_C(c) c ## ULL -# endif - - -#endif /* stdint.h */ \ No newline at end of file diff --git a/user/libs/libc/src/include/export/stdio.h b/user/libs/libc/src/include/export/stdio.h deleted file mode 100644 index 5d6a9d6b..00000000 --- a/user/libs/libc/src/include/export/stdio.h +++ /dev/null @@ -1,63 +0,0 @@ -#pragma once - -#include -#include - -#if defined(__cplusplus) -extern "C" { -#endif - -// 字体颜色的宏定义 -#define COLOR_WHITE 0x00ffffff //白 -#define COLOR_BLACK 0x00000000 //黑 -#define COLOR_RED 0x00ff0000 //红 -#define COLOR_ORANGE 0x00ff8000 //橙 -#define COLOR_YELLOW 0x00ffff00 //黄 -#define COLOR_GREEN 0x0000ff00 //绿 -#define COLOR_BLUE 0x000000ff //蓝 -#define COLOR_INDIGO 0x0000ffff //靛 -#define COLOR_PURPLE 0x008000ff //紫 - -#define SEEK_SET 0 /* Seek relative to start-of-file */ -#define SEEK_CUR 1 /* Seek relative to current position */ -#define SEEK_END 2 /* Seek relative to end-of-file */ - -#define SEEK_MAX 3 - -/* The value returned by fgetc and similar functions to indicate the - end of the file. */ -#define EOF (-1) - -typedef struct { - int fd; // 文件描述符 -} FILE; - -extern FILE* stdin; -extern FILE* stdout; -extern FILE* stderr; - -/** - * @brief 往屏幕上输出字符串 - * - * @param str 字符串指针 - * @param front_color 前景色 - * @param bg_color 背景色 - * @return int64_t - */ -int64_t put_string(char *str, uint64_t front_color, uint64_t bg_color); - -int printf(const char *fmt, ...); -int sprintf(char *buf, const char *fmt, ...); -int vsprintf(char *buf, const char *fmt, va_list args); - -int fflush(FILE *stream); -int fprintf(FILE *restrict stream, const char *restrict format, ...); -int ferror(FILE *stream); -FILE *fopen(const char *restrict pathname, const char *restrict mode); -int fclose(FILE *stream); -int puts(const char *s); -int putchar(int c); -int getchar(void); -#if defined(__cplusplus) -} /* extern "C" */ -#endif diff --git a/user/libs/libc/src/include/export/stdlib.h b/user/libs/libc/src/include/export/stdlib.h deleted file mode 100644 index fd541439..00000000 --- a/user/libs/libc/src/include/export/stdlib.h +++ /dev/null @@ -1,56 +0,0 @@ -#pragma once -#include - -#if defined(__cplusplus) -extern "C" { -#endif - -/** - * @brief 获取一块堆内存 - * - * @param size 内存大小 - * @return void* 内存空间的指针 - */ -void *malloc(ssize_t size); - -/** - * @brief 释放一块堆内存 - * - * @param ptr 堆内存的指针 - */ -void free(void* ptr); - -/** - * @brief 返回int的绝对值 - * - * @param i - * @return int - */ -int abs(int i); -long labs(long i); -long long llabs(long long i); - -/** - * @brief 字符串转int - * - * @param str - * @return int - */ -int atoi(const char * str); - -/** - * @brief 退出进程 - * - * @param status - */ -void exit(int status); - -/** - * @brief 通过发送SIGABRT,从而退出当前进程 - * - */ -void abort(); - -#if defined(__cplusplus) -} /* extern "C" */ -#endif diff --git a/user/libs/libc/src/include/export/string.h b/user/libs/libc/src/include/export/string.h deleted file mode 100644 index cd2f9880..00000000 --- a/user/libs/libc/src/include/export/string.h +++ /dev/null @@ -1,153 +0,0 @@ -#pragma once - -#include - -#if defined(__cplusplus) -extern "C" { -#endif - -void *memset(void *dst, unsigned char C, uint64_t size); - -/** - * @brief 获取字符串的长度 - * - * @param s 字符串 - * @return size_t 长度 - */ -size_t strlen(const char *s); - -/* - 比较字符串 FirstPart and SecondPart - FirstPart = SecondPart => 0 - FirstPart > SecondPart => 1 - FirstPart < SecondPart => -1 -*/ -int strcmp(const char *FirstPart, const char *SecondPart); - -/** - * @brief 拷贝指定字节数的字符串 - * - * @param dst 目标地址 - * @param src 源字符串 - * @param Count 字节数 - * @return char* - */ -char *strncpy(char *dst, const char *src, size_t count); - -/** - * @brief 拷贝整个字符串 - * - * @param dst 目标地址 - * @param src 源地址 - * @return char* 目标字符串 - */ -char* strcpy(char* dst, const char* src); - -/** - * @brief 拼接两个字符串(将src接到dest末尾) - * - * @param dest 目标串 - * @param src 源串 - * @return char* - */ -char *strcat(char *dest, const char *src); - -/** - * @brief 内存拷贝函数 - * - * @param dst 目标数组 - * @param src 源数组 - * @param Num 字节数 - * @return void* - */ -static void *memcpy(void *dst, const void *src, long Num) -{ - int d0 = 0, d1 = 0, d2 = 0; - __asm__ __volatile__("cld \n\t" - "rep \n\t" - "movsq \n\t" - "testb $4,%b4 \n\t" - "je 1f \n\t" - "movsl \n\t" - "1:\ttestb $2,%b4 \n\t" - "je 2f \n\t" - "movsw \n\t" - "2:\ttestb $1,%b4 \n\t" - "je 3f \n\t" - "movsb \n\t" - "3: \n\t" - : "=&c"(d0), "=&D"(d1), "=&S"(d2) - : "0"(Num / 8), "q"(Num), "1"(dst), "2"(src) - : "memory"); - return dst; -} - -/** - * @brief 分割字符串 - * - * @param str 要被分解成一组小字符串的字符串 - * @param delim 包含分隔符的字符串 - * @return 分割结果 - */ -char *strtok(char *str, const char *delim); - -/** - * @brief 分割字符串 - * - * @param str 要被分解成一组小字符串的字符串 - * @param delim 包含分隔符的字符串 - * @param saveptr 用于存储当前操作的字符串 - * @return 分割结果 - */ -char *strtok_r(char *str, const char *delim, char **saveptr); - -//! 以下函数没有经过检验,不确保正常工作 - -/** - * @brief 检索字符串 str1 中第一个不在字符串 str2 中出现的字符下标 - * - * @param str1 被检索的字符串 - * @param str2 进行匹配的字符列表 - * @return str1 中第一个不在字符串 str2 中出现的字符下标 - */ -size_t strspn(const char *str1, const char *str2); - -/** - * @brief 检索字符串 str1 开头连续有几个字符都不含字符串 str2 中的字符 - * - * @param str1 被检索的字符串 - * @param str2 进行匹配的字符列表 - * @return str1 开头连续都不含字符串 str2 中字符的字符数 - */ -size_t strcspn(const char *str1, const char *str2); - -/** - * @brief 检索字符串 str1 中第一个匹配字符串 str2 中字符的字符 - * - * @param str1 被检索的字符串 - * @param str2 进行匹配的字符列表 - * @return str1 中第一个匹配字符串 str2 中字符的指针,如果未找到字符则返回 NULL - */ -char *strpbrk(const char *str1, const char *str2); - -/** - * @brief 在字符串中查找第一次出现的字符 - * - * @param str 被查找的字符串 - * @param c 要查找的字符 - * @return 指向找到的字符的指针,如果未找到该字符则返回 NULL - */ -char *strchr(const char *str, int c); - -/** - * @brief 在字符串中查找最后一次出现的字符 - * - * @param str 被查找的字符串 - * @param c 要查找的字符 - * @return 指向找到的字符的指针,如果未找到该字符则返回 NULL - */ -char *strrchr(const char *str, int c); - -#if defined(__cplusplus) -} /* extern "C" */ -#endif diff --git a/user/libs/libc/src/include/export/sys/stat.h b/user/libs/libc/src/include/export/sys/stat.h deleted file mode 100644 index 452a4e54..00000000 --- a/user/libs/libc/src/include/export/sys/stat.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once -#include - -/** - * @brief 系统内存信息结构体(单位:kB) - * - */ -struct mstat_t -{ - uint64_t total; // 计算机的总内存数量大小 - uint64_t used; // 已使用的内存大小 - uint64_t free; // 空闲物理页所占的内存大小 - uint64_t shared; // 共享的内存大小 - uint64_t cache_used; // 位于slab缓冲区中的已使用的内存大小 - uint64_t cache_free; // 位于slab缓冲区中的空闲的内存大小 - uint64_t available; // 系统总空闲内存大小(包括kmalloc缓冲区) -}; - -int mkdir(const char *path, mode_t mode); - -/** - * @brief 获取系统的内存信息 - * - * @param stat 传入的内存信息结构体 - * @return int 错误码 - */ -int mstat(struct mstat_t* stat); -int pipe(int *fd); \ No newline at end of file diff --git a/user/libs/libc/src/include/export/sys/types.h b/user/libs/libc/src/include/export/sys/types.h deleted file mode 100644 index 599deb64..00000000 --- a/user/libs/libc/src/include/export/sys/types.h +++ /dev/null @@ -1,83 +0,0 @@ -#pragma once - -#include -#include - -typedef unsigned char u_char; -typedef unsigned short u_short; -typedef unsigned int u_int; -typedef unsigned long u_long; - -typedef uint32_t uid_t; -typedef uint32_t gid_t; -typedef long long ssize_t; - -typedef int64_t pid_t; -typedef __SIZE_TYPE__ size_t; - -typedef char *caddr_t; - -typedef int id_t; - -typedef uint64_t ino_t; -typedef int64_t off_t; - -typedef uint32_t blkcnt_t; -typedef uint32_t blksize_t; -typedef uint32_t dev_t; -typedef uint16_t mode_t; -typedef uint32_t nlink_t; - -typedef int64_t time_t; -typedef uint32_t useconds_t; -typedef int32_t suseconds_t; -typedef uint32_t clock_t; - -typedef uint64_t fsblkcnt_t; -typedef uint64_t fsfilcnt_t; - -#define __socklen_t_defined -#define __socklen_t uint32_t -typedef __socklen_t socklen_t; - -struct utimbuf -{ - time_t actime; - time_t modtime; -}; - -typedef int pthread_t; -typedef int pthread_key_t; -typedef uint32_t pthread_once_t; - -typedef struct __pthread_mutex_t -{ - uint32_t lock; - pthread_t owner; - int level; - int type; -} pthread_mutex_t; - -typedef void *pthread_attr_t; -typedef struct __pthread_mutexattr_t -{ - int type; -} pthread_mutexattr_t; - -typedef struct __pthread_cond_t -{ - pthread_mutex_t *mutex; - uint32_t value; - int clockid; // clockid_t -} pthread_cond_t; - -typedef uint64_t pthread_rwlock_t; -typedef void *pthread_rwlockattr_t; -typedef struct __pthread_spinlock_t -{ - int m_lock; -} pthread_spinlock_t; -typedef struct __pthread_condattr_t -{ - int clockid; // clockid_t -} pthread_condattr_t; \ No newline at end of file diff --git a/user/libs/libc/src/include/export/sys/wait.h b/user/libs/libc/src/include/export/sys/wait.h deleted file mode 100644 index 737441b2..00000000 --- a/user/libs/libc/src/include/export/sys/wait.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#include - -/** - * @brief 等待所有子进程退出 - * - * @param stat_loc 返回的子进程结束状态 - * @return pid_t - */ -pid_t wait(int *stat_loc); - -/** - * @brief 等待指定pid的子进程退出 - * - * @param pid 子进程的pid - * @param stat_loc 返回的子进程结束状态 - * @param options 额外的控制选项 - * @return pid_t - */ -pid_t waitpid(pid_t pid, int *stat_loc, int options); diff --git a/user/libs/libc/src/include/export/time.h b/user/libs/libc/src/include/export/time.h deleted file mode 100644 index d741f327..00000000 --- a/user/libs/libc/src/include/export/time.h +++ /dev/null @@ -1,53 +0,0 @@ -#pragma once - -#include - -#if defined(__cplusplus) -extern "C" { -#endif - -// 操作系统定义时间以ns为单位 -#define CLOCKS_PER_SEC 1000000 - -struct tm -{ - int tm_sec; /* Seconds. [0-60] (1 leap second) */ - int tm_min; /* Minutes. [0-59] */ - int tm_hour; /* Hours. [0-23] */ - int tm_mday; /* Day. [1-31] */ - int tm_mon; /* Month. [0-11] */ - int tm_year; /* Year - 1900. */ - int tm_wday; /* Day of week. [0-6] */ - int tm_yday; /* Days in year.[0-365] */ - int tm_isdst; /* DST. [-1/0/1]*/ - - long int __tm_gmtoff; /* Seconds east of UTC. */ - const char *__tm_zone; /* Timezone abbreviation. */ -}; - - -struct timespec -{ - long int tv_sec; // 秒 - long int tv_nsec; // 纳秒 -}; - -/** - * @brief 休眠指定时间 - * - * @param rqtp 指定休眠的时间 - * @param rmtp 返回的剩余休眠时间 - * @return int - */ -int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); - -/** - * @brief 获取系统当前cpu时间 - * - * @return clock_t - */ -clock_t clock(); - -#if defined(__cplusplus) -} /* extern "C" */ -#endif diff --git a/user/libs/libc/src/include/export/unistd.h b/user/libs/libc/src/include/export/unistd.h deleted file mode 100644 index 24560692..00000000 --- a/user/libs/libc/src/include/export/unistd.h +++ /dev/null @@ -1,138 +0,0 @@ -#pragma once -#include -#include - -#if defined(__cplusplus) -extern "C" { -#endif - -/** - * @brief 关闭文件接口 - * - * @param fd 文件描述符 - * @return int - */ -int close(int fd); - -/** - * @brief 从文件读取数据的接口 - * - * @param fd 文件描述符 - * @param buf 缓冲区 - * @param count 待读取数据的字节数 - * @return ssize_t 成功读取的字节数 - */ -ssize_t read(int fd, void *buf, size_t count); - -/** - * @brief 向文件写入数据的接口 - * - * @param fd 文件描述符 - * @param buf 缓冲区 - * @param count 待写入数据的字节数 - * @return ssize_t 成功写入的字节数 - */ -ssize_t write(int fd, void const *buf, size_t count); - -/** - * @brief 调整文件的访问位置 - * - * @param fd 文件描述符号 - * @param offset 偏移量 - * @param whence 调整模式 - * @return uint64_t 调整结束后的文件访问位置 - */ -off_t lseek(int fd, off_t offset, int whence); - -/** - * @brief fork当前进程 - * - * @return pid_t - */ -pid_t fork(void); - -/** - * @brief fork当前进程,但是与父进程共享VM、flags、fd - * - * @return pid_t - */ -pid_t vfork(void); - -/** - * @brief 将堆内存调整为end_brk - * - * @param end_brk 新的堆区域的结束地址 - * end_brk=-1 ===> 返回堆区域的起始地址 - * end_brk=-2 ===> 返回堆区域的结束地址 - * @return uint64_t 错误码 - * - */ -uint64_t brk(uint64_t end_brk); - -/** - * @brief 将堆内存空间加上offset(注意,该系统调用只应在普通进程中调用,而不能是内核线程) - * - * @param increment offset偏移量 - * @return uint64_t the previous program break - */ -void *sbrk(int64_t increment); - -/** - * @brief 切换当前工作目录 - * - * @param dest_path 目标目录 - * @return int64_t 成功:0,失败:负值(错误码) - */ -int64_t chdir(char *dest_path); - -/** - * @brief 执行新的程序 - * - * @param path 文件路径 - * @param argv 参数列表 - * @return int - */ -int execv(const char *path, char *const argv[]); - -/** - * @brief 睡眠指定时间 - * - * @param usec 微秒 - * @return int - */ -extern int usleep(useconds_t usec); - -/** - * @brief 删除文件夹 - * - * @param path 绝对路径 - * @return int 错误码 - */ -int rmdir(const char *path); - -int rm(const char * path); - -/** - * @brief 交换n字节 - * @param src 源地址 - * @param dest 目的地址 - * @param nbytes 交换字节数 - */ -void swab(void *restrict src, void *restrict dest, ssize_t nbytes); - -/** - * @brief 创建pipe - * @param fildes 分别是读端fildes[0]和写端fildes[1] - */ -int pipe(int fildes[2]); - -pid_t getpid(void); - -int dup(int fd); - -int dup2(int ofd, int nfd); -char *getcwd(char* buf, size_t size); - -#if defined(__cplusplus) -} /* extern "C" */ -#endif diff --git a/user/libs/libc/src/include/internal/bindings/.gitignore b/user/libs/libc/src/include/internal/bindings/.gitignore deleted file mode 100644 index 84dc93d4..00000000 --- a/user/libs/libc/src/include/internal/bindings/.gitignore +++ /dev/null @@ -1 +0,0 @@ -bindings.rs \ No newline at end of file diff --git a/user/libs/libc/src/include/internal/bindings/mod.rs b/user/libs/libc/src/include/internal/bindings/mod.rs deleted file mode 100644 index 90c70dcc..00000000 --- a/user/libs/libc/src/include/internal/bindings/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod bindings; diff --git a/user/libs/libc/src/include/internal/bindings/wrapper.h b/user/libs/libc/src/include/internal/bindings/wrapper.h deleted file mode 100644 index 08c4d3db..00000000 --- a/user/libs/libc/src/include/internal/bindings/wrapper.h +++ /dev/null @@ -1,15 +0,0 @@ -/** - * @file wrapper.h - * @author longjin (longjin@RinGoTek.cn) - * @brief 这是为libc的C代码的相关接口创建rust绑定的wrapper - * @version 0.1 - * @date 2023-02-11 - * - * @copyright Copyright (c) 2023 - * - */ -#pragma once - -// 这里导出在include/export文件夹下的头文件 -#include -#include \ No newline at end of file diff --git a/user/libs/libc/src/include/internal/mod.rs b/user/libs/libc/src/include/internal/mod.rs deleted file mode 100644 index 90c70dcc..00000000 --- a/user/libs/libc/src/include/internal/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod bindings; diff --git a/user/libs/libc/src/include/mod.rs b/user/libs/libc/src/include/mod.rs deleted file mode 100644 index becabbd6..00000000 --- a/user/libs/libc/src/include/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod internal; diff --git a/user/libs/libc/src/lib.rs b/user/libs/libc/src/lib.rs deleted file mode 100644 index d7c44679..00000000 --- a/user/libs/libc/src/lib.rs +++ /dev/null @@ -1,28 +0,0 @@ -#![no_std] // <1> -#![no_main] // <1> -#![feature(core_intrinsics)] // <2> -#![feature(alloc_error_handler)] -#![feature(panic_info_message)] - -#[allow(non_upper_case_globals)] -#[allow(non_camel_case_types)] -#[allow(non_snake_case)] -use core::panic::PanicInfo; - -use include::internal::bindings::bindings::putchar; - -mod include; - -#[panic_handler] -fn panic(_info: &PanicInfo) -> ! { - loop {} -} - -#[no_mangle] -pub extern "C" fn scanf() { - loop { - unsafe { - putchar(88); - } - } -} diff --git a/user/libs/libc/src/libc_init.c b/user/libs/libc/src/libc_init.c deleted file mode 100644 index cc190ceb..00000000 --- a/user/libs/libc/src/libc_init.c +++ /dev/null @@ -1,18 +0,0 @@ -#include -#include - -FILE *stdin; -FILE *stdout; -FILE *stderr; - -void _libc_init() -{ - // 初始化标准流对应的文件描述符 - stdin = malloc(sizeof(FILE)); - stdout = malloc(sizeof(FILE)); - stderr = malloc(sizeof(FILE)); - - stdin->fd = 0; - stdout->fd = 1; - stderr->fd = 2; -} \ No newline at end of file diff --git a/user/libs/libc/src/malloc.c b/user/libs/libc/src/malloc.c deleted file mode 100644 index 3f38e4d4..00000000 --- a/user/libs/libc/src/malloc.c +++ /dev/null @@ -1,394 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#define PAGE_4K_SHIFT 12 -#define PAGE_2M_SHIFT 21 -#define PAGE_1G_SHIFT 30 -#define PAGE_GDT_SHIFT 39 - -// 不同大小的页的容量 -#define PAGE_4K_SIZE (1UL << PAGE_4K_SHIFT) -#define PAGE_2M_SIZE (1UL << PAGE_2M_SHIFT) -#define PAGE_1G_SIZE (1UL << PAGE_1G_SHIFT) - -// 屏蔽低于x的数值 -#define PAGE_4K_MASK (~(PAGE_4K_SIZE - 1)) -#define PAGE_2M_MASK (~(PAGE_2M_SIZE - 1)) - -// 将addr按照x的上边界对齐 -#define PAGE_4K_ALIGN(addr) (((unsigned long)(addr) + PAGE_4K_SIZE - 1) & PAGE_4K_MASK) -#define PAGE_2M_ALIGN(addr) (((unsigned long)(addr) + PAGE_2M_SIZE - 1) & PAGE_2M_MASK) - -#define ALIGN_UP16(x) (((x) + 15) & ~15) -#define PAGE_ALIGN_UP(x) (((x) + PAGE_4K_SIZE - 1) & PAGE_4K_MASK) - -/** - * @brief 显式链表的结点 - * - */ -typedef struct malloc_mem_chunk_t -{ - uint64_t length; // 整个块所占用的内存区域的大小 - struct malloc_mem_chunk_t *prev; // 上一个结点的指针 - struct malloc_mem_chunk_t *next; // 下一个结点的指针 -} malloc_mem_chunk_t; - -static uint64_t brk_base_addr = 0; // 堆区域的内存基地址 -static uint64_t brk_max_addr = 0; // 堆区域的内存最大地址 -static uint64_t brk_managed_addr = 0; // 堆区域已经被管理的地址 - -// 空闲链表 -// 按start_addr升序排序 -static malloc_mem_chunk_t *malloc_free_list = NULL; -static malloc_mem_chunk_t *malloc_free_list_end = NULL; // 空闲链表的末尾结点 - -static uint64_t count_last_free_size = 0; // 统计距离上一次回收内存,已经free了多少内存 - -/** - * @brief 将块插入空闲链表 - * - * @param ck 待插入的块 - */ -static void malloc_insert_free_list(malloc_mem_chunk_t *ck); - -/** - * @brief 当堆顶空闲空间大于2个页的空间的时候,释放1个页 - * - */ -static void release_brk(); - -/** - * @brief 在链表中检索符合要求的空闲块(best fit) - * - * @param size 块的大小 - * @return malloc_mem_chunk_t* - */ -static malloc_mem_chunk_t *malloc_query_free_chunk_bf(uint64_t size) -{ - // 在满足best fit的前提下,尽可能的使分配的内存在低地址 - // 使得总的堆内存可以更快被释放 - - if (malloc_free_list == NULL) - { - return NULL; - } - malloc_mem_chunk_t *ptr = malloc_free_list; - malloc_mem_chunk_t *best = NULL; - // printf("query size=%d", size); - while (ptr != NULL) - { - // printf("ptr->length=%#010lx\n", ptr->length); - if (ptr->length == size) - { - best = ptr; - break; - } - - if (ptr->length > size) - { - if (best == NULL) - best = ptr; - else if (best->length > ptr->length) - best = ptr; - } - ptr = ptr->next; - } - - return best; -} - -/** - * @brief 在链表中检索符合要求的空闲块(first fit) - * - * @param size - * @return malloc_mem_chunk_t* - */ -static malloc_mem_chunk_t *malloc_query_free_chunk_ff(uint64_t size) -{ - if (malloc_free_list == NULL) - return NULL; - malloc_mem_chunk_t *ptr = malloc_free_list; - - while (ptr) - { - if (ptr->length >= size) - { - return ptr; - } - ptr = ptr->next; - } - - return NULL; -} - -/** - * @brief 扩容malloc管理的内存区域 - * - * @param size 扩大的内存大小 - */ -static int malloc_enlarge(int64_t size) -{ - if (brk_base_addr == 0) // 第一次调用,需要初始化 - { - brk_base_addr = sbrk(0); - // printf("brk_base_addr=%#018lx\n", brk_base_addr); - brk_managed_addr = brk_base_addr; - brk_max_addr = sbrk(0); - } - - int64_t free_space = brk_max_addr - brk_managed_addr; - // printf("size=%ld\tfree_space=%ld\n", size, free_space); - if (free_space < size) // 现有堆空间不足 - { - if (sbrk(PAGE_ALIGN_UP(size - free_space)) != (void *)(-1)) - brk_max_addr = sbrk((0)); - else - { - put_string("malloc_enlarge(): no_mem\n", COLOR_YELLOW, COLOR_BLACK); - return -ENOMEM; - } - - // printf("brk max addr = %#018lx\n", brk_max_addr); - } - - // 扩展管理的堆空间 - // 在新分配的内存的底部放置header - malloc_mem_chunk_t *new_ck = (malloc_mem_chunk_t *)brk_managed_addr; - memset(new_ck, 0, sizeof(malloc_mem_chunk_t)); - new_ck->length = brk_max_addr - brk_managed_addr; - // printf("new_ck->start_addr=%#018lx\tbrk_max_addr=%#018lx\tbrk_managed_addr=%#018lx\n", (uint64_t)new_ck, brk_max_addr, brk_managed_addr); - new_ck->prev = NULL; - new_ck->next = NULL; - brk_managed_addr = brk_max_addr; - - malloc_insert_free_list(new_ck); - - return 0; -} - -/** - * @brief 合并空闲块 - * - */ -static void malloc_merge_free_chunk() -{ - if (malloc_free_list == NULL) - return; - malloc_mem_chunk_t *ptr = malloc_free_list->next; - while (ptr != NULL) - { - // 内存块连续 - if (((uint64_t)(ptr->prev) + ptr->prev->length == (uint64_t)ptr)) - { - // printf("merged %#018lx and %#018lx\n", (uint64_t)ptr, (uint64_t)(ptr->prev)); - // 将ptr与前面的空闲块合并 - ptr->prev->length += ptr->length; - ptr->prev->next = ptr->next; - if (ptr->next == NULL) - malloc_free_list_end = ptr->prev; - else - ptr->next->prev = ptr->prev; - // 由于内存组成结构的原因,不需要free掉header - ptr = ptr->prev; - } - ptr = ptr->next; - } -} - -/** - * @brief 将块插入空闲链表 - * - * @param ck 待插入的块 - */ -static void malloc_insert_free_list(malloc_mem_chunk_t *ck) -{ - if (malloc_free_list == NULL) // 空闲链表为空 - { - malloc_free_list = ck; - malloc_free_list_end = ck; - ck->prev = ck->next = NULL; - return; - } - else - { - - malloc_mem_chunk_t *ptr = malloc_free_list; - while (ptr != NULL) - { - if ((uint64_t)ptr < (uint64_t)ck) - { - if (ptr->next == NULL) // 当前是最后一个项 - { - ptr->next = ck; - ck->next = NULL; - ck->prev = ptr; - malloc_free_list_end = ck; - break; - } - else if ((uint64_t)(ptr->next) > (uint64_t)ck) - { - ck->prev = ptr; - ck->next = ptr->next; - ptr->next = ck; - ck->next->prev = ck; - break; - } - } - else // 在ptr之前插入 - { - - if (ptr->prev == NULL) // 是第一个项 - { - malloc_free_list = ck; - ck->prev = NULL; - ck->next = ptr; - ptr->prev = ck; - break; - } - else - { - ck->prev = ptr->prev; - ck->next = ptr; - ck->prev->next = ck; - ptr->prev = ck; - break; - } - } - ptr = ptr->next; - } - } -} - -/** - * @brief 获取一块堆内存 - * - * @param size 内存大小 - * @return void* 内存空间的指针 - * - * 分配内存的时候,结点的prev next指针所占用的空间被当做空闲空间分配出去 - */ -void *malloc(ssize_t size) -{ - // printf("malloc\n"); - // 计算需要分配的块的大小 - if (size + sizeof(uint64_t) <= sizeof(malloc_mem_chunk_t)) - size = sizeof(malloc_mem_chunk_t); - else - size += sizeof(uint64_t); - - // 采用best fit - malloc_mem_chunk_t *ck = malloc_query_free_chunk_bf(size); - - if (ck == NULL) // 没有空闲块 - { - - // printf("no free blocks\n"); - // 尝试合并空闲块 - malloc_merge_free_chunk(); - ck = malloc_query_free_chunk_bf(size); - - // 找到了合适的块 - if (ck) - goto found; - - // printf("before enlarge\n"); - // 找不到合适的块,扩容堆区域 - if (malloc_enlarge(size) == -ENOMEM) - return (void *)-ENOMEM; // 内存不足 - - malloc_merge_free_chunk(); // 扩容后运行合并,否则会导致碎片 - - // 扩容后再次尝试获取 - - ck = malloc_query_free_chunk_bf(size); - } -found:; - - // printf("ck = %#018lx\n", (uint64_t)ck); - if (ck == NULL) - return (void *)-ENOMEM; - // printf("ck->prev=%#018lx ck->next=%#018lx\n", ck->prev, ck->next); - // 分配空闲块 - // 从空闲链表取出 - if (ck->prev == NULL) // 当前是链表的第一个块 - { - malloc_free_list = ck->next; - } - else - ck->prev->next = ck->next; - - if (ck->next != NULL) // 当前不是最后一个块 - ck->next->prev = ck->prev; - else - malloc_free_list_end = ck->prev; - - // 当前块剩余的空间还能容纳多一个结点的空间,则分裂当前块 - if ((int64_t)(ck->length) - size > sizeof(malloc_mem_chunk_t)) - { - // printf("seperate\n"); - malloc_mem_chunk_t *new_ck = (malloc_mem_chunk_t *)(((uint64_t)ck) + size); - new_ck->length = ck->length - size; - new_ck->prev = new_ck->next = NULL; - // printf("new_ck=%#018lx, new_ck->length=%#010lx\n", (uint64_t)new_ck, new_ck->length); - ck->length = size; - malloc_insert_free_list(new_ck); - } - // printf("malloc done: %#018lx, length=%#018lx\n", ((uint64_t)ck + sizeof(uint64_t)), ck->length); - // 此时链表结点的指针的空间被分配出去 - return (void *)((uint64_t)ck + sizeof(uint64_t)); -} - -/** - * @brief 当堆顶空闲空间大于2个页的空间的时候,释放1个页 - * - */ -static void release_brk() -{ - // 先检测最顶上的块 - // 由于块按照开始地址排列,因此找最后一个块 - if (malloc_free_list_end == NULL) - { - printf("release(): free list end is null. \n"); - return; - } - if ((uint64_t)malloc_free_list_end + malloc_free_list_end->length == brk_max_addr && (uint64_t)malloc_free_list_end <= brk_max_addr - (PAGE_2M_SIZE << 1)) - { - int64_t delta = ((brk_max_addr - (uint64_t)malloc_free_list_end) & PAGE_2M_MASK) - PAGE_2M_SIZE; - // printf("(brk_max_addr - (uint64_t)malloc_free_list_end) & PAGE_2M_MASK=%#018lx\n ", (brk_max_addr - (uint64_t)malloc_free_list_end) & PAGE_2M_MASK); - // printf("PAGE_2M_SIZE=%#018lx\n", PAGE_2M_SIZE); - // printf("tdfghgbdfggkmfn=%#018lx\n ", (brk_max_addr - (uint64_t)malloc_free_list_end) & PAGE_2M_MASK - PAGE_2M_SIZE); - // printf("delta=%#018lx\n ", delta); - if (delta <= 0) // 不用释放内存 - return; - sbrk(-delta); - brk_max_addr = sbrk(0); - brk_managed_addr = brk_max_addr; - - malloc_free_list_end->length = brk_max_addr - (uint64_t)malloc_free_list_end; - } -} -/** - * @brief 释放一块堆内存 - * - * @param ptr 堆内存的指针 - */ -void free(void *ptr) -{ - // 找到结点(此时prev和next都处于未初始化的状态) - malloc_mem_chunk_t *ck = (malloc_mem_chunk_t *)((uint64_t)ptr - sizeof(uint64_t)); - // printf("free(): addr = %#018lx\t len=%#018lx\n", (uint64_t)ck, ck->length); - count_last_free_size += ck->length; - - malloc_insert_free_list(ck); - - if (count_last_free_size > PAGE_2M_SIZE) - { - count_last_free_size = 0; - malloc_merge_free_chunk(); - release_brk(); - } -} diff --git a/user/libs/libc/src/math/Makefile b/user/libs/libc/src/math/Makefile deleted file mode 100644 index c70df022..00000000 --- a/user/libs/libc/src/math/Makefile +++ /dev/null @@ -1,14 +0,0 @@ - - -CFLAGS += -I . - -all: fabs.o round.o pow.o - -fabs.o: fabs.c - $(CC) $(CFLAGS) -c fabs.c -o fabs.o - -round.o: round.c - $(CC) $(CFLAGS) -c round.c -o round.o - -pow.o: pow.c - $(CC) $(CFLAGS) -c pow.c -o pow.o \ No newline at end of file diff --git a/user/libs/libc/src/math/fabs.c b/user/libs/libc/src/math/fabs.c deleted file mode 100644 index 53b71306..00000000 --- a/user/libs/libc/src/math/fabs.c +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include -#include - -double fabs(double x) -{ - union - { - double f; - uint64_t i; - } u = {x}; - u.i &= -1ULL / 2; - return u.f; -} - - -#if __LDBL_MANT_DIG__ == 53 && __LDBL_MAX_EXP__ == 1024 -long double fabsl(long double x) -{ - return fabs(x); -} -#elif (__LDBL_MANT_DIG__ == 64 || __LDBL_MANT_DIG__ == 113) && __LDBL_MAX_EXP__ == 16384 -long double fabsl(long double x) -{ - union ldshape u = {x}; - - u.i.se &= 0x7fff; - return u.f; -} -#endif \ No newline at end of file diff --git a/user/libs/libc/src/math/pow.c b/user/libs/libc/src/math/pow.c deleted file mode 100644 index b42d3d22..00000000 --- a/user/libs/libc/src/math/pow.c +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include - -int64_t pow(int64_t x, int y) -{ - int64_t res = 1; - for (int i = 0; i < y; ++i) - res *= x; - return res; -} \ No newline at end of file diff --git a/user/libs/libc/src/math/round.c b/user/libs/libc/src/math/round.c deleted file mode 100644 index 2642b6ee..00000000 --- a/user/libs/libc/src/math/round.c +++ /dev/null @@ -1,41 +0,0 @@ -#include - -#if __FLT_EVAL_METHOD__ == 0 || __FLT_EVAL_METHOD__ == 1 -#define EPS __DBL_EPSILON__ -#elif __FLT_EVAL_METHOD__ == 2 -#define EPS __LDBL_EPSILON__ -#endif -static const double toint = 1 / EPS; - -double round(double x) -{ - union - { - double f; - uint64_t i; - } u = {x}; - - int e = u.i >> 52 & 0x7ff; - double y; - - if (e >= 0x3ff + 52) - return x; - if (u.i >> 63) - x = -x; - if (e < 0x3ff - 1) - { - /* raise inexact if x!=0 */ - FORCE_EVAL(x + toint); - return 0 * u.f; - } - y = x + toint - toint - x; - if (y > 0.5) - y = y + x - 1; - else if (y <= -0.5) - y = y + x + 1; - else - y = y + x; - if (u.i >> 63) - y = -y; - return y; -} \ No newline at end of file diff --git a/user/libs/libc/src/printf.c b/user/libs/libc/src/printf.c deleted file mode 100644 index 1e813bde..00000000 --- a/user/libs/libc/src/printf.c +++ /dev/null @@ -1,556 +0,0 @@ -#include - -#include -#include -#include -#include -#include -#include - -static char *write_num(char *str, uint64_t num, int base, int field_width, int precision, int flags); -static char *write_float_point_num(char *str, double num, int field_width, int precision, int flags); - -static int skip_and_atoi(const char **s) -{ - /** - * @brief 获取连续的一段字符对应整数的值 - * @param:**s 指向 指向字符串的指针 的指针 - */ - int ans = 0; - while (is_digit(**s)) - { - ans = ans * 10 + (**s) - '0'; - ++(*s); - } - return ans; -} - -/** - * @brief 往屏幕上输出字符串 - * - * @param str 字符串指针 - * @param front_color 前景色 - * @param bg_color 背景色 - * @return int64_t - */ -int64_t put_string(char *str, uint64_t front_color, uint64_t bg_color) -{ - return syscall_invoke(SYS_PUT_STRING, (uint64_t)str, front_color, bg_color, 0, 0, 0); -} - -int printf(const char *fmt, ...) -{ - char buf[4096]; - int count = 0; - va_list args; - va_start(args, fmt); - - count = vsprintf(buf, fmt, args); - va_end(args); - // put_string(buf, COLOR_WHITE, COLOR_BLACK); - write(1, buf, count); - return count; -} - -int sprintf(char *buf, const char *fmt, ...) -{ - int count = 0; - va_list args; - - va_start(args, fmt); - count = vsprintf(buf, fmt, args); - va_end(args); - - return count; -} - -/** - * 将字符串按照fmt和args中的内容进行格式化,然后保存到buf中 - * @param buf 结果缓冲区 - * @param fmt 格式化字符串 - * @param args 内容 - * @return 最终字符串的长度 - */ -int vsprintf(char *buf, const char *fmt, va_list args) -{ - // 当需要输出的字符串的指针为空时,使用该字符填充目标字符串的指针 - static const char __end_zero_char = '\0'; - - char *str = NULL, *s = NULL; - - str = buf; - - int flags; // 用来存储格式信息的bitmap - int field_width; // 区域宽度 - int precision; // 精度 - int qualifier; // 数据显示的类型 - int len; - - // 开始解析字符串 - for (; *fmt; ++fmt) - { - // 内容不涉及到格式化,直接输出 - if (*fmt != '%') - { - *str = *fmt; - ++str; - continue; - } - - // 开始格式化字符串 - - // 清空标志位和field宽度 - field_width = flags = 0; - - bool flag_tmp = true; - bool flag_break = false; - - ++fmt; - while (flag_tmp) - { - switch (*fmt) - { - case '\0': - // 结束解析 - flag_break = true; - flag_tmp = false; - break; - - case '-': - // 左对齐 - flags |= LEFT; - ++fmt; - break; - case '+': - // 在正数前面显示加号 - flags |= PLUS; - ++fmt; - break; - case ' ': - flags |= SPACE; - ++fmt; - break; - case '#': - // 在八进制数前面显示 '0o',在十六进制数前面显示 '0x' 或 '0X' - flags |= SPECIAL; - ++fmt; - break; - case '0': - // 显示的数字之前填充‘0’来取代空格 - flags |= PAD_ZERO; - ++fmt; - break; - default: - flag_tmp = false; - break; - } - } - if (flag_break) - break; - - // 获取区域宽度 - field_width = -1; - if (*fmt == '*') - { - field_width = va_arg(args, int); - ++fmt; - } - else if (is_digit(*fmt)) - { - field_width = skip_and_atoi(&fmt); - if (field_width < 0) - { - field_width = -field_width; - flags |= LEFT; - } - } - - // 获取小数精度 - precision = -1; - if (*fmt == '.') - { - ++fmt; - if (*fmt == '*') - { - precision = va_arg(args, int); - ++fmt; - } - else if is_digit (*fmt) - { - precision = skip_and_atoi(&fmt); - } - } - - // 获取要显示的数据的类型 - if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt == 'Z') - { - qualifier = *fmt; - ++fmt; - } - // 为了支持lld - if (qualifier == 'l' && *fmt == 'l', *(fmt + 1) == 'd') - ++fmt; - - // 转化成字符串 - long long *ip; - switch (*fmt) - { - // 输出 % - case '%': - *str++ = '%'; - - break; - // 显示一个字符 - case 'c': - // 靠右对齐 - if (!(flags & LEFT)) - { - while (--field_width > 0) - { - *str = ' '; - ++str; - } - } - - *str++ = (unsigned char)va_arg(args, int); - - while (--field_width > 0) - { - *str = ' '; - ++str; - } - - break; - - // 显示一个字符串 - case 's': - s = va_arg(args, char *); - if (!s) - s = &__end_zero_char; - len = strlen(s); - if (precision < 0) - { - // 未指定精度 - precision = len; - } - - else if (len > precision) - { - len = precision; - } - - // 靠右对齐 - if (!(flags & LEFT)) - while (len < field_width--) - { - *str = ' '; - ++str; - } - - for (int i = 0; i < len; i++) - { - *str = *s; - ++s; - ++str; - } - - while (len < field_width--) - { - *str = ' '; - ++str; - } - - break; - // 以八进制显示字符串 - case 'o': - flags |= SMALL; - case 'O': - flags |= SPECIAL; - if (qualifier == 'l') - str = write_num(str, va_arg(args, long long), 8, field_width, precision, flags); - else - str = write_num(str, va_arg(args, int), 8, field_width, precision, flags); - break; - - // 打印指针指向的地址 - case 'p': - if (field_width == 0) - { - field_width = 2 * sizeof(void *); - flags |= PAD_ZERO; - } - - str = write_num(str, (unsigned long)va_arg(args, void *), 16, field_width, precision, flags); - - break; - - // 打印十六进制 - case 'x': - flags |= SMALL; - case 'X': - // flags |= SPECIAL; - if (qualifier == 'l') - str = write_num(str, va_arg(args, int64_t), 16, field_width, precision, flags); - else - str = write_num(str, va_arg(args, int), 16, field_width, precision, flags); - break; - - // 打印十进制有符号整数 - case 'i': - case 'd': - - flags |= SIGN; - if (qualifier == 'l') - str = write_num(str, va_arg(args, long long), 10, field_width, precision, flags); - else - str = write_num(str, va_arg(args, int), 10, field_width, precision, flags); - break; - - // 打印十进制无符号整数 - case 'u': - if (qualifier == 'l') - str = write_num(str, va_arg(args, unsigned long long), 10, field_width, precision, flags); - else - str = write_num(str, va_arg(args, unsigned int), 10, field_width, precision, flags); - break; - - // 输出有效字符数量到*ip对应的变量 - case 'n': - - if (qualifier == 'l') - ip = va_arg(args, long long *); - else - ip = (int64_t *)va_arg(args, int *); - - *ip = str - buf; - break; - case 'f': - // 默认精度为3 - if (precision < 0) - precision = 3; - - str = write_float_point_num(str, va_arg(args, double), field_width, precision, flags); - - break; - - // 对于不识别的控制符,直接输出 - default: - *str++ = '%'; - if (*fmt) - *str++ = *fmt; - else - --fmt; - break; - } - } - *str = '\0'; - - // 返回缓冲区已有字符串的长度。 - return str - buf; -} - -static char *write_num(char *str, uint64_t num, int base, int field_width, int precision, int flags) -{ - /** - * @brief 将数字按照指定的要求转换成对应的字符串 - * - * @param str 要返回的字符串 - * @param num 要打印的数值 - * @param base 基数 - * @param field_width 区域宽度 - * @param precision 精度 - * @param flags 标志位 - */ - - // 首先判断是否支持该进制 - if (base < 2 || base > 36) - return 0; - char pad, sign, tmp_num[100]; - - const char *digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - // 显示小写字母 - if (flags & SMALL) - digits = "0123456789abcdefghijklmnopqrstuvwxyz"; - - if (flags & LEFT) - flags &= ~PAD_ZERO; - // 设置填充元素 - pad = (flags & PAD_ZERO) ? '0' : ' '; - - sign = 0; - - if (flags & SIGN) - { - int64_t signed_num = (int64_t)num; - if (signed_num < 0) - { - sign = '-'; - num = -signed_num; - } - else - num = signed_num; - } - else - { - // 设置符号 - sign = (flags & PLUS) ? '+' : ((flags & SPACE) ? ' ' : 0); - } - - // sign占用了一个宽度 - if (sign) - --field_width; - - if (flags & SPECIAL) - if (base == 16) // 0x占用2个位置 - field_width -= 2; - else if (base == 8) // O占用一个位置 - --field_width; - - int js_num = 0; // 临时数字字符串tmp_num的长度 - - if (num == 0) - tmp_num[js_num++] = '0'; - else - { - num = llabs(num); - // 进制转换 - while (num > 0) - { - tmp_num[js_num++] = digits[num % base]; // 注意这里,输出的数字,是小端对齐的。低位存低位 - num /= base; - } - } - - if (js_num > precision) - precision = js_num; - - field_width -= precision; - - // 靠右对齐 - if (!(flags & (LEFT + PAD_ZERO))) - while (field_width-- > 0) - *str++ = ' '; - - if (sign) - *str++ = sign; - if (flags & SPECIAL) - if (base == 16) - { - *str++ = '0'; - *str++ = digits[33]; - } - else if (base == 8) - *str++ = digits[24]; // 注意这里是英文字母O或者o - if (!(flags & LEFT)) - while (field_width-- > 0) - *str++ = pad; - while (js_num < precision) - { - --precision; - *str++ = '0'; - } - - while (js_num-- > 0) - *str++ = tmp_num[js_num]; - - while (field_width-- > 0) - *str++ = ' '; - - return str; -} - -static char *write_float_point_num(char *str, double num, int field_width, int precision, int flags) -{ - /** - * @brief 将浮点数按照指定的要求转换成对应的字符串 - * - * @param str 要返回的字符串 - * @param num 要打印的数值 - * @param field_width 区域宽度 - * @param precision 精度 - * @param flags 标志位 - */ - - char pad, sign, tmp_num_z[100], tmp_num_d[350]; - - const char *digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - // 显示小写字母 - if (flags & SMALL) - digits = "0123456789abcdefghijklmnopqrstuvwxyz"; - - // 设置填充元素 - pad = (flags & PAD_ZERO) ? '0' : ' '; - sign = 0; - if (flags & SIGN && num < 0) - { - sign = '-'; - num = -num; - } - else - { - // 设置符号 - sign = (flags & PLUS) ? '+' : ((flags & SPACE) ? ' ' : 0); - } - - // sign占用了一个宽度 - if (sign) - --field_width; - - int js_num_z = 0, js_num_d = 0; // 临时数字字符串tmp_num_z tmp_num_d的长度 - uint64_t num_z = (uint64_t)(num); // 获取整数部分 - uint64_t num_decimal = (uint64_t)(round(1.0 * (num - num_z) * pow(10, precision))); // 获取小数部分 - - if (num == 0 || num_z == 0) - tmp_num_z[js_num_z++] = '0'; - else - { - // 存储整数部分 - while (num_z > 0) - { - tmp_num_z[js_num_z++] = digits[num_z % 10]; // 注意这里,输出的数字,是小端对齐的。低位存低位 - num_z /= 10; - } - } - - while (num_decimal > 0) - { - tmp_num_d[js_num_d++] = digits[num_decimal % 10]; - num_decimal /= 10; - } - - field_width -= (precision + 1 + js_num_z); - - // 靠右对齐 - if (!(flags & LEFT)) - while (field_width-- > 0) - *str++ = pad; - - if (sign) - *str++ = sign; - - // 输出整数部分 - // while (js_num_z-- > 0) - // *str++ = tmp_num_z[js_num_z]; - while (js_num_z > 0) - { - *str++ = tmp_num_z[js_num_z - 1]; - --js_num_z; - } - *str++ = '.'; - - // 输出小数部分 - int total_dec_count = js_num_d; - for (int i = 0; i < precision && js_num_d-- > 0; ++i) - *str++ = tmp_num_d[js_num_d]; - - while (total_dec_count < precision) - { - ++total_dec_count; - *str++ = '0'; - } - - while (field_width-- > 0) - *str++ = ' '; - - return str; -} \ No newline at end of file diff --git a/user/libs/libc/src/signal.c b/user/libs/libc/src/signal.c deleted file mode 100644 index 1b6d7bf2..00000000 --- a/user/libs/libc/src/signal.c +++ /dev/null @@ -1,67 +0,0 @@ -#include -#include -#include -#include - -#pragma GCC push_options -#pragma GCC optimize("O0") -void __libc_sa_restorer() -{ - // 在这里发起sigreturn,请注意,由于内核需要读取到原来的do_signal时保存的栈帧,因此这里不能发生函数调用(会导致函数压栈),只能够这样来完成sigreturn - __asm__ __volatile__("int $0x80 \n\t" ::"a"(SYS_RT_SIGRETURN) : "memory"); -} -#pragma GCC pop_options - -/** - * @brief 设置信号处理动作(简单版本) - * - * @param signum - * @param handler - * @return int - */ -int signal(int signum, __sighandler_t handler) -{ - struct sigaction sa = {0}; - sa.sa_handler = handler; - // 由于DragonOS必须由用户程序指定一个sa_restorer,因此这里设置为libc的sa_restorer - sa.sa_restorer = &__libc_sa_restorer; - // printf("handler address: %#018lx\n", handler); - // printf("restorer address: %#018lx\n", &__libc_sa_restorer); - sigaction(signum, &sa, NULL); -} - -/** - * @brief 设置信号处理动作 - * - * @param signum 信号 - * @param act 处理动作(不可为NULL) - * @param oldact 返回的旧的处理动作(若为NULL,则不返回) - * @return int 错误码(遵循posix) - */ -int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact) -{ - return syscall_invoke(SYS_SIGACTION, (uint64_t)signum, (uint64_t)act, (uint64_t)oldact, 0, 0, 0); -} - -/** - * @brief 向当前进程发送一个信号 - * - * @param sig signal number - * @return int 错误码 - */ -int raise(int sig) -{ - return kill(getpid(), sig); -} - -/** - * @brief - * - * @param pid 进程的标识符 - * @param sig signal number - * @return int 错误码 - */ -int kill(pid_t pid, int sig) -{ - syscall_invoke(SYS_KILL, pid, sig, 0, 0, 0, 0); -} \ No newline at end of file diff --git a/user/libs/libc/src/stdio.c b/user/libs/libc/src/stdio.c deleted file mode 100644 index 001d07bc..00000000 --- a/user/libs/libc/src/stdio.c +++ /dev/null @@ -1,91 +0,0 @@ -#include -#include -#include -#include -#include - -int fprintf(FILE *restrict stream, const char *restrict format, ...) -{ - const int bufsize = 65536 * 2; - char *buf = malloc(bufsize); - memset(buf, 0, bufsize); - va_list args; - - va_start(args, format); - vsprintf(buf, format, args); - va_end(args); - - int len = strlen(buf); - if (len > bufsize - 1) - { - len = bufsize - 1; - buf[bufsize - 1] = 0; - } - write(stream->fd, buf, len); - free(buf); -} - -int getchar(void) -{ - unsigned int c; - read(0, &c, 1); - return c; -} - -int puts(const char *s) -{ - return put_string(s, COLOR_WHITE, COLOR_BLACK); -} - -int putchar(int c) -{ - return printf("%c", (char)c); -} - -int fflush(FILE *stream) -{ - return 0; -} - -int ferror(FILE *stream) -{ - return 0; -} - -int fclose(FILE *stream) -{ - int retval = close(stream->fd); - if (retval) - return retval; - if (stream->fd >= 3) - free(stream); - - return 0; -} - -// FIXME: -// 请注意,这个函数的实现,没有遵照posix,行为也与Linux的不一致,请在将来用Rust重构时改变它,以使得它的行为与Linux的一致。 -FILE *fopen(const char *restrict pathname, const char *restrict mode) -{ - FILE *stream = malloc(sizeof(FILE)); - memset(stream, 0, sizeof(FILE)); - int o_flags = 0; - - if (strcmp(mode, "r") == 0) - o_flags = O_RDONLY; - else if (strcmp(mode, "r+") == 0) - o_flags = O_RDWR; - else if (strcmp(mode, "w") == 0) - o_flags = O_WRONLY; - else if (strcmp(mode, "w+") == 0) - o_flags = O_RDWR | O_CREAT; - else if (strcmp(mode, "a") == 0) - o_flags = O_APPEND | O_CREAT; - else if (strcmp(mode, "a+") == 0) - o_flags = O_APPEND | O_CREAT; - - int fd = open(pathname, o_flags); - if (fd >= 0) - stream->fd = fd; - return stream; -} diff --git a/user/libs/libc/src/stdlib.c b/user/libs/libc/src/stdlib.c deleted file mode 100644 index acd3caf3..00000000 --- a/user/libs/libc/src/stdlib.c +++ /dev/null @@ -1,72 +0,0 @@ -#include -#include -#include -#include -#include - -extern void _fini(); - -int abs(int i) -{ - return i < 0 ? -i : i; -} - -long labs(long i) -{ - return i < 0 ? -i : i; -} - -long long llabs(long long i) -{ - return i < 0 ? -i : i; -} - -int atoi(const char *str) -{ - int n = 0, neg = 0; - - while (isspace(*str)) - { - str++; - } - - switch (*str) - { - case '-': - neg = 1; - break; - case '+': - str++; - break; - } - - /* Compute n as a negative number to avoid overflow on INT_MIN */ - while (isdigit(*str)) - { - n = 10 * n - (*str++ - '0'); - } - - return neg ? n : -n; -} - -/** - * @brief 退出进程 - * - * @param status - */ -void exit(int status) -{ - _fini(); - syscall_invoke(SYS_EXIT, status, 0, 0, 0, 0, 0); -} - -/** - * @brief 通过发送SIGABRT,从而退出当前进程 - * - */ -void abort() -{ - // step1:设置SIGABRT的处理函数为SIG_DFL - signal(SIGABRT, SIG_DFL); - raise(SIGABRT); -} \ No newline at end of file diff --git a/user/libs/libc/src/string.c b/user/libs/libc/src/string.c deleted file mode 100644 index 948295fc..00000000 --- a/user/libs/libc/src/string.c +++ /dev/null @@ -1,231 +0,0 @@ -#include -#include - -size_t strlen(const char *s) -{ - register int __res = 0; - while (s[__res] != '\0') - { - ++__res; - } - return __res; -} - -int strcmp(const char *FirstPart, const char *SecondPart) -{ - register int __res; - __asm__ __volatile__("cld \n\t" - "1: \n\t" - "lodsb \n\t" - "scasb \n\t" - "jne 2f \n\t" - "testb %%al, %%al \n\t" - "jne 1b \n\t" - "xorl %%eax, %%eax \n\t" - "jmp 3f \n\t" - "2: \n\t" - "movl $1, %%eax \n\t" - "jl 3f \n\t" - "negl %%eax \n\t" - "3: \n\t" - : "=a"(__res) - : "D"(FirstPart), "S"(SecondPart) - :); - return __res; -} - -void *memset(void *dst, unsigned char C, uint64_t size) -{ - - int d0, d1; - unsigned long tmp = C * 0x0101010101010101UL; - __asm__ __volatile__("cld \n\t" - "rep \n\t" - "stosq \n\t" - "testb $4, %b3 \n\t" - "je 1f \n\t" - "stosl \n\t" - "1:\ttestb $2, %b3 \n\t" - "je 2f\n\t" - "stosw \n\t" - "2:\ttestb $1, %b3 \n\t" - "je 3f \n\t" - "stosb \n\t" - "3: \n\t" - : "=&c"(d0), "=&D"(d1) - : "a"(tmp), "q"(size), "0"(size / 8), "1"(dst) - : "memory"); - return dst; -} - -char *strncpy(char *dst, const char *src, size_t count) -{ - __asm__ __volatile__("cld \n\t" - "1: \n\t" - "decq %2 \n\t" - "js 2f \n\t" - "lodsb \n\t" - "stosb \n\t" - "testb %%al, %%al \n\t" - "jne 1b \n\t" - "rep \n\t" - "stosb \n\t" - "2: \n\t" - : - : "S"(src), "D"(dst), "c"(count) - : "ax", "memory"); - return dst; -} - -char *strcat(char *dest, const char *src) -{ - strcpy(dest + strlen(dest), src); - return dest; -} - -char *strcpy(char *dst, const char *src) -{ - while (*src) - { - *(dst++) = *(src++); - } - *dst = 0; - - return dst; -} - -char *strtok(char *str, const char *delim) -{ - static char *saveptr; - return strtok_r(str, delim, &saveptr); -} - -char *strtok_r(char *str, const char *delim, char **saveptr) -{ - char *end; - if (str == NULL) - str = *saveptr; - if (*str == '\0') - { - *saveptr = str; - return NULL; - } - str += strspn(str, delim); - if (*str == '\0') - { - *saveptr = str; - return NULL; - } - end = str + strcspn(str, delim); - if (*end == '\0') - { - *saveptr = end; - return str; - } - *end = '\0'; - *saveptr = end + 1; - return str; -} - -size_t strspn(const char *str1, const char *str2) -{ - if (str1 == NULL || str2 == NULL) - return 0; - bool cset[256] = {0}; - while ((*str2) != '\0') - { - cset[*str2] = 1; - ++str2; - } - int index = 0; - while (str1[index] != '\0') - { - if (cset[str1[index]]) - index++; - else - break; - } - return index; -} - -size_t strcspn(const char *str1, const char *str2) -{ - if (str1 == NULL || str2 == NULL) - return 0; - bool cset[256] = {0}; - while ((*str2) != '\0') - { - cset[*str2] = 1; - ++str2; - } - int len = 0; - while (str1[len] != '\0') - { - if (!cset[str1[len]]) - len++; - else - break; - } - return len; -} - -char *strpbrk(const char *str1, const char *str2) -{ - typedef unsigned char uchar; - - if (str1 == NULL || str2 == NULL) - return NULL; - uchar cset[32] = {0}; - while ((*str2) != '\0') - { - uchar t = (uchar)*str2++; - cset[t % 32] |= 1 << (t / 32); - } - while ((*str1) != '\0') - { - uchar t = (uchar)*str1; - if (cset[t % 32] & (1 << (t / 32))) - { - return (char *)str1; - } - else - { - ++str1; - } - } - return NULL; -} - -char *strchr(const char *str, int c) -{ - if (str == NULL) - return NULL; - - while (*str != '\0') - { - if (*str == c) - { - return str; - } - str++; - } - return NULL; -} - -char *strrchr(const char *str, int c) -{ - if (str == NULL) - return NULL; - - char *p_char = NULL; - while (*str != '\0') - { - if (*str == (char)c) - { - p_char = (char *)str; - } - str++; - } - - return p_char; -} \ No newline at end of file diff --git a/user/libs/libc/src/sys/Makefile b/user/libs/libc/src/sys/Makefile deleted file mode 100644 index 97b44b8b..00000000 --- a/user/libs/libc/src/sys/Makefile +++ /dev/null @@ -1,11 +0,0 @@ - -all: wait.o stat.o - -CFLAGS += -I . - - -wait.o: wait.c - $(CC) $(CFLAGS) -c wait.c -o wait.o - -stat.o: stat.c - $(CC) $(CFLAGS) -c stat.c -o stat.o \ No newline at end of file diff --git a/user/libs/libc/src/sys/stat.c b/user/libs/libc/src/sys/stat.c deleted file mode 100644 index f804acde..00000000 --- a/user/libs/libc/src/sys/stat.c +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -int mkdir(const char *path, mode_t mode) -{ - return syscall_invoke(SYS_MKDIR, (uint64_t)path, (uint64_t)mode, 0, 0, 0, 0); -} - -/** - * @brief 获取系统的内存信息 - * - * @param stat 传入的内存信息结构体 - * @return int 错误码 - */ -int mstat(struct mstat_t *stat) -{ - char *buf = (char *)malloc(128); - memset(buf, 0, 128); - int fd = open("/proc/meminfo", O_RDONLY); - if (fd <= 0) - { - printf("ERROR: Cannot open file: /proc/meminfo, fd=%d\n", fd); - return -1; - } - read(fd, buf, 127); - close(fd); - char *str = strtok(buf, "\n\t"); - char *value = (char *)malloc(strlen(str) - 3); - int count = 0; - while (str != NULL) - { - // printf("%d: %s\n", count, str); - switch (count) - { - case 1: - strncpy(value, str, strlen(str) - 3); - stat->total = atoi(value); - break; - case 3: - strncpy(value, str, strlen(str) - 3); - stat->free = atoi(value); - break; - default: - break; - } - str = strtok(NULL, "\n\t"); - count++; - } - stat->used = stat->total - stat->free; - - free(buf); - free(value); - return 0; -} diff --git a/user/libs/libc/src/sys/wait.c b/user/libs/libc/src/sys/wait.c deleted file mode 100644 index 47df1bd2..00000000 --- a/user/libs/libc/src/sys/wait.c +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include - -/** - * @brief 等待所有子进程退出 - * - * @param stat_loc 返回的子进程结束状态 - * @return pid_t - */ -pid_t wait(int *stat_loc) -{ - return waitpid((pid_t)(-1), stat_loc, 0); -} - -/** - * @brief 等待指定pid的子进程退出 - * - * @param pid 子进程的pid - * @param stat_loc 返回的子进程结束状态 - * @param options 额外的控制选项 - * @return pid_t - */ -pid_t waitpid(pid_t pid, int *stat_loc, int options) -{ - return (pid_t)syscall_invoke(SYS_WAIT4, (uint64_t)pid, (uint64_t)stat_loc, options, 0, 0, 0); -} \ No newline at end of file diff --git a/user/libs/libc/src/time.c b/user/libs/libc/src/time.c deleted file mode 100644 index 5fe3d79c..00000000 --- a/user/libs/libc/src/time.c +++ /dev/null @@ -1,42 +0,0 @@ -#include -#include -#include -#include - -/** - * @brief 休眠指定时间 - * - * @param rqtp 指定休眠的时间 - * @param rmtp 返回的剩余休眠时间 - * @return int - */ -int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) -{ - return syscall_invoke(SYS_NANOSLEEP, (uint64_t)rqtp, (uint64_t)rmtp, 0, 0, 0, 0); -} - -/** - * @brief 睡眠指定时间 - * - * @param usec 微秒 - * @return int - */ -int usleep(useconds_t usec) -{ - struct timespec ts = { - tv_sec : (long int)(usec / 1000000), - tv_nsec : (long int)(usec % 1000000) * 1000UL - }; - - return nanosleep(&ts, NULL); -} - -/** - * @brief 获取系统当前cpu时间 - * - * @return clock_t - */ -clock_t clock() -{ - return (clock_t)syscall_invoke(SYS_CLOCK, 0, 0, 0, 0, 0, 0); -} \ No newline at end of file diff --git a/user/libs/libc/src/unistd.c b/user/libs/libc/src/unistd.c deleted file mode 100644 index a8304a89..00000000 --- a/user/libs/libc/src/unistd.c +++ /dev/null @@ -1,243 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -/** - * @brief 关闭文件接口 - * - * @param fd 文件描述符 - * @return int - */ -int close(int fd) -{ - return syscall_invoke(SYS_CLOSE, fd, 0, 0, 0, 0, 0); -} - -/** - * @brief 从文件读取数据的接口 - * - * @param fd 文件描述符 - * @param buf 缓冲区 - * @param count 待读取数据的字节数 - * @return ssize_t 成功读取的字节数 - */ -ssize_t read(int fd, void *buf, size_t count) -{ - return (ssize_t)syscall_invoke(SYS_READ, fd, (uint64_t)buf, count, 0, 0, 0); -} - -/** - * @brief 向文件写入数据的接口 - * - * @param fd 文件描述符 - * @param buf 缓冲区 - * @param count 待写入数据的字节数 - * @return ssize_t 成功写入的字节数 - */ -ssize_t write(int fd, void const *buf, size_t count) -{ - return (ssize_t)syscall_invoke(SYS_WRITE, fd, (uint64_t)buf, count, 0, 0, 0); -} - -/** - * @brief 调整文件的访问位置 - * - * @param fd 文件描述符号 - * @param offset 偏移量 - * @param whence 调整模式 - * @return uint64_t 调整结束后的文件访问位置 - */ -off_t lseek(int fd, off_t offset, int whence) -{ - return (off_t)syscall_invoke(SYS_LSEEK, fd, offset, whence, 0, 0, 0); -} - -/** - * @brief fork当前进程 - * - * @return pid_t - */ -pid_t fork(void) -{ - return (pid_t)syscall_invoke(SYS_FORK, 0, 0, 0, 0, 0, 0); -} -/** - * @brief 调用匿名管道 - * - * @return int 如果失败返回负数 - */ -int pipe(int fd[2]) -{ - return (int)syscall_invoke(SYS_PIPE, fd, 0, 0, 0, 0, 0); -} -/** - * @brief 调用带参数的匿名管道 - * - * @return int 如果失败返回负数 - */ -int pipe2(int fd[2], int flags) -{ - return (int)syscall_invoke(SYS_PIPE, fd, flags, 0, 0, 0, 0); -} -/** - * @brief fork当前进程,但是与父进程共享VM、flags、fd - * - * @return pid_t - */ -pid_t vfork(void) -{ - return (pid_t)syscall_invoke(SYS_VFORK, 0, 0, 0, 0, 0, 0); -} - -/** - * @brief 将堆内存调整为end_brk - * - * @param end_brk 新的堆区域的结束地址 - * end_brk=-1 ===> 返回堆区域的起始地址 - * end_brk=-2 ===> 返回堆区域的结束地址 - * @return uint64_t 错误码 - * - */ -uint64_t brk(uint64_t end_brk) -{ - uint64_t x = (uint64_t)syscall_invoke(SYS_BRK, (uint64_t)end_brk, 0, 0, 0, 0, 0); - if (x < end_brk) - { - errno = -ENOMEM; - return -1; - } - return 0; -} - -/** - * @brief 将堆内存空间加上offset(注意,该系统调用只应在普通进程中调用,而不能是内核线程) - * - * @param increment offset偏移量 - * @return uint64_t the previous program break - */ -void *sbrk(int64_t increment) -{ - void *retval = (void *)syscall_invoke(SYS_SBRK, (uint64_t)increment, 0, 0, 0, 0, 0); - if (retval == (void *)-ENOMEM) - return (void *)(-1); - else - { - errno = 0; - return (void *)retval; - } -} - -/** - * @brief 切换当前工作目录 - * - * @param dest_path 目标目录 - * @return int64_t 成功:0,失败:负值(错误码) - */ -int64_t chdir(char *dest_path) -{ - if (dest_path == NULL) - { - errno = -EFAULT; - return -1; - } - else - { - return syscall_invoke(SYS_CHDIR, (uint64_t)dest_path, 0, 0, 0, 0, 0); - } -} - -/** - * @brief 执行新的程序 - * - * @param path 文件路径 - * @param argv 参数列表 - * @return int - */ -int execv(const char *path, char *const argv[]) -{ - if (path == NULL) - { - errno = -ENOENT; - return -1; - } - - int retval = syscall_invoke(SYS_EXECVE, (uint64_t)path, (uint64_t)argv, 0, 0, 0, 0); - if (retval != 0) - return -1; - else - return 0; -} - -/** - * @brief 删除文件夹 - * - * @param path 绝对路径 - * @return int 错误码 - */ -int rmdir(const char *path) -{ - return syscall_invoke(SYS_UNLINK_AT, 0, (uint64_t)path, AT_REMOVEDIR, 0, 0, 0); -} - -/** - * @brief 删除文件 - * - * @param path 绝对路径 - * @return int - */ -int rm(const char *path) -{ - return syscall_invoke(SYS_UNLINK_AT, 0, (uint64_t)path, 0, 0, 0, 0); -} - -/** - * @brief 交换n字节 - * @param src 源地址 - * @param dest 目的地址 - * @param nbytes 交换字节数 - */ -void swab(void *restrict src, void *restrict dest, ssize_t nbytes) -{ - unsigned char buf[32]; - char *_src = src; - char *_dest = dest; - uint32_t transfer; - for (; nbytes > 0; nbytes -= transfer) - { - transfer = (nbytes > 32) ? 32 : nbytes; - memcpy(buf, _src, transfer); - memcpy(_src, _dest, transfer); - memcpy(_dest, buf, transfer); - _src += transfer; - _dest += transfer; - } -} - -/** - * @brief 获取当前进程的pid(进程标识符) - * - * @return pid_t 当前进程的pid - */ -pid_t getpid(void) -{ - return syscall_invoke(SYS_GETPID, 0, 0, 0, 0, 0, 0); -} - -int dup(int fd) -{ - return syscall_invoke(SYS_DUP, fd, 0, 0, 0, 0, 0); -} - -int dup2(int ofd, int nfd) -{ - return syscall_invoke(SYS_DUP2, ofd, nfd, 0, 0, 0, 0); -} - -char *getcwd(char *buf, size_t size) -{ - return syscall_invoke(SYS_GETCWD, buf, size, 0, 0, 0, 0); -} \ No newline at end of file diff --git a/user/libs/libsystem/Makefile b/user/libs/libsystem/Makefile deleted file mode 100644 index f708200d..00000000 --- a/user/libs/libsystem/Makefile +++ /dev/null @@ -1,8 +0,0 @@ -all: libsystem -# echo $(shell pwd) - - -libsystem: syscall.o - -syscall.o: syscall.c - $(CC) $(CFLAGS) -c syscall.c -o syscall.o diff --git a/user/libs/libsystem/syscall.c b/user/libs/libsystem/syscall.c deleted file mode 100644 index 19b8eeca..00000000 --- a/user/libs/libsystem/syscall.c +++ /dev/null @@ -1,23 +0,0 @@ -#include "syscall.h" -#include -#include -long syscall_invoke(uint64_t syscall_id, uint64_t arg0, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5) -{ - uint64_t __err_code; - __asm__ __volatile__( - "movq %2, %%rdi \n\t" - "movq %3, %%rsi \n\t" - "movq %4, %%rdx \n\t" - "movq %5, %%r10 \n\t" - "movq %6, %%r8 \n\t" - "movq %7, %%r9 \n\t" - "int $0x80 \n\t" - "movq %%rax, %0 \n\t" - :"=a"(__err_code) - : "a"(syscall_id), "m"(arg0), "m"(arg1), "m"(arg2), "m"(arg3), "m"(arg4), "m"(arg5) - : "memory", "r8", "r9", "r10", "r11", "rcx", "rdx", "rdi", "rsi"); - // printf("errcode = %#018lx\n", __err_code); - errno = __err_code; - - return __err_code; -} diff --git a/user/libs/libsystem/syscall.h b/user/libs/libsystem/syscall.h deleted file mode 100644 index 5bd77c07..00000000 --- a/user/libs/libsystem/syscall.h +++ /dev/null @@ -1,103 +0,0 @@ -#pragma once - -#include - -// 定义系统调用号 -#define SYS_READ 0 -#define SYS_WRITE 1 -#define SYS_OPEN 2 -#define SYS_CLOSE 3 - -#define SYS_FSTAT 5 -#define SYS_LSEEK 8 -#define SYS_MMAP 9 -#define SYS_MPROTECT 10 -#define SYS_MUNMAP 11 -#define SYS_BRK 12 -#define SYS_SIGACTION 13 - -#define SYS_RT_SIGRETURN 15 -#define SYS_IOCTL 16 - -#define SYS_DUP 32 -#define SYS_DUP2 33 - -#define SYS_NANOSLEEP 35 - -#define SYS_GETPID 39 - -#define SYS_SOCKET 41 -#define SYS_CONNECT 42 -#define SYS_ACCEPT 43 -#define SYS_SENDTO 44 -#define SYS_RECVFROM 45 - -#define SYS_RECVMSG 47 -#define SYS_SHUTDOWN 48 -#define SYS_BIND 49 -#define SYS_LISTEN 50 -#define SYS_GETSOCKNAME 51 -#define SYS_GETPEERNAME 52 - -#define SYS_SETSOCKOPT 54 -#define SYS_GETSOCKOPT 55 -#define SYS_CLONE 56 -#define SYS_FORK 57 -#define SYS_VFORK 58 -#define SYS_EXECVE 59 -#define SYS_EXIT 60 -#define SYS_WAIT4 61 -#define SYS_KILL 62 - -#define SYS_FCNTL 72 - -#define SYS_FTRUNCATE 77 -#define SYS_GET_DENTS 78 - -#define SYS_GETCWD 79 - -#define SYS_CHDIR 80 - -#define SYS_MKDIR 83 - -#define SYS_GETTIMEOFDAY 96 - -#define SYS_ARCH_PRCTL 158 - -#define SYS_REBOOT 169 - -#define SYS_GETPPID 110 -#define SYS_GETPGID 121 - -#define SYS_MKNOD 133 - -#define SYS_FUTEX 202 - -#define SYS_SET_TID_ADDR 218 - -#define SYS_UNLINK_AT 263 - -#define SYS_PIPE 293 - -#define SYS_WRITEV 20 - -// 与linux不一致的调用,在linux基础上累加 -#define SYS_PUT_STRING 100000 -#define SYS_SBRK 100001 -/// todo: 该系统调用与Linux不一致,将来需要删除该系统调用!!! 删的时候记得改C版本的libc -#define SYS_CLOCK 100002 -#define SYS_SCHED 100003 - -/** - * @brief 用户态系统调用函数 - * - * @param syscall_id - * @param arg0 - * @param arg1 - * @param arg2 - * @param arg3 - * @param arg4 - * @param arg5 - * @return long - */ -long syscall_invoke(uint64_t syscall_id, uint64_t arg0, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5); \ No newline at end of file