osx - CoreFoundation functions in Mac OS X Assembly -


i have following code in assembly (assembled clang)

updated thought cause alignment; still doesn't.

updated again; code still seg faults suggestions(thank suggestions though stephen canon ) tried subtract 4, 8, 12 didn't work same stack realignment issue updated more info.

.globl _main .data _hw:    .asciz  "hello world\n\0"  .text _main:  push 8      # 4 bytes push _hw    # 4 bytes push 0      # 4 bytes ##https://developer.apple.com/library/mac/documentation/corefoundation/reference/cfstringref/reference/reference.html#//apple_ref/c/func/cfstringcreatewithcstring call _cfstringcreatewithcstring # 4 bytes ## push cfstr return value in eax sub esp, 8  # 8 bytes push eax    # 4 bytes ##https://developer.apple.com/library/ios/documentation/corefoundation/reference/cftyperef/reference/reference.html#//apple_ref/c/func/cfshow call _cfshow # 4 bytes add esp, 8 # remove padding stack pointer mov eax, 99 ret 

program execution

start of main stack empty

=============== (0xffff) |             | |    stack    | |             | =============== 

the push 8, address of _hw, , 0 call _cfstringcreatewithcstring. looks like

=============== (0xffff) |      8      | |-------------- (0xfffb) 4 bytes 8 |  hw address | |-------------- (0xfff7) 4 bytes address of hw |      0      | |-------------- (0xfff3) 4 bytes 0 (null) |    call     | --------------- (0xffef) 4 bytes address return after call (eip?) 8 on x64? 

then cfstringcreatewithcstring called saving return address (popping off call correct?), popping arguments off stack , jumping saved eip address after executing , putting return value in eax.

after stack looks like

===============  0xffff |             | |    stack    | |             | =============== 

i subtract 8 esp looks like

=============== (0xffff) |   padding   | |   8 bytes   | |-------------- (0xfff7) (esp) |             | =============== 

i push eax cfstringcreatewithcstring stack looks like

=============== (0xffff) |   padding   | |   8 bytes   | |-------------- (0xfff7) # 8 bytes padding subtracting stack counter |     eax     | |-------------- (0xfff3) # 4 bytes eax, return last call, or 8 bytes on x64? |    call     | |-------------- (0xffef) # 4 bytes return after call (eip?) =============== 

after call cfshow (and popping off arguments , address call) stack looks this

=============== (0xffff) |   padding   | |   8 bytes   | |-------------- (0xfff7) # 8 bytes padding subtracting stack counter, cfshow doesn't touch expects 4 byte address  

i add 8 bytes esp removing padding looks this

=============== (0xffff) |             | |    stack    | |             | =============== 

correct?

this type run code, need change because processor 64 bit?

macbookpro:helloworld user$ cat hand.s .globl _main .data _hw:    .asciz  "hello world\n\0"  .text _main:  push 8      # 4 bytes push _hw    # 4 bytes push 0      # 4 bytes call _cfstringcreatewithcstring ## push cfstr return value in eax sub esp, 8  # 8 bytes push eax    # 12 bytes call _cfshow mov eax, 99 ret 

my compilation steps, using clang's built in assembler (gas think) , ld. on mac os x 64 bit mountain lion

macbookpro:helloworld user$ clang -cc1as -filetype obj -mllvm --x86-asm-syntax=intel -o hand.o hand.s 

link corefoundation

macbookpro:helloworld user$ ld -macosx_version_min 10.8.0 -o hand hand.o -lsystem -framework corefoundation 

run executable.

macbookpro:helloworld user$ ./hand segmentation fault: 11 macbookpro:helloworld user$  

results in following error

segmentation fault: 11 

i can't test code , not familiar assembly syntax, seems wrong me...

imho, proper code should be:

.globl _main .data _hw:    .asciz  "hello world\n\0"  .text _main:           push  8         push  _hw       push  0         call  _cfstringcreatewithcstring       add   esp, 12  ; remove arguments stack...      sub   esp, 8   ; align stack next call...      push  eax      call  _cfshow      add   esp, 12      mov   eax, 99     ret 

note, add/sub sequence:

    add   esp, 12  ; remove arguments stack...      sub   esp, 8   ; align stack next call... 

i wrote way in order clean logic, of course can reduced to:

    add   esp, 4  

so, final code:

.globl _main .data _hw:    .asciz  "hello world\n\0"  .text _main:           push  8         push  _hw       push  0         call  _cfstringcreatewithcstring       add   esp, 4   ; clean arguments , align stack next call...      push  eax      call  _cfshow      add   esp, 12      mov   eax, 99     ret 

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -