From 62741d601271b6cdaac52718e1d32fa1901b8e2d Mon Sep 17 00:00:00 2001 From: zhangxinyu <840317537@qq.com> Date: Thu, 1 Jun 2023 15:34:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=94=A8=E6=88=B7=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E7=BC=96=E8=AF=91=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ch4/user/Makefile | 8 ++++++-- ch4/user/build.py | 39 --------------------------------------- ch4/user/src/linker.ld | 9 +++++---- 3 files changed, 11 insertions(+), 45 deletions(-) delete mode 100644 ch4/user/build.py diff --git a/ch4/user/Makefile b/ch4/user/Makefile index 367e9f2..984ed3e 100644 --- a/ch4/user/Makefile +++ b/ch4/user/Makefile @@ -3,6 +3,10 @@ TARGET := riscv64gc-unknown-none-elf OBJDUMP := rust-objdump --arch-name=riscv64 OBJCOPY := rust-objcopy --binary-architecture=riscv64 +RUST_FLAGS := -Clink-arg=-Tsrc/linker.ld # 使用我们自己的链接脚本 +RUST_FLAGS += -Cforce-frame-pointers=yes # 强制编译器生成帧指针 +RUST_FLAGS:=$(strip ${RUST_FLAGS}) + APP_DIR := src/bin TARGET_DIR := target/$(TARGET)/$(MODE) APPS := $(wildcard $(APP_DIR)/*.rs) @@ -12,12 +16,12 @@ BINS := $(patsubst $(APP_DIR)/%.rs, $(TARGET_DIR)/%.bin, $(APPS)) # 编译elf文件 build_elf: clean - @python3 build.py + CARGO_BUILD_RUSTFLAGS="$(RUST_FLAGS)" \ + cargo build --$(MODE) --target=$(TARGET) # 把每个elf文件去掉无关代码 build: build_elf - @$(foreach elf, $(ELFS), $(OBJCOPY) $(elf) --strip-all -O binary $(patsubst $(TARGET_DIR)/%, $(TARGET_DIR)/%.bin, $(elf));) clean: diff --git a/ch4/user/build.py b/ch4/user/build.py deleted file mode 100644 index 08661bd..0000000 --- a/ch4/user/build.py +++ /dev/null @@ -1,39 +0,0 @@ -import os - -base_address = 0x80400000 # 第一个应用的起始地址 -step = 0x20000 # 每个应用的大小 -linker = "src/linker.ld" # 自定义链接脚本 - -RUST_FLAGS = f"-Clink-arg=-T{linker} " # 使用我们自己的链接脚本 -RUST_FLAGS += "-Cforce-frame-pointers=yes " # 强制编译器生成帧指针 -TARGET = "riscv64gc-unknown-none-elf" - -app_id = 0 -apps: list[str] = os.listdir("src/bin") -apps.sort() -for app_file in apps: - app_name = app_file.split(".")[0] - - lines = [] # 修改了base_address linker.ld - lines_before = [] # 最原本的linker.ld的文本, 最下面会恢复 - - # 读出原本文件 - with open(linker, "r") as f: - for line in f.readlines(): - lines_before.append(line) # 保存原本的文本 - line = line.replace(hex(base_address), hex(base_address+step*app_id)) # 替换的文本 - lines.append(line) - with open(linker, "w+") as f: - f.writelines(lines) - - # 逐个编译 - cmd = f"CARGO_BUILD_RUSTFLAGS='{RUST_FLAGS}' cargo build --bin {app_name} --release --target={TARGET}" - print(cmd) - os.system(cmd) - - print(f"[build.py] application {app_name} start with address {hex(base_address+step*app_id)}") - - # 恢复 - with open(linker, "w+") as f: - f.writelines(lines_before) - app_id += 1 \ No newline at end of file diff --git a/ch4/user/src/linker.ld b/ch4/user/src/linker.ld index 9d48923..02f7b6b 100644 --- a/ch4/user/src/linker.ld +++ b/ch4/user/src/linker.ld @@ -1,7 +1,8 @@ + OUTPUT_ARCH(riscv) ENTRY(_start) -BASE_ADDRESS = 0x80400000; +BASE_ADDRESS = 0x10000; SECTIONS { @@ -10,22 +11,22 @@ SECTIONS *(.text.entry) *(.text .text.*) } + . = ALIGN(4K); .rodata : { *(.rodata .rodata.*) *(.srodata .srodata.*) } + . = ALIGN(4K); .data : { *(.data .data.*) *(.sdata .sdata.*) } .bss : { - start_bss = .; *(.bss .bss.*) *(.sbss .sbss.*) - end_bss = .; } /DISCARD/ : { *(.eh_frame) *(.debug*) } -} \ No newline at end of file +}