diff --git a/articles/20230919-elf2flt-elf2flt-build-0.md b/articles/20230919-elf2flt-elf2flt-build-0.md new file mode 100644 index 0000000000000000000000000000000000000000..043c64866e93b903846679eab2c860613248c96c --- /dev/null +++ b/articles/20230919-elf2flt-elf2flt-build-0.md @@ -0,0 +1,79 @@ +> Corrector: [TinyCorrect](https://gitee.com/tinylab/tinycorrect) v0.2-rc2 - [spaces toc urls refs pangu]
+> Author: Odysseus <320873791@qq.com>
+> Date: 2023/09/19
+> Revisor: walimis <>
+> Project: [RISC-V Linux 内核剖析](https://gitee.com/tinylab/riscv-linux)
+> Proposal: [为 ELF2FLT 完善独立编译与安装支持](https://gitee.com/tinylab/riscv-linux/issues/I79PO2)
+> Sponsor: PLCT Lab, ISCAS + +# elf2flt 篇二 原理和构建 + +## 前言 + +我们在上一篇文章里分析了 elf2flt 的源码,现在我们来阐述其原理,并分析其构建过程。 + +## 原理解析 + +从上一篇的内容,我们发现对于 elf2flt 来说,其执行上是分成这样几个步骤的: + +1. 解析 ELF 文件:elf2flt 首先解析输入的 ELF 文件。这包括读取 ELF 头部、节表、程序头部以及节的内容等信息。 +2. 分析节表信息:elf2flt 分析 ELF 文件的节表,识别哪些节包含可执行代码、数据以及其他相关信息。通常,只有包含可执行代码和数据的节才会包含在 bFLT 文件中。 +3. 生成 bFLT 文件头部:elf2flt 创建一个 bFLT 文件头部,该头部包含了 bFLT 文件的元信息,如入口地址、代码段长度、数据段长度等。 +4. 将节内容复制到 bFLT 文件:elf2flt 从 ELF 文件中将识别出的代码和数据节的内容复制到 bFLT 文件中。这通常涉及将节的内容按照 bFLT 文件的格式进行重新组织和排列。 +5. 生成重定位信息:如果 ELF 文件包含重定位信息,elf2flt 会生成相应的 bFLT 文件重定位信息,以确保在加载 bFLT 文件时可以正确解析和处理符号引用。 +6. 生成 bFLT 文件:最后,elf2flt 将生成的 bFLT 文件写入磁盘,可以将其用于嵌入式系统中的启动加载。 + +其执行关键,也是之所以能将 ELF 格式转化成可以供无 mmu 系统使用的 bFLT 格式的可执行文件的关键,是其将每节的虚拟地址映射到新生成的重定位信息中,使所有在运行时才确定关系的映射信息转为在加载时确定。 + +而动态链接库不是 elf2flt 能够处理的,在此就不说明。(PS:动态链接库可以考虑通过定制化的动态加载器,但实现难度较大) + +## 部署编译 + +从官方文档给出的教程来看,elf2flt 的编译部署主要是如下过程: + +### 准备 + +先更新 `filenames.h`、`elf/riscv.h` 和 `elf/reloc-macros.h`: + +```bash +wget -c https://github.com/gcc-mirror/gcc/raw/master/include/filenames.h +mkdir elf/ && cd elf/ +wget -c https://gitlab.com/git-mirrors/binutils/-/raw/master/include/elf/riscv.h +wget -c https://gitlab.com/git-mirrors/binutils/-/raw/master/include/elf/reloc-macros.h +``` + +安装相关依赖: + +```bash +sudo apt install binutils-dev libiberty-dev +``` + +安装交叉编译器: + +```bash +sudo apt install gcc-riscv64-linux-gnu +``` + +### 配置 + +```bash +./configure --target=riscv64-linux-gnu -with-bfd-include-dir=/usr/include/ --with-binutils-include-dir=/usr/include/ --with-libbfd=/lib/x86_64-linux-gnu/libbfd.a --with-libiberty=/lib/x86_64-linux-gnu/libiberty.a --disable-werror +``` + +### 构建 + +```bash +make +``` + +## 总结 + +总的来说,上面的配置选项适用于在 x86 平台上对 riscv64 平台预先编译构建,但如果要在 riscv64 平台上面直接进行构建,相对来说就会困难许多。其构建过程很难直接遵循上述逻辑,接下来一章,会解释独立在 riscv64 平台进行构建的过程,并且对其进行简化和修改。 + +## 参考资料 + +-. [https://gitee.com/tinylab/riscv-linux/blob/master/articles/20230919-elf2flt-flt-analysis-0.md][001] +-. [https://gitee.com/tinylab/elf2flt][002] + +[001]: https://gitee.com/tinylab/riscv-linux/blob/master/articles/20230919-elf2flt-flt-analysis-0.md +[002]: https://gitee.com/tinylab/elf2flt