Removing RunTime Library/Initialization CRT

Started by Irwin, October 26, 2007, 11:18:50 PM

Previous topic - Next topic

Irwin

As the topic says, I don't want to use any run-time library/initialization CRT, in low-level terms; I don't want any initialization code (no SEH installation, no main/winmain initializers), I want access straight at the entry point without any useless code.

Thanks :)

EDIT: Got it :) /NODEFAULTLIB flag on linking :D

codamateur

Not sure it would be enough to use this setting. Any problem of entry point at compilation?

Irwin

I'm not having any problems at all but I did use "/entry:main" as a precautionary step before when linking :P It works fine and goes straight to my main:

void main()
{
    ExitProcess(WinMain(GetModuleHandle(NULL), NULL, NULL, SW_NORMAL));
}

;----------

1337020C <ModuleEntryPoint>    /$  55               push    ebp
1337020D                       |.  89E5             mov     ebp, esp
1337020F                       |.  6A 00            push    0                                                    ; /pModule = NULL
13370211                       |.  FF15 60023713    call    near dword ptr ds:[<&KERNEL32.GetModuleHandleA>]     ; \GetModuleHandleA
13370217                       |.  6A 01            push    1                                                    ; /Arg4 = 00000001
13370219                       |.  6A 00            push    0                                                    ; |Arg3 = 00000000
1337021B                       |.  6A 00            push    0                                                    ; |Arg2 = 00000000
1337021D                       |.  50               push    eax                                                  ; |Arg1
1337021E                       |.  E8 DDFFFFFF      call    hello.13370200                                       ; \hello.13370200
13370223                       |.  50               push    eax                                                  ; /ExitCode
13370224                       \.  FF15 64023713    call    near dword ptr ds:[<&KERNEL32.ExitProcess>]          ; \ExitProcess
1337022A                        .  5D               pop     ebp
1337022B                        .  C3               retn


I'm sure a few will get a laugh out of the image-base but it's my trade-mark, it also helps make ripping impossible :P

TimoVJL

#3
Using linkers predefined name

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#pragma nodefaultlib
#pragma comment(linker, "/MERGE:.data=.text")

void __cdecl WinMainCRTStartup(void) // windows
//void __cdecl mainCRTStartup( void ) // console
{
ExitProcess(WinMain(GetModuleHandle(NULL), NULL, NULL, SW_NORMAL));
}

int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
return 0;
}

Edit: added #pragma comment(linker, "/MERGE:.data=.text")

May the source be with you

codamateur

What is the size of the binaries of the program after compilation?

Vortex

Irwin,

I am using my own tiny run-time library to reduce the size of executables. You can reduce the size of a typical hello world application to 2048 bytes ( and even more. )

http://www.smorgasbordet.com/forum/index.php?topic=88.0
Code it... That's all...

codamateur

#6
I m a "do it smaller" freak. a simple hello world program can be done in 1024 bytes and be win32 compatible, nothing new for you i know :)

Vortex

codamateur,

I am a member of assembly forums and I saw a lot of "world's smallest Win32 PE demo"s  Creating the smallest executable in every occasion would not bring you much benefit as you would need extra time to do special optimization tricks to reduce the size with not much compensation for your efforts. I think it's a better idea to focus on algorithm design where execution speed of your code is critical.
Code it... That's all...

Irwin

Thank you for your replies everyone :)

timovjl: I actually knew of that with MSVC++ but didn't know it applied to Pelles, thanks :)

codamateur: The initial size of the PE is 1024bytes (1kb) but that's only because I merge all sections (reloc table on DLLs is an exception) and also finely align the PE.

Vortex: Hey there, I've seen you from MASM forums. It's good to see that I'm not the only ASM programmer lurking these forums :P Anyway, thanks for the link to your run-time library but I doubt I'll be using it any time soon since I like to be in control of *all* of my code and maintain maximum flexibility.

Again, thanks for the replies :)

Vortex

Hi Irwin,

You are not alone :)

No problem. I appreciate your efforts to get full control on your code. My run-time library is a simple example demonstrating the combination of asm and C.
Code it... That's all...