flat assembler
Message board for the users of flat assembler.

Index > Unix > fasm on MacOS X [Snow Leopard / Lion]

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
Shirk



Joined: 12 Sep 2011
Posts: 10
Shirk 21 Sep 2011, 21:20
Hi,

it took the better part of the evening to debug this but I've found and fixed a piece in the libc-fasm to get it running on OSX.
The former steps used to run fasm on OSX consested of:


  • download libc fasm
  • download objectconv
  • convert fasm.o to MachO32
  • link fasm with gcc


This steps are still working however the latest release (1.69.32 at the time of this writing) will complain about being
'out of memory' of one tries to assemble anything - even an empty file.

I was able to backtrack the issue to the function 'open' in system.inc.
The specific line that causes the problem is line 95:

Code:
    93 :       jnz    copy_path
    94 :       cmp    edi,buffer+1000h
    95 :       ja     out_of_memory
    96 :       ret
    


I was confused about what could go wrong with this - it's basically just a comparison between the string position (edi) and the end of 'buffer' ('buffer+1000h').
So I went on to add some debugging output to that method to dump the assigned value of 'edi', the address of 'buffer' as well as 'buffer+1000h'.
The results where puzzling:

Code:
flat assembler  version 1.69.32  (16384 kilobytes memory)

mov edi, buffer                 - edi: 99856
mov eax, buffer+1000h           - eax: 98400
mov eax, buffer; add eax, 1000h - eax: 103952
error:out of memory
    


It looks like the code assembled on linux behaves differently on OSX - look at the odd adress in 'edi'.. and 'buffer+1000h' is somehow resolved to just 'buffer' (or even less than that..)!
The same code runs just fine on linux - no out of memory, no strange gaps in the addresses..

My proposed fix is to use the variant shown in my debug line #3 - load the address of buffer in a register and then add 1000h at runtime:


Code:
--- a/source/libc/system.inc       2011-07-24 00:42:00.000000000 +0200
+++ b/source/libc/system.inc     2011-09-21 23:03:12.000000000 +0200
@@ -91,7 +91,9 @@
    stos    byte [edi]
  or      al,al
       jnz     copy_path
-  cmp     edi,buffer+1000h
+   mov     eax,buffer
+ add     eax,1000h
+  cmp     edi,eax
     ja      out_of_memory
       ret
 create:
    


This code works on linux as well as on OSX - I tested it by assembling fasm with this modified version and then using that binary to in turn assemble itself.
I got three working versions of fasm able to compile the fasm sources.

- I hope this fix is acceptable, or at least a start for an osx version

P.S. I'm eager to know the reason for this odd behavior - if someone knows it..

Cheers,
Shirk


Description:
Download
Filename: fasm-fix-out-of-memory-lion.diff.txt
Filesize: 297 Bytes
Downloaded: 1390 Time(s)

Post 21 Sep 2011, 21:20
View user's profile Send private message Reply with quote
metalfishx



Joined: 30 Sep 2004
Posts: 65
Location: Florida, Uruguay
metalfishx 03 Nov 2011, 19:51
Hi Shirk!
Thanks for your help! I will try to compile it on a linux box, then on Lion!
I want to start playing with FASM again, but now on mac Smile
Thanks!

_________________
---------------------------------------
Roberto A. Berrospe Machin
Ruta Internet, Florida Uruguay
---------------------------------------
Post 03 Nov 2011, 19:51
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
revolution 03 Nov 2011, 20:20
This might be a problem with relocations.

I wonder if Tomasz has seen this thread? Perhaps someone might want to PM Tomasz?
Post 03 Nov 2011, 20:20
View user's profile Send private message Visit poster's website Reply with quote
metalfishx



Joined: 30 Sep 2004
Posts: 65
Location: Florida, Uruguay
metalfishx 03 Nov 2011, 22:54
I think forgot everything! I should start learning from 0 again... lol...

Anything I try to compile, compiles just fine, to object file, but when I try to link using gcc or ld I get this:

ld: warning: ignoring file test.o, file was built for unsupported file format which is not the architecture being linked (i386)
Undefined symbols for architecture i386:
"_main", referenced from:
start in crt1.10.6.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status

Any ideas?

Thanks
Post 03 Nov 2011, 22:54
View user's profile Send private message Visit poster's website Reply with quote
metalfishx



Joined: 30 Sep 2004
Posts: 65
Location: Florida, Uruguay
metalfishx 03 Nov 2011, 23:06
OK using:

objconv -fmacho32 -nu test.o test_m.o

Just figured the macho... that will convert the object.
Then:

ld -arch i386 -o test test_m.o /usr/lib/crt1.o -lc

I get:

ld: warning: -macosx_version_min not specificed, assuming 10.7
Undefined symbols for architecture i386:
"__printf", referenced from:
___main in test_m.o
"_main", referenced from:
__start in crt1.o
(maybe you meant: ___main)
ld: symbol(s) not found for architecture i386

So looks like it's closer... but, its bad fasm doesn't really compiles to macho...
Post 03 Nov 2011, 23:06
View user's profile Send private message Visit poster's website Reply with quote
metalfishx



Joined: 30 Sep 2004
Posts: 65
Location: Florida, Uruguay
metalfishx 04 Nov 2011, 04:02
Ok! Got something compiled! lol!

This:

Code:
format ELF
section '.text' executable 
public main
extrn printf
main: 
push  ebp 
mov   ebp, esp 
push  ebx 
mov  ebx, esp 
and  esp, 0xfffffff0 
sub  esp, 12 
push ebx 
add  esp, 16 
mov  dword [esp], msgHelloWorld 
call printf 
sub esp, 16 
pop ebx 
mov esp, ebx 
pop ebx 
mov esp, ebp 
pop ebp 
mov eax, 0 
ret 
section '.data' writeable 
msgHelloWorld db 'Hello world from FASM!',0xA,0
    


Compiled and linked with:

>fasm test.asm test.o
>objconv -fmacho32 -nu test.o test_m.o
>ld -arch i386 -macosx_version_min 10.7 -o test test_m.o /usr/lib/crt1.o -lc

Attached is the fasm+objconv binaries for Mac Os X (Lion in my case, but should work on Snow and maybe Leopard?), you should have the SDK installed of course.


Description: fasm + objconv (by Agner Fog) compiled for Mac OS X Intel i386 (Should work on Lion and Snow Leopard)...
Download
Filename: fasmobjconvmac.zip
Filesize: 256.37 KB
Downloaded: 1441 Time(s)


_________________
---------------------------------------
Roberto A. Berrospe Machin
Ruta Internet, Florida Uruguay
---------------------------------------
Post 04 Nov 2011, 04:02
View user's profile Send private message Visit poster's website Reply with quote
STLVNUB



Joined: 08 Aug 2008
Posts: 13
STLVNUB 04 Nov 2011, 07:46
Another fellow Hackintosher??
Thanks for this
Post 04 Nov 2011, 07:46
View user's profile Send private message Reply with quote
metalfishx



Joined: 30 Sep 2004
Posts: 65
Location: Florida, Uruguay
metalfishx 04 Nov 2011, 13:40
Yeah, well actually I am on a MacBook Air, but yes, I had hackintosh long time ago Smile
Post 04 Nov 2011, 13:40
View user's profile Send private message Visit poster's website Reply with quote
metalfishx



Joined: 30 Sep 2004
Posts: 65
Location: Florida, Uruguay
metalfishx 04 Nov 2011, 15:34
Hey, just an opinion now this is sticky; I opine that the title should change to "FASM on MacOS X [Snow Leopard / Lion]" maybe?
Smile
Post 04 Nov 2011, 15:34
View user's profile Send private message Visit poster's website Reply with quote
metalfishx



Joined: 30 Sep 2004
Posts: 65
Location: Florida, Uruguay
metalfishx 04 Nov 2011, 16:42
64 bit works too...
Need to figure (learn) lots of things, but did a test using simple syscall, compiled and works good Smile

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
    


Compiled, converted and linked with:

> fasm test64.asm test64.o
> objconv -fmacho64 -nu test64.o test64_m.o
> ld -arch x86_64 -macosx_version_min 10.6 -o test64 test64_m.o /usr/lib/crt1.o -lc

Smile

_________________
---------------------------------------
Roberto A. Berrospe Machin
Ruta Internet, Florida Uruguay
---------------------------------------
Post 04 Nov 2011, 16:42
View user's profile Send private message Visit poster's website Reply with quote
metalfishx



Joined: 30 Sep 2004
Posts: 65
Location: Florida, Uruguay
metalfishx 04 Nov 2011, 18:03
Hey guys, here you go.
I've attached fasm source, fasm and objconv binaries, and also a bash script "compile" i've created to make all these calls easier...


The script is simple:
Quote:

> compile filename osver architecture

* fname = asmfile with no extension, .asm will be added.

* osver = Mac OS X Version: 10.6 or 10.7 [default 10.6]

* architecture = 32 or 64 (bits) [default 32]


Thanks.


Description: * fasm v1.69.35 binary+patched source (Shirk patch to work on Mac OS X)
* objconv binary
* "compile" bash script i've created to make all these calls easier...

Download
Filename: fasm-v1.69.35-objconv-v2.12-mac.zip
Filesize: 56.57 KB
Downloaded: 1342 Time(s)


_________________
---------------------------------------
Roberto A. Berrospe Machin
Ruta Internet, Florida Uruguay
---------------------------------------
Post 04 Nov 2011, 18:03
View user's profile Send private message Visit poster's website Reply with quote
metalfishx



Joined: 30 Sep 2004
Posts: 65
Location: Florida, Uruguay
metalfishx 04 Nov 2011, 23:12
More advances in testing 64 bits libc calls...

Code:
format ELF64 

;new line
nl = 0x0A
      
section '.text' executable

extrn printf
extrn exit 

public main


main: 
  push       rbp
      mov        rbp, rsp
 sub        rsp, 0x10
        xor        al, al
   lea        rcx, qword [theMessage]
  mov        rdi, rcx
 call       printf
   mov        rcx, 0
   call       exit
     pop        rbp
      ret


section '.data' writable
         
theMessage  db nl,nl,'*************************',nl,'FASM 64 bit printf call under OS X!',nl,'*************************',nl,nl,0

    


linking with ld will give you an error:

Quote:

Undefined symbols for architecture x86_64:
"start", referenced from:
-u command line option
ld: symbol(s) not found for architecture x86_64


Why? ehmmmmmmmmmmm not sure (still learning hehe), any ideas?... Smile

But well, linking this with gcc works just fine!

Use: gcc -m64 -o file file.o

Smile

_________________
---------------------------------------
Roberto A. Berrospe Machin
Ruta Internet, Florida Uruguay
---------------------------------------
Post 04 Nov 2011, 23:12
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
revolution 05 Nov 2011, 04:06
metalfishx wrote:
linking with ld will give you an error:

Quote:

Undefined symbols for architecture x86_64:
"start", referenced from:
-u command line option
ld: symbol(s) not found for architecture x86_64


Why? ehmmmmmmmmmmm not sure (still learning hehe), any ideas?... Smile
That error looks like you forgot to set the entry label. The linker is looking for 'start' but you have 'main'. Perhaps add 'entry main' to your source? Or just rename 'main' to 'start'?
Post 05 Nov 2011, 04:06
View user's profile Send private message Visit poster's website Reply with quote
metalfishx



Joined: 30 Sep 2004
Posts: 65
Location: Florida, Uruguay
metalfishx 05 Nov 2011, 04:40
Hi! Thanks for the answer!
That wasn't the problem, I figured it out, It was my fault! (of course) Very Happy
I was missing libc.dylib Smile ...

Now I am calling ld this way:

> ld -arch x86_64 -macosx_version_min 10.7 -o printf64 printf64.o /usr/lib/crt1.o /usr/lib/libc.dylib

Attached is the modified "compile"; for now the libs are console only, but good for playing a little Smile


Description: modified compile bash script to include libc.dylib ...
Download
Filename: compile.zip
Filesize: 1.16 KB
Downloaded: 1326 Time(s)


_________________
---------------------------------------
Roberto A. Berrospe Machin
Ruta Internet, Florida Uruguay
---------------------------------------
Post 05 Nov 2011, 04:40
View user's profile Send private message Visit poster's website Reply with quote
Shirk



Joined: 12 Sep 2011
Posts: 10
Shirk 14 Nov 2011, 16:17
Well.. so much for topic-reply notification - I totally missed 12 replies!

I'm going to pm / mail thomasz - let's see what he thinks about the patch
(or the underlying issue).

Thank's at all who read and added to this thread - especially to metalfishx for the binaries Smile
Post 14 Nov 2011, 16:17
View user's profile Send private message Reply with quote
metalfishx



Joined: 30 Sep 2004
Posts: 65
Location: Florida, Uruguay
metalfishx 14 Nov 2011, 16:32
Oh, you welcome; thank you all for for the great work with FASM... Smile

_________________
---------------------------------------
Roberto A. Berrospe Machin
Ruta Internet, Florida Uruguay
---------------------------------------
Post 14 Nov 2011, 16:32
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
revolution 16 Nov 2011, 08:12
Shirk wrote:
Well.. so much for topic-reply notification - I totally missed 12 replies!
If you check the message again I think you will find that you didn't tick the "notify me" box.
Post 16 Nov 2011, 08:12
View user's profile Send private message Visit poster's website Reply with quote
Shirk



Joined: 12 Sep 2011
Posts: 10
Shirk 18 Nov 2011, 18:37
@revolution - no, it is checked but probably I've read a post without being logged in.

FYI - I PM'ed Thomasz and we agreed that it looks like a bug in objconv.
Something related to a relocation type not properly handled in the conversion.

So - do we have any alternative like objcopy or something else to convert the
binary?
Post 18 Nov 2011, 18:37
View user's profile Send private message Reply with quote
metalfishx



Joined: 30 Sep 2004
Posts: 65
Location: Florida, Uruguay
metalfishx 18 Nov 2011, 19:20
Would be amazing to have FASM to do the whole conversion Very Happy
I have to admint that I been trying NASM for a while; it comes bundled with Mac OS X SDK and it handles everything, including macho format and things, but I went back to FASM; because I don't like NASM at all, the syntax, the macros, I prefer FASM all the way; it's like comparing C and Objective C... Smile
Post 18 Nov 2011, 19:20
View user's profile Send private message Visit poster's website Reply with quote
zab



Joined: 28 May 2012
Posts: 6
zab 28 May 2012, 22:45
Hi guys,

I'm very new to FASM and assembly in general, so bear with me please.
I've 2 Apple machines. One with OSX Lion (64bits) and the other one under OSX Snow Leopard (32bits).
Can someone point please to a FASM binary so I can start learn programming on OSX?

N.B: I'm not interested in doing any graphical programming with FASM, only console applications.

Thanks
Zab
Post 28 May 2012, 22:45
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.