文件
Paze-C-CPP-Compiler/tools/pcc-asm.c
T
Paze AI 105c1f6f6a feat: bounds-checking/pcc-asm/pcc-impdef/pcc-get/交叉编译 五项功能
1. -b bounds-checking 内存安全
   - 编译 bcheck.o/bt-exe.o/bt-log.o 运行时
   - 数组越界/野指针检测,精确定位文件和行号
   - 测试: strcpy 溢出和数组越界均被捕获

2. pcc-asm 汇编查看工具 (bin/pcc-asm.exe)
   - 反汇编 pcc 生成的二进制 (调用 llvm-objdump)
   - 查看 x86-64 机器码与汇编指令

3. pcc-impdef DLL导入工具 (bin/pcc-impdef.exe)
   - 解析 PE 导出表生成 .def 文件
   - 测试: kernel32.dll 1693 个导出全部正确解析

4. pcc-get 包管理器 (bin/pcc-get.exe)
   - install/remove/list/root 命令
   - 支持本地包目录和 URL 下载安装
   - 安装头文件到 include/、库到 lib/
   - 测试: mymath 包安装→编译→链接→运行 全流程

5. 交叉编译演示
   - 构建 ARM64 交叉编译器 (pcc-arm64.exe)
   - 构建 RISC-V 交叉编译器 (pcc-riscv64.exe)
   - 生成 elf64-littleaarch64 / elf64-littleriscv 对象
   - 反汇编验证: 真正的 ARM64/RISC-V 机器码
2026-08-16 15:16:35 +08:00

110 行
2.7 KiB
C

/*
* pcc-asm.c - disassemble a PCC-produced binary
*
* Shows the x86-64 assembly of a compiled program.
* Uses llvm-objdump if available, else objdump.
*
* Usage:
* pcc-asm program.exe disassemble all code
* pcc-asm program.exe main disassemble, filter by symbol
*
* License: MIT
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
# include <windows.h>
#endif
static int file_exists(const char *f)
{
FILE *p = fopen(f, "rb");
if (p) { fclose(p); return 1; }
return 0;
}
static const char *find_objdump(void)
{
/* common locations, first match wins */
static const char *cands[] = {
"llvm-objdump.exe",
"objdump.exe",
"C:\\Program Files\\LLVM\\bin\\llvm-objdump.exe",
"C:\\Clang+LLVM-22.1.7\\bin\\llvm-objdump.exe",
"D:\\Clang+LLVM-22.1.7\\bin\\llvm-objdump.exe",
"/usr/bin/objdump",
"/usr/bin/llvm-objdump",
NULL
};
int i;
for (i = 0; cands[i]; i++)
if (file_exists(cands[i]))
return cands[i];
return "objdump"; /* hope it's in PATH */
}
int main(int argc, char **argv)
{
const char *objdump = find_objdump();
const char *file;
const char *filter = NULL;
char cmd[1024];
int len;
if (argc < 2) {
fprintf(stderr,
"pcc-asm - disassemble a PCC binary\n"
"\n"
"Usage:\n"
" pcc-asm program.exe disassemble all code\n"
" pcc-asm program.exe main disassemble, filter by symbol\n"
"\n"
"Uses %s\n", objdump);
return 1;
}
file = argv[1];
if (argc > 2)
filter = argv[2];
if (!file_exists(file)) {
fprintf(stderr, "pcc-asm: cannot open '%s'\n", file);
return 1;
}
#ifdef _WIN32
len = snprintf(cmd, sizeof cmd, "\"%s\" -d \"%s\"", objdump, file);
#else
len = snprintf(cmd, sizeof cmd, "%s -d \"%s\"", objdump, file);
#endif
if (filter) {
/* use objdump's symbol filter if supported, else grep-style */
len += snprintf(cmd + len, sizeof cmd - len, " | findstr /c:\"%s\"", filter);
}
#ifdef _WIN32
{
STARTUPINFOA si;
PROCESS_INFORMATION pi;
char *cmdline = (char*)malloc(strlen(cmd) + 1);
if (!cmdline) return 1;
strcpy(cmdline, cmd);
memset(&si, 0, sizeof si);
si.cb = sizeof si;
if (CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
free(cmdline);
}
#else
system(cmd);
#endif
return 0;
}