修改用户应用编译方式

ch4
zhangxinyu 2 years ago
parent c8c00b3c9e
commit 62741d6012

@ -3,6 +3,10 @@ TARGET := riscv64gc-unknown-none-elf
OBJDUMP := rust-objdump --arch-name=riscv64 OBJDUMP := rust-objdump --arch-name=riscv64
OBJCOPY := rust-objcopy --binary-architecture=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 APP_DIR := src/bin
TARGET_DIR := target/$(TARGET)/$(MODE) TARGET_DIR := target/$(TARGET)/$(MODE)
APPS := $(wildcard $(APP_DIR)/*.rs) APPS := $(wildcard $(APP_DIR)/*.rs)
@ -12,12 +16,12 @@ BINS := $(patsubst $(APP_DIR)/%.rs, $(TARGET_DIR)/%.bin, $(APPS))
# 编译elf文件 # 编译elf文件
build_elf: clean build_elf: clean
@python3 build.py CARGO_BUILD_RUSTFLAGS="$(RUST_FLAGS)" \
cargo build --$(MODE) --target=$(TARGET)
# 把每个elf文件去掉无关代码 # 把每个elf文件去掉无关代码
build: build_elf build: build_elf
@$(foreach elf, $(ELFS), $(OBJCOPY) $(elf) --strip-all -O binary $(patsubst $(TARGET_DIR)/%, $(TARGET_DIR)/%.bin, $(elf));)
clean: clean:

@ -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

@ -1,7 +1,8 @@
OUTPUT_ARCH(riscv) OUTPUT_ARCH(riscv)
ENTRY(_start) ENTRY(_start)
BASE_ADDRESS = 0x80400000; BASE_ADDRESS = 0x10000;
SECTIONS SECTIONS
{ {
@ -10,22 +11,22 @@ SECTIONS
*(.text.entry) *(.text.entry)
*(.text .text.*) *(.text .text.*)
} }
. = ALIGN(4K);
.rodata : { .rodata : {
*(.rodata .rodata.*) *(.rodata .rodata.*)
*(.srodata .srodata.*) *(.srodata .srodata.*)
} }
. = ALIGN(4K);
.data : { .data : {
*(.data .data.*) *(.data .data.*)
*(.sdata .sdata.*) *(.sdata .sdata.*)
} }
.bss : { .bss : {
start_bss = .;
*(.bss .bss.*) *(.bss .bss.*)
*(.sbss .sbss.*) *(.sbss .sbss.*)
end_bss = .;
} }
/DISCARD/ : { /DISCARD/ : {
*(.eh_frame) *(.eh_frame)
*(.debug*) *(.debug*)
} }
} }

Loading…
Cancel
Save