I am using Hutch's Masm32 package. The linker (ms incremental linker) has an /ALIGN option. msdn explains this option as:

"Syntax
/ALIGN:number

This option specifies the alignment of each section within the linear address space of the program. The number argument is in bytes and must be a power of two. The default is 4K. The linker issues a warning if the alignment produces an invalid image."

When I use this option (say /ALIGN:1024), the linker gives the following warning:

"LINK : warning LNK4108: /ALIGN specified without /DRIVER or /VXD; image may not run"

msdn explains this warning as:

"/ALIGN specified without /DRIVER or /VXD; image may not run

The /ALIGN option has been specified without also specifying either /DRIVER or /VXD.

Bad alignment can prevent the operating system from loading the final image. Do not use the /ALIGN option unless building a driver or vxd."

I noticed another thing about this option; it prevents uninitialized data allocation. Say, one defines 128K uninitialized data in the .data? section in the code. The final executable of this code is, say, 3.5K. When one uses the /ALIGN option, the final .exe works as before but becomes about 130K. I (using a pdf from msdn explaining pecoff) took out the redundant 128K data (bunch of zeros) from the 130K .exe file and set some parameters in the .exe (executable's size, data size, data section entry, everything I see relevant with the redundant data). The .exe I created using a hex editor did not work. I checked several times what I did, but the .exe I created seems ok. The quesitons I have:

- is there a pe-checker (or format checker) in the internet or something?
- is there anyone who has information about /ALIGN option?
- why do you think ms does not recommend to use the /ALIGN option unless you write driver or vxd? has it something to do with virtual page size of the executable?

Note: /ALIGN option attracts my attention because one can produce executables with almost no redundant information (I am not counting the stupidness of pecoff, since we have to use it). Here is something from me: The Smallest :) win32 executable (it shows a message and terminates, may be smaller if it only terminates :) but the former is more interesting I suppose)
Posted on 2003-07-28 11:35:00 by Jfcgauss
Don't Win9x OS's require a certain section alignment to load the PE file? That may be why MS doesn't want you to mess with the /ALIGN option.

If you don't use any dlls but the system ones, I believe that ret is equivalent to ExitProcess. If you replace that and re-do your import table, you can save quite a few bytes from your small Win32 exe (91 in this case, 12% of the original size)
Posted on 2003-07-28 14:00:34 by evwr
/FILEALIGN ???
Posted on 2003-07-28 14:02:42 by Delight
I remember from somewhere that a program should choose exitProcess to exit, but I dont remember where I read it. But yours seems to exit properly, also.

I could not find a /FILEALIGN option of the linker. It may be an undocumented option. What does it do?
Posted on 2003-07-28 18:05:21 by Jfcgauss
nuMiT_or's Small PE project may be of interest: flatpe. There you will find a link to Vecna's 276.exe. Here's the disassembled source in NASM syntax:
Posted on 2003-07-28 22:02:43 by Poimander
Jfcgauss,

Welcome to the win32asm forum,nice to see you here. :)

Some tweakings with the ALIGN option is possible,but you shouldn't use this trick
to get smaller executables,you need to preserve the usual file and section alignment
values proposed by Microsoft to maintain the compatibility with different versions
of Windows.The minimum size of a portable executable must be 1024 bytes. (DOS MZ+PE
header + 1 section)

So,how to get smaller executables?

i)Using a smaller DOS stub,
ii)Merging sections.(You can merge all the sections except the one for the resources)



\masm32\bin\link /SUBSYSTEM:WINDOWS /STUB:stub.exe /MERGE:.data=.text /MERGE:.idata=.text Msgbox.obj


The attachment contains an example.If you rebuild the example,don't mind the warnings
of the linker,there is not a problem with them.

PS:Derslerinde ba?ar?lar dilerim.
Posted on 2003-07-29 05:50:32 by Vortex
Jfcgauss,

If you want to use 1000h file alignment just add /opt:win98 to the linker commandline, /opt:nowin98 for 200h alignment (If you are using linker ver 5.xx this is used by default).
Posted on 2003-07-29 06:13:55 by Bi_Dark
There's a little app to change the alignment of an existing PE file. It seems to work fine, however I don't know what compatibility issues it might cause... although there shouldn't be much trouble using 4096 or 512 for the alignment (for fast loading and small executable, respectively). Just remember that if the linker is creating 4k aligned files, it's because they load much faster that way.

The program is called "Virogen's PE Realinger" and the link is http://virogen.cjb.net, but the site does not seem to work... perhaps you should search in google.
Posted on 2003-07-29 09:37:58 by QvasiModo