flat assembler
Message board for the users of flat assembler.

Index > Windows > Is my understanding about PE's File Alignment correct?

Author
Thread Post new topic Reply to topic
uu



Joined: 20 Jul 2024
Posts: 44
uu 13 Aug 2024, 10:24
This is more related to creating tiny PE by adjusting the size of File Alignment setting.

If my understanding is correct, then my newly drawn diagrams are all set.

Normally the default value of File Alignment is 512, and let's say I have two sections with each 300 bytes and 200 bytes, that would make the total EXE size 1536 bytes.
If I reduce it by halve to 256, only code section cannot fit single section, so there's need for multiple of 256. Correct?

If I further set the File Alignment to 4 or 16, it would make sure no unused null padded bytes. Question


Description: 256 File Alignment
Filesize: 31.09 KB
Viewed: 2063 Time(s)

256file_alignment.PNG


Description: 512 File Alignment
Filesize: 26.77 KB
Viewed: 2063 Time(s)

512file_alignment.PNG


Post 13 Aug 2024, 10:24
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20620
Location: In your JS exploiting you and your system
revolution 13 Aug 2024, 10:53
The Windows loader has some restrictions that you need to comply with.

You are correct that the formal PE spec can accommodate different alignments, but the standard Windows loader doesn't accept all of those configurations.
Post 13 Aug 2024, 10:53
View user's profile Send private message Visit poster's website Reply with quote
uu



Joined: 20 Jul 2024
Posts: 44
uu 13 Aug 2024, 11:09
revolution wrote:
The Windows loader has some restrictions that you need to comply with.

You are correct that the formal PE spec can accommodate different alignments, but the standard Windows loader doesn't accept all of those configurations.


Thank you for the answer, I will only take my chance during size coding competition. Laughing
Post 13 Aug 2024, 11:09
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20620
Location: In your JS exploiting you and your system
revolution 14 Aug 2024, 02:08
There is also the native PE format.
Code:
~ cat test.asm
format pe TYPE
entry $
ret

~ fasm -d TYPE=native test.asm
flat assembler  version 1.73.31  (16384 kilobytes memory)
1 passes, 448 bytes.

~ fasm -d TYPE=gui test.asm
flat assembler  version 1.73.31  (16384 kilobytes memory)
1 passes, 1024 bytes.

~ fasm -d TYPE=console test.asm
flat assembler  version 1.73.31  (16384 kilobytes memory)
1 passes, 1024 bytes.

~    
Post 14 Aug 2024, 02:08
View user's profile Send private message Visit poster's website Reply with quote
uu



Joined: 20 Jul 2024
Posts: 44
uu 14 Aug 2024, 05:11
revolution wrote:
There is also the native PE format.


Small, but native is generating .SYS file, which is driver I think. Rolling Eyes
Post 14 Aug 2024, 05:11
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20620
Location: In your JS exploiting you and your system
revolution 14 Aug 2024, 05:35
Tell the competition organiser to run your code as a kernel mode driver. Twisted Evil

Nothing can possibly go wrong.
Post 14 Aug 2024, 05:35
View user's profile Send private message Visit poster's website Reply with quote
Mat Quasar



Joined: 15 Dec 2024
Posts: 87
Mat Quasar 15 Dec 2024, 10:01
I ran a test on ChatGPT and its answer to SectionAlignment was correct, according to my understanding. Its description is also good, although short.

My prompt:
Quote:
Does section alignment field in PE header mean memory alignment? If the image base is 0x400000 and section alignment is 8K, what is the memory address of first section and second section?


ChatGPT answer:

Yes, the Section Alignment field in the PE (Portable Executable) header specifies the alignment of sections in memory once the file is loaded. This alignment determines the starting addresses of sections in memory.

Key Points:

Section Alignment:

It is the granularity (in bytes) to which sections are aligned in memory.
Typical values are 4 KB (0x1000) or 8 KB (0x2000), matching common page sizes.

Image Base:

It is the starting address where the PE image is loaded in memory. In your example, this is 0x400000.

First Section:

The first section starts immediately after the PE headers. However, its address in memory is rounded up to the nearest multiple of the Section Alignment value.

Subsequent Sections:

Each section begins at an address that is the previous section's start address plus its size, rounded up to the Section Alignment.

Example Calculation

Image Base: 0x400000
Section Alignment: 0x2000 (8 KB)

Let's assume:
The PE header and its associated data are less than the Section Alignment size (e.g., 1 KB).

Memory Address of First Section
The first section's starting address is aligned to the Section Alignment. Since the PE image base is 0x400000 and alignment is 8 KB, the address of the first section is:

Code:
0x400000 + 0x2000 = 0x402000
    


Memory Address of Second Section
Assuming the first section has a size of 0x1000 (4 KB), the second section's starting address is calculated by aligning the end of the first section to the Section Alignment. Since 4 KB is less than 8 KB, the second section will start at the next 8 KB boundary:

Code:
0x402000 + 0x2000 = 0x404000
    


Summary

First Section: 0x402000
Second Section: 0x404000

The alignment ensures sections are loaded at predictable memory boundaries, which is important for performance and compatibility with the OS.
Post 15 Dec 2024, 10:01
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1145
Location: Russia
macomics 15 Dec 2024, 11:47
That's right, but ChatGPT forgot to explain that a separate section will also be created for PE header data in RAM. This can be seen in a memory card, say, x64dbg


Description:
Filesize: 241.1 KB
Viewed: 1328 Time(s)

Снимок экрана_20241215_154654.png


Post 15 Dec 2024, 11:47
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.