Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"name": "DragonOS",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/user/libs/libc/src/include"
"${workspaceFolder}/user/libs/libc/src/include",
"${workspaceFolder}/user/libs/libc/src/include/export"
],
"defines": [
"__x86_64__",
Expand Down
13 changes: 9 additions & 4 deletions user/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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
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)

ECHO:
Expand Down Expand Up @@ -44,9 +44,14 @@ sys_api_lib_stage_1: make_output_dir


sys_api_lib: sys_api_lib_stage_1
$(AR) crvs $(ROOT_PATH)/bin/sysroot/usr/lib/libc.a $(shell find ./libs/* -name "*.o")
$(shell find ./libs/* -name "*.o" | xargs -I {} cp {} $(ROOT_PATH)/bin/sysroot/usr/lib/)
$(shell cp -r $(ROOT_PATH)/user/libs/libc/src/include/* $(ROOT_PATH)/bin/sysroot/usr/include/)

# 打包系统库
mkdir -p $(ROOT_PATH)/bin/tmp/user/sys_api_lib_build_tmp
$(AR) x $(ROOT_PATH)/user/libs/libc/target/x86_64-unknown-none/release/liblibc.a --output=$(ROOT_PATH)/bin/tmp/user/sys_api_lib_build_tmp
$(AR) crvs $(ROOT_PATH)/bin/sysroot/usr/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/* $(ROOT_PATH)/bin/sysroot/usr/include/)
$(shell cp -r $(ROOT_PATH)/user/libs/libc/src/arch/x86_64/c*.o $(ROOT_PATH)/bin/sysroot/usr/lib/)

clean:
Expand Down
2 changes: 1 addition & 1 deletion user/apps/about/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ GIT_COMMIT_SHA1=$(shell git log -n 1 | head -n 1 | cut -d ' ' -f 2 | cut -c1-8)

all: about.o

$(LD) -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/about $(shell find . -name "*.o") $(shell find $(sys_libs_dir) -name "*.o") -T about.lds
$(LD) -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/about $(shell find . -name "*.o") $(ROOT_PATH)/bin/sysroot/usr/lib/libc.a -T about.lds

$(OBJCOPY) -I elf64-x86-64 -R ".eh_frame" -R ".comment" -O elf64-x86-64 $(tmp_output_dir)/about $(output_dir)/about.elf

Expand Down
4 changes: 4 additions & 0 deletions user/apps/about/about.lds
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ SECTIONS
_text = .;

*(.text)
*(.text.*)

_etext = .;
}
Expand All @@ -23,6 +24,7 @@ SECTIONS
{
_data = .;
*(.data)
*(.data.*)

_edata = .;
}
Expand All @@ -33,6 +35,7 @@ SECTIONS
{
_rodata = .;
*(.rodata)
*(.rodata.*)
_erodata = .;
}

Expand All @@ -41,6 +44,7 @@ SECTIONS
{
_bss = .;
*(.bss)
*(.bss.*)
_ebss = .;
}

Expand Down
2 changes: 1 addition & 1 deletion user/apps/shell/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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") $(shell find $(sys_libs_dir) -name "*.o") -T shell.lds
$(LD) -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/shell $(shell find . -name "*.o") $(ROOT_PATH)/bin/sysroot/usr/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
Expand Down
4 changes: 4 additions & 0 deletions user/apps/shell/shell.lds
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ SECTIONS
_text = .;

*(.text)
*(.text.*)

_etext = .;
}
Expand All @@ -23,6 +24,7 @@ SECTIONS
{
_data = .;
*(.data)
*(.data.*)

_edata = .;
}
Expand All @@ -33,6 +35,7 @@ SECTIONS
{
_rodata = .;
*(.rodata)
*(.rodata.*)
_erodata = .;
}

Expand All @@ -41,6 +44,7 @@ SECTIONS
{
_bss = .;
*(.bss)
*(.bss.*)
_ebss = .;
}

Expand Down
2 changes: 1 addition & 1 deletion user/apps/test_signal/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
all: main.o

$(LD) -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/test_signal $(shell find . -name "*.o") $(shell find $(sys_libs_dir) -name "*.o") -T link.lds
$(LD) -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/test_signal $(shell find . -name "*.o") $(ROOT_PATH)/bin/sysroot/usr/lib/libc.a -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
main.o: main.c
Expand Down
4 changes: 4 additions & 0 deletions user/apps/test_signal/link.lds
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ SECTIONS
_text = .;

*(.text)
*(.text.*)

_etext = .;
}
Expand All @@ -23,6 +24,7 @@ SECTIONS
{
_data = .;
*(.data)
*(.data.*)

_edata = .;
}
Expand All @@ -33,6 +35,7 @@ SECTIONS
{
_rodata = .;
*(.rodata)
*(.rodata.*)
_erodata = .;
}

Expand All @@ -41,6 +44,7 @@ SECTIONS
{
_bss = .;
*(.bss)
*(.bss.*)
_ebss = .;
}

Expand Down
45 changes: 45 additions & 0 deletions user/libs/libc/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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!");
}
}
2 changes: 1 addition & 1 deletion user/libs/libc/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ libc: $(libc_objs) $(libc_sub_dirs) libc_rust

libc_rust:
rustup default nightly
cargo +nightly build --release --target ./x86_64-unknown-none.json
cargo +nightly build --release --target ./arch/x86_64/x86_64-unknown-none.json
1 change: 1 addition & 0 deletions user/libs/libc/src/include/internal/bindings/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bindings.rs
1 change: 1 addition & 0 deletions user/libs/libc/src/include/internal/bindings/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod bindings;
15 changes: 15 additions & 0 deletions user/libs/libc/src/include/internal/bindings/wrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @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 <stdio.h>
#include <unistd.h>
1 change: 1 addition & 0 deletions user/libs/libc/src/include/internal/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod bindings;
1 change: 1 addition & 0 deletions user/libs/libc/src/include/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod internal;
11 changes: 9 additions & 2 deletions user/libs/libc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@
#[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;

use core::panic::PanicInfo;
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}

#[no_mangle]
pub extern "C" fn scanf() {

loop {
unsafe {
putchar(88);
}
}
}