flat assembler
Message board for the users of flat assembler.

Index > Main > Bug with empty sections

Author
Thread Post new topic Reply to topic
chorus



Joined: 16 Mar 2004
Posts: 23
chorus
Hello,
Just noticed FASM creates invalid win32 executables if you include an "empty" section. ex.,

Code:
  section ".data" data readable writeable
  section ".code" code readable executable
  ; some code
    


Not that anyone does this in practice, but I have some libraries which are essentially includes of a bunch of .code sections. With the new proc/endp macros, if I don't happen to use a function from a particular source file, then the code isn't compile by the macro. And I end up with an empty code section and an executable that doesn't work

Just thought I'd let someone know

--Chorus
Post 25 Sep 2004, 15:37
View user's profile Send private message Reply with quote
comrade



Joined: 16 Jun 2003
Posts: 1137
Location: Russian Federation
comrade
insert at least one nop always?
Post 25 Sep 2004, 15:48
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
chorus



Joined: 16 Mar 2004
Posts: 23
chorus
Yeah, pretty much anything will work that is guaranteed to assemble: nop, a single byte defined, etc. Just thought I'd post it so it could be taken a look at for a "bug fix". Or maybe I should look into extending the .code and .data macros I am using to see if I can watch for the "empty" case

--Chorus
Post 25 Sep 2004, 16:09
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3500
Location: Bulgaria
JohnFound
IMHO, it is not good idea to use separate code sections for every library. Even if you add one single byte, the section size becomes 512 bytes added to your .exe file.
Also, are you sure that empty sections causes crash? For example if we have section only with uninitialized data this section will be zero length without problems.

Regards
Post 25 Sep 2004, 17:43
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
chorus



Joined: 16 Mar 2004
Posts: 23
chorus
Quote:
IMHO, it is not good idea to use separate code sections for every library. Even if you add one single byte, the section size becomes 512 bytes added to your .exe file.


It isn't a good idea, no. I'd much rather have a "group" directive for the code segments, so that when the libraries are assembled, they'd be merged into one segment... but Fasm doesn't currently support this. So I have a bunch of libraries that are essentially libxxx.asm with .data and .code sections and get included into my main program. Another feature that Fasm is currently missing (I believe) is statically linked libraries. This would also solve the problem, but you work with what you have. So this arrangement, though poor programming practice, is convenient for unfinished programs.

Quote:
Also, are you sure that empty sections causes crash?


Well, when I have "empty" sections, Windows tell me executable is invalid. When I don't have empty sections, the program runs. If I put a "nop" the program runs. And it isn't crashes I'm getting -- the executable just plain refuses to even start. Trying to run the program from Explorer simply returns an error that the executable is invalid. Presumably, the empty section breaks the PE somehow, but I don't know enough about that end of things to know why.

Quote:
For example if we have section only with uninitialized data this section will be zero length without problems.


This I haven't checked yet, and I'm currently not at a Windows box to test it, though I believe you. I do know that if there is nothing at all, my executable doesn't work. It should be easy enough to verify: just add an extra section declaration immediately before another one and reassemble.

--Chorus
Post 25 Sep 2004, 22:33
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3500
Location: Bulgaria
JohnFound
I tested it with empty code section and empty data section and the file runs OK on my Win98 machine. (only adding extra 512 bytes to the executable) What is your OS. Or maybe this is some side effect - check your antivirus program or something...

About source level libraries and how to create code and data in merged sections - simply check out the sources of the project Fresh. (globals.inc for example)

Regards.


Last edited by JohnFound on 25 Sep 2004, 23:35; edited 1 time in total
Post 25 Sep 2004, 23:32
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7498
Location: Kraków, Poland
Tomasz Grysztar
Grouping the sections together and linking static libraries is the linker's job - you need to use the object output of flat assembler and some external linker to achieve this (fasm itself doesn't contain a linker).
Post 25 Sep 2004, 23:34
View user's profile Send private message Visit poster's website Reply with quote
comrade



Joined: 16 Jun 2003
Posts: 1137
Location: Russian Federation
comrade
For a pseudo-modular solution without using a linker, see here:
http://board.flatassembler.net/topic.php?p=12342&highlight=#12342
Post 26 Sep 2004, 00:16
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
chorus



Joined: 16 Mar 2004
Posts: 23
chorus
@JohnFound:

Quote:
I tested it with empty code section and empty data section and the file runs OK on my Win98 machine.


Both Windows 2000 and XP create this problem for me. 2 different machines.

Quote:
Or maybe this is some side effect - check your antivirus program or something...


Don't have an anti virus program on one of the machines... below is some code that recreates the problem. If the "nop" is commented out, the program doesn't run. If it isn't, then it runs. Maybe this only affects the NT family... I dunno. Also, I'm using FASM 1.55, although I'm assuming you are, too.

@Privalov:
Quote:
Grouping the sections together and linking static libraries is the linker's job


For linking static libraries, I'll agree to that. For grouping sections, I don't necessarily. Other assemblers offer this feature; I believe TASM and MASM do. If you don't in yours, it doesn't bother me and it's completely up to you. I switched to FASM a while back, and I have no intention of going back to MASM. It's a good assembler. It would just be a nice feature is all.

But anyways, using my current setup, I can hit F9, and my program assembles and runs. I don't have to worry about linking assembly code to assembly code (yes, I'm about that lazy). All I get is an executable with a bunch of sections, which I can live with while developing. For a final product, in only takes a couple minutes to cut and paste all the code together.

--Chorus


Here's an example proggy:

Code:
format PE GUI 4.0
entry start

include '%fasminc%\win32a.inc'

section '.data' data readable writeable
;commenting the following nop creates an "invalid executable"
;according to Windows 2000 and XP

nop

section '.data' data readable writeable
  ParentClass          dd sizeof.WNDCLASSEX                       ;cbSize
                       dd CS_DBLCLKS or CS_HREDRAW or CS_VREDRAW  ;style
                       dd ParentWndProc                           ;lpfnWndProc
                       dd NULL                                    ;cbClsExtra
                       dd NULL                                    ;cbWndExtra
                       dd NULL                                    ;hInstance
                       dd NULL                                    ;hIcon
                       dd NULL                                    ;hCursor
                       dd COLOR_WINDOW+1                          ;hbrBackground
                       dd NULL                                    ;lpszMenu
                       dd szParentClass                           ;lpszClass
                       dd NULL                                    ;hIconSm


  szParentClass        db 'SKELETON',0
  szProgramTitle       db 'Skeleton FASM App',0

  hInstance            dd ?
  hwndParent           dd ?

  msg                  MSG

section '.code' code readable executable

proc ParentWndProc, hWnd,uMsg,wParam,lParam
        enter
        mov eax,[uMsg]
        cmp eax,WM_DESTROY
        je .Destroy
.Default:
        invoke DefWindowProc,[hWnd],[uMsg],[wParam],[lParam]
        return
.Destroy:
        invoke PostQuitMessage,0
        xor eax,eax
        return
endp

start:
        invoke GetModuleHandle,NULL
        mov [hInstance],eax

        invoke LoadIcon,NULL,IDI_APPLICATION
        mov [ParentClass+WNDCLASSEX.hIcon],eax

        invoke LoadCursor,0,IDC_ARROW
        mov [ParentClass+WNDCLASSEX.hCursor],eax

        mov eax,[hInstance]
        mov [ParentClass+WNDCLASSEX.hInstance],eax
        invoke RegisterClassEx,ParentClass

        invoke CreateWindowEx,0,szParentClass,szProgramTitle,\
          WS_VISIBLE+WS_OVERLAPPEDWINDOW,144,128,256,256,\
          NULL,NULL,[hInstance],NULL
        jmp @f
.MessageLoop:
        invoke TranslateMessage,msg
        invoke DispatchMessage,msg
@@:
        invoke GetMessage,msg,NULL,0,0
        test eax,eax
        jnz .MessageLoop
        invoke ExitProcess,[msg.wParam]

section '.idata' import data readable writeable

  library kernel,'KERNEL32.DLL',\
          user,'USER32.DLL'

  import kernel,\
         GetModuleHandle,'GetModuleHandleA',\
         ExitProcess,'ExitProcess'

  import user,\
         RegisterClassEx,'RegisterClassExA',\
         CreateWindowEx,'CreateWindowExA',\
         DefWindowProc,'DefWindowProcA',\
         GetMessage,'GetMessageA',\
         TranslateMessage,'TranslateMessage',\
         DispatchMessage,'DispatchMessageA',\
         SendMessage,'SendMessageA',\
         LoadCursor,'LoadCursorA',\
         LoadIcon,'LoadIconA',\
         LoadMenu,'LoadMenuA',\
         PostQuitMessage,'PostQuitMessage'
    
Post 26 Sep 2004, 00:27
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3500
Location: Bulgaria
JohnFound
This example works under Win98, but not under Win2000. If you include "rb 1" in this section - everything is OK - regardless of the fact that the section have 0 length. Actually there are two sizes of the section in PE format - one is the size of the section in the file and one is the size in memory when the section is loaded. Maybe Win2000 consider invalid section that have both these sizes 0?

Regards
Post 26 Sep 2004, 01:42
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7498
Location: Kraków, Poland
Tomasz Grysztar
Quote:
For linking static libraries, I'll agree to that. For grouping sections, I don't necessarily.

Though some assemblers also do it, it still remains to linker to merge finally all the data sections into one section etc. - this is what I meant. So even if the assembler doesn't do it, the linker should do it.

One of the substantial features of flat assembler is its literacy - as it is intended mainly as a low-level (high control) tool. As the manual says: "All output code is always in the order in which it was entered into the
source file". That's why all the sections are always generated exactly as they occur in source (even the empty ones), and also this is one of the reasons for the "flat assembler" name.

The Win2000/XP for some reason treats the PE files that contain a section with VirtualSize equal to 0 as corrupted, though in the official Microsoft's PE/COFF specification it's not mentioned.
Post 26 Sep 2004, 09:52
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7498
Location: Kraków, Poland
Tomasz Grysztar
I see one possible solution for the problem of Windows not accepting zero-sized section - align the size of such section like any other small size up to one page (so the virtual size would be really 4096 bytes). I noticed that Microsoft's linker does it like this sometimes.
Post 15 Dec 2005, 21:52
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7108
Location: Slovakia
vid
0 aligned to 4096 should be still 0. If it is a bug, then you are not one to solve it, add it to FAQ maybe. I don't like such workarounds.
Post 15 Dec 2005, 22:12
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number 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-2019, Tomasz Grysztar.

Powered by rwasa.