feat(parser): 使用静态数组初始化测试向量

- 将多个测试用例中的 `scc_vec_unsafe_from_array` 替换为
  `scc_vec_unsafe_from_static_array` 以提高性能
- 此更改影响了 `test_parser_unit` 和 `test_parser_type` 函数中的多个位置

feat(sccf): 添加SCC格式支持和相关工具

- 创建新的 sccf 库用于处理 SCCF (SCC Format) 文件格式
- 实现了基本的文件格式定义,包括头部、段表、符号表等结构
- 添加了构建器和链接器的基本框架
- 包含格式化工具的初始实现

refactor(main): 修复输出文件检查逻辑

- 修正主函数中输出文件检查条件,确保在 fp 为 null 时正确处理
- 更新输出消息显示逻辑以匹配文件操作状态
This commit is contained in:
zzy
2026-03-16 11:02:32 +08:00
parent 45c59e0b65
commit cabd1710ed
12 changed files with 614 additions and 27 deletions

165
libs/sccf/include/sccf.h Normal file
View File

@@ -0,0 +1,165 @@
/**
* @file sccf.h
* @brief scc format (sccf) 头文件
*
* SCCF是一个极简的可执行可链接文件格式, 专注于内部链接处理,
* 同时提供外部符号导入/导出的抽象接口。
*/
#ifndef __SCC_FORMAT_H__
#define __SCC_FORMAT_H__
#include <stdint.h>
#define sccf_byte_t uint8_t
#define sccf_enum_t uint32_t
#define sccf_size_t uint64_t
#define sccf_isize_t int64_t
#ifdef __cplusplus
extern "C" {
#endif
/** SCCF魔数 */
#define SCCF_MAGIC "SCCFmt\0\0"
/** SCCF版本号 */
#define SCCF_VERSION 1
/** 架构类型 */
typedef enum {
SCCF_ARCH_UNKNOWN = 0, ///< 未知占位符架构
SCCF_ARCH_RISCV32 = 1, ///< RISC-V 32位 小端
SCCF_ARCH_RISCV64 = 2, ///< RISC-V 64位 小端
SCCF_ARCH_X86 = 3, ///< x86 小端
SCCF_ARCH_AMD64 = 4, ///< AMD64 小端
} sccf_arch_t;
/** 文件标志位 */
typedef enum {
SCCF_TYPE_FLAG_EXECUTABLE = 0x01, ///< 可执行文件
SCCF_TYPE_FLAG_RELOCATABLE = 0x02, ///< 可重定位文件
SCCF_TYPE_FLAG_EXE_RELOC = 0x03, ///< 内部链接后的可执行文件
} sccf_type_flags_t;
/** 符号类型 */
typedef enum {
SCCF_SYM_TYPE_UNDEF = 0, ///< 未定义
SCCF_SYM_TYPE_FUNC = 1, ///< 函数
SCCF_SYM_TYPE_DATA = 2, ///< 数据
SCCF_SYM_TYPE_OBJECT = 3, ///< 对象
} sccf_sym_type_t;
/** 符号绑定类型 */
typedef enum {
SCCF_SYM_BIND_LOCAL = 0, ///< 局部符号
SCCF_SYM_BIND_GLOBAL = 1, ///< 全局符号
SCCF_SYM_BIND_WEAK = 2, ///< 弱引用
} sccf_sym_bind_t;
/** 符号可见性 */
typedef enum {
SCCF_SYM_VIS_DEFAULT = 0, ///< 默认可见性
SCCF_SYM_VIS_HIDDEN = 1, ///< 隐藏
SCCF_SYM_VIS_PROTECTED = 2, ///< 受保护
} sccf_sym_vis_t;
/** 段类型 */
typedef enum {
SCCF_SECT_NONE = 0, ///< 无
SCCF_SECT_CODE = 1, ///< 代码段
SCCF_SECT_DATA = 2, ///< 数据段
SCCF_SECT_RODATA = 3, ///< 只读数据段
SCCF_SECT_UNINIT_DATA = 4, ///< BSS段(未初始化数据)
SCCF_SECT_SYMTAB = 5, ///< 符号表
SCCF_SECT_STRTAB = 6, ///< 字符串表
SCCF_SECT_RELOC = 7, ///< 重定位表
} sccf_sect_type_t;
/** 重定位类型 */
typedef enum {
SCCF_RELOC_ABS = 1, ///< 绝对地址
SCCF_RELOC_REL = 2, ///< 相对地址
} sccf_reloc_type_t;
/**
* @brief SCCF文件头
*/
typedef struct sccf_header {
sccf_byte_t magic[8]; ///< 魔数: "SCCFmt\0\0"
sccf_enum_t type; ///< 类型
sccf_enum_t version; ///< 版本号
sccf_enum_t arch; ///< 架构
sccf_size_t entry_point; ///< 入口点在代码段的相对地址
sccf_size_t sect_header_num; ///< 节头数量
sccf_size_t reserved[4]; ///< 保留字段, 可置零
} sccf_header_t;
/**
* @brief SCCF段
*/
typedef struct {
sccf_byte_t name[8]; ///< 段名称 仅供展示 eg. emoji or ".text"
sccf_enum_t scf_sect_type; ///< 段类型 (内部实际区分的方式)
sccf_size_t size; ///< 段数据实际的大小
sccf_size_t data_size; ///< 段数据的有效数据大小
sccf_size_t addralign; ///< 内存对齐要求 (字节对齐, 如 1,2,4,8,...)
sccf_size_t info; ///< 段信息 (如条目数,链接索引等)
sccf_size_t reserved[2]; ///< 保留
} sccf_sect_header_t;
/**
* @brief SCCF符号表
*/
typedef struct {
sccf_size_t name_offset; ///< 符号名称在字符串表中的偏移量
sccf_enum_t scf_sym_type; ///< 符号类型
sccf_enum_t scf_sym_bind; ///< 符号绑定类型
sccf_enum_t scf_sym_vis; ///< 符号可见性
sccf_enum_t scf_sect_type; ///< 该符号的段类型
sccf_size_t scf_sect_offset; ///< 该符号在段中的偏移量
sccf_size_t scf_sym_size; ///< 该符号符号选中的大小
} sccf_sym_t;
/**
* @brief SCCF重定向条目
*/
typedef struct {
sccf_size_t offset; ///< 在数据段中的偏移量
sccf_size_t sym_idx; ///< 符号索引
sccf_enum_t type; ///< 重定位类型
sccf_enum_t sect_type; ///< 段类型(代码段/数据段)
sccf_isize_t addend; ///< 加数
} sccf_reloc_t;
/**
* @brief sccf 格式结构(可选 填充到某种自定义的对齐
* 默认启用sect_datas的64bytes对齐规则)
*
* @warning 其中sccf_sect_header_t的顺序必须和sccf_sect_data_t一致,
* 但是不规定sect_headers内部的顺序下面顺序仅供参考
*
* sccf_header_t header;
* sccf_sect_header_t sect_headers[header.sect_header_num] = {
* [0] = text_section,
* [1] = data_section,
* [2] = strtab_section,
* [3] = symtab_section,
* [4] = reloc_section,
* [5-...] = other_section,
* };
* sccf_sect_data_t sect_datas[header.sect_header_num][...] = {
* [0] = text_section_data,
* [1] = data_section_data,
* [2] = strtab_section_data,
* [3] = symtab_section_data,
* [4] = reloc_section_data,
* [5-...] = other_section_data,
* };
*/
#ifdef __cplusplus
}
#endif
#endif /* __SCC_FORMAT_H__ */

View File

@@ -0,0 +1,28 @@
#ifndef __SCC_FORMAT_BUILDER_H__
#define __SCC_FORMAT_BUILDER_H__
#include "sccf_utils.h"
#include <scc_utils.h>
typedef SCC_VEC(sccf_sym_t) sccf_sym_vec_t;
typedef SCC_VEC(sccf_reloc_t) sccf_reloc_vec_t;
typedef struct {
sccf_t sccf;
int aligned;
scc_strpool_t strpool;
scc_hashtable_t str2offset;
sccf_sym_vec_t syms;
sccf_reloc_vec_t relocs;
} sccf_builder_t;
void sccf_builder_init(sccf_builder_t *builder);
void sccf_builder_add_section(sccf_builder_t *builder,
sccf_sect_header_t *sect_header,
sccf_sect_data_t *sect_data);
void sccf_builder_to_buffer(sccf_builder_t *builder, sccf_buffer_t *buffer);
void sccf_builder_to_file(sccf_builder_t *builder, const char *file_path);
#endif /* __SCC_FORMAT_BUILDER_H__ */

View File

@@ -0,0 +1,18 @@
#ifndef __SCC_FORMAT_LINKER_H__
#define __SCC_FORMAT_LINKER_H__
#include "sccf_builder.h"
typedef SCC_VEC(sccf_t) sccf_vec_t;
typedef struct {
sccf_builder_t builder;
sccf_vec_t link_sccfs;
scc_strpool_t strpool;
scc_hashtable_t str2offset;
sccf_sym_vec_t syms;
sccf_reloc_vec_t relocs;
} sccf_linker_t;
#endif /* __SCC_FORMAT_LINKER_H__ */

View File

@@ -0,0 +1,296 @@
/**
* @file sccf_utils.h
* @brief SCCF 格式辅助函数
*
* 提供基于节头表的节定位、查找和遍历函数, 不涉及字节序转换。
* 假设文件已经为主机字节序。
*/
#ifndef __SCC_FORMAT_UTILS_H__
#define __SCC_FORMAT_UTILS_H__
#include "sccf.h"
#include <scc_core.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @name 节头表定位
* @{
*/
/**
* @brief 获取节头表的起始地址
* @param base 文件缓冲区起始地址
* @return 节头表指针 (需强制转换为 sccf_sect_header_t*)
*/
static inline u8 *sccf_sect_header_table(u8 *base) {
return base + sizeof(sccf_header_t);
}
/**
* @brief 获取指定索引的节头指针
* @param base 文件缓冲区起始地址
* @param idx 节索引 (0 <= idx < sect_header_num)
* @return 指向该节头的指针, 若索引无效返回 null
*/
static inline sccf_sect_header_t *sccf_sect_header(u8 *base, usize idx) {
sccf_header_t *hdr = (sccf_header_t *)base;
if (idx >= (usize)hdr->sect_header_num)
return null;
u8 *table = sccf_sect_header_table(base);
return (sccf_sect_header_t *)(table + idx * sizeof(sccf_sect_header_t));
}
/** @} */
/**
* @name 节数据定位
* @{
*/
/**
* @brief 计算指定索引的节数据在文件中的偏移
* @param base 文件缓冲区起始地址
* @param idx 节索引
* @return 数据起始偏移 (相对于文件头) , 若索引无效返回 0, 有效值始终大于 0
*/
static inline usize sccf_sect_data_offset(u8 *base, usize idx) {
const sccf_header_t *hdr = (const sccf_header_t *)base;
if (idx >= (usize)hdr->sect_header_num)
return 0;
usize offset = sizeof(sccf_header_t) +
(usize)hdr->sect_header_num * sizeof(sccf_sect_header_t);
for (usize i = 0; i < idx; ++i) {
const sccf_sect_header_t *sh = sccf_sect_header(base, i);
offset +=
(usize)sh->size; /* 使用 size 字段作为节数据在文件中的实际大小 */
}
return offset;
}
/**
* @brief 获取指定索引的节数据起始地址
* @param base 文件缓冲区起始地址
* @param idx 节索引
* @return 数据起始地址, 若索引无效返回 null
*/
static inline u8 *sccf_sect_data(u8 *base, usize idx) {
usize off = sccf_sect_data_offset(base, idx);
return (off == 0) ? null : base + off;
}
/** @} */
/**
* @name 节查找
* @{
*/
/**
* @brief 查找指定类型的第一个节
* @param base 文件缓冲区起始地址
* @param type 要查找的节类型 (如 SCCF_SECT_CODE)
* @return 节索引, 未找到返回 hdr->sect_header_num
*/
static inline usize sccf_find_sect_by_type(u8 *base, sccf_enum_t type) {
const sccf_header_t *hdr = (const sccf_header_t *)base;
for (usize i = 0; i < (usize)hdr->sect_header_num; ++i) {
sccf_sect_header_t *sh = sccf_sect_header(base, i);
if (sh->scf_sect_type == type)
return i;
}
return (usize)hdr->sect_header_num;
}
/**
* @brief 查找指定名称的节
* @param base 文件缓冲区起始地址
* @param name 8字节名称 (不足补零, 可用 memcmp 比较)
* @return 节索引, 未找到返回 hdr->sect_header_num
*/
static inline usize sccf_find_sect_by_name(u8 *base, const u8 name[8]) {
const sccf_header_t *hdr = (const sccf_header_t *)base;
for (usize i = 0; i < (usize)hdr->sect_header_num; ++i) {
const sccf_sect_header_t *sh = sccf_sect_header(base, i);
if (scc_memcmp(sh->name, name, 8) == 0)
return i;
}
return (usize)hdr->sect_header_num;
}
/** @} */
/**
* @name 节遍历
* @{
*/
/**
* @brief 遍历所有节的回调函数类型
* @param idx 节索引
* @param sh 节头指针
* @param data 节数据起始地址
* @param size 节数据大小 (即节头的 size 字段)
* @param user 用户自定义数据
*/
typedef void (*sccf_sect_visit_fn)(usize idx, const sccf_sect_header_t *sh,
const u8 *data, usize size, void *user);
/**
* @brief 遍历所有节, 对每个节调用回调函数
* @param base 文件缓冲区起始地址
* @param visit 回调函数
* @param user 用户数据透传
*/
static inline void sccf_foreach_sect(u8 *base, sccf_sect_visit_fn visit,
void *user) {
const sccf_header_t *hdr = (const sccf_header_t *)base;
for (usize i = 0; i < (usize)hdr->sect_header_num; ++i) {
sccf_sect_header_t *sh = sccf_sect_header(base, i);
const u8 *data = sccf_sect_data(base, i);
if (data) {
visit(i, sh, data, (usize)sh->size, user);
}
}
}
/** @} */
/**
* @defgroup 序列化和反序列化
*/
typedef SCC_VEC(u8) sccf_buffer_t;
typedef SCC_VEC(u8) sccf_sect_data_t;
typedef SCC_VEC(sccf_sect_data_t) sccf_sect_data_vec_t;
typedef SCC_VEC(sccf_sect_header_t) sccf_sect_header_vec_t;
typedef struct {
sccf_header_t header;
sccf_sect_header_vec_t sect_headers;
sccf_sect_data_vec_t sect_datas;
} sccf_t;
/**
* @brief 初始化sccf
* @param[out] sccf
*/
static inline void sccf_init(sccf_t *sccf) {
scc_memcpy(sccf->header.magic, SCCF_MAGIC, sizeof(sccf->header.magic));
sccf->header.type = 0;
sccf->header.version = SCCF_VERSION;
sccf->header.arch = SCCF_ARCH_UNKNOWN;
sccf->header.entry_point = 0;
sccf->header.sect_header_num = 0;
scc_memset(sccf->header.reserved, 0, sizeof(sccf->header.reserved));
scc_vec_init(sccf->sect_headers);
scc_vec_init(sccf->sect_datas);
}
/**
* @brief 从buffer解析到sccf可以选择是否复制, 如果不复制则不能修改
* @warning 注意内存释放, 如果选择复制的话
*
* @param[out] sccf
* @param[in] buffer
* @param[in] copied
*/
static inline void sccf_parse(sccf_t *sccf, sccf_buffer_t *buffer, int copied) {
u8 *data = scc_vec_unsafe_get_data(*buffer);
sccf->header = *(sccf_header_t *)data;
usize offset = sizeof(sccf_header_t);
if (copied) {
scc_vec_init(sccf->sect_headers);
for (usize i = 0; i < sccf->header.sect_header_num; i += 1) {
scc_vec_push(sccf->sect_headers,
*(sccf_sect_header_t *)(data + offset));
offset += sizeof(sccf_sect_header_t);
}
} else {
u8 *header_table = data + offset;
scc_vec_unsafe_from_buffer(sccf->sect_headers,
(sccf_sect_header_t *)header_table,
sccf->header.sect_header_num);
offset += sizeof(sccf_sect_header_t) * sccf->header.sect_header_num;
}
scc_vec_init(sccf->sect_datas);
scc_vec_foreach(sccf->sect_headers, i) {
sccf_sect_header_t *header = &scc_vec_at(sccf->sect_headers, i);
sccf_sect_data_t sect_data;
scc_vec_init(sect_data);
if (copied) {
scc_vec_realloc(sect_data, header->size);
scc_memcpy(scc_vec_unsafe_get_data(sect_data), data + offset,
header->size);
scc_vec_size(sect_data) = header->size;
} else {
scc_vec_unsafe_from_buffer(sect_data, data + offset, header->size);
}
scc_vec_push(sccf->sect_datas, sect_data);
offset += header->size;
}
}
/**
* @brief 获取sccf大小
*
* @param[in] sccf
* @return usize
*/
static inline usize sccf_size(sccf_t *sccf) {
if (scc_vec_size(sccf->sect_datas) != scc_vec_size(sccf->sect_headers) ||
scc_vec_size(sccf->sect_headers) != sccf->header.sect_header_num) {
Panic();
}
usize size = sizeof(sccf_header_t);
size += sizeof(sccf_sect_header_t) * sccf->header.sect_header_num;
scc_vec_foreach(sccf->sect_headers, i) {
sccf_sect_header_t *sccf_sect_header =
&scc_vec_at(sccf->sect_headers, i);
Assert(scc_vec_size(scc_vec_at(sccf->sect_datas, i)) ==
sccf_sect_header->size);
size += sccf_sect_header->size;
}
return size;
}
/**
* @brief 将sccf转换成buffer
* @warning buffer需要被初始化且为空
*
* @param[in] sccf
* @param[out] buffer
*/
static inline void sccf_write(sccf_t *sccf, sccf_buffer_t *buffer) {
usize size = sccf_size(sccf);
if (scc_vec_size(*buffer) < size) {
scc_vec_realloc(*buffer, size);
scc_vec_size(*buffer) = size;
}
u8 *data = scc_vec_unsafe_get_data(*buffer);
usize offset = 0;
scc_memcpy(data, &sccf->header, sizeof(sccf_header_t));
data += sizeof(sccf_header_t);
scc_memcpy(data, scc_vec_unsafe_get_data(sccf->sect_headers),
sizeof(sccf_sect_header_t) * sccf->header.sect_header_num);
data += sizeof(sccf_sect_header_t) * sccf->header.sect_header_num;
scc_vec_foreach(sccf->sect_datas, i) {
sccf_sect_data_t *sect_data = &scc_vec_at(sccf->sect_datas, i);
scc_memcpy(data, scc_vec_unsafe_get_data(*sect_data), sect_data->size);
data += sect_data->size;
}
if (data - scc_vec_unsafe_get_data(*buffer) != size) {
Panic();
}
}
#ifdef __cplusplus
}
#endif
#endif /* __SCC_FORMAT_UTILS_H__ */