flat assembler
Message board for the users of flat assembler.

Index > Unix > How to translate this code to fasmg?

Author
Thread Post new topic Reply to topic
TmX



Joined: 02 Mar 2006
Posts: 837
Location: Jakarta, Indonesia
TmX
I found this code example:
Code:
format ELF64 

section '.text' executable  
public main 
extrn printf
extrn exit 

;macro for create .size constant automatically
struc db [data]{
common
. db data
.size = $ - .
}

;testing using syscall 

main:
   mov rax, 0x2000004            ; sys_write
   mov rdi, 1                    ; stdout 
   mov rsi, qword msgHelloWorld  ; string
   mov rdx, msgHelloWorld.size   ; length
   syscall
   mov rax, 0x2000001            ; sys_exit
   xor rdi, rdi                  ; exit code
   syscall


section '.data' writeable  
msgHelloWorld db 'Hello 64bit World from FASM!',0x0A,0    


My first translation attempt:
Code:
format MachO executable

include 'selfhost.inc'

section '.text' executable  
public main 
extrn printf
extrn exit 

;macro for create .size constant automatically
struc db [data]{
common
. db data
.size = $ - .
}

;testing using syscall 

main:
   mov rax, 0x2000004            ; sys_write
   mov rdi, 1                    ; stdout 
   mov rsi, qword msgHelloWorld  ; string
   mov rdx, msgHelloWorld.size   ; length
   syscall
   mov rax, 0x2000001            ; sys_exit
   xor rdi, rdi                  ; exit code
   syscall


section '.data' writeable  
msgHelloWorld db 'Hello 64bit World from FASM!',0x0A,0    


The result is:
Quote:

flat assembler version g.jmhx
hello64_01.asm [1]:
format MachO executable
Processed: format MachO executable
Error: invalid argument.


How to fix this?
I'm on MacOS Big Sur 11.6.6
Post 21 May 2022, 16:53
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8028
Location: Kraków, Poland
Tomasz Grysztar
In order to have that FORMAT option available you need to include format.inc, either one that comes with fasmg core examples, or the one from the fasmg compatibility package.

Set up the INCLUDE variable to point to either of these packages and then include the format/format.inc - you can add the INCLUDE on top of the source file, or you can add it directly from the command line when assembling:
Code:
fasmg hello64.asm -iInclude\ \'format/format.inc\'    

And your source requires at least a few tweaks, and conversion of the fasm macro syntax:
Code:
format MachO64 executable

segment '__TEXT' readable executable

section '__text' align 16

;macro for create .size constant automatically
struc db data&
 . db data
 .size = $ - .
end struc

;testing using syscall

main:
   mov rax, 0x2000004            ; sys_write
   mov rdi, 1                    ; stdout
   mov rsi, qword msgHelloWorld  ; string
   mov rdx, msgHelloWorld.size   ; length
   syscall
   mov rax, 0x2000001            ; sys_exit
   xor rdi, rdi                  ; exit code
   syscall


segment '__DATA' readable writable

section '__data' align 4

msgHelloWorld db 'Hello 64bit World from FASM!',0x0A,0    
(See the migration guide for more info on the syntax differences.)

You could also use a different starting point - the Mach-O example published in the official repository. Note that it uses the formatter directly, without the FORMAT directive emulation. But it too requires the compatibility package to be available through the INCLUDE path (you can make a shell wrapper for fasmg that would set up INCLUDE variable locally for the assembly).
Post 21 May 2022, 17:29
View user's profile Send private message Visit poster's website Reply with quote
TmX



Joined: 02 Mar 2006
Posts: 837
Location: Jakarta, Indonesia
TmX
Ah, forgot to download the fasmg compatibility package, thanks for reminding.

Managed to build the code, but somehow MacOS refused to run it:
Quote:

andre@Hephaestus FASMG % echo $INCLUDE
/Users/andre/fasmg/packages/x86/include
andre@Hephaestus FASMG % fasmg hello64.asm
flat assembler version g.jmhx
3 passes, 4126 bytes.
andre@Hephaestus FASMG % file hello64
hello64: Mach-O 64-bit executable x86_64
andre@Hephaestus FASMG % ./hello64
zsh: permission denied: ./hello64
andre@Hephaestus FASMG % chmod +x hello64
andre@Hephaestus FASMG % ./hello64
zsh: killed ./hello64
andre@Hephaestus FASMG % chmod u+x hello64
andre@Hephaestus FASMG % ./hello64
zsh: killed ./hello64
andre@Hephaestus FASMG %


Perhaps I'm missing something?
Post 22 May 2022, 11:39
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18846
Location: In your JS exploiting you and your system
revolution
I have no experience with Mac but this line worries me:
Code:
   mov rsi, qword msgHelloWorld  ; string    
I would have expected:
Code:
   lea rsi, [msgHelloWorld]  ; string    
Either that, or there should be a relocation table.
Post 22 May 2022, 12:16
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8028
Location: Kraków, Poland
Tomasz Grysztar
TmX wrote:
Perhaps I'm missing something?
You're missing an ENTRY statement for sure (well, I forgot to add one). See the official example I linked.
Post 22 May 2022, 12:36
View user's profile Send private message Visit poster's website Reply with quote
TmX



Joined: 02 Mar 2006
Posts: 837
Location: Jakarta, Indonesia
TmX
Like this?
Code:
include 'format/format.inc'

format MachO64 executable

segment '__TEXT' readable executable
entry $, rdi: 1, rsi: msgHelloWorld

section '__text' align 16

;macro for create .size constant automatically
struc db data&
 . db data
 .size = $ - .
end struc

;testing using syscall

main:
   mov rax, 0x2000004            ; sys_write
   mov rdi, 1                    ; stdout
   mov rsi, qword msgHelloWorld  ; string
   mov rdx, msgHelloWorld.size   ; length
   syscall
   mov rax, 0x2000001            ; sys_exit
   xor rdi, rdi                  ; exit code
   syscall


segment '__DATA' readable writable

section '__data' align 4

msgHelloWorld db 'Hello 64bit World from FASM!',0x0A,0    


BTW, I also tried building the demo_syscall64.asm example as is.

Both produced same result:
Quote:

zsh: killed ...
Post 22 May 2022, 13:35
View user's profile Send private message Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 219
FlierMate
I think it is "entry main". Or change the "main:" label to "entry $".
Post 23 May 2022, 11:27
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.