flat assembler
Message board for the users of flat assembler.

Index > Main > Multi-Section Binary?

Author
Thread Post new topic Reply to topic
sid123



Joined: 30 Jul 2013
Posts: 340
Location: Asia, Singapore
sid123
Hi,
Really apologize for creating another stupid thread, but I've got SmallerC working with FASM. Smile
FASM compiles it nicely but for the port I had to remove the 'section .text' and 'section .data' macros from the Code Generator. The problem is that removing this generates an "Invalid OpCode Exception", I tried it with NASM (without the sections), and it too cause the same exception, which means that there is some need of these sections. The problem is from what I've read from the FASM manual, sections are only supported in PE-Executable format.
Btw I am talking about sections in a "Flat Binary"
(linky)
Anyway to get around Razz
Cheers,
Sid

_________________
"Those who can make you believe in absurdities can make you commit atrocities" -- Voltaire https://github.com/Benderx2/R3X
XD
Post 17 Mar 2014, 01:42
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17671
Location: In your JS exploiting you and your system
revolution
Which program generates "Invalid OpCode Exception"?

Which program did you remove "'section .text' and 'section .data' macros"?

Did you alter fasm/nasm sources or some other program?

It is not clear from your description what you are doing.
Post 17 Mar 2014, 02:00
View user's profile Send private message Visit poster's website Reply with quote
sid123



Joined: 30 Jul 2013
Posts: 340
Location: Asia, Singapore
sid123
1. The program that was compiled by both the assemblers.
2. In the SmallerC code generator,
Code:
 if (OutputFormat == FormatSegmented || OutputFormat == FormatSegHuge)
    {
      CodeHeader = "section .text "; // Nulled it out for FASM
      DataHeader = "section .data"; // Nulled out for FASM
    }
    

3. No I altered SmallerC's sources to generate FASM output.
4. What I am asking is:
Code:
section '.data' data readable writeable     

Works fine with PE format, however is there any way I can use exactly the same in binary format. (FASM errors out by saying 'illegal instruction' when compiling for flat binary.)
I guess I've found what I am looking for: http://board.flatassembler.net/topic.php?p=143064
Post 17 Mar 2014, 02:11
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17671
Location: In your JS exploiting you and your system
revolution
There are a few versions of raw PE generator macros/code on this board. You could search for those and find one that is suitable.

But I have to ask why are you assembling to binary and then expecting sections? The concept of a section in an unformatted binary file does not exist. Perhaps you need to rethink your approach here.
Post 17 Mar 2014, 02:18
View user's profile Send private message Visit poster's website Reply with quote
sid123



Joined: 30 Jul 2013
Posts: 340
Location: Asia, Singapore
sid123
Well, Even I think there is no need of sections in a binary format but the problem is with the Code Generator:
Code:
mov     eax, [ebp-12]
        mov     ebx, eax
        mov     al, [ebx]
        movsx   eax, al
        cmp     eax, 45
        ;; This or the below line jumps to L220.
        jne     L220
        mov     eax, -1
        mov     [ebp-44], eax
        mov     eax, [ebp-12]
        inc     dword [ebp-12]
L220:
   ;; Empty next label
L219:
    ;; Empty next label
L216:
;; But this contains data, problem.
;; Without sections this is also being executed Question
section .data
        db      "0123456789abcdef",0
;; This is what should be executed.
section .text
        mov     eax, L222
        mov     [ebp-16], eax
    

I guess what I mean is: Is there anyway that I can tell fasm to put all strings, data (word, byte, dword whatever) and the code in two distinct places so they remain sane?
Post 17 Mar 2014, 02:26
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17671
Location: In your JS exploiting you and your system
revolution
I see your problem. And the "proper" solution is to use a linker. An alternative solution is to use macros; perhaps not to dissimilar to the list building macros.
Post 17 Mar 2014, 03:33
View user's profile Send private message Visit poster's website Reply with quote
alexfru



Joined: 23 Mar 2014
Posts: 79
alexfru
sid123 wrote:
Well, Even I think there is no need of sections in a binary format but the problem is with the Code Generator:
Code:
mov     eax, [ebp-12]
        mov     ebx, eax
        mov     al, [ebx]
        movsx   eax, al
        cmp     eax, 45
        ;; This or the below line jumps to L220.
        jne     L220
        mov     eax, -1
        mov     [ebp-44], eax
        mov     eax, [ebp-12]
        inc     dword [ebp-12]
L220:
   ;; Empty next label
L219:
    ;; Empty next label
L216:
;; But this contains data, problem.
;; Without sections this is also being executed Question
section .data
        db      "0123456789abcdef",0
;; This is what should be executed.
section .text
        mov     eax, L222
        mov     [ebp-16], eax
    

I guess what I mean is: Is there anyway that I can tell fasm to put all strings, data (word, byte, dword whatever) and the code in two distinct places so they remain sane?


You may have missed the -flat32 option of Smaller C. Use it instead of -seg32.

With it the compiler will still generate code and data in the same order, but it will omit lines with "section .text" and "section .data" and it will insert additional jump instructions into the code in order to jump over data variables that otherwise would be interpreted as code by the CPU, which is your current problem precisely.

Example of code generated with -flat32:
Code:
; JumpIfZero
        test    eax, eax
        je      L1331
; {
; RPN'ized expression: "CodeHeader L1334 = "
; Expanded expression: "CodeHeader L1334 =(4) "
        jmp     L1335
L1334:
        db      "section .text",0
L1335:
; Fused expression:    "=(204) *CodeHeader L1334 "
        mov     eax, L1334
        mov     [_CodeHeader], eax
    


Of course, with such code/data organization you must make sure that the memory in which the code generated with -flat32 will reside is readable, writable and executable. But I don't think this should be a problem since you don't appear to support any kind of executable file format with sections/segments that may have distinct memory accessibility requirements (rwx, etc). So, flat it is.
Post 23 Mar 2014, 12:38
View user's profile Send private message Reply with quote
sid123



Joined: 30 Jul 2013
Posts: 340
Location: Asia, Singapore
sid123
Ah, great! Thanks!
Btw I have almost ported your compiler, some bugfixes my syscalls and soon to get SmallerC running! Smile
Post 23 Mar 2014, 14:51
View user's profile Send private message Reply with quote
alexfru



Joined: 23 Mar 2014
Posts: 79
alexfru
revolution wrote:
There are a few versions of raw PE generator macros/code on this board. You could search for those and find one that is suitable.

But I have to ask why are you assembling to binary and then expecting sections? The concept of a section in an unformatted binary file does not exist. Perhaps you need to rethink your approach here.


While it may look like an odd thing, NASM "supports" sections in the flat/raw/binary format. All it does with the sections is grouping them together by name (and perhaps by some other attributes), which lets me directly create DOS 16-bit .COMs and .EXEs, Windows PE .EXEs, Linux ELF executables and so on with proper section descriptions in the resultant executable without using a linker.

I know NASM != FASM != *ASM. Nonetheless, I find this feature useful in NASM. It lets me (re)compile my compiler in DOS, Windows and Linux for any of these OSes using only NASM and a C compiler (my Smaller C or gcc or Open Watcom or something else). No need to search for and research cross-compilers and other tools. Perhaps, with some hacking or modifications to Smaller C, the same result can be achieved with FASM, but with NASM you get it out of the box.
Post 24 Mar 2014, 04:25
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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.