{"id":23256847,"url":"https://github.com/qfcy/machine-code-loader","last_synced_at":"2025-06-21T00:08:03.667Z","repository":{"id":268506806,"uuid":"903763538","full_name":"qfcy/machine-code-loader","owner":"qfcy","description":"Dynamic loading and execution of machine code in C++, resembling how operating systems load executables. C++动态加载和执行机器码，类似操作系统加载可执行文件。","archived":false,"fork":false,"pushed_at":"2025-01-03T13:28:22.000Z","size":250,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-06T04:28:05.238Z","etag":null,"topics":["c-plus-plus","dynamic-loading","executable-file","low-level-programming","machine-code","metaprogramming","operating-system-concepts"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/qfcy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-15T14:01:32.000Z","updated_at":"2025-03-03T03:58:02.000Z","dependencies_parsed_at":"2024-12-17T07:43:59.798Z","dependency_job_id":"8075420a-7ded-4e57-b4cb-267ec755c4fc","html_url":"https://github.com/qfcy/machine-code-loader","commit_stats":null,"previous_names":["qfcy/machine-code-loader"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/qfcy/machine-code-loader","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qfcy%2Fmachine-code-loader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qfcy%2Fmachine-code-loader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qfcy%2Fmachine-code-loader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qfcy%2Fmachine-code-loader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/qfcy","download_url":"https://codeload.github.com/qfcy/machine-code-loader/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qfcy%2Fmachine-code-loader/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261039105,"owners_count":23100974,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c-plus-plus","dynamic-loading","executable-file","low-level-programming","machine-code","metaprogramming","operating-system-concepts"],"created_at":"2024-12-19T12:19:52.834Z","updated_at":"2025-06-21T00:07:58.621Z","avatar_url":"https://github.com/qfcy.png","language":"C++","readme":"这是应用C++实现动态加载并执行机器码的项目，实现了类似操作系统加载可执行文件（如`.exe`）并执行的功能。  \n`.bin`文件格式是一种跨平台的可执行文件格式，一个`.bin`文件包含一个函数的机器码。  \n由于`.bin`文件仅依赖于通用的C标准库`RuntimeEnv`，在相同的处理器架构上（如x64），同一个`.bin`文件可以跨Windows和Linux等多种操作系统运行，具备了`.exe`等传统格式所没有的优势。  \n此外，`.bin`文件能加载其他`.bin`文件的代码并相互调用，实现导入模块的功能。  \n\n## 环境配置\n\n项目使用[gcc](https://gcc.gnu.org/)编译，此外还需要安装[Python](https://www.python.org)，用于运行`runtime_env_generator.py`。  \n\n## bin_dk.cpp\n\n主程序，在这里编写`.bin`文件的代码，类似Java的JDK。  \n用法: `bin_dk`，不带参数。  \n运行之后`bin_dk`会从`bin_dk.exe`自身提取函数的机器指令，生成`.bin`文件。  \n\n**bin文件的编写**  \n编写bin文件和编写普通C/C++程序相同，但目前需要注意：  \n- bin文件目前只能通过env调用外部函数，不能直接调用外部函数，因此无法使用C++的多数特性，甚至`new`、`delete`。(`static_cast`等部分不用调用外部函数的特性除外)\n- bin文件不支持定义在常量存储区的字符串，如`const char *s=\"test\";`，需要将常量字符串放在栈上分配，如`char s[]=\"test\";`，由于编译器会将栈上分配的字符串数据存放在代码段，嵌入机器码中。\n- `main`函数需要定义`DUMP_BIN`，或者`DUMP_BIN_SIZE`和`DUMP_BIN_MINSIZE`的宏，用来在编译后运行`bin_dk`时导出这些函数的机器码，生成bin文件。\n如果导出的bin文件过小，运行时会出现段错误。可以通过在`DUMP_BIN_MINSIZE`中增加导出大小来解决。\n- bin文件函数的参数是任意的，但如果要作为主程序运行，参数必须是`(int argc,const char *argv[],RuntimeEnv *env)`。不是这个参数的bin文件能被其他bin文件导入，但不能单独作为主程序运行。\n`env`的作用是提供C的标准库函数，如`malloc`，`fopen`等。完整的支持函数列表参见`runtime_env.h`或`runtime_env_generator.py`。\n\n这是一个示例，输出Hello world：  \n```cpp\n#include \"bin_dk.h\"\n\nint main_bin(int argc,const char *argv[],RuntimeEnv *env){ // 真正的.bin文件的入口函数\n    char msg[]=\"Hello world!\\n\";\n    env-\u003eprintf(msg);\n    return 0;\n}\nint main() { // 仅用于导出机器码到.bin文件\n    DUMP_BIN(main_bin);\n    return 0;\n}\n```\n**一些RuntimeEnv的特有函数和常量**\n- `env-\u003eversion`: 获取当前运行时的版本，如`env-\u003eversion.major`, `env-\u003eversion.minor`, `env-\u003eversion.revision`。\n- `env-\u003eplatform`: 当前运行的平台，目前有`WIN32_`, `POSIX`和`UNKNOWN`。\n- `int env-\u003eimport(const char *modname)`: 导入外部的bin文件作为函数使用，`modname`的格式可以是`module`,`module.bin`,`path/module`,`path/module.bin`的任意一种。导入成功时返回`IMPORT_SUCCESS`，失败时返回其他值，具体值参考`constants.h`。\n- `void* env-\u003egetFunc(const char *funcname)`: 获取导入的外部bin文件的函数指针，失败时返回`nullptr`。\n- `void* env-\u003egetLibraryFunc(const char *libname, const char *funcname)`: 获取外部动态库(dll或so文件)的函数，libname是动态库的文件名，funcname是函数名，失败时返回`nullptr`。\n动态库会在第一次调用`getLibraryFunc`时自动加载，无需手动加载。\n- `void env-\u003efreeLibrary(const char *libname)`: 显式释放加载的动态库，释放后如果再次用相同库调用`getLibraryFunc`，库会被重新加载。\n- `void env-\u003edebugModuleInfo()`: 向`stdout`输出当前已加载的其他bin文件模块，和加载的动态库的信息。\n- `void env-\u003estackTrace()`: 向`stderr`输出当前堆栈信息。\n\n## bin_runtime.cpp\n\n负责运行`.bin`文件的程序，提供了C标准库的运行环境，类似Java的JRE。  \n命令行：\n```\nbin_runtime \u003c主程序bin文件\u003e [传递给bin文件的参数1 参数2 ...]\n```\n`bin_runtime`检测到段错误时，会自行处理错误并输出调试信息。  \n\n## 部分其他文件\n\n- `make.bat`: Windows上构建项目的脚本，不带参数运行。\n- `bin_dk.h`: `bin_dk.cpp`开头必须包含的头文件。\n- `runtime_env_generator.py`: 用于生成`runtime_env.h`头文件。由于`runtime_env.h`包含的标准库函数过多，难以维护，这里用了Python脚本自动生成`runtime_env.h`。\n- `constants.h`: 包含一些常量以及类型。","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqfcy%2Fmachine-code-loader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqfcy%2Fmachine-code-loader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqfcy%2Fmachine-code-loader/lists"}