feat(ast): 添加汇编器模块并改进AST定义和IR转换
- 在README.md中添加asm汇编器模块说明 - 更新ast_def.h中的枚举注释,添加sema相关信息以明确语义分析作用域 - 重命名函数参数param_types为params,使命名更清晰 - 移除call表达式中的_target字段,简化结构 - 为member、identifier等字段添加///< fill by sema注释说明填充时机 - 为jump语句添加_target字段用于语义分析 - 更新所有AST初始化函数,接受位置信息参数以改进错误定位 - 修复alignof表达式的类型应为ALIGN_OF而非SIZE_OF的问题 - 重构ast2ir.h,引入scc_ast2ir_ctx_t上下文结构体统一管理转换状态 - 添加符号表、节点到IR映射等必要的转换上下文信息
This commit is contained in:
@@ -1,101 +0,0 @@
|
||||
#include <scc_pe_builder.h>
|
||||
#include <scc_pe_idata.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
const char data[] = "Hello, World from SCC PE Builder!\n\0";
|
||||
|
||||
/* clang-format off */
|
||||
const 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 */
|
||||
scc_pe_builder_t builder = {0};
|
||||
scc_pe_builder_init(&builder, true, 4096, 512);
|
||||
scc_pe_reserve_header(&builder, 3);
|
||||
scc_pe_section_range code_range = scc_pe_reserve_section_header(
|
||||
&builder, (BYTE *)".text\0\0",
|
||||
IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE,
|
||||
sizeof(code), sizeof(code));
|
||||
|
||||
scc_pe_section_range data_range = scc_pe_reserve_section_header(
|
||||
&builder, (BYTE *)".data\0\0",
|
||||
IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ |
|
||||
IMAGE_SCN_MEM_WRITE,
|
||||
sizeof(data), sizeof(data));
|
||||
|
||||
scc_winpe_idata_builder_t idata_builder;
|
||||
scc_winpe_idata_lib_vec_t idata_libs;
|
||||
scc_vec_init(idata_libs);
|
||||
scc_winpe_idata_lib_t ucrtbase;
|
||||
ucrtbase.name = "ucrtbase.dll";
|
||||
scc_vec_init(ucrtbase.symbol_names);
|
||||
scc_vec_push(ucrtbase.symbol_names, "puts");
|
||||
scc_vec_push(idata_libs, ucrtbase);
|
||||
scc_pe_idata_builder_init(&idata_builder, &idata_libs);
|
||||
u32 idata_size = scc_pe_reserve_idata(&idata_builder);
|
||||
scc_pe_section_range idata_range = scc_pe_reserve_section_header(
|
||||
&builder, (BYTE *)".idata\0",
|
||||
IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ |
|
||||
IMAGE_SCN_MEM_WRITE,
|
||||
idata_size, idata_size);
|
||||
scc_vec_at(builder.image_data_directory_vec, IMAGE_DIRECTORY_ENTRY_IMPORT) =
|
||||
(IMAGE_DATA_DIRECTORY){.VirtualAddress = idata_range.virual_address,
|
||||
.Size = idata_range.virual_size};
|
||||
scc_pe_buffer_t idata_buffer =
|
||||
scc_pe_construct_idata(&idata_builder, &idata_range);
|
||||
|
||||
u32 entry_point_offset = 0;
|
||||
u64 base_address = 0x140000000;
|
||||
u32 entry_point = code_range.virual_address + entry_point_offset;
|
||||
scc_pe_config_t config = (scc_pe_config_t){
|
||||
.machine = IMAGE_FILE_MACHINE_AMD64,
|
||||
.time_date_stamp = 0,
|
||||
.characteristics =
|
||||
IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_LARGE_ADDRESS_AWARE,
|
||||
.major_linker_version = 14,
|
||||
.minor_linker_version = 0,
|
||||
.address_of_entry_point = entry_point,
|
||||
.image_base = base_address,
|
||||
.major_operating_system_version = 6,
|
||||
.minor_operating_system_version = 0,
|
||||
.major_image_version = 0,
|
||||
.minor_image_version = 0,
|
||||
.major_subsystem_version = 6,
|
||||
.minor_subsystem_version = 0,
|
||||
.subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI,
|
||||
.dll_characteristics = IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA |
|
||||
IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE |
|
||||
IMAGE_DLLCHARACTERISTICS_NX_COMPAT,
|
||||
.size_of_stack_reserve = 0x100000,
|
||||
.size_of_stack_commit = 0x1000,
|
||||
.size_of_heap_reserve = 0x100000,
|
||||
.size_of_heap_commit = 0x1000,
|
||||
};
|
||||
|
||||
// FIXME 需要确保宿主机为小端
|
||||
*(u32 *)((u8 *)code + 7) =
|
||||
(data_range.virual_address + 0) - (code_range.virual_address + 7 + 4);
|
||||
*(u32 *)((u8 *)code + 13) =
|
||||
(u64)scc_pe_idata_get_symbol_rva(&idata_builder, "puts") -
|
||||
(code_range.virual_address + 13 + 4);
|
||||
|
||||
scc_pe_write_header(&builder, &config);
|
||||
scc_pe_write_section(&builder, &code_range, (u8 *)code, sizeof(code));
|
||||
scc_pe_write_section(&builder, &data_range, (u8 *)data, sizeof(data));
|
||||
scc_pe_write_section(&builder, &idata_range,
|
||||
scc_vec_unsafe_get_data(idata_buffer),
|
||||
scc_vec_size(idata_buffer));
|
||||
scc_pe_dump_to_file(&builder, "hello_world.exe");
|
||||
}
|
||||
@@ -13,9 +13,9 @@ image_import_descriptor_init(u32 import_lookup_table_rva, u32 name_rva,
|
||||
return iid;
|
||||
};
|
||||
|
||||
static void scc_winpe_hnt_builder_push(scc_winpe_hnt_builder_t *builder,
|
||||
static void scc_winpe_hnt_builder_push(scc_pe_hnt_builder_t *builder,
|
||||
const char *name, u16 hint);
|
||||
static void scc_winpe_hnt_builder_init(scc_winpe_hnt_builder_t *builder) {
|
||||
static void scc_winpe_hnt_builder_init(scc_pe_hnt_builder_t *builder) {
|
||||
scc_vec_init(builder->data);
|
||||
builder->section_offset = 0;
|
||||
scc_hashtable_init(&builder->str_map,
|
||||
@@ -23,7 +23,7 @@ static void scc_winpe_hnt_builder_init(scc_winpe_hnt_builder_t *builder) {
|
||||
(scc_hashtable_equal_func_t)scc_strcmp);
|
||||
}
|
||||
|
||||
static void scc_winpe_hnt_builder_push(scc_winpe_hnt_builder_t *builder,
|
||||
static void scc_winpe_hnt_builder_push(scc_pe_hnt_builder_t *builder,
|
||||
const char *name, u16 hint) {
|
||||
///< 可选哈希表去重
|
||||
if (scc_hashtable_get(&builder->str_map, name)) {
|
||||
@@ -49,13 +49,13 @@ static void scc_winpe_hnt_builder_push(scc_winpe_hnt_builder_t *builder,
|
||||
Assert(scc_vec_size(builder->data) % 2 == 0);
|
||||
}
|
||||
|
||||
static u64 scc_winpe_hnt_get_offset(scc_winpe_hnt_builder_t *builder,
|
||||
static u64 scc_winpe_hnt_get_offset(scc_pe_hnt_builder_t *builder,
|
||||
const char *name) {
|
||||
return (u64)scc_hashtable_get(&builder->str_map, name);
|
||||
}
|
||||
|
||||
void scc_pe_idata_builder_init(scc_winpe_idata_builder_t *builder,
|
||||
scc_winpe_idata_lib_vec_t *idata_libs) {
|
||||
void scc_pe_idata_builder_init(scc_pe_idata_builder_t *builder,
|
||||
scc_pe_idata_lib_vec_t *idata_libs) {
|
||||
scc_vec_init(builder->buffer);
|
||||
builder->idata_libs = *idata_libs;
|
||||
|
||||
@@ -66,7 +66,7 @@ void scc_pe_idata_builder_init(scc_winpe_idata_builder_t *builder,
|
||||
(scc_hashtable_equal_func_t)scc_strcmp);
|
||||
}
|
||||
|
||||
u32 scc_pe_reserve_idata(scc_winpe_idata_builder_t *builder) {
|
||||
u32 scc_pe_reserve_idata(scc_pe_idata_builder_t *builder) {
|
||||
u32 idata_size = (scc_vec_size(builder->idata_libs) + 1) *
|
||||
sizeof(IMAGE_IMPORT_DESCRIPTOR);
|
||||
|
||||
@@ -89,7 +89,7 @@ u32 scc_pe_reserve_idata(scc_winpe_idata_builder_t *builder) {
|
||||
return idata_size;
|
||||
}
|
||||
|
||||
scc_pe_buffer_t scc_pe_construct_idata(scc_winpe_idata_builder_t *builder,
|
||||
scc_pe_buffer_t scc_pe_construct_idata(scc_pe_idata_builder_t *builder,
|
||||
scc_pe_section_range *idata_range) {
|
||||
u32 idata_rva = idata_range->virual_address;
|
||||
u32 hnt_offset = builder->hnt_builder.section_offset;
|
||||
|
||||
0
libs/target/pe/src/scc_pe_reloc.c
Normal file
0
libs/target/pe/src/scc_pe_reloc.c
Normal file
Reference in New Issue
Block a user