flat assembler
Message board for the users of flat assembler.

Index > Windows > Optimizing the data section

Author
Thread Post new topic Reply to topic
pete



Joined: 20 Apr 2009
Posts: 110
pete 08 May 2009, 08:22
r22 wrote at the "data moved…"-topic that reserved data belongs at the end of the section. This first made me guess that the reserved data doesn't get reserved at all, just the names of these reserved bytes point to the end of the data section which is sort of seperated from the code-section so when the yet uninit. data gets initialized while execution, the sections don't intersect with each other.
Something like:
Code:
...
str1 db "Huhu",0        ; end of initialized data, start of uninit. data:
str2 equ $             ; 50 Byte Buffer
str3 equ $+50               ; 50 Byte Buffer
str4 equ $+100
...
    

Well, i pushed my reserved bytes at the end of the data section and expected my exe-file to get smaller, but it didn't, of course because these bytes get "really" reserved…
So why does it make sense to put uninitialized data at the end of the section?

I have a lot of dd's in my data section to store often used window-handles. Are those too reserved bytes when i define them with "?", f.i.:
Code:
handle dd ?
    

Should'nt those too be moved at the end of the data section?

My last two questions are: Where can i find some info about how Win32 separates the sections of an application and where those sections seat in memory?

Thank you in advance.
Post 08 May 2009, 08:22
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 08 May 2009, 08:54
If you are generating a PE file then you will only see a reduction in size if it passes a page boundary (512B). And your code will fail if you try to access into the next VM page.
Code:
format PE ...
...
mov [any_data],eax ;CRASH: the data has not been paged into your memory or you overwrite the following page of data
.data
string1 db 'Hello world!',0  ;13 bytes forces a new 512 byte section in file
some_data = $ ;Okay if you have less than 4083 bytes used at runtime
any_data = $+4096 ;error at runtime, the data was not reserved!    
Post 08 May 2009, 08:54
View user's profile Send private message Visit poster's website Reply with quote
pete



Joined: 20 Apr 2009
Posts: 110
pete 08 May 2009, 09:04
Aha; i think i got the thing about the 512-Byte-Pages.
But what's a VM page? It's 4096 bytes in size? Does it hold the whole program?

And why is it important to define uninit. data at the end of the section?
Post 08 May 2009, 09:04
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 08 May 2009, 09:35
Code:
rb 1000000 ;force the OS the allocate 1000000 bytes at startup    
The above tells the OS that you need 1000000 bytes of data for the program's use, but you don't want to store 1000000 bytes of zero's in the disk file, that is just waste.

The x86 architecture has a standard 4kB paging granularity (it has other sizes also, but Windows usually only uses 4kb), so the smallest amount of virtual memory (VM) that can be allocated is 4kB.
Code:
db 123 ;force the OS to allocate 4kB of memory    


If you define uninitialised data at the start and follow it with initialised data then you have wasted space in your disk file.
Code:
rb 1000000
db 123 ;this byte MUST be in the disk file, so fasm has to put 1000000 bytes of padding    
Post 08 May 2009, 09:35
View user's profile Send private message Visit poster's website Reply with quote
pete



Joined: 20 Apr 2009
Posts: 110
pete 08 May 2009, 10:06
Aha! So when my data section is:
Code:
db 123
rb 1000000 
    

… then only the first 4 kB get written on disk, while the rest gets allocated at application startup?
Post 08 May 2009, 10:06
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 08 May 2009, 10:13
Only 512 bytes written to disk, but the rest is correct.
Post 08 May 2009, 10:13
View user's profile Send private message Visit poster's website Reply with quote
pete



Joined: 20 Apr 2009
Posts: 110
pete 08 May 2009, 10:24
Thank you a lot, revolution!
Post 08 May 2009, 10:24
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.