NO

Author Topic: Removing RunTime Library/Initialization CRT  (Read 3302 times)

Irwin

  • Guest
Removing RunTime Library/Initialization CRT
« on: October 26, 2007, 11:18:50 pm »
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
« Last Edit: October 27, 2007, 01:38:10 am by Irwin »

codamateur

  • Guest
Re: Removing RunTime Library/Initialization CRT
« Reply #1 on: October 27, 2007, 01:44:22 am »
Not sure it would be enough to use this setting. Any problem of entry point at compilation?

Irwin

  • Guest
Re: Removing RunTime Library/Initialization CRT
« Reply #2 on: October 27, 2007, 02:14:31 am »
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:

Code: [Select]
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

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1628
Re: Removing RunTime Library/Initialization CRT
« Reply #3 on: October 27, 2007, 12:01:10 pm »
Using linkers predefined name
Code: [Select]
#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")

« Last Edit: October 27, 2007, 02:04:51 pm by timovjl »
May the source be with you

codamateur

  • Guest
Re: Removing RunTime Library/Initialization CRT
« Reply #4 on: October 27, 2007, 01:23:01 pm »
What is the size of the binaries of the program after compilation?

Offline Vortex

  • Member
  • *
  • Posts: 501
    • http://www.vortex.masmcode.com
Re: Removing RunTime Library/Initialization CRT
« Reply #5 on: October 27, 2007, 01:58:24 pm »
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

  • Guest
Re: Removing RunTime Library/Initialization CRT
« Reply #6 on: October 27, 2007, 04:05:04 pm »
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 :)
« Last Edit: October 27, 2007, 04:06:38 pm by codamateur »

Offline Vortex

  • Member
  • *
  • Posts: 501
    • http://www.vortex.masmcode.com
Re: Removing RunTime Library/Initialization CRT
« Reply #7 on: October 27, 2007, 11:06:42 pm »
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

  • Guest
Re: Removing RunTime Library/Initialization CRT
« Reply #8 on: October 28, 2007, 03:00:52 am »
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 :)

Offline Vortex

  • Member
  • *
  • Posts: 501
    • http://www.vortex.masmcode.com
Re: Removing RunTime Library/Initialization CRT
« Reply #9 on: October 28, 2007, 10:30:14 am »
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...