ARM Darwin assembly - search for system calls (possibly a tutorial)

So, I started building programming. It's pretty simple in my Ubuntu field: using NASMamd GNU ld, I was able to write more or less complex HelloWorld-style programs in half an hour. But when it comes to the iPhone, it is so complicated. First of all, I have JB'en iPhone 3G on firmware 4.2.1, which means that I am using the Darwin v10 core ARM port. Secondly. I have to use GNU since there is no NASM for iPhone: the built-in toolchain (both Xcode on Mac OS X and openware tooolchain on Linux) uses GCC. Therefore, I gathered basic information about: - how to write an assembly in GNU as a language; - What are the basic ARM instructions, registers, memory access.

But even HelloWorld requires kernel calls to write to stdout. My question is: which kernel should be used and how (which arguments go there); I have to use the swi # ARM instruction, right?

So, can you post any info / links to tutorials or anyone with ARM Darwin Hello world asm code?

Currently I can do this:

;Hello World for Linux and NASM
section data
hello db "Hello World"
helloLen equ $ - hello

section text
global _start
_start:
    mov eax, 4 ; sys_write
    mov ebx, 1 ; to stdout
    mov ecx, hello ; address of string
    mov edx, helloLen ; value (because of eq!!!) of strLen
    int 0x80 ; call awesome Linux kernel

    mov eax, 1 ; sys_exit
    mov ebx, 0 ; "return 0; " if you like C
    int 0x80 ; call kernel to end program

on ARM, however, I could only do this:

.text
start:
    mov r0, #0
    mov r1, #234
    add r2, r0, r1
@all mov and add and other stuff works fine
    swi #0xc00
@all that I get is Bad system call error

So, anyone?

+3
source share
2 answers

Here's how libc (libSystem) does it:

; ssize_t read(int, void *, size_t)
                EXPORT _read
_read
                MOV     R12, #3         ; SYS_read
                SVC     0x80 ; ''      ; do a syscall
                BCC     _ok             ; carry clear = no error
                LDR     R12, =(cerror_ptr - . - 8) ; otherwise call error handler
                LDR     R12, [PC,R12]   ; load pointer
                B       _call_error
                DCD cerror_ptr - .
_call_error                              
                BX      R12 ; cerror    ; jump to it (error number is in R0)
_ok
                BX      LR              ; return to caller
; End of function _read

i.e:.

  • The system call number is in R12 (see sys / syscall.h).
  • The system call command is SVC 0x80 (SWI 0x80).
  • Other parameters correspond to ABI (R0-R3, then stack).
  • , R0.
+1

, , , ,

http://blog.softboysxp.com/post/7888230192/a-minimal-168-byte-mach-o-arm-executable-for-ios

.text
.globl start

start:
mov r2, #14
adr r1, hello_str
mov r0, #1
mov r12, #4
swi 0x80

mov r0, #0
mov r12, #1
swi 0x80

hello_str:
.ascii  "Hello, World!\n"

compile:
as new.asm -o new.o
ld new.o -o new
./new
+1

Source: https://habr.com/ru/post/1778856/


All Articles