Author Topic: Making your exe smaller  (Read 4488 times)

0 Members and 1 Guest are viewing this topic.

Offline saida

  • C= 64
  • **
  • Posts: 57
  • Karma: 8
    • View Profile
    • saidas webpage
Making your exe smaller
« on: October 04, 2006 »
Normaly, you will create a data segment for your data, and a code segment for your code. This is however not the best way to go, if you are aiming for a really small exe.
If you are going to include a lib made in asm into your c project, you will get two data "sections". This drasticly increases the size of the final exe.
Look at the following code:
Code: [Select]
.data
 szMyString db "param1",0 ;7 bytes
.code
 push offset szMyString ; 5 bytes
 call myFunctionWithOneParameterPushed ; 5 bytes

(total of 7+5+5 = 17 bytes)
This is indeed a nice and structured way to program, but...

instead, look at this code:
Code: [Select]
.code
 call _ff ; 5 bytes
 szMyString db "param1",0 ;7 bytes
_ff:
 call myFunctionWithOneParameterPushed ; 5 bytes

(also 17 bytes, but we got rid of the data section)
When we call the label _ff, the processor automaticly pushes the return value to the stack. And guess what, that return value is the address to our string!
So this has eliminated the use of a data segment... or has it?
There is still one more problem to solve: how do we write to the "read-and-execute-only" code segment?
We could use the win api to unprotect the code segment in runtime - but that would be useless because of the extra bytes needed to do so.
Instead, you can link the masm code with:
\masm32\bin\Link /SUBSYSTEM:WINDOWS "myProgram.obj" /section:.text,RWE /MERGE:.rdata=.data /MERGE:.data=.text

If you want to use code that write to the code segment in a c-program, you will have to link your c program so it makes the code segment (.text) writable - dont forget that.
If your compiler fails to do this, just use PEExplorer or something like that to make your exe's code segment writable.

I hope this help someone out there, struggeling with huge mysterial overheads between his c-code, and asm code.
Sorry if something is unclear, i had to much coffee. ::)

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Making your exe smaller
« Reply #1 on: October 04, 2006 »
Since in flat model, all the segments, cs, ss, ds are all pointing at the same memory, isn't it simply possible to write code into ds:address and then execute it via cs:address?  Or am I missing something?

Jim
Challenge Trophies Won:

Offline saida

  • C= 64
  • **
  • Posts: 57
  • Karma: 8
    • View Profile
    • saidas webpage
Re: Making your exe smaller
« Reply #2 on: October 05, 2006 »
I dont think you have execute priveleges in ds. (only RW)
But i guess you could as well make that area RWE if you wanted to.
The main point here is to get rid of the overhead created, when you have two datasections (one from the asm obj, and one from the main c-program)
This is the only way me and auld got rid of it so far. Its weird, because i really do understand why we couldnt just merge our two data sections..

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Making your exe smaller
« Reply #3 on: October 05, 2006 »
I don't mean execute from ds, I mean write your self modifying code or whatever into ds, but then jump to it in cs - it's at the same offset.

If both the data sections are in dgroup the linker should fix that for you and only have one dsection in the exe.  If not, find out what the C compiler is calling the data section and use an identical section name in your asm.  Or am I confused again - it happens :D

Jim
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17394
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Making your exe smaller
« Reply #4 on: October 05, 2006 »
Knowing you Jim, you're probably right!
Shockwave ^ Codigos
Challenge Trophies Won:

Offline saida

  • C= 64
  • **
  • Posts: 57
  • Karma: 8
    • View Profile
    • saidas webpage
Re: Making your exe smaller
« Reply #5 on: October 06, 2006 »
Probably - because im totaly lost ;)

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2750
  • Karma: 493
    • View Profile
    • http://www.rbraz.com/
Re: Making your exe smaller
« Reply #6 on: October 06, 2006 »
That is a great trick indeed    O0
Challenge Trophies Won: