使用 Visual Studio 创建一个大小为 100 字节的 C 程序 [英] Create a C program of size 100 bytes with Visual Studio

查看:31
本文介绍了使用 Visual Studio 创建一个大小为 100 字节的 C 程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个 C 应用程序,它在构建时将创建一个大小为 100 字节或更少的可执行文件.

I want to write a C application program which upon building will create an executable of size 100 bytes or less.

即使我用一个空的 main() 创建一个简单的 C 程序,我的输出文件在 Visual Studio 2015 上也会变成 11KB.有没有办法告诉 VS 不包含任何默认库将减少我的可执行文件大小.或者有没有其他方法可以减小可执行文件的大小?

Even if I create a simple C program with just an empty main(), my output file becomes 11KB on Visual Studio 2015. Is there a way to tell VS not to include any default libs which will reduce my executable size. Or is there any other way to reduce the executable file size?

推荐答案

您可以为 hello world 创建一个文本部分(即包含可执行代码的部分)小于 100 字节的可执行文件 控制台输出,但 .EXE 文件大小会更大,因为它需要具有有效的 PE 格式结构,并且该格式需要相当多的填充./NODEFAULT 当然会给你错误.然后,您必须重新实现缺少的任何内容,通常会使事情变得无操作,并且您需要使用链接时代码生成,以便删除对空函数的所有调用(以及函数本身).您还需要找到禁用所有酷"功能的编译器标志.例如.要使编译器停止发出缓冲区安全检查 (__security_check_cookie & al.),请提供 /GS- 选项.

You can create an executable with the text section (i.e. the section that has the executable code in it) of less than 100 bytes for a hello world console output, but the .EXE file size will be larger because it needs to have a valid PE format structure, and that format requires quite a bit of padding. /NODEFAULT will of course give you errors. You'll then have to reimplement whatever's missing, usually making things no-op, and you'll need to use link-time code generation so that all the calls to the empty functions get removed (as well as the functions themselves). You'll also need to find compiler flags that disable all "cool" features. E.g. to make the compiler stop emitting buffer security checks (__security_check_cookie & al.), provide the /GS- option.

您可能需要使用自定义工具将 .EXE 文件从 VS 链接器发出的不必要的 PE 垃圾中完全剥离.并且您的可执行文件仍将至少与 KERNEL32.DLL 进行运行时链接,因为没有它,您将无法做任何有用的事情.如果你够勇敢,你可以直接使用 NTDLL.DLL(即原生 API),但这可能需要超过 100 字节的代码.

You'll probably need to use a custom tool to completely strip the .EXE file from unnecessary PE cruft that VS linker emits. And your executable will still be runtime-linked with at least KERNEL32.DLL, since without that you won't be able to do anything useful. If you're brave, you could use NTDLL.DLL (i.e. the native API) directly, but that will probably need more than 100 bytes of code to.

您的可执行文件还需要面向 32 位架构;64 位将大 25% 左右(以如此小的部分大小开始).

Your executable will also need to be targeting the 32 bit architecture; 64-bit one will be about 25% larger (at such small section sizes to start with).

这是一个很好的挑战.

这篇关于使用 Visual Studio 创建一个大小为 100 字节的 C 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆