Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First, you are using old 32-bit Linux kernel calling convention on Mac OS X - this absolutely doesn't work.</p> <p>Second, syscalls in Mac OS X are structured in a different way - they all have a leading class identifier <em>and</em> a syscall number. The class can be Mach, BSD or something else (see <a href="http://www.opensource.apple.com/source/xnu/xnu-1699.26.8/osfmk/mach/i386/syscall_sw.h" rel="noreferrer">here</a> in the XNU source) and is shifted 24 bits to the left. Normal BSD syscalls have class <code>2</code> and thus begin from <code>0x2000000</code>. Syscalls in class <code>0</code> are <em>invalid</em>.</p> <p>As per §A.2.1 of the <a href="http://www.x86-64.org/documentation/abi.pdf" rel="noreferrer">SysV AMD64 ABI</a>, also followed by Mac OS X, syscall id (together with its class on XNU!) goes to <code>%rax</code> (or to <code>%eax</code> as the high 32 bits are unused on XNU). The fist argument goes in <code>%rdi</code>. Next goes to <code>%rsi</code>. And so on. <code>%rcx</code> is used by the kernel and its value is destroyed and that's why all functions in <code>libc.dyld</code> save it into <code>%r10</code> before making syscalls (similarly to the <code>kernel_trap</code> macro from <code>syscall_sw.h</code>).</p> <p>Third, code sections in Mach-O binaries are called <code>__text</code> and not <code>.text</code> as in Linux ELF and also reside in the <code>__TEXT</code> segment, collectively referred as <code>(__TEXT,__text)</code> (<code>nasm</code> automatically translates <code>.text</code> as appropriate if Mach-O is selected as target object type) - see the <a href="https://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/MachORuntime/Reference/reference.html" rel="noreferrer">Mac OS X ABI Mach-O File Format Reference</a>. Even if you get the assembly instructions right, putting them in the wrong segment/section leads to bus error. You can either use the <code>.section __TEXT,__text</code> directive (see <a href="https://developer.apple.com/library/mac/#documentation/developertools/Reference/Assembler/040-Assembler_Directives/asm_directives.html" rel="noreferrer">here</a> for directive syntax) or you can also use the (simpler) <code>.text</code> directive, or you can drop it altogether since it is assumed if no <code>-n</code> option was supplied to <code>as</code> (see the manpage of <code>as</code>).</p> <p>Fourth, the default entry point for the Mach-O <code>ld</code> is called <code>start</code> (although, as you've already figured it out, it can be changed via the <code>-e</code> linker option).</p> <p>Given all the above you should modify your assembler source to read as follows:</p> <pre><code>; You could also add one of the following directives for completeness ; .text ; or ; .section __TEXT,__text .globl start start: movl $0x2000001, %eax movl $32, %edi syscall </code></pre> <p>Here it is, working as expected:</p> <pre><code>$ as -o exit.o exit.s; ld -o exit exit.o $ ./exit; echo $? 32 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload