Author Topic: OSX Mach-O 64-bit BIN output  (Read 161 times)

aw27

  • Member
  • *****
  • Posts: 1038
  • Let's Make ASM Great Again!
OSX Mach-O 64-bit BIN output
« on: January 08, 2018, 12:14:54 AM »
This example tests UASM's bin output capability against the Mach-O 64-bit format. (approved  :t)

Code: [Select]
; OSX Mach-O 64-bit "Hello world" console application.
; Uses UASM's bin output format, so no linker needed.
; uasm64 -bin -Fo binMacho64. binMacho64.asm (Can be built in Windows)
; Tested in latest OSX (High-Sierra, 10.13.2)
; Will output: Hello, UASM World!

.x64
include machoinc.inc

__origin EQU 100000000h

_TEXT segment para public FLAT 'CODE'
    ORG __origin
   
start_text label byte
    mach_header_64 <MH_MAGIC_64, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL, MH_EXECUTE \
    ,4,sizeof_Command,MH_NOUNDEFS,0>

__COMMANDSstart label byte
; Load Command 0
___PAGEZEROstart label byte
segment_command_64<LC_SEGMENT_64, sizeof_pagezero,\
"__PAGEZERO",0,__origin, 0,0,0,0,0,0>
sizeof_pagezero equ $-___PAGEZEROstart

___TEXTstart label byte
; Load Command 1
segment_command_64<LC_SEGMENT_64, sizeof_text, "__TEXT", __origin,1000h,0,\
                  1000h,VM_PROT_READ OR VM_PROT_WRITE OR VM_PROT_EXECUTE, \
                  VM_PROT_READ OR VM_PROT_EXECUTE,0,0>
section_64<"__text", "__TEXT", IMAGEREL ___codestart+__origin, ___codeend-___codestart, ___codestart,1,0,0,LC_REQ_DYLD or MH_NOFIXPREBINDING,0,0,0>
sizeof_text equ $ - ___TEXTstart

;Load command 2
__DATAstart label byte
segment_command_64<LC_SEGMENT_64, sizeof_data, "__DATA", IMAGEREL _dataOrig+__origin,1000h,1000h,\
                  1000h,VM_PROT_READ OR VM_PROT_WRITE OR VM_PROT_EXECUTE, \
                  VM_PROT_READ OR VM_PROT_WRITE,1,0>
section_64<"__data", "__DATA", IMAGEREL _dataOrig+__origin, _dataend-_dataOrig, 1000h,1h,0,0,0,0,0,0>
sizeof_data equ $ - __DATAstart

;Load command 3
__UNIX_THREADstart label byte
thread_command<LC_UNIXTHREAD, sizeof_Unix, x86_THREAD_STATE64,\
x86_THREAD_STATE64_COUNT, <0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,IMAGEREL ___codestart +__origin,0,0,0,0>>
sizeof_Unix =$-__UNIX_THREADstart

sizeof_Command = $-__COMMANDSstart
   
___codestart label byte   
mov rax,2000004h
mov rdi, 1
lea rsi, string
mov rdx, SIZEOF string
syscall

mov rax,2000001h
mov rdi,0
syscall
___codeend label byte
org 4096   
_TEXT ends

_DATA segment dword public FLAT 'DATA'
_dataOrig label qword
string db "Hello, UASM World!",0Ah,0
_dataend label qword
org 4096
_DATA ends

end

johnsa

  • Member
  • ****
  • Posts: 660
    • Uasm
Re: OSX Mach-O 64-bit BIN output
« Reply #1 on: January 08, 2018, 03:11:13 AM »
Nice work testing it out in such detail, I love it :)

I've just uploaded the 2.46.6 builds to the site, give that a try and it should fix that stack alignment issue (both the Linux reported one from Gonefishing's example and your OSX sample)

Cheers
John

aw27

  • Member
  • *****
  • Posts: 1038
  • Let's Make ASM Great Again!
Re: OSX Mach-O 64-bit BIN output
« Reply #2 on: January 08, 2018, 06:25:09 AM »
Mach-O, and in general every low-level thingy in OSX is poorly documented. In addition, there are virtually no experts publishing anything of value.
Makes sense, 99.99% of Apple users couldn't care less about those things - it is just a well marketed symbol of status.