Files
scc/libs/target/sccf2target/tests/test_sccf2pe_run.c
zzy 31d7e91ef1 refactor(ast2ir): 重构ABI类型系统并修复union结构问题
- 将scc_ast_def.h中的attr_of从union改为struct以修复结构定义问题
- 添加type_abi依赖到ast2ir模块的cbuild.toml配置文件中
- 重命名scc_ast2ir.h中的abi字段为type_abi,并更新相关初始化函数签名
- 移除废弃的scc_abi_type.h和相关平台ABI头文件
- 添加辅助函数is_variadic_marker和fixed_param_count用于处理可变参数
- 添加数组和聚合类型初始化的辅助函数
2026-06-01 12:14:13 +08:00

73 lines
3.2 KiB
C
Raw 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.
#include <sccf.h>
#include <sccf2pe.h>
#include <sccf_builder.h>
#include <stdio.h>
#include <x86/scc_x86_patch.h>
int main() {
char data[] = "Hello, World from SCC PE Builder!\n\0";
/* clang-format off */
char code[] = {
// sub rsp, 0x28 ; 为函数调用分配栈空间
0x48, 0x83, 0xEC, 0x28,
// lea rcx, [rip + data_offset] ; 将字符串地址加载到RCX第一个参数
0x48, 0x8D, 0x0D, 0x00, 0x00, 0x00, 0x00,
// call qword ptr [rip + puts_iat] ; 通过IAT调用puts
0xFF, 0x15, 0x00, 0x00, 0x00, 0x00,
// add rsp, 0x28 ; 恢复栈空间
0x48, 0x83, 0xC4, 0x28,
// xor eax, eax ; 设置返回值为0
0x33, 0xC0,
// ret ; 返回
0xC3,
};
/* clang-format on */
sccf_builder_t builder;
sccf_builder_init(&builder);
sccf_sect_data_t text_section = {
.data = (u8 *)code, .size = sizeof(code), .cap = sizeof(code)};
sccf_sect_data_t data_section = {
.data = (u8 *)data, .size = sizeof(data), .cap = sizeof(data)};
sccf_builder_add_text_section(&builder, &text_section);
sccf_builder_add_data_section(&builder, &data_section);
usize str_idx =
sccf_builder_add_symbol(&builder, "str_data",
&(sccf_sym_t){
.sccf_sect_offset = 0,
.sccf_sect_type = SCCF_SECT_DATA,
.sccf_sym_bind = SCCF_SYM_BIND_GLOBAL,
.sccf_sym_size = sizeof(data),
.sccf_sym_type = SCCF_SYM_TYPE_DATA,
.sccf_sym_vis = SCCF_SYM_VIS_DEFAULT,
});
usize puts_idx =
sccf_builder_add_symbol(&builder, "puts",
&(sccf_sym_t){
.sccf_sect_offset = 0,
.sccf_sect_type = SCCF_SECT_NONE,
.sccf_sym_bind = SCCF_SYM_BIND_GLOBAL,
.sccf_sym_size = 8,
.sccf_sym_type = SCCF_SYM_TYPE_EXTERN,
.sccf_sym_vis = SCCF_SYM_VIS_DEFAULT,
});
sccf_builder_add_reloc(&builder,
(sccf_reloc_t){.addend = 4,
.offset = 7,
.sect_type = SCCF_SECT_CODE,
.sym_idx = str_idx,
.reloc_type = SCC_X86_OPR_RELBR});
sccf_builder_add_reloc(&builder,
(sccf_reloc_t){.addend = 4,
.offset = 13,
.sect_type = SCCF_SECT_CODE,
.sym_idx = puts_idx,
.reloc_type = SCC_X86_OPR_RELBR});
const sccf_t *sccf = sccf_builder_to_sccf(&builder);
scc_pe_builder_t pe_builder;
sccf2pe(&pe_builder, sccf);
scc_pe_dump_to_file(&pe_builder, __FILE__ "/../../test.exe");
}