Files
scc/libs/format/include/scf.h
zzy 3aaf3a3991 feat: add SCF format library and rename components to SCC prefix
- Introduce new SCF (SCC Format) library with header, implementation, and test files
- SCF is a minimal executable/linkable format focused on internal linking with external symbol import/export abstraction
- Rename lexer and lex_parser packages from 'smcc_' to 'scc_' prefix for consistency
- Update hashmap implementation to use 'scc_' prefix for types and structures
- Add build configuration for new format library with dependencies on libcore and libutils
2025-12-11 17:29:12 +08:00

151 lines
3.3 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @file scf.h
* @brief scc format (SMF) 头文件
*
* SCF是一个极简的可执行可链接文件格式专注于内部链接处理
* 同时提供外部符号导入/导出的抽象接口。
*/
#ifndef __SCC_FORMAT_H__
#define __SCC_FORMAT_H__
#include <stddef.h>
#include <stdint.h>
#define scf_byte_t uint8_t
#define scf_enum_t uint32_t
#define scf_size_t uint32_t
#ifdef __cplusplus
extern "C" {
#endif
/** SCF魔数 */
#define SCF_MAGIC "SCF\0"
/** SCF版本号 */
#define SCF_VERSION 1
/** 架构类型 */
typedef enum {
SCF_ARCH_UNKNOWN = 0,
SCF_ARCH_RV32 = 1,
SCF_ARCH_RV64 = 2,
SCF_ARCH_X86 = 3,
SCF_ARCH_X64 = 4,
} scf_arch_t;
/** 文件标志位 */
typedef enum {
SCF_FLAG_EXECUTABLE = 0x01, // 可执行文件
SCF_FLAG_RELOCATABLE = 0x02, // 可重定位文件
SCF_FLAG_EXE_RELOC =
SCF_FLAG_EXECUTABLE | SCF_FLAG_RELOCATABLE, // 内部链接后的可执行文件
} scf_flags_t;
/** 符号类型 */
typedef enum {
SCF_SYM_TYPE_UNDEF = 0, // 未定义
SCF_SYM_TYPE_FUNC = 1, // 函数
SCF_SYM_TYPE_DATA = 2, // 数据
SCF_SYM_TYPE_OBJECT = 3, // 对象
} scf_sym_type_t;
/** 符号绑定类型 */
typedef enum {
SCF_SYM_BIND_LOCAL = 0, // 局部符号
SCF_SYM_BIND_GLOBAL = 1, // 全局符号
SCF_SYM_BIND_WEAK = 2, // 弱引用
} scf_sym_bind_t;
/** 符号可见性 */
typedef enum {
SCF_SYM_VIS_DEFAULT = 0, // 默认可见性
SCF_SYM_VIS_HIDDEN = 1, // 隐藏
SCF_SYM_VIS_PROTECTED = 2, // 受保护
} scf_sym_vis_t;
/** 段类型 */
typedef enum {
SCF_SECT_NONE = 0, // 无
SCF_SECT_CODE = 1, // 代码段
SCF_SECT_DATA = 2, // 数据段
SCF_SECT_BSS = 3, // BSS段未初始化数据
SCF_SECT_RODATA = 4, // 只读数据
} scf_sect_type_t;
/** 重定位类型 */
typedef enum {
SCF_RELOC_ABS = 1, // 绝对地址
SCF_RELOC_REL = 2, // 相对地址
SCF_RELOC_PC = 3, // PC相对
} scf_reloc_type_t;
/**
* @brief SCF文件头
*/
typedef struct {
scf_byte_t magic[4]; // 魔数: "SCF\0"
scf_enum_t type; // 类型
scf_enum_t version; // 版本号
scf_enum_t arch; // 架构
scf_enum_t flags; // 标志位
scf_size_t entry_point; // 入口点地址
scf_size_t data_size;
scf_size_t code_size;
scf_size_t strtab_size;
scf_size_t sym_count;
scf_size_t reloc_count;
} scf_header_t;
/**
* @brief SCF段
*/
typedef struct {
scf_size_t size;
scf_enum_t scf_sect_type;
scf_byte_t data[1];
} scf_sect_t;
/**
* @brief SCF符号表
*/
typedef struct {
scf_size_t name_offset;
scf_enum_t scf_sym_type;
scf_enum_t scf_sym_bind;
scf_enum_t scf_sym_vis;
scf_enum_t scf_sect_type;
scf_size_t scf_sect_offset;
scf_size_t scf_sym_size;
} scf_sym_t;
/**
* @brief SCF重定向条目
*/
typedef struct {
scf_size_t offset; // 在段中的偏移量
scf_size_t sym_idx; // 符号索引
scf_enum_t type; // 重定位类型
scf_enum_t sect_type; // 段类型(代码段/数据段)
scf_size_t addend; // 加数
} scf_reloc_t;
/*
scf 文件结构
scf_header_t header;
scf_sect_data_t text;
scf_sect_data_t data;
scf_sect_data_t symtab;
scf_sect_data_t reloc;
scf_sect_data_t strtab;
*/
#ifdef __cplusplus
}
#endif
#endif /* __SCC_FORMAT_H__ */