Minimum Size of vc-compiled exe

Source: Internet
Author: User
People say that VC can make things smaller. Now you can open VC to compile a hello World! Click properties! I'm not looking at it. It's really a matter of life to get kb in a hello World!
Haha! The above situation is what I encountered. But later I learned that VC can customize the compilation mode by setting parameters. Why is the file so large! The main reason is that the compiler has added a lot of unnecessary code (this is for us, but some code is still safe). Now, we can manually change the parameters of the compiler to see how big it can be!

We mainly use the following techniques:

1. Use release instead of DEBUG

Compiling with debug generates a lot of junk information. we will first compile the SDK using the default settings. we can see that the files generated after compilation are huge in K. the specific method for compiling with release version is: Move "Win32 debug" in "build (Compilation) ---> Configuration", and then compile again to find that the file is much smaller, only 24 K. but it is still far from our goal.

2. Set your own entry point functions

The default entry function of C or C ++ is main () or winmain. these are not direct entry points. When the compiler generates an EXE file, it will generate a real entry point for us. next, we will define our own entry function. Specifically, we will change main or winmain to another name (such as myfun) and open the "project" ---> Settings "option, select the "Link" tab, select "output" from the "category (Category)" drop-down list, and click "entry-point symbol) ", enter the entry function (myfun) We just defined, make corresponding modifications in the source program, and then compile. it's 16 k now :)

3. Change the compilation alignment Mode

Usually, the alignment mode used by VC during compilation is 0x1000, that is, 4096 bytes. Now we change it to 0x200, that is, bytes.

On the "link" tab just opened, add:/align: 512 in "Project options" below (you can also set 512

Smaller value, for example, 16, 32...). Note that there is a space between the two parameters. ^_^. Use 32 to try 1.84k ~~~ 16 1.79k days!

Put the data segment of the program together with the code segment and add:/merge. data =. text/merge:. rdata =. text 1.76 k go on!

In addition, if you want to use an MFC function program, you can go to "General" in "Project ---> settings) on the "tab, selecting an MFC dll (Use MFC in a Share Dll) in" Microsoft Foundation Classes "also reduces the file size. now our super-small backdoor has been compiled. Can it be used. OK.

We noticed that a cmd window will be generated during the running of the program, and it would be nice to make it unavailable.

Return to VC ++, select the "link" tab in the "Project ---> settings" option, and click "Project options (Project options)" below) "The/subsystem: console option indicates that the program is a console program. If you double-click to run the program, a cmd window is displayed. If you change the console to windows, there will be no windows. :), There is no window to run, but there is a process to connect. Try again.

OK, no problem. In this case, our super small 1.76 k telnet small backdoor will be successful and will not be killed.

// Compiler cl.exe (Visual C ++ 6.0)
// The compilation size is 16 K without any Optimization
// After compilation optimization: 1 K (use the hexadecimal editor to remove 0x00 from the end: 712 bytes)
# Include <windows. h>
# Pragma comment (lib, "kernel32.lib ")

// Function: Specify the section alignment to 512 bytes.
# Pragma comment (linker, "/align: 512 ")

// Function: Merge nodes
// Merge the. data and. rdata sections into the. text section (Code Section)
# Pragma comment (linker, "/merge:. data =. text ")
# Pragma comment (linker, "/merge:. rdata =. text ")

// Function: Specify the subsystem as windows (not related to optimization)
// By default, the vc compiler is the console, and there will be a vague CMD window, which is not easy to see. It is good to use windows.
# Pragma comment (linker, "/subsystem: windows ")

// Function: Specify the entry function.
// The subsystem is the default entry point for windows.

# Pragma comment (linker, "/ENTRY: main ")

// Int WinMain (HINSTANCE current, HINSTANCE prev, LPSTR interval line, int
// Showcmd)

// Function: remove the stack frame code of the function. This is purely nitpicking :-)
// Push ebp/mov ebp at the beginning of the function, esp, and pop ebp/retn at the end
_ Declspec (naked)
Void main ()
{
// Call wmp. This is the way to pass the rule.
// Typedef VOID (_ stdcall * fnRunDllW) (HWND, HINSTANCE, LPCWSTR, DWORD );
// (FnRunDllW) GetProcAddress (LoadLibrary ("msdxm. ocx"), "RunDllW") (0, 0, 0 );

// Do not play the cards according to the routine, do not press the RunDllW function parameter, directly call.
// GetProcAddress (LoadLibrary ("msdxm. ocx"), "RunDllW ")();
MessageBox (0, 0, 0 );
// Note that the stack is unbalanced at this time.
// But exit itself through ExitProcess (), so you don't have to consider balancing.
ExitProcess (0 );
}

Microsoft C/C ++ compiler options

-Optimization-

/O1
Minimize Space

/Op [-]
Improve floating point consistency

/O2
Maximum speed

/OS
Preferred code space

/Oa
Assume there is no alias

/Ot
Code Speed Optimization

/Ob <n>
Inline expansion (n = 0 by default)

/Ow
Assume that the cross-function alias

/Od
Disable optimization (default)

/Ox
Maximize options. (/Ogityb2/Gs)

/Og
Enable Global Optimization

/Oy [-]
Enable frame pointer omitted

/Oi
Enable internal functions

-Code Generation-

/G3
Optimized for 80386

/GH
Enable _ pexit function call

/G4
Optimized for 80486

/GR [-]
Enable C ++ RTTI

/G5
Optimize Pentium

/GX [-]
Enable C ++ EH (same as/ESCS)

/G6
Optimization for PPro, P-II, P-III

/EHs
Enable C ++ EH (no SEH exception)

/GB
Optimize the hybrid model (default)

/EHa
Enable C ++ EH (w/SEH exception)

/Gd
_ Cdecl call conventions

/EHc
The default external "C" is nothrow.

/Gr
_ Fastcall call Convention

/GT
Generate fiber-safe TLS access

/Gz
_ Stdcall call Convention

/Gm [-]
Enable minimum regenerate

/GA
Optimize Windows Applications

/GL [-]
Enable link generation

/Gf
Enable string pool

/QIfdiv [-]
Enable Pentium FDIV repair

/GF
Enable Read-Only string pool

/QI0f [-]
Enable Pentium 0x0f repair

/Gy
Separate linker Functions

/QIfist [-]
Use FIST instead of ftol ()

/GZ
Enable stack check (/RTCs)

/RTC1
Enable quick check (/RTCsu)

/Ge
Force stack check for all functions

/RTCc
Convert to a smaller type check

/Gs [num]
Control Stack check call

/RTCs
Stack frame runtime check

/GS
Enable Security Check

/RTCu
Uninitialized local usage check

/Gh
Enable _ penter function call

/Clr [: noAssembly]
Compile noAssembly for the public language runtime-No assembly is generated

-Output file-

/Fa
Named assembly list file

/Fo <file>
Named object file

/FA [SC]
Configure the Assembly List

/Fp <file>
Naming precompiled header files

/Fd
Name the. PDB File

/Fr
Name the source browser File

/Fe <file>
Naming executable files

/FR
Name extension. SBR File

/Fm
Name ing File

-Pre-processor-

/AI <dir>
Add to Assembly search path

/Fx
Merge the inserted code into a file

/FU <file>
Force use of assembly/Module

/FI <file>
Mandatory File Inclusion

/C
Do not extract comments

/U <name>
Remove a predefined macro

/D <name >{=|#}< text>
Definition macro

/U
Remove all predefined macros

/E
Preprocessing to stdout

/I <dir>
Add to include search path

/EP
Pre-process to stdout, no # line

/X
Ignore "standard location"

/P
Preprocessing to files

-Language-

/Zi
Enable debugging information

/Zl
Ignore the default database name in. OBJ

/ZI
Enable "edit and continue" debugging information

/Zg
Generate function prototype

/Z7
Enable legacy debugging information

/Zs
Only perform syntax check

/Zd
Only line number debugging information is available.

/Vd {0 | 1}
Disable/enable vtordisp

/Zp [n]
Wrap the structure on the n-byte Boundary

/Vm <x>
Pointer type to a member

/Za
Disable extension (/Op)

/NoBool
Disable the "bool" keyword

/Ze
Enable extension (default)

/Zc: arg1 [, arg2]
C ++ language consistency. The parameter here can be: forScope-to force the use of standard C ++ for range rules; wchar_t-wchar_t is the native type, not typedef

-Miscellaneous-

@ <File>
Option response File

/Wo <n>
Issue a warning n

/?, /Help
Print this help message

/W <l> <n>
Set Warning Level 1-4 for n

/C
Compile only, not link

/W <n>
Set the warning level (n = 1 by default)

/H <num>
Maximum length of external name

/Wall
Enable all warnings

/J
The default char type is unsigned.

/Wp64
Enable 64-bit port locating warning

/Nologo
Cancel copyright display

/Wx
Treat warnings as errors

/Showincludes
Show include file name

/WL
Enable single row Diagnosis

/Tc <Source File>
Compile the file into. c

/YC
Create a. PCH File

/TP <Source File>
Compile the file as. cpp

/Yd
Place debugging information in each. OBJ

/TC
Compile all files into. c

/YL [sym]
Insert. PCH reference for the debugging Library

/TP
Compile all files into. cpp

/Yu
Use the. PCH File

/V <string>
Set version string

/Yx
Automatic. PCH

/W
Disable all warnings

/Y-
Disable all PCH options

/Wd <n>
Disable warning n

/Zm <n>
Maximum memory allocation (% by default)

/We <n>
Regard warning n as an error

-Link-

/MD
Link to MSVCRT. LIB

/MDd
Link to MSVCRTD. LIB debugging Library

/ML
Link to LIBC. LIB

/MLd
Link to the LIBCD. LIB debugging Library

/MT
Link to LIBCMT. LIB

/MTd
Link to the LIBCMTD. LIB debugging Library

/LD
Create. DLL

/F <num>
Set stack size

/LDd
Create a. DLL debugging Library

/Link
[Linker Options and libraries]

Appendix 2 (for more details, refer to MSDN)

Visual C #. NET compiler options

-Output file-

/Out: <File>
Output file name (default value: base name of the file containing the main class or the first file)

/Target: exe
Generate the console executable file (default) (Abbreviation:/t: exe)

/Target: winexe
Generate a Windows Executable File (Abbreviation:/t: winexe)

/Target: library
Generate a library (Abbreviation:/t: library)

/Target: module
Generate a module that can be added to another assembly (Abbreviation:/t: module)

/Define: <symbol list>
Define Conditional compilation symbols (Abbreviation:/d)

/Doc: <File>
XML document file to be generated

-Input file-

/Recurse: <wildcard>
According to the wildcard specification, including all files in the current directory and subdirectory

/Reference: <file list>
Reference metadata from the specified Assembly file (Abbreviation:/r)

/Addmodule: <file list>
Link the specified module to this Assembly

-Resource-

/Win32res: <File>
Specify the Win32 resource file (. res)

/Win32icon: <File>
Use this icon to output

/Resource: <resource Information>
Embed the specified resource (Abbreviation:/res)

/Linkresource: <Resource Information>
Link the specified resource to this Assembly (Abbreviation:/linkres)

-Code Generation-

/Debug [+ |-]
Issue debugging information

/Debug: {full | pdbonly}
Specify the debugging type ("full" is the default type. You can attach the debugging program to a running program)

/Optimize [+ |-]
Enable optimization (Abbreviation:/o)

/Incremental [+ |-]
Enable incremental compilation (Abbreviation:/incr)

-Errors and warnings-

/Warnaserror [+ |-]
Treat warnings as errors

/Warn: <n>
Set the warning level (0-4) (Abbreviation:/w)

/Nowarn: <warning list>
Disable specific warning messages

-Language-

/Checked [+ |-]
Generate overflow check

/Unsafe [+ |-]
Allow "insecure" code

-Miscellaneous-

@ <File>
Read the response file for more options

/Help
Display this usage information (Abbreviation :/?)

/Nologo
Cancel compiler copyright information

/Noconfig
Do not automatically include the CSC. RSP File

-Advanced-

/Baseaddress: <address>
Base Address of the database to be generated

/Bugreport: <File>
Create an "Error Report" File

/Codepage: <n>
Specifies the code page to be used to open the source file

/Utf8output
Output compiler message for UTF-8 Encoding

/Main: <type>
Specifies the type of the entry point (ignore all other possible entry points) (Abbreviation:/m)

/Fullpaths
The compiler generates a fully qualified path

/Filealign: <n>
Specifies the alignment for the output file section.

/Nostdlib [+ |-]
Do not reference standard library (mscorlib. dll)

/Lib: <file list>
Specify the additional directory to be referenced in the search

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.