Anyone know which linker/assembler flag I need to make my exe's really small. Are .exe linked with the runtime library by default.

Posted on 2002-08-23 12:05:04 by timkempster
There is no magic "make the exe smaller" linker option!
You've got to consider the design of the application, there are tricks you can do in the code. And you've got to know how the PE executable works, as to where you can trim the fat.

The only thing you can do simply is to reduce the section size. The default section size on the linker that hutch-- provides with MASM has the minimum safe size as the default (512 bytes), while the linker that comes with Visual C++ has a default of 4096 bytes. So if you use the VC linker then you'll get smaller exes by setting the switch (I can't remember what it is, but the link /help should tell you) to 512.
Beyond this, you need to think as to how you can eliminate data sections, this can be done by merging section, by putting large data items in the .data? section rather than .data if possible (.data? is a separate data section, so in some cases it can be smaller to eliminate this section by shifting the variables into the .data section instead).

After that, all you can do is reduce the size of the code.

Posted on 2002-08-23 16:07:38 by Mirno
Use a binary format and write PE header manually.
There is a lot of space wasted between the end of the section headers and 512 offset in the file. Anyway, the minimum file size would be 512 bytes (header) + 512 (1 section ), but in memory it would take 4K + 4K

I couldn't make a valid PE image without any section.
But, the file itself would take 4-8K on the disk, depending on the size of partition.

check PECOFF.pdf on MS website
Posted on 2002-08-23 17:13:43 by Sergo
The link option is /FILEALIGN:512

Posted on 2002-08-23 17:22:26 by S/390
I would recommend against non standard PE formats as they may not work on all versions of Windows. The header which includes the MZ and PE headers takes 512 bytes anyway and if you program has any code at all, it will produce another section of 512 bytes so your effective minimum with very little code is 1024 bytes.

I agree with Mirno here, learn what CAN be done from the way PE files work and write your code accordingly. On very small files, avoiding a .DATA section drops the size by 512 bytes and if you must have initialised data like string data, you can write it directly into the code section. Any normal variables that require GLOBAL scope should be put in the uninitialised data section ( .DATA? ) as it takes up less space in the disk file.

The rest will be your coding style and how efficient you write it.

Posted on 2002-08-23 22:59:49 by hutch--
You can gain space merging the sections in only one, selecting the
proper charactheristics and set the file allign to 200h:

/FILEALIGN:0x200 / /MERGE:.rdata=.text /SECTION:.text,EWR

More dificult is writting a really flat PE. I found some sources and I did
tests until achieve good results using exclusively NASM.

Here are the codes. The zip includes two projects. One es for C, compiled
with mingw32 gcc free compiler and linked with MS LINK. The other is a
flat PE file assembled with NASM.
Posted on 2002-08-24 05:45:11 by n u M I T_o r